Skip to main content

CencDecrypter

Struct CencDecrypter 

Source
pub struct CencDecrypter { /* private fields */ }
Available on (crate features decrypt-cenc or decrypt-hls) and crate feature decrypt-cenc only.
Expand description

A decrypter for Common Encryption (CENC) protected ISO Base Media File Format (ISOBMFF) streams.

CencDecrypter provides utilities to decrypt fragmented MP4 files or individual fragments encrypted using common encryption schemes (e.g., cenc, cbcs, cens, cbc1).

Implementations§

Source§

impl CencDecrypter

Source

pub fn new(key: &str) -> Result<Self>

Creates a new CencDecrypter with a 16-byte decryption key.

The key must be a hexadecimal string representing the 16-byte decryption key.

§Errors

Returns an error if the hex string is invalid or if the decoded key is not exactly 16 bytes.

Source

pub fn with_init(key: &str, init: &[u8]) -> Result<Self>

Creates a new CencDecrypter with a decryption key and pre-parse initialization data.

The initialization data (init) represents the MP4 metadata/header (e.g., moov box), which is parsed to extract the track encryption (tenc) and scheme type (schm) parameters.

§Errors

Returns an error if the key is invalid or if parsing the initialization data fails.

Source

pub fn decrypt_fragment( &self, input: Vec<u8>, init: Option<&[u8]>, ) -> Result<Vec<u8>>

Decrypts a single MP4 fragment in-place and returns the decrypted fragment.

A fragment typically consists of a movie fragment box (moof) followed by a media data box (mdat).

If init is provided, its track encryption parameters are parsed and used for decryption. Otherwise, cached parameters are used. If both are missing, it attempts to parse the track encryption parameters directly from the input fragment.

§Limitations
  • Single-Track Assumption: The function assumes that the fragment contains media data for a single track, or that all encrypted tracks share the same encryption parameters. It does not differentiate between tracks using track IDs (e.g., track_id in tenc, traf, or tfhd boxes is ignored), and applies the parsed track encryption parameters to all samples sequentially.
§Errors

Returns an error if parsing the fragment, parsing the initialization data, or decryption fails.

Source

pub fn decrypt_stream<R: Read, W: Write>( &mut self, reader: &mut R, writer: &mut W, init: Option<&[u8]>, ) -> Result<()>

Decrypts a fragmented MP4 stream from a reader and writes the decrypted output to a writer.

If init is provided, it is parsed for track encryption parameters. Otherwise, the decrypter will read the initialization data from the beginning of the stream.

§Limitations
  • Fragment Box Order: It expects each fragment to consist of a moof box followed eventually by an mdat box. While typical for DASH/HLS fragmented streams, streams with complex interleaving or out-of-order boxes might not be handled correctly.
  • Unfragmented (Progressive) Streams: Only fragmented MP4 streams or individual fragments are decrypted. If an unfragmented MP4 stream is processed (indicated by the absence of a moof box), it is written to the output unmodified without attempting decryption.
§Errors

Returns an error if reading from the stream, parsing metadata, decrypting, or writing fails.

Trait Implementations§

Source§

impl Clone for CencDecrypter

Source§

fn clone(&self) -> CencDecrypter

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.