scuffle_flv/
file.rs

1use byteorder::{BigEndian, ReadBytesExt};
2use bytes::{Buf, Bytes};
3
4use super::header::FlvHeader;
5use super::tag::FlvTag;
6
7/// An FLV file is a combination of a [`FlvHeader`] followed by the
8/// `FLVFileBody` (which is just a series of [`FlvTag`]s)
9///
10/// The `FLVFileBody` is defined by:
11/// - video_file_format_spec_v10.pdf (Chapter 1 - The FLV File Format - Page 8)
12/// - video_file_format_spec_v10_1.pdf (Annex E.3 - The FLV File Body)
13#[derive(Debug, Clone, PartialEq)]
14pub struct FlvFile {
15    pub header: FlvHeader,
16    pub tags: Vec<FlvTag>,
17}
18
19impl FlvFile {
20    /// Demux an FLV file from a reader.
21    /// The reader needs to be a [`std::io::Cursor`] with a [`Bytes`] buffer because we
22    /// take advantage of zero-copy reading.
23    pub fn demux(reader: &mut std::io::Cursor<Bytes>) -> std::io::Result<Self> {
24        let header = FlvHeader::demux(reader)?;
25
26        let mut tags = Vec::new();
27        while reader.has_remaining() {
28            // We don't care about the previous tag size, its only really used for seeking
29            // backwards.
30            reader.read_u32::<BigEndian>()?;
31
32            // If there is no more data, we can stop reading.
33            if !reader.has_remaining() {
34                break;
35            }
36
37            // Demux the tag from the reader.
38            let tag = FlvTag::demux(reader)?;
39            tags.push(tag);
40        }
41
42        Ok(FlvFile { header, tags })
43    }
44}