2017-07-29 14:57:01 +00:00
|
|
|
extern crate gstreamer as gst;
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::prelude::*;
|
2017-08-11 14:09:32 +00:00
|
|
|
extern crate gstreamer_audio as gst_audio;
|
|
|
|
|
2017-08-14 17:45:35 +00:00
|
|
|
extern crate byte_slice_cast;
|
|
|
|
use byte_slice_cast::*;
|
|
|
|
|
2017-07-29 14:57:01 +00:00
|
|
|
use std::i16;
|
|
|
|
|
2017-11-12 18:07:02 +00:00
|
|
|
#[path = "../examples-common.rs"]
|
|
|
|
mod examples_common;
|
|
|
|
|
|
|
|
fn example_main() {
|
2017-07-29 14:57:01 +00:00
|
|
|
gst::init().unwrap();
|
|
|
|
|
2017-08-11 14:09:32 +00:00
|
|
|
let pipeline = gst::parse_launch(&format!(
|
|
|
|
"audiotestsrc name=src ! audio/x-raw,format={},channels=1 ! fakesink",
|
|
|
|
gst_audio::AUDIO_FORMAT_S16.to_string()
|
|
|
|
)).unwrap();
|
2017-07-29 14:57:01 +00:00
|
|
|
let bus = pipeline.get_bus().unwrap();
|
|
|
|
|
2017-07-31 11:16:42 +00:00
|
|
|
let src = pipeline
|
|
|
|
.clone()
|
2017-08-17 14:58:15 +00:00
|
|
|
.dynamic_cast::<gst::Bin>()
|
2017-07-31 11:16:42 +00:00
|
|
|
.unwrap()
|
|
|
|
.get_by_name("src")
|
|
|
|
.unwrap();
|
2017-07-29 14:57:01 +00:00
|
|
|
let src_pad = src.get_static_pad("src").unwrap();
|
2017-10-01 13:52:15 +00:00
|
|
|
src_pad.add_probe(gst::PadProbeType::BUFFER, |_, probe_info| {
|
2017-08-17 14:58:15 +00:00
|
|
|
if let Some(gst::PadProbeData::Buffer(ref buffer)) = probe_info.data {
|
2017-08-11 12:20:43 +00:00
|
|
|
let map = buffer.map_readable().unwrap();
|
2017-08-11 14:09:32 +00:00
|
|
|
|
2017-08-14 17:45:35 +00:00
|
|
|
let samples = if let Ok(samples) = map.as_slice().as_slice_of::<i16>() {
|
2017-08-11 14:09:32 +00:00
|
|
|
samples
|
|
|
|
} else {
|
2017-08-17 14:58:15 +00:00
|
|
|
return gst::PadProbeReturn::Ok;
|
2017-08-11 14:09:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let sum: f64 = samples
|
|
|
|
.iter()
|
2017-08-02 17:15:16 +00:00
|
|
|
.map(|sample| {
|
2017-09-10 12:10:04 +00:00
|
|
|
let f = f64::from(*sample) / f64::from(i16::MAX);
|
2017-08-02 17:15:16 +00:00
|
|
|
f * f
|
|
|
|
})
|
|
|
|
.sum();
|
2017-08-11 14:09:32 +00:00
|
|
|
let rms = (sum / (samples.len() as f64)).sqrt();
|
2017-08-02 17:15:16 +00:00
|
|
|
println!("rms: {}", rms);
|
2017-07-29 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 14:58:15 +00:00
|
|
|
gst::PadProbeReturn::Ok
|
2017-07-29 14:57:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let ret = pipeline.set_state(gst::State::Playing);
|
|
|
|
assert_ne!(ret, gst::StateChangeReturn::Failure);
|
|
|
|
|
2017-09-13 16:35:35 +00:00
|
|
|
while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) {
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::MessageView;
|
|
|
|
|
2017-07-29 14:57:01 +00:00
|
|
|
match msg.view() {
|
2017-07-30 14:06:44 +00:00
|
|
|
MessageView::Eos(..) => break,
|
2017-07-29 14:57:01 +00:00
|
|
|
MessageView::Error(err) => {
|
|
|
|
println!(
|
2017-11-16 11:58:56 +00:00
|
|
|
"Error from {:?}: {} ({:?})",
|
|
|
|
msg.get_src().map(|s| s.get_path_string()),
|
2017-07-29 14:57:01 +00:00
|
|
|
err.get_error(),
|
|
|
|
err.get_debug()
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ret = pipeline.set_state(gst::State::Null);
|
|
|
|
assert_ne!(ret, gst::StateChangeReturn::Failure);
|
|
|
|
}
|
2017-11-12 18:07:02 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// tutorials_common::run is only required to set up the application environent on macOS
|
|
|
|
// (but not necessary in normal Cocoa applications where this is set up autmatically)
|
|
|
|
examples_common::run(example_main);
|
|
|
|
}
|