1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-17 16:02:59 +00:00
hls_m3u8/src/tags/master_playlist/session_key.rs

200 lines
5.6 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 08:57:28 +00:00
use crate::types::{DecryptionKey, EncryptionMethod, ProtocolVersion, RequiredVersion};
2019-09-17 12:45:10 +00:00
use crate::utils::tag;
2019-09-15 14:45:43 +00:00
use crate::Error;
2019-09-10 09:05:20 +00:00
2019-09-22 16:00:38 +00:00
/// # [4.3.4.5. EXT-X-SESSION-KEY]
/// The [ExtXSessionKey] tag allows encryption keys from [Media Playlist]s
/// to be specified in a [Master Playlist]. This allows the client to
/// preload these keys without having to read the [Media Playlist]s
/// first.
2019-09-06 10:55:00 +00:00
///
2019-09-22 16:00:38 +00:00
/// Its format is:
/// ```text
/// #EXT-X-SESSION-KEY:<attribute-list>
/// ```
///
/// [Media Playlist]: crate::MediaPlaylist
/// [Master Playlist]: crate::MasterPlaylist
2019-09-06 10:55:00 +00:00
/// [4.3.4.5. EXT-X-SESSION-KEY]: https://tools.ietf.org/html/rfc8216#section-4.3.4.5
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-09-17 12:45:10 +00:00
pub struct ExtXSessionKey(DecryptionKey);
2019-09-06 10:55:00 +00:00
impl ExtXSessionKey {
pub(crate) const PREFIX: &'static str = "#EXT-X-SESSION-KEY:";
2019-09-15 14:45:43 +00:00
/// Makes a new [ExtXSessionKey] tag.
2019-09-22 16:00:38 +00:00
///
2019-09-15 14:45:43 +00:00
/// # Panic
2019-09-22 16:00:38 +00:00
/// An [ExtXSessionKey] should only be used, if the segments of the stream are encrypted.
/// Therefore this function will panic, if the `method` is [EncryptionMethod::None].
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXSessionKey;
/// use hls_m3u8::types::EncryptionMethod;
///
/// let session_key = ExtXSessionKey::new(
/// EncryptionMethod::Aes128,
/// "https://www.example.com/"
/// );
/// ```
2019-09-21 10:11:36 +00:00
pub fn new<T: ToString>(method: EncryptionMethod, uri: T) -> Self {
2019-09-15 14:45:43 +00:00
if method == EncryptionMethod::None {
panic!("The EncryptionMethod is not allowed to be None");
}
2019-09-17 12:45:10 +00:00
Self(DecryptionKey::new(method, uri))
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-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXSessionKey {
fn required_version(&self) -> ProtocolVersion {
2019-09-22 16:00:38 +00:00
self.0.required_version()
2019-09-06 10:55:00 +00:00
}
}
impl fmt::Display for ExtXSessionKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-09-22 16:00:38 +00:00
if self.0.method == EncryptionMethod::None {
return Err(fmt::Error);
}
2019-09-17 12:45:10 +00:00
write!(f, "{}{}", Self::PREFIX, self.0)
2019-09-06 10:55:00 +00:00
}
}
impl FromStr for ExtXSessionKey {
2019-09-15 14:45:43 +00:00
type Err = Error;
2019-09-08 09:30:52 +00:00
2019-09-10 09:05:20 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
2019-09-15 14:45:43 +00:00
let input = tag(input, Self::PREFIX)?;
2019-09-17 12:45:10 +00:00
Ok(Self(input.parse()?))
}
}
2019-09-15 14:45:43 +00:00
2019-09-17 12:45:10 +00:00
impl Deref for ExtXSessionKey {
type Target = DecryptionKey;
2019-09-15 14:45:43 +00:00
2019-09-17 12:45:10 +00:00
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ExtXSessionKey {
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() {
let mut key = ExtXSessionKey::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-15 14:45:43 +00:00
assert_eq!(
key.to_string(),
"#EXT-X-SESSION-KEY:METHOD=AES-128,\
URI=\"https://www.example.com/hls-key/key.bin\",\
IV=0x10ef8f758ca555115584bb5b3c687f52"
.to_string()
);
}
#[test]
fn test_parser() {
assert_eq!(
r#"#EXT-X-SESSION-KEY:METHOD=AES-128,URI="https://priv.example.com/key.php?r=52""#
.parse::<ExtXSessionKey>()
.unwrap(),
ExtXSessionKey::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 = ExtXSessionKey::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-15 14:45:43 +00:00
assert_eq!(
"#EXT-X-SESSION-KEY:METHOD=AES-128,\
URI=\"https://www.example.com/hls-key/key.bin\",\
IV=0X10ef8f758ca555115584bb5b3c687f52"
.parse::<ExtXSessionKey>()
.unwrap(),
key
);
2019-09-22 16:00:38 +00:00
key.set_key_format(Some(KeyFormat::Identity));
2019-09-15 14:45:43 +00:00
assert_eq!(
2019-09-22 16:00:38 +00:00
"#EXT-X-SESSION-KEY:\
METHOD=AES-128,\
URI=\"https://www.example.com/hls-key/key.bin\",\
IV=0x10ef8f758ca555115584bb5b3c687f52,\
KEYFORMAT=\"identity\""
.parse::<ExtXSessionKey>()
.unwrap(),
2019-09-15 14:45:43 +00:00
key
)
2019-09-06 10:55:00 +00:00
}
2019-09-22 08:57:28 +00:00
#[test]
fn test_required_version() {
assert_eq!(
ExtXSessionKey::new(EncryptionMethod::Aes128, "https://www.example.com/")
.required_version(),
ProtocolVersion::V1
);
}
2019-09-22 18:33:40 +00:00
#[test]
#[should_panic]
// ExtXSessionKey::new should panic, if the provided
// EncryptionMethod is None!
fn test_new_panic() {
ExtXSessionKey::new(EncryptionMethod::None, "");
}
#[test]
#[should_panic]
fn test_display_err() {
ExtXSessionKey(DecryptionKey::new(EncryptionMethod::None, "")).to_string();
}
#[test]
fn test_deref() {
let key = ExtXSessionKey::new(EncryptionMethod::Aes128, "https://www.example.com/");
assert_eq!(key.method(), EncryptionMethod::Aes128);
assert_eq!(key.uri(), &Some("https://www.example.com/".into()));
}
#[test]
fn test_deref_mut() {
let mut key = ExtXSessionKey::new(EncryptionMethod::Aes128, "https://www.example.com/");
key.set_method(EncryptionMethod::None);
assert_eq!(key.method(), EncryptionMethod::None);
key.set_uri(Some("https://www.github.com/"));
assert_eq!(key.uri(), &Some("https://www.github.com/".into()));
}
2019-09-06 10:55:00 +00:00
}