MediaCodec.dequeueOutputBuffer taking very long when encoding h264 on Android

后端 未结 1 1804
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 19:09

I\'m trying to encode h264 video on Android for real-time video streaming using MediaCodec but dequeueOutputBuffer keeps taking very long (actually it\'s very fast sometimes

相关标签:
1条回答
  • 2021-01-19 19:33

    Yes, there is something wrong with your code - you are waiting synchronously for the current frame to be output from the encoder before proceeding with the next frame. Most hardware codecs have a bit more latency than you would expect, and in order to get the proper throughput as the encoder is capable of, you need to use it asynchronously.

    That is, after sending one input buffer for encoding, you should not wait for the encoded output buffer, but only check if there is output. You should then go on and input the next buffer, and again check for any available output. Only once you don't get an input buffer immediately, you can start waiting for output. This way, there's always more than one input buffer available for the encoder to start working on, to keep it busy to actually achieve the frame rate that it is capable of.

    (If you are ok with requiring Android 5.0, you could take a look at MediaCodec.setCallback, which makes it easier to work with asynchronously.)

    There are even some codecs (mainly decoders though, if my memory serves me correctly) that won't even output the first buffer until you have passed more than a few input buffers.

    0 讨论(0)
提交回复
热议问题