I'm trying to produce short sequential mp4 files from CameraPreview data via MediaCodec.createInputSurface()
. However, recreating the MediaCodec
and it's associated Surface requires stopping the Camera to allow another call to mCamera.setPreviewTexture(...)
. This delay results in an unacceptable amount of dropped frames.
Therefore I need to generate the CODEC_CONFIG
and END_OF_STREAM
data periodically without recreating the input Surface, and thus having to call mCamera.setPreviewTexture(...)
. Is this possible assuming the MediaFormat
is unchanged?
(I'm adapting fadden's CameraToMpegTest example. My complete code is here)
Unsuccessful attempts:
Calling MediaCodec.signalEndOfInputStream()
, draining the MediaCodec
, and then calling MediaCodec.flush()
between chunks produces an IllegalStateException
on the 2nd call to MediaCodec.signalEndOfInputStream()
.
Calling MediaCodec.signalEndOfInputStream()
, draining the MediaCodec
, and then calling MediaCodec.stop(); MediaCodec.configure(...), MediaCodec.start()
between chunks without again calling MediaCodec.createInputSurface()
produces the following error:
09-30 13:12:49.889 17638-17719/x.xx.xxxx E/Surface﹕ queueBuffer: error queuing buffer to SurfaceTexture, -19
09-30 13:12:49.889 17638-17719/x.xx.xxxx E/IMGSRV﹕ :0: UnlockPostBuffer: Failed to queue buffer 0x592e1e70
09-30 13:12:49.889 17638-17719/x.xx.xxxx E/CameraToMpegTest﹕ Encoding loop exception!
09-30 13:12:49.889 17638-17719/x.xx.xxxx W/System.err﹕ java.lang.RuntimeException: eglSwapBuffers: EGL error: 0x300b
09-30 13:12:49.896 17638-17719/x.xx.xxxx W/System.err﹕ at x.xx.xxxx.ChunkedHWRecorder$CodecInputSurface.checkEglError(ChunkedHWRecorder.java:731)
09-30 13:12:49.896 17638-17719/x.xx.xxxx W/System.err﹕ at x.xx.xxxx.ChunkedHWRecorder$CodecInputSurface.swapBuffers(ChunkedHWRecorder.java:713)
09-30 13:12:49.896 17638-17719/x.xx.xxxx W/System.err﹕ at x.xx.xxxx.ChunkedHWRecorder.startRecording(ChunkedHWRecorder.java:164)
09-30 13:12:49.896 17638-17719/x.xx.xxxx W/System.err﹕ at x.xx.xxxx.HWRecorderActivity$CameraToMpegWrapper.run(HWRecorderActivity.java:76)
09-30 13:12:49.896 17638-17719/x.xx.xxxx W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
Solved Thanks fadden. The complete solution source is here.