1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-17 07:52:58 +00:00
hls_m3u8/src/tags/media_segment/key.rs

171 lines
4.5 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
2019-09-17 12:45:10 +00:00
use std::ops::{Deref, DerefMut};
2019-09-06 10:55:00 +00:00
use std::str::FromStr;
2019-09-22 18:33:40 +00:00
use crate::types::{DecryptionKey, EncryptionMethod};
2019-09-17 12:45:10 +00:00
use crate::utils::tag;
2019-09-13 14:06:52 +00:00
use crate::Error;
2019-09-22 16:00:38 +00:00
/// # [4.4.2.4. EXT-X-KEY]
/// [Media Segment]s may be encrypted. The [ExtXKey] tag specifies how to
/// decrypt them. It applies to every [Media Segment] and to every Media
/// Initialization Section declared by an [ExtXMap] tag, that appears
/// between it and the next [ExtXKey] tag in the Playlist file with the
/// same [KeyFormat] attribute (or the end of the Playlist file).
///
/// The format is:
/// ```text
/// #EXT-X-KEY:<attribute-list>
/// ```
2019-09-06 10:55:00 +00:00
///
2019-09-15 14:45:43 +00:00
/// # Note
/// In case of an empty key (`EncryptionMethod::None`), all attributes will be ignored.
2019-09-22 16:00:38 +00:00
///
/// [KeyFormat]: crate::types::KeyFormat
/// [ExtXMap]: crate::tags::ExtXMap
/// [Media Segment]: crate::MediaSegment
/// [4.4.2.4. EXT-X-KEY]:
/// https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-04#section-4.4.2.4
2019-09-06 10:55:00 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-09-17 12:45:10 +00:00
pub struct ExtXKey(DecryptionKey);
2019-09-06 10:55:00 +00:00
impl ExtXKey {
pub(crate) const PREFIX: &'static str = "#EXT-X-KEY:";
/// Makes a new `ExtXKey` tag.
2019-09-21 11:24:05 +00:00
/// # Examples
2019-09-15 14:45:43 +00:00
/// ```
/// use hls_m3u8::tags::ExtXKey;
/// use hls_m3u8::types::EncryptionMethod;
///
/// let key = ExtXKey::new(
/// EncryptionMethod::Aes128,
2019-09-21 10:11:36 +00:00
/// "https://www.example.com/"
2019-09-15 14:45:43 +00:00
/// );
///
/// assert_eq!(
/// key.to_string(),
/// "#EXT-X-KEY:METHOD=AES-128,URI=\"https://www.example.com/\""
/// );
/// ```
2019-09-21 10:11:36 +00:00
pub fn new<T: ToString>(method: EncryptionMethod, uri: T) -> Self {
2019-09-17 12:45:10 +00:00
Self(DecryptionKey::new(method, uri))
2019-09-06 10:55:00 +00:00
}
/// Makes a new `ExtXKey` tag without a decryption key.
2019-09-21 11:24:05 +00:00
/// # Examples
2019-09-15 14:45:43 +00:00
/// ```
/// use hls_m3u8::tags::ExtXKey;
///
/// let key = ExtXKey::empty();
///
/// assert_eq!(
/// key.to_string(),
/// "#EXT-X-KEY:METHOD=NONE"
/// );
/// ```
2019-09-22 18:33:40 +00:00
pub const fn empty() -> Self {
2019-09-17 12:45:10 +00:00
Self(DecryptionKey {
2019-09-15 14:45:43 +00:00
method: EncryptionMethod::None,
uri: None,
iv: None,
key_format: None,
2019-09-22 18:33:40 +00:00
key_format_versions: None,
2019-09-17 12:45:10 +00:00
})
2019-09-15 14:45:43 +00:00
}
2019-09-17 12:45:10 +00:00
/// Returns whether the [EncryptionMethod] is [None](EncryptionMethod::None).
2019-09-21 11:24:05 +00:00
/// # Examples
2019-09-15 14:45:43 +00:00
/// ```
/// use hls_m3u8::tags::ExtXKey;
/// use hls_m3u8::types::EncryptionMethod;
///
/// let key = ExtXKey::empty();
///
/// assert_eq!(
/// key.method() == EncryptionMethod::None,
/// key.is_empty()
/// );
/// ```
pub fn is_empty(&self) -> bool {
2019-09-17 12:45:10 +00:00
self.0.method() == EncryptionMethod::None
2019-09-15 14:45:43 +00:00
}
2019-09-17 12:45:10 +00:00
}
2019-09-15 14:45:43 +00:00
2019-09-17 12:45:10 +00:00
impl FromStr for ExtXKey {
type Err = Error;
2019-09-06 10:55:00 +00:00
2019-09-17 12:45:10 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
let input = tag(input, Self::PREFIX)?;
Ok(Self(input.parse()?))
2019-09-06 10:55:00 +00:00
}
}
impl fmt::Display for ExtXKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-09-17 12:45:10 +00:00
write!(f, "{}{}", Self::PREFIX, self.0)
2019-09-06 10:55:00 +00:00
}
}
2019-09-17 12:45:10 +00:00
impl Deref for ExtXKey {
type Target = DecryptionKey;
2019-09-14 09:31:16 +00:00
2019-09-17 12:45:10 +00:00
fn deref(&self) -> &Self::Target {
&self.0
}
}
2019-09-15 14:45:43 +00:00
2019-09-17 12:45:10 +00:00
impl DerefMut for ExtXKey {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
2019-09-06 10:55:00 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
2019-09-22 16:00:38 +00:00
use crate::types::{EncryptionMethod, KeyFormat};
2019-09-06 10:55:00 +00:00
#[test]
2019-09-15 14:45:43 +00:00
fn test_display() {
assert_eq!(
ExtXKey::empty().to_string(),
"#EXT-X-KEY:METHOD=NONE".to_string()
);
2019-09-17 12:45:10 +00:00
let mut key = ExtXKey::empty();
2019-09-22 18:33:40 +00:00
// it is expected, that all attributes will be ignored for an empty key!
2019-09-22 16:00:38 +00:00
key.set_key_format(Some(KeyFormat::Identity));
2019-09-22 18:33:40 +00:00
key.set_iv(Some([
2019-09-15 14:45:43 +00:00
16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82,
2019-09-22 18:33:40 +00:00
]));
2019-09-21 10:11:36 +00:00
key.set_uri(Some("https://www.example.com"));
2019-09-22 18:33:40 +00:00
key.set_key_format_versions(Some(vec![1, 2, 3]));
2019-09-15 14:45:43 +00:00
2019-09-17 12:45:10 +00:00
assert_eq!(key.to_string(), "#EXT-X-KEY:METHOD=NONE".to_string());
2019-09-15 14:45:43 +00:00
}
#[test]
fn test_parser() {
assert_eq!(
2019-09-22 16:00:38 +00:00
"#EXT-X-KEY:\
METHOD=AES-128,\
URI=\"https://priv.example.com/key.php?r=52\""
2019-09-15 14:45:43 +00:00
.parse::<ExtXKey>()
.unwrap(),
ExtXKey::new(
EncryptionMethod::Aes128,
2019-09-21 10:11:36 +00:00
"https://priv.example.com/key.php?r=52"
2019-09-15 14:45:43 +00:00
)
);
let mut key = ExtXKey::new(
EncryptionMethod::Aes128,
2019-09-21 10:11:36 +00:00
"https://www.example.com/hls-key/key.bin",
2019-09-15 14:45:43 +00:00
);
2019-09-22 18:33:40 +00:00
key.set_iv(Some([
2019-09-15 14:45:43 +00:00
16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82,
2019-09-22 18:33:40 +00:00
]));
2019-09-06 10:55:00 +00:00
}
}