1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-10 17:29:36 +00:00
hls_m3u8/src/tags/master_playlist/i_frame_stream_inf.rs

268 lines
7.6 KiB
Rust
Raw Normal View History

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-10-05 10:49:08 +00:00
use crate::types::{HdcpLevel, ProtocolVersion, StreamInf, StreamInfBuilder};
2019-09-10 09:05:20 +00:00
use crate::utils::{quote, tag, unquote};
2019-10-04 09:02:21 +00:00
use crate::{Error, RequiredVersion};
2019-09-06 10:55:00 +00:00
2019-10-05 07:44:23 +00:00
/// # [4.3.5.3. EXT-X-I-FRAME-STREAM-INF]
2019-10-05 10:49:08 +00:00
///
2019-10-03 14:23:27 +00:00
/// The [`ExtXIFrameStreamInf`] tag identifies a [`Media Playlist`] file,
/// containing the I-frames of a multimedia presentation.
2019-09-06 10:55:00 +00:00
///
2019-10-03 14:23:27 +00:00
/// I-frames are encoded video frames, whose decoding
/// does not depend on any other frame.
2019-09-22 08:57:28 +00:00
///
2019-10-03 14:23:27 +00:00
/// [`Master Playlist`]: crate::MasterPlaylist
/// [`Media Playlist`]: crate::MediaPlaylist
2019-10-05 07:44:23 +00:00
/// [4.3.5.3. EXT-X-I-FRAME-STREAM-INF]: https://tools.ietf.org/html/rfc8216#section-4.3.4.5
2019-09-22 18:33:40 +00:00
#[derive(PartialOrd, 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-21 13:20:19 +00:00
stream_inf: StreamInf,
2019-09-06 10:55:00 +00:00
}
2019-10-05 10:49:08 +00:00
#[derive(Default, Debug, Clone, PartialEq)]
/// Builder for [`ExtXIFrameStreamInf`].
pub struct ExtXIFrameStreamInfBuilder {
uri: Option<String>,
stream_inf: StreamInfBuilder,
}
impl ExtXIFrameStreamInfBuilder {
/// An `URI` to the [`MediaPlaylist`] file.
///
/// [`MediaPlaylist`]: crate::MediaPlaylist
pub fn uri<T: Into<String>>(&mut self, value: T) -> &mut Self {
self.uri = Some(value.into());
self
}
/// The maximum bandwidth of the stream.
pub fn bandwidth(&mut self, value: u64) -> &mut Self {
self.stream_inf.bandwidth(value);
self
}
/// The average bandwidth of the stream.
pub fn average_bandwidth(&mut self, value: u64) -> &mut Self {
self.stream_inf.average_bandwidth(value);
self
}
/// Every media format in any of the renditions specified by the Variant
/// Stream.
pub fn codecs<T: Into<String>>(&mut self, value: T) -> &mut Self {
self.stream_inf.codecs(value);
self
}
/// The resolution of the stream.
pub fn resolution(&mut self, value: (usize, usize)) -> &mut Self {
self.stream_inf.resolution(value);
self
}
/// High-bandwidth Digital Content Protection
pub fn hdcp_level(&mut self, value: HdcpLevel) -> &mut Self {
self.stream_inf.hdcp_level(value);
self
}
/// It indicates the set of video renditions, that should be used when
/// playing the presentation.
pub fn video<T: Into<String>>(&mut self, value: T) -> &mut Self {
self.stream_inf.video(value);
self
}
/// Build an [`ExtXIFrameStreamInf`].
pub fn build(&self) -> crate::Result<ExtXIFrameStreamInf> {
Ok(ExtXIFrameStreamInf {
uri: self
.uri
.clone()
.ok_or_else(|| Error::missing_value("frame rate"))?,
stream_inf: self.stream_inf.build().map_err(Error::builder_error)?,
})
}
}
2019-09-06 10:55:00 +00:00
impl ExtXIFrameStreamInf {
pub(crate) const PREFIX: &'static str = "#EXT-X-I-FRAME-STREAM-INF:";
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXIFrameStreamInf`] tag.
2019-09-22 18:33:40 +00:00
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXIFrameStreamInf;
/// let stream = ExtXIFrameStreamInf::new("https://www.example.com", 20);
/// ```
2019-09-08 09:30:52 +00:00
pub fn new<T: ToString>(uri: T, bandwidth: u64) -> Self {
2019-10-05 14:08:03 +00:00
Self {
2019-09-08 09:30:52 +00:00
uri: uri.to_string(),
2019-09-21 13:20:19 +00:00
stream_inf: StreamInf::new(bandwidth),
2019-09-06 10:55:00 +00:00
}
}
2019-10-05 10:49:08 +00:00
/// Returns a builder for [`ExtXIFrameStreamInf`].
pub fn builder() -> ExtXIFrameStreamInfBuilder { ExtXIFrameStreamInfBuilder::default() }
2019-10-03 14:23:27 +00:00
/// Returns the `URI`, that identifies the associated [`media playlist`].
2019-09-21 09:53:34 +00:00
///
/// # 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());
/// ```
2019-10-03 14:23:27 +00:00
///
/// [`media playlist`]: crate::MediaPlaylist
2019-10-03 15:01:15 +00:00
pub const fn uri(&self) -> &String { &self.uri }
2019-09-21 09:53:34 +00:00
2019-10-03 14:23:27 +00:00
/// Sets the `URI`, that identifies the associated [`media playlist`].
2019-09-21 09:53:34 +00:00
///
/// # 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());
/// ```
2019-10-03 14:23:27 +00:00
///
/// [`media playlist`]: crate::MediaPlaylist
2019-09-21 09:53:34 +00:00
pub fn set_uri<T: ToString>(&mut self, value: T) -> &mut Self {
self.uri = value.to_string();
self
}
2019-09-22 08:57:28 +00:00
}
2019-09-21 09:53:34 +00:00
2019-10-03 14:23:27 +00:00
/// This tag requires [`ProtocolVersion::V1`].
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXIFrameStreamInf {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 }
2019-09-06 10:55:00 +00:00
}
impl fmt::Display for ExtXIFrameStreamInf {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", Self::PREFIX)?;
2019-09-21 13:20:19 +00:00
write!(f, "URI={},{}", quote(&self.uri), self.stream_inf)?;
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;
2019-09-10 09:05:20 +00:00
2019-09-14 09:31:16 +00:00
for (key, value) in input.parse::<AttributePairs>()? {
2019-09-22 18:33:40 +00:00
if let "URI" = key.as_str() {
uri = Some(unquote(value));
2019-09-06 10:55:00 +00:00
}
}
2019-09-22 18:33:40 +00:00
let uri = uri.ok_or_else(|| Error::missing_value("URI"))?;
2019-09-13 14:06:52 +00:00
2019-09-21 13:20:19 +00:00
Ok(Self {
2019-09-06 10:55:00 +00:00
uri,
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 ExtXIFrameStreamInf {
type Target = StreamInf;
2019-10-03 15:01:15 +00:00
fn deref(&self) -> &Self::Target { &self.stream_inf }
2019-09-21 13:20:19 +00:00
}
impl DerefMut for ExtXIFrameStreamInf {
2019-10-03 15:01:15 +00:00
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.stream_inf }
2019-09-21 13:20:19 +00:00
}
2019-09-06 10:55:00 +00:00
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
2019-09-06 10:55:00 +00:00
2019-10-06 14:39:18 +00:00
#[test]
fn test_builder() {
let mut i_frame_stream_inf =
ExtXIFrameStreamInf::new("http://example.com/audio-only.m3u8", 200_000);
i_frame_stream_inf
.set_average_bandwidth(Some(100_000))
.set_codecs(Some("mp4a.40.5"))
.set_resolution(1920, 1080)
.set_hdcp_level(Some(HdcpLevel::None))
.set_video(Some("video"));
assert_eq!(
ExtXIFrameStreamInf::builder()
.uri("http://example.com/audio-only.m3u8")
.bandwidth(200_000)
.average_bandwidth(100_000)
.codecs("mp4a.40.5")
.resolution((1920, 1080))
.hdcp_level(HdcpLevel::None)
.video("video")
.build()
.unwrap(),
i_frame_stream_inf
);
}
2019-09-06 10:55:00 +00:00
#[test]
2019-09-08 10:49:22 +00:00
fn test_display() {
2019-09-22 16:00:38 +00:00
assert_eq!(
ExtXIFrameStreamInf::new("foo", 1000).to_string(),
"#EXT-X-I-FRAME-STREAM-INF:URI=\"foo\",BANDWIDTH=1000".to_string()
);
2019-09-08 10:49:22 +00:00
}
#[test]
fn test_parser() {
assert_eq!(
2019-09-22 16:00:38 +00:00
"#EXT-X-I-FRAME-STREAM-INF:URI=\"foo\",BANDWIDTH=1000"
.parse::<ExtXIFrameStreamInf>()
.unwrap(),
ExtXIFrameStreamInf::new("foo", 1000)
2019-09-08 10:49:22 +00:00
);
2019-09-22 18:33:40 +00:00
assert!("garbage".parse::<ExtXIFrameStreamInf>().is_err());
2019-09-08 10:49:22 +00:00
}
#[test]
2019-09-22 08:57:28 +00:00
fn test_required_version() {
2019-09-08 10:49:22 +00:00
assert_eq!(
2019-09-22 08:57:28 +00:00
ExtXIFrameStreamInf::new("foo", 1000).required_version(),
2019-09-08 10:49:22 +00:00
ProtocolVersion::V1
);
2019-09-06 10:55:00 +00:00
}
2019-09-22 18:33:40 +00:00
#[test]
fn test_deref() {
assert_eq!(
ExtXIFrameStreamInf::new("https://www.example.com", 20).average_bandwidth(),
None
)
}
#[test]
fn test_deref_mut() {
assert_eq!(
ExtXIFrameStreamInf::new("https://www.example.com", 20)
.set_average_bandwidth(Some(4))
.average_bandwidth(),
Some(4)
)
}
2019-09-06 10:55:00 +00:00
}