2

I want to decode datagram packets that contains H264 video with mediaDecoder. (the packets are sent from rtsp server over rtp)

I suppose I'm doing something wrong cause i'm getting -1 (that means no such buffer is currently available) from dequeueInputBuffer()

I'm searching for days but can't find suitable solution.

what is the mean of this result? what i'm doing wrong?

here is my code. please help me to get it working.

private void decodeVideo() {

    new Thread(new Runnable() {

        @Override
        public void run() {
            MediaCodec codec = MediaCodec.createDecoderByType("video/avc");
            MediaFormat mediaFormat = MediaFormat.createVideoFormat(
                    "video/avc", 640, 480);
            MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
            codec.configure(mediaFormat, mHolder.getSurface(), null, 0);
            codec.start();
            ByteBuffer[] inputBuffers = codec.getInputBuffers();
            ByteBuffer[] outputBuffers = codec.getOutputBuffers();
            while (flag) {
                int inputBufferIndex = codec.dequeueInputBuffer(10000);
                if (inputBufferIndex >= 0) {
                    while (mPackets.size() <= 0) {
                        try {
                            Log.d(TAG, "nopackets");
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    Log.d(TAG, "now I have packets!");
                    DatagramPacket currentDatagram = mPackets.remove();
                    inputBuffers[inputBufferIndex] = ByteBuffer
                            .wrap(currentDatagram.getData());

                    codec.queueInputBuffer(inputBufferIndex, 0,
                            currentDatagram.getData().length, 10000, 0);
                }
                int outputBufferIndex = codec.dequeueOutputBuffer(info,
                        10000);
                if (outputBufferIndex >= 0) {

                    codec.releaseOutputBuffer(outputBufferIndex, true);
                } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
                    outputBuffers = codec.getOutputBuffers();
                } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                    // Subsequent data will conform to new format.
                    MediaFormat format = codec.getOutputFormat();

                }
            }
            codec.stop();
            codec.release();
            codec = null;

        }
    }).start();
}

thank you for giving your time.

dvrm
  • 3,749
  • 4
  • 34
  • 43
  • I had a similar issue, and was able to fix it by decreasing the number of milliseconds between calls draining the encoder to the muxer. In my codebase both the decode and drain steps call dequeueOutputBuffer, and somehow switching screens in my activity would lock up the buffers indefinitely. – matt snider Feb 24 '15 at 03:08

2 Answers2

1

Have you tried changing the timeout parameter?, -1 means infinite:

codec.dequeueInputBuffer(-1);

Just in case an input buffer will be availabe later. Note that the timeout is in microseconds.

finlir
  • 220
  • 4
  • 11
  • I've tried to change it and didn't work, but now it's working, not 100% sure that was the problem – dvrm Jul 10 '14 at 07:51
0

I had the same issue.Maybe you can try these:

codec.dequeueInputBuffer(1000000);  //this is 1 second.

or

if (inputBufferIndex >= 0) {
    ...
}
else {
   continue;  //maybe infinite
}

I met this issue on samsung note2 android4.1. It's ok on samsung note4 android4.4. This often only happens at the time of the first frame.

qqli
  • 196
  • 2
  • 6