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
use std::fmt;
use std::iter;

use tags::{
    ExtInf, ExtXByteRange, ExtXDateRange, ExtXDiscontinuity, ExtXKey, ExtXMap, ExtXProgramDateTime,
    MediaSegmentTag,
};
use types::{ProtocolVersion, SingleLineString};
use {ErrorKind, Result};

/// Media segment builder.
#[derive(Debug, Clone)]
pub struct MediaSegmentBuilder {
    key_tags: Vec<ExtXKey>,
    map_tag: Option<ExtXMap>,
    byte_range_tag: Option<ExtXByteRange>,
    date_range_tag: Option<ExtXDateRange>,
    discontinuity_tag: Option<ExtXDiscontinuity>,
    program_date_time_tag: Option<ExtXProgramDateTime>,
    inf_tag: Option<ExtInf>,
    uri: Option<SingleLineString>,
}
impl MediaSegmentBuilder {
    /// Makes a new `MediaSegmentBuilder` instance.
    pub fn new() -> Self {
        MediaSegmentBuilder {
            key_tags: Vec::new(),
            map_tag: None,
            byte_range_tag: None,
            date_range_tag: None,
            discontinuity_tag: None,
            program_date_time_tag: None,
            inf_tag: None,
            uri: None,
        }
    }

    /// Sets the URI of the resulting media segment.
    pub fn uri(&mut self, uri: SingleLineString) -> &mut Self {
        self.uri = Some(uri);
        self
    }

    /// Sets the given tag to the resulting media segment.
    pub fn tag<T: Into<MediaSegmentTag>>(&mut self, tag: T) -> &mut Self {
        match tag.into() {
            MediaSegmentTag::ExtInf(t) => self.inf_tag = Some(t),
            MediaSegmentTag::ExtXByteRange(t) => self.byte_range_tag = Some(t),
            MediaSegmentTag::ExtXDateRange(t) => self.date_range_tag = Some(t),
            MediaSegmentTag::ExtXDiscontinuity(t) => self.discontinuity_tag = Some(t),
            MediaSegmentTag::ExtXKey(t) => self.key_tags.push(t),
            MediaSegmentTag::ExtXMap(t) => self.map_tag = Some(t),
            MediaSegmentTag::ExtXProgramDateTime(t) => self.program_date_time_tag = Some(t),
        }
        self
    }

    /// Builds a `MediaSegment` instance.
    pub fn finish(self) -> Result<MediaSegment> {
        let uri = track_assert_some!(self.uri, ErrorKind::InvalidInput);
        let inf_tag = track_assert_some!(self.inf_tag, ErrorKind::InvalidInput);
        Ok(MediaSegment {
            key_tags: self.key_tags,
            map_tag: self.map_tag,
            byte_range_tag: self.byte_range_tag,
            date_range_tag: self.date_range_tag,
            discontinuity_tag: self.discontinuity_tag,
            program_date_time_tag: self.program_date_time_tag,
            inf_tag,
            uri,
        })
    }
}
impl Default for MediaSegmentBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Media segment.
#[derive(Debug, Clone)]
pub struct MediaSegment {
    key_tags: Vec<ExtXKey>,
    map_tag: Option<ExtXMap>,
    byte_range_tag: Option<ExtXByteRange>,
    date_range_tag: Option<ExtXDateRange>,
    discontinuity_tag: Option<ExtXDiscontinuity>,
    program_date_time_tag: Option<ExtXProgramDateTime>,
    inf_tag: ExtInf,
    uri: SingleLineString,
}
impl fmt::Display for MediaSegment {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for t in &self.key_tags {
            writeln!(f, "{}", t)?;
        }
        if let Some(ref t) = self.map_tag {
            writeln!(f, "{}", t)?;
        }
        if let Some(ref t) = self.byte_range_tag {
            writeln!(f, "{}", t)?;
        }
        if let Some(ref t) = self.date_range_tag {
            writeln!(f, "{}", t)?;
        }
        if let Some(ref t) = self.discontinuity_tag {
            writeln!(f, "{}", t)?;
        }
        if let Some(ref t) = self.program_date_time_tag {
            writeln!(f, "{}", t)?;
        }
        writeln!(f, "{}", self.inf_tag)?;
        writeln!(f, "{}", self.uri)?;
        Ok(())
    }
}
impl MediaSegment {
    /// Returns the URI of the media segment.
    pub fn uri(&self) -> &SingleLineString {
        &self.uri
    }

    /// Returns the `EXT-X-INF` tag associated with the media segment.
    pub fn inf_tag(&self) -> &ExtInf {
        &self.inf_tag
    }

    /// Returns the `EXT-X-BYTERANGE` tag associated with the media segment.
    pub fn byte_range_tag(&self) -> Option<ExtXByteRange> {
        self.byte_range_tag
    }

    /// Returns the `EXT-X-DATERANGE` tag associated with the media segment.
    pub fn date_range_tag(&self) -> Option<&ExtXDateRange> {
        self.date_range_tag.as_ref()
    }

    /// Returns the `EXT-X-DISCONTINUITY` tag associated with the media segment.
    pub fn discontinuity_tag(&self) -> Option<ExtXDiscontinuity> {
        self.discontinuity_tag
    }

    /// Returns the `EXT-X-PROGRAM-DATE-TIME` tag associated with the media segment.
    pub fn program_date_time_tag(&self) -> Option<&ExtXProgramDateTime> {
        self.program_date_time_tag.as_ref()
    }

    /// Returns the `EXT-X-MAP` tag associated with the media segment.
    pub fn map_tag(&self) -> Option<&ExtXMap> {
        self.map_tag.as_ref()
    }

    /// Returns the `EXT-X-KEY` tags associated with the media segment.
    pub fn key_tags(&self) -> &[ExtXKey] {
        &self.key_tags
    }

    /// Returns the protocol compatibility version that this segment requires.
    pub fn requires_version(&self) -> ProtocolVersion {
        iter::empty()
            .chain(self.key_tags.iter().map(|t| t.requires_version()))
            .chain(self.map_tag.iter().map(|t| t.requires_version()))
            .chain(self.byte_range_tag.iter().map(|t| t.requires_version()))
            .chain(self.date_range_tag.iter().map(|t| t.requires_version()))
            .chain(self.discontinuity_tag.iter().map(|t| t.requires_version()))
            .chain(
                self.program_date_time_tag
                    .iter()
                    .map(|t| t.requires_version()),
            ).chain(iter::once(self.inf_tag.requires_version()))
            .max()
            .expect("Never fails")
    }
}