2019-09-10 09:05:20 +00:00
|
|
|
use std::fmt;
|
2019-09-21 13:20:19 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2019-09-10 09:05:20 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2019-09-06 10:55:00 +00:00
|
|
|
use crate::attribute::AttributePairs;
|
2019-09-21 13:20:19 +00:00
|
|
|
use crate::types::{ClosedCaptions, DecimalFloatingPoint, ProtocolVersion, StreamInf};
|
|
|
|
use crate::utils::{quote, tag, unquote};
|
2019-09-13 14:06:52 +00:00
|
|
|
use crate::Error;
|
2019-09-06 10:55:00 +00:00
|
|
|
|
|
|
|
/// [4.3.4.2. EXT-X-STREAM-INF]
|
|
|
|
///
|
|
|
|
/// [4.3.4.2. EXT-X-STREAM-INF]: https://tools.ietf.org/html/rfc8216#section-4.3.4.2
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct ExtXStreamInf {
|
2019-09-21 10:11:36 +00:00
|
|
|
uri: String,
|
2019-09-06 10:55:00 +00:00
|
|
|
frame_rate: Option<DecimalFloatingPoint>,
|
2019-09-08 09:30:52 +00:00
|
|
|
audio: Option<String>,
|
|
|
|
subtitles: Option<String>,
|
2019-09-06 10:55:00 +00:00
|
|
|
closed_captions: Option<ClosedCaptions>,
|
2019-09-21 13:20:19 +00:00
|
|
|
stream_inf: StreamInf,
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ExtXStreamInf {
|
|
|
|
pub(crate) const PREFIX: &'static str = "#EXT-X-STREAM-INF:";
|
|
|
|
|
2019-09-21 13:20:19 +00:00
|
|
|
/// Makes a new [ExtXStreamInf] tag.
|
|
|
|
/// ```
|
|
|
|
/// # use hls_m3u8::tags::ExtXStreamInf;
|
|
|
|
/// #
|
|
|
|
/// let stream = ExtXStreamInf::new("https://www.example.com/", 20);
|
|
|
|
/// ```
|
2019-09-21 10:11:36 +00:00
|
|
|
pub fn new<T: ToString>(uri: T, bandwidth: u64) -> Self {
|
2019-09-06 10:55:00 +00:00
|
|
|
ExtXStreamInf {
|
2019-09-21 10:11:36 +00:00
|
|
|
uri: uri.to_string(),
|
2019-09-06 10:55:00 +00:00
|
|
|
frame_rate: None,
|
|
|
|
audio: None,
|
|
|
|
subtitles: None,
|
|
|
|
closed_captions: None,
|
2019-09-21 13:20:19 +00:00
|
|
|
stream_inf: StreamInf::new(bandwidth),
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the URI that identifies the associated media playlist.
|
2019-09-21 10:11:36 +00:00
|
|
|
pub const fn uri(&self) -> &String {
|
2019-09-06 10:55:00 +00:00
|
|
|
&self.uri
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the maximum frame rate for all the video in the variant stream.
|
2019-09-15 16:54:25 +00:00
|
|
|
pub fn frame_rate(&self) -> Option<f64> {
|
|
|
|
self.frame_rate.map_or(None, |v| Some(v.as_f64()))
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the group identifier for the audio in the variant stream.
|
2019-09-08 09:30:52 +00:00
|
|
|
pub fn audio(&self) -> Option<&String> {
|
2019-09-06 10:55:00 +00:00
|
|
|
self.audio.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the group identifier for the subtitles in the variant stream.
|
2019-09-08 09:30:52 +00:00
|
|
|
pub fn subtitles(&self) -> Option<&String> {
|
2019-09-06 10:55:00 +00:00
|
|
|
self.subtitles.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the value of `CLOSED-CAPTIONS` attribute.
|
|
|
|
pub fn closed_captions(&self) -> Option<&ClosedCaptions> {
|
|
|
|
self.closed_captions.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the protocol compatibility version that this tag requires.
|
2019-09-08 10:23:33 +00:00
|
|
|
pub const fn requires_version(&self) -> ProtocolVersion {
|
2019-09-06 10:55:00 +00:00
|
|
|
ProtocolVersion::V1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ExtXStreamInf {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-09-21 13:20:19 +00:00
|
|
|
write!(f, "{}{}", Self::PREFIX, self.stream_inf)?;
|
2019-09-14 19:42:06 +00:00
|
|
|
if let Some(value) = &self.frame_rate {
|
|
|
|
write!(f, ",FRAME-RATE={:.3}", value.as_f64())?;
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
2019-09-14 19:42:06 +00:00
|
|
|
if let Some(value) = &self.audio {
|
|
|
|
write!(f, ",AUDIO={}", quote(value))?;
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
2019-09-14 19:42:06 +00:00
|
|
|
if let Some(value) = &self.subtitles {
|
|
|
|
write!(f, ",SUBTITLES={}", quote(value))?;
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
2019-09-14 19:42:06 +00:00
|
|
|
if let Some(value) = &self.closed_captions {
|
|
|
|
write!(f, ",CLOSED-CAPTIONS={}", value)?;
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
write!(f, "\n{}", self.uri)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for ExtXStreamInf {
|
|
|
|
type Err = Error;
|
2019-09-08 10:23:33 +00:00
|
|
|
|
2019-09-10 09:05:20 +00:00
|
|
|
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
|
|
|
let mut lines = input.lines();
|
2019-09-14 09:31:16 +00:00
|
|
|
let first_line = lines.next().ok_or(Error::missing_value("first_line"))?;
|
2019-09-21 13:20:19 +00:00
|
|
|
let uri = lines.next().ok_or(Error::missing_value("URI"))?;
|
2019-09-10 09:05:20 +00:00
|
|
|
|
2019-09-21 13:20:19 +00:00
|
|
|
let input = tag(first_line, Self::PREFIX)?;
|
2019-09-06 10:55:00 +00:00
|
|
|
|
|
|
|
let mut frame_rate = None;
|
|
|
|
let mut audio = None;
|
|
|
|
let mut subtitles = None;
|
|
|
|
let mut closed_captions = None;
|
2019-09-13 14:06:52 +00:00
|
|
|
|
2019-09-21 13:20:19 +00:00
|
|
|
for (key, value) in input.parse::<AttributePairs>()? {
|
2019-09-14 09:31:16 +00:00
|
|
|
match key.as_str() {
|
2019-09-13 14:06:52 +00:00
|
|
|
"FRAME-RATE" => frame_rate = Some((value.parse())?),
|
2019-09-08 09:30:52 +00:00
|
|
|
"AUDIO" => audio = Some(unquote(value)),
|
|
|
|
"SUBTITLES" => subtitles = Some(unquote(value)),
|
2019-09-13 14:06:52 +00:00
|
|
|
"CLOSED-CAPTIONS" => closed_captions = Some((value.parse())?),
|
2019-09-21 13:20:19 +00:00
|
|
|
_ => {}
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 09:05:20 +00:00
|
|
|
|
2019-09-21 13:20:19 +00:00
|
|
|
Ok(Self {
|
2019-09-21 10:11:36 +00:00
|
|
|
uri: uri.to_string(),
|
2019-09-06 10:55:00 +00:00
|
|
|
frame_rate,
|
|
|
|
audio,
|
|
|
|
subtitles,
|
|
|
|
closed_captions,
|
2019-09-21 13:20:19 +00:00
|
|
|
stream_inf: input.parse()?,
|
2019-09-06 10:55:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 13:20:19 +00:00
|
|
|
impl Deref for ExtXStreamInf {
|
|
|
|
type Target = StreamInf;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.stream_inf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for ExtXStreamInf {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.stream_inf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 10:55:00 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2019-09-14 19:08:35 +00:00
|
|
|
fn test_parser() {
|
2019-09-15 09:25:41 +00:00
|
|
|
let stream_inf = "#EXT-X-STREAM-INF:BANDWIDTH=1000\nhttp://www.example.com"
|
2019-09-14 19:08:35 +00:00
|
|
|
.parse::<ExtXStreamInf>()
|
|
|
|
.unwrap();
|
|
|
|
|
2019-09-15 09:25:41 +00:00
|
|
|
assert_eq!(
|
|
|
|
stream_inf,
|
2019-09-21 10:11:36 +00:00
|
|
|
ExtXStreamInf::new("http://www.example.com", 1000)
|
2019-09-15 09:25:41 +00:00
|
|
|
);
|
2019-09-14 19:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_requires_version() {
|
|
|
|
assert_eq!(
|
|
|
|
ProtocolVersion::V1,
|
2019-09-21 10:11:36 +00:00
|
|
|
ExtXStreamInf::new("http://www.example.com", 1000).requires_version()
|
2019-09-14 19:08:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_display() {
|
|
|
|
assert_eq!(
|
2019-09-21 10:11:36 +00:00
|
|
|
ExtXStreamInf::new("http://www.example.com/", 1000).to_string(),
|
2019-09-15 09:25:41 +00:00
|
|
|
"#EXT-X-STREAM-INF:BANDWIDTH=1000\nhttp://www.example.com/".to_string()
|
2019-09-14 19:08:35 +00:00
|
|
|
);
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|