Skip to main content

build_mpd/
build_mpd.rs

1//! Build a small static MPD in code — one video and one audio Representation,
2//! each with a `SegmentTemplate` — and render it with `Mpd::to_xml`.
3//!
4//! Run with `cargo run -p broadcast-dash --example build_mpd`.
5
6use broadcast_dash::{
7    AdaptationSet, CommonAttributes, Mpd, Period, PresentationType, Representation, SegmentTemplate,
8};
9
10fn main() {
11    let mut video = Representation::default();
12    video.id = "v0".to_string();
13    video.bandwidth = 2_000_000;
14    let mut vc = CommonAttributes::default();
15    vc.codecs = Some("avc1.42E01E".to_string());
16    vc.mime_type = Some("video/mp4".to_string());
17    vc.width = Some(1280);
18    vc.height = Some(720);
19    vc.frame_rate = Some("30".to_string());
20    video.common = vc;
21    let mut tmpl = SegmentTemplate::default();
22    tmpl.media = Some("chunk-$Number$.m4s".to_string());
23    tmpl.initialization = Some("init.mp4".to_string());
24    tmpl.multi.base.timescale = Some(90000);
25    tmpl.multi.duration = Some(90000);
26    tmpl.multi.start_number = Some(1);
27    video.segment.template = Some(tmpl);
28
29    let mut audio = Representation::default();
30    audio.id = "a0".to_string();
31    audio.bandwidth = 128_000;
32    let mut ac = CommonAttributes::default();
33    ac.codecs = Some("mp4a.40.2".to_string());
34    ac.mime_type = Some("audio/mp4".to_string());
35    ac.audio_sampling_rate = Some("48000".to_string());
36    audio.common = ac;
37    let mut atmpl = SegmentTemplate::default();
38    atmpl.media = Some("audio-chunk-$Number$.m4s".to_string());
39    atmpl.initialization = Some("audio-init.mp4".to_string());
40    atmpl.multi.base.timescale = Some(48000);
41    atmpl.multi.duration = Some(48000);
42    atmpl.multi.start_number = Some(1);
43    audio.segment.template = Some(atmpl);
44
45    let mut video_set = AdaptationSet::default();
46    video_set.id = Some(0);
47    video_set.content_type = Some("video".to_string());
48    video_set.representations.push(video);
49
50    let mut audio_set = AdaptationSet::default();
51    audio_set.id = Some(1);
52    audio_set.content_type = Some("audio".to_string());
53    audio_set.representations.push(audio);
54
55    let mut period = Period::default();
56    period.id = Some("p0".to_string());
57    period.adaptation_sets.push(video_set);
58    period.adaptation_sets.push(audio_set);
59
60    let mut mpd = Mpd::default();
61    mpd.id = Some("demo".to_string());
62    mpd.profiles = vec!["urn:mpeg:dash:profile:isoff-on-demand:2011".to_string()];
63    mpd.kind = PresentationType::Static;
64    mpd.media_presentation_duration = Some(std::time::Duration::from_secs(60));
65    mpd.min_buffer_time = Some(std::time::Duration::from_secs(2));
66    mpd.periods.push(period);
67
68    let xml = mpd.to_xml().expect("render");
69    print!("{xml}");
70    assert_eq!(Mpd::parse(xml.as_bytes()).expect("re-parse"), mpd);
71}