1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-12-23 04:26:27 +00:00

internalize ExtMediaSequence

This commit is contained in:
Luro02 2020-03-25 13:08:26 +01:00
parent c56a56abe8
commit 99b6b23acc
No known key found for this signature in database
GPG key ID: B66FD4F74501A9CF
4 changed files with 17 additions and 74 deletions

View file

@ -5,60 +5,13 @@ use crate::types::ProtocolVersion;
use crate::utils::tag;
use crate::{Error, RequiredVersion};
/// # [4.4.3.2. EXT-X-MEDIA-SEQUENCE]
///
/// The [`ExtXMediaSequence`] tag indicates the Media Sequence Number of
/// the first [`Media Segment`] that appears in a Playlist file.
///
/// [Media Segment]: crate::MediaSegment
/// [4.4.3.2. EXT-X-MEDIA-SEQUENCE]:
/// https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-04#section-4.4.3.2
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ExtXMediaSequence(u64);
/// Indicates the Media Sequence Number of the first `MediaSegment` that
/// appears in a `MediaPlaylist`.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct ExtXMediaSequence(pub usize);
impl ExtXMediaSequence {
pub(crate) const PREFIX: &'static str = "#EXT-X-MEDIA-SEQUENCE:";
/// Makes a new [`ExtXMediaSequence`] tag.
///
/// # Example
///
/// ```
/// # use hls_m3u8::tags::ExtXMediaSequence;
/// let media_sequence = ExtXMediaSequence::new(5);
/// ```
#[must_use]
pub const fn new(seq_num: u64) -> Self { Self(seq_num) }
/// Returns the sequence number of the first media segment,
/// that appears in the associated playlist.
///
/// # Example
///
/// ```
/// # use hls_m3u8::tags::ExtXMediaSequence;
/// let media_sequence = ExtXMediaSequence::new(5);
///
/// assert_eq!(media_sequence.seq_num(), 5);
/// ```
#[must_use]
pub const fn seq_num(self) -> u64 { self.0 }
/// Sets the sequence number.
///
/// # Example
///
/// ```
/// # use hls_m3u8::tags::ExtXMediaSequence;
/// let mut media_sequence = ExtXMediaSequence::new(5);
///
/// media_sequence.set_seq_num(10);
/// assert_eq!(media_sequence.seq_num(), 10);
/// ```
pub fn set_seq_num(&mut self, value: u64) -> &mut Self {
self.0 = value;
self
}
}
/// This tag requires [`ProtocolVersion::V1`].
@ -80,7 +33,7 @@ impl FromStr for ExtXMediaSequence {
let input = tag(input, Self::PREFIX)?;
let seq_num = input.parse().map_err(|e| Error::parse_int(input, e))?;
Ok(Self::new(seq_num))
Ok(Self(seq_num))
}
}
@ -92,7 +45,7 @@ mod test {
#[test]
fn test_display() {
assert_eq!(
ExtXMediaSequence::new(123).to_string(),
ExtXMediaSequence(123).to_string(),
"#EXT-X-MEDIA-SEQUENCE:123".to_string()
);
}
@ -100,7 +53,7 @@ mod test {
#[test]
fn test_required_version() {
assert_eq!(
ExtXMediaSequence::new(123).required_version(),
ExtXMediaSequence(123).required_version(),
ProtocolVersion::V1
);
}
@ -108,16 +61,8 @@ mod test {
#[test]
fn test_parser() {
assert_eq!(
ExtXMediaSequence::new(123),
ExtXMediaSequence(123),
"#EXT-X-MEDIA-SEQUENCE:123".parse().unwrap()
);
}
#[test]
fn test_seq_num() {
let mut sequence = ExtXMediaSequence::new(123);
assert_eq!(sequence.seq_num(), 123);
sequence.set_seq_num(1);
assert_eq!(sequence.seq_num(), 1);
}
}

View file

@ -8,6 +8,6 @@ mod target_duration;
pub use discontinuity_sequence::*;
pub(crate) use end_list::*;
pub(crate) use i_frames_only::*;
pub use media_sequence::*;
pub use playlist_type::*;
pub(crate) use media_sequence::*;
pub use target_duration::*;

View file

@ -5,7 +5,7 @@
use std::time::Duration;
use hls_m3u8::tags::{ExtInf, ExtXByteRange, ExtXMediaSequence, ExtXTargetDuration};
use hls_m3u8::tags::{ExtInf, ExtXByteRange, ExtXTargetDuration};
use hls_m3u8::types::PlaylistType;
use hls_m3u8::{MediaPlaylist, MediaSegment};
use pretty_assertions::assert_eq;
@ -26,8 +26,8 @@ macro_rules! generate_tests {
generate_tests! {
test_media_playlist_with_byterange => {
MediaPlaylist::builder()
.media_sequence(1)
.target_duration(ExtXTargetDuration::new(Duration::from_secs(10)))
.media_sequence(ExtXMediaSequence::new(0))
.segments(vec![
MediaSegment::builder()
.inf(ExtInf::new(Duration::from_secs_f64(10.0)))
@ -54,7 +54,7 @@ generate_tests! {
"#EXTM3U\n",
"#EXT-X-VERSION:4\n",
"#EXT-X-TARGETDURATION:10\n",
"#EXT-X-MEDIA-SEQUENCE:0\n",
"#EXT-X-MEDIA-SEQUENCE:1\n",
"#EXT-X-BYTERANGE:75232@0\n",
"#EXTINF:10,\n",
@ -121,7 +121,7 @@ generate_tests! {
test_allow_cache => {
MediaPlaylist::builder()
.target_duration(Duration::from_secs(10))
.media_sequence(0)
.media_sequence(1)
.playlist_type(PlaylistType::Vod)
.segments(vec![
MediaSegment::builder()
@ -238,7 +238,7 @@ generate_tests! {
"#EXTM3U\n",
"#EXT-X-VERSION:4\n",
"#EXT-X-TARGETDURATION:10\n",
"#EXT-X-MEDIA-SEQUENCE:0\n",
"#EXT-X-MEDIA-SEQUENCE:1\n",
"#EXT-X-PLAYLIST-TYPE:VOD\n",
"#EXT-X-BYTERANGE:522828@0\n",

View file

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