1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-26 00:10:32 +00:00
hls_m3u8/src/tags/media_segment/map.rs

215 lines
5.9 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
use std::str::FromStr;
2020-02-02 12:38:11 +00:00
use shorthand::ShortHand;
2019-09-13 14:06:52 +00:00
use crate::attribute::AttributePairs;
2019-10-04 09:02:21 +00:00
use crate::tags::ExtXKey;
use crate::types::{ByteRange, ProtocolVersion};
2019-09-13 14:06:52 +00:00
use crate::utils::{quote, tag, unquote};
2019-10-04 09:02:21 +00:00
use crate::{Encrypted, Error, RequiredVersion};
2019-09-13 14:06:52 +00:00
2019-10-06 14:39:18 +00:00
/// # [4.3.2.5. EXT-X-MAP]
///
2019-10-03 14:23:27 +00:00
/// The [`ExtXMap`] tag specifies how to obtain the Media Initialization
2019-10-06 14:39:18 +00:00
/// Section, required to parse the applicable [`MediaSegment`]s.
2019-09-06 10:55:00 +00:00
///
2019-10-06 14:39:18 +00:00
/// [`MediaSegment`]: crate::MediaSegment
/// [4.3.2.5. EXT-X-MAP]: https://tools.ietf.org/html/rfc8216#section-4.3.2.5
2020-02-02 12:38:11 +00:00
#[derive(ShortHand, Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[shorthand(enable(must_use, into))]
2019-09-06 10:55:00 +00:00
pub struct ExtXMap {
2020-02-02 12:38:11 +00:00
/// The `URI` that identifies a resource, that contains the media
/// initialization section.
///
/// # Example
///
/// ```
/// # use hls_m3u8::tags::ExtXMap;
/// let mut map = ExtXMap::new("https://prod.mediaspace.com/init.bin");
/// # assert_eq!(
/// # map.uri(),
/// # &"https://prod.mediaspace.com/init.bin".to_string()
/// # );
/// map.set_uri("https://www.google.com/init.bin");
///
/// assert_eq!(map.uri(), &"https://www.google.com/init.bin".to_string());
/// ```
2019-09-08 09:30:52 +00:00
uri: String,
2020-02-02 12:38:11 +00:00
/// The range of the media initialization section.
///
/// # Example
///
/// ```
/// # use hls_m3u8::tags::ExtXMap;
/// use hls_m3u8::types::ByteRange;
///
/// let mut map = ExtXMap::with_range(
/// "https://prod.mediaspace.com/init.bin",
/// ByteRange::new(9, Some(2)),
/// );
///
/// map.set_range(Some(ByteRange::new(1, None)));
/// assert_eq!(map.range(), Some(ByteRange::new(1, None)));
/// ```
#[shorthand(enable(copy))]
2019-09-06 10:55:00 +00:00
range: Option<ByteRange>,
2020-02-02 12:38:11 +00:00
#[shorthand(enable(skip))]
2019-10-04 09:02:21 +00:00
keys: Vec<ExtXKey>,
2019-09-06 10:55:00 +00:00
}
impl ExtXMap {
pub(crate) const PREFIX: &'static str = "#EXT-X-MAP:";
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXMap`] tag.
2019-10-06 14:39:18 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-10-06 14:39:18 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXMap;
/// let map = ExtXMap::new("https://prod.mediaspace.com/init.bin");
/// ```
2019-09-08 09:30:52 +00:00
pub fn new<T: ToString>(uri: T) -> Self {
2019-10-04 09:02:21 +00:00
Self {
2019-09-08 09:30:52 +00:00
uri: uri.to_string(),
range: None,
2019-10-04 09:02:21 +00:00
keys: vec![],
2019-09-08 09:30:52 +00:00
}
2019-09-06 10:55:00 +00:00
}
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXMap`] tag with the given range.
2019-10-06 14:39:18 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-10-06 14:39:18 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXMap;
/// use hls_m3u8::types::ByteRange;
///
/// let map = ExtXMap::with_range(
/// "https://prod.mediaspace.com/init.bin",
/// ByteRange::new(9, Some(2)),
/// );
/// ```
2019-09-08 09:30:52 +00:00
pub fn with_range<T: ToString>(uri: T, range: ByteRange) -> Self {
2019-10-04 09:02:21 +00:00
Self {
2019-09-08 09:30:52 +00:00
uri: uri.to_string(),
2019-09-06 10:55:00 +00:00
range: Some(range),
2019-10-04 09:02:21 +00:00
keys: vec![],
2019-09-06 10:55:00 +00:00
}
}
2019-09-22 08:57:28 +00:00
}
2019-09-06 10:55:00 +00:00
2019-10-04 09:02:21 +00:00
impl Encrypted for ExtXMap {
fn keys(&self) -> &Vec<ExtXKey> { &self.keys }
fn keys_mut(&mut self) -> &mut Vec<ExtXKey> { &mut self.keys }
}
/// This tag requires [`ProtocolVersion::V6`].
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXMap {
2019-10-06 15:30:24 +00:00
// this should return ProtocolVersion::V5, if it does not contain an
// EXT-X-I-FRAMES-ONLY!
// http://alexzambelli.com/blog/2016/05/04/understanding-hls-versions-and-client-compatibility/
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V6 }
2019-10-06 15:30:24 +00:00
fn introduced_version(&self) -> ProtocolVersion { ProtocolVersion::V5 }
2019-09-06 10:55:00 +00:00
}
impl fmt::Display for ExtXMap {
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-10-04 09:02:21 +00:00
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.range {
write!(f, ",BYTERANGE={}", quote(value))?;
2019-09-06 10:55:00 +00:00
}
2019-10-04 09:02:21 +00:00
2019-09-06 10:55:00 +00:00
Ok(())
}
}
impl FromStr for ExtXMap {
type Err = Error;
2019-09-08 10:23:33 +00:00
2019-09-13 14:06:52 +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 range = None;
2019-09-14 09:31:16 +00:00
for (key, value) in AttributePairs::new(input) {
match key {
2019-09-08 09:30:52 +00:00
"URI" => uri = Some(unquote(value)),
2019-09-06 10:55:00 +00:00
"BYTERANGE" => {
2019-10-06 14:39:18 +00:00
range = Some(unquote(value).parse()?);
2019-09-06 10:55:00 +00:00
}
_ => {
// [6.3.1. General Client Responsibilities]
2019-10-03 15:01:15 +00:00
// > ignore any attribute/value pair with an unrecognized
// AttributeName.
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("EXT-X-URI"))?;
2019-10-04 09:02:21 +00:00
Ok(Self {
uri,
range,
keys: vec![],
})
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
#[test]
2019-09-22 08:57:28 +00:00
fn test_display() {
assert_eq!(
ExtXMap::new("foo").to_string(),
"#EXT-X-MAP:URI=\"foo\"".to_string(),
);
assert_eq!(
ExtXMap::with_range("foo", ByteRange::new(9, Some(2))).to_string(),
"#EXT-X-MAP:URI=\"foo\",BYTERANGE=\"9@2\"".to_string(),
);
}
#[test]
fn test_parser() {
assert_eq!(
ExtXMap::new("foo"),
"#EXT-X-MAP:URI=\"foo\"".parse().unwrap()
);
assert_eq!(
ExtXMap::with_range("foo", ByteRange::new(9, Some(2))),
"#EXT-X-MAP:URI=\"foo\",BYTERANGE=\"9@2\"".parse().unwrap()
);
2019-10-06 14:39:18 +00:00
assert_eq!(
ExtXMap::with_range("foo", ByteRange::new(9, Some(2))),
"#EXT-X-MAP:URI=\"foo\",BYTERANGE=\"9@2\",UNKNOWN=IGNORED"
.parse()
.unwrap()
);
2019-09-22 08:57:28 +00:00
}
#[test]
fn test_required_version() {
assert_eq!(ExtXMap::new("foo").required_version(), ProtocolVersion::V6);
assert_eq!(
ExtXMap::with_range("foo", ByteRange::new(9, Some(2))).required_version(),
ProtocolVersion::V6
);
2019-09-06 10:55:00 +00:00
}
2019-10-06 14:39:18 +00:00
#[test]
fn test_encrypted() {
assert_eq!(ExtXMap::new("foo").keys(), &vec![]);
assert_eq!(ExtXMap::new("foo").keys_mut(), &mut vec![]);
}
2019-09-06 10:55:00 +00:00
}