use alloc::vec::Vec;
use super::header::{self, CUEI, HEADER_LEN};
use crate::error::{Error, Result};
use crate::traits::SpliceDescriptorDef;
use dvb_common::{Parse, Serialize};
pub const TAG: u8 = 0x01;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DtmfDescriptor {
pub identifier: u32,
pub preroll: u8,
pub dtmf_chars: Vec<u8>,
}
impl Default for DtmfDescriptor {
fn default() -> Self {
Self {
identifier: CUEI,
preroll: 0,
dtmf_chars: Vec::new(),
}
}
}
impl DtmfDescriptor {
#[must_use]
pub fn preroll_duration(&self) -> core::time::Duration {
core::time::Duration::from_millis(u64::from(self.preroll) * 100)
}
}
impl<'a> Parse<'a> for DtmfDescriptor {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (identifier, body) = header::descriptor_body(bytes, TAG, "DTMF_descriptor")?;
if body.len() < 2 {
return Err(Error::BufferTooShort {
need: HEADER_LEN + 2,
have: bytes.len(),
what: "DTMF_descriptor preroll/count",
});
}
let preroll = body[0];
let dtmf_count = (body[1] >> 5) as usize; if body.len() < 2 + dtmf_count {
return Err(Error::BufferTooShort {
need: HEADER_LEN + 2 + dtmf_count,
have: bytes.len(),
what: "DTMF_descriptor chars",
});
}
Ok(Self {
identifier,
preroll,
dtmf_chars: body[2..2 + dtmf_count].to_vec(),
})
}
}
impl Serialize for DtmfDescriptor {
type Error = Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + 2 + self.dtmf_chars.len()
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let need = self.serialized_len();
if buf.len() < need {
return Err(Error::OutputBufferTooSmall {
need,
have: buf.len(),
});
}
if self.dtmf_chars.len() > 7 {
return Err(Error::InvalidValue {
field: "DTMF_descriptor.dtmf_count",
reason: "more than 7 DTMF characters (3-bit count)",
});
}
header::write_header(buf, TAG, self.identifier, 2 + self.dtmf_chars.len());
buf[HEADER_LEN] = self.preroll;
buf[HEADER_LEN + 1] = ((self.dtmf_chars.len() as u8) << 5) | 0x1F;
buf[HEADER_LEN + 2..need].copy_from_slice(&self.dtmf_chars);
Ok(need)
}
}
impl<'a> SpliceDescriptorDef<'a> for DtmfDescriptor {
const TAG: u8 = TAG;
const NAME: &'static str = "DTMF";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip() {
let d = DtmfDescriptor {
identifier: CUEI,
preroll: 40,
dtmf_chars: vec![b'1', b'2', b'3', b'*'],
};
let bytes = d.to_bytes();
assert_eq!(bytes[0], TAG);
let back = DtmfDescriptor::parse(&bytes).unwrap();
assert_eq!(d, back);
assert_eq!(back.to_bytes(), bytes);
assert_eq!(back.preroll_duration(), core::time::Duration::from_secs(4));
}
#[test]
fn round_trip_empty_chars() {
let d = DtmfDescriptor::default();
let back = DtmfDescriptor::parse(&d.to_bytes()).unwrap();
assert_eq!(d, back);
}
}