Rust MediaCodec
This library provides Rust bindings to the Android MediaCodec APIs. It also adds some pretty nifty utilities to make working with buffers on Android easier.
Features Currently Implemented
Some Decoding example:
use log::debug;
use mediacodec::{Frame, MediaCodec, MediaExtractor, SampleFormat, VideoFrame};
#[no_mangle]
extern "C" fn process() {
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() {
let index = extractor.track_index();
if index < 0 {
break;
}
let codec = &mut decoders[index as usize];
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; }
}
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(_) => {
}
SampleFormat::F32(_) => {
}
},
Frame::Video(value) => match value {
VideoFrame::Hardware => {
}
VideoFrame::RawFrame(_) => {
}
},
}
}
buffer.set_render(true);
}
}
}
You can find some more examples in the examples directory.