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.
|
|
|
|
|
|
|
|
use glib::prelude::*;
|
|
|
|
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
|
|
|
|
|
|
|
fn main() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
let mut path = Path::new("target/debug");
|
|
|
|
if !path.exists() {
|
2020-04-09 17:57:18 +00:00
|
|
|
path = Path::new("../../target/debug");
|
2018-04-05 13:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst::Registry::get().scan_path(path);
|
|
|
|
}
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
{
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
let mut path = Path::new("target/release");
|
|
|
|
if !path.exists() {
|
2020-04-09 17:57:18 +00:00
|
|
|
path = Path::new("../../target/release");
|
2018-04-05 13:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst::Registry::get().scan_path(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = env::args().collect::<Vec<_>>();
|
|
|
|
assert_eq!(args.len(), 6);
|
|
|
|
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
|
|
|
|
|
|
|
let l = glib::MainLoop::new(None, false);
|
|
|
|
let pipeline = gst::Pipeline::new(None);
|
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 {
|
2019-11-30 18:51:31 +00:00
|
|
|
let sink =
|
2018-04-05 13:00:43 +00:00
|
|
|
gst::ElementFactory::make("fakesink", Some(format!("sink-{}", i).as_str())).unwrap();
|
2019-11-30 18:51:31 +00:00
|
|
|
sink.set_property("sync", &false).unwrap();
|
|
|
|
sink.set_property("async", &false).unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
|
2020-04-25 21:18:20 +00:00
|
|
|
let counter_clone = Arc::clone(&counter);
|
|
|
|
sink.get_static_pad("sink").unwrap().add_probe(
|
|
|
|
gst::PadProbeType::BUFFER,
|
|
|
|
move |_pad, _probe_info| {
|
|
|
|
let _ = counter_clone.fetch_add(1, Ordering::SeqCst);
|
|
|
|
gst::PadProbeReturn::Ok
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-04-05 13:00:43 +00:00
|
|
|
let source = match source.as_str() {
|
|
|
|
"udpsrc" => {
|
|
|
|
let source =
|
|
|
|
gst::ElementFactory::make("udpsrc", Some(format!("source-{}", i).as_str()))
|
|
|
|
.unwrap();
|
|
|
|
source
|
|
|
|
.set_property("port", &(40000i32 + (i as i32)))
|
|
|
|
.unwrap();
|
|
|
|
source
|
|
|
|
.set_property("retrieve-sender-address", &false)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
source
|
|
|
|
}
|
|
|
|
"ts-udpsrc" => {
|
|
|
|
let source =
|
|
|
|
gst::ElementFactory::make("ts-udpsrc", Some(format!("source-{}", i).as_str()))
|
|
|
|
.unwrap();
|
|
|
|
source
|
2020-04-27 09:22:26 +00:00
|
|
|
.set_property("port", &(40000i32 + (i as i32)))
|
2018-04-05 13:00:43 +00:00
|
|
|
.unwrap();
|
|
|
|
source
|
|
|
|
.set_property("context", &format!("context-{}", (i as u32) % n_groups))
|
|
|
|
.unwrap();
|
|
|
|
source.set_property("context-wait", &wait).unwrap();
|
|
|
|
|
|
|
|
source
|
|
|
|
}
|
2020-04-25 21:18:20 +00:00
|
|
|
"tcpclientsrc" => {
|
|
|
|
let source = gst::ElementFactory::make(
|
|
|
|
"tcpclientsrc",
|
|
|
|
Some(format!("source-{}", i).as_str()),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
source.set_property("host", &"127.0.0.1").unwrap();
|
2020-04-27 09:22:26 +00:00
|
|
|
source.set_property("port", &40000i32).unwrap();
|
2020-04-25 21:18:20 +00:00
|
|
|
|
|
|
|
source
|
|
|
|
}
|
2018-07-06 07:32:24 +00:00
|
|
|
"ts-tcpclientsrc" => {
|
|
|
|
let source = gst::ElementFactory::make(
|
|
|
|
"ts-tcpclientsrc",
|
|
|
|
Some(format!("source-{}", i).as_str()),
|
2018-11-05 11:43:38 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-04-27 09:22:26 +00:00
|
|
|
source.set_property("host", &"127.0.0.1").unwrap();
|
|
|
|
source.set_property("port", &40000i32).unwrap();
|
2018-07-06 07:32:24 +00:00
|
|
|
source
|
|
|
|
.set_property("context", &format!("context-{}", (i as u32) % n_groups))
|
|
|
|
.unwrap();
|
|
|
|
source.set_property("context-wait", &wait).unwrap();
|
|
|
|
|
|
|
|
source
|
|
|
|
}
|
2018-04-05 13:00:43 +00:00
|
|
|
"tonegeneratesrc" => {
|
|
|
|
let source = gst::ElementFactory::make(
|
|
|
|
"tonegeneratesrc",
|
|
|
|
Some(format!("source-{}", i).as_str()),
|
2018-11-05 11:43:38 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
source
|
|
|
|
.set_property("samplesperbuffer", &((wait as i32) * 8000 / 1000))
|
|
|
|
.unwrap();
|
|
|
|
|
2019-11-30 18:51:31 +00:00
|
|
|
sink.set_property("sync", &true).unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
|
|
|
|
source
|
|
|
|
}
|
|
|
|
"ts-tonesrc" => {
|
|
|
|
let source =
|
|
|
|
gst::ElementFactory::make("ts-tonesrc", Some(format!("source-{}", i).as_str()))
|
|
|
|
.unwrap();
|
|
|
|
source
|
|
|
|
.set_property("samples-per-buffer", &((wait as u32) * 8000 / 1000))
|
|
|
|
.unwrap();
|
|
|
|
source
|
|
|
|
.set_property("context", &format!("context-{}", (i as u32) % n_groups))
|
|
|
|
.unwrap();
|
|
|
|
source.set_property("context-wait", &wait).unwrap();
|
|
|
|
|
|
|
|
source
|
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
|
2019-11-30 18:51:31 +00:00
|
|
|
pipeline.add_many(&[&source, &sink]).unwrap();
|
|
|
|
source.link(&sink).unwrap();
|
2018-04-05 13:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let bus = pipeline.get_bus().unwrap();
|
|
|
|
let l_clone = l.clone();
|
|
|
|
bus.add_watch(move |_, msg| {
|
|
|
|
use gst::MessageView;
|
|
|
|
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::Eos(..) => l_clone.quit(),
|
|
|
|
MessageView::Error(err) => {
|
|
|
|
println!(
|
|
|
|
"Error from {:?}: {} ({:?})",
|
|
|
|
err.get_src().map(|s| s.get_path_string()),
|
|
|
|
err.get_error(),
|
|
|
|
err.get_debug()
|
|
|
|
);
|
|
|
|
l_clone.quit();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
glib::Continue(true)
|
2019-12-18 05:50:10 +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
|
|
|
|
|
|
|
println!("started");
|
|
|
|
|
2020-04-25 21:18:20 +00:00
|
|
|
thread::spawn(move || {
|
|
|
|
let throughput_factor = 1_000f32 / (n_streams as f32);
|
|
|
|
let mut prev_reset_instant: Option<Instant> = None;
|
|
|
|
let mut count;
|
|
|
|
let mut reset_instant;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
count = counter.fetch_and(0, Ordering::SeqCst);
|
|
|
|
reset_instant = Instant::now();
|
|
|
|
|
|
|
|
if let Some(prev_reset_instant) = prev_reset_instant {
|
|
|
|
println!(
|
|
|
|
"{:>5.1} / s / stream",
|
|
|
|
(count as f32) * throughput_factor
|
|
|
|
/ ((reset_instant - prev_reset_instant).as_millis() as f32)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(sleep_duration) = THROUGHPUT_PERIOD.checked_sub(reset_instant.elapsed()) {
|
|
|
|
thread::sleep(sleep_duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_reset_instant = Some(reset_instant);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-05 13:00:43 +00:00
|
|
|
l.run();
|
|
|
|
}
|