pub struct CencDecrypter { /* private fields */ }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
impl CencDecrypter
Sourcepub fn new(key: &str) -> Result<Self>
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.
Sourcepub fn with_init(key: &str, init: &[u8]) -> Result<Self>
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.
Sourcepub fn decrypt_fragment(
&self,
input: Vec<u8>,
init: Option<&[u8]>,
) -> Result<Vec<u8>>
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_idintenc,traf, ortfhdboxes 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.
Sourcepub fn decrypt_stream<R: Read, W: Write>(
&mut self,
reader: &mut R,
writer: &mut W,
init: Option<&[u8]>,
) -> Result<()>
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
moofbox followed eventually by anmdatbox. 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
moofbox), 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
impl Clone for CencDecrypter
Source§fn clone(&self) -> CencDecrypter
fn clone(&self) -> CencDecrypter
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more