scuffle_flv/video/body/
legacy.rs

1//! Legacy video tag body
2//!
3//! Types and functions defined by the legacy FLV spec, Annex E.4.3.1.
4
5use std::io;
6
7use bytes::Bytes;
8use scuffle_bytes_util::BytesCursorExt;
9use scuffle_bytes_util::zero_copy::Deserialize;
10use scuffle_h264::AVCDecoderConfigurationRecord;
11
12use crate::video::header::legacy::{LegacyVideoTagHeader, LegacyVideoTagHeaderAvcPacket};
13
14/// Legacy FLV `VideoTagBody`
15///
16/// This is a container for video data.
17/// This enum contains the data for the different types of video tags.
18///
19/// Defined by:
20/// - video_file_format_spec_v10.pdf (Chapter 1 - The FLV File Format - Video
21///   tags)
22/// - video_file_format_spec_v10_1.pdf (Annex E.4.3.1 - VIDEODATA)
23#[derive(Debug, Clone, PartialEq)]
24pub enum LegacyVideoTagBody<'a> {
25    /// Empty body because the header contains a [`VideoCommand`](crate::video::header::VideoCommand)
26    Command,
27    /// AVC/H.264 configuration record
28    AvcVideoPacketSeqHdr(AVCDecoderConfigurationRecord<'a>),
29    /// Any other video data
30    Other {
31        /// The video data
32        data: Bytes,
33    },
34}
35
36impl LegacyVideoTagBody<'_> {
37    /// Demux the video tag body from the given reader.
38    ///
39    /// The reader will be consumed entirely.
40    pub fn demux(header: &LegacyVideoTagHeader, reader: &mut io::Cursor<Bytes>) -> io::Result<Self> {
41        match header {
42            LegacyVideoTagHeader::VideoCommand(_) => Ok(Self::Command),
43            LegacyVideoTagHeader::AvcPacket(LegacyVideoTagHeaderAvcPacket::SequenceHeader) => {
44                // AVCVIDEOPACKET
45                let avc_decoder_configuration_record =
46                    AVCDecoderConfigurationRecord::deserialize(scuffle_bytes_util::zero_copy::IoRead::from(reader))?;
47                Ok(Self::AvcVideoPacketSeqHdr(avc_decoder_configuration_record))
48            }
49            _ => Ok(Self::Other {
50                data: reader.extract_remaining(),
51            }),
52        }
53    }
54}