1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-09 16:59:34 +00:00
hls_m3u8/src/tags/shared/start.rs

195 lines
5.1 KiB
Rust
Raw Normal View History

use std::fmt;
use std::str::FromStr;
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.
///
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))]
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,
}
2019-09-06 10:55:00 +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,
}
}
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,
}
}
2019-09-22 08:57:28 +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 }
}
2019-09-06 10:55:00 +00:00
impl fmt::Display for ExtXStart {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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")?;
}
2020-02-10 12:13:41 +00:00
Ok(())
}
}
2019-09-06 10:55:00 +00:00
impl FromStr for ExtXStart {
type Err = Error;
2019-09-08 10:23:33 +00:00
2019-09-13 14:06:52 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
let input = tag(input, Self::PREFIX)?;
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
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)?,
_ => {
// [6.3.1. General Client Responsibilities]
2019-10-03 15:01:15 +00:00
// > ignore any attribute/value pair with an unrecognized
// AttributeName.
}
}
}
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 {
time_offset,
2020-02-10 12:13:41 +00:00
is_precise,
})
}
}
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
#[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)),
2019-09-22 08:57:28 +00:00
"#EXT-X-START:TIME-OFFSET=-1.23".parse().unwrap(),
);
assert_eq!(
2020-02-24 15:44:02 +00:00
ExtXStart::with_precise(Float::new(1.23), true),
2019-09-22 08:57:28 +00:00
"#EXT-X-START:TIME-OFFSET=1.23,PRECISE=YES".parse().unwrap(),
);
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),
2019-09-22 18:33:40 +00:00
"#EXT-X-START:TIME-OFFSET=1.23,PRECISE=YES,UNKNOWN=TAG"
.parse()
.unwrap(),
);
}
}