1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2025-01-08 19:25:24 +00:00

internalize ExtXTargetDuration

This commit is contained in:
Luro02 2020-03-25 13:37:47 +01:00
parent f48876ee07
commit 429f3f8c3d
No known key found for this signature in database
GPG key ID: B66FD4F74501A9CF
5 changed files with 24 additions and 75 deletions

View file

@ -1,13 +1,11 @@
mod discontinuity_sequence;
mod end_list;
mod i_frames_only;
mod media_sequence;
mod playlist_type;
mod target_duration;
pub(crate) mod discontinuity_sequence;
pub(crate) mod end_list;
pub(crate) mod i_frames_only;
pub(crate) mod media_sequence;
pub(crate) mod target_duration;
pub(crate) use discontinuity_sequence::*;
pub(crate) use end_list::*;
pub(crate) use i_frames_only::*;
pub use playlist_type::*;
pub(crate) use media_sequence::*;
pub use target_duration::*;
pub(crate) use target_duration::*;

View file

@ -2,57 +2,16 @@ use std::fmt;
use std::str::FromStr;
use std::time::Duration;
use derive_more::Deref;
use crate::types::ProtocolVersion;
use crate::utils::tag;
use crate::{Error, RequiredVersion};
/// # [4.3.3.1. EXT-X-TARGETDURATION]
///
/// The [`ExtXTargetDuration`] tag specifies the maximum [`MediaSegment`]
/// duration.
///
/// [`MediaSegment`]: crate::MediaSegment
/// [4.3.3.1. EXT-X-TARGETDURATION]:
/// https://tools.ietf.org/html/rfc8216#section-4.3.3.1
#[derive(Deref, Debug, Clone, Copy, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
pub struct ExtXTargetDuration(Duration);
/// Specifies the maximum `MediaSegment` duration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
pub(crate) struct ExtXTargetDuration(pub Duration);
impl ExtXTargetDuration {
pub(crate) const PREFIX: &'static str = "#EXT-X-TARGETDURATION:";
/// Makes a new [`ExtXTargetDuration`] tag.
///
/// # Example
///
/// ```
/// # use hls_m3u8::tags::ExtXTargetDuration;
/// use std::time::Duration;
///
/// let target_duration = ExtXTargetDuration::new(Duration::from_secs(20));
/// ```
///
/// # Note
///
/// The nanoseconds part of the [`Duration`] will be discarded.
#[must_use]
pub const fn new(duration: Duration) -> Self { Self(Duration::from_secs(duration.as_secs())) }
/// Returns the maximum media segment duration.
///
/// # Example
///
/// ```
/// # use hls_m3u8::tags::ExtXTargetDuration;
/// use std::time::Duration;
///
/// let target_duration = ExtXTargetDuration::new(Duration::from_nanos(2_000_000_000));
///
/// assert_eq!(target_duration.duration(), Duration::from_secs(2));
/// ```
#[must_use]
pub const fn duration(&self) -> Duration { self.0 }
}
/// This tag requires [`ProtocolVersion::V1`].
@ -70,17 +29,14 @@ impl FromStr for ExtXTargetDuration {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let input = tag(input, Self::PREFIX)?;
let input = input.parse().map_err(|e| Error::parse_int(input, e))?;
let input = tag(input, Self::PREFIX)?
.parse()
.map_err(|e| Error::parse_int(input, e))?;
Ok(Self::new(Duration::from_secs(input)))
Ok(Self(Duration::from_secs(input)))
}
}
impl From<Duration> for ExtXTargetDuration {
fn from(value: Duration) -> Self { Self::new(value) }
}
#[cfg(test)]
mod test {
use super::*;
@ -89,7 +45,7 @@ mod test {
#[test]
fn test_display() {
assert_eq!(
ExtXTargetDuration::new(Duration::from_secs(5)).to_string(),
ExtXTargetDuration(Duration::from_secs(5)).to_string(),
"#EXT-X-TARGETDURATION:5".to_string()
);
}
@ -97,7 +53,7 @@ mod test {
#[test]
fn test_required_version() {
assert_eq!(
ExtXTargetDuration::new(Duration::from_secs(5)).required_version(),
ExtXTargetDuration(Duration::from_secs(5)).required_version(),
ProtocolVersion::V1
);
}
@ -105,13 +61,8 @@ mod test {
#[test]
fn test_parser() {
assert_eq!(
ExtXTargetDuration::new(Duration::from_secs(5)),
ExtXTargetDuration(Duration::from_secs(5)),
"#EXT-X-TARGETDURATION:5".parse().unwrap()
);
}
#[test]
fn test_deref() {
assert_eq!(ExtXTargetDuration::new(Duration::from_secs(5)).as_secs(), 5);
}
}

View file

@ -10,6 +10,6 @@ mod shared;
pub use basic::*;
pub use master_playlist::*;
pub use media_playlist::*;
pub(crate) use media_playlist::*;
pub use media_segment::*;
pub use shared::*;

View file

@ -5,7 +5,7 @@
use std::time::Duration;
use hls_m3u8::tags::{ExtInf, ExtXByteRange, ExtXTargetDuration};
use hls_m3u8::tags::{ExtInf, ExtXByteRange};
use hls_m3u8::types::PlaylistType;
use hls_m3u8::{MediaPlaylist, MediaSegment};
use pretty_assertions::assert_eq;
@ -27,7 +27,7 @@ generate_tests! {
test_media_playlist_with_byterange => {
MediaPlaylist::builder()
.media_sequence(1)
.target_duration(ExtXTargetDuration::new(Duration::from_secs(10)))
.target_duration(Duration::from_secs(10))
.segments(vec![
MediaSegment::builder()
.inf(ExtInf::new(Duration::from_secs_f64(10.0)))
@ -72,7 +72,7 @@ generate_tests! {
test_absolute_uris => {
MediaPlaylist::builder()
.playlist_type(PlaylistType::Vod)
.target_duration(ExtXTargetDuration::new(Duration::from_secs(10)))
.target_duration(Duration::from_secs(10))
.segments(vec![
MediaSegment::builder()
.inf(ExtInf::new(Duration::from_secs(10)))

View file

@ -1,7 +1,7 @@
// https://tools.ietf.org/html/rfc8216#section-8
use std::time::Duration;
use hls_m3u8::tags::{ExtInf, ExtXKey, ExtXMedia, ExtXTargetDuration, VariantStream};
use hls_m3u8::tags::{ExtInf, ExtXKey, ExtXMedia, VariantStream};
use hls_m3u8::types::{DecryptionKey, EncryptionMethod, MediaType, StreamData};
use hls_m3u8::{MasterPlaylist, MediaPlaylist, MediaSegment};
use pretty_assertions::assert_eq;
@ -22,7 +22,7 @@ macro_rules! generate_tests {
generate_tests! {
test_simple_playlist => {
MediaPlaylist::builder()
.target_duration(ExtXTargetDuration::new(Duration::from_secs(10)))
.target_duration(Duration::from_secs(10))
.segments(vec![
MediaSegment::builder()
.inf(ExtInf::new(Duration::from_secs_f64(9.009)))
@ -58,7 +58,7 @@ generate_tests! {
},
test_live_media_playlist_using_https => {
MediaPlaylist::builder()
.target_duration(ExtXTargetDuration::new(Duration::from_secs(8)))
.target_duration(Duration::from_secs(8))
.media_sequence(2680)
.segments(vec![
MediaSegment::builder()
@ -94,7 +94,7 @@ generate_tests! {
},
test_media_playlist_with_encrypted_segments => {
MediaPlaylist::builder()
.target_duration(ExtXTargetDuration::new(Duration::from_secs(15)))
.target_duration(Duration::from_secs(15))
.media_sequence(7794)
.segments(vec![
MediaSegment::builder()