I'm trying to play some audio taken from an MPEG2 transport (.ts) file. I'm getting the audio stream via MediaExtractor, using readSampleData to copy samples to MediaCodec input buffers, then writing the MediaCodec output buffer to an AudioTrack. MediaExtractor indicates the audio MIME type as "audio/mp4a-latm".
All this works wonderfully on a Nexus 7 2013 tablet.
But it does not work at all on an inexpensive Neutab x7 tablet. The problem is that after a few samples, dequeueInputBuffer repeatedly returns -1. Commenting out the call to AudioTrack.write() does not change that fact, so the issue is with the operation of MediaCodec.
The code looks like (logging, some error handling omitted):
audioInputBufferIndex = audioCodec.dequeueInputBuffer(TIMEOUT);
if (audioInputBufferIndex >= 0) {
audioInputBuffers[audioInputBufferIndex].clear();
sampleSize = audioExtractor.readSampleData(audioInputBuffers[audioInputBufferIndex], 0);
audioPresentationTimeUs = audioExtractor.getSampleTime();
audioCodec.queueInputBuffer(audioInputBufferIndex,0,sampleSize,audioPresentationTimeUs,0);
audioOutputBufferIndex = audioCodec.dequeueOutputBuffer(audioBufferInfo, TIMEOUT);
if (audioOutputBufferIndex >= 0) {
ByteBuffer buffer = audioOutputBuffers[audioOutputBufferIndex];
byte[] chunk = new byte[audioBufferInfo.size];
buffer.get(chunk);
buffer.clear();
if (audioTrack != null & chunk.length > 0) {
audioTrack.write(chunk, 0, chunk.length);
audioExtractor.advance();
}
audioCodec.releaseOutputBuffer(audioOutputBufferIndex,false);
The audioCodec
is configured with a MediaFormat obtained from audioExtractor
. I've tried increasing the TIMEOUT to large values, no joy.
Why would this code behave differently on these two devices? Any knobs to turn?
There is a "Videos" app on the Neutab that plays this file just fine.Logcat reveals that it's also using MediaExtractor and AudioTrack but not, appparently, MediaCodec. I see Omx* entries in logcat, so maybe it's using libstagefright directly.