From 4d829768a119d6fc2d4c6e32b3dbcf0918bc052a Mon Sep 17 00:00:00 2001 From: David Wendt Date: Mon, 10 Apr 2023 19:33:51 -0400 Subject: [PATCH] tests: Add integration tests for full tag parsing --- flv/src/script.rs | 6 ++-- flv/src/sound.rs | 10 +++--- flv/src/tag.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++ flv/src/video.rs | 6 ++-- 4 files changed, 95 insertions(+), 11 deletions(-) diff --git a/flv/src/script.rs b/flv/src/script.rs index 37e613fab..8d790f397 100644 --- a/flv/src/script.rs +++ b/flv/src/script.rs @@ -105,8 +105,8 @@ impl<'a> Value<'a> { /// otherwise identical. #[derive(PartialEq, Debug)] pub struct Variable<'a> { - name: &'a [u8], - data: Value<'a>, + pub name: &'a [u8], + pub data: Value<'a>, } impl<'a> Variable<'a> { @@ -119,7 +119,7 @@ impl<'a> Variable<'a> { } #[derive(PartialEq, Debug)] -pub struct ScriptData<'a>(Vec>); +pub struct ScriptData<'a>(pub Vec>); impl<'a> ScriptData<'a> { /// Parse a script data structure. diff --git a/flv/src/sound.rs b/flv/src/sound.rs index 3b5a46f3f..ec4fc9329 100644 --- a/flv/src/sound.rs +++ b/flv/src/sound.rs @@ -111,11 +111,11 @@ pub enum AudioDataType<'a> { #[derive(PartialEq, Eq, Debug)] pub struct AudioData<'a> { - format: SoundFormat, - rate: SoundRate, - size: SoundSize, - sound_type: SoundType, - data: AudioDataType<'a>, + pub format: SoundFormat, + pub rate: SoundRate, + pub size: SoundSize, + pub sound_type: SoundType, + pub data: AudioDataType<'a>, } impl<'a> AudioData<'a> { diff --git a/flv/src/tag.rs b/flv/src/tag.rs index fa435ca9a..4cd59f00c 100644 --- a/flv/src/tag.rs +++ b/flv/src/tag.rs @@ -4,12 +4,14 @@ use crate::sound::AudioData; use crate::video::VideoData; #[repr(u8)] +#[derive(PartialEq, Debug)] pub enum TagData<'a> { Audio(AudioData<'a>) = 8, Video(VideoData<'a>) = 9, Script(ScriptData<'a>) = 18, } +#[derive(PartialEq, Debug)] pub struct Tag<'a> { timestamp: i32, stream_id: u32, //24 bits max @@ -50,3 +52,85 @@ impl<'a> Tag<'a> { } } } + +#[cfg(test)] +mod tests { + use crate::reader::FlvReader; + use crate::script::{ScriptData, Value, Variable}; + use crate::sound::{AudioData, AudioDataType, SoundFormat, SoundRate, SoundSize, SoundType}; + use crate::tag::{Tag, TagData}; + use crate::video::{CodecId, FrameType, VideoData, VideoPacket}; + + #[test] + fn read_tag_sounddata() { + let data = [ + 0x08, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0xFB, 0x12, 0x34, + 0x56, 0x78, + ]; + let mut reader = FlvReader::from_source(&data); + + assert_eq!( + Tag::parse(&mut reader), + Some(Tag { + timestamp: 0, + stream_id: 0x5000, + data: TagData::Audio(AudioData { + format: SoundFormat::Speex, + rate: SoundRate::R44_000, + size: SoundSize::Bits16, + sound_type: SoundType::Stereo, + data: AudioDataType::Raw(&[0x12, 0x34, 0x56, 0x78]) + }) + }) + ) + } + + #[test] + fn read_tag_videodata() { + let data = [ + 0x09, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x21, 0x12, 0x34, + 0x56, 0x78, + ]; + let mut reader = FlvReader::from_source(&data); + + assert_eq!( + Tag::parse(&mut reader), + Some(Tag { + timestamp: 0, + stream_id: 0x5000, + data: TagData::Video(VideoData { + frame_type: FrameType::Keyframe, + codec_id: CodecId::SorensonH263, + data: VideoPacket::Data(&[0x12, 0x34, 0x56, 0x78]) + }) + }) + ) + } + + #[test] + fn read_tag_scriptdata() { + let data = [ + 0x12, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x03, 0x01, + 0x02, 0x03, 0x06, 0x00, 0x03, 0x01, 0x02, 0x03, 0x05, 0x00, 0x00, 0x09, + ]; + let mut reader = FlvReader::from_source(&data); + + assert_eq!( + Tag::parse(&mut reader), + Some(Tag { + timestamp: 0, + stream_id: 0x5000, + data: TagData::Script(ScriptData(vec![ + Variable { + name: &[0x01, 0x02, 0x03], + data: Value::Undefined + }, + Variable { + name: &[0x01, 0x02, 0x03], + data: Value::Null + } + ])) + }) + ) + } +} diff --git a/flv/src/video.rs b/flv/src/video.rs index 3924465b4..4041a899d 100644 --- a/flv/src/video.rs +++ b/flv/src/video.rs @@ -86,9 +86,9 @@ pub enum VideoPacket<'a> { #[derive(PartialEq, Eq, Debug)] pub struct VideoData<'a> { - frame_type: FrameType, - codec_id: CodecId, - data: VideoPacket<'a>, + pub frame_type: FrameType, + pub codec_id: CodecId, + pub data: VideoPacket<'a>, } impl<'a> VideoData<'a> {