Send DTMF events

This commit is contained in:
Rafael Caricio 2023-03-26 18:34:10 +02:00
parent c31409d242
commit 7b56793c52
Signed by: rafaelcaricio
GPG key ID: 3C86DBCE8E93C947
3 changed files with 1400 additions and 6 deletions

1292
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,11 +1,9 @@
[package] [package]
name = "${REPO_NAME_LOWER}" name = "dfmt-test"
license = "MPL-2.0" license = "MPL-2.0"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
gst = { package = "gstreamer", git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs", features = ["v1_20"] } gst = { package = "gstreamer", git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs", features = ["v1_20"] }
gst-video = { package = "gstreamer-video", git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs" } gst-video = { package = "gstreamer-video", git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs" }
@ -13,7 +11,8 @@ gst-audio = { package = "gstreamer-audio", git = "https://gitlab.freedesktop.org
gst-app = { package = "gstreamer-app", git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs" } gst-app = { package = "gstreamer-app", git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs" }
anyhow = "1" anyhow = "1"
ctrlc = "3.2" ctrlc = "3.2"
rand = "0.8.5"
tokio = { version = "1.17", features = ["full"] } tokio = { version = "1.17", features = ["full"] }
axum = "0.5" axum = "0.6"
tower = "0.4" tower = "0.4"
tower-http = { version = "0.3", features = ["add-extension"] } tower-http = { version = "0.4" }

View file

@ -5,24 +5,77 @@ use gst::glib;
use gst::glib::once_cell::sync::Lazy; use gst::glib::once_cell::sync::Lazy;
use gst::prelude::*; use gst::prelude::*;
use std::thread; use std::thread;
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)]
struct DtmfEvent {
number: Option<i32>,
volume: Option<i32>,
start: bool,
}
impl DtmfEvent {
fn from_number(number: i32) -> Self {
Self { number: Some(number), volume: Some(36), start: true }
}
fn end(&self) -> Self {
Self { number: None, volume: None, start: false }
}
}
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.starts_with("dtmf-event") {
anyhow::bail!("Not a dtmf-event structure: {name}");
}
let number = structure.get_optional::<i32>("number")?;
let volume = structure.get_optional::<i32>("volume")?;
let start = structure.get::<bool>("start")?;
Ok(Self { number, volume, start })
}
}
impl From<DtmfEvent> for gst::Structure {
fn from(event: DtmfEvent) -> Self {
let mut st = gst::Structure::builder("dtmf-event")
.field("type", 1)
.field("start", event.start)
.build();
if let Some(number) = event.number {
st.set("number", number);
}
if let Some(volume) = event.volume {
st.set("volume", volume);
}
st
}
}
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#"
videotestsrc ! videoconvert ! timeoverlay shaded-background=true ! gtksink dtmfsrc name=src ! mix.
audiotestsrc freq=20 ! audiomixer name=mix ! tee name=t ! queue ! 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);
@ -45,6 +98,30 @@ fn main() -> Result<()> {
err.debug() err.debug()
); );
main_loop.quit(); main_loop.quit();
},
MessageView::Element(element) => {
let dtmf_event = match DtmfEvent::try_from(element.structure().unwrap()) {
Ok(ev) => ev,
Err(err) => {
gst::error!(CAT, "Failed to parse DTMF event {:?} with error: {:?}", element.structure().unwrap(), err);
return glib::Continue(true);
}
};
match element.structure().unwrap().name().as_str() {
"dtmf-event" => {
gst::info!(CAT, "Detected DTMF event");
}
"dtmf-event-processed" => {
if dtmf_event.start {
gst::info!(CAT, "DTMF event processed: {:?}", dtmf_event);
} else {
gst::info!(CAT, "Ending DTMF event processed");
}
}
_ => {
gst::info!(CAT, "Received unknown event: {:?}", element.structure().unwrap().name());
}
}
} }
_ => (), _ => (),
}; };
@ -53,6 +130,32 @@ fn main() -> Result<()> {
}) })
.expect("Failed to add bus watch"); .expect("Failed to add bus watch");
thread::spawn({
let pipeline_weak = pipeline.downgrade();
move || {
let Some(pipeline) = pipeline_weak.upgrade() else {
gst::error!(CAT, "Pipeline gone...");
return;
};
let source = pipeline.by_name("src").unwrap();
let mut rng = rand::thread_rng();
loop {
let dtmf_event = DtmfEvent::from_number(rng.gen_range(0..15));
source.send_event(gst::event::CustomUpstream::new(dtmf_event.clone().into()));
gst::info!(CAT, "Sent DTMF event {:?}", dtmf_event);
sleep(std::time::Duration::from_millis(10));
source.send_event(gst::event::CustomUpstream::new(dtmf_event.end().into()));
sleep(std::time::Duration::from_millis(1000));
}
}
});
thread::spawn({ thread::spawn({
let pipeline_weak = pipeline.downgrade(); let pipeline_weak = pipeline.downgrade();
move || { move || {