1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use crate::error::ParseError;
use std::convert::TryFrom;

/// FLV file header

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Header {
    /// File version (for example, 0x01 for FLV version 1)

    pub version: u8,

    /// audio tags are present

    pub audio_flag: bool,

    /// video tags are present

    pub video_flag: bool,

    /// The DataOffset field usually has a value of 9 for FLV version 1.

    /// This field is present to accommodate larger headers in future versions.

    pub data_offset: u32,
}

impl Header {
    pub const SIGNATURE: [u8; 3] = [b'F', b'L', b'V'];
    pub const SIZE: usize = 9;
}

impl TryFrom<[u8; Header::SIZE]> for Header {
    type Error = ParseError;

    fn try_from(
        [f, l, v, version, flag, d1, d2, d3, d4]: [u8; Header::SIZE],
    ) -> Result<Self, ParseError> {
        match [f, l, v] {
            Self::SIGNATURE => {}
            [s1, s2, s3] => return Err(ParseError::HeaderSignature(s1, s2, s3)),
        }

        let reserved_flag = 0b11111010 & flag;
        if reserved_flag != 0 {
            return Err(ParseError::HeaderTypeFlagsReserved(reserved_flag));
        }

        let audio_flag = 0b00000100 & flag != 0;
        let video_flag = 0b00000001 & flag != 0;
        let data_offset = u32::from_be_bytes([d1, d2, d3, d4]);

        Ok(Self {
            version,
            audio_flag,
            video_flag,
            data_offset,
        })
    }
}

impl From<Header> for [u8; Header::SIZE] {
    fn from(h: Header) -> Self {
        let flag =
            if h.audio_flag { 0b0000100 } else { 0 } | if h.video_flag { 0b00000001 } else { 0 };

        let [o1, o2, o3, o4] = h.data_offset.to_be_bytes();

        [
            Header::SIGNATURE[0],
            Header::SIGNATURE[1],
            Header::SIGNATURE[2],
            h.version,
            flag,
            o1,
            o2,
            o3,
            o4,
        ]
    }
}

#[test]
fn parse_header() {
    let header = Header {
        version: 1,
        audio_flag: true,
        video_flag: true,
        data_offset: 0x12345678,
    };

    let bytes: [u8; Header::SIZE] = header.into();

    let parsed = Header::try_from(bytes);

    assert_eq!(Ok(header), parsed);
    assert_eq!(Ok(bytes), parsed.map(|h| h.into()));
}

/// Flv tag type

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TagType {
    Audio,        // 8

    Video,        // 9

    ScriptData,   // 18

    Reserved(u8), // all others

}

impl From<TagType> for u8 {
    fn from(tt: TagType) -> Self {
        match tt {
            TagType::Audio => 8,
            TagType::Video => 9,
            TagType::ScriptData => 18,
            TagType::Reserved(n) => n,
        }
    }
}

/// Flv tag header

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TagHeader {
    pub tag_type: TagType,
    pub data_size: u32,
    pub timestamp: i32, // UI24 + UI8 => SI32

                        // pub stream_id: u32, // UI24 always 0

}

impl TagHeader {
    pub const SIZE: usize = (8 + 24 + 24 + 8 + 24) / 8;
    pub const MAX_DATA_SIZE: usize = 0x00ffffff; // u24::max_value()

}

impl From<[u8; TagHeader::SIZE]> for TagHeader {
    fn from([tt, s1, s2, s3, t1, t2, t3, t0, _, _, _]: [u8; TagHeader::SIZE]) -> Self {
        let tag_type = match tt {
            8 => TagType::Audio,
            9 => TagType::Video,
            18 => TagType::ScriptData,
            n => TagType::Reserved(n),
        };

        // UI24 big endian

        let data_size = u32::from_be_bytes([0, s1, s2, s3]);

        // t0: Extension of the timestamp field to form a SI32 value.

        // This field represents the upper 8 bits, while the previous timestamp

        // field represents the lower 24 bits of the time in milliseconds.

        //

        // t1~t3: time in milliseconds which the data in this tag applies.

        // This value is relative to the first tag in the FLV file, which always

        // has a timestamp of 0.

        let timestamp = i32::from_be_bytes([t0, t1, t2, t3]);

        TagHeader {
            tag_type,
            data_size,
            timestamp,
        }
    }
}

impl From<TagHeader> for [u8; TagHeader::SIZE] {
    fn from(h: TagHeader) -> Self {
        let tt = h.tag_type.into();

        let [_, s1, s2, s3] = h.data_size.to_be_bytes();
        let [t0, t1, t2, t3] = h.timestamp.to_be_bytes();

        [tt, s1, s2, s3, t1, t2, t3, t0, 0, 0, 0]
    }
}

/// Sound format

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SoundFormat {
    LinearPCMPlatformEndian = 0,
    ADPCM = 1,
    MP3 = 2,
    LinearPCMLittleEndian = 3,
    Nellymoser16 = 4,
    Nellymoser8 = 5,
    Nellymoser = 6,
    G711ALaw = 7,
    G711MuLaw = 8,
    Reserved = 9,
    AAC = 10,
    Speex = 11,
    MP38kHz = 14,
    DeviceSpecific = 15,
}

impl TryFrom<u8> for SoundFormat {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use SoundFormat::*;
        Ok(match (value & 0b_1111_0000) >> 4 {
            0 => LinearPCMPlatformEndian,
            1 => ADPCM,
            2 => MP3,
            3 => LinearPCMLittleEndian,
            4 => Nellymoser16,
            5 => Nellymoser8,
            6 => Nellymoser,
            7 => G711ALaw,
            8 => G711MuLaw,
            9 => Reserved,
            10 => AAC,
            11 => Speex,
            14 => MP38kHz,
            15 => DeviceSpecific,
            n => return Err(ParseError::SoundFormat(n)),
        })
    }
}

impl From<SoundFormat> for u8 {
    fn from(sf: SoundFormat) -> Self {
        (sf as u8) << 4
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SoundRate {
    R5p5kHz = 0,
    R11kHz = 1,
    R22kHz = 2,
    R44kHz = 3,
}

impl TryFrom<u8> for SoundRate {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use SoundRate::*;
        Ok(match (value & 0b_0000_1100) >> 2 {
            0 => R5p5kHz,
            1 => R11kHz,
            2 => R22kHz,
            3 => R44kHz,
            n => return Err(ParseError::SoundRate(n)),
        })
    }
}

impl From<SoundRate> for u8 {
    fn from(sr: SoundRate) -> Self {
        (sr as u8) << 2
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SoundSize {
    S8Bit = 0,
    S16Bit = 1,
}

impl TryFrom<u8> for SoundSize {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use SoundSize::*;
        Ok(match (value & 0b_0000_0010) >> 1 {
            0 => S8Bit,
            1 => S16Bit,
            n => return Err(ParseError::SoundSize(n)),
        })
    }
}

impl From<SoundSize> for u8 {
    fn from(ss: SoundSize) -> Self {
        (ss as u8) << 1
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SoundType {
    Mono = 0,
    Stereo = 1,
}

impl TryFrom<u8> for SoundType {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use SoundType::*;
        Ok(match value & 0b_0000_0001 {
            0 => Mono,
            1 => Stereo,
            n => return Err(ParseError::SoundType(n)),
        })
    }
}

impl From<SoundType> for u8 {
    fn from(st: SoundType) -> Self {
        st as u8
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AudioDataHeader {
    pub sound_format: SoundFormat,
    pub sound_rate: SoundRate,
    pub sound_size: SoundSize,
    pub sound_type: SoundType,
}

impl TryFrom<u8> for AudioDataHeader {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        let sound_format = SoundFormat::try_from(value)?;
        let sound_rate = SoundRate::try_from(value)?;
        let sound_size = SoundSize::try_from(value)?;
        let sound_type = SoundType::try_from(value)?;

        Ok(Self {
            sound_format,
            sound_rate,
            sound_size,
            sound_type,
        })
    }
}

impl From<AudioDataHeader> for u8 {
    fn from(h: AudioDataHeader) -> Self {
        u8::from(h.sound_format)
            | u8::from(h.sound_rate)
            | u8::from(h.sound_size)
            | u8::from(h.sound_type)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum VideoFrameType {
    KeyFrame = 1,
    InterFrame = 2,
    DisposableInterFrame = 3,
    GeneratedKeyFrame = 4,
    VideoInfoOrCommandFrame = 5,
}

impl TryFrom<u8> for VideoFrameType {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use VideoFrameType::*;
        Ok(match (value & 0xf0) >> 4 {
            1 => KeyFrame,
            2 => InterFrame,
            3 => DisposableInterFrame,
            4 => GeneratedKeyFrame,
            5 => VideoInfoOrCommandFrame,
            n => return Err(ParseError::VideoFrameType(n)),
        })
    }
}

impl From<VideoFrameType> for u8 {
    fn from(vft: VideoFrameType) -> Self {
        (vft as u8) << 4
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum VideoCodecId {
    JPEG = 1,
    SorensonH263 = 2,
    ScreenVideo = 3,
    On2VP6 = 4,
    On2VP6WithAlpha = 5,
    ScreenVideoVersion2 = 6,
    AVC = 7,
    // AV1 = 13,

}

impl TryFrom<u8> for VideoCodecId {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use VideoCodecId::*;
        Ok(match value & 0xf {
            1 => JPEG,
            2 => SorensonH263,
            3 => ScreenVideo,
            4 => On2VP6,
            5 => On2VP6WithAlpha,
            6 => ScreenVideoVersion2,
            7 => AVC,
            // 13 => AV1,

            n => return Err(ParseError::VideoCodecId(n)),
        })
    }
}

impl From<VideoCodecId> for u8 {
    fn from(vci: VideoCodecId) -> Self {
        vci as u8
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct VideoDataHeader {
    pub frame_type: VideoFrameType,
    pub codec_id: VideoCodecId,
}

impl TryFrom<u8> for VideoDataHeader {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        let frame_type = VideoFrameType::try_from(value)?;
        let codec_id = VideoCodecId::try_from(value)?;

        Ok(Self {
            frame_type,
            codec_id,
        })
    }
}

impl From<VideoDataHeader> for u8 {
    fn from(h: VideoDataHeader) -> Self {
        u8::from(h.frame_type) | u8::from(h.codec_id)
    }
}

/// SeekFlag for client-side seeking video frame sequence

///

/// if FrameType = 5, instead of a video payload, the message stream contains

/// a UI8 with the following meaning:

/// * 0 = Start of client-side seeking video frame sequence

/// * 1 = End of client-side seeking video frame sequence

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SeekFlag {
    Start = 0,
    End = 1,
}

impl TryFrom<u8> for SeekFlag {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use SeekFlag::*;

        Ok(match value {
            0 => Start,
            1 => End,
            n => return Err(ParseError::SeekFlag(n)),
        })
    }
}

impl From<SeekFlag> for u8 {
    fn from(sf: SeekFlag) -> Self {
        sf as u8
    }
}

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct MetaData {
    pub duration: f64,
    pub width: f64,
    pub height: f64,
    pub videodatarate: f64,
    pub framerate: f64,
    pub videocodecid: f64,
    pub audiosamplerate: f64,
    pub audiosamplesize: f64,
    pub stereo: bool,
    pub audiocodecid: f64,
    pub filesize: f64,
}