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
use std::collections::HashSet;
use std::fmt;
use std::iter;
use std::str::FromStr;

use line::{Line, Lines, Tag};
use tags::{
    ExtM3u, ExtXIFrameStreamInf, ExtXIndependentSegments, ExtXMedia, ExtXSessionData,
    ExtXSessionKey, ExtXStart, ExtXStreamInf, ExtXVersion, MasterPlaylistTag,
};
use types::{ClosedCaptions, MediaType, ProtocolVersion, QuotedString};
use {Error, ErrorKind, Result};

/// Master playlist builder.
#[derive(Debug, Clone)]
pub struct MasterPlaylistBuilder {
    version: Option<ProtocolVersion>,
    independent_segments_tag: Option<ExtXIndependentSegments>,
    start_tag: Option<ExtXStart>,
    media_tags: Vec<ExtXMedia>,
    stream_inf_tags: Vec<ExtXStreamInf>,
    i_frame_stream_inf_tags: Vec<ExtXIFrameStreamInf>,
    session_data_tags: Vec<ExtXSessionData>,
    session_key_tags: Vec<ExtXSessionKey>,
}
impl MasterPlaylistBuilder {
    /// Makes a new `MasterPlaylistBuilder` instance.
    pub fn new() -> Self {
        MasterPlaylistBuilder {
            version: None,
            independent_segments_tag: None,
            start_tag: None,
            media_tags: Vec::new(),
            stream_inf_tags: Vec::new(),
            i_frame_stream_inf_tags: Vec::new(),
            session_data_tags: Vec::new(),
            session_key_tags: Vec::new(),
        }
    }

    /// Sets the protocol compatibility version of the resulting playlist.
    ///
    /// If the resulting playlist has tags which requires a compatibility version greater than `version`,
    /// `finish()` method will fail with an `ErrorKind::InvalidInput` error.
    ///
    /// The default is the maximum version among the tags in the playlist.
    pub fn version(&mut self, version: ProtocolVersion) -> &mut Self {
        self.version = Some(version);
        self
    }

    /// Adds the given tag to the resulting playlist.
    ///
    /// If it is forbidden to have multiple instance of the tag, the existing one will be overwritten.
    pub fn tag<T: Into<MasterPlaylistTag>>(&mut self, tag: T) -> &mut Self {
        match tag.into() {
            MasterPlaylistTag::ExtXIndependentSegments(t) => {
                self.independent_segments_tag = Some(t);
            }
            MasterPlaylistTag::ExtXStart(t) => self.start_tag = Some(t),
            MasterPlaylistTag::ExtXMedia(t) => self.media_tags.push(t),
            MasterPlaylistTag::ExtXStreamInf(t) => self.stream_inf_tags.push(t),
            MasterPlaylistTag::ExtXIFrameStreamInf(t) => self.i_frame_stream_inf_tags.push(t),
            MasterPlaylistTag::ExtXSessionData(t) => self.session_data_tags.push(t),
            MasterPlaylistTag::ExtXSessionKey(t) => self.session_key_tags.push(t),
        }
        self
    }

    /// Builds a `MasterPlaylist` instance.
    pub fn finish(self) -> Result<MasterPlaylist> {
        let required_version = self.required_version();
        let specified_version = self.version.unwrap_or(required_version);
        track_assert!(
            required_version <= specified_version,
            ErrorKind::InvalidInput,
            "required_version:{}, specified_version:{}",
            required_version,
            specified_version,
        );

        track!(self.validate_stream_inf_tags())?;
        track!(self.validate_i_frame_stream_inf_tags())?;
        track!(self.validate_session_data_tags())?;
        track!(self.validate_session_key_tags())?;

        Ok(MasterPlaylist {
            version_tag: ExtXVersion::new(specified_version),
            independent_segments_tag: self.independent_segments_tag,
            start_tag: self.start_tag,
            media_tags: self.media_tags,
            stream_inf_tags: self.stream_inf_tags,
            i_frame_stream_inf_tags: self.i_frame_stream_inf_tags,
            session_data_tags: self.session_data_tags,
            session_key_tags: self.session_key_tags,
        })
    }

    fn required_version(&self) -> ProtocolVersion {
        iter::empty()
            .chain(
                self.independent_segments_tag
                    .iter()
                    .map(|t| t.requires_version()),
            ).chain(self.start_tag.iter().map(|t| t.requires_version()))
            .chain(self.media_tags.iter().map(|t| t.requires_version()))
            .chain(self.stream_inf_tags.iter().map(|t| t.requires_version()))
            .chain(
                self.i_frame_stream_inf_tags
                    .iter()
                    .map(|t| t.requires_version()),
            ).chain(self.session_data_tags.iter().map(|t| t.requires_version()))
            .chain(self.session_key_tags.iter().map(|t| t.requires_version()))
            .max()
            .expect("Never fails")
    }

    fn validate_stream_inf_tags(&self) -> Result<()> {
        let mut has_none_closed_captions = false;
        for t in &self.stream_inf_tags {
            if let Some(group_id) = t.audio() {
                track_assert!(
                    self.check_media_group(MediaType::Audio, group_id),
                    ErrorKind::InvalidInput,
                    "Unmatched audio group: {:?}",
                    group_id
                );
            }
            if let Some(group_id) = t.video() {
                track_assert!(
                    self.check_media_group(MediaType::Video, group_id),
                    ErrorKind::InvalidInput,
                    "Unmatched video group: {:?}",
                    group_id
                );
            }
            if let Some(group_id) = t.subtitles() {
                track_assert!(
                    self.check_media_group(MediaType::Subtitles, group_id),
                    ErrorKind::InvalidInput,
                    "Unmatched subtitles group: {:?}",
                    group_id
                );
            }
            match t.closed_captions() {
                Some(&ClosedCaptions::GroupId(ref group_id)) => {
                    track_assert!(
                        self.check_media_group(MediaType::ClosedCaptions, group_id),
                        ErrorKind::InvalidInput,
                        "Unmatched closed-captions group: {:?}",
                        group_id
                    );
                }
                Some(&ClosedCaptions::None) => {
                    has_none_closed_captions = true;
                }
                None => {}
            }
        }
        if has_none_closed_captions {
            track_assert!(
                self.stream_inf_tags
                    .iter()
                    .all(|t| t.closed_captions() == Some(&ClosedCaptions::None)),
                ErrorKind::InvalidInput
            );
        }
        Ok(())
    }

    fn validate_i_frame_stream_inf_tags(&self) -> Result<()> {
        for t in &self.i_frame_stream_inf_tags {
            if let Some(group_id) = t.video() {
                track_assert!(
                    self.check_media_group(MediaType::Video, group_id),
                    ErrorKind::InvalidInput,
                    "Unmatched video group: {:?}",
                    group_id
                );
            }
        }
        Ok(())
    }

    fn validate_session_data_tags(&self) -> Result<()> {
        let mut set = HashSet::new();
        for t in &self.session_data_tags {
            track_assert!(
                set.insert((t.data_id(), t.language())),
                ErrorKind::InvalidInput,
                "Conflict: {}",
                t
            );
        }
        Ok(())
    }

    fn validate_session_key_tags(&self) -> Result<()> {
        let mut set = HashSet::new();
        for t in &self.session_key_tags {
            track_assert!(
                set.insert(t.key()),
                ErrorKind::InvalidInput,
                "Conflict: {}",
                t
            );
        }
        Ok(())
    }

    fn check_media_group(&self, media_type: MediaType, group_id: &QuotedString) -> bool {
        self.media_tags
            .iter()
            .any(|t| t.media_type() == media_type && t.group_id() == group_id)
    }
}
impl Default for MasterPlaylistBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Master playlist.
#[derive(Debug, Clone)]
pub struct MasterPlaylist {
    version_tag: ExtXVersion,
    independent_segments_tag: Option<ExtXIndependentSegments>,
    start_tag: Option<ExtXStart>,
    media_tags: Vec<ExtXMedia>,
    stream_inf_tags: Vec<ExtXStreamInf>,
    i_frame_stream_inf_tags: Vec<ExtXIFrameStreamInf>,
    session_data_tags: Vec<ExtXSessionData>,
    session_key_tags: Vec<ExtXSessionKey>,
}
impl MasterPlaylist {
    /// Returns the `EXT-X-VERSION` tag contained in the playlist.
    pub fn version_tag(&self) -> ExtXVersion {
        self.version_tag
    }

    /// Returns the `EXT-X-INDEPENDENT-SEGMENTS` tag contained in the playlist.
    pub fn independent_segments_tag(&self) -> Option<ExtXIndependentSegments> {
        self.independent_segments_tag
    }

    /// Returns the `EXT-X-START` tag contained in the playlist.
    pub fn start_tag(&self) -> Option<ExtXStart> {
        self.start_tag
    }

    /// Returns the `EXT-X-MEDIA` tags contained in the playlist.
    pub fn media_tags(&self) -> &[ExtXMedia] {
        &self.media_tags
    }

    /// Returns the `EXT-X-STREAM-INF` tags contained in the playlist.
    pub fn stream_inf_tags(&self) -> &[ExtXStreamInf] {
        &self.stream_inf_tags
    }

    /// Returns the `EXT-X-I-FRAME-STREAM-INF` tags contained in the playlist.
    pub fn i_fream_stream_inf_tags(&self) -> &[ExtXIFrameStreamInf] {
        &self.i_frame_stream_inf_tags
    }

    /// Returns the `EXT-X-SESSION-DATA` tags contained in the playlist.
    pub fn session_data_tags(&self) -> &[ExtXSessionData] {
        &self.session_data_tags
    }

    /// Returns the `EXT-X-SESSION-KEY` tags contained in the playlist.
    pub fn session_key_tags(&self) -> &[ExtXSessionKey] {
        &self.session_key_tags
    }
}
impl fmt::Display for MasterPlaylist {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{}", ExtM3u)?;
        if self.version_tag.version() != ProtocolVersion::V1 {
            writeln!(f, "{}", self.version_tag)?;
        }
        for t in &self.media_tags {
            writeln!(f, "{}", t)?;
        }
        for t in &self.stream_inf_tags {
            writeln!(f, "{}", t)?;
        }
        for t in &self.i_frame_stream_inf_tags {
            writeln!(f, "{}", t)?;
        }
        for t in &self.session_data_tags {
            writeln!(f, "{}", t)?;
        }
        for t in &self.session_key_tags {
            writeln!(f, "{}", t)?;
        }
        if let Some(ref t) = self.independent_segments_tag {
            writeln!(f, "{}", t)?;
        }
        if let Some(ref t) = self.start_tag {
            writeln!(f, "{}", t)?;
        }
        Ok(())
    }
}
impl FromStr for MasterPlaylist {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self> {
        let mut builder = MasterPlaylistBuilder::new();
        for (i, line) in Lines::new(s).enumerate() {
            match track!(line)? {
                Line::Blank | Line::Comment(_) => {}
                Line::Tag(tag) => {
                    if i == 0 {
                        track_assert_eq!(tag, Tag::ExtM3u(ExtM3u), ErrorKind::InvalidInput);
                        continue;
                    }
                    match tag {
                        Tag::ExtM3u(_) => {
                            track_panic!(ErrorKind::InvalidInput);
                        }
                        Tag::ExtXVersion(t) => {
                            track_assert_eq!(builder.version, None, ErrorKind::InvalidInput);
                            builder.version(t.version());
                        }
                        Tag::ExtInf(_)
                        | Tag::ExtXByteRange(_)
                        | Tag::ExtXDiscontinuity(_)
                        | Tag::ExtXKey(_)
                        | Tag::ExtXMap(_)
                        | Tag::ExtXProgramDateTime(_)
                        | Tag::ExtXDateRange(_)
                        | Tag::ExtXTargetDuration(_)
                        | Tag::ExtXMediaSequence(_)
                        | Tag::ExtXDiscontinuitySequence(_)
                        | Tag::ExtXEndList(_)
                        | Tag::ExtXPlaylistType(_)
                        | Tag::ExtXIFramesOnly(_) => {
                            track_panic!(ErrorKind::InvalidInput, "{}", tag)
                        }
                        Tag::ExtXMedia(t) => {
                            builder.tag(t);
                        }
                        Tag::ExtXStreamInf(t) => {
                            builder.tag(t);
                        }
                        Tag::ExtXIFrameStreamInf(t) => {
                            builder.tag(t);
                        }
                        Tag::ExtXSessionData(t) => {
                            builder.tag(t);
                        }
                        Tag::ExtXSessionKey(t) => {
                            builder.tag(t);
                        }
                        Tag::ExtXIndependentSegments(t) => {
                            track_assert_eq!(
                                builder.independent_segments_tag,
                                None,
                                ErrorKind::InvalidInput
                            );
                            builder.tag(t);
                        }
                        Tag::ExtXStart(t) => {
                            track_assert_eq!(builder.start_tag, None, ErrorKind::InvalidInput);
                            builder.tag(t);
                        }
                        Tag::Unknown(_) => {
                            // [6.3.1. General Client Responsibilities]
                            // > ignore any unrecognized tags.
                        }
                    }
                }
                Line::Uri(uri) => {
                    track_panic!(ErrorKind::InvalidInput, "Unexpected URI: {:?}", uri);
                }
            }
        }
        track!(builder.finish())
    }
}