2019-09-13 14:06:52 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-09-14 19:21:44 +00:00
|
|
|
use derive_builder::Builder;
|
2020-02-02 12:38:11 +00:00
|
|
|
use shorthand::ShortHand;
|
2019-09-14 19:21:44 +00:00
|
|
|
|
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,
|
|
|
|
};
|
2020-03-23 12:34:26 +00:00
|
|
|
use crate::types::{DecryptionKey, ProtocolVersion};
|
|
|
|
use crate::{Decryptable, RequiredVersion};
|
2018-02-12 16:00:23 +00:00
|
|
|
|
2019-10-04 09:02:21 +00:00
|
|
|
/// Media segment.
|
2020-02-02 12:38:11 +00:00
|
|
|
#[derive(ShortHand, Debug, Clone, Builder, PartialEq, PartialOrd)]
|
|
|
|
#[builder(setter(into, strip_option))]
|
|
|
|
#[shorthand(enable(must_use, get_mut, collection_magic))]
|
2019-09-14 19:21:44 +00:00
|
|
|
pub struct MediaSegment {
|
2020-02-02 13:33:57 +00:00
|
|
|
/// All [`ExtXKey`] tags.
|
2020-02-02 12:38:11 +00:00
|
|
|
#[builder(default)]
|
2019-10-04 09:02:21 +00:00
|
|
|
keys: Vec<ExtXKey>,
|
2020-02-02 12:38:11 +00:00
|
|
|
/// The [`ExtXMap`] tag associated with the media segment.
|
2019-09-14 19:21:44 +00:00
|
|
|
#[builder(default)]
|
2020-02-16 11:50:52 +00:00
|
|
|
map: Option<ExtXMap>,
|
2020-02-02 12:38:11 +00:00
|
|
|
/// The [`ExtXByteRange`] tag associated with the [`MediaSegment`].
|
2019-09-14 19:21:44 +00:00
|
|
|
#[builder(default)]
|
2020-02-16 11:50:52 +00:00
|
|
|
byte_range: Option<ExtXByteRange>,
|
2020-02-02 12:38:11 +00:00
|
|
|
/// The [`ExtXDateRange`] tag associated with the media segment.
|
2019-09-14 19:21:44 +00:00
|
|
|
#[builder(default)]
|
2020-02-16 11:50:52 +00:00
|
|
|
date_range: Option<ExtXDateRange>,
|
2020-02-02 12:38:11 +00:00
|
|
|
/// The [`ExtXDiscontinuity`] tag associated with the media segment.
|
2019-09-14 19:21:44 +00:00
|
|
|
#[builder(default)]
|
2020-02-16 11:50:52 +00:00
|
|
|
discontinuity: Option<ExtXDiscontinuity>,
|
2020-02-02 12:38:11 +00:00
|
|
|
/// The [`ExtXProgramDateTime`] tag associated with the media
|
|
|
|
/// segment.
|
2019-09-14 19:21:44 +00:00
|
|
|
#[builder(default)]
|
2020-02-16 11:50:52 +00:00
|
|
|
program_date_time: Option<ExtXProgramDateTime>,
|
2020-02-02 12:38:11 +00:00
|
|
|
/// The [`ExtInf`] tag associated with the [`MediaSegment`].
|
2020-02-16 11:50:52 +00:00
|
|
|
inf: ExtInf,
|
2020-02-02 12:38:11 +00:00
|
|
|
/// The `URI` of the [`MediaSegment`].
|
|
|
|
#[shorthand(enable(into))]
|
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 {
|
2020-02-14 12:05:18 +00:00
|
|
|
/// Returns a builder for a [`MediaSegment`].
|
2020-02-24 15:45:10 +00:00
|
|
|
#[must_use]
|
|
|
|
#[inline]
|
2019-10-05 14:24:48 +00:00
|
|
|
pub fn builder() -> MediaSegmentBuilder { MediaSegmentBuilder::default() }
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:00:23 +00:00
|
|
|
impl MediaSegmentBuilder {
|
2019-10-04 09:02:21 +00:00
|
|
|
/// Pushes an [`ExtXKey`] tag.
|
2020-02-16 11:50:52 +00:00
|
|
|
pub fn push_key<VALUE: Into<ExtXKey>>(&mut self, value: VALUE) -> &mut Self {
|
|
|
|
if let Some(keys) = &mut self.keys {
|
|
|
|
keys.push(value.into());
|
2019-09-14 19:21:44 +00:00
|
|
|
} else {
|
2019-10-04 09:02:21 +00:00
|
|
|
self.keys = Some(vec![value.into()]);
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
2020-02-16 11:50:52 +00:00
|
|
|
|
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 {
|
2020-02-16 11:51:49 +00:00
|
|
|
// self.keys will be printed by MediaPlaylist!
|
2020-02-02 12:38:11 +00:00
|
|
|
|
2020-02-16 11:50:52 +00:00
|
|
|
if let Some(value) = &self.map {
|
2019-09-14 19:42:06 +00:00
|
|
|
writeln!(f, "{}", value)?;
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
2020-02-02 12:38:11 +00:00
|
|
|
|
2020-02-16 11:50:52 +00:00
|
|
|
if let Some(value) = &self.byte_range {
|
2019-09-14 19:42:06 +00:00
|
|
|
writeln!(f, "{}", value)?;
|
2018-02-14 19:47:44 +00:00
|
|
|
}
|
2020-02-02 12:38:11 +00:00
|
|
|
|
2020-02-16 11:50:52 +00:00
|
|
|
if let Some(value) = &self.date_range {
|
2019-09-14 19:42:06 +00:00
|
|
|
writeln!(f, "{}", value)?;
|
2018-02-14 19:47:44 +00:00
|
|
|
}
|
2020-02-02 12:38:11 +00:00
|
|
|
|
2020-02-16 11:50:52 +00:00
|
|
|
if let Some(value) = &self.discontinuity {
|
2019-09-14 19:42:06 +00:00
|
|
|
writeln!(f, "{}", value)?;
|
2018-02-14 19:47:44 +00:00
|
|
|
}
|
2020-02-02 12:38:11 +00:00
|
|
|
|
2020-02-16 11:50:52 +00:00
|
|
|
if let Some(value) = &self.program_date_time {
|
2019-09-14 19:42:06 +00:00
|
|
|
writeln!(f, "{}", value)?;
|
2018-02-14 19:47:44 +00:00
|
|
|
}
|
2020-02-02 12:38:11 +00:00
|
|
|
|
2020-02-16 11:50:52 +00:00
|
|
|
writeln!(f, "{}", self.inf)?; // TODO: there might be a `,` missing
|
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,
|
2020-02-16 11:50:52 +00:00
|
|
|
self.map,
|
|
|
|
self.byte_range,
|
|
|
|
self.date_range,
|
|
|
|
self.discontinuity,
|
|
|
|
self.program_date_time,
|
|
|
|
self.inf
|
2019-10-05 14:24:48 +00:00
|
|
|
]
|
2018-02-12 16:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-04 09:02:21 +00:00
|
|
|
|
2020-03-23 12:34:26 +00:00
|
|
|
impl Decryptable for MediaSegment {
|
|
|
|
fn keys(&self) -> Vec<&DecryptionKey> {
|
|
|
|
//
|
|
|
|
self.keys.iter().filter_map(ExtXKey::as_ref).collect()
|
|
|
|
}
|
2019-10-04 09:02:21 +00:00
|
|
|
}
|
2019-10-06 14:39:18 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-10-08 13:42:33 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2019-10-06 14:39:18 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_display() {
|
|
|
|
assert_eq!(
|
|
|
|
MediaSegment::builder()
|
2020-02-16 11:50:52 +00:00
|
|
|
.map(ExtXMap::new("https://www.example.com/"))
|
2020-02-23 17:56:41 +00:00
|
|
|
.byte_range(ExtXByteRange::from(5..25))
|
2020-02-16 11:50:52 +00:00
|
|
|
//.date_range() // TODO!
|
|
|
|
.discontinuity(ExtXDiscontinuity)
|
|
|
|
.inf(ExtInf::new(Duration::from_secs(4)))
|
2019-10-06 14:39:18 +00:00
|
|
|
.uri("http://www.uri.com/")
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
2020-02-14 12:05:18 +00:00
|
|
|
concat!(
|
|
|
|
"#EXT-X-MAP:URI=\"https://www.example.com/\"\n",
|
|
|
|
"#EXT-X-BYTERANGE:20@5\n",
|
|
|
|
"#EXT-X-DISCONTINUITY\n",
|
|
|
|
"#EXTINF:4,\n",
|
|
|
|
"http://www.uri.com/\n"
|
|
|
|
)
|
|
|
|
.to_string()
|
2019-10-06 14:39:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|