Send DTMF events typed

This commit is contained in:
Rafael Caricio 2023-03-26 22:26:46 +02:00
parent 3b046bdff3
commit 5d85a687c1
Signed by: rafaelcaricio
GPG key ID: 3C86DBCE8E93C947

View file

@ -4,41 +4,51 @@ use anyhow::Result;
use gst::glib; use gst::glib;
use gst::glib::once_cell::sync::Lazy; use gst::glib::once_cell::sync::Lazy;
use gst::prelude::*; use gst::prelude::*;
use rand::prelude::*;
use std::thread; use std::thread;
use std::thread::sleep; use std::thread::sleep;
use rand::prelude::*;
use tokio::runtime::Builder; use tokio::runtime::Builder;
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| { static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
gst::DebugCategory::new("main", gst::DebugColorFlags::empty(), Some("Main function")) gst::DebugCategory::new("main", gst::DebugColorFlags::empty(), Some("Main function"))
}); });
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
enum DtmfEvent { struct DtmfEvent(i32);
Start(DtmfStart),
End(i32), impl TryFrom<&gst::StructureRef> for DtmfEvent {
type Error = anyhow::Error;
fn try_from(structure: &gst::StructureRef) -> anyhow::Result<Self> {
let name = structure.name().to_string();
if name != "dtmf-event" {
anyhow::bail!("Not a dtmf-event structure: {name}");
}
let number = structure.get::<i32>("number")?;
Ok(Self(number))
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
struct DtmfStart { enum DtmfCommand {
number: i32, Start(i32),
volume: i32, End(Option<i32>),
} }
impl DtmfEvent { impl DtmfCommand {
fn start(number: i32) -> Self { fn start(number: i32) -> Self {
Self::Start(DtmfStart { number, volume: 36 }) Self::Start(number)
} }
fn end(self) -> Self { fn end(self) -> Self {
match self { match self {
Self::Start(start) => Self::End(start.number), Self::Start(number) => Self::End(Some(number)),
Self::End(_) => self, Self::End(_) => self,
} }
} }
} }
impl TryFrom<&gst::StructureRef> for DtmfEvent { impl TryFrom<&gst::StructureRef> for DtmfCommand {
type Error = anyhow::Error; type Error = anyhow::Error;
fn try_from(structure: &gst::StructureRef) -> anyhow::Result<Self> { fn try_from(structure: &gst::StructureRef) -> anyhow::Result<Self> {
@ -46,59 +56,60 @@ impl TryFrom<&gst::StructureRef> for DtmfEvent {
if !name.starts_with("dtmf-event") { if !name.starts_with("dtmf-event") {
anyhow::bail!("Not a dtmf-event structure: {name}"); anyhow::bail!("Not a dtmf-event structure: {name}");
} }
let number = structure.get::<i32>("number")?; let number = structure.get_optional::<i32>("number")?;
return if structure.get::<bool>("start")? { if structure.get::<bool>("start")? {
let volume = structure.get::<i32>("volume")?; Ok(Self::Start(number.ok_or_else(|| {
Ok(Self::Start(DtmfStart { number, volume })) anyhow::anyhow!("No number specified for start DTMF command")
})?))
} else { } else {
Ok(Self::End(number)) Ok(Self::End(number))
} }
} }
} }
impl From<DtmfEvent> for gst::Structure { impl TryFrom<DtmfCommand> for gst::Structure {
fn from(event: DtmfEvent) -> Self { type Error = anyhow::Error;
match event {
DtmfEvent::Start(start) => { fn try_from(event: DtmfCommand) -> anyhow::Result<Self> {
gst::Structure::builder("dtmf-event") let structure = match event {
.field("type", 1) DtmfCommand::Start(number) => gst::Structure::builder("dtmf-event")
.field("start", true) .field("type", 1)
.field("number", start.number) .field("start", true)
.field("volume", start.volume) .field("number", number)
.build() .field("volume", 36)
} .build(),
DtmfEvent::End(number) => { DtmfCommand::End(number) => {
let Some(number) = number else {
anyhow::bail!("Cannot send end DTMF command without a specified number");
};
gst::Structure::builder("dtmf-event") gst::Structure::builder("dtmf-event")
.field("type", 1) .field("type", 1)
.field("start", false) .field("start", false)
.field("number", number) .field("number", number)
.build() .build()
} }
} };
Ok(structure)
} }
} }
fn main() -> Result<()> { fn main() -> Result<()> {
gst::init()?; gst::init()?;
//videotestsrc ! videoconvert ! timeoverlay shaded-background=true ! gtksink
let pipeline = gst::parse_launch( let pipeline = gst::parse_launch(
r#" r#"
dtmfsrc name=src ! mix. dtmfsrc name=src ! mix.
audiotestsrc freq=20 ! audiomixer name=mix ! tee name=t ! queue ! audioconvert ! autoaudiosink name=audiosink audiotestsrc freq=0 ! audiomixer name=mix ! dtmfdetect ! audioconvert ! autoaudiosink name=audiosink
"#, "#,
)? )?
.downcast::<gst::Pipeline>() .downcast::<gst::Pipeline>()
.unwrap(); .unwrap();
// t.! queue ! audioconvert ! dtmfdetect
let context = glib::MainContext::default(); let context = glib::MainContext::default();
let main_loop = glib::MainLoop::new(Some(&context), false); let main_loop = glib::MainLoop::new(Some(&context), false);
pipeline.set_state(gst::State::Playing)?;
let bus = pipeline.bus().unwrap(); let bus = pipeline.bus().unwrap();
bus.add_watch({ bus.add_watch({
let main_loop = main_loop.clone(); let main_loop = main_loop.clone();
@ -108,38 +119,50 @@ fn main() -> Result<()> {
match msg.view() { match msg.view() {
MessageView::Eos(..) => main_loop.quit(), MessageView::Eos(..) => main_loop.quit(),
MessageView::Error(err) => { MessageView::Error(err) => {
gst::error!(CAT, obj: &err.src().unwrap(), gst::error!(CAT, obj: err.src().unwrap(),
"Error from {:?}: {} ({:?})", "Error from {:?}: {} ({:?})",
err.src().map(|s| s.path_string()), err.src().map(|s| s.path_string()),
err.error(), err.error(),
err.debug() err.debug()
); );
main_loop.quit(); main_loop.quit();
}, }
MessageView::Element(element) => { MessageView::Element(element) => {
let dtmf_event = match DtmfEvent::try_from(element.structure().unwrap()) {
Ok(ev) => ev,
Err(err) => {
gst::info!(CAT, "Failed to parse DTMF event {:?} with error: {:?}", element.structure().unwrap(), err);
return glib::Continue(true);
}
};
match element.structure().unwrap().name().as_str() { match element.structure().unwrap().name().as_str() {
"dtmf-event" => { "dtmf-event" => {
gst::info!(CAT, "Detected DTMF event"); let dtmf_event = DtmfEvent::try_from(element.structure().unwrap())
.expect("Failed to parse DTMF event");
gst::info!(CAT, "Detected DTMF event: {:?}", dtmf_event);
} }
"dtmf-event-processed" => { "dtmf-event-processed" => {
match dtmf_event { let dtmf_cmd = match DtmfCommand::try_from(element.structure().unwrap())
DtmfEvent::Start(start) => { {
gst::info!(CAT, "DTMF event {} processed", start.number); Ok(ev) => ev,
Err(err) => {
gst::error!(
CAT,
"Failed to parse DTMF event {:?} with error: {:?}",
element.structure().unwrap(),
err
);
return glib::Continue(true);
} }
DtmfEvent::End(number) => { };
gst::info!(CAT, "Ending DTMF event {number} processed"); match dtmf_cmd {
DtmfCommand::Start(number) => {
gst::info!(CAT, "Processed DTMF event {}", number);
}
DtmfCommand::End(_) => {
gst::info!(CAT, "Processed ending DTMF event");
} }
} }
} }
_ => { _ => {
gst::info!(CAT, "Received unknown event: {:?}", element.structure().unwrap().name()); gst::error!(
CAT,
"Received unknown event: {:?}",
element.structure().unwrap().name()
);
} }
} }
} }
@ -160,16 +183,34 @@ fn main() -> Result<()> {
let source = pipeline.by_name("src").unwrap(); let source = pipeline.by_name("src").unwrap();
// wait pipeline to be running
let bus = pipeline.bus().unwrap();
while let Some(msg) = bus.timed_pop(None) {
use gst::MessageView;
if let MessageView::StateChanged(state_changed) = msg.view() {
if state_changed.src().unwrap() == &source
&& state_changed.current() == gst::State::Playing
{
break;
}
}
}
gst::info!(CAT, "Pipeline is running");
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
loop { loop {
let dtmf_event = DtmfEvent::start(rng.gen_range(0..15)); let dtmf_cmd = DtmfCommand::start(rng.gen_range(0..15));
source.send_event(gst::event::CustomUpstream::new(dtmf_event.clone().into())); source.send_event(gst::event::CustomUpstream::new(
gst::info!(CAT, "Sent DTMF event {:?}", dtmf_event); dtmf_cmd.try_into().unwrap(),
));
gst::info!(CAT, "Sent DTMF event {:?}", dtmf_cmd);
sleep(std::time::Duration::from_millis(10)); sleep(std::time::Duration::from_millis(10));
source.send_event(gst::event::CustomUpstream::new(dtmf_event.end().into())); source.send_event(gst::event::CustomUpstream::new(
dtmf_cmd.end().try_into().unwrap(),
));
sleep(std::time::Duration::from_millis(1000)); sleep(std::time::Duration::from_millis(1000));
} }
@ -189,6 +230,8 @@ fn main() -> Result<()> {
} }
}); });
pipeline.set_state(gst::State::Playing)?;
ctrlc::set_handler({ ctrlc::set_handler({
let main_loop = main_loop.clone(); let main_loop = main_loop.clone();
move || { move || {