saki4510t / ScreenRecordingSample

Simultaneous audio and screen recording sample using MediaCodec/MediaMuxer/VirtualDisplay
Apache License 2.0
189 stars 89 forks source link

IllegalStateException on some devices on drain dequeueOutputBuffer #4

Open RazorPT opened 8 years ago

RazorPT commented 8 years ago

I am having this kind of crash on some devices it only happens sometimes not too often i have tried to debug the problem to no avail, so i am asking here if you any idea what might be going on?

Fatal Exception: java.lang.IllegalStateException android.media.MediaCodec.native_dequeueOutputBuffer (MediaCodec.java) android.media.MediaCodec.dequeueOutputBuffer (MediaCodec.java:1033) com.serenegiant.media.MediaEncoder.drain (MediaEncoder.java:331) com.serenegiant.media.MediaEncoder.run (MediaEncoder.java:160)

Here is the line code that is throwing the exception: encoderStatus = mMediaCodec.dequeueOutputBuffer(mBufferInfo, TIMEOUT_USEC);

Thanks for your time and for developing this sample :+1:

Edit: The few times i noticed this bug was on S4 while recording Clash of Clans at 720p with 8Mpbs and some other devices like HTC One. Maybe high bitrate might trigger the problem?

saki4510t commented 8 years ago

Hi,

The error will occur when trying to dequeue output buffer before MediaCodec encoder correctly initialize/failed to initialize. I assume drain will be called before any calling of encode

I assume it will be able to ignore when it occur once on start recording(just surrounding try-catch block). But I assume it will be better to add flag to show whether the encoder is already initialized or wait it's initialize at top of run method.

saki

RazorPT commented 8 years ago

Hum the thing is if i remember the test i did sometime ago is that the app crash after 1 minute or so. But by your explanation it should crash right at the beggining the drain was called for the first time no?

Also while using MediaRecorder api for recording sometimes the recording app can get killed if the the phone is getting low on resources and since the recording app is a background app android will most likely kill the app if its under stress could this scenario cause this exception?

Thanks for your time!

saki4510t commented 8 years ago

Well as I know MediaCodec usually raises IllegalStateException when start encoding/decoding or when stop encoding/decoding(as a result that the instance is not ready, in illegal state). But as a reference document says, unfortunately almost all method call of MediaCodec will raise IllegalStateException. The exception only shows the MediaCodec instance can not execute the request by some reasons. So as you pointed out, IllegalStateException may be raised under such stressed condition.

saki