tests: Add integration tests for full tag parsing

This commit is contained in:
David Wendt 2023-04-10 19:33:51 -04:00 committed by kmeisthax
parent e50ee7c139
commit 4d829768a1
4 changed files with 95 additions and 11 deletions

View File

@ -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<Variable<'a>>);
pub struct ScriptData<'a>(pub Vec<Variable<'a>>);
impl<'a> ScriptData<'a> {
/// Parse a script data structure.

View File

@ -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> {

View File

@ -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
}
]))
})
)
}
}

View File

@ -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> {