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 = 0x04;
const COMPONENT_LEN: usize = 5;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct AudioComponent {
pub component_tag: u8,
pub iso_code: [u8; 3],
pub bit_stream_mode: u8,
pub num_channels: u8,
pub full_srvc_audio: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct AudioDescriptor {
pub identifier: u32,
pub components: Vec<AudioComponent>,
}
impl Default for AudioDescriptor {
fn default() -> Self {
Self {
identifier: CUEI,
components: Vec::new(),
}
}
}
impl<'a> Parse<'a> for AudioDescriptor {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (identifier, body) = header::descriptor_body(bytes, TAG, "audio_descriptor")?;
if body.is_empty() {
return Err(Error::BufferTooShort {
need: HEADER_LEN + 1,
have: bytes.len(),
what: "audio_descriptor audio_count",
});
}
let audio_count = (body[0] >> 4) as usize; let mut pos = 1;
let mut components = Vec::with_capacity(audio_count);
for _ in 0..audio_count {
if body.len() < pos + COMPONENT_LEN {
return Err(Error::BufferTooShort {
need: HEADER_LEN + pos + COMPONENT_LEN,
have: bytes.len(),
what: "audio_descriptor component",
});
}
let component_tag = body[pos];
let iso_code = [body[pos + 1], body[pos + 2], body[pos + 3]];
let packed = body[pos + 4];
components.push(AudioComponent {
component_tag,
iso_code,
bit_stream_mode: packed >> 5, num_channels: (packed >> 1) & 0x0F, full_srvc_audio: packed & 0x01 != 0, });
pos += COMPONENT_LEN;
}
Ok(Self {
identifier,
components,
})
}
}
impl Serialize for AudioDescriptor {
type Error = Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + 1 + self.components.len() * COMPONENT_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.components.len() > 15 {
return Err(Error::InvalidValue {
field: "audio_descriptor.audio_count",
reason: "more than 15 components (4-bit count)",
});
}
let body_len = 1 + self.components.len() * COMPONENT_LEN;
header::write_header(buf, TAG, self.identifier, body_len);
buf[HEADER_LEN] = ((self.components.len() as u8) << 4) | 0x0F;
let mut pos = HEADER_LEN + 1;
for c in &self.components {
buf[pos] = c.component_tag;
buf[pos + 1..pos + 4].copy_from_slice(&c.iso_code);
buf[pos + 4] = ((c.bit_stream_mode & 0x07) << 5)
| ((c.num_channels & 0x0F) << 1)
| u8::from(c.full_srvc_audio);
pos += COMPONENT_LEN;
}
Ok(need)
}
}
impl<'a> SpliceDescriptorDef<'a> for AudioDescriptor {
const TAG: u8 = TAG;
const NAME: &'static str = "AUDIO";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip() {
let d = AudioDescriptor {
identifier: CUEI,
components: vec![
AudioComponent {
component_tag: 0xFF,
iso_code: *b"eng",
bit_stream_mode: 0b101,
num_channels: 0b1010,
full_srvc_audio: true,
},
AudioComponent {
component_tag: 0x12,
iso_code: *b"spa",
bit_stream_mode: 0,
num_channels: 2,
full_srvc_audio: false,
},
],
};
let bytes = d.to_bytes();
assert_eq!(bytes[0], TAG);
let back = AudioDescriptor::parse(&bytes).unwrap();
assert_eq!(d, back);
assert_eq!(back.to_bytes(), bytes);
}
#[test]
fn round_trip_empty() {
let d = AudioDescriptor::default();
assert_eq!(AudioDescriptor::parse(&d.to_bytes()).unwrap(), d);
}
}