2020-12-20 15:09:22 +00:00
|
|
|
use gst::element_error;
|
2017-11-19 03:18:34 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
#[path = "../examples-common.rs"]
|
|
|
|
mod examples_common;
|
|
|
|
|
2020-05-02 23:23:38 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
use derive_more::{Display, Error};
|
|
|
|
|
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "Missing element {}", _0)]
|
|
|
|
struct MissingElement(#[error(not(source))] &'static str);
|
|
|
|
|
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "No such pad {} in {}", _0, _1)]
|
|
|
|
struct NoSuchPad(#[error(not(source))] &'static str, String);
|
|
|
|
|
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "Unknown payload type {}", _0)]
|
|
|
|
struct UnknownPT(#[error(not(source))] u32);
|
|
|
|
|
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "Usage: {} (play | record) DROP_PROBABILITY", _0)]
|
|
|
|
struct UsageError(#[error(not(source))] String);
|
|
|
|
|
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "Received error from {}: {} (debug: {:?})", src, error, debug)]
|
2017-11-19 03:18:34 +00:00
|
|
|
struct ErrorMessage {
|
|
|
|
src: String,
|
|
|
|
error: String,
|
|
|
|
debug: Option<String>,
|
2020-05-02 23:23:38 +00:00
|
|
|
source: glib::Error,
|
2017-11-19 03:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 18:19:24 +00:00
|
|
|
fn make_element(
|
2017-11-19 03:18:34 +00:00
|
|
|
factory_name: &'static str,
|
2019-05-23 18:19:24 +00:00
|
|
|
element_name: Option<&str>,
|
2017-11-19 03:18:34 +00:00
|
|
|
) -> Result<gst::Element, Error> {
|
2019-07-11 15:50:37 +00:00
|
|
|
match gst::ElementFactory::make(factory_name, element_name) {
|
2019-12-17 19:00:42 +00:00
|
|
|
Ok(elem) => Ok(elem),
|
|
|
|
Err(_) => Err(Error::from(MissingElement(factory_name))),
|
2017-11-19 03:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_static_pad")]
|
2021-04-20 10:23:24 +00:00
|
|
|
fn static_pad(element: &gst::Element, pad_name: &'static str) -> Result<gst::Pad, Error> {
|
2021-04-20 10:24:17 +00:00
|
|
|
match element.static_pad(pad_name) {
|
2017-11-19 03:18:34 +00:00
|
|
|
Some(pad) => Ok(pad),
|
|
|
|
None => {
|
2021-04-11 19:39:50 +00:00
|
|
|
let element_name = element.name();
|
2018-12-09 16:09:20 +00:00
|
|
|
Err(Error::from(NoSuchPad(pad_name, element_name.to_string())))
|
2017-11-19 03:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_request_pad")]
|
2021-04-20 10:23:24 +00:00
|
|
|
fn request_pad(element: &gst::Element, pad_name: &'static str) -> Result<gst::Pad, Error> {
|
2021-04-20 13:12:36 +00:00
|
|
|
match element.request_pad_simple(pad_name) {
|
2017-11-19 03:18:34 +00:00
|
|
|
Some(pad) => Ok(pad),
|
|
|
|
None => {
|
2021-04-11 19:39:50 +00:00
|
|
|
let element_name = element.name();
|
2018-12-09 16:09:20 +00:00
|
|
|
Err(Error::from(NoSuchPad(pad_name, element_name.to_string())))
|
2017-11-19 03:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn connect_rtpbin_srcpad(src_pad: &gst::Pad, sink: &gst::Element) -> Result<(), Error> {
|
2021-04-11 19:39:50 +00:00
|
|
|
let name = src_pad.name();
|
2018-07-20 07:21:06 +00:00
|
|
|
let split_name = name.split('_');
|
2017-11-19 03:18:34 +00:00
|
|
|
let split_name = split_name.collect::<Vec<&str>>();
|
|
|
|
let pt = split_name[5].parse::<u32>()?;
|
|
|
|
|
|
|
|
match pt {
|
|
|
|
96 => {
|
2021-04-20 10:24:17 +00:00
|
|
|
let sinkpad = static_pad(sink, "sink")?;
|
2019-01-08 16:13:37 +00:00
|
|
|
src_pad.link(&sinkpad)?;
|
2017-11-19 03:18:34 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(Error::from(UnknownPT(pt))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_fec_decoder(rtpbin: &gst::Element, sess_id: u32) -> Result<gst::Element, Error> {
|
|
|
|
let fecdec = make_element("rtpulpfecdec", None)?;
|
|
|
|
let internal_storage = rtpbin
|
2021-02-22 15:14:27 +00:00
|
|
|
.emit_by_name("get-internal-storage", &[&sess_id])
|
2017-11-19 03:18:34 +00:00
|
|
|
.unwrap()
|
2020-10-20 13:14:10 +00:00
|
|
|
.unwrap()
|
|
|
|
.get::<glib::Object>()
|
2021-04-20 07:19:02 +00:00
|
|
|
.unwrap();
|
2017-11-19 03:18:34 +00:00
|
|
|
|
2020-10-20 13:14:10 +00:00
|
|
|
fecdec.set_property("storage", &internal_storage)?;
|
|
|
|
fecdec.set_property("pt", &100u32)?;
|
2017-11-19 03:18:34 +00:00
|
|
|
|
|
|
|
Ok(fecdec)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn example_main() -> Result<(), Error> {
|
|
|
|
gst::init()?;
|
|
|
|
|
|
|
|
let args: Vec<_> = env::args().collect();
|
|
|
|
|
|
|
|
if args.len() != 3 {
|
|
|
|
return Err(Error::from(UsageError(args[0].clone())));
|
|
|
|
}
|
|
|
|
|
|
|
|
let drop_probability = args[2].parse::<f32>()?;
|
|
|
|
|
|
|
|
let pipeline = gst::Pipeline::new(None);
|
|
|
|
let src = make_element("udpsrc", None)?;
|
|
|
|
let netsim = make_element("netsim", None)?;
|
|
|
|
let rtpbin = make_element("rtpbin", None)?;
|
|
|
|
let depay = make_element("rtpvp8depay", None)?;
|
|
|
|
let dec = make_element("vp8dec", None)?;
|
|
|
|
let conv = make_element("videoconvert", None)?;
|
|
|
|
let scale = make_element("videoscale", None)?;
|
|
|
|
let filter = make_element("capsfilter", None)?;
|
|
|
|
|
2018-02-22 10:18:37 +00:00
|
|
|
pipeline.add_many(&[&src, &netsim, &rtpbin, &depay, &dec, &conv, &scale, &filter])?;
|
2017-11-19 03:18:34 +00:00
|
|
|
gst::Element::link_many(&[&depay, &dec, &conv, &scale, &filter])?;
|
|
|
|
|
|
|
|
match args[1].as_str() {
|
|
|
|
"play" => {
|
|
|
|
let sink = make_element("autovideosink", None)?;
|
|
|
|
pipeline.add(&sink)?;
|
|
|
|
filter.link(&sink)?;
|
|
|
|
}
|
|
|
|
"record" => {
|
|
|
|
let enc = make_element("x264enc", None)?;
|
|
|
|
let mux = make_element("matroskamux", None)?;
|
|
|
|
let sink = make_element("filesink", None)?;
|
|
|
|
|
|
|
|
pipeline.add_many(&[&enc, &mux, &sink])?;
|
|
|
|
gst::Element::link_many(&[&filter, &enc, &mux, &sink])?;
|
2020-10-20 13:14:10 +00:00
|
|
|
sink.set_property("location", &"out.mkv")?;
|
2021-08-17 05:47:42 +00:00
|
|
|
enc.set_property_from_str("tune", "zerolatency")?;
|
2017-11-19 03:18:34 +00:00
|
|
|
eprintln!("Recording to out.mkv");
|
|
|
|
}
|
|
|
|
_ => return Err(Error::from(UsageError(args[0].clone()))),
|
|
|
|
}
|
|
|
|
|
|
|
|
src.link(&netsim)?;
|
|
|
|
|
|
|
|
rtpbin.connect("new-storage", false, |values| {
|
2019-08-11 07:33:34 +00:00
|
|
|
let storage = values[1]
|
|
|
|
.get::<gst::Element>()
|
2021-04-20 07:19:02 +00:00
|
|
|
.expect("rtpbin \"new-storage\" signal values[1]");
|
2020-10-20 13:14:10 +00:00
|
|
|
storage.set_property("size-time", &250_000_000u64).unwrap();
|
2017-11-19 03:18:34 +00:00
|
|
|
|
|
|
|
None
|
|
|
|
})?;
|
|
|
|
|
|
|
|
rtpbin.connect("request-pt-map", false, |values| {
|
2019-08-11 07:33:34 +00:00
|
|
|
let pt = values[2]
|
2021-04-20 07:19:02 +00:00
|
|
|
.get::<u32>()
|
2019-08-11 07:33:34 +00:00
|
|
|
.expect("rtpbin \"new-storage\" signal values[2]");
|
2017-11-19 03:18:34 +00:00
|
|
|
match pt {
|
|
|
|
100 => Some(
|
|
|
|
gst::Caps::new_simple(
|
|
|
|
"application/x-rtp",
|
|
|
|
&[
|
|
|
|
("media", &"video"),
|
|
|
|
("clock-rate", &90000i32),
|
|
|
|
("is-fec", &true),
|
|
|
|
],
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.to_value(),
|
2017-11-19 03:18:34 +00:00
|
|
|
),
|
|
|
|
96 => Some(
|
|
|
|
gst::Caps::new_simple(
|
|
|
|
"application/x-rtp",
|
|
|
|
&[
|
|
|
|
("media", &"video"),
|
|
|
|
("clock-rate", &90000i32),
|
|
|
|
("encoding-name", &"VP8"),
|
|
|
|
],
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.to_value(),
|
2017-11-19 03:18:34 +00:00
|
|
|
),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
|
|
|
rtpbin.connect("request-fec-decoder", false, |values| {
|
2019-08-11 07:33:34 +00:00
|
|
|
let rtpbin = values[0]
|
|
|
|
.get::<gst::Element>()
|
2021-04-20 07:19:02 +00:00
|
|
|
.expect("rtpbin \"request-fec-decoder\" signal values[0]");
|
2019-08-11 07:33:34 +00:00
|
|
|
let sess_id = values[1]
|
2021-04-20 07:19:02 +00:00
|
|
|
.get::<u32>()
|
2019-08-11 07:33:34 +00:00
|
|
|
.expect("rtpbin \"request-fec-decoder\" signal values[1]");
|
2017-11-19 03:18:34 +00:00
|
|
|
|
|
|
|
match make_fec_decoder(&rtpbin, sess_id) {
|
|
|
|
Ok(elem) => Some(elem.to_value()),
|
|
|
|
Err(err) => {
|
2020-12-20 15:09:22 +00:00
|
|
|
element_error!(
|
2017-11-19 03:18:34 +00:00
|
|
|
rtpbin,
|
|
|
|
gst::LibraryError::Failed,
|
|
|
|
("Failed to make FEC decoder"),
|
|
|
|
["{}", err]
|
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
2021-04-20 10:24:17 +00:00
|
|
|
let srcpad = static_pad(&netsim, "src")?;
|
|
|
|
let sinkpad = request_pad(&rtpbin, "recv_rtp_sink_0")?;
|
2019-01-08 16:13:37 +00:00
|
|
|
srcpad.link(&sinkpad)?;
|
2017-11-19 03:18:34 +00:00
|
|
|
|
2018-07-27 10:07:24 +00:00
|
|
|
let depay_weak = depay.downgrade();
|
2017-11-19 03:18:34 +00:00
|
|
|
rtpbin.connect_pad_added(move |rtpbin, src_pad| {
|
2018-07-27 10:07:24 +00:00
|
|
|
let depay = match depay_weak.upgrade() {
|
|
|
|
Some(depay) => depay,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2021-07-30 10:19:24 +00:00
|
|
|
match connect_rtpbin_srcpad(src_pad, &depay) {
|
2017-11-19 03:18:34 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => {
|
2020-12-20 15:09:22 +00:00
|
|
|
element_error!(
|
2017-11-19 03:18:34 +00:00
|
|
|
rtpbin,
|
|
|
|
gst::LibraryError::Failed,
|
|
|
|
("Failed to link srcpad"),
|
|
|
|
["{}", err]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let rtp_caps = gst::Caps::new_simple("application/x-rtp", &[("clock-rate", &90000i32)]);
|
|
|
|
|
|
|
|
let video_caps =
|
|
|
|
gst::Caps::new_simple("video/x-raw", &[("width", &1920i32), ("height", &1080i32)]);
|
|
|
|
|
2020-10-20 13:14:10 +00:00
|
|
|
src.set_property("address", &"127.0.0.1")?;
|
|
|
|
src.set_property("caps", &rtp_caps)?;
|
|
|
|
netsim.set_property("drop-probability", &drop_probability)?;
|
|
|
|
rtpbin.set_property("do-lost", &true)?;
|
|
|
|
filter.set_property("caps", &video_caps)?;
|
2017-11-19 03:18:34 +00:00
|
|
|
|
|
|
|
let bus = pipeline
|
2021-04-11 19:39:50 +00:00
|
|
|
.bus()
|
2017-11-19 03:18:34 +00:00
|
|
|
.expect("Pipeline without bus. Shouldn't happen!");
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline
|
|
|
|
.set_state(gst::State::Playing)
|
|
|
|
.expect("Unable to set the pipeline to the `Playing` state");
|
2017-11-19 03:18:34 +00:00
|
|
|
|
2021-04-28 22:29:13 +00:00
|
|
|
for msg in bus.iter_timed(gst::ClockTime::NONE) {
|
2017-11-19 03:18:34 +00:00
|
|
|
use gst::MessageView;
|
|
|
|
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::Eos(..) => break,
|
|
|
|
MessageView::Error(err) => {
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline
|
|
|
|
.set_state(gst::State::Null)
|
|
|
|
.expect("Unable to set the pipeline to the `Null` state");
|
2018-07-27 10:07:24 +00:00
|
|
|
|
2018-02-22 10:18:37 +00:00
|
|
|
return Err(ErrorMessage {
|
2018-07-27 10:36:40 +00:00
|
|
|
src: msg
|
2021-04-11 19:39:50 +00:00
|
|
|
.src()
|
|
|
|
.map(|s| String::from(s.path_string()))
|
2018-07-20 07:21:06 +00:00
|
|
|
.unwrap_or_else(|| String::from("None")),
|
2021-04-11 19:39:50 +00:00
|
|
|
error: err.error().to_string(),
|
|
|
|
debug: err.debug(),
|
|
|
|
source: err.error(),
|
2018-10-08 12:02:23 +00:00
|
|
|
}
|
|
|
|
.into());
|
2017-11-19 03:18:34 +00:00
|
|
|
}
|
2019-02-28 08:54:32 +00:00
|
|
|
MessageView::StateChanged(s) => {
|
2021-04-11 19:39:50 +00:00
|
|
|
if let Some(element) = msg.src() {
|
|
|
|
if element == pipeline && s.current() == gst::State::Playing {
|
2018-10-08 12:02:23 +00:00
|
|
|
eprintln!("PLAYING");
|
|
|
|
gst::debug_bin_to_dot_file(
|
|
|
|
&pipeline,
|
|
|
|
gst::DebugGraphDetails::all(),
|
|
|
|
"client-playing",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 08:54:32 +00:00
|
|
|
}
|
2017-11-19 03:18:34 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline
|
|
|
|
.set_state(gst::State::Null)
|
|
|
|
.expect("Unable to set the pipeline to the `Null` state");
|
2017-11-19 03:18:34 +00:00
|
|
|
|
2018-04-23 17:46:29 +00:00
|
|
|
Ok(())
|
2017-11-19 03:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
match examples_common::run(example_main) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => eprintln!("Error! {}", e),
|
|
|
|
}
|
|
|
|
}
|