1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-06 23:49:21 +00:00
hls_m3u8/src/tags/media_playlist/target_duration.rs

92 lines
2.3 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
use std::str::FromStr;
use std::time::Duration;
2019-09-13 14:06:52 +00:00
2019-09-22 08:57:28 +00:00
use crate::types::{ProtocolVersion, RequiredVersion};
2019-09-13 14:06:52 +00:00
use crate::utils::tag;
use crate::Error;
2019-09-06 10:55:00 +00:00
2019-09-22 16:00:38 +00:00
/// # [4.4.3.1. EXT-X-TARGETDURATION]
/// The [ExtXTargetDuration] tag specifies the maximum [Media Segment]
/// duration.
2019-09-06 10:55:00 +00:00
///
2019-09-22 16:00:38 +00:00
/// Its format is:
/// ```text
/// #EXT-X-TARGETDURATION:<s>
/// ```
/// where `s` is the target [Duration] in seconds.
///
/// [Media Segment]: crate::MediaSegment
/// [4.4.3.1. EXT-X-TARGETDURATION]:
/// https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-04#section-4.4.3.1
2019-09-14 19:08:35 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
2019-09-22 08:57:28 +00:00
pub struct ExtXTargetDuration(Duration);
2019-09-06 10:55:00 +00:00
impl ExtXTargetDuration {
pub(crate) const PREFIX: &'static str = "#EXT-X-TARGETDURATION:";
2019-09-22 16:00:38 +00:00
/// Makes a new [ExtXTargetduration] tag.
2019-09-06 10:55:00 +00:00
///
2019-09-22 16:00:38 +00:00
/// # Note
/// The nanoseconds part of the [Duration] will be discarded.
2019-09-08 10:23:33 +00:00
pub const fn new(duration: Duration) -> Self {
2019-09-22 16:00:38 +00:00
// TOOD: round instead of discarding?
2019-09-22 08:57:28 +00:00
Self(Duration::from_secs(duration.as_secs()))
2019-09-06 10:55:00 +00:00
}
/// Returns the maximum media segment duration in the associated playlist.
2019-09-08 10:23:33 +00:00
pub const fn duration(&self) -> Duration {
2019-09-22 08:57:28 +00:00
self.0
2019-09-06 10:55:00 +00:00
}
2019-09-22 08:57:28 +00:00
}
2019-09-06 10:55:00 +00:00
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXTargetDuration {
fn required_version(&self) -> ProtocolVersion {
2019-09-06 10:55:00 +00:00
ProtocolVersion::V1
}
}
impl fmt::Display for ExtXTargetDuration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-09-22 08:57:28 +00:00
write!(f, "{}{}", Self::PREFIX, self.0.as_secs())
2019-09-06 10:55:00 +00:00
}
}
impl FromStr for ExtXTargetDuration {
type Err = Error;
2019-09-13 14:06:52 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
let input = tag(input, Self::PREFIX)?.parse()?;
2019-09-22 08:57:28 +00:00
Ok(Self::new(Duration::from_secs(input)))
2019-09-06 10:55:00 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
2019-09-22 08:57:28 +00:00
fn test_display() {
assert_eq!(
ExtXTargetDuration::new(Duration::from_secs(5)).to_string(),
"#EXT-X-TARGETDURATION:5".to_string()
);
}
#[test]
fn test_required_version() {
assert_eq!(
ExtXTargetDuration::new(Duration::from_secs(5)).required_version(),
ProtocolVersion::V1
);
}
#[test]
fn test_parser() {
assert_eq!(
ExtXTargetDuration::new(Duration::from_secs(5)),
"#EXT-X-TARGETDURATION:5".parse().unwrap()
);
2019-09-06 10:55:00 +00:00
}
}