1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-19 00:38:07 +00:00
hls_m3u8/src/tags/master_playlist/session_key.rs

167 lines
5 KiB
Rust
Raw Normal View History

2020-02-21 09:45:04 +00:00
use core::convert::TryFrom;
2019-09-06 10:55:00 +00:00
use std::fmt;
use derive_more::{AsMut, AsRef, From};
2020-02-02 14:23:47 +00:00
2020-02-21 09:45:04 +00:00
use crate::tags::ExtXKey;
use crate::types::{DecryptionKey, ProtocolVersion};
2019-09-17 12:45:10 +00:00
use crate::utils::tag;
2019-10-04 09:02:21 +00:00
use crate::{Error, RequiredVersion};
2019-09-10 09:05:20 +00:00
2020-02-14 12:05:18 +00:00
/// The [`ExtXSessionKey`] tag allows encryption keys from [`MediaPlaylist`]s
/// to be specified in a [`MasterPlaylist`]. This allows the client to
/// preload these keys without having to read the [`MediaPlaylist`]s
2019-09-22 16:00:38 +00:00
/// first.
2019-09-06 10:55:00 +00:00
///
2020-03-25 10:49:16 +00:00
/// If an [`ExtXSessionKey`] is used, the values of [`DecryptionKey::method`],
/// [`DecryptionKey::format`] and [`DecryptionKey::versions`] must match any
2020-03-17 14:48:02 +00:00
/// [`ExtXKey`] with the same uri field.
///
2020-02-14 12:05:18 +00:00
/// [`MediaPlaylist`]: crate::MediaPlaylist
/// [`MasterPlaylist`]: crate::MasterPlaylist
2020-03-25 10:49:16 +00:00
/// [`ExtXKey`]: crate::tags::ExtXKey
#[derive(AsRef, AsMut, From, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ExtXSessionKey<'a>(pub DecryptionKey<'a>);
2019-09-06 10:55:00 +00:00
impl<'a> ExtXSessionKey<'a> {
2019-09-06 10:55:00 +00:00
pub(crate) const PREFIX: &'static str = "#EXT-X-SESSION-KEY:";
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXSessionKey`] tag.
2019-09-22 16:00:38 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-09-22 16:00:38 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXSessionKey;
/// use hls_m3u8::types::{DecryptionKey, EncryptionMethod};
2019-09-22 16:00:38 +00:00
///
/// let session_key = ExtXSessionKey::new(DecryptionKey::new(
2020-02-21 09:45:04 +00:00
/// EncryptionMethod::Aes128,
/// "https://www.example.com/",
/// ));
2019-09-22 16:00:38 +00:00
/// ```
2020-02-24 15:30:43 +00:00
#[must_use]
#[inline]
pub const fn new(inner: DecryptionKey<'a>) -> Self { Self(inner) }
/// Makes the struct independent of its lifetime, by taking ownership of all
/// internal [`Cow`]s.
///
/// # Note
///
/// This is a relatively expensive operation.
///
/// [`Cow`]: std::borrow::Cow
#[must_use]
pub fn into_owned(self) -> ExtXSessionKey<'static> { ExtXSessionKey(self.0.into_owned()) }
2020-02-21 09:45:04 +00:00
}
impl<'a> TryFrom<ExtXKey<'a>> for ExtXSessionKey<'a> {
2020-02-21 09:45:04 +00:00
type Error = Error;
fn try_from(value: ExtXKey<'a>) -> Result<Self, Self::Error> {
if let ExtXKey(Some(inner)) = value {
Ok(Self(inner))
} else {
Err(Error::custom("missing decryption key"))
2020-02-21 09:45:04 +00:00
}
2019-09-06 10:55:00 +00:00
}
2019-09-22 08:57:28 +00:00
}
2019-09-06 10:55:00 +00:00
2020-02-10 12:21:48 +00:00
/// This tag requires the same [`ProtocolVersion`] that is returned by
/// `DecryptionKey::required_version`.
impl<'a> RequiredVersion for ExtXSessionKey<'a> {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { self.0.required_version() }
2019-09-06 10:55:00 +00:00
}
impl<'a> fmt::Display for ExtXSessionKey<'a> {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", Self::PREFIX, self.0.to_string())
2019-09-06 10:55:00 +00:00
}
}
impl<'a> TryFrom<&'a str> for ExtXSessionKey<'a> {
type Error = Error;
2019-09-08 09:30:52 +00:00
fn try_from(input: &'a str) -> Result<Self, Self::Error> {
Ok(Self(DecryptionKey::try_from(tag(input, Self::PREFIX)?)?))
2019-09-17 12:45:10 +00:00
}
}
2019-09-15 14:45:43 +00:00
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
macro_rules! generate_tests {
( $( { $struct:expr, $str:expr } ),+ $(,)* ) => {
#[test]
fn test_display() {
$(
assert_eq!($struct.to_string(), $str.to_string());
)+
}
#[test]
fn test_parser() {
$(
assert_eq!($struct, TryFrom::try_from($str).unwrap());
)+
}
}
2019-09-15 14:45:43 +00:00
}
generate_tests! {
{
ExtXSessionKey::new(
DecryptionKey::builder()
.method(EncryptionMethod::Aes128)
.uri("https://www.example.com/hls-key/key.bin")
.iv([
16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82,
])
.build()
.unwrap(),
),
2020-02-21 09:45:04 +00:00
concat!(
"#EXT-X-SESSION-KEY:",
"METHOD=AES-128,",
"URI=\"https://www.example.com/hls-key/key.bin\",",
"IV=0x10ef8f758ca555115584bb5b3c687f52"
2020-02-21 09:45:04 +00:00
)
},
{
ExtXSessionKey::new(
DecryptionKey::builder()
.method(EncryptionMethod::Aes128)
.uri("https://www.example.com/hls-key/key.bin")
.iv([
16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82,
])
.format(KeyFormat::Identity)
.build()
.unwrap(),
),
2020-02-21 09:45:04 +00:00
concat!(
"#EXT-X-SESSION-KEY:",
"METHOD=AES-128,",
"URI=\"https://www.example.com/hls-key/key.bin\",",
"IV=0x10ef8f758ca555115584bb5b3c687f52,",
"KEYFORMAT=\"identity\"",
)
}
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(DecryptionKey::new(
2020-02-21 09:45:04 +00:00
EncryptionMethod::Aes128,
"https://www.example.com/"
))
.required_version(),
2019-09-22 08:57:28 +00:00
ProtocolVersion::V1
);
}
2019-09-06 10:55:00 +00:00
}