use alloc::vec::Vec;
use crate::amf0::{Amf0Limits, Amf0Value, amf0_parse_values_with};
use crate::flv::{OnMetadata, PublishMessage, PublishTrack, publish_open};
use crate::message::MessageTypeId;
const TEST_AMF0_LIMITS: Amf0Limits = Amf0Limits {
max_depth: 32,
max_values: 4096,
};
const AVC_CONFIG: &[u8] = &[
0x01, 0x64, 0x00, 0x1E, 0xFF, 0xE1, 0x00, 0x19, 0x67, 0x64, 0x00, 0x1E, 0xAC, 0xD9, 0x40, 0xA0,
0x2F, 0xF9, 0x70, 0x11, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03, 0x00, 0x3C, 0x0F, 0x18,
0x31, 0x96, 0x01, 0x00, 0x05, 0x68, 0xEB, 0xEC, 0xB2, 0x2C,
];
const ASC: &[u8] = &[0x12, 0x08];
const AMF0_STRING_ONMETADATA_LEN: usize = 3 + 10;
fn av_and_aac() -> Vec<PublishTrack> {
alloc::vec![
PublishTrack::Video {
width: 320,
height: 240,
avc_config: Vec::from(AVC_CONFIG),
},
PublishTrack::Audio {
sample_rate: 44_100,
channel_count: 1,
asc: Vec::from(ASC),
},
]
}
#[test]
fn the_plan_is_metadata_then_video_then_audio() {
let plan = publish_open(&av_and_aac()).expect("the plan builds");
let kinds: Vec<MessageTypeId> = plan.iter().map(PublishMessage::message_type).collect();
assert_eq!(
kinds,
[
MessageTypeId::DataAmf0,
MessageTypeId::Video,
MessageTypeId::Audio
]
);
}
fn script_properties(body: &[u8]) -> Vec<(alloc::string::String, Amf0Value)> {
let values = amf0_parse_values_with(body, TEST_AMF0_LIMITS)
.expect("the FLV script body parses")
.0;
match values.as_slice() {
[Amf0Value::String(handler), Amf0Value::EcmaArray(properties)] => {
assert_eq!(handler, "onMetaData");
properties.clone()
}
other => panic!("not an onMetaData script body: {other:?}"),
}
}
#[test]
fn the_metadata_message_is_the_publishers_data_message_without_a_duration() {
let plan = publish_open(&av_and_aac()).expect("the plan builds");
let PublishMessage::Metadata(body) = &plan[0] else {
panic!("the first message is the metadata body");
};
let values = amf0_parse_values_with(body, TEST_AMF0_LIMITS)
.expect("the data message parses")
.0;
let [
Amf0Value::String(set),
Amf0Value::String(handler),
Amf0Value::EcmaArray(properties),
] = values.as_slice()
else {
panic!("not @setDataFrame, onMetaData, array: {values:?}");
};
assert_eq!(
(set.as_str(), handler.as_str()),
("@setDataFrame", "onMetaData")
);
let names: Vec<&str> = properties.iter().map(|(name, _)| name.as_str()).collect();
assert_eq!(
names,
[
"width",
"height",
"videocodecid",
"audiocodecid",
"audiosamplerate",
"stereo"
],
"a live stream states no duration"
);
assert_eq!(properties[0].1, Amf0Value::Number(320.0));
assert_eq!(properties[1].1, Amf0Value::Number(240.0));
assert_eq!(properties[5].1, Amf0Value::Boolean(false));
}
#[test]
fn both_metadata_forms_state_the_same_properties() {
let tracks = [
PublishTrack::Video {
width: 320,
height: 240,
avc_config: Vec::new(),
},
PublishTrack::Audio {
sample_rate: 44_100,
channel_count: 1,
asc: Vec::new(),
},
];
let metadata = OnMetadata::new(&tracks, None).expect("builds");
let file = metadata.script_body().expect("file form");
assert!(
file.starts_with(&[0x02, 0x00, 0x0A]),
"the file form begins with the AMF0 onMetaData name (§E.4.1)"
);
let publish = metadata.publish_body().expect("publish form");
assert_eq!(
crate::flv::strip_set_data_frame(&publish)[..AMF0_STRING_ONMETADATA_LEN],
file[..AMF0_STRING_ONMETADATA_LEN],
"stripping @setDataFrame leaves the file form's handler name first"
);
let publish_values = amf0_parse_values_with(&publish, TEST_AMF0_LIMITS)
.expect("parses")
.0;
assert_eq!(
publish_values.last(),
Some(&Amf0Value::EcmaArray(script_properties(&file)))
);
}
#[test]
fn the_sequence_headers_are_the_files_own_tag_bodies() {
let plan = publish_open(&av_and_aac()).expect("the plan builds");
let PublishMessage::SequenceHeader {
message_type_id,
body,
} = &plan[1]
else {
panic!("the second message is the video sequence header");
};
assert_eq!(*message_type_id, MessageTypeId::Video);
let mut expected = alloc::vec![0x17, 0x00, 0x00, 0x00, 0x00];
expected.extend_from_slice(AVC_CONFIG);
assert_eq!(body, &expected);
let PublishMessage::SequenceHeader {
message_type_id,
body,
} = &plan[2]
else {
panic!("the third message is the audio sequence header");
};
assert_eq!(*message_type_id, MessageTypeId::Audio);
assert_eq!(body, &alloc::vec![0xAE, 0x00, 0x12, 0x08]);
}
#[test]
fn the_metadata_message_goes_out_as_the_data_message_it_is() {
let plan = publish_open(&av_and_aac()).expect("the plan builds");
let message = plan[0].to_message();
assert_eq!(message.message_type_id, MessageTypeId::DataAmf0);
let PublishMessage::Metadata(body) = &plan[0] else {
panic!("the first message is the metadata body");
};
assert_eq!(&message.payload, body);
}
#[test]
fn a_publish_without_a_carriable_track_is_refused() {
assert!(publish_open(&[]).is_err());
}
#[cfg(feature = "target")]
mod target {
use crate::flv::PublishTarget;
#[test]
fn rtmp_url_splits_app_and_stream_key() {
let split = |url: &str| {
let target = PublishTarget::parse(url);
(target.app, target.stream_key)
};
assert_eq!(
split("rtmp://host/app/streamkey"),
("app".to_string(), "streamkey".to_string()),
"two path segments: app + stream key"
);
assert_eq!(
split("rtmp://host:1935/live/mystream"),
("live".to_string(), "mystream".to_string()),
"with an explicit port"
);
assert_eq!(
split("rtmp://host/app/instance/streamkey"),
("app/instance".to_string(), "streamkey".to_string()),
"multi-segment app: everything but the last segment"
);
assert_eq!(
split("rtmp://host/app"),
("app".to_string(), String::new()),
"one segment only: no stream key given"
);
assert_eq!(
split("rtmp://host"),
("live".to_string(), String::new()),
"no path at all: falls back to the pre-existing \"live\" default"
);
assert_eq!(
split("not a url"),
("live".to_string(), String::new()),
"unparseable URL: same fallback, never panics"
);
}
}