Compare commits
6 commits
main
...
dtmf-examp
Author | SHA1 | Date | |
---|---|---|---|
eeba1e6806 | |||
5d85a687c1 | |||
3b046bdff3 | |||
7b56793c52 | |||
c31409d242 | |||
c189d88fdb |
4 changed files with 585 additions and 203 deletions
567
Cargo.lock
generated
567
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,11 +1,9 @@
|
|||
[package]
|
||||
name = "${REPO_NAME_LOWER}"
|
||||
name = "dfmt-test"
|
||||
license = "MPL-2.0"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
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" }
|
||||
|
@ -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" }
|
||||
anyhow = "1"
|
||||
ctrlc = "3.2"
|
||||
rand = "0.8.5"
|
||||
tokio = { version = "1.17", features = ["full"] }
|
||||
axum = "0.5"
|
||||
axum = "0.6"
|
||||
tower = "0.4"
|
||||
tower-http = { version = "0.3", features = ["add-extension"] }
|
||||
tower-http = { version = "0.4" }
|
||||
|
|
30
README.md
30
README.md
|
@ -1,3 +1,29 @@
|
|||
# ${REPO_NAME}
|
||||
# Audio Commands using DTMF
|
||||
|
||||
${REPO_DESCRIPTION}
|
||||
This is a simple example of how to use DTMF elements of GStreamer with ergonomic Rust code.
|
||||
|
||||
## Installation
|
||||
|
||||
You will need to install the following dependencies from homebrew:
|
||||
|
||||
```bash
|
||||
brew install gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly gst-libav
|
||||
```
|
||||
|
||||
Edit the gst-plugins-bad formula to add the following line:
|
||||
|
||||
```ruby
|
||||
depends_on "spandsp"
|
||||
```
|
||||
|
||||
Set the following environment variables:
|
||||
|
||||
```bash
|
||||
export HOMEBREW_NO_INSTALL_FROM_API=1
|
||||
```
|
||||
|
||||
Then reinstall the formula from source:
|
||||
|
||||
```bash
|
||||
brew reinstall --build-from-source gst-plugins-bad
|
||||
```
|
||||
|
|
182
src/main.rs
182
src/main.rs
|
@ -4,20 +4,103 @@ use anyhow::Result;
|
|||
use gst::glib;
|
||||
use gst::glib::once_cell::sync::Lazy;
|
||||
use gst::prelude::*;
|
||||
use std::{process, thread};
|
||||
use rand::prelude::*;
|
||||
use std::thread;
|
||||
use std::thread::sleep;
|
||||
use tokio::runtime::Builder;
|
||||
|
||||
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
||||
gst::DebugCategory::new("main", gst::DebugColorFlags::empty(), Some("Main function"))
|
||||
});
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct DtmfEvent(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, Copy)]
|
||||
enum DtmfCommand {
|
||||
Start(i32),
|
||||
End(Option<i32>),
|
||||
}
|
||||
|
||||
impl DtmfCommand {
|
||||
fn start(number: i32) -> Self {
|
||||
Self::Start(number)
|
||||
}
|
||||
|
||||
fn end(self) -> Self {
|
||||
match self {
|
||||
Self::Start(number) => Self::End(Some(number)),
|
||||
Self::End(_) => self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&gst::StructureRef> for DtmfCommand {
|
||||
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")?;
|
||||
if structure.get::<bool>("start")? {
|
||||
Ok(Self::Start(number.ok_or_else(|| {
|
||||
anyhow::anyhow!("No number specified for start DTMF command")
|
||||
})?))
|
||||
} else {
|
||||
Ok(Self::End(number))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DtmfCommand> for gst::Structure {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(event: DtmfCommand) -> anyhow::Result<Self> {
|
||||
let structure = match event {
|
||||
DtmfCommand::Start(number) => gst::Structure::builder("dtmf-event")
|
||||
.field("type", 1)
|
||||
.field("start", true)
|
||||
.field("number", number)
|
||||
.field("volume", 36)
|
||||
.build(),
|
||||
DtmfCommand::End(number) => {
|
||||
let Some(number) = number else {
|
||||
anyhow::bail!("Cannot send end DTMF command without a specified number");
|
||||
};
|
||||
gst::Structure::builder("dtmf-event")
|
||||
.field("type", 1)
|
||||
.field("start", false)
|
||||
.field("number", number)
|
||||
.build()
|
||||
}
|
||||
};
|
||||
Ok(structure)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
gst::init()?;
|
||||
|
||||
let pipeline = gst::parse_launch(
|
||||
r#"
|
||||
|
||||
videotestsrc ! videoconvert ! timeoverlay shaded-background=true ! gtksink
|
||||
dtmfsrc name=src ! mix.
|
||||
audiotestsrc freq=0 ! audiomixer name=mix ! dtmfdetect ! audioconvert ! autoaudiosink name=audiosink
|
||||
|
||||
"#,
|
||||
)?
|
||||
|
@ -27,8 +110,6 @@ fn main() -> Result<()> {
|
|||
let context = glib::MainContext::default();
|
||||
let main_loop = glib::MainLoop::new(Some(&context), false);
|
||||
|
||||
pipeline.set_state(gst::State::Playing)?;
|
||||
|
||||
let bus = pipeline.bus().unwrap();
|
||||
bus.add_watch({
|
||||
let main_loop = main_loop.clone();
|
||||
|
@ -38,7 +119,7 @@ fn main() -> Result<()> {
|
|||
match msg.view() {
|
||||
MessageView::Eos(..) => main_loop.quit(),
|
||||
MessageView::Error(err) => {
|
||||
gst::error!(CAT, obj: &err.src().unwrap(),
|
||||
gst::error!(CAT, obj: err.src().unwrap(),
|
||||
"Error from {:?}: {} ({:?})",
|
||||
err.src().map(|s| s.path_string()),
|
||||
err.error(),
|
||||
|
@ -46,6 +127,45 @@ fn main() -> Result<()> {
|
|||
);
|
||||
main_loop.quit();
|
||||
}
|
||||
MessageView::Element(element) => {
|
||||
match element.structure().unwrap().name().as_str() {
|
||||
"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" => {
|
||||
let dtmf_cmd = match DtmfCommand::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 dtmf_cmd {
|
||||
DtmfCommand::Start(number) => {
|
||||
gst::info!(CAT, "Processed DTMF event {}", number);
|
||||
}
|
||||
DtmfCommand::End(_) => {
|
||||
gst::info!(CAT, "Processed ending DTMF event");
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
gst::error!(
|
||||
CAT,
|
||||
"Received unknown event: {:?}",
|
||||
element.structure().unwrap().name()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
glib::Continue(true)
|
||||
|
@ -53,6 +173,50 @@ fn main() -> Result<()> {
|
|||
})
|
||||
.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();
|
||||
|
||||
// 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();
|
||||
loop {
|
||||
let dtmf_cmd = DtmfCommand::start(rng.gen_range(0..15));
|
||||
|
||||
source.send_event(gst::event::CustomUpstream::new(
|
||||
dtmf_cmd.try_into().unwrap(),
|
||||
));
|
||||
gst::info!(CAT, "Sent DTMF event {:?}", dtmf_cmd);
|
||||
|
||||
sleep(std::time::Duration::from_millis(10));
|
||||
|
||||
source.send_event(gst::event::CustomUpstream::new(
|
||||
dtmf_cmd.end().try_into().unwrap(),
|
||||
));
|
||||
|
||||
sleep(std::time::Duration::from_millis(1000));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
thread::spawn({
|
||||
let pipeline_weak = pipeline.downgrade();
|
||||
move || {
|
||||
|
@ -66,12 +230,12 @@ fn main() -> Result<()> {
|
|||
}
|
||||
});
|
||||
|
||||
pipeline.set_state(gst::State::Playing)?;
|
||||
|
||||
ctrlc::set_handler({
|
||||
let pipeline_weak = pipeline.downgrade();
|
||||
let main_loop = main_loop.clone();
|
||||
move || {
|
||||
let pipeline = pipeline_weak.upgrade().unwrap();
|
||||
pipeline.set_state(gst::State::Null).unwrap();
|
||||
process::exit(0);
|
||||
main_loop.quit();
|
||||
}
|
||||
})?;
|
||||
|
||||
|
|
Loading…
Reference in a new issue