use std::time::Duration;
use bytes::{BufMut, BytesMut};
pub const TAG_TYPE_AUDIO: u8 = 0x08;
pub const TAG_TYPE_VIDEO: u8 = 0x09;
pub const TAG_TYPE_METADATA: u8 = 0x12;
pub const FLV_TAG_HEADER_SIZE: usize = 11;
pub const SOUND_FORMAT_ALAW: u8 = 7;
pub const SOUND_FORMAT_ULAW: u8 = 8;
pub const SOUND_FORMAT_AAC: u8 = 10;
pub const AAC_SOUND_HEADER: u8 = (SOUND_FORMAT_AAC << 4) | 0x0f;
pub fn write_tag_header(buf: &mut BytesMut, tag_type: u8, body_size: usize, timestamp_ms: u32) {
let timestamp_ext = (timestamp_ms >> 24) as u8;
let timestamp_base = timestamp_ms & 0x00ff_ffff;
buf.put_u8(tag_type);
buf.put_u8((body_size >> 16) as u8);
buf.put_u8((body_size >> 8) as u8);
buf.put_u8(body_size as u8);
buf.put_u8((timestamp_base >> 16) as u8);
buf.put_u8((timestamp_base >> 8) as u8);
buf.put_u8(timestamp_base as u8);
buf.put_u8(timestamp_ext);
buf.put_u8(0);
buf.put_u8(0);
buf.put_u8(0);
}
pub fn composition_time_ms(pts: Duration, dts: Option<Duration>) -> i32 {
match dts {
Some(dts) if pts >= dts => (pts - dts).as_millis().min(0x7f_ffff) as i32,
Some(dts) => -((dts - pts).as_millis().min(0x80_0000) as i32),
None => 0,
}
}
pub fn write_composition_time(buf: &mut BytesMut, cts: i32) {
let encoded = (cts as u32) & 0x00ff_ffff;
buf.put_u8((encoded >> 16) as u8);
buf.put_u8((encoded >> 8) as u8);
buf.put_u8(encoded as u8);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tag_header_layout() {
let mut buf = BytesMut::new();
write_tag_header(&mut buf, TAG_TYPE_VIDEO, 0x010203, 0x0123_4567);
assert_eq!(
buf.as_ref(),
&[0x09, 0x01, 0x02, 0x03, 0x23, 0x45, 0x67, 0x01, 0, 0, 0]
);
let mut buf = BytesMut::new();
write_tag_header(&mut buf, TAG_TYPE_AUDIO, 5, 20);
assert_eq!(buf.as_ref(), &[0x08, 0, 0, 5, 0, 0, 20, 0, 0, 0, 0]);
}
#[test]
fn composition_time_clamps_to_signed_24bit() {
assert_eq!(
composition_time_ms(Duration::from_millis(40), Some(Duration::from_millis(0))),
40
);
assert_eq!(
composition_time_ms(Duration::ZERO, Some(Duration::from_millis(40))),
-40
);
assert_eq!(composition_time_ms(Duration::ZERO, None), 0);
assert_eq!(
composition_time_ms(Duration::from_secs(100 * 24 * 3600), Some(Duration::ZERO)),
0x7f_ffff
);
assert_eq!(
composition_time_ms(Duration::ZERO, Some(Duration::from_secs(100 * 24 * 3600))),
-0x80_0000
);
}
#[test]
fn composition_time_encoding_matches_mask() {
let mut buf = BytesMut::new();
write_composition_time(&mut buf, -1);
assert_eq!(buf.as_ref(), &[0xff, 0xff, 0xff]);
let mut buf = BytesMut::new();
write_composition_time(&mut buf, -0x80_0000);
assert_eq!(buf.as_ref(), &[0x80, 0x00, 0x00]);
let mut buf = BytesMut::new();
write_composition_time(&mut buf, 0x7f_ffff);
assert_eq!(buf.as_ref(), &[0x7f, 0xff, 0xff]);
}
#[test]
fn aac_sound_header_has_nonzero_rate_bits() {
assert_eq!(AAC_SOUND_HEADER, 0xAF);
}
}