Search code examples
androidandroid-mediacodec

MediaCodec returns -1,-2,-3 in first seconds


I use MediaCodec to decode video data, but when I use dequeueOutputBuffer method ,get the result MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED,MediaCodec.INFO_OUTPUT_FORMAT_CHANGED and MediaCodec.INFO_TRY_AGAIN_LATER in the first seconds.I want get the normal result of it as soon as I use this method.

String mime = format.getString(MediaFormat.KEY_MIME);
mMediaCodec = MediaCodec.createDecoderByType(mime);
mMediaCodec.configure(format, mSurface , null , 0 );
mMediaCodec.start();
try{
    ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();
    ByteBuffer[] outputBuffers = mMediaCodec.getOutputBuffers();
    int inputBufferIndex = mMediaCodec.dequeueInputBuffer(TLMediaCodec.TIMEOUT_USEC);
    if (inputBufferIndex >= 0) {
        ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
        inputBuffer.clear();
        inputBuffer.put(data);
        mMediaCodec.queueInputBuffer(inputBufferIndex, 0, data.length, ultimestamp, 0);
        int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(mBufferInfo, 100);

        if (outputBufferIndex >= 0)
        {
            try
            {
                mVideoDecodeDelay = System.currentTimeMillis() - mVideoDecodeDelay;
                boolean render = mBufferInfo.size > 0;
                mMediaCodec.releaseOutputBuffer(outputBufferIndex, render);
            }
            catch (Exception e)
            {
                TLLogger.trace(TAG, "decodeOutput failed -- > " + e.toString());
                e.printStackTrace();
            }
        }
        else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED)
        {
            outputBuffers = mMediaCodec.getOutputBuffers();
        }
        else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED)
        {
            MediaFormat format = mMediaCodec.getOutputFormat();
        }
    }
} catch{
    e.printStackTrace();
}

above is my code,I could not find any dismiss to improve this problem. Thank you for help!!!


Solution

  • That's normal. You don't send one frame into MediaCodec and get a frame out; you pass a series of frames in and pull them out as they become available. Some latency between start() and the availability of the first frame is expected (e.g. this). If you would rather be notified than poll, try the newer asynchronous API methods.

    See the "basic usage" notes near the top of the bigflake page.