2018-11-05 10:10:14 +00:00
|
|
|
// This example demonstrates the use of the appsink element.
|
|
|
|
// It operates the following pipeline:
|
|
|
|
|
|
|
|
// {audiotestsrc} - {appsink}
|
|
|
|
|
|
|
|
// The application specifies what format it wants to handle. This format
|
|
|
|
// is applied by calling set_caps on the appsink. Now it's the audiotestsrc's
|
|
|
|
// task to provide this data format. If the element connected to the appsink's
|
|
|
|
// sink-pad were not able to provide what we ask them to, this would fail.
|
|
|
|
// This is the format we request:
|
|
|
|
// Audio / Signed 16bit / 1 channel / arbitrary sample rate
|
|
|
|
|
2020-12-20 15:09:22 +00:00
|
|
|
use gst::element_error;
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::prelude::*;
|
2017-08-01 18:44:01 +00:00
|
|
|
|
2017-08-14 17:45:35 +00:00
|
|
|
use byte_slice_cast::*;
|
|
|
|
|
2017-08-01 18:44:01 +00:00
|
|
|
use std::i16;
|
|
|
|
use std::i32;
|
|
|
|
|
2020-05-02 23:23:38 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
use derive_more::{Display, Error};
|
2017-11-11 14:53:03 +00:00
|
|
|
|
2017-11-12 18:07:02 +00:00
|
|
|
#[path = "../examples-common.rs"]
|
|
|
|
mod examples_common;
|
|
|
|
|
2020-05-02 23:23:38 +00:00
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "Missing element {}", _0)]
|
|
|
|
struct MissingElement(#[error(not(source))] &'static str);
|
2017-11-11 14:53:03 +00:00
|
|
|
|
2020-05-02 23:23:38 +00:00
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "Received error from {}: {} (debug: {:?})", src, error, debug)]
|
2017-11-11 14:53:03 +00:00
|
|
|
struct ErrorMessage {
|
|
|
|
src: String,
|
|
|
|
error: String,
|
|
|
|
debug: Option<String>,
|
2020-05-02 23:23:38 +00:00
|
|
|
source: glib::Error,
|
2017-11-11 14:53:03 +00:00
|
|
|
}
|
2017-08-01 18:44:01 +00:00
|
|
|
|
2017-11-11 14:53:03 +00:00
|
|
|
fn create_pipeline() -> Result<gst::Pipeline, Error> {
|
|
|
|
gst::init()?;
|
|
|
|
|
|
|
|
let pipeline = gst::Pipeline::new(None);
|
2019-12-17 19:00:42 +00:00
|
|
|
let src = gst::ElementFactory::make("audiotestsrc", None)
|
|
|
|
.map_err(|_| MissingElement("audiotestsrc"))?;
|
|
|
|
let sink = gst::ElementFactory::make("appsink", None).map_err(|_| MissingElement("appsink"))?;
|
2017-11-11 14:53:03 +00:00
|
|
|
|
|
|
|
pipeline.add_many(&[&src, &sink])?;
|
|
|
|
src.link(&sink)?;
|
2017-08-03 15:26:52 +00:00
|
|
|
|
2018-07-27 10:07:24 +00:00
|
|
|
let appsink = sink
|
2017-08-17 14:58:15 +00:00
|
|
|
.dynamic_cast::<gst_app::AppSink>()
|
2017-08-03 15:26:52 +00:00
|
|
|
.expect("Sink element is expected to be an appsink!");
|
2017-08-01 18:44:01 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Tell the appsink what format we want. It will then be the audiotestsrc's job to
|
|
|
|
// provide the format we request.
|
|
|
|
// This can be set after linking the two objects, because format negotiation between
|
|
|
|
// both elements will happen during pre-rolling of the pipeline.
|
2019-04-15 15:17:42 +00:00
|
|
|
appsink.set_caps(Some(&gst::Caps::new_simple(
|
2017-08-01 18:44:01 +00:00
|
|
|
"audio/x-raw",
|
|
|
|
&[
|
2019-10-04 06:11:30 +00:00
|
|
|
("format", &gst_audio::AUDIO_FORMAT_S16.to_str()),
|
2017-08-02 17:15:16 +00:00
|
|
|
("layout", &"interleaved"),
|
|
|
|
("channels", &(1i32)),
|
2017-08-17 14:58:15 +00:00
|
|
|
("rate", &gst::IntRange::<i32>::new(1, i32::MAX)),
|
2017-08-01 18:44:01 +00:00
|
|
|
],
|
2019-04-15 15:17:42 +00:00
|
|
|
)));
|
2017-08-01 18:44:01 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Getting data out of the appsink is done by setting callbacks on it.
|
|
|
|
// The appsink will then call those handlers, as soon as data is available.
|
2017-12-10 13:18:54 +00:00
|
|
|
appsink.set_callbacks(
|
2020-06-25 16:22:25 +00:00
|
|
|
gst_app::AppSinkCallbacks::builder()
|
2018-11-05 10:10:14 +00:00
|
|
|
// Add a handler to the "new-sample" signal.
|
2017-12-10 13:18:54 +00:00
|
|
|
.new_sample(|appsink| {
|
2018-11-05 10:10:14 +00:00
|
|
|
// Pull the sample in question out of the appsink's buffer.
|
2019-12-17 19:00:42 +00:00
|
|
|
let sample = appsink.pull_sample().map_err(|_| gst::FlowError::Eos)?;
|
2019-01-08 16:13:37 +00:00
|
|
|
let buffer = sample.get_buffer().ok_or_else(|| {
|
2020-12-20 15:09:22 +00:00
|
|
|
element_error!(
|
2017-12-10 13:18:54 +00:00
|
|
|
appsink,
|
|
|
|
gst::ResourceError::Failed,
|
|
|
|
("Failed to get buffer from appsink")
|
|
|
|
);
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
2017-12-10 13:18:54 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// At this point, buffer is only a reference to an existing memory region somewhere.
|
|
|
|
// When we want to access its content, we have to map it while requesting the required
|
|
|
|
// mode of access (read, read/write).
|
|
|
|
// This type of abstraction is necessary, because the buffer in question might not be
|
|
|
|
// on the machine's main memory itself, but rather in the GPU's memory.
|
|
|
|
// So mapping the buffer makes the underlying memory region accessible to us.
|
|
|
|
// See: https://gstreamer.freedesktop.org/documentation/plugin-development/advanced/allocation.html
|
2019-12-17 19:00:42 +00:00
|
|
|
let map = buffer.map_readable().map_err(|_| {
|
2020-12-20 15:09:22 +00:00
|
|
|
element_error!(
|
2017-12-10 13:18:54 +00:00
|
|
|
appsink,
|
|
|
|
gst::ResourceError::Failed,
|
|
|
|
("Failed to map buffer readable")
|
|
|
|
);
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
2017-12-10 13:18:54 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// We know what format the data in the memory region has, since we requested
|
|
|
|
// it by setting the appsink's caps. So what we do here is interpret the
|
|
|
|
// memory region we mapped as an array of signed 16 bit integers.
|
2019-01-08 16:13:37 +00:00
|
|
|
let samples = map.as_slice_of::<i16>().map_err(|_| {
|
2020-12-20 15:09:22 +00:00
|
|
|
element_error!(
|
2017-12-10 13:18:54 +00:00
|
|
|
appsink,
|
|
|
|
gst::ResourceError::Failed,
|
|
|
|
("Failed to interprete buffer as S16 PCM")
|
|
|
|
);
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
2017-12-10 13:18:54 +00:00
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// For buffer (= chunk of samples), we calculate the root mean square:
|
|
|
|
// (https://en.wikipedia.org/wiki/Root_mean_square)
|
2017-12-10 13:18:54 +00:00
|
|
|
let sum: f64 = samples
|
|
|
|
.iter()
|
|
|
|
.map(|sample| {
|
|
|
|
let f = f64::from(*sample) / f64::from(i16::MAX);
|
|
|
|
f * f
|
2018-10-08 12:02:23 +00:00
|
|
|
})
|
|
|
|
.sum();
|
2017-12-10 13:18:54 +00:00
|
|
|
let rms = (sum / (samples.len() as f64)).sqrt();
|
|
|
|
println!("rms: {}", rms);
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
Ok(gst::FlowSuccess::Ok)
|
2018-10-08 12:02:23 +00:00
|
|
|
})
|
|
|
|
.build(),
|
2017-12-10 13:18:54 +00:00
|
|
|
);
|
2017-08-01 18:44:01 +00:00
|
|
|
|
2017-08-03 15:26:52 +00:00
|
|
|
Ok(pipeline)
|
|
|
|
}
|
|
|
|
|
2017-11-11 15:56:37 +00:00
|
|
|
fn main_loop(pipeline: gst::Pipeline) -> Result<(), Error> {
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline.set_state(gst::State::Playing)?;
|
2017-08-03 15:26:52 +00:00
|
|
|
|
|
|
|
let bus = pipeline
|
|
|
|
.get_bus()
|
|
|
|
.expect("Pipeline without bus. Shouldn't happen!");
|
2017-08-01 18:44:01 +00:00
|
|
|
|
2018-12-27 22:06:03 +00:00
|
|
|
for msg in bus.iter_timed(gst::CLOCK_TIME_NONE) {
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::MessageView;
|
|
|
|
|
2017-08-01 18:44:01 +00:00
|
|
|
match msg.view() {
|
|
|
|
MessageView::Eos(..) => break,
|
|
|
|
MessageView::Error(err) => {
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline.set_state(gst::State::Null)?;
|
2019-10-04 07:47:48 +00:00
|
|
|
return Err(ErrorMessage {
|
2018-12-09 16:09:20 +00:00
|
|
|
src: msg
|
2018-07-27 10:36:40 +00:00
|
|
|
.get_src()
|
2018-12-09 16:09:20 +00:00
|
|
|
.map(|s| String::from(s.get_path_string()))
|
2017-11-27 11:01:03 +00:00
|
|
|
.unwrap_or_else(|| String::from("None")),
|
2020-03-19 11:31:52 +00:00
|
|
|
error: err.get_error().to_string(),
|
2019-12-22 07:59:23 +00:00
|
|
|
debug: err.get_debug(),
|
2020-05-02 23:23:38 +00:00
|
|
|
source: err.get_error(),
|
2019-10-04 07:47:48 +00:00
|
|
|
}
|
|
|
|
.into());
|
2017-08-01 18:44:01 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline.set_state(gst::State::Null)?;
|
2017-08-03 15:26:52 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-11-12 18:07:02 +00:00
|
|
|
fn example_main() {
|
2017-11-11 15:56:37 +00:00
|
|
|
match create_pipeline().and_then(main_loop) {
|
2017-08-03 15:26:52 +00:00
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => eprintln!("Error! {}", e),
|
|
|
|
}
|
2017-08-01 18:44:01 +00:00
|
|
|
}
|
2017-11-12 18:07:02 +00:00
|
|
|
|
|
|
|
fn main() {
|
2021-04-10 11:42:04 +00:00
|
|
|
// 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)
|
2017-11-12 18:07:02 +00:00
|
|
|
examples_common::run(example_main);
|
|
|
|
}
|