2020-12-04 22:21:57 +00:00
|
|
|
// Copyright (C) 2020 Mathieu Duponchelle <mathieu@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/>.
|
2020-12-04 22:21:57 +00:00
|
|
|
//
|
2022-01-15 18:40:12 +00:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2020-12-04 22:21:57 +00:00
|
|
|
|
2021-09-08 12:33:31 +00:00
|
|
|
#![allow(clippy::single_match)]
|
2020-12-04 22:21:57 +00:00
|
|
|
|
2022-10-17 17:48:43 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
|
2020-12-04 22:21:57 +00:00
|
|
|
fn init() {
|
|
|
|
use std::sync::Once;
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
INIT.call_once(|| {
|
|
|
|
gst::init().unwrap();
|
2022-10-23 15:42:58 +00:00
|
|
|
gstjson::plugin_register_static().expect("json test");
|
2020-12-04 22:21:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_enc() {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let input = "{\"foo\":42}";
|
|
|
|
|
|
|
|
let mut h = gst_check::Harness::new("jsongstenc");
|
|
|
|
|
|
|
|
h.set_src_caps_str("application/x-json, format=test");
|
|
|
|
|
|
|
|
let buf = {
|
2021-03-25 17:44:56 +00:00
|
|
|
let mut buf = gst::Buffer::from_mut_slice(Vec::from(input));
|
2020-12-04 22:21:57 +00:00
|
|
|
let buf_ref = buf.get_mut().unwrap();
|
2022-10-17 17:48:43 +00:00
|
|
|
buf_ref.set_pts(gst::ClockTime::ZERO);
|
|
|
|
buf_ref.set_duration(2.seconds());
|
2020-12-04 22:21:57 +00:00
|
|
|
buf
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(h.push(buf), Ok(gst::FlowSuccess::Ok));
|
|
|
|
|
|
|
|
let buf = h.pull().expect("Couldn't pull buffer");
|
|
|
|
let map = buf.map_readable().expect("Couldn't map buffer readable");
|
|
|
|
assert_eq!(
|
|
|
|
std::str::from_utf8(map.as_ref()),
|
|
|
|
Ok("{\"Header\":{\"format\":\"test\"}}\n"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let buf = h.pull().expect("Couldn't pull buffer");
|
2021-05-26 17:03:09 +00:00
|
|
|
assert_eq!(buf.pts(), Some(gst::ClockTime::ZERO));
|
2022-10-17 17:48:43 +00:00
|
|
|
assert_eq!(buf.duration(), Some(2.seconds()));
|
2020-12-04 22:21:57 +00:00
|
|
|
let map = buf.map_readable().expect("Couldn't map buffer readable");
|
|
|
|
assert_eq!(
|
|
|
|
std::str::from_utf8(map.as_ref()),
|
|
|
|
Ok("{\"Buffer\":{\"pts\":0,\"duration\":2000000000,\"data\":{\"foo\":42}}}\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse() {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let input = [
|
|
|
|
"{\"Header\":{\"format\":\"test\"}}\n",
|
|
|
|
"{\"Buffer\":{\"pts\":0,\"duration\":2000000000,\"data\":{\"foo\":42}}}\n",
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut h = gst_check::Harness::new("jsongstparse");
|
|
|
|
|
|
|
|
h.set_src_caps_str("text/x-raw");
|
|
|
|
|
|
|
|
for input in &input {
|
|
|
|
let buf = gst::Buffer::from_mut_slice(Vec::from(&input[..]));
|
|
|
|
assert_eq!(h.push(buf), Ok(gst::FlowSuccess::Ok));
|
|
|
|
}
|
|
|
|
|
|
|
|
while h.events_in_queue() > 0 {
|
2021-09-08 12:33:31 +00:00
|
|
|
use gst::EventView;
|
|
|
|
|
2020-12-04 22:21:57 +00:00
|
|
|
let ev = h.pull_event().unwrap();
|
|
|
|
|
|
|
|
match ev.view() {
|
|
|
|
EventView::Caps(ev) => {
|
2021-11-06 07:34:10 +00:00
|
|
|
assert!(ev.caps().is_strictly_equal(
|
|
|
|
&gst::Caps::builder("application/x-json")
|
|
|
|
.field("format", "test")
|
|
|
|
.build()
|
|
|
|
));
|
2020-12-04 22:21:57 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let buf = h.pull().expect("Couldn't pull buffer");
|
|
|
|
let map = buf.map_readable().expect("Couldn't map buffer readable");
|
2021-05-26 17:03:09 +00:00
|
|
|
assert_eq!(buf.pts(), Some(gst::ClockTime::ZERO));
|
2022-10-17 17:48:43 +00:00
|
|
|
assert_eq!(buf.duration(), Some(2.seconds()));
|
2020-12-04 22:21:57 +00:00
|
|
|
assert_eq!(std::str::from_utf8(map.as_ref()), Ok("{\"foo\":42}"));
|
|
|
|
}
|