2019-03-31 09:58:11 +00:00
|
|
|
use crate::tags::{
|
2018-10-04 11:18:56 +00:00
|
|
|
ExtInf, ExtXByteRange, ExtXDateRange, ExtXDiscontinuity, ExtXKey, ExtXMap, ExtXProgramDateTime,
|
|
|
|
MediaSegmentTag,
|
|
|
|
};
|
2019-03-31 09:58:11 +00:00
|
|
|
use crate::types::{ProtocolVersion, SingleLineString};
|
|
|
|
use crate::{ErrorKind, Result};
|
|
|
|
use std::fmt;
|
|
|
|
use std::iter;
|
2018-02-12 16:00:23 +00:00
|
|
|
|
2018-02-14 19:47:44 +00:00
|
|
|
/// Media segment builder.
|
2018-02-12 16:00:23 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct MediaSegmentBuilder {
|
2018-02-14 19:47:44 +00:00
|
|
|
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>,
|
2018-02-14 15:50:57 +00:00
|
|
|
uri: Option<SingleLineString>,
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
|
|
|
impl MediaSegmentBuilder {
|
2018-02-14 19:47:44 +00:00
|
|
|
/// Makes a new `MediaSegmentBuilder` instance.
|
2018-02-12 16:00:23 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
MediaSegmentBuilder {
|
2018-02-14 19:47:44 +00:00
|
|
|
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,
|
2018-02-12 16:00:23 +00:00
|
|
|
uri: None,
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// Sets the URI of the resulting media segment.
|
2018-02-14 15:50:57 +00:00
|
|
|
pub fn uri(&mut self, uri: SingleLineString) -> &mut Self {
|
2018-02-12 16:00:23 +00:00
|
|
|
self.uri = Some(uri);
|
|
|
|
self
|
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// Sets the given tag to the resulting media segment.
|
2018-02-12 16:00:23 +00:00
|
|
|
pub fn tag<T: Into<MediaSegmentTag>>(&mut self, tag: T) -> &mut Self {
|
|
|
|
match tag.into() {
|
2018-02-14 19:47:44 +00:00
|
|
|
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),
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// Builds a `MediaSegment` instance.
|
2018-02-12 16:00:23 +00:00
|
|
|
pub fn finish(self) -> Result<MediaSegment> {
|
|
|
|
let uri = track_assert_some!(self.uri, ErrorKind::InvalidInput);
|
2018-02-14 19:47:44 +00:00
|
|
|
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()
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:47:44 +00:00
|
|
|
/// Media segment.
|
2018-02-12 16:00:23 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct MediaSegment {
|
2018-02-14 19:47:44 +00:00
|
|
|
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,
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
|
|
|
impl fmt::Display for MediaSegment {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-02-14 19:47:44 +00:00
|
|
|
for t in &self.key_tags {
|
|
|
|
writeln!(f, "{}", t)?;
|
|
|
|
}
|
|
|
|
if let Some(ref t) = self.map_tag {
|
|
|
|
writeln!(f, "{}", t)?;
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
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)?;
|
|
|
|
}
|
2019-05-20 07:01:01 +00:00
|
|
|
writeln!(f, "{},", self.inf_tag)?;
|
2018-02-12 16:00:23 +00:00
|
|
|
writeln!(f, "{}", self.uri)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl MediaSegment {
|
2018-02-14 19:47:44 +00:00
|
|
|
/// Returns the URI of the media segment.
|
|
|
|
pub fn uri(&self) -> &SingleLineString {
|
2018-02-12 16:00:23 +00:00
|
|
|
&self.uri
|
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// Returns the `EXT-X-INF` tag associated with the media segment.
|
|
|
|
pub fn inf_tag(&self) -> &ExtInf {
|
|
|
|
&self.inf_tag
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// Returns the `EXT-X-BYTERANGE` tag associated with the media segment.
|
|
|
|
pub fn byte_range_tag(&self) -> Option<ExtXByteRange> {
|
|
|
|
self.byte_range_tag
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// 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()
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// Returns the `EXT-X-DISCONTINUITY` tag associated with the media segment.
|
|
|
|
pub fn discontinuity_tag(&self) -> Option<ExtXDiscontinuity> {
|
|
|
|
self.discontinuity_tag
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// Returns the `EXT-X-PROGRAM-DATE-TIME` tag associated with the media segment.
|
2018-02-15 12:01:38 +00:00
|
|
|
pub fn program_date_time_tag(&self) -> Option<&ExtXProgramDateTime> {
|
|
|
|
self.program_date_time_tag.as_ref()
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// 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
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
2018-02-14 19:47:44 +00:00
|
|
|
|
|
|
|
/// 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()),
|
2019-03-31 09:54:21 +00:00
|
|
|
)
|
|
|
|
.chain(iter::once(self.inf_tag.requires_version()))
|
2018-02-14 19:47:44 +00:00
|
|
|
.max()
|
|
|
|
.expect("Never fails")
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
|
|
|
}
|