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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! A library to parse m3u8 playlists (HTTP Live Streaming) [link]
//! (https://tools.ietf.org/html/draft-pantos-http-live-streaming-19).
//!
//! #Examples
//!
//! Parsing a playlist and let the parser figure out if it's a media or master playlist.
//!
//! ```
//! extern crate nom;
//! extern crate m3u8_rs;
//! use m3u8_rs::playlist::Playlist;
//! use nom::IResult;
//! use std::io::Read;
//!
//! fn main() {
//!     let mut file = std::fs::File::open("playlist.m3u8").unwrap();
//!     let mut bytes: Vec<u8> = Vec::new();
//!     file.read_to_end(&mut bytes).unwrap();
//!
//!     // Option 1: fn parse_playlist_res(input) -> Result<Playlist, _>
//!     match m3u8_rs::parse_playlist_res(&bytes) {
//!         Ok(Playlist::MasterPlaylist(pl)) => println!("Master playlist:\n{:?}", pl),
//!         Ok(Playlist::MediaPlaylist(pl)) => println!("Media playlist:\n{:?}", pl),
//!         Err(e) => println!("Error: {:?}", e)
//!     }
//!
//!     // Option 2: fn parse_playlist(input) -> IResult<_, Playlist, _>
//!     match m3u8_rs::parse_playlist(&bytes) {
//!         IResult::Done(i, Playlist::MasterPlaylist(pl)) => println!("Master playlist:\n{:?}", pl),
//!         IResult::Done(i, Playlist::MediaPlaylist(pl)) => println!("Media playlist:\n{:?}", pl),
//!         IResult::Error(e) =>  panic!("Parsing error: \n{}", e),
//!         IResult::Incomplete(e) => panic!("Parsing error: \n{:?}", e),
//!     }
//! }
//! ```
//!
//! Parsing a master playlist directly
//!
//! ```
//! extern crate nom;
//! extern crate m3u8_rs;
//! use std::io::Read;
//! use nom::IResult;
//!
//! fn main() {
//!     let mut file = std::fs::File::open("masterplaylist.m3u8").unwrap();
//!     let mut bytes: Vec<u8> = Vec::new();
//!     file.read_to_end(&mut bytes).unwrap();
//!     
//!     if let IResult::Done(_, pl) = m3u8_rs::parse_master_playlist(&bytes) {
//!         println!("{:?}", pl);
//!     }
//! }
//!
//! ```
//!
//! Creating a playlist and writing it back to a vec/file
//!
//! ```
//! extern crate m3u8_rs;
//! use m3u8_rs::playlist::{MediaPlaylist, MediaPlaylistType, MediaSegment};
//!
//! fn main() {
//!     let playlist = MediaPlaylist { 
//!         version: 6,
//!         target_duration: 3.0,
//!         media_sequence: 338559,
//!         discontinuity_sequence: 1234,
//!         end_list: true,
//!         playlist_type: Some(MediaPlaylistType::Vod),
//!         segments: vec![
//!             MediaSegment {
//!                 uri: "20140311T113819-01-338559live.ts".into(),
//!                 duration: 2.002,
//!                 title: Some("title".into()),
//!                 ..Default::default()
//!             },
//!         ],
//!         ..Default::default()
//!     };
//! 
//!     //let mut v: Vec<u8> = Vec::new();
//!     //playlist.write_to(&mut v).unwrap();
//! 
//!     //let mut file = std::fs::File::open("playlist.m3u8").unwrap();
//!     //playlist.write_to(&mut file).unwrap();
//! }
//!
//! ```

#[macro_use]
extern crate nom;

pub mod playlist;

use nom::*;
use std::str;
use std::f32;
use std::string;
use std::str::FromStr;
use std::result::Result;
use std::collections::HashMap;
use playlist::*;

// -----------------------------------------------------------------------------------------------
// Playlist parser
// -----------------------------------------------------------------------------------------------

/// Parse a m3u8 playlist.
///
/// #Examples
///
/// let mut file = std::fs::File::open("playlist.m3u8").unwrap();
/// let mut bytes: Vec<u8> = Vec::new();
/// file.read_to_end(&mut bytes).unwrap();
///
/// let parsed = m3u8_rs::parse_playlist(&bytes);
///
/// let playlist = match parsed {
///     IResult::Done(i, playlist) => playlist,
///     IResult::Error(e) =>  panic!("Parsing error: \n{}", e),
///     IResult::Incomplete(e) => panic!("Parsing error: \n{:?}", e),
/// };
///
/// match playlist {
///     Playlist::MasterPlaylist(pl) => println!("Master playlist:\n{:?}", pl),
///     Playlist::MediaPlaylist(pl) => println!("Media playlist:\n{:?}", pl),
/// }
pub fn parse_playlist(input: &[u8]) -> IResult<&[u8], Playlist> {
    match is_master_playlist(input) {
        true => parse_master_playlist(input).map(Playlist::MasterPlaylist),
        false => parse_media_playlist(input).map(Playlist::MediaPlaylist),
    }
}

/// Parse a m3u8 playlist just like `parse_playlist`. This returns a Result<PLaylist,_>.
///
/// #Examples
///
/// ```
/// use m3u8_rs::playlist::{Playlist};
/// use std::io::Read;
///
/// let mut file = std::fs::File::open("playlist.m3u8").unwrap();
/// let mut bytes: Vec<u8> = Vec::new();
/// file.read_to_end(&mut bytes).unwrap();
///
/// let parsed = m3u8_rs::parse_playlist_res(&bytes);
///
/// match parsed {
///     Ok(Playlist::MasterPlaylist(pl)) => println!("Master playlist:\n{:?}", pl),
///     Ok(Playlist::MediaPlaylist(pl)) => println!("Media playlist:\n{:?}", pl),
///     Err(e) => println!("Error: {:?}", e)
/// }
/// ```
pub fn parse_playlist_res(input: &[u8]) -> Result<Playlist, IResult<&[u8], Playlist>> {
    let parse_result = parse_playlist(input);
    match parse_result {
        IResult::Done(_, playlist) => Ok(playlist),
        _ => Err(parse_result),
    }
}

/// Parse input as a master playlist
pub fn parse_master_playlist(input: &[u8]) -> IResult<&[u8], MasterPlaylist> {
    parse_master_playlist_tags(input).map(MasterPlaylist::from_tags)
}

/// Parse input as a master playlist
pub fn parse_master_playlist_res(input: &[u8]) -> Result<MasterPlaylist, IResult<&[u8], MasterPlaylist>> {
    let parse_result = parse_master_playlist(input);
    match parse_result {
        IResult::Done(_, playlist) => Ok(playlist),
        _ => Err(parse_result),
    }
}

/// Parse input as a media playlist
pub fn parse_media_playlist(input: &[u8]) -> IResult<&[u8], MediaPlaylist> {
    parse_media_playlist_tags(input).map(MediaPlaylist::from_tags)
}

/// Parse input as a media playlist
pub fn parse_media_playlist_res(input: &[u8]) -> Result<MediaPlaylist, IResult<&[u8], MediaPlaylist>> {
    let parse_result = parse_media_playlist(input);
    match parse_result {
        IResult::Done(_, playlist) => Ok(playlist),
        _ => Err(parse_result),
    }
}

/// When a media tag or no master tag is found, this returns false.
pub fn is_master_playlist(input: &[u8]) -> bool {
    // Assume it's not a master playlist
    contains_master_tag(input).map(|t| t.0).unwrap_or(false)
}

/// Scans input looking for either a master or media `#EXT` tag.
///
/// Returns `Some(true/false)` when a master/media tag is found. Otherwise returns `None`.
///
/// - None: Unkown tag or empty line
/// - Some(true, tagstring): Line contains a master playlist tag
/// - Some(false, tagstring): Line contains a media playlist tag
pub fn contains_master_tag(input: &[u8]) -> Option<(bool, String)> {

    let mut is_master_opt = None;
    let mut current_input: &[u8] = input;

    while is_master_opt == None {
        match is_master_playlist_tag_line(current_input) {
            IResult::Done(rest, result) => {
                current_input = rest;
                is_master_opt = result; // result can be None (no media or master tag found)
            }
            _ => break, // Parser error encountered, can't read any more lines.
        }
    }

    is_master_opt
}

named!(pub is_master_playlist_tag_line(&[u8]) -> Option<(bool, String)>,
    chain!(
        tag: opt!(alt!(
                  map!(tag!("#EXT-X-STREAM-INF"),         |t| (true, t))
                | map!(tag!("#EXT-X-I-FRAME-STREAM-INF"), |t| (true, t))
                | map!(tag!("#EXT-X-MEDIA"),              |t| (true, t))
                | map!(tag!("#EXT-X-SESSION-KEY"),        |t| (true, t))
                | map!(tag!("#EXT-X-SESSION-DATA"),       |t| (true, t))

                | map!(tag!("#EXT-X-TARGETDURATION"),         |t| (false, t))
                | map!(tag!("#EXT-X-MEDIA-SEQUENCE"),         |t| (false, t))
                | map!(tag!("#EXT-X-DISCONTINUITY-SEQUENCE"), |t| (false, t))
                | map!(tag!("#EXT-X-ENDLIST"),                |t| (false, t))
                | map!(tag!("#EXT-X-PLAYLIST-TYPE"),          |t| (false, t))
                | map!(tag!("#EXT-X-I-FRAMES-ONLY"),          |t| (false, t))

                | map!(tag!("#EXTINF"),                       |t| (false, t))
                | map!(tag!("#EXT-X-BYTERANGE"),              |t| (false, t))
                | map!(tag!("#EXT-X-DISCONTINUITY"),          |t| (false, t))
                | map!(tag!("#EXT-X-KEY"),                    |t| (false, t))
                | map!(tag!("#EXT-X-MAP"),                    |t| (false, t))
                | map!(tag!("#EXT-X-PROGRAM-DATE-TIME"),      |t| (false, t))
                | map!(tag!("#EXT-X-DATERANGE"),              |t| (false, t))
        ))
        ~ consume_line
        , || {
            tag.map(|(a,b)| (a, from_utf8_slice(b).unwrap()))
        }
    )
);

// -----------------------------------------------------------------------------------------------
// Master Playlist Tags
// -----------------------------------------------------------------------------------------------

pub fn parse_master_playlist_tags(input: &[u8]) -> IResult<&[u8], Vec<MasterPlaylistTag>> {
    chain!(input,
        mut tags: many0!(chain!(m:master_playlist_tag ~ multispace?, || m)) ~ eof?,
        || { tags.reverse(); tags }
    )
}

/// Contains all the tags required to parse a master playlist.
#[derive(Debug)]
pub enum MasterPlaylistTag {
    M3U(String),
    Version(usize),
    VariantStream(VariantStream),
    AlternativeMedia(AlternativeMedia),
    SessionData(SessionData),
    SessionKey(SessionKey),
    Start(Start),
    IndependentSegments,
    Unknown(ExtTag),
    Comment(String),
    Uri(String),
}

pub fn master_playlist_tag(input: &[u8]) -> IResult<&[u8], MasterPlaylistTag> {
    alt!(input,
          map!(m3u_tag, MasterPlaylistTag::M3U)
        | map!(version_tag, MasterPlaylistTag::Version)

        | map!(variant_stream_tag, MasterPlaylistTag::VariantStream)
        | map!(variant_i_frame_stream_tag, MasterPlaylistTag::VariantStream)
        | map!(alternative_media_tag, MasterPlaylistTag::AlternativeMedia)
        | map!(session_data_tag, MasterPlaylistTag::SessionData)
        | map!(session_key_tag, MasterPlaylistTag::SessionKey)
        | map!(start_tag, MasterPlaylistTag::Start)
        | map!(tag!("#EXT-X-INDEPENDENT-SEGMENTS"), |_| MasterPlaylistTag::IndependentSegments)

        | map!(ext_tag, MasterPlaylistTag::Unknown)
        | map!(comment_tag, MasterPlaylistTag::Comment)

        | map!(consume_line, MasterPlaylistTag::Uri)
    )
}

named!(pub variant_stream_tag<VariantStream>,
    chain!(tag!("#EXT-X-STREAM-INF:") ~ attributes: key_value_pairs,
           || VariantStream::from_hashmap(attributes, false))
);

named!(pub variant_i_frame_stream_tag<VariantStream>,
    chain!( tag!("#EXT-X-I-FRAME-STREAM-INF:") ~ attributes: key_value_pairs,
            || VariantStream::from_hashmap(attributes, true))
);

named!(pub alternative_media_tag<AlternativeMedia>,
    chain!( tag!("#EXT-X-MEDIA:") ~ attributes: key_value_pairs,
            || AlternativeMedia::from_hashmap(attributes))
);

named!(pub session_data_tag<SessionData>,
    chain!( tag!("#EXT-X-SESSION-DATA:") ~ attributes: key_value_pairs,
            || SessionData::from_hashmap(attributes))
);

named!(pub session_key_tag<SessionKey>,
    chain!( tag!("#EXT-X-SESSION-KEY:") ~ session_key: map!(key, SessionKey),
            || session_key)
);

// -----------------------------------------------------------------------------------------------
// Media Playlist
// -----------------------------------------------------------------------------------------------

pub fn parse_media_playlist_tags(input: &[u8]) -> IResult<&[u8], Vec<MediaPlaylistTag>> {
    chain!(input,
        mut tags: many0!(chain!(m:media_playlist_tag ~ multispace?, || m)) ~ eof?,
        || { tags.reverse(); tags }
    )
}

/// Contains all the tags required to parse a media playlist.
#[derive(Debug)]
pub enum MediaPlaylistTag {
    M3U(String),
    Version(usize),
    Segment(SegmentTag),
    TargetDuration(f32),
    MediaSequence(i32),
    DiscontinuitySequence(i32),
    EndList,
    PlaylistType(MediaPlaylistType),
    IFramesOnly,
    Start(Start),
    IndependentSegments,
}

pub fn media_playlist_tag(input: &[u8]) -> IResult<&[u8], MediaPlaylistTag> {
    alt!(input,
      map!(m3u_tag, MediaPlaylistTag::M3U)
    | map!(version_tag, MediaPlaylistTag::Version)

    | map!(chain!(tag!("#EXT-X-TARGETDURATION:") ~ n:float,||n), MediaPlaylistTag::TargetDuration)
    | map!(chain!(tag!("#EXT-X-MEDIA-SEQUENCE:") ~ n:number,||n), MediaPlaylistTag::MediaSequence)
    | map!(chain!(tag!("#EXT-X-DISCONTINUITY-SEQUENCE:") ~ n:number,||n), MediaPlaylistTag::DiscontinuitySequence)
    | map!(chain!(tag!("#EXT-X-PLAYLIST-TYPE:") ~ t:playlist_type, ||t), MediaPlaylistTag::PlaylistType)
    | map!(tag!("#EXT-X-I-FRAMES-ONLY"), |_| MediaPlaylistTag::IFramesOnly)
    | map!(start_tag, MediaPlaylistTag::Start)
    | map!(tag!("#EXT-X-INDEPENDENT-SEGMENTS"), |_| MediaPlaylistTag::IndependentSegments)
    | map!(tag!("#EXT-X-ENDLIST"), |_| MediaPlaylistTag::EndList)

    | map!(media_segment_tag, MediaPlaylistTag::Segment)
    )
}

named!(pub playlist_type<MediaPlaylistType>,
    map_res!(
        map_res!(take_until_either_and_consume!("\r\n"), str::from_utf8), 
        MediaPlaylistType::from_str
    )
);

// -----------------------------------------------------------------------------------------------
// Media Segment
// -----------------------------------------------------------------------------------------------

/// All possible media segment tags.
#[derive(Debug)]
pub enum SegmentTag {
    Extinf(f32, Option<String>),
    ByteRange(ByteRange),
    Discontinuity,
    Key(Key),
    Map(Map),
    ProgramDateTime(String),
    DateRange(String),
    Unknown(ExtTag),
    Comment(String),
    Uri(String),
}

pub fn media_segment_tag(input: &[u8]) -> IResult<&[u8], SegmentTag> {
    alt!(input,
      map!(chain!(tag!("#EXTINF:") ~ e:duration_title_tag,||e), |(a,b)| SegmentTag::Extinf(a,b))
    | map!(chain!(tag!("#EXT-X-BYTERANGE:") ~ r:byte_range_val, || r), SegmentTag::ByteRange)
    | map!(tag!("#EXT-X-DISCONTINUITY"), |_| SegmentTag::Discontinuity)
    | map!(chain!(tag!("#EXT-X-KEY:") ~ k:key, || k), SegmentTag::Key)
    | map!(chain!(tag!("#EXT-X-MAP:") ~ m:map, || m), SegmentTag::Map)
    | map!(chain!(tag!("#EXT-X-PROGRAM-DATE-TIME:") ~ t:consume_line, || t), SegmentTag::ProgramDateTime)
    | map!(chain!(tag!("#EXT-X-DATE-RANGE:") ~ t:consume_line, || t), SegmentTag::DateRange)

    | map!(ext_tag, SegmentTag::Unknown)
    | map!(comment_tag, SegmentTag::Comment)

    | map!(consume_line, SegmentTag::Uri)
    )
}

named!(pub duration_title_tag<(f32, Option<String>)>,
    chain!(
          duration: float
        ~ tag!(",")?
        ~ title: opt!(map_res!(take_until_either_and_consume!("\r\n,"), from_utf8_slice))
        ~ tag!(",")?
        ,
        || (duration, title)
    )
);

named!(pub key<Key>, map!(key_value_pairs, Key::from_hashmap));

named!(pub map<Map>, map!(key_value_pairs, Map::from_hashmap));

// -----------------------------------------------------------------------------------------------
// Basic tags
// -----------------------------------------------------------------------------------------------

named!(pub m3u_tag<String>,
    map_res!(tag!("#EXTM3U"), from_utf8_slice)
);

named!(pub version_tag<usize>,
    chain!(
        tag!("#EXT-X-VERSION:") ~ version: map_res!(digit, str::from_utf8),
        || version.parse().unwrap_or_default()
    )
);

named!(pub start_tag<Start>,
    chain!(tag!("#EXT-X-START:") ~ attributes:key_value_pairs, || Start::from_hashmap(attributes))
);

named!(pub ext_tag<ExtTag>,
    chain!(
          tag!("#EXT-")
        ~ tag: map_res!(take_until_and_consume!(":"), from_utf8_slice)
        ~ rest: map_res!(take_until_either_and_consume!("\r\n"), from_utf8_slice)
        ,
        || ExtTag { tag: tag, rest: rest }
    )
);

named!(pub comment_tag<String>,
    chain!(
        tag!("#") ~ text: map_res!(take_until_either_and_consume!("\r\n"), from_utf8_slice),
        || text
    )
);

// -----------------------------------------------------------------------------------------------
// Util
// -----------------------------------------------------------------------------------------------

named!(pub key_value_pairs(&[u8]) -> HashMap<String, String>,
    map!(
        many0!(chain!(space? ~ k:key_value_pair,|| k))
        ,
        |pairs: Vec<(String, String)>| {
            pairs.into_iter().collect()
        }
    )
);

named!(pub key_value_pair(&[u8]) -> (String, String),
    chain!(
          peek!(none_of!("\r\n"))
        ~ left: map_res!(take_until_and_consume!("="), from_utf8_slice)
        ~ right: alt!(quoted | unquoted)
        ~ char!(',')?
        ,
        || (left, right)
    )
);

named!(pub quoted<String>,
    delimited!(char!('\"'), map_res!(is_not!("\""), from_utf8_slice), char!('\"'))
);

named!(pub unquoted<String>,
    map_res!(take_until_either!(",\r\n"), from_utf8_slice)
);

named!(pub consume_line<String>,
    map_res!(take_until_either_and_consume!("\r\n"), from_utf8_slice)
);

named!(pub number<i32>,
    map_res!(map_res!(digit, str::from_utf8), str::FromStr::from_str)
);

named!(pub byte_range_val<ByteRange>,
    chain!(
          n: number
        ~ o: opt!(chain!(char!('@') ~ n:number,||n))
        ,
        || ByteRange { length: n, offset: o }
    )
);

named!(pub float<f32>,
    chain!(
          left: map_res!(digit, str::from_utf8)
        ~ right_opt: opt!(chain!(char!('.') ~ d:map_res!(digit, str::from_utf8),|| d)),
        ||
        match right_opt {
            Some(right) => {
                let mut num = String::from(left);
                num.push('.');
                num.push_str(right);
                num.parse().unwrap()
            },
            None => left.parse().unwrap(),
        }
    )
);

pub fn from_utf8_slice(s: &[u8]) -> Result<String, string::FromUtf8Error> {
    String::from_utf8(s.to_vec())
}

pub fn from_utf8_slice2(s: &[u8]) -> Result<String, str::Utf8Error> {
    str::from_utf8(s).map(String::from)
}