1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-15 11:30:35 +00:00
hls_m3u8/src/tags/master_playlist/session_key.rs

73 lines
2.1 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use crate::types::{DecryptionKey, ProtocolVersion};
use crate::{Error, ErrorKind, Result};
use std::fmt;
use std::str::FromStr;
/// [4.3.4.5. EXT-X-SESSION-KEY]
///
/// [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)]
pub struct ExtXSessionKey {
key: DecryptionKey,
}
impl ExtXSessionKey {
pub(crate) const PREFIX: &'static str = "#EXT-X-SESSION-KEY:";
/// Makes a new `ExtXSessionKey` tag.
2019-09-08 10:23:33 +00:00
pub const fn new(key: DecryptionKey) -> Self {
2019-09-06 10:55:00 +00:00
ExtXSessionKey { key }
}
/// Returns a decryption key for the playlist.
2019-09-08 10:23:33 +00:00
pub const fn key(&self) -> &DecryptionKey {
2019-09-06 10:55:00 +00:00
&self.key
}
/// Returns the protocol compatibility version that this tag requires.
pub fn requires_version(&self) -> ProtocolVersion {
self.key.requires_version()
}
}
impl fmt::Display for ExtXSessionKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", Self::PREFIX, self.key)
}
}
impl FromStr for ExtXSessionKey {
type Err = Error;
2019-09-08 09:30:52 +00:00
2019-09-06 10:55:00 +00:00
fn from_str(s: &str) -> Result<Self> {
track_assert!(s.starts_with(Self::PREFIX), ErrorKind::InvalidInput);
let suffix = s.split_at(Self::PREFIX.len()).1;
let key = track!(suffix.parse())?;
Ok(ExtXSessionKey { key })
}
}
#[cfg(test)]
mod test {
use super::*;
2019-09-08 09:30:52 +00:00
use crate::types::{EncryptionMethod, InitializationVector};
2019-09-06 10:55:00 +00:00
#[test]
fn ext_x_session_key() {
let tag = ExtXSessionKey::new(DecryptionKey {
method: EncryptionMethod::Aes128,
2019-09-08 09:30:52 +00:00
uri: "foo".to_string(),
2019-09-06 10:55:00 +00:00
iv: Some(InitializationVector([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
])),
key_format: None,
key_format_versions: None,
});
let text =
r#"#EXT-X-SESSION-KEY:METHOD=AES-128,URI="foo",IV=0x000102030405060708090a0b0c0d0e0f"#;
assert_eq!(text.parse().ok(), Some(tag.clone()));
assert_eq!(tag.to_string(), text);
assert_eq!(tag.requires_version(), ProtocolVersion::V2);
}
}