1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 13:39:27 +00:00
hls_m3u8/src/media_segment.rs

140 lines
4.4 KiB
Rust
Raw Normal View History

2019-09-13 14:06:52 +00:00
use std::fmt;
use std::iter;
2019-09-14 19:21:44 +00:00
use derive_builder::Builder;
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,
};
2019-03-31 09:58:11 +00:00
use crate::types::{ProtocolVersion, SingleLineString};
2018-02-12 16:00:23 +00:00
2019-09-14 19:21:44 +00:00
/// Media segment.
#[derive(Debug, Clone, Builder)]
#[builder(setter(into, strip_option))]
pub struct MediaSegment {
#[builder(default)]
/// Sets all [ExtXKey] tags.
2018-02-14 19:47:44 +00:00
key_tags: Vec<ExtXKey>,
2019-09-14 19:21:44 +00:00
#[builder(default)]
/// Sets an [ExtXMap] tag.
2018-02-14 19:47:44 +00:00
map_tag: Option<ExtXMap>,
2019-09-14 19:21:44 +00:00
#[builder(default)]
/// Sets an [ExtXByteRange] tag.
2018-02-14 19:47:44 +00:00
byte_range_tag: Option<ExtXByteRange>,
2019-09-14 19:21:44 +00:00
#[builder(default)]
/// Sets an [ExtXDateRange] tag.
2018-02-14 19:47:44 +00:00
date_range_tag: Option<ExtXDateRange>,
2019-09-14 19:21:44 +00:00
#[builder(default)]
/// Sets an [ExtXDiscontinuity] tag.
2018-02-14 19:47:44 +00:00
discontinuity_tag: Option<ExtXDiscontinuity>,
2019-09-14 19:21:44 +00:00
#[builder(default)]
/// Sets an [ExtXProgramDateTime] tag.
2018-02-14 19:47:44 +00:00
program_date_time_tag: Option<ExtXProgramDateTime>,
2019-09-14 19:21:44 +00:00
/// Sets an [ExtInf] tag.
inf_tag: ExtInf,
/// Sets an Uri.
uri: SingleLineString,
2018-02-12 16:00:23 +00:00
}
2019-09-08 10:23:33 +00:00
2018-02-12 16:00:23 +00:00
impl MediaSegmentBuilder {
2019-09-14 19:21:44 +00:00
/// Pushes an [ExtXKey] tag.
pub fn push_key_tag<VALUE: Into<ExtXKey>>(&mut self, value: VALUE) -> &mut Self {
if let Some(key_tags) = &mut self.key_tags {
key_tags.push(value.into());
} else {
self.key_tags = Some(vec![value.into()]);
2018-02-12 16:00:23 +00:00
}
self
}
}
2019-09-08 10:23:33 +00:00
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(())
}
}
2019-09-08 10:23:33 +00:00
2018-02-12 16:00:23 +00:00
impl MediaSegment {
2019-09-14 19:21:44 +00:00
/// Creates a [MediaSegmentBuilder].
pub fn builder() -> MediaSegmentBuilder {
MediaSegmentBuilder::default()
}
2018-02-14 19:47:44 +00:00
/// Returns the URI of the media segment.
2019-09-08 10:23:33 +00:00
pub const 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.
2019-09-08 10:23:33 +00:00
pub const fn inf_tag(&self) -> &ExtInf {
2018-02-14 19:47:44 +00:00
&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.
2019-09-08 10:23:33 +00:00
pub const fn byte_range_tag(&self) -> Option<ExtXByteRange> {
2018-02-14 19:47:44 +00:00
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.
2019-09-08 10:23:33 +00:00
pub const fn discontinuity_tag(&self) -> Option<ExtXDiscontinuity> {
2018-02-14 19:47:44 +00:00
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.
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()
2019-09-14 19:21:44 +00:00
.unwrap_or(ProtocolVersion::V7)
2018-02-12 16:00:23 +00:00
}
}