From 9eccea8a7fa16f8a2115aa889db67edeab3ad4ed Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Sun, 29 Mar 2020 12:58:32 +0200 Subject: [PATCH] automatically infer start of ByteRange --- src/media_playlist.rs | 23 +++++++++++++++++++++-- tests/media_playlist.rs | 3 ++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/media_playlist.rs b/src/media_playlist.rs index 9fb4aad..892e398 100644 --- a/src/media_playlist.rs +++ b/src/media_playlist.rs @@ -8,8 +8,9 @@ use derive_builder::Builder; use crate::line::{Line, Lines, Tag}; use crate::media_segment::MediaSegment; use crate::tags::{ - ExtM3u, ExtXDiscontinuitySequence, ExtXEndList, ExtXIFramesOnly, ExtXIndependentSegments, - ExtXKey, ExtXMediaSequence, ExtXStart, ExtXTargetDuration, ExtXVersion, + ExtM3u, ExtXByteRange, ExtXDiscontinuitySequence, ExtXEndList, ExtXIFramesOnly, + ExtXIndependentSegments, ExtXKey, ExtXMediaSequence, ExtXStart, ExtXTargetDuration, + ExtXVersion, }; use crate::types::{ DecryptionKey, EncryptionMethod, InitializationVector, KeyFormat, PlaylistType, ProtocolVersion, @@ -295,6 +296,8 @@ impl MediaPlaylistBuilder { } let mut position = sequence_number; + let mut previous_range: Option = None; + for segment in segments .iter() .filter_map(|(_, s)| if s.explicit_number { None } else { Some(s) }) @@ -322,6 +325,22 @@ impl MediaPlaylistBuilder { } } + // add the lower bound to the byterange automatically + if let Some(range) = &mut segment.byte_range { + if range.start().is_none() { + if let Some(previous_range) = previous_range { + // the end of the previous_range is the start of the next range + *range = range.saturating_add(previous_range.end()); + range.set_start(Some(previous_range.end())); + } else { + // assume that the byte range starts at zero + range.set_start(Some(0)); + } + } + + previous_range = segment.byte_range; + } + result_segments.insert(segment.number, segment); position += 1; } diff --git a/tests/media_playlist.rs b/tests/media_playlist.rs index 7477910..b1fb799 100644 --- a/tests/media_playlist.rs +++ b/tests/media_playlist.rs @@ -43,6 +43,7 @@ generate_tests! { .unwrap(), MediaSegment::builder() .duration(ExtInf::new(Duration::from_secs_f64(10.0))) + // 834433..904297 .byte_range(ExtXByteRange::from(..69864)) .uri("video.ts") .build() @@ -64,7 +65,7 @@ generate_tests! { "#EXTINF:10,\n", "video.ts\n", - "#EXT-X-BYTERANGE:69864\n", + "#EXT-X-BYTERANGE:69864@834433\n", "#EXTINF:10,\n", "video.ts\n" )