2018-04-05 13:00:43 +00:00
|
|
|
// Copyright (C) 2018 Sebastian Dröge <sebastian@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.
|
2022-01-15 19:18:47 +00:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2023-07-06 13:43:37 +00:00
|
|
|
use gst::glib::once_cell::sync::Lazy;
|
2018-04-05 13:00:43 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
|
2018-04-05 17:28:20 +00:00
|
|
|
use std::env;
|
2020-04-25 21:18:20 +00:00
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::thread;
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
const THROUGHPUT_PERIOD: Duration = Duration::from_secs(20);
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
pub static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
|
|
|
"ts-benchmark",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("Thread-sharing benchmarking receiver"),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2018-04-05 13:00:43 +00:00
|
|
|
fn main() {
|
|
|
|
gst::init().unwrap();
|
2022-09-07 21:10:29 +00:00
|
|
|
// Register the plugins statically:
|
|
|
|
// - The executable can be run from anywhere.
|
|
|
|
// - No risk of running against a previous version.
|
|
|
|
// - `main` can use features that rely on `static`s or `thread_local`
|
|
|
|
// such as `Context::acquire` which otherwise don't point to
|
|
|
|
// the same `static` or `thread_local`, probably because
|
|
|
|
// the shared object uses its owns and the executable, others.
|
|
|
|
gstthreadshare::plugin_register_static().unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
|
|
|
|
let args = env::args().collect::<Vec<_>>();
|
2022-03-21 11:25:48 +00:00
|
|
|
assert!(args.len() > 4);
|
2018-04-05 13:00:43 +00:00
|
|
|
let n_streams: u16 = args[1].parse().unwrap();
|
|
|
|
let source = &args[2];
|
2018-06-30 16:24:38 +00:00
|
|
|
let n_groups: u32 = args[3].parse().unwrap();
|
|
|
|
let wait: u32 = args[4].parse().unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
// Nb buffers to await before stopping.
|
2022-09-07 21:10:29 +00:00
|
|
|
let max_buffers: Option<f32> = if args.len() > 5 {
|
2022-03-21 11:25:48 +00:00
|
|
|
args[5].parse().ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let is_rtp = args.len() > 6 && (args[6] == "rtp");
|
|
|
|
|
2022-11-03 11:33:39 +00:00
|
|
|
let rtp_caps = gst::Caps::builder("application/x-rtp")
|
2022-03-21 11:25:48 +00:00
|
|
|
.field("media", "audio")
|
|
|
|
.field("payload", 8i32)
|
|
|
|
.field("clock-rate", 8000)
|
|
|
|
.field("encoding-name", "PCMA")
|
|
|
|
.build();
|
|
|
|
|
2018-04-05 13:00:43 +00:00
|
|
|
let l = glib::MainLoop::new(None, false);
|
2022-10-22 16:06:29 +00:00
|
|
|
let pipeline = gst::Pipeline::default();
|
2020-04-25 21:18:20 +00:00
|
|
|
let counter = Arc::new(AtomicU64::new(0));
|
2018-04-05 13:00:43 +00:00
|
|
|
|
|
|
|
for i in 0..n_streams {
|
2022-03-21 11:25:48 +00:00
|
|
|
let build_context = || format!("context-{}", (i as u32) % n_groups);
|
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let sink = gst::ElementFactory::make("fakesink")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("sink-{i}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("sync", false)
|
|
|
|
.property("async", false)
|
|
|
|
.property("signal-handoffs", true)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2022-03-21 11:25:48 +00:00
|
|
|
sink.connect(
|
|
|
|
"handoff",
|
|
|
|
true,
|
|
|
|
glib::clone!(@strong counter => move |_| {
|
|
|
|
let _ = counter.fetch_add(1, Ordering::SeqCst);
|
|
|
|
None
|
|
|
|
}),
|
2020-04-25 21:18:20 +00:00
|
|
|
);
|
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
let (source, context) = match source.as_str() {
|
2018-04-05 13:00:43 +00:00
|
|
|
"udpsrc" => {
|
2022-10-19 16:18:43 +00:00
|
|
|
let source = gst::ElementFactory::make("udpsrc")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("source-{i}").as_str())
|
2022-11-03 11:33:39 +00:00
|
|
|
.property("port", 5004i32 + i as i32)
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("retrieve-sender-address", false)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
(source, None)
|
2018-04-05 13:00:43 +00:00
|
|
|
}
|
|
|
|
"ts-udpsrc" => {
|
2022-03-21 11:25:48 +00:00
|
|
|
let context = build_context();
|
2022-10-19 16:18:43 +00:00
|
|
|
let source = gst::ElementFactory::make("ts-udpsrc")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("source-{i}").as_str())
|
2022-11-03 11:33:39 +00:00
|
|
|
.property("port", 5004i32 + i as i32)
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("context", &context)
|
|
|
|
.property("context-wait", wait)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
if is_rtp {
|
|
|
|
source.set_property("caps", &rtp_caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
(source, Some(context))
|
2018-04-05 13:00:43 +00:00
|
|
|
}
|
2020-04-25 21:18:20 +00:00
|
|
|
"tcpclientsrc" => {
|
2022-10-19 16:18:43 +00:00
|
|
|
let source = gst::ElementFactory::make("tcpclientsrc")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("source-{i}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("host", "127.0.0.1")
|
|
|
|
.property("port", 40000i32)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-04-25 21:18:20 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
(source, None)
|
2020-04-25 21:18:20 +00:00
|
|
|
}
|
2018-07-06 07:32:24 +00:00
|
|
|
"ts-tcpclientsrc" => {
|
2022-03-21 11:25:48 +00:00
|
|
|
let context = build_context();
|
2022-10-19 16:18:43 +00:00
|
|
|
let source = gst::ElementFactory::make("ts-tcpclientsrc")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("source-{i}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("host", "127.0.0.1")
|
|
|
|
.property("port", 40000i32)
|
|
|
|
.property("context", &context)
|
|
|
|
.property("context-wait", wait)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2018-07-06 07:32:24 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
(source, Some(context))
|
2018-07-06 07:32:24 +00:00
|
|
|
}
|
2018-04-05 13:00:43 +00:00
|
|
|
"tonegeneratesrc" => {
|
2022-10-19 16:18:43 +00:00
|
|
|
let source = gst::ElementFactory::make("tonegeneratesrc")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("source-{i}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("samplesperbuffer", (wait as i32) * 8000 / 1000)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2021-11-08 09:55:40 +00:00
|
|
|
sink.set_property("sync", true);
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
(source, None)
|
2018-04-05 13:00:43 +00:00
|
|
|
}
|
|
|
|
"ts-tonesrc" => {
|
2022-03-21 11:25:48 +00:00
|
|
|
let context = build_context();
|
2022-10-19 16:18:43 +00:00
|
|
|
let source = gst::ElementFactory::make("ts-tonesrc")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("source-{i}").as_str())
|
2022-12-13 09:43:16 +00:00
|
|
|
.property("samples-per-buffer", wait * 8000 / 1000)
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("context", &context)
|
|
|
|
.property("context-wait", wait)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
(source, Some(context))
|
2018-04-05 13:00:43 +00:00
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
if is_rtp {
|
2022-10-19 16:18:43 +00:00
|
|
|
let jb = gst::ElementFactory::make("ts-jitterbuffer")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("jb-{i}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("context-wait", wait)
|
|
|
|
.property("latency", wait)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2022-03-21 11:25:48 +00:00
|
|
|
if let Some(context) = context {
|
|
|
|
jb.set_property("context", &context);
|
|
|
|
}
|
|
|
|
|
|
|
|
let elements = &[&source, &jb, &sink];
|
|
|
|
pipeline.add_many(elements).unwrap();
|
|
|
|
gst::Element::link_many(elements).unwrap();
|
|
|
|
} else {
|
2022-11-03 11:33:39 +00:00
|
|
|
let elements = &[&source, &sink];
|
2022-03-21 11:25:48 +00:00
|
|
|
pipeline.add_many(elements).unwrap();
|
|
|
|
gst::Element::link_many(elements).unwrap();
|
|
|
|
}
|
2018-04-05 13:00:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 12:49:54 +00:00
|
|
|
let bus = pipeline.bus().unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
let l_clone = l.clone();
|
2023-04-14 09:46:43 +00:00
|
|
|
let _bus_watch = bus
|
|
|
|
.add_watch(move |_, msg| {
|
|
|
|
use gst::MessageView;
|
|
|
|
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::Eos(..) => l_clone.quit(),
|
|
|
|
MessageView::Error(err) => {
|
|
|
|
gst::error!(
|
|
|
|
CAT,
|
|
|
|
"Error from {:?}: {} ({:?})",
|
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error(),
|
|
|
|
err.debug()
|
|
|
|
);
|
|
|
|
l_clone.quit();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2023-07-06 13:27:28 +00:00
|
|
|
glib::ControlFlow::Continue
|
2023-04-14 09:46:43 +00:00
|
|
|
})
|
|
|
|
.expect("Failed to add bus watch");
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2019-01-11 23:45:05 +00:00
|
|
|
pipeline.set_state(gst::State::Playing).unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
gst::info!(CAT, "started");
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2022-03-21 11:25:48 +00:00
|
|
|
let l_clone = l.clone();
|
2020-04-25 21:18:20 +00:00
|
|
|
thread::spawn(move || {
|
2022-09-07 21:10:29 +00:00
|
|
|
let n_streams_f32 = n_streams as f32;
|
|
|
|
|
|
|
|
let mut total_count = 0.0;
|
|
|
|
let mut ramp_up_complete_instant: Option<Instant> = None;
|
|
|
|
|
|
|
|
#[cfg(feature = "tuning")]
|
|
|
|
let ctx_0 = gstthreadshare::runtime::Context::acquire(
|
|
|
|
"context-0",
|
|
|
|
Duration::from_millis(wait as u64),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
#[cfg(feature = "tuning")]
|
|
|
|
let mut parked_init = Duration::ZERO;
|
2020-04-25 21:18:20 +00:00
|
|
|
|
|
|
|
loop {
|
2022-09-07 21:10:29 +00:00
|
|
|
total_count += counter.fetch_and(0, Ordering::SeqCst) as f32 / n_streams_f32;
|
2022-03-21 11:25:48 +00:00
|
|
|
if let Some(max_buffers) = max_buffers {
|
2022-09-07 21:10:29 +00:00
|
|
|
if total_count > max_buffers {
|
2022-03-21 11:25:48 +00:00
|
|
|
gst::info!(CAT, "Stopping");
|
|
|
|
let stopping_instant = Instant::now();
|
|
|
|
pipeline.set_state(gst::State::Ready).unwrap();
|
|
|
|
gst::info!(CAT, "Stopped. Took {:?}", stopping_instant.elapsed());
|
|
|
|
pipeline.set_state(gst::State::Null).unwrap();
|
|
|
|
gst::info!(CAT, "Unprepared");
|
|
|
|
l_clone.quit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 21:10:29 +00:00
|
|
|
if let Some(init) = ramp_up_complete_instant {
|
|
|
|
let elapsed = init.elapsed();
|
2022-03-21 11:25:48 +00:00
|
|
|
gst::info!(
|
|
|
|
CAT,
|
2022-11-03 11:33:39 +00:00
|
|
|
"Thrpt: {:>6.2}",
|
2022-09-07 21:10:29 +00:00
|
|
|
total_count * 1_000.0 / elapsed.as_millis() as f32
|
2020-04-25 21:18:20 +00:00
|
|
|
);
|
|
|
|
|
2022-09-07 21:10:29 +00:00
|
|
|
#[cfg(feature = "tuning")]
|
|
|
|
gst::info!(
|
|
|
|
CAT,
|
2022-11-03 11:33:39 +00:00
|
|
|
"Parked: {:>6.2}%",
|
2022-09-07 21:10:29 +00:00
|
|
|
(ctx_0.parked_duration() - parked_init).as_nanos() as f32 * 100.0
|
|
|
|
/ elapsed.as_nanos() as f32
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Ramp up 30s worth of buffers before following parked
|
|
|
|
if total_count > 50.0 * 30.0 {
|
|
|
|
total_count = 0.0;
|
|
|
|
ramp_up_complete_instant = Some(Instant::now());
|
|
|
|
#[cfg(feature = "tuning")]
|
|
|
|
{
|
|
|
|
parked_init = ctx_0.parked_duration();
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 21:18:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 21:10:29 +00:00
|
|
|
thread::sleep(THROUGHPUT_PERIOD);
|
2020-04-25 21:18:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-05 13:00:43 +00:00
|
|
|
l.run();
|
|
|
|
}
|