2021-06-10 10:39:27 +04:00
|
|
|
// Copyright (C) 2021 Scott Lamb <slamb@slamb.org>
|
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
|
|
use std::num::NonZeroU16;
|
|
|
|
|
|
|
|
use criterion::{criterion_group, criterion_main, Criterion};
|
2021-08-28 03:30:55 +04:00
|
|
|
use retina::client::{rtp::InorderParser, Timeline};
|
2021-06-11 00:50:20 +04:00
|
|
|
use retina::codec::{CodecItem, Depacketizer};
|
2021-06-10 10:39:27 +04:00
|
|
|
use std::convert::TryFrom;
|
2021-06-11 00:50:20 +04:00
|
|
|
use std::io::Write;
|
2021-06-10 10:39:27 +04:00
|
|
|
|
|
|
|
// This holds just the RTSP data portions of a session from this public endpoint.
|
|
|
|
// https://www.wowza.com/html/mobile.html
|
|
|
|
// Big Buck Bunny is (c) copyright 2008, Blender Foundation, licensed via
|
|
|
|
// Creative Commons Attribution 3.0. See https://peach.blender.org/about/
|
|
|
|
const BUNNY: &[u8] = include_bytes!("bunny.rtsp");
|
|
|
|
|
|
|
|
// TODO: it'd be nice to have a slick way of loading saved RTSP flows for testing.
|
|
|
|
// For now, this builds state via several internal interfaces.
|
2021-06-11 00:50:20 +04:00
|
|
|
fn h264_aac<F: FnMut(CodecItem) -> ()>(mut f: F) {
|
2021-06-10 10:39:27 +04:00
|
|
|
let mut remaining = BUNNY;
|
|
|
|
let mut timelines = [
|
|
|
|
Timeline::new(Some(0), 12_000, None).unwrap(),
|
|
|
|
Timeline::new(Some(0), 90_000, None).unwrap(),
|
|
|
|
];
|
|
|
|
let mut rtps = [
|
2021-08-28 03:30:55 +04:00
|
|
|
InorderParser::new(None, Some(1)),
|
|
|
|
InorderParser::new(None, Some(1)),
|
2021-06-10 10:39:27 +04:00
|
|
|
];
|
|
|
|
let mut depacketizers = [
|
|
|
|
Depacketizer::new("audio", "mpeg4-generic", 12_000, NonZeroU16::new(2), Some("profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1490")).unwrap(),
|
|
|
|
Depacketizer::new("video", "h264", 90_000, None, Some("packetization-mode=1;profile-level-id=42C01E;sprop-parameter-sets=Z0LAHtkDxWhAAAADAEAAAAwDxYuS,aMuMsg==")).unwrap(),
|
|
|
|
];
|
2021-07-08 21:52:39 +04:00
|
|
|
let conn_ctx = retina::ConnectionContext::dummy();
|
2022-05-11 20:26:00 +04:00
|
|
|
let stream_ctx = retina::StreamContext::dummy();
|
2021-08-28 03:30:55 +04:00
|
|
|
let pkt_ctx = retina::PacketContext::dummy();
|
2021-06-10 10:39:27 +04:00
|
|
|
while !remaining.is_empty() {
|
2021-06-11 00:50:20 +04:00
|
|
|
assert!(remaining.len() > 4);
|
|
|
|
assert_eq!(remaining[0], b'$');
|
|
|
|
let channel_id = remaining[1];
|
|
|
|
let len = u16::from_be_bytes([remaining[2], remaining[3]]);
|
|
|
|
let (data, after) = remaining.split_at(4 + usize::from(len));
|
|
|
|
let data = bytes::Bytes::from_static(&data[4..]);
|
|
|
|
remaining = after;
|
|
|
|
let stream_id = match channel_id {
|
2021-06-10 10:39:27 +04:00
|
|
|
0 => 0,
|
|
|
|
2 => 1,
|
|
|
|
1 | 3 => continue, // RTCP
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2021-07-08 21:52:39 +04:00
|
|
|
let pkt = match rtps[stream_id].rtp(
|
2021-08-19 23:56:54 +04:00
|
|
|
&retina::client::SessionOptions::default(),
|
2022-05-11 20:26:00 +04:00
|
|
|
&stream_ctx,
|
2022-04-02 02:30:40 +04:00
|
|
|
None,
|
2021-07-08 21:52:39 +04:00
|
|
|
&conn_ctx,
|
2021-08-28 03:30:55 +04:00
|
|
|
&pkt_ctx,
|
2021-07-08 21:52:39 +04:00
|
|
|
&mut timelines[stream_id],
|
|
|
|
stream_id,
|
|
|
|
data,
|
|
|
|
) {
|
2022-05-11 02:52:01 +04:00
|
|
|
Ok(Some(retina::client::PacketItem::Rtp(rtp))) => rtp,
|
2021-06-10 10:39:27 +04:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
depacketizers[stream_id].push(pkt).unwrap();
|
2022-04-27 00:02:10 +04:00
|
|
|
while let Some(pkt) = depacketizers[stream_id]
|
2022-05-11 20:26:00 +04:00
|
|
|
.pull(&conn_ctx, &stream_ctx)
|
2022-04-27 00:02:10 +04:00
|
|
|
.unwrap()
|
|
|
|
{
|
2021-06-11 00:50:20 +04:00
|
|
|
f(pkt);
|
|
|
|
}
|
2021-06-10 10:39:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
let mut g = c.benchmark_group("depacketize");
|
2021-06-11 00:50:20 +04:00
|
|
|
let mut w = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.open("/dev/null")
|
|
|
|
.unwrap();
|
|
|
|
let w = &mut w;
|
2021-06-10 10:39:27 +04:00
|
|
|
g.throughput(criterion::Throughput::Bytes(
|
|
|
|
u64::try_from(BUNNY.len()).unwrap(),
|
2021-06-11 00:50:20 +04:00
|
|
|
));
|
|
|
|
g.bench_function("h264_aac_discard", |b| {
|
|
|
|
b.iter(|| h264_aac(|_pkt: CodecItem| ()))
|
|
|
|
});
|
|
|
|
g.bench_function("h264_aac_writevideo", move |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
h264_aac(|pkt: CodecItem| {
|
|
|
|
let v = match pkt {
|
|
|
|
CodecItem::VideoFrame(v) => v,
|
|
|
|
_ => return,
|
|
|
|
};
|
2021-06-25 06:18:27 +04:00
|
|
|
w.write_all(&v.data()[..]).unwrap();
|
2021-06-11 00:50:20 +04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
2021-06-10 10:39:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|