diff --git a/examples/Cargo.toml b/examples/Cargo.toml index cd7930aed..35ec4f7da 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -18,6 +18,7 @@ gst-audio = { package = "gstreamer-audio", path = "../gstreamer-audio" } gst-base = { package = "gstreamer-base", path = "../gstreamer-base" } gst-video = { package = "gstreamer-video", path = "../gstreamer-video" } gst-pbutils = { package = "gstreamer-pbutils", path = "../gstreamer-pbutils" } +gst-play = { package = "gstreamer-play", path = "../gstreamer-play", optional = true } gst-player = { package = "gstreamer-player", path = "../gstreamer-player", optional = true } ges = { package = "gstreamer-editing-services", path = "../gstreamer-editing-services", optional = true } gst-sdp = { package = "gstreamer-sdp", path = "../gstreamer-sdp", optional = true } @@ -107,6 +108,10 @@ name = "transmux" [[bin]] name = "pad_probes" +[[bin]] +name = "play" +required-features = ["gst-play"] + [[bin]] name = "playbin" diff --git a/examples/src/bin/play.rs b/examples/src/bin/play.rs new file mode 100644 index 000000000..51a2dce00 --- /dev/null +++ b/examples/src/bin/play.rs @@ -0,0 +1,62 @@ +// This example shows how to use the GstPlay API. +// The GstPlay API is a convenience API to allow implement playback applications +// without having to write too much code. +// Most of the tasks a play needs to support (such as seeking and switching +// audio / subtitle streams or changing the volume) are all supported by simple +// one-line function calls on the GstPlay. + +use std::env; + +use anyhow::Error; + +#[path = "../examples-common.rs"] +mod examples_common; + +use gst_play::{Play, PlayMessage, PlayVideoRenderer}; + +fn main_loop(uri: &str) -> Result<(), Error> { + gst::init()?; + + let play = Play::new(PlayVideoRenderer::NONE); + play.set_uri(Some(uri)); + play.play(); + + let mut result = Ok(()); + for msg in play.message_bus().iter_timed(gst::ClockTime::NONE) { + match PlayMessage::parse(&msg) { + Ok(PlayMessage::EndOfStream) => { + play.stop(); + break; + } + Ok(PlayMessage::Error { error, details: _ }) => { + result = Err(error); + play.stop(); + break; + } + Ok(_) => (), + Err(_) => unreachable!(), + } + } + result.map_err(|e| e.into()) +} + +fn example_main() { + let args: Vec<_> = env::args().collect(); + let uri: &str = if args.len() == 2 { + args[1].as_ref() + } else { + println!("Usage: play uri"); + std::process::exit(-1) + }; + + match main_loop(uri) { + Ok(r) => r, + Err(e) => eprintln!("Error! {}", e), + } +} + +fn main() { + // tutorials_common::run is only required to set up the application environment on macOS + // (but not necessary in normal Cocoa applications where this is set up automatically) + examples_common::run(example_main); +}