1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-03 05:59:22 +00:00
hls_m3u8/src/tags/master_playlist/i_frame_stream_inf.rs

360 lines
11 KiB
Rust
Raw Normal View History

2019-09-10 09:05:20 +00:00
use std::fmt;
use std::str::FromStr;
2019-09-06 10:55:00 +00:00
use crate::attribute::AttributePairs;
2019-09-08 09:30:52 +00:00
use crate::types::{DecimalResolution, HdcpLevel, ProtocolVersion};
2019-09-06 11:21:05 +00:00
use crate::utils::parse_u64;
2019-09-10 09:05:20 +00:00
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.3. EXT-X-I-FRAME-STREAM-INF]
///
/// [4.3.4.3. EXT-X-I-FRAME-STREAM-INF]: https://tools.ietf.org/html/rfc8216#section-4.3.4.3
2019-09-21 09:53:34 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-09-06 10:55:00 +00:00
pub struct ExtXIFrameStreamInf {
2019-09-08 09:30:52 +00:00
uri: String,
2019-09-06 10:55:00 +00:00
bandwidth: u64,
average_bandwidth: Option<u64>,
2019-09-08 09:30:52 +00:00
codecs: Option<String>,
2019-09-06 10:55:00 +00:00
resolution: Option<DecimalResolution>,
hdcp_level: Option<HdcpLevel>,
2019-09-08 09:30:52 +00:00
video: Option<String>,
2019-09-06 10:55:00 +00:00
}
impl ExtXIFrameStreamInf {
pub(crate) const PREFIX: &'static str = "#EXT-X-I-FRAME-STREAM-INF:";
/// Makes a new `ExtXIFrameStreamInf` tag.
2019-09-08 09:30:52 +00:00
pub fn new<T: ToString>(uri: T, bandwidth: u64) -> Self {
2019-09-06 10:55:00 +00:00
ExtXIFrameStreamInf {
2019-09-08 09:30:52 +00:00
uri: uri.to_string(),
2019-09-06 10:55:00 +00:00
bandwidth,
average_bandwidth: None,
codecs: None,
resolution: None,
hdcp_level: None,
video: None,
}
}
2019-09-21 09:53:34 +00:00
/// Returns the URI, that identifies the associated media playlist.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
/// assert_eq!(stream.uri(), &"https://www.example.com".to_string());
/// ```
pub const fn uri(&self) -> &String {
&self.uri
}
/// Sets the URI, that identifies the associated media playlist.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let mut stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
///
/// stream.set_uri("../new/uri");
/// assert_eq!(stream.uri(), &"../new/uri".to_string());
/// ```
pub fn set_uri<T: ToString>(&mut self, value: T) -> &mut Self {
self.uri = value.to_string();
self
}
/// Returns the peak segment bit rate of the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
/// assert_eq!(stream.bandwidth(), 20);
/// ```
pub const fn bandwidth(&self) -> u64 {
self.bandwidth
}
/// Sets the group identifier for the video in the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let mut stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
///
/// stream.set_video(Some("video"));
/// assert_eq!(stream.video(), &Some("video".to_string()));
/// ```
pub fn set_video<T: ToString>(&mut self, value: Option<T>) -> &mut Self {
self.video = value.map(|v| v.to_string());
self
}
/// Returns the group identifier for the video in the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
/// assert_eq!(stream.video(), &None);
/// ```
pub const fn video(&self) -> &Option<String> {
&self.video
}
/// Sets the peak segment bit rate of the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let mut stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
///
/// stream.set_bandwidth(5);
/// assert_eq!(stream.bandwidth(), 5);
/// ```
pub fn set_bandwidth(&mut self, value: u64) -> &mut Self {
self.bandwidth = value;
self
}
/// Returns the average segment bit rate of the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
/// assert_eq!(stream.average_bandwidth(), None);
/// ```
pub const fn average_bandwidth(&self) -> Option<u64> {
self.average_bandwidth
}
/// Sets the average segment bit rate of the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let mut stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
///
/// stream.set_average_bandwidth(Some(300));
/// assert_eq!(stream.average_bandwidth(), Some(300));
/// ```
pub fn set_average_bandwidth(&mut self, value: Option<u64>) -> &mut Self {
self.average_bandwidth = value;
self
}
/// A string that represents the list of codec types contained the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
/// assert_eq!(stream.codecs(), &None);
/// ```
pub const fn codecs(&self) -> &Option<String> {
&self.codecs
}
/// A string that represents the list of codec types contained the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let mut stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
///
/// stream.set_uri("../new/uri");
/// assert_eq!(stream.uri(), &"../new/uri".to_string());
/// ```
pub fn set_codecs<T: ToString>(&mut self, value: Option<T>) -> &mut Self {
self.codecs = value.map(|v| v.to_string());
self
}
/// Returns the resolution of the stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
/// assert_eq!(stream.resolution(), None);
/// ```
2019-09-15 08:51:04 +00:00
pub fn resolution(&self) -> Option<(usize, usize)> {
if let Some(res) = &self.resolution {
Some((res.width(), res.height()))
} else {
None
}
}
2019-09-21 09:53:34 +00:00
/// Sets the resolution of the stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let mut stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
///
/// stream.set_resolution(1920, 1080);
/// assert_eq!(stream.resolution(), Some((1920, 1080)));
/// ```
2019-09-15 08:51:04 +00:00
pub fn set_resolution(&mut self, width: usize, height: usize) -> &mut Self {
if let Some(res) = &mut self.resolution {
res.set_width(width);
res.set_height(height);
} else {
self.resolution = Some(DecimalResolution::new(width, height));
}
self
}
2019-09-21 09:53:34 +00:00
/// The HDCP level of the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
/// assert_eq!(stream.uri(), &"https://www.example.com".to_string());
/// ```
pub const fn hdcp_level(&self) -> Option<HdcpLevel> {
self.hdcp_level
}
/// The HDCP level of the variant stream.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// #
/// let mut stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
///
/// stream.set_uri("../new/uri");
/// assert_eq!(stream.uri(), &"../new/uri".to_string());
/// ```
pub fn set_hdcp_level<T: Into<HdcpLevel>>(&mut self, value: Option<T>) -> &mut Self {
self.hdcp_level = value.map(|v| v.into());
self
}
2019-09-06 10:55:00 +00:00
/// 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 ExtXIFrameStreamInf {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", Self::PREFIX)?;
2019-09-08 09:30:52 +00:00
write!(f, "URI={}", quote(&self.uri))?;
2019-09-06 10:55:00 +00:00
write!(f, ",BANDWIDTH={}", self.bandwidth)?;
2019-09-08 09:30:52 +00:00
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.average_bandwidth {
write!(f, ",AVERAGE-BANDWIDTH={}", value)?;
2019-09-06 10:55:00 +00:00
}
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.codecs {
write!(f, ",CODECS={}", quote(value))?;
2019-09-06 10:55:00 +00:00
}
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.resolution {
write!(f, ",RESOLUTION={}", value)?;
2019-09-06 10:55:00 +00:00
}
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.hdcp_level {
write!(f, ",HDCP-LEVEL={}", value)?;
2019-09-06 10:55:00 +00:00
}
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.video {
write!(f, ",VIDEO={}", quote(value))?;
2019-09-06 10:55:00 +00:00
}
Ok(())
}
}
impl FromStr for ExtXIFrameStreamInf {
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 input = tag(input, Self::PREFIX)?;
2019-09-06 10:55:00 +00:00
let mut uri = None;
let mut bandwidth = None;
let mut average_bandwidth = None;
let mut codecs = None;
let mut resolution = None;
let mut hdcp_level = None;
let mut video = None;
2019-09-10 09:05:20 +00:00
2019-09-14 09:31:16 +00:00
for (key, value) in input.parse::<AttributePairs>()? {
match key.as_str() {
2019-09-08 09:30:52 +00:00
"URI" => uri = Some(unquote(value)),
2019-09-13 14:06:52 +00:00
"BANDWIDTH" => bandwidth = Some(parse_u64(value)?),
"AVERAGE-BANDWIDTH" => average_bandwidth = Some(parse_u64(value)?),
2019-09-08 09:30:52 +00:00
"CODECS" => codecs = Some(unquote(value)),
2019-09-13 14:06:52 +00:00
"RESOLUTION" => resolution = Some(value.parse()?),
"HDCP-LEVEL" => hdcp_level = Some(value.parse()?),
2019-09-08 09:30:52 +00:00
"VIDEO" => video = Some(unquote(value)),
2019-09-06 10:55:00 +00:00
_ => {
// [6.3.1. General Client Responsibilities]
// > ignore any attribute/value pair with an unrecognized AttributeName.
}
}
}
2019-09-13 14:06:52 +00:00
let uri = uri.ok_or(Error::missing_value("URI"))?;
let bandwidth = bandwidth.ok_or(Error::missing_value("BANDWIDTH"))?;
2019-09-06 10:55:00 +00:00
Ok(ExtXIFrameStreamInf {
uri,
bandwidth,
average_bandwidth,
codecs,
resolution,
hdcp_level,
video,
})
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
2019-09-08 10:49:22 +00:00
fn test_display() {
2019-09-06 10:55:00 +00:00
let text = r#"#EXT-X-I-FRAME-STREAM-INF:URI="foo",BANDWIDTH=1000"#;
2019-09-08 10:49:22 +00:00
assert_eq!(ExtXIFrameStreamInf::new("foo", 1000).to_string(), text);
}
#[test]
fn test_parser() {
let text = r#"#EXT-X-I-FRAME-STREAM-INF:URI="foo",BANDWIDTH=1000"#;
let i_frame_stream_inf = ExtXIFrameStreamInf::new("foo", 1000);
assert_eq!(
text.parse::<ExtXIFrameStreamInf>().unwrap(),
i_frame_stream_inf.clone()
);
assert_eq!(i_frame_stream_inf.uri(), "foo");
2019-09-21 09:53:34 +00:00
assert_eq!(i_frame_stream_inf.bandwidth(), 1000);
2019-09-08 10:49:22 +00:00
// TODO: test all the optional fields
}
#[test]
fn test_requires_version() {
assert_eq!(
ExtXIFrameStreamInf::new("foo", 1000).requires_version(),
ProtocolVersion::V1
);
2019-09-06 10:55:00 +00:00
}
}