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

189 lines
5.6 KiB
Rust
Raw Normal View History

2019-09-13 14:06:52 +00:00
use std::fmt;
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-10-04 09:02:21 +00:00
use crate::types::ProtocolVersion;
use crate::{Encrypted, RequiredVersion};
2018-02-12 16:00:23 +00:00
2019-09-14 19:21:44 +00:00
#[derive(Debug, Clone, Builder)]
#[builder(setter(into, strip_option))]
2019-10-04 09:02:21 +00:00
/// Media segment.
2019-09-14 19:21:44 +00:00
pub struct MediaSegment {
#[builder(default)]
2019-10-04 09:02:21 +00:00
/// Sets all [`ExtXKey`] tags.
keys: Vec<ExtXKey>,
2019-09-14 19:21:44 +00:00
#[builder(default)]
2019-10-04 09:02:21 +00:00
/// 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)]
2019-10-04 09:02:21 +00:00
/// 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)]
2019-10-04 09:02:21 +00:00
/// 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)]
2019-10-04 09:02:21 +00:00
/// 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)]
2019-10-04 09:02:21 +00:00
/// Sets an [`ExtXProgramDateTime`] tag.
2018-02-14 19:47:44 +00:00
program_date_time_tag: Option<ExtXProgramDateTime>,
2019-10-04 09:02:21 +00:00
/// Sets an [`ExtInf`] tag.
2019-09-14 19:21:44 +00:00
inf_tag: ExtInf,
2019-10-04 09:02:21 +00:00
/// Sets an `URI`.
2019-09-21 10:11:36 +00:00
uri: String,
2018-02-12 16:00:23 +00:00
}
2019-09-08 10:23:33 +00:00
2019-10-05 14:24:48 +00:00
impl MediaSegment {
/// Returns a Builder for a [`MasterPlaylist`].
pub fn builder() -> MediaSegmentBuilder { MediaSegmentBuilder::default() }
/// Returns the `URI` of the media segment.
pub const fn uri(&self) -> &String { &self.uri }
/// Sets the `URI` of the media segment.
pub fn set_uri<T>(&mut self, value: T) -> &mut Self
where
T: Into<String>,
{
self.uri = value.into();
self
}
/// Returns the [`ExtInf`] tag associated with the media segment.
pub const fn inf_tag(&self) -> &ExtInf { &self.inf_tag }
/// Sets the [`ExtInf`] tag associated with the media segment.
pub fn set_inf_tag<T>(&mut self, value: T) -> &mut Self
where
T: Into<ExtInf>,
{
self.inf_tag = value.into();
self
}
/// Returns the [`ExtXByteRange`] tag associated with the media segment.
pub const fn byte_range_tag(&self) -> Option<ExtXByteRange> { self.byte_range_tag }
/// Sets the [`ExtXByteRange`] tag associated with the media segment.
pub fn set_byte_range_tag<T>(&mut self, value: Option<T>) -> &mut Self
where
T: Into<ExtXByteRange>,
{
self.byte_range_tag = value.map(Into::into);
self
}
/// Returns the [`ExtXDateRange`] tag associated with the media segment.
pub const fn date_range_tag(&self) -> &Option<ExtXDateRange> { &self.date_range_tag }
/// Sets the [`ExtXDateRange`] tag associated with the media segment.
pub fn set_date_range_tag<T>(&mut self, value: Option<T>) -> &mut Self
where
T: Into<ExtXDateRange>,
{
self.date_range_tag = value.map(Into::into);
self
}
/// Returns the [`ExtXDiscontinuity`] tag associated with the media segment.
pub const fn discontinuity_tag(&self) -> Option<ExtXDiscontinuity> { self.discontinuity_tag }
/// Sets the [`ExtXDiscontinuity`] tag associated with the media segment.
pub fn set_discontinuity_tag<T>(&mut self, value: Option<T>) -> &mut Self
where
T: Into<ExtXDiscontinuity>,
{
self.discontinuity_tag = value.map(Into::into);
self
}
/// Returns the [`ExtXProgramDateTime`] tag associated with the media
/// segment.
pub const fn program_date_time_tag(&self) -> Option<ExtXProgramDateTime> {
self.program_date_time_tag
}
/// Sets the [`ExtXProgramDateTime`] tag associated with the media
/// segment.
pub fn set_program_date_time_tag<T>(&mut self, value: Option<T>) -> &mut Self
where
T: Into<ExtXProgramDateTime>,
{
self.program_date_time_tag = value.map(Into::into);
self
}
/// Returns the [`ExtXMap`] tag associated with the media segment.
pub const fn map_tag(&self) -> &Option<ExtXMap> { &self.map_tag }
/// Sets the [`ExtXMap`] tag associated with the media segment.
pub fn set_map_tag<T>(&mut self, value: Option<T>) -> &mut Self
where
T: Into<ExtXMap>,
{
self.map_tag = value.map(Into::into);
self
}
}
2018-02-12 16:00:23 +00:00
impl MediaSegmentBuilder {
2019-10-04 09:02:21 +00:00
/// Pushes an [`ExtXKey`] tag.
2019-09-14 19:21:44 +00:00
pub fn push_key_tag<VALUE: Into<ExtXKey>>(&mut self, value: VALUE) -> &mut Self {
2019-10-04 09:02:21 +00:00
if let Some(key_tags) = &mut self.keys {
2019-09-14 19:21:44 +00:00
key_tags.push(value.into());
} else {
2019-10-04 09:02:21 +00:00
self.keys = 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 {
2019-10-04 09:02:21 +00:00
for value in &self.keys {
2019-09-14 19:42:06 +00:00
writeln!(f, "{}", value)?;
2018-02-14 19:47:44 +00:00
}
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.map_tag {
writeln!(f, "{}", value)?;
2018-02-12 16:00:23 +00:00
}
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.byte_range_tag {
writeln!(f, "{}", value)?;
2018-02-14 19:47:44 +00:00
}
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.date_range_tag {
writeln!(f, "{}", value)?;
2018-02-14 19:47:44 +00:00
}
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.discontinuity_tag {
writeln!(f, "{}", value)?;
2018-02-14 19:47:44 +00:00
}
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.program_date_time_tag {
writeln!(f, "{}", value)?;
2018-02-14 19:47:44 +00:00
}
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
2019-09-22 08:57:28 +00:00
impl RequiredVersion for MediaSegment {
fn required_version(&self) -> ProtocolVersion {
2019-10-05 14:24:48 +00:00
required_version![
self.keys,
self.map_tag,
self.byte_range_tag,
self.date_range_tag,
self.discontinuity_tag,
self.program_date_time_tag,
self.inf_tag
]
2018-02-12 16:00:23 +00:00
}
}
2019-10-04 09:02:21 +00:00
impl Encrypted for MediaSegment {
fn keys(&self) -> &Vec<ExtXKey> { &self.keys }
fn keys_mut(&mut self) -> &mut Vec<ExtXKey> { &mut self.keys }
}