Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent java.lang.IllegalStateException generated by MediaCodec.dequeueInputBuffer

The following code generates java.lang.IllegalStateException once in a while:

 int iInputBufIndex = _mcDecoder.dequeueInputBuffer(TIMEOUT_USEC);

where _mcDecoder is an instance of MediaCodec. The decoded stream is H.264 video. The code works well most of the time. Is there a way to check the state of MediaCodec proactively to avoid this exception?

The stack trace:

java.lang.IllegalStateException
    at android.media.MediaCodec.dequeueInputBuffer(Native Method)
at net.mydomain.android.MYNAMESPACE.MYCLASS.MyMethod0(Unknown Source)
at net.mydomain.android.MYNAMESPACE.MYCLASS.ProcessH264(Unknown Source)
at net.mydomain.android.MYNAMESPACE.MYCLASS.MyMethod1(Unknown Source)
at net.mydomain.android.MYNAMESPACE.MYCLASS.MyMethod2(Unknown Source)
at net.mydomain.android.MYNAMESPACE.MYCLASS.MyMethod3(Unknown Source)
at net.mydomain.android.MYNAMESPACE.MYCLASS.access$5(Unknown Source)
at net.mydomain.android.MYNAMESPACE.MYCLASS$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:841)
like image 747
Hong Avatar asked Aug 17 '14 18:08

Hong


1 Answers

This exception is thrown if the codec internally has returned an error. This can e.g. be caused by an erroneous bitstream (although the decoders mostly should handle that I think) or perhaps by passing packets incorrectly. (Trying to decode interlaced H.264 video via MediaCodec on modern Qualcomm chipsets also causes the object to get into the error state, see e.g. https://code.google.com/p/android/issues/detail?id=72336 - and this one is particularly nasty because it crashes the whole process when trying to shut down the codec.)

There's no way of knowing that this has happened (other than perhaps reading the system log) other than noticing the exceptions. (Perhaps that'd be a good addition as a new API?) I'm not quite sure if it ever works to just keep on trying to feed the decoder if it would manage to recover, or if it's a lost cause and the whole decoder should be closed.

like image 107
mstorsjo Avatar answered Oct 04 '22 22:10

mstorsjo