2017-10-08 13:32:28 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
|
2017-11-12 13:53:50 +00:00
|
|
|
#[path = "../tutorials-common.rs"]
|
|
|
|
mod tutorials_common;
|
|
|
|
|
|
|
|
fn tutorial_main() {
|
2017-10-08 13:32:28 +00:00
|
|
|
// Initialize GStreamer
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2022-10-19 11:13:57 +00:00
|
|
|
let uri =
|
|
|
|
"https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm";
|
|
|
|
|
2017-10-08 13:32:28 +00:00
|
|
|
// Create the elements
|
2022-10-19 11:13:57 +00:00
|
|
|
let source = gst::ElementFactory::make("uridecodebin")
|
|
|
|
.name("source")
|
|
|
|
// Set the URI to play
|
|
|
|
.property("uri", uri)
|
|
|
|
.build()
|
2017-10-08 13:32:28 +00:00
|
|
|
.expect("Could not create uridecodebin element.");
|
2022-10-19 11:13:57 +00:00
|
|
|
let convert = gst::ElementFactory::make("audioconvert")
|
|
|
|
.name("convert")
|
|
|
|
.build()
|
2017-10-08 13:32:28 +00:00
|
|
|
.expect("Could not create convert element.");
|
2022-10-19 11:13:57 +00:00
|
|
|
let sink = gst::ElementFactory::make("autoaudiosink")
|
|
|
|
.name("sink")
|
|
|
|
.build()
|
2019-04-15 15:17:42 +00:00
|
|
|
.expect("Could not create sink element.");
|
2022-10-19 11:13:57 +00:00
|
|
|
let resample = gst::ElementFactory::make("audioresample")
|
|
|
|
.name("resample")
|
|
|
|
.build()
|
2020-07-10 08:35:57 +00:00
|
|
|
.expect("Could not create resample element.");
|
2017-10-08 13:32:28 +00:00
|
|
|
|
|
|
|
// Create the empty pipeline
|
2022-10-22 14:24:21 +00:00
|
|
|
let pipeline = gst::Pipeline::builder().name("test-pipeline").build();
|
2017-10-08 13:32:28 +00:00
|
|
|
|
|
|
|
// Build the pipeline Note that we are NOT linking the source at this
|
|
|
|
// point. We will do it later.
|
2020-07-10 08:35:57 +00:00
|
|
|
pipeline
|
|
|
|
.add_many(&[&source, &convert, &resample, &sink])
|
|
|
|
.unwrap();
|
|
|
|
gst::Element::link_many(&[&convert, &resample, &sink]).expect("Elements could not be linked.");
|
2017-10-08 13:32:28 +00:00
|
|
|
|
|
|
|
// Connect the pad-added signal
|
2020-07-10 08:32:49 +00:00
|
|
|
source.connect_pad_added(move |src, src_pad| {
|
2021-04-11 19:39:50 +00:00
|
|
|
println!("Received new pad {} from {}", src_pad.name(), src.name());
|
2017-10-08 13:32:28 +00:00
|
|
|
|
|
|
|
let sink_pad = convert
|
2021-04-20 10:24:17 +00:00
|
|
|
.static_pad("sink")
|
2017-10-08 13:32:28 +00:00
|
|
|
.expect("Failed to get static sink pad from convert");
|
|
|
|
if sink_pad.is_linked() {
|
|
|
|
println!("We are already linked. Ignoring.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_pad_caps = src_pad
|
2021-04-11 19:39:50 +00:00
|
|
|
.current_caps()
|
2017-10-08 13:32:28 +00:00
|
|
|
.expect("Failed to get caps of new pad.");
|
|
|
|
let new_pad_struct = new_pad_caps
|
2021-04-20 10:24:17 +00:00
|
|
|
.structure(0)
|
2017-10-08 13:32:28 +00:00
|
|
|
.expect("Failed to get first structure of caps.");
|
2021-04-11 19:39:50 +00:00
|
|
|
let new_pad_type = new_pad_struct.name();
|
2017-10-08 13:32:28 +00:00
|
|
|
|
|
|
|
let is_audio = new_pad_type.starts_with("audio/x-raw");
|
|
|
|
if !is_audio {
|
|
|
|
println!(
|
|
|
|
"It has type {} which is not raw audio. Ignoring.",
|
|
|
|
new_pad_type
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
let res = src_pad.link(&sink_pad);
|
2019-02-21 17:30:36 +00:00
|
|
|
if res.is_err() {
|
2017-10-08 13:32:28 +00:00
|
|
|
println!("Type is {} but link failed.", new_pad_type);
|
|
|
|
} else {
|
|
|
|
println!("Link succeeded (type {}).", new_pad_type);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start playing
|
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-10-08 13:32:28 +00:00
|
|
|
|
|
|
|
// Wait until error or EOS
|
2021-04-11 19:39:50 +00:00
|
|
|
let bus = pipeline.bus().unwrap();
|
2021-04-28 22:29:13 +00:00
|
|
|
for msg in bus.iter_timed(gst::ClockTime::NONE) {
|
2017-10-08 13:32:28 +00:00
|
|
|
use gst::MessageView;
|
2020-10-10 08:00:48 +00:00
|
|
|
|
2017-10-08 13:32:28 +00:00
|
|
|
match msg.view() {
|
|
|
|
MessageView::Error(err) => {
|
|
|
|
eprintln!(
|
2017-11-16 11:58:56 +00:00
|
|
|
"Error received from element {:?} {}",
|
2021-04-11 19:39:50 +00:00
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error()
|
2017-10-08 13:32:28 +00:00
|
|
|
);
|
2021-04-11 19:39:50 +00:00
|
|
|
eprintln!("Debugging information: {:?}", err.debug());
|
2017-10-08 13:32:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-01-29 14:26:01 +00:00
|
|
|
MessageView::StateChanged(state_changed) => {
|
2023-01-04 17:48:41 +00:00
|
|
|
if state_changed.src().map(|s| s == &pipeline).unwrap_or(false) {
|
2017-11-16 11:58:56 +00:00
|
|
|
println!(
|
|
|
|
"Pipeline state changed from {:?} to {:?}",
|
2021-04-11 19:39:50 +00:00
|
|
|
state_changed.old(),
|
|
|
|
state_changed.current()
|
2017-11-16 11:58:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-10-08 13:32:28 +00:00
|
|
|
MessageView::Eos(..) => break,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-08 13:32:28 +00:00
|
|
|
}
|
2017-11-12 13:53:50 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
tutorials_common::run(tutorial_main);
|
|
|
|
}
|