1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-21 01:38:07 +00:00
hls_m3u8/src/tags/master_playlist/i_frame_stream_inf.rs

177 lines
4.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-09-22 08:57:28 +00:00
use crate::types::{ProtocolVersion, RequiredVersion, StreamInf};
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
2019-09-22 08:57:28 +00:00
/// # [4.3.4.3. EXT-X-I-FRAME-STREAM-INF]
/// The [ExtXIFrameStreamInf] tag identifies a [Media Playlist] file
/// containing the I-frames of a multimedia presentation. It stands
2019-09-22 16:00:38 +00:00
/// alone, in that it does not apply to a particular `URI` in the [Master Playlist].
2019-09-06 10:55:00 +00:00
///
2019-09-22 08:57:28 +00:00
/// Its format is:
///
/// ```text
/// #EXT-X-I-FRAME-STREAM-INF:<attribute-list>
/// ```
///
/// [Master Playlist]: crate::MasterPlaylist
/// [Media Playlist]: crate::MediaPlaylist
2019-09-06 10:55:00 +00:00
/// [4.3.4.3. EXT-X-I-FRAME-STREAM-INF]: https://tools.ietf.org/html/rfc8216#section-4.3.4.3
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
}
impl ExtXIFrameStreamInf {
pub(crate) const PREFIX: &'static str = "#EXT-X-I-FRAME-STREAM-INF:";
2019-09-22 08:57:28 +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-09-06 10:55:00 +00:00
ExtXIFrameStreamInf {
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-09-22 16:00:38 +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());
/// ```
pub const fn uri(&self) -> &String {
&self.uri
}
2019-09-22 16:00:38 +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());
/// ```
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-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXIFrameStreamInf {
fn required_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-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;
fn deref(&self) -> &Self::Target {
&self.stream_inf
}
}
impl DerefMut for ExtXIFrameStreamInf {
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-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
}