From 53049478851630f66db9bf0d1cd8cf9250467942 Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Fri, 21 Feb 2020 20:44:09 +0100 Subject: [PATCH] various minor improvements --- src/master_playlist.rs | 2 ++ src/media_playlist.rs | 2 +- src/tags/media_segment/key.rs | 13 +++++++++---- src/tags/media_segment/program_date_time.rs | 4 ++-- src/tags/shared/mod.rs | 11 ----------- src/types/stream_data.rs | 19 ++++++++++++++++--- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/master_playlist.rs b/src/master_playlist.rs index c0ecd43..37d79c4 100644 --- a/src/master_playlist.rs +++ b/src/master_playlist.rs @@ -82,6 +82,8 @@ pub struct MasterPlaylist { /// # Note /// /// This tag is optional. + /// + /// [`MediaPlaylist`]: crate::MediaPlaylist #[builder(default)] session_keys: Vec, /// This is a list of all tags that could not be identified while parsing diff --git a/src/media_playlist.rs b/src/media_playlist.rs index bbfe944..0209226 100644 --- a/src/media_playlist.rs +++ b/src/media_playlist.rs @@ -270,7 +270,7 @@ impl fmt::Display for MediaPlaylist { // an old key might be removed: for k in &available_keys { if k.key_format() == key.key_format() && &key != k { - remove_key = Some(k.clone()); + remove_key = Some(*k); break; } } diff --git a/src/tags/media_segment/key.rs b/src/tags/media_segment/key.rs index 87d12fc..379276b 100644 --- a/src/tags/media_segment/key.rs +++ b/src/tags/media_segment/key.rs @@ -50,6 +50,8 @@ pub struct ExtXKey { /// # Note /// /// This attribute is required. + /// + /// [`MediaSegment`]: crate::MediaSegment #[shorthand(enable(copy))] pub(crate) method: EncryptionMethod, /// An `URI` that specifies how to obtain the key. @@ -100,7 +102,7 @@ pub struct ExtXKey { #[builder(setter(into, strip_option), default)] // TODO: workaround for https://github.com/Luro02/shorthand/issues/20 #[shorthand(enable(copy), disable(option_as_ref))] - pub(crate) iv: Option<[u8; 16]>, + pub(crate) iv: Option<[u8; 0x10]>, /// The [`KeyFormat`] specifies how the key is /// represented in the resource identified by the `URI`. /// @@ -151,8 +153,9 @@ pub struct ExtXKey { impl ExtXKeyBuilder { fn validate(&self) -> Result<(), String> { if self.method != Some(EncryptionMethod::None) && self.uri.is_none() { - return Err(Error::custom("missing URL").to_string()); + return Err(Error::missing_value("URL").to_string()); } + Ok(()) } } @@ -332,8 +335,10 @@ impl fmt::Display for ExtXKey { } if let Some(value) = &self.iv { - // TODO: use hex::encode_to_slice - write!(f, ",IV=0x{}", hex::encode(&value))?; + let mut result = [0; 0x10 * 2]; + hex::encode_to_slice(value, &mut result).unwrap(); + + write!(f, ",IV=0x{}", ::core::str::from_utf8(&result).unwrap())?; } if let Some(value) = &self.key_format { diff --git a/src/tags/media_segment/program_date_time.rs b/src/tags/media_segment/program_date_time.rs index ff91b00..bb68f37 100644 --- a/src/tags/media_segment/program_date_time.rs +++ b/src/tags/media_segment/program_date_time.rs @@ -11,9 +11,9 @@ use crate::{Error, RequiredVersion}; /// # [4.3.2.6. EXT-X-PROGRAM-DATE-TIME] /// /// The [`ExtXProgramDateTime`] tag associates the first sample of a -/// [`Media Segment`] with an absolute date and/or time. +/// [`MediaSegment`] with an absolute date and/or time. /// -/// [`Media Segment`]: crate::MediaSegment +/// [`MediaSegment`]: crate::MediaSegment /// [4.3.2.6. EXT-X-PROGRAM-DATE-TIME]: /// https://tools.ietf.org/html/rfc8216#section-4.3.2.6 #[derive(Deref, DerefMut, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] diff --git a/src/tags/shared/mod.rs b/src/tags/shared/mod.rs index 052f0eb..956efe7 100644 --- a/src/tags/shared/mod.rs +++ b/src/tags/shared/mod.rs @@ -1,14 +1,3 @@ -//! The tags in this section can appear in either [`MasterPlaylist`]s or -//! [`MediaPlaylist`]s. If one of these tags appears in a [`MasterPlaylist`], -//! it should not appear in any [`MediaPlaylist`] referenced by that -//! [`MasterPlaylist`]. A tag that appears in both must have the same value; -//! otherwise, clients should ignore the value in the [`MediaPlaylist`](s). -//! -//! These tags must not appear more than once in a Playlist. If a tag -//! appears more than once, clients must fail to parse the Playlist. -//! -//! [`MediaPlaylist`]: crate::MediaPlaylist -//! [`MasterPlaylist`]: crate::MasterPlaylist mod independent_segments; mod start; diff --git a/src/types/stream_data.rs b/src/types/stream_data.rs index 8aad849..f8f03e0 100644 --- a/src/types/stream_data.rs +++ b/src/types/stream_data.rs @@ -5,9 +5,9 @@ use derive_builder::Builder; use shorthand::ShortHand; use crate::attribute::AttributePairs; -use crate::types::{HdcpLevel, Resolution}; +use crate::types::{HdcpLevel, ProtocolVersion, Resolution}; use crate::utils::{quote, unquote}; -use crate::Error; +use crate::{Error, RequiredVersion}; /// The [`StreamData`] struct contains the data that is shared between both /// variants of the [`VariantStream`]. @@ -15,7 +15,7 @@ use crate::Error; /// [`VariantStream`]: crate::tags::VariantStream #[derive(ShortHand, Builder, PartialOrd, Debug, Clone, PartialEq, Eq, Hash, Ord)] #[builder(setter(strip_option))] -#[builder(derive(Debug, PartialEq))] +#[builder(derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash))] #[shorthand(enable(must_use, into))] pub struct StreamData { /// The peak segment bitrate of the [`VariantStream`] in bits per second. @@ -324,6 +324,19 @@ impl FromStr for StreamData { } } +/// This struct requires [`ProtocolVersion::V1`]. +impl RequiredVersion for StreamData { + fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 } + + fn introduced_version(&self) -> ProtocolVersion { + if self.video.is_some() { + ProtocolVersion::V4 + } else { + ProtocolVersion::V1 + } + } +} + #[cfg(test)] mod tests { use super::*;