2018-12-28 22:00:28 +00:00
|
|
|
// Copyright (C) 2018 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
2022-01-15 18:40:12 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
|
|
|
|
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
|
|
|
// <https://mozilla.org/MPL/2.0/>.
|
2018-12-28 22:00:28 +00:00
|
|
|
//
|
2022-01-15 18:40:12 +00:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2018-12-28 22:00:28 +00:00
|
|
|
|
2021-09-08 12:33:31 +00:00
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
2018-12-28 22:00:28 +00:00
|
|
|
use gst::prelude::*;
|
2019-03-22 14:08:54 +00:00
|
|
|
use gst::EventView;
|
2020-11-22 17:21:45 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2018-12-28 22:00:28 +00:00
|
|
|
use rand::{Rng, SeedableRng};
|
2019-03-22 14:08:54 +00:00
|
|
|
use std::path::PathBuf;
|
2018-12-28 22:00:28 +00:00
|
|
|
|
|
|
|
fn init() {
|
2019-07-11 13:41:42 +00:00
|
|
|
use std::sync::Once;
|
|
|
|
static INIT: Once = Once::new();
|
2018-12-28 22:00:28 +00:00
|
|
|
|
|
|
|
INIT.call_once(|| {
|
|
|
|
gst::init().unwrap();
|
2019-01-21 20:13:07 +00:00
|
|
|
gstrsclosedcaption::plugin_register_static().expect("mccparse test");
|
2018-12-28 22:00:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Randomized test passing buffers of arbitrary sizes to the parser
|
|
|
|
#[test]
|
|
|
|
fn test_parse() {
|
|
|
|
init();
|
|
|
|
let mut data = include_bytes!("captions-test_708.mcc").as_ref();
|
|
|
|
|
|
|
|
let mut rnd = if let Ok(seed) = std::env::var("MCC_PARSE_TEST_SEED") {
|
|
|
|
rand::rngs::SmallRng::seed_from_u64(
|
|
|
|
seed.parse::<u64>()
|
|
|
|
.expect("MCC_PARSE_TEST_SEED has to contain a 64 bit integer seed"),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let seed = rand::random::<u64>();
|
|
|
|
println!("seed {}", seed);
|
|
|
|
rand::rngs::SmallRng::seed_from_u64(seed)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut h = gst_check::Harness::new("mccparse");
|
|
|
|
|
2019-01-21 15:34:44 +00:00
|
|
|
h.set_src_caps_str("application/x-mcc, version=(int) 1");
|
|
|
|
|
2018-12-28 22:00:28 +00:00
|
|
|
let mut input_len = 0;
|
|
|
|
let mut output_len = 0;
|
|
|
|
let mut checksum = 0u32;
|
|
|
|
let mut expected_timecode = None;
|
|
|
|
|
|
|
|
while !data.is_empty() {
|
|
|
|
let l = if data.len() == 1 {
|
|
|
|
1
|
|
|
|
} else {
|
2021-01-09 10:24:28 +00:00
|
|
|
rnd.gen_range(1..=data.len())
|
2018-12-28 22:00:28 +00:00
|
|
|
};
|
|
|
|
let buf = gst::Buffer::from_mut_slice(Vec::from(&data[0..l]));
|
2021-04-12 12:49:54 +00:00
|
|
|
input_len += buf.size();
|
2018-12-28 22:00:28 +00:00
|
|
|
assert_eq!(h.push(buf), Ok(gst::FlowSuccess::Ok));
|
|
|
|
while let Some(buf) = h.try_pull() {
|
2021-04-12 12:49:54 +00:00
|
|
|
output_len += buf.size();
|
2018-12-28 22:00:28 +00:00
|
|
|
checksum = checksum.wrapping_add(
|
|
|
|
buf.map_readable()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.fold(0u32, |s, v| s.wrapping_add(*v as u32)),
|
|
|
|
);
|
|
|
|
|
|
|
|
let tc_meta = buf
|
2021-04-20 12:58:11 +00:00
|
|
|
.meta::<gst_video::VideoTimeCodeMeta>()
|
2018-12-28 22:00:28 +00:00
|
|
|
.expect("No timecode meta");
|
|
|
|
if let Some(ref timecode) = expected_timecode {
|
2021-04-12 12:49:54 +00:00
|
|
|
assert_eq!(&tc_meta.tc(), timecode);
|
2018-12-28 22:00:28 +00:00
|
|
|
} else {
|
2021-04-12 12:49:54 +00:00
|
|
|
expected_timecode = Some(tc_meta.tc());
|
2018-12-28 22:00:28 +00:00
|
|
|
}
|
2019-12-22 09:18:30 +00:00
|
|
|
if let Some(ref mut tc) = expected_timecode {
|
|
|
|
tc.increment_frame();
|
|
|
|
}
|
2018-12-28 22:00:28 +00:00
|
|
|
}
|
|
|
|
data = &data[l..];
|
|
|
|
}
|
|
|
|
|
2020-06-24 22:33:57 +00:00
|
|
|
h.push_event(gst::event::Eos::new());
|
2018-12-28 22:00:28 +00:00
|
|
|
while let Some(buf) = h.try_pull() {
|
2021-04-12 12:49:54 +00:00
|
|
|
output_len += buf.size();
|
2018-12-28 22:00:28 +00:00
|
|
|
checksum = checksum.wrapping_add(
|
|
|
|
buf.map_readable()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.fold(0u32, |s, v| s.wrapping_add(*v as u32)),
|
|
|
|
);
|
|
|
|
|
|
|
|
let tc_meta = buf
|
2021-04-20 12:58:11 +00:00
|
|
|
.meta::<gst_video::VideoTimeCodeMeta>()
|
2018-12-28 22:00:28 +00:00
|
|
|
.expect("No timecode meta");
|
|
|
|
if let Some(ref timecode) = expected_timecode {
|
2021-04-12 12:49:54 +00:00
|
|
|
assert_eq!(&tc_meta.tc(), timecode);
|
2018-12-28 22:00:28 +00:00
|
|
|
} else {
|
2021-04-12 12:49:54 +00:00
|
|
|
expected_timecode = Some(tc_meta.tc());
|
2018-12-28 22:00:28 +00:00
|
|
|
}
|
2019-12-22 09:18:30 +00:00
|
|
|
if let Some(ref mut tc) = expected_timecode {
|
|
|
|
tc.increment_frame();
|
|
|
|
}
|
2018-12-28 22:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert!(expected_timecode.is_some());
|
2019-12-22 09:18:30 +00:00
|
|
|
assert_eq!(input_len, 28_818);
|
|
|
|
assert_eq!(output_len, 42_383);
|
|
|
|
assert_eq!(checksum, 3_988_480);
|
2018-12-28 22:00:28 +00:00
|
|
|
|
|
|
|
let caps = h
|
2021-04-12 12:49:54 +00:00
|
|
|
.sinkpad()
|
2018-12-28 22:00:28 +00:00
|
|
|
.expect("harness has no sinkpad")
|
2021-04-12 12:49:54 +00:00
|
|
|
.current_caps()
|
2018-12-28 22:00:28 +00:00
|
|
|
.expect("pad has no caps");
|
|
|
|
assert_eq!(
|
|
|
|
caps,
|
|
|
|
gst::Caps::builder("closedcaption/x-cea-708")
|
2021-11-06 07:34:10 +00:00
|
|
|
.field("format", "cdp")
|
|
|
|
.field("framerate", gst::Fraction::new(30000, 1001))
|
2018-12-28 22:00:28 +00:00
|
|
|
.build()
|
|
|
|
);
|
|
|
|
}
|
2019-03-22 14:08:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pull() {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
path.push("tests/captions-test_708.mcc");
|
|
|
|
|
|
|
|
let mut h = gst_check::Harness::new_parse(&format!("filesrc location={:?} ! mccparse", path));
|
|
|
|
|
|
|
|
h.play();
|
|
|
|
|
|
|
|
/* Let's first pull until EOS */
|
|
|
|
loop {
|
|
|
|
let mut done = false;
|
|
|
|
|
|
|
|
while h.events_in_queue() != 0 {
|
|
|
|
let event = h.pull_event();
|
|
|
|
|
2019-12-18 05:50:10 +00:00
|
|
|
if let Ok(event) = event {
|
2019-03-22 14:08:54 +00:00
|
|
|
match event.view() {
|
|
|
|
EventView::Eos(_) => {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while h.buffers_in_queue() != 0 {
|
|
|
|
let _ = h.pull();
|
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now seek and check that we receive buffers with appropriate PTS */
|
2020-06-24 22:33:57 +00:00
|
|
|
h.push_upstream_event(gst::event::Seek::new(
|
|
|
|
1.0,
|
|
|
|
gst::SeekFlags::FLUSH,
|
|
|
|
gst::SeekType::Set,
|
2022-07-04 16:04:11 +00:00
|
|
|
gst::ClockTime::SECOND,
|
2020-06-24 22:33:57 +00:00
|
|
|
gst::SeekType::Set,
|
2022-10-17 17:48:43 +00:00
|
|
|
2.seconds(),
|
2020-06-24 22:33:57 +00:00
|
|
|
));
|
2019-03-22 14:08:54 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut done = false;
|
|
|
|
|
|
|
|
while h.buffers_in_queue() != 0 {
|
2019-12-18 05:50:10 +00:00
|
|
|
if let Ok(buffer) = h.pull() {
|
2021-06-04 17:06:24 +00:00
|
|
|
let pts = buffer.pts().unwrap();
|
2022-10-17 17:48:43 +00:00
|
|
|
assert!(pts > gst::ClockTime::SECOND && pts < 2.seconds());
|
2019-03-22 14:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while h.events_in_queue() != 0 {
|
|
|
|
let event = h.pull_event();
|
|
|
|
|
2019-12-18 05:50:10 +00:00
|
|
|
if let Ok(event) = event {
|
2019-03-22 14:08:54 +00:00
|
|
|
match event.view() {
|
|
|
|
EventView::Eos(_) => {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|