Compare commits

...

2 commits

Author SHA1 Message Date
Anton Eicher ebf40a71ea
Merge d0357096dd into 487d63da4d 2024-01-30 10:44:19 +00:00
Anton Eicher d0357096dd Support setting of float precision for EXT-INF, defaulting to 5 decimal places 2024-01-30 12:44:05 +02:00
2 changed files with 32 additions and 3 deletions

View file

@ -821,7 +821,7 @@ impl Default for MediaPlaylistType {
/// A [Media Segment](https://tools.ietf.org/html/draft-pantos-http-live-streaming-19#section-3)
/// is specified by a URI and optionally a byte range.
#[derive(Debug, Default, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone)]
pub struct MediaSegment {
pub uri: String,
/// `#EXTINF:<duration>,[<title>]`
@ -842,9 +842,13 @@ pub struct MediaSegment {
pub daterange: Option<DateRange>,
/// `#EXT-`
pub unknown_tags: Vec<ExtTag>,
/// Precision of floating-point values, such as `#EXTINF:<duration>`
pub float_precision: usize,
}
impl MediaSegment {
pub const DEFAULT_FLOAT_PRECISION: usize = 5;
pub fn empty() -> MediaSegment {
Default::default()
}
@ -884,7 +888,7 @@ impl MediaSegment {
writeln!(w, "{}", unknown_tag)?;
}
write!(w, "#EXTINF:{:.5},", self.duration)?;
write!(w, "#EXTINF:{:.*},", self.float_precision, self.duration)?;
if let Some(ref v) = self.title {
writeln!(w, "{}", v)?;
@ -896,6 +900,24 @@ impl MediaSegment {
}
}
impl Default for MediaSegment {
fn default() -> Self {
Self {
uri: Default::default(),
duration: Default::default(),
title: Default::default(),
byte_range: Default::default(),
discontinuity: Default::default(),
key: Default::default(),
map: Default::default(),
program_date_time: Default::default(),
daterange: Default::default(),
unknown_tags: Default::default(),
float_precision: MediaSegment::DEFAULT_FLOAT_PRECISION,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub enum KeyMethod {
None,

View file

@ -200,7 +200,7 @@ fn create_and_parse_master_playlist_empty() {
#[test]
fn create_segment_float_inf() {
let playlist = MediaPlaylist {
let mut playlist = MediaPlaylist {
version: Some(6),
target_duration: 3.0,
media_sequence: 338559,
@ -221,6 +221,12 @@ fn create_segment_float_inf() {
let m3u8_str: &str = std::str::from_utf8(&v).unwrap();
assert!(m3u8_str.contains("#EXTINF:2.00000,title"));
playlist.segments[0].float_precision = 0;
playlist.write_to(&mut v).unwrap();
let m3u8_str: &str = std::str::from_utf8(&v).unwrap();
assert!(m3u8_str.contains("#EXTINF:2,title"));
}
#[test]
@ -407,6 +413,7 @@ fn create_and_parse_media_playlist_full() {
tag: "X-CUE-OUT".into(),
rest: Some("DURATION=2.002".into()),
}],
..Default::default()
}],
unknown_tags: vec![],
});