Expand description
This crate provides bindings to the MediaCodec APIs in the Android NDK.
Examples:
§Decoding
ⓘ
use mediacodec::{Frame, MediaCodec, MediaExtractor, SampleFormat, VideoFrame};
let mut extractor = MediaExtractor::from_url("/path/to/a/resource").unwrap();
debug!("Track count: {}", extractor.track_count());
let mut decoders = vec![];
for i in 0..extractor.track_count() {
let format = extractor.track_format(i).unwrap();
debug!("{}", format.to_string());
let mime_type = format.get_string("mime").unwrap();
let mut codec = MediaCodec::create_decoder(&mime_type).unwrap();
codec.init(&format, None, 0).unwrap();
codec.start().unwrap();
decoders.push(codec);
extractor.select_track(i);
}
while extractor.has_next() {
// 1. Get the track index
let index = extractor.track_index();
if index < 0 {
break;
}
let codec = &mut decoders[index as usize];
// Fetch the codec's input buffer
while let Ok(mut buffer) = codec.dequeue_input() {
if !extractor.read_next(&mut buffer) {
debug!(
"MediaExtractor.read_next() returned false! has_next(): {}",
extractor.has_next()
);
break; // VERY IMPORTANT, there's nothing else to DO, so break!!!
}
// When the buffer gets dropped (here), the buffer will be queued back to MediaCodec
// And we don't have to do anything else
}
// Check for output
let output_fmt = codec.output_format().unwrap();
while let Ok(mut buffer) = codec.dequeue_output() {
if let Some(ref frame) = buffer.frame() {
match frame {
Frame::Audio(value) => match value.format() {
SampleFormat::S16(_) => {
// Do something with the audio frame
}
SampleFormat::F32(_) => {
// Do something with the audio frame
}
},
Frame::Video(value) => match value {
VideoFrame::Hardware => {
// Nothing TODO. The frame will be rendered
}
VideoFrame::RawFrame(_) => {
// Read out the raw buffers or something
}
},
}
}
// Set the buffer to render when dropped. Only applicable to video codecs that have a hardware buffer (i.e, attached to a native window)
buffer.set_render(true);
}
}
§Demuxing
ⓘ
use log::debug;
use mediacodec::{Frame, MediaExtractor, SampleFormat, VideoFrame};
let mut extractor = MediaExtractor::from_url("/path/to/a/resource").unwrap();
debug!("Track count: {}", extractor.track_count());
for i in 0..extractor.track_count() {
let format = extractor.track_format(i).unwrap();
debug!("{}", format.to_string());
let mime_type = format.get_string("mime").unwrap();
extractor.select_track(i);
}
while extractor.has_next() {
// 1. Get the track index
let index = extractor.track_index();
if index < 0 {
break;
}
// Get a codec buffer and read data into it
}
Structs§
- AMedia
Codec Crypto Info - AMedia
Crypto - AMedia
Format - AMedia
Muxer - ANative
Window - Represents an image buffer (or a Surface in Java)
- ARect
- Audio
Frame - Represents an audio frame, with sample format and channels
- Buffer
Info - Codec
Input Buffer - This represents a buffer returned by mediacodec’s input This buffer should be filled with input data depending on whether the codec is an encoder or decoder
- Codec
Output Buffer - Represents a mediacodec output buffer
- Crypto
Info Pattern - Media
Codec - The MediaCodec structure itself.
- Media
Extractor - MediaExtractor is a demuxer that opens a file or resource and demuxes the data to hand over to MediaCodec
- Media
Format - This structure stores data in key-value pairs for use in MediaCodec and other places in the NDK
- Media
Muxer - The Type-Safe wrapper for
AMediaMuxer
. - Native
Window - Native
Window Buffer - RawVideo
Frame - A raw video frame with pixel format and a byte buffer to read the data
Enums§
- Buffer
Flag - Crypto
Info Mode - Frame
- Represents a codec frame (either audio or video)
- Info
Flag - Media
Status - Native
Window Format - Window Formats
- Native
Window Transform - Output
Format - Sample
Format - Represents an audio sample format, and contains the samples buffer
- Video
Frame
Constants§
Functions§
- AMedia
Muxer_ ⚠addTrack - Since: API 21
- AMedia
Muxer_ ⚠delete - Since: API 21
- AMedia
Muxer_ ⚠new - Since: API 21
- AMedia
Muxer_ ⚠setLocation - Since: API 21
- AMedia
Muxer_ ⚠setOrientation Hint - Since: API 21
- AMedia
Muxer_ ⚠start - Since: API 21
- AMedia
Muxer_ ⚠stop - Since: API 21
- AMedia
Muxer_ ⚠write Sample Data - Since: API 21