From be7da027f875d5ca62c8f23cc782d9ad891195fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Sun, 5 May 2024 01:49:19 +0100 Subject: [PATCH] rtp: klv: add some basic tests Part-of: --- net/rtp/src/klv/mod.rs | 4 + net/rtp/src/klv/tests/.gitattributes | 1 + net/rtp/src/klv/tests/day-flight.klv | Bin 0 -> 977 bytes net/rtp/src/klv/tests/mod.rs | 3 + net/rtp/src/klv/tests/tests.rs | 232 +++++++++++++++++++++++++++ 5 files changed, 240 insertions(+) create mode 100644 net/rtp/src/klv/tests/.gitattributes create mode 100644 net/rtp/src/klv/tests/day-flight.klv create mode 100644 net/rtp/src/klv/tests/mod.rs create mode 100644 net/rtp/src/klv/tests/tests.rs diff --git a/net/rtp/src/klv/mod.rs b/net/rtp/src/klv/mod.rs index b6b31996..24c4daa4 100644 --- a/net/rtp/src/klv/mod.rs +++ b/net/rtp/src/klv/mod.rs @@ -4,3 +4,7 @@ pub mod depay; pub mod pay; mod klv_utils; + +#[allow(clippy::module_inception)] +#[cfg(test)] +mod tests; diff --git a/net/rtp/src/klv/tests/.gitattributes b/net/rtp/src/klv/tests/.gitattributes new file mode 100644 index 00000000..b73979e3 --- /dev/null +++ b/net/rtp/src/klv/tests/.gitattributes @@ -0,0 +1 @@ +*.klv binary diff --git a/net/rtp/src/klv/tests/day-flight.klv b/net/rtp/src/klv/tests/day-flight.klv new file mode 100644 index 0000000000000000000000000000000000000000..a6bd5472bee66b6e82ab719f3f165b5bdb9a6448 GIT binary patch literal 977 zcmZ|OUr3Wt7{~GVJUhNdv;ALgY_TP2`G*O$i>+ZGrj|0RH_{->%v?nqbN#yrq_!9` zXq8=fW#NUqMHivemRS@7gCaGe~oKaZTbIzz;)O-26`|y3v5jC}LP!pmd zDnbN7hz`RFfd(%*RTtuogp@#oPXzP%QXrCA)x4vlOw-yQKHPumSf8`2b+^a8iuz~f zA86?0&ET>Y9Bn!f;(8F;4b=H`ZqP`7WZ|ueE>Au0HB;g8mfqD=5XiG~>d#)RvCzp! z*Yj3T$2ag@&36rM4{>>^kbG55z}_P&5G(zUZJfAyL4>5g#LtParYoA$3R`*w{cInN}p;${RS* z*Diu3fW){WalI9ZcYC%WachyNe-{WKaplrO6aATUv>|ce;|wPrWyFNUi1OkeVnSj> zd9gsOMPk_chCoSHiCDF+$y^}XMKD#Bg5Xjlt~rau)ZTd{`in%}u4`E%UHWz~jl^GH z%a9n}G{lL0oX9K6?9fqGF~KX!c_c?n mCt)VEe?VcTeintP*CQyDFd}Xg;zpt5A@vy1a^FkfT<335wDvXt literal 0 HcmV?d00001 diff --git a/net/rtp/src/klv/tests/mod.rs b/net/rtp/src/klv/tests/mod.rs new file mode 100644 index 00000000..a79d7e29 --- /dev/null +++ b/net/rtp/src/klv/tests/mod.rs @@ -0,0 +1,3 @@ +// SPDX-License-Identifier: MPL-2.0 + +mod tests; diff --git a/net/rtp/src/klv/tests/tests.rs b/net/rtp/src/klv/tests/tests.rs new file mode 100644 index 00000000..c5c4f3e9 --- /dev/null +++ b/net/rtp/src/klv/tests/tests.rs @@ -0,0 +1,232 @@ +// GStreamer RTP KLV Payloader / Depayloader - unit tests +// +// Copyright (C) 2024 Tim-Philipp Müller +// +// 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 +// . +// +// SPDX-License-Identifier: MPL-2.0 + +use crate::klv::klv_utils::*; +use crate::tests::{run_test_pipeline_full, ExpectedBuffer, ExpectedPacket, Source}; + +fn init() { + use std::sync::Once; + static INIT: Once = Once::new(); + + INIT.call_once(|| { + gst::init().unwrap(); + crate::plugin_register_static().expect("rtp klv test"); + }); +} + +const KLV_DATA: &[u8] = include_bytes!("day-flight.klv").as_slice(); + +pub(crate) fn parse_klv_packets(data: &[u8]) -> anyhow::Result> { + let mut packets = vec![]; + + let mut data = &data[0..]; + + while !data.is_empty() { + let size = peek_klv(data)?; + // eprintln!("KLV unit {} of size {size}", packets.len()); + packets.push(&data[0..][..size]); + data = &data[size..]; + } + + Ok(packets) +} + +fn make_buffer( + data: &'static [u8], + pts: gst::ClockTime, + duration: Option, + flags: gst::BufferFlags, +) -> gst::Buffer { + let mut buf = gst::Buffer::from_slice(data); + + let buf_ref = buf.get_mut().unwrap(); + buf_ref.set_pts(pts); + if let Some(duration) = duration { + buf_ref.set_duration(duration); + } + buf_ref.set_flags(flags); + + buf +} + +// test_klv_pay_depay +// +// Check basic payloading/depayloading +// +#[test] +fn test_klv_pay_depay() { + let klv_packets = parse_klv_packets(KLV_DATA).unwrap(); + + init(); + + let input_caps = gst::Caps::builder("meta/x-klv") + .field("parsed", true) + .build(); + + let mut input_buffers = vec![]; + + for (i, klv) in klv_packets.iter().enumerate() { + input_buffers.push(make_buffer( + klv, + gst::ClockTime::from_seconds(i as u64), + gst::ClockTime::NONE, + if i == 0 { + gst::BufferFlags::DISCONT + } else { + gst::BufferFlags::empty() + }, + )); + } + + let mut expected_pay = vec![]; + + for (i, _klv) in klv_packets.iter().enumerate() { + let expected_flags = match i { + 0 => gst::BufferFlags::DISCONT | gst::BufferFlags::MARKER, + _ => gst::BufferFlags::MARKER, + }; + expected_pay.push(vec![ExpectedPacket::builder() + .pts(gst::ClockTime::from_seconds(i as u64)) + .flags(expected_flags) + .pt(96) + .rtp_time(i as u32 * 90_000) + .marker_bit(true) + .build()]); + } + + let mut expected_depay = vec![]; + + for (i, _klv) in klv_packets.into_iter().enumerate() { + let expected_flags = match i { + 0 => gst::BufferFlags::DISCONT, + _ => gst::BufferFlags::empty(), + }; + let expected_size = match i { + 0..=4 => 163, + 5 => 162, + _ => unreachable!(), + }; + expected_depay.push(vec![ExpectedBuffer::builder() + .pts(gst::ClockTime::from_seconds(i as u64)) + .size(expected_size) + .flags(expected_flags) + .build()]); + } + + let expected_output_caps = input_caps.clone(); + + run_test_pipeline_full( + Source::Buffers(input_caps, input_buffers), + "rtpklvpay2", + "rtpklvdepay2", + expected_pay, + expected_depay, + Some(expected_output_caps), + ); +} + +// test_klv_pay_depay_fragmented +// +// Check basic payloading/depayloading with fragmentated payloads +// +#[test] +fn test_klv_pay_depay_fragmented() { + let klv_packets = parse_klv_packets(KLV_DATA).unwrap(); + + init(); + + let input_caps = gst::Caps::builder("meta/x-klv") + .field("parsed", true) + .build(); + + let mut input_buffers = vec![]; + + for (i, klv) in klv_packets.iter().enumerate() { + input_buffers.push(make_buffer( + klv, + gst::ClockTime::from_seconds(i as u64), + gst::ClockTime::NONE, + if i == 0 { + gst::BufferFlags::DISCONT + } else { + gst::BufferFlags::empty() + }, + )); + } + + let mut expected_pay = vec![]; + + for (i, _klv) in klv_packets.iter().enumerate() { + expected_pay.push(vec![ + ExpectedPacket::builder() + .pts(gst::ClockTime::from_seconds(i as u64)) + .flags(if i == 0 { + gst::BufferFlags::DISCONT + } else { + gst::BufferFlags::empty() + }) + .pt(96) + .rtp_time(i as u32 * 90_000) + .marker_bit(false) + .build(), + ExpectedPacket::builder() + .pts(gst::ClockTime::from_seconds(i as u64)) + .flags(gst::BufferFlags::empty()) + .pt(96) + .rtp_time(i as u32 * 90_000) + .marker_bit(false) + .build(), + ExpectedPacket::builder() + .pts(gst::ClockTime::from_seconds(i as u64)) + .flags(gst::BufferFlags::empty()) + .pt(96) + .rtp_time(i as u32 * 90_000) + .marker_bit(false) + .build(), + ExpectedPacket::builder() + .pts(gst::ClockTime::from_seconds(i as u64)) + .flags(gst::BufferFlags::MARKER) + .pt(96) + .rtp_time(i as u32 * 90_000) + .marker_bit(true) + .build(), + ]); + } + + let mut expected_depay = vec![]; + + for (i, _klv) in klv_packets.into_iter().enumerate() { + let expected_flags = match i { + 0 => gst::BufferFlags::DISCONT, + _ => gst::BufferFlags::empty(), + }; + let expected_size = match i { + 0..=4 => 163, + 5 => 162, + _ => unreachable!(), + }; + expected_depay.push(vec![ExpectedBuffer::builder() + .pts(gst::ClockTime::from_seconds(i as u64)) + .size(expected_size) + .flags(expected_flags) + .build()]); + } + + let expected_output_caps = input_caps.clone(); + + run_test_pipeline_full( + Source::Buffers(input_caps, input_buffers), + "rtpklvpay2 mtu=60", + "rtpklvdepay2", + expected_pay, + expected_depay, + Some(expected_output_caps), + ); +}