Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the new MediaCodec getInputBuffer(int index)

I have an example that I am working from for decoding mp3 audio with MediaCodec for playing using MediaPlayer and AudioTrack. The example is using getInputBuffers() which is now depricated in API 21+. The new getInputBuffer(int index) returns one buffer instead of an array and the API reference for the MediaCodec still shows the use case for getInputBuffers().

Can anyone explain how I need to go about using the new method? Do I just get the index 0 each time? I started to loop and get each and make an array but there isn't a place, that I have seen, where I can get the length of available buffers.

like image 309
StewVanB Avatar asked Jul 15 '15 00:07

StewVanB


People also ask

How does MediaCodec work?

It processes data asynchronously and uses a set of input and output buffers. At a simplistic level, you request (or receive) an empty input buffer, fill it up with data and send it to the codec for processing. The codec uses up the data and transforms it into one of its empty output buffers.

What is Android MediaCodec?

MediaCodec class can be used to access low-level media codecs, i.e. encoder/decoder components. It is part of the Android low-level multimedia support infrastructure (normally used together with MediaExtractor , MediaSync , MediaMuxer , MediaCrypto , MediaDrm , Image , Surface , and AudioTrack .)


1 Answers

You shouldn't try to fetch all the buffers.

Prior to API 21, you'd do ByteBuffer inputs[] = codec.getInputBuffers(). Then index = codec.dequeueInputBuffer() would return a buffer index, and you'd use inputs[index], and finally submit the buffer with codec.queueInputBuffer(index, ...).

Notice that you never touch more than one element in inputs[] at a time, and you're only allowed to touch that element between the dequeueInputBuffer and queueInputBuffer calls.

Now instead of having an array of ByteBuffer objects, where you only use one at a time, you're now supposed to fetch only the single ByteBuffer you are going to fill. That is, after index = codec.dequeueInputBuffer(), instead of using inputs[index], call codec.getInputBuffer(index).

So basically, the array that you used to get returned from getInputBuffers() still exists internally within the MediaCodec object, but you don't need to keep the full array available, only fetch the single buffer you're going to use each time.

like image 189
mstorsjo Avatar answered Sep 18 '22 07:09

mstorsjo