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

166 lines
4.7 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-10-05 12:45:40 +00:00
use crate::types::{DecryptionKey, EncryptionMethod, ProtocolVersion};
2019-09-17 12:45:10 +00:00
use crate::utils::tag;
2019-10-05 12:45:40 +00:00
use crate::{Error, RequiredVersion};
2019-09-13 14:06:52 +00:00
2019-10-05 12:45:40 +00:00
/// # [4.3.2.4. EXT-X-KEY]
///
2019-10-03 14:23:27 +00:00
/// [`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).
2019-09-22 16:00:38 +00:00
///
2019-09-15 14:45:43 +00:00
/// # Note
2020-02-02 12:38:11 +00:00
///
2019-10-05 12:45:40 +00:00
/// In case of an empty key ([`EncryptionMethod::None`]),
2019-10-03 14:23:27 +00:00
/// all attributes will be ignored.
2019-09-22 16:00:38 +00:00
///
2019-10-03 14:23:27 +00:00
/// [`KeyFormat`]: crate::types::KeyFormat
/// [`ExtXMap`]: crate::tags::ExtXMap
/// [`Media Segment`]: crate::MediaSegment
2019-10-05 12:45:40 +00:00
/// [4.3.2.4. EXT-X-KEY]: https://tools.ietf.org/html/rfc8216#section-4.3.2.4
2019-10-12 09:38:28 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
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:";
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXKey`] tag.
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-09-15 14:45:43 +00:00
/// ```
2019-10-05 12:45:40 +00:00
/// # use hls_m3u8::tags::ExtXKey;
2019-09-15 14:45:43 +00:00
/// use hls_m3u8::types::EncryptionMethod;
///
2019-10-03 15:01:15 +00:00
/// let key = ExtXKey::new(EncryptionMethod::Aes128, "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
}
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXKey`] tag without a decryption key.
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-09-15 14:45:43 +00:00
/// ```
2019-10-05 12:45:40 +00:00
/// # use hls_m3u8::tags::ExtXKey;
2019-09-15 14:45:43 +00:00
/// let key = ExtXKey::empty();
///
2019-10-03 15:01:15 +00:00
/// assert_eq!(key.to_string(), "#EXT-X-KEY:METHOD=NONE");
2019-09-15 14:45:43 +00:00
/// ```
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-10-03 14:23:27 +00:00
/// Returns whether the [`EncryptionMethod`] is
2019-10-05 12:45:40 +00:00
/// [`None`].
2019-10-03 14:23:27 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-09-15 14:45:43 +00:00
/// ```
2019-10-05 12:45:40 +00:00
/// # use hls_m3u8::tags::ExtXKey;
2019-09-15 14:45:43 +00:00
/// use hls_m3u8::types::EncryptionMethod;
///
/// let key = ExtXKey::empty();
///
2019-10-03 15:01:15 +00:00
/// assert_eq!(key.method() == EncryptionMethod::None, key.is_empty());
2019-09-15 14:45:43 +00:00
/// ```
2019-10-05 12:45:40 +00:00
///
/// [`None`]: EncryptionMethod::None
2019-10-03 15:01:15 +00:00
pub fn is_empty(&self) -> bool { self.0.method() == EncryptionMethod::None }
2019-09-17 12:45:10 +00:00
}
2019-09-15 14:45:43 +00:00
2020-02-02 12:38:11 +00:00
/// This tag requires the [`ProtocolVersion`] returned by
/// [`DecryptionKey::required_version`].
2019-10-05 12:45:40 +00:00
impl RequiredVersion for ExtXKey {
fn required_version(&self) -> ProtocolVersion { self.0.required_version() }
}
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 {
2019-10-03 15:01:15 +00:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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-10-03 15:01:15 +00:00
fn deref(&self) -> &Self::Target { &self.0 }
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 DerefMut for ExtXKey {
2019-10-03 15:01:15 +00:00
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};
use pretty_assertions::assert_eq;
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
}
}