Add Examples #1
1 changed files with 48 additions and 28 deletions
76
src/main.rs
76
src/main.rs
|
@ -14,19 +14,27 @@ static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|||
});
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct DtmfEvent {
|
||||
number: Option<i32>,
|
||||
volume: Option<i32>,
|
||||
start: bool,
|
||||
enum DtmfEvent {
|
||||
Start(DtmfStart),
|
||||
End(i32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct DtmfStart {
|
||||
number: i32,
|
||||
volume: i32,
|
||||
}
|
||||
|
||||
impl DtmfEvent {
|
||||
fn from_number(number: i32) -> Self {
|
||||
Self { number: Some(number), volume: Some(36), start: true }
|
||||
fn start(number: i32) -> Self {
|
||||
Self::Start(DtmfStart { number, volume: 36 })
|
||||
}
|
||||
|
||||
fn end(&self) -> Self {
|
||||
Self { number: None, volume: None, start: false }
|
||||
fn end(self) -> Self {
|
||||
match self {
|
||||
Self::Start(start) => Self::End(start.number),
|
||||
Self::End(_) => self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,26 +46,35 @@ impl TryFrom<&gst::StructureRef> for DtmfEvent {
|
|||
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 })
|
||||
let number = structure.get::<i32>("number")?;
|
||||
return if structure.get::<bool>("start")? {
|
||||
let volume = structure.get::<i32>("volume")?;
|
||||
Ok(Self::Start(DtmfStart { number, volume }))
|
||||
} else {
|
||||
Ok(Self::End(number))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
match event {
|
||||
DtmfEvent::Start(start) => {
|
||||
gst::Structure::builder("dtmf-event")
|
||||
.field("type", 1)
|
||||
.field("start", true)
|
||||
.field("number", start.number)
|
||||
.field("volume", start.volume)
|
||||
.build()
|
||||
}
|
||||
DtmfEvent::End(number) => {
|
||||
gst::Structure::builder("dtmf-event")
|
||||
.field("type", 1)
|
||||
.field("start", false)
|
||||
.field("number", number)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
if let Some(volume) = event.volume {
|
||||
st.set("volume", volume);
|
||||
}
|
||||
st
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -103,7 +120,7 @@ fn main() -> Result<()> {
|
|||
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);
|
||||
gst::info!(CAT, "Failed to parse DTMF event {:?} with error: {:?}", element.structure().unwrap(), err);
|
||||
return glib::Continue(true);
|
||||
}
|
||||
};
|
||||
|
@ -112,10 +129,13 @@ fn main() -> Result<()> {
|
|||
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");
|
||||
match dtmf_event {
|
||||
DtmfEvent::Start(start) => {
|
||||
gst::info!(CAT, "DTMF event {} processed", start.number);
|
||||
}
|
||||
DtmfEvent::End(number) => {
|
||||
gst::info!(CAT, "Ending DTMF event {number} processed");
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
|
@ -142,7 +162,7 @@ fn main() -> Result<()> {
|
|||
|
||||
let mut rng = rand::thread_rng();
|
||||
loop {
|
||||
let dtmf_event = DtmfEvent::from_number(rng.gen_range(0..15));
|
||||
let dtmf_event = DtmfEvent::start(rng.gen_range(0..15));
|
||||
|
||||
source.send_event(gst::event::CustomUpstream::new(dtmf_event.clone().into()));
|
||||
gst::info!(CAT, "Sent DTMF event {:?}", dtmf_event);
|
||||
|
|
Loading…
Reference in a new issue