audio codec dequeueInputBuffer returns -1 on device

隐身守侯 提交于 2019-12-25 05:27:40

问题


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.


回答1:


As far as I read your code, you only try to dequeue output buffers once every time you manage to queue one input buffer.

Keep in mind that the decoder runs asynchronously, and has got a limited number of input and output buffers. Even if the decoder does decode your input buffer, the output may not be available in an output buffer immediately. Increasing the TIMEOUT value in this case could maybe help, but it's not guaranteed.

Try splitting your two if statements, so that you call dequeueOutputBuffer every time around in the loop, even if dequeueInputBuffer didn't return any buffer.



来源:https://stackoverflow.com/questions/28889837/audio-codec-dequeueinputbuffer-returns-1-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!