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
|
/*
This file is part of jellything (https://codeberg.org/metamuffin/jellything)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use crate::{SMediaInfo, fragment_index::fragment_index, stream_info};
use anyhow::Result;
use jellystream_types::{StreamContainer, StreamFormatInfo, TrackKind};
use std::{
fmt::{Display, Write},
io::{Cursor, Read},
ops::Range,
};
pub fn dash(sinfo: &SMediaInfo) -> Result<Box<dyn Read + Send + Sync>> {
let (_iinfo, info) = stream_info(&sinfo)?;
let mut out = String::new();
writeln!(
out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?> \
<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
xmlns=\"urn:mpeg:dash:schema:mpd:2011\" \
xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
xsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\" \
profiles=\"urn:mpeg:dash:profile:isoff-live:2011\" \
type=\"static\" \
mediaPresentationDuration=\"{}\" \
maxSegmentDuration=\"PT5.0S\" \
minBufferTime=\"PT10.0S\">",
Time(info.duration)
)?;
writeln!(out, "<ProgramInformation></ProgramInformation>")?;
writeln!(out, "<ServiceDescription id=\"0\"></ServiceDescription>")?;
writeln!(out, r#"<Period id="0" start="PT0.0S">"#)?;
for (as_id, track) in info.tracks.iter().enumerate() {
let frags = fragment_index(&sinfo, as_id)?;
match track.kind {
TrackKind::Video => {
let max_width = track
.formats
.iter()
.flat_map(|f| f.width)
.max()
.unwrap_or_default();
let max_height = track
.formats
.iter()
.flat_map(|f| f.height)
.max()
.unwrap_or_default();
let framerate = "2997/1000";
let par = "16:9"; // TODO
writeln!(
out,
"<AdaptationSet \
id=\"{as_id}\" \
contentType=\"video\" \
startWithSAP=\"1\" \
segmentAlignment=\"true\" \
bitstreamSwitching=\"true\" \
frameRate=\"{framerate}\" \
maxWidth=\"{max_width}\" \
maxHeight=\"{max_height}\" \
par=\"{par}\" \
lang=\"eng\">"
)?;
for (repr_id, format) in track.formats.iter().enumerate() {
let StreamFormatInfo {
width: Some(width),
height: Some(height),
bitrate,
..
} = format
else {
unreachable!()
};
let container = choose_container(format);
let container_mime = container.mime_type(track.kind);
let codec_param = &format.codec_param;
writeln!(
out,
"<Representation \
id=\"{repr_id}\" \
mimeType=\"{container_mime}\" \
codecs=\"{codec_param}\" \
bandwidth=\"{bitrate:.0}\" \
width=\"{width}\" \
height=\"{height}\" \
scanType=\"unknown\" \
sar=\"1:1\">"
)?;
write_segment_template(&mut out, as_id, container, &frags)?;
writeln!(out, "</Representation>")?;
}
writeln!(out, "</AdaptationSet>")?;
}
TrackKind::Audio => {
writeln!(
out,
"<AdaptationSet \
id=\"{as_id}\" \
contentType=\"audio\" \
startWithSAP=\"1\" \
segmentAlignment=\"true\" \
bitstreamSwitching=\"true\">"
)?;
for (repr_id, format) in track.formats.iter().enumerate() {
let StreamFormatInfo {
bitrate,
samplerate: Some(samplerate),
..
} = format
else {
unreachable!()
};
let container = choose_container(format);
let container_mime = container.mime_type(track.kind);
let codec_param = &format.codec_param;
writeln!(
out,
"<Representation \
id=\"{repr_id}\" \
mimeType=\"{container_mime}\" \
codecs=\"{codec_param}\" \
bandwidth=\"{bitrate:.0}\" \
audioSamplingRate=\"{samplerate:.0}\">"
)?;
write_segment_template(&mut out, as_id, container, &frags)?;
writeln!(out, "</Representation>")?;
}
writeln!(out, "</AdaptationSet>")?;
}
TrackKind::Subtitle => (),
}
}
writeln!(out, r#"</Period>"#)?;
writeln!(out, r#"</MPD>"#)?;
Ok(Box::new(Cursor::new(out)))
}
fn choose_container(format: &StreamFormatInfo) -> StreamContainer {
if format.containers.contains(&StreamContainer::WebM) {
StreamContainer::WebM
} else if format.containers.contains(&StreamContainer::MP4) {
StreamContainer::MP4
} else {
StreamContainer::Matroska
}
}
fn write_segment_template(
out: &mut String,
as_id: usize,
container: StreamContainer,
frags: &[Range<f64>],
) -> Result<()> {
writeln!(
out,
"<SegmentTemplate \
timescale=\"1000\" \
initialization=\"stream?fragmentinit&t={as_id}&c={container}&f=$RepresentationID$\" \
media=\"stream?fragment&t={as_id}&c={container}&f=$RepresentationID$&i=$Number$\" \
startNumber=\"0\">"
)?;
writeln!(out, "{}", Timeline(&frags))?;
writeln!(out, "</SegmentTemplate>")?;
Ok(())
}
struct Time(f64);
impl Display for Time {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "PT{:.01}S", self.0)
}
}
struct Timeline<'a>(&'a [Range<f64>]);
impl Display for Timeline<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<SegmentTimeline>")?;
let mut c = 0;
let mut last_t = 0;
let mut last_d = 0;
let mut r = 0;
let mut first = true;
for seg in self.0 {
let t = (seg.end * 1000.) as i64;
let d = t - last_t;
if d != last_d && last_d != 0 {
match (r, first) {
(1, true) => write!(f, r#"<S t="0" d="{last_d}" />"#)?,
(1, false) => write!(f, r#"<S d="{last_d}" />"#)?,
(_, true) => write!(f, r#"<S t="0" d="{last_d}" r="{r}" />"#)?,
(_, false) => write!(f, r#"<S d="{last_d}" r="{r}" />"#)?,
}
first = false;
c += r;
r = 1;
} else {
r += 1;
}
last_t = t;
last_d = d;
}
c += r;
match (r, first) {
(1, true) => write!(f, r#"<S t="0" d="{last_d}" />"#)?,
(1, false) => write!(f, r#"<S d="{last_d}" />"#)?,
(_, true) => write!(f, r#"<S t="0" d="{last_d}" r="{r}" />"#)?,
(_, false) => write!(f, r#"<S d="{last_d}" r="{r}" />"#)?,
}
assert_eq!(c, self.0.len());
write!(f, "</SegmentTimeline>")?;
Ok(())
}
}
|