2020-04-22 07:54:48 +00:00
|
|
|
use std::convert::TryFrom;
|
2018-02-14 10:24:39 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2020-02-10 12:13:41 +00:00
|
|
|
use shorthand::ShortHand;
|
|
|
|
|
2019-09-13 14:06:52 +00:00
|
|
|
use crate::attribute::AttributePairs;
|
2020-02-10 12:13:41 +00:00
|
|
|
use crate::types::{Float, ProtocolVersion};
|
2019-09-13 14:06:52 +00:00
|
|
|
use crate::utils::{parse_yes_or_no, tag};
|
2019-10-04 09:02:21 +00:00
|
|
|
use crate::{Error, RequiredVersion};
|
2019-09-13 14:06:52 +00:00
|
|
|
|
2020-02-24 15:44:02 +00:00
|
|
|
/// This tag indicates a preferred point at which to start
|
|
|
|
/// playing a Playlist.
|
2018-02-14 10:24:39 +00:00
|
|
|
///
|
2020-02-24 15:44:02 +00:00
|
|
|
/// By default, clients should start playback at this point when beginning a
|
|
|
|
/// playback session.
|
2020-03-25 15:13:40 +00:00
|
|
|
#[derive(ShortHand, PartialOrd, Debug, Clone, Copy, PartialEq, Eq, Ord, Hash)]
|
2020-02-10 12:13:41 +00:00
|
|
|
#[shorthand(enable(must_use))]
|
2018-02-14 10:24:39 +00:00
|
|
|
pub struct ExtXStart {
|
2020-02-24 15:44:02 +00:00
|
|
|
/// The time offset of the [`MediaSegment`]s in the playlist.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use hls_m3u8::tags::ExtXStart;
|
|
|
|
/// use hls_m3u8::types::Float;
|
|
|
|
///
|
|
|
|
/// let mut start = ExtXStart::new(Float::new(20.123456));
|
|
|
|
/// # assert_eq!(start.time_offset(), Float::new(20.123456));
|
|
|
|
///
|
|
|
|
/// start.set_time_offset(Float::new(1.0));
|
|
|
|
/// assert_eq!(start.time_offset(), Float::new(1.0));
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [`MediaSegment`]: crate::MediaSegment
|
|
|
|
#[shorthand(enable(copy))]
|
2020-02-10 12:13:41 +00:00
|
|
|
time_offset: Float,
|
2020-02-24 15:44:02 +00:00
|
|
|
/// Whether clients should not render media stream whose presentation times
|
|
|
|
/// are prior to the specified time offset.
|
2020-02-10 12:13:41 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use hls_m3u8::tags::ExtXStart;
|
2020-02-24 15:44:02 +00:00
|
|
|
/// use hls_m3u8::types::Float;
|
|
|
|
///
|
|
|
|
/// let mut start = ExtXStart::new(Float::new(20.123456));
|
2020-02-10 12:13:41 +00:00
|
|
|
/// # assert_eq!(start.is_precise(), false);
|
|
|
|
/// start.set_is_precise(true);
|
|
|
|
///
|
|
|
|
/// assert_eq!(start.is_precise(), true);
|
|
|
|
/// ```
|
|
|
|
is_precise: bool,
|
2018-02-14 10:24:39 +00:00
|
|
|
}
|
2019-09-06 10:55:00 +00:00
|
|
|
|
2018-02-14 10:24:39 +00:00
|
|
|
impl ExtXStart {
|
|
|
|
pub(crate) const PREFIX: &'static str = "#EXT-X-START:";
|
|
|
|
|
2019-10-03 14:23:27 +00:00
|
|
|
/// Makes a new [`ExtXStart`] tag.
|
2019-09-22 08:57:28 +00:00
|
|
|
///
|
2019-09-22 18:33:40 +00:00
|
|
|
/// # Example
|
2020-02-02 12:38:11 +00:00
|
|
|
///
|
2019-09-22 18:33:40 +00:00
|
|
|
/// ```
|
|
|
|
/// # use hls_m3u8::tags::ExtXStart;
|
2020-02-24 15:44:02 +00:00
|
|
|
/// use hls_m3u8::types::Float;
|
2020-02-10 12:13:41 +00:00
|
|
|
///
|
2020-02-24 15:44:02 +00:00
|
|
|
/// let start = ExtXStart::new(Float::new(20.123456));
|
|
|
|
/// ```
|
|
|
|
#[must_use]
|
|
|
|
pub const fn new(time_offset: Float) -> Self {
|
2019-09-22 18:33:40 +00:00
|
|
|
Self {
|
2020-02-24 15:44:02 +00:00
|
|
|
time_offset,
|
2020-02-10 12:13:41 +00:00
|
|
|
is_precise: false,
|
2018-02-14 10:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 14:23:27 +00:00
|
|
|
/// Makes a new [`ExtXStart`] tag with the given `precise` flag.
|
2019-09-22 08:57:28 +00:00
|
|
|
///
|
2019-09-22 18:33:40 +00:00
|
|
|
/// # Example
|
2020-02-02 12:38:11 +00:00
|
|
|
///
|
2019-09-22 18:33:40 +00:00
|
|
|
/// ```
|
|
|
|
/// # use hls_m3u8::tags::ExtXStart;
|
2020-02-24 15:44:02 +00:00
|
|
|
/// use hls_m3u8::types::Float;
|
|
|
|
///
|
|
|
|
/// let start = ExtXStart::with_precise(Float::new(20.123456), true);
|
2020-02-10 12:13:41 +00:00
|
|
|
/// assert_eq!(start.is_precise(), true);
|
2019-09-22 18:33:40 +00:00
|
|
|
/// ```
|
2020-02-24 15:44:02 +00:00
|
|
|
#[must_use]
|
|
|
|
pub const fn with_precise(time_offset: Float, is_precise: bool) -> Self {
|
2019-09-22 18:33:40 +00:00
|
|
|
Self {
|
2020-02-24 15:44:02 +00:00
|
|
|
time_offset,
|
2020-02-10 12:13:41 +00:00
|
|
|
is_precise,
|
2018-02-14 10:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-22 08:57:28 +00:00
|
|
|
}
|
2018-02-14 10:24:39 +00:00
|
|
|
|
2020-02-02 12:38:11 +00:00
|
|
|
/// This tag requires [`ProtocolVersion::V1`].
|
2019-09-22 08:57:28 +00:00
|
|
|
impl RequiredVersion for ExtXStart {
|
2019-10-03 15:01:15 +00:00
|
|
|
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 }
|
2018-02-14 10:24:39 +00:00
|
|
|
}
|
2019-09-06 10:55:00 +00:00
|
|
|
|
2018-02-14 10:24:39 +00:00
|
|
|
impl fmt::Display for ExtXStart {
|
2020-04-09 06:43:13 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-02-14 10:24:39 +00:00
|
|
|
write!(f, "{}", Self::PREFIX)?;
|
|
|
|
write!(f, "TIME-OFFSET={}", self.time_offset)?;
|
2020-02-10 12:13:41 +00:00
|
|
|
|
|
|
|
if self.is_precise {
|
2018-02-14 14:11:25 +00:00
|
|
|
write!(f, ",PRECISE=YES")?;
|
2018-02-14 10:24:39 +00:00
|
|
|
}
|
2020-02-10 12:13:41 +00:00
|
|
|
|
2018-02-14 10:24:39 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 10:55:00 +00:00
|
|
|
|
2020-04-22 07:54:48 +00:00
|
|
|
impl TryFrom<&str> for ExtXStart {
|
|
|
|
type Error = Error;
|
2019-09-08 10:23:33 +00:00
|
|
|
|
2020-04-22 07:54:48 +00:00
|
|
|
fn try_from(input: &str) -> Result<Self, Self::Error> {
|
2019-09-13 14:06:52 +00:00
|
|
|
let input = tag(input, Self::PREFIX)?;
|
2018-02-14 10:24:39 +00:00
|
|
|
|
|
|
|
let mut time_offset = None;
|
2020-02-10 12:13:41 +00:00
|
|
|
let mut is_precise = false;
|
2019-09-13 14:06:52 +00:00
|
|
|
|
2020-01-26 12:11:57 +00:00
|
|
|
for (key, value) in AttributePairs::new(input) {
|
|
|
|
match key {
|
2020-02-10 12:13:41 +00:00
|
|
|
"TIME-OFFSET" => time_offset = Some(value.parse()?),
|
|
|
|
"PRECISE" => is_precise = parse_yes_or_no(value)?,
|
2018-02-14 10:24:39 +00:00
|
|
|
_ => {
|
|
|
|
// [6.3.1. General Client Responsibilities]
|
2019-10-03 15:01:15 +00:00
|
|
|
// > ignore any attribute/value pair with an unrecognized
|
|
|
|
// AttributeName.
|
2018-02-14 10:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 12:13:41 +00:00
|
|
|
let time_offset = time_offset.ok_or_else(|| Error::missing_value("TIME-OFFSET"))?;
|
2019-09-13 14:06:52 +00:00
|
|
|
|
2019-09-22 18:33:40 +00:00
|
|
|
Ok(Self {
|
2018-02-14 10:24:39 +00:00
|
|
|
time_offset,
|
2020-02-10 12:13:41 +00:00
|
|
|
is_precise,
|
2018-02-14 10:24:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2019-10-08 13:42:33 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2018-02-14 10:24:39 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-09-22 08:57:28 +00:00
|
|
|
fn test_display() {
|
|
|
|
assert_eq!(
|
2020-02-24 15:44:02 +00:00
|
|
|
ExtXStart::new(Float::new(-1.23)).to_string(),
|
2019-09-22 08:57:28 +00:00
|
|
|
"#EXT-X-START:TIME-OFFSET=-1.23".to_string(),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-02-24 15:44:02 +00:00
|
|
|
ExtXStart::with_precise(Float::new(1.23), true).to_string(),
|
2019-09-22 08:57:28 +00:00
|
|
|
"#EXT-X-START:TIME-OFFSET=1.23,PRECISE=YES".to_string(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_required_version() {
|
|
|
|
assert_eq!(
|
2020-02-24 15:44:02 +00:00
|
|
|
ExtXStart::new(Float::new(-1.23)).required_version(),
|
2019-09-22 08:57:28 +00:00
|
|
|
ProtocolVersion::V1,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-02-24 15:44:02 +00:00
|
|
|
ExtXStart::with_precise(Float::new(1.23), true).required_version(),
|
2019-09-22 08:57:28 +00:00
|
|
|
ProtocolVersion::V1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parser() {
|
|
|
|
assert_eq!(
|
2020-02-24 15:44:02 +00:00
|
|
|
ExtXStart::new(Float::new(-1.23)),
|
2020-04-22 07:54:48 +00:00
|
|
|
ExtXStart::try_from("#EXT-X-START:TIME-OFFSET=-1.23").unwrap(),
|
2019-09-22 08:57:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-02-24 15:44:02 +00:00
|
|
|
ExtXStart::with_precise(Float::new(1.23), true),
|
2020-04-22 07:54:48 +00:00
|
|
|
ExtXStart::try_from("#EXT-X-START:TIME-OFFSET=1.23,PRECISE=YES").unwrap(),
|
2019-09-22 08:57:28 +00:00
|
|
|
);
|
2019-09-22 18:33:40 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-02-24 15:44:02 +00:00
|
|
|
ExtXStart::with_precise(Float::new(1.23), true),
|
2020-04-22 07:54:48 +00:00
|
|
|
ExtXStart::try_from("#EXT-X-START:TIME-OFFSET=1.23,PRECISE=YES,UNKNOWN=TAG").unwrap(),
|
2019-09-22 18:33:40 +00:00
|
|
|
);
|
2018-02-14 10:24:39 +00:00
|
|
|
}
|
|
|
|
}
|