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
|
|
|
|
/// 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-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-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-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-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
|
|
|
|
}
|
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>()? {
|
|
|
|
match key.as_str() {
|
2019-09-08 09:30:52 +00:00
|
|
|
"URI" => uri = Some(unquote(value)),
|
2019-09-21 13:20:19 +00:00
|
|
|
_ => {}
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 14:06:52 +00:00
|
|
|
let uri = uri.ok_or(Error::missing_value("URI"))?;
|
|
|
|
|
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-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]
|
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
|
|
|
}
|
|
|
|
}
|