Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tttocea608.rs « tests « closedcaption « video - gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 34fd3b3b1c79a1a0f26a930c242383cf0f034c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (C) 2020 Mathieu Duponchelle <mathieu@centricular.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
// Boston, MA 02110-1335, USA.

#[macro_use]
extern crate pretty_assertions;
use gst::EventView;

fn init() {
    use std::sync::Once;
    static INIT: Once = Once::new();

    INIT.call_once(|| {
        gst::init().unwrap();
        gstrsclosedcaption::plugin_register_static().unwrap();
    });
}

fn new_timed_buffer<T: AsRef<[u8]> + Send + 'static>(
    slice: T,
    timestamp: gst::ClockTime,
    duration: gst::ClockTime,
) -> gst::buffer::Buffer {
    let mut buf = gst::Buffer::from_slice(slice);
    let buf_ref = buf.get_mut().unwrap();
    buf_ref.set_pts(timestamp);
    buf_ref.set_duration(duration);
    buf
}

#[test]
fn test_non_timed_buffer() {
    init();

    let mut h = gst_check::Harness::new_parse("tttocea608 mode=pop-on");
    h.set_src_caps_str("text/x-raw");

    let inbuf = gst::Buffer::from_slice(&"Hello");

    assert_eq!(h.push(inbuf), Err(gst::FlowError::Error));
}

/* Check translation of a simple string */
#[test]
fn test_one_timed_buffer_and_eos() {
    init();

    let mut h = gst_check::Harness::new_parse("tttocea608 mode=pop-on");
    h.set_src_caps_str("text/x-raw");

    while h.events_in_queue() != 0 {
        let _event = h.pull_event().unwrap();
    }

    let inbuf = new_timed_buffer(&"Hello", gst::SECOND, gst::SECOND);

    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    let expected: [(gst::ClockTime, gst::ClockTime, [u8; 2usize]); 11] = [
        (700_000_000.into(), 33_333_333.into(), [0x94, 0x20]), /* resume_caption_loading */
        (733_333_333.into(), 33_333_334.into(), [0x94, 0x20]), /* control doubled */
        (766_666_667.into(), 33_333_333.into(), [0x94, 0xae]), /* erase_non_displayed_memory */
        (800_000_000.into(), 33_333_333.into(), [0x94, 0xae]), /* control doubled */
        (833_333_333.into(), 33_333_334.into(), [0x94, 0x40]), /* preamble */
        (866_666_667.into(), 33_333_333.into(), [0x94, 0x40]), /* control doubled */
        (900_000_000.into(), 33_333_333.into(), [0xc8, 0xe5]), /* H e */
        (933_333_333.into(), 33_333_334.into(), [0xec, 0xec]), /* l l */
        (966_666_667.into(), 33_333_333.into(), [0xef, 0x80]), /* o, nil */
        (gst::SECOND, 33_333_333.into(), [0x94, 0x2f]),        /* end_of_caption */
        (1_033_333_333.into(), 33_333_334.into(), [0x94, 0x2f]), /* control doubled */
    ];

    for (i, e) in expected.iter().enumerate() {
        let outbuf = h.try_pull().unwrap();

        assert_eq!(
            e.0,
            outbuf.get_pts(),
            "Unexpected PTS for {}th buffer",
            i + 1
        );
        assert_eq!(
            e.1,
            outbuf.get_duration(),
            "Unexpected duration for {}th buffer",
            i + 1
        );

        let data = outbuf.map_readable().unwrap();
        assert_eq!(e.2, &*data);
    }

    assert_eq!(h.buffers_in_queue(), 0);

    h.push_event(gst::Event::new_eos().build());

    /* Check that we do receive an erase_display */
    assert_eq!(h.buffers_in_queue(), 2);
    while h.buffers_in_queue() > 0 {
        let outbuf = h.try_pull().unwrap();
        let data = outbuf.map_readable().unwrap();
        assert_eq!(&*data, &[0x94, 0x2c]);
    }

    assert_eq!(h.events_in_queue() >= 1, true);

    /* Gap event, we ignore those here and test them separately */
    while h.events_in_queue() > 1 {
        let _event = h.pull_event().unwrap();
    }

    let event = h.pull_event().unwrap();
    assert_eq!(event.get_type(), gst::EventType::Eos);
}

/* Here we test that the erase_display_memory control code
 * gets inserted at the correct moment, when there's enough
 * of an interval between two buffers
 */
#[test]
fn test_erase_display_memory_non_spliced() {
    init();

    let mut h = gst_check::Harness::new_parse("tttocea608 mode=pop-on");
    h.set_src_caps_str("text/x-raw");

    while h.events_in_queue() != 0 {
        let _event = h.pull_event().unwrap();
    }

    let inbuf = new_timed_buffer(&"Hello", 1_000_000_000.into(), gst::SECOND);
    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    let inbuf = new_timed_buffer(&"World", 3_000_000_000.into(), gst::SECOND);
    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    let mut erase_display_buffers = 0;

    while h.buffers_in_queue() > 0 {
        let outbuf = h.pull().unwrap();

        if outbuf.get_pts() == 2_000_000_000.into() || outbuf.get_pts() == 2_033_333_333.into() {
            let data = outbuf.map_readable().unwrap();
            assert_eq!(&*data, &[0x94, 0x2c]);
            erase_display_buffers += 1;
        }
    }

    assert_eq!(erase_display_buffers, 2);
}

/* Here we test that the erase_display_memory control code
 * gets spliced in with the byte pairs of the following buffer
 * when there's not enough of an interval between them.
 */
#[test]
fn test_erase_display_memory_spliced() {
    init();

    let mut h = gst_check::Harness::new_parse("tttocea608 mode=pop-on");
    h.set_src_caps_str("text/x-raw");

    while h.events_in_queue() != 0 {
        let _event = h.pull_event().unwrap();
    }

    let inbuf = new_timed_buffer(&"Hello", 1_000_000_000.into(), gst::SECOND);
    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    let inbuf = new_timed_buffer(&"World", 2_200_000_000.into(), gst::SECOND);
    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    let mut erase_display_buffers = 0;
    let mut prev_pts: gst::ClockTime = 0.into();

    while h.buffers_in_queue() > 0 {
        let outbuf = h.pull().unwrap();

        /* Check that our timestamps are strictly ascending */
        assert!(outbuf.get_pts() > prev_pts);

        if outbuf.get_pts() == 2_000_000_000.into() || outbuf.get_pts() == 2_033_333_333.into() {
            let data = outbuf.map_readable().unwrap();
            assert_eq!(&*data, &[0x94, 0x2c]);
            erase_display_buffers += 1;
        }

        prev_pts = outbuf.get_pts();
    }

    assert_eq!(erase_display_buffers, 2);
}

/* Here we test that the erase_display_memory control code
 * gets output "in time" when we receive gaps
 */
#[test]
fn test_erase_display_memory_gaps() {
    init();

    let mut h = gst_check::Harness::new_parse("tttocea608 mode=pop-on");
    h.set_src_caps_str("text/x-raw");

    while h.events_in_queue() != 0 {
        let _event = h.pull_event().unwrap();
    }

    let inbuf = new_timed_buffer(&"Hello", 1_000_000_000.into(), gst::SECOND);
    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    /* Let's first push a gap that doesn't leave room for our two control codes */
    let gap_event = gst::Event::new_gap(2 * gst::SECOND, 2_533_333_333.into()).build();
    assert_eq!(h.push_event(gap_event), true);
    let mut erase_display_buffers = 0;

    while h.buffers_in_queue() > 0 {
        let outbuf = h.pull().unwrap();

        let data = outbuf.map_readable().unwrap();
        if *data == [0x94, 0x2c] {
            erase_display_buffers += 1;
        }
    }

    assert_eq!(erase_display_buffers, 0);

    let gap_event = gst::Event::new_gap(4_533_333_333.into(), 1.into()).build();
    assert_eq!(h.push_event(gap_event), true);

    while h.buffers_in_queue() > 0 {
        let outbuf = h.pull().unwrap();

        let data = outbuf.map_readable().unwrap();
        if *data == [0x94, 0x2c] {
            erase_display_buffers += 1;
        }
    }

    assert_eq!(erase_display_buffers, 2);
}

/* Here we verify that the element outputs a continuous stream
 * with gap events
 */
#[test]
fn test_output_gaps() {
    init();

    let mut h = gst_check::Harness::new_parse("tttocea608 mode=pop-on");
    h.set_src_caps_str("text/x-raw");

    while h.events_in_queue() != 0 {
        let _event = h.pull_event().unwrap();
    }

    let inbuf = new_timed_buffer(&"Hello", 1_000_000_000.into(), gst::SECOND);
    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    let inbuf = new_timed_buffer(&"World", 3_000_000_000.into(), gst::SECOND);
    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    assert_eq!(h.events_in_queue(), 3);

    /* One gap from the start of the segment to the first
     * buffer, another from the end_of_caption control code for
     * the first buffer to its erase_display control code,
     * then one gap from erase_display to the beginning
     * of the second buffer
     */
    let expected: [(gst::ClockTime, gst::ClockTime); 3] = [
        (0.into(), 700_000_000.into()),
        (1_066_666_667.into(), 933_333_333.into()),
        (2_066_666_667.into(), 633_333_333.into()),
    ];

    for e in &expected {
        let event = h.pull_event().unwrap();

        assert_eq!(event.get_type(), gst::EventType::Gap);

        if let EventView::Gap(ev) = event.view() {
            let (timestamp, duration) = ev.get();
            assert_eq!(e.0, timestamp);
            assert_eq!(e.1, duration);
        }
    }
}

#[test]
fn test_one_timed_buffer_and_eos_roll_up2() {
    init();

    let mut h = gst_check::Harness::new_parse("tttocea608 mode=roll-up2");
    h.set_src_caps_str("text/x-raw");

    while h.events_in_queue() != 0 {
        let _event = h.pull_event().unwrap();
    }

    let inbuf = new_timed_buffer(&"Hello", gst::SECOND, gst::SECOND);
    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    let inbuf = new_timed_buffer(&"World", gst::SECOND, 1.into());
    assert_eq!(h.push(inbuf), Ok(gst::FlowSuccess::Ok));

    let expected: [(gst::ClockTime, gst::ClockTime, [u8; 2usize]); 12] = [
        (1_000_000_000.into(), 33_333_333.into(), [0x94, 0x2c]), /* erase_display_memory */
        (1_033_333_333.into(), 33_333_334.into(), [0x94, 0x2c]), /* control doubled */
        (1_066_666_667.into(), 33_333_333.into(), [0x94, 0x25]), /* roll_up_2 */
        (1_100_000_000.into(), 33_333_333.into(), [0x94, 0x25]), /* control doubled */
        (1_133_333_333.into(), 33_333_334.into(), [0x94, 0xe0]), /* preamble */
        (1_166_666_667.into(), 33_333_333.into(), [0x94, 0xe0]), /* control doubled */
        (1_200_000_000.into(), 33_333_333.into(), [0xc8, 0xe5]), /* H e */
        (1_233_333_333.into(), 33_333_334.into(), [0xec, 0xec]), /* l l */
        (1_266_666_667.into(), 33_333_333.into(), [0xef, 0x80]), /* o, nil */
        (2_000_000_000.into(), 0.into(), [0x20, 0x57]),          /* SPACE, W */
        (2_000_000_000.into(), 0.into(), [0xef, 0xf2]),          /* o, r */
        (2_000_000_000.into(), 0.into(), [0xec, 0x64]),          /* l, d */
    ];

    for (i, e) in expected.iter().enumerate() {
        let outbuf = h.try_pull().unwrap();

        assert_eq!(
            e.0,
            outbuf.get_pts(),
            "Unexpected PTS for {}th buffer",
            i + 1
        );
        assert_eq!(
            e.1,
            outbuf.get_duration(),
            "Unexpected duration for {}th buffer",
            i + 1
        );

        let data = outbuf.map_readable().unwrap();
        assert_eq!(e.2, &*data);
    }

    assert_eq!(h.buffers_in_queue(), 0);

    h.push_event(gst::Event::new_eos().build());

    let expected_gaps: [(gst::ClockTime, gst::ClockTime); 2] = [
        (0.into(), 1_000_000_000.into()),
        (1_300_000_000.into(), 700_000_000.into()),
    ];

    for e in &expected_gaps {
        let event = h.pull_event().unwrap();

        assert_eq!(event.get_type(), gst::EventType::Gap);

        if let EventView::Gap(ev) = event.view() {
            let (timestamp, duration) = ev.get();
            assert_eq!(e.0, timestamp);
            assert_eq!(e.1, duration);
        }
    }

    assert_eq!(h.events_in_queue(), 1);

    let event = h.pull_event().unwrap();
    assert_eq!(event.get_type(), gst::EventType::Eos);
}