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
//! Script data structures
use core::fmt;
use std::io;
use bytes::Bytes;
use scuffle_amf0::de::MultiValue;
use scuffle_amf0::decoder::Amf0Decoder;
use scuffle_amf0::{Amf0Object, Amf0Value};
use scuffle_bytes_util::{BytesCursorExt, StringCow};
use serde::de::VariantAccess;
use serde_derive::Deserialize;
use crate::audio::header::enhanced::AudioFourCc;
use crate::audio::header::legacy::SoundFormat;
use crate::error::FlvError;
use crate::video::header::enhanced::VideoFourCc;
use crate::video::header::legacy::VideoCodecId;
/// FLV `onMetaData` audio codec ID.
///
/// Either a legacy [`SoundFormat`] or an enhanced [`AudioFourCc`].
/// Appears as `audiocodecid` in the [`OnMetaData`] script data.
#[derive(Debug, Clone, PartialEq)]
pub enum OnMetaDataAudioCodecId {
/// Legacy audio codec ID.
Legacy(SoundFormat),
/// Enhanced audio codec ID.
Enhanced(AudioFourCc),
}
impl<'de> serde::Deserialize<'de> for OnMetaDataAudioCodecId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let n: u32 = serde::Deserialize::deserialize(deserializer)?;
// Since SoundFormat is a u8, we can be sure that the number represents an AudioFourCc if it is greater
// than u8::MAX.
// Additionally, since the smallest possible AudioFourCc (4 spaces) is greater than u8::MAX,
// we can be sure that the number cannot represent an AudioFourCc if it is smaller than u8::MAX.
if n > u8::MAX as u32 {
Ok(Self::Enhanced(AudioFourCc::from(n.to_be_bytes())))
} else {
Ok(Self::Legacy(SoundFormat::from(n as u8)))
}
}
}
/// FLV `onMetaData` video codec ID.
///
/// Either a legacy [`VideoCodecId`] or an enhanced [`VideoFourCc`].
/// Appears as `videocodecid` in the [`OnMetaData`] script data.
#[derive(Debug, Clone, PartialEq)]
pub enum OnMetaDataVideoCodecId {
/// Legacy video codec ID.
Legacy(VideoCodecId),
/// Enhanced video codec ID.
Enhanced(VideoFourCc),
}
impl<'de> serde::Deserialize<'de> for OnMetaDataVideoCodecId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let n: u32 = serde::Deserialize::deserialize(deserializer)?;
// Since VideoCodecId is a u8, we can be sure that the number represents an VideoFourCc if it is greater
// than u8::MAX.
// Additionally, since the smallest possible VideoFourCc (4 spaces) is greater than u8::MAX,
// we can be sure that the number cannot represent an VideoFourCc if it is smaller than u8::MAX.
if n > u8::MAX as u32 {
Ok(Self::Enhanced(VideoFourCc::from(n.to_be_bytes())))
} else {
Ok(Self::Legacy(VideoCodecId::from(n as u8)))
}
}
}
/// FLV `onMetaData` script data
///
/// Defined by:
/// - Legacy FLV spec, Annex E.5
/// - Enhanced RTMP spec, page 13-16, Enhancing onMetaData
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase", bound = "'a: 'de")]
pub struct OnMetaData<'a> {
/// Audio codec ID used in the file.
#[serde(default)]
pub audiocodecid: Option<OnMetaDataAudioCodecId>,
/// Audio bitrate, in kilobits per second.
#[serde(default)]
pub audiodatarate: Option<f64>,
/// Delay introduced by the audio codec, in seconds.
#[serde(default)]
pub audiodelay: Option<f64>,
/// Frequency at which the audio stream is replayed.
#[serde(default)]
pub audiosamplerate: Option<f64>,
/// Resolution of a single audio sample.
#[serde(default)]
pub audiosamplesize: Option<f64>,
/// Indicating the last video frame is a key frame.
#[serde(default)]
pub can_seek_to_end: Option<bool>,
/// Creation date and time.
#[serde(default)]
pub creationdate: Option<String>,
/// Total duration of the file, in seconds.
#[serde(default)]
pub duration: Option<f64>,
/// Total size of the file, in bytes.
#[serde(default)]
pub filesize: Option<f64>,
/// Number of frames per second.
#[serde(default)]
pub framerate: Option<f64>,
/// Height of the video, in pixels.
#[serde(default)]
pub height: Option<f64>,
/// Indicates stereo audio.
#[serde(default)]
pub stereo: Option<bool>,
/// Video codec ID used in the file.
#[serde(default)]
pub videocodecid: Option<OnMetaDataVideoCodecId>,
/// Video bitrate, in kilobits per second.
#[serde(default)]
pub videodatarate: Option<f64>,
/// Width of the video, in pixels.
#[serde(default)]
pub width: Option<f64>,
/// The audioTrackIdInfoMap and videoTrackIdInfoMap objects are designed to store
/// metadata for audio and video tracks respectively. Each object uses a TrackId as
/// a key to map to properties that detail the unique characteristics of each
/// individual track, diverging from the default configurations.
///
/// Key-Value Structure:
/// - Keys: Each TrackId acts as a unique identifier for a specific audio or video track.
/// - Values: Track Objects containing metadata that specify characteristics which deviate from the default track settings.
///
/// Properties of Each Track Object:
/// - These properties detail non-standard configurations needed for
/// custom handling of the track, facilitating specific adjustments
/// to enhance track performance and quality for varied conditions.
/// - For videoTrackIdInfoMap:
/// - Properties such as width, height, videodatarate, etc.
/// specify video characteristics that differ from standard
/// settings.
/// - For audioTrackIdInfoMap:
/// - Properties such as audiodatarate, channels, etc., define
/// audio characteristics that differ from standard
/// configurations.
///
/// Purpose:
/// - The purpose of these maps is to specify unique properties for
/// each track, ensuring tailored configurations that optimize
/// performance and quality for specific media content and delivery
/// scenarios.
///
/// This structure provides a framework for detailed customization and control over
/// the media tracks, ensuring optimal management and delivery across various types
/// of content and platforms.
#[serde(default, borrow)]
pub audio_track_id_info_map: Option<Amf0Object<'a>>,
/// See [`OnMetaData::audio_track_id_info_map`].
#[serde(default, borrow)]
pub video_track_id_info_map: Option<Amf0Object<'a>>,
/// Any other metadata contained in the script data.
#[serde(flatten, borrow)]
pub other: Amf0Object<'a>,
}
/// XMP Metadata
///
/// Defined by:
/// - Legacy FLV spec, Annex E.6
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase", bound = "'a: 'de")]
pub struct OnXmpData<'a> {
/// XMP metadata, formatted according to the XMP metadata specification.
///
/// For further details, see [www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart3.pdf](https://web.archive.org/web/20090306165322/https://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart3.pdf).
#[serde(default, rename = "liveXML")]
live_xml: Option<StringCow<'a>>,
/// Any other metadata contained in the script data.
#[serde(flatten, borrow)]
other: Amf0Object<'a>,
}
/// FLV `SCRIPTDATA` tag
///
/// Defined by:
/// - Legacy FLV spec, Annex E.4.4.1
#[derive(Debug, Clone, PartialEq)]
pub enum ScriptData<'a> {
/// `onMetaData` script data.
///
/// Boxed because it's so big.
OnMetaData(Box<OnMetaData<'a>>),
/// `onXMPData` script data.
OnXmpData(OnXmpData<'a>),
/// Any other script data.
Other {
/// The name of the script data.
name: StringCow<'a>,
/// The data of the script data.
data: Vec<Amf0Value<'static>>,
},
}
impl<'de> serde::Deserialize<'de> for ScriptData<'de> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor;
const SCRIPT_DATA: &str = "ScriptData";
const ON_META_DATA: &str = "onMetaData";
const ON_XMP_DATA: &str = "onXMPData";
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = ScriptData<'de>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(SCRIPT_DATA)
}
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
where
A: serde::de::EnumAccess<'de>,
{
let (name, content): (StringCow<'de>, A::Variant) = data.variant()?;
match name.as_ref() {
ON_META_DATA => Ok(ScriptData::OnMetaData(Box::new(content.newtype_variant()?))),
ON_XMP_DATA => Ok(ScriptData::OnXmpData(content.newtype_variant()?)),
_ => Ok(ScriptData::Other {
name,
data: content
.newtype_variant::<MultiValue<Vec<Amf0Value>>>()?
.0
.into_iter()
.map(|v| v.into_owned())
.collect(),
}),
}
}
}
deserializer.deserialize_enum(SCRIPT_DATA, &[ON_META_DATA, ON_XMP_DATA], Visitor)
}
}
impl ScriptData<'_> {
/// Demux the [`ScriptData`] from the given reader.
pub fn demux(reader: &mut io::Cursor<Bytes>) -> Result<Self, FlvError> {
let buf = reader.extract_remaining();
let mut decoder = Amf0Decoder::from_buf(buf);
serde::de::Deserialize::deserialize(&mut decoder).map_err(FlvError::Amf0)
}
}
#[cfg(test)]
#[cfg_attr(all(test, coverage_nightly), coverage(off))]
mod tests {
use scuffle_amf0::Amf0Marker;
use scuffle_amf0::encoder::Amf0Encoder;
use super::*;
#[test]
fn script_on_meta_data() {
let width = 1280.0f64.to_be_bytes();
#[rustfmt::skip]
let data = [
Amf0Marker::String as u8,
0, 10, // Length (10 bytes)
b'o', b'n', b'M', b'e', b't', b'a', b'D', b'a', b't', b'a',// "onMetaData"
Amf0Marker::Object as u8,
0, 5, // Length (5 bytes)
b'w', b'i', b'd', b't', b'h', // "width"
Amf0Marker::Number as u8,
width[0],
width[1],
width[2],
width[3],
width[4],
width[5],
width[6],
width[7],
0, 0, Amf0Marker::ObjectEnd as u8,
];
let mut reader = io::Cursor::new(Bytes::from_owner(data));
let script_data = ScriptData::demux(&mut reader).unwrap();
let ScriptData::OnMetaData(metadata) = script_data else {
panic!("expected onMetaData");
};
assert_eq!(
*metadata,
OnMetaData {
audiocodecid: None,
audiodatarate: None,
audiodelay: None,
audiosamplerate: None,
audiosamplesize: None,
can_seek_to_end: None,
creationdate: None,
duration: None,
filesize: None,
framerate: None,
height: None,
stereo: None,
videocodecid: None,
videodatarate: None,
width: Some(1280.0),
audio_track_id_info_map: None,
video_track_id_info_map: None,
other: Amf0Object::new(),
}
);
}
#[test]
fn script_on_meta_data_full() {
let mut data = Vec::new();
let mut encoder = Amf0Encoder::new(&mut data);
let audio_track_id_info_map = [("test".into(), Amf0Value::Number(1.0))].into_iter().collect();
let video_track_id_info_map = [("test2".into(), Amf0Value::Number(2.0))].into_iter().collect();
encoder.encode_string("onMetaData").unwrap();
let object: Amf0Object = [
(
"audiocodecid".into(),
Amf0Value::Number(u32::from_be_bytes(AudioFourCc::Aac.0) as f64),
),
("audiodatarate".into(), Amf0Value::Number(128.0)),
("audiodelay".into(), Amf0Value::Number(0.0)),
("audiosamplerate".into(), Amf0Value::Number(44100.0)),
("audiosamplesize".into(), Amf0Value::Number(16.0)),
("canSeekToEnd".into(), Amf0Value::Boolean(true)),
("creationdate".into(), Amf0Value::String("2025-01-01T00:00:00Z".into())),
("duration".into(), Amf0Value::Number(60.0)),
("filesize".into(), Amf0Value::Number(1024.0)),
("framerate".into(), Amf0Value::Number(30.0)),
("height".into(), Amf0Value::Number(720.0)),
("stereo".into(), Amf0Value::Boolean(true)),
(
"videocodecid".into(),
Amf0Value::Number(u32::from_be_bytes(VideoFourCc::Avc.0) as f64),
),
("videodatarate".into(), Amf0Value::Number(1024.0)),
("width".into(), Amf0Value::Number(1280.0)),
("audioTrackIdInfoMap".into(), Amf0Value::Object(audio_track_id_info_map)),
("videoTrackIdInfoMap".into(), Amf0Value::Object(video_track_id_info_map)),
]
.into_iter()
.collect();
encoder.encode_object(&object).unwrap();
let mut reader = io::Cursor::new(Bytes::from_owner(data));
let script_data = ScriptData::demux(&mut reader).unwrap();
let ScriptData::OnMetaData(metadata) = script_data else {
panic!("expected onMetaData");
};
assert_eq!(
*metadata,
OnMetaData {
audiocodecid: Some(OnMetaDataAudioCodecId::Enhanced(AudioFourCc::Aac)),
audiodatarate: Some(128.0),
audiodelay: Some(0.0),
audiosamplerate: Some(44100.0),
audiosamplesize: Some(16.0),
can_seek_to_end: Some(true),
creationdate: Some("2025-01-01T00:00:00Z".to_string()),
duration: Some(60.0),
filesize: Some(1024.0),
framerate: Some(30.0),
height: Some(720.0),
stereo: Some(true),
videocodecid: Some(OnMetaDataVideoCodecId::Enhanced(VideoFourCc::Avc)),
videodatarate: Some(1024.0),
width: Some(1280.0),
audio_track_id_info_map: Some([("test".into(), Amf0Value::Number(1.0))].into_iter().collect()),
video_track_id_info_map: Some([("test2".into(), Amf0Value::Number(2.0))].into_iter().collect()),
other: Amf0Object::new(),
}
);
}
#[test]
fn script_on_xmp_data() {
#[rustfmt::skip]
let data = [
Amf0Marker::String as u8,
0, 9, // Length (9 bytes)
b'o', b'n', b'X', b'M', b'P', b'D', b'a', b't', b'a',// "onXMPData"
Amf0Marker::Object as u8,
0, 7, // Length (7 bytes)
b'l', b'i', b'v', b'e', b'X', b'M', b'L', // "liveXML"
Amf0Marker::String as u8,
0, 5, // Length (5 bytes)
b'h', b'e', b'l', b'l', b'o', // "hello"
0, 4, // Length (7 bytes)
b't', b'e', b's', b't', // "test"
Amf0Marker::Null as u8,
0, 0, Amf0Marker::ObjectEnd as u8,
];
let mut reader = io::Cursor::new(Bytes::from_owner(data));
let script_data = ScriptData::demux(&mut reader).unwrap();
let ScriptData::OnXmpData(xmp_data) = script_data else {
panic!("expected onXMPData");
};
assert_eq!(
xmp_data,
OnXmpData {
live_xml: Some("hello".into()),
other: [("test".into(), Amf0Value::Null)].into_iter().collect(),
}
);
}
#[test]
fn script_other() {
#[rustfmt::skip]
let data = [
Amf0Marker::String as u8,
0, 10, // Length (10 bytes)
b'o', b'n', b'W', b'h', b'a', b't', b'e', b'v', b'e', b'r',// "onWhatever"
Amf0Marker::Object as u8,
0, 4, // Length (4 bytes)
b't', b'e', b's', b't', // "test"
Amf0Marker::String as u8,
0, 5, // Length (5 bytes)
b'h', b'e', b'l', b'l', b'o', // "hello"
0, 0, Amf0Marker::ObjectEnd as u8,
];
let mut reader = io::Cursor::new(Bytes::from_owner(data));
let script_data = ScriptData::demux(&mut reader).unwrap();
let ScriptData::Other { name, data } = script_data else {
panic!("expected onXMPData");
};
let object: Amf0Object = [("test".into(), Amf0Value::String("hello".into()))].into_iter().collect();
assert_eq!(name, "onWhatever");
assert_eq!(data.len(), 1);
assert_eq!(data[0], Amf0Value::Object(object));
}
}