mirror of
https://github.com/rutgersc/m3u8-rs.git
synced 2024-06-02 06:11:01 +00:00
Support setting of float precision for EXT-INF, defaulting to 5 decimal places
This commit is contained in:
parent
25741d56e6
commit
d0357096dd
|
@ -821,7 +821,7 @@ impl Default for MediaPlaylistType {
|
||||||
|
|
||||||
/// A [Media Segment](https://tools.ietf.org/html/draft-pantos-http-live-streaming-19#section-3)
|
/// 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.
|
/// is specified by a URI and optionally a byte range.
|
||||||
#[derive(Debug, Default, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct MediaSegment {
|
pub struct MediaSegment {
|
||||||
pub uri: String,
|
pub uri: String,
|
||||||
/// `#EXTINF:<duration>,[<title>]`
|
/// `#EXTINF:<duration>,[<title>]`
|
||||||
|
@ -842,9 +842,13 @@ pub struct MediaSegment {
|
||||||
pub daterange: Option<DateRange>,
|
pub daterange: Option<DateRange>,
|
||||||
/// `#EXT-`
|
/// `#EXT-`
|
||||||
pub unknown_tags: Vec<ExtTag>,
|
pub unknown_tags: Vec<ExtTag>,
|
||||||
|
/// Precision of floating-point values, such as `#EXTINF:<duration>`
|
||||||
|
pub float_precision: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MediaSegment {
|
impl MediaSegment {
|
||||||
|
pub const DEFAULT_FLOAT_PRECISION: usize = 5;
|
||||||
|
|
||||||
pub fn empty() -> MediaSegment {
|
pub fn empty() -> MediaSegment {
|
||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
|
@ -884,7 +888,7 @@ impl MediaSegment {
|
||||||
writeln!(w, "{}", unknown_tag)?;
|
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 {
|
if let Some(ref v) = self.title {
|
||||||
writeln!(w, "{}", v)?;
|
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)]
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
|
||||||
pub enum KeyMethod {
|
pub enum KeyMethod {
|
||||||
None,
|
None,
|
||||||
|
|
|
@ -200,7 +200,7 @@ fn create_and_parse_master_playlist_empty() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_segment_float_inf() {
|
fn create_segment_float_inf() {
|
||||||
let playlist = MediaPlaylist {
|
let mut playlist = MediaPlaylist {
|
||||||
version: Some(6),
|
version: Some(6),
|
||||||
target_duration: 3.0,
|
target_duration: 3.0,
|
||||||
media_sequence: 338559,
|
media_sequence: 338559,
|
||||||
|
@ -221,6 +221,12 @@ fn create_segment_float_inf() {
|
||||||
|
|
||||||
let m3u8_str: &str = std::str::from_utf8(&v).unwrap();
|
let m3u8_str: &str = std::str::from_utf8(&v).unwrap();
|
||||||
assert!(m3u8_str.contains("#EXTINF:2.00000,title"));
|
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]
|
#[test]
|
||||||
|
@ -407,6 +413,7 @@ fn create_and_parse_media_playlist_full() {
|
||||||
tag: "X-CUE-OUT".into(),
|
tag: "X-CUE-OUT".into(),
|
||||||
rest: Some("DURATION=2.002".into()),
|
rest: Some("DURATION=2.002".into()),
|
||||||
}],
|
}],
|
||||||
|
..Default::default()
|
||||||
}],
|
}],
|
||||||
unknown_tags: vec![],
|
unknown_tags: vec![],
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue