mirror of
https://github.com/rutgersc/m3u8-rs.git
synced 2024-06-02 05:52:41 +00:00
Support setting of float precision for EXT-INF using a pub static Atomic instead
This commit is contained in:
parent
d0357096dd
commit
53e34a6bb8
|
@ -6,11 +6,16 @@
|
|||
use crate::QuotedOrUnquoted;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::f32;
|
||||
use std::fmt;
|
||||
use std::fmt::Display;
|
||||
use std::io::Write;
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::usize::MAX;
|
||||
use std::{f32, usize};
|
||||
|
||||
/// The output precision for floats, such as #EXTINF (default is unset)
|
||||
pub static WRITE_OPT_FLOAT_PRECISION: AtomicUsize = AtomicUsize::new(MAX);
|
||||
|
||||
macro_rules! write_some_attribute_quoted {
|
||||
($w:expr, $tag:expr, $o:expr) => {
|
||||
|
@ -842,13 +847,9 @@ 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()
|
||||
}
|
||||
|
@ -888,7 +889,14 @@ impl MediaSegment {
|
|||
writeln!(w, "{}", unknown_tag)?;
|
||||
}
|
||||
|
||||
write!(w, "#EXTINF:{:.*},", self.float_precision, self.duration)?;
|
||||
match WRITE_OPT_FLOAT_PRECISION.load(Ordering::Relaxed) {
|
||||
MAX => {
|
||||
write!(w, "#EXTINF:{},", self.duration)?;
|
||||
}
|
||||
n => {
|
||||
write!(w, "#EXTINF:{:.*},", n, self.duration)?;
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(ref v) = self.title {
|
||||
writeln!(w, "{}", v)?;
|
||||
|
@ -907,13 +915,13 @@ impl Default for MediaSegment {
|
|||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
16
tests/lib.rs
16
tests/lib.rs
|
@ -8,6 +8,7 @@ use std::collections::HashMap;
|
|||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::{fs, io};
|
||||
|
||||
fn all_sample_m3u_playlists() -> Vec<path::PathBuf> {
|
||||
|
@ -200,7 +201,7 @@ fn create_and_parse_master_playlist_empty() {
|
|||
|
||||
#[test]
|
||||
fn create_segment_float_inf() {
|
||||
let mut playlist = MediaPlaylist {
|
||||
let playlist = MediaPlaylist {
|
||||
version: Some(6),
|
||||
target_duration: 3.0,
|
||||
media_sequence: 338559,
|
||||
|
@ -218,15 +219,14 @@ fn create_segment_float_inf() {
|
|||
|
||||
let mut v: Vec<u8> = Vec::new();
|
||||
playlist.write_to(&mut v).unwrap();
|
||||
|
||||
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"));
|
||||
|
||||
WRITE_OPT_FLOAT_PRECISION.store(5, Ordering::Relaxed);
|
||||
|
||||
playlist.write_to(&mut v).unwrap();
|
||||
let m3u8_str: &str = std::str::from_utf8(&v).unwrap();
|
||||
assert!(m3u8_str.contains("#EXTINF:2.00000,title"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue