CameraToMpegTest.java无法正常工作,以IllegalStateException结束:由于状态错误而无法停止

时间:2014-10-21 13:23:54

标签: java android mediacodec

我从http://bigflake.com/mediacodec/运行CameraToMpegTest.java时遇到问题。当我从活动开始时:

public class MyActivity extends Activity {
    private CameraToMpegTest ctmt  = new CameraToMpegTest();

    ...

    @Override
    protected void onResume() {
        super.onResume();

        try {
            ctmt.testEncodeCameraToMp4();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

    }

    ...      

我以这个logcat结束:

D/CameraToMpegTest﹕ video/avc output 640x480 @6000000
D/CameraToMpegTest﹕ Camera preview size is 640x480
I/OMXClient﹕ Using client-side OMX mux.
E/ACodec﹕ [OMX.Nvidia.h264.encoder] storeMetaDataInBuffers (output) failed w/ err -2147483648
I/ACodec﹕ setupVideoEncoder succeeded
D/libEGL﹕ loaded /system/lib/egl/libEGL_tegra.so
D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_tegra.so
D/libEGL﹕ loaded /system/lib/egl/libGLESv2_tegra.so
I/CameraToMpegTest﹕ Output file is /storage/emulated/0/test.640x480.mp4
W/System.err﹕ java.lang.IllegalStateException: Can't stop due to wrong state.
W/System.err﹕ at android.media.MediaMuxer.stop(MediaMuxer.java:229)
W/System.err﹕ at CameraToMpegTest.releaseEncoder(CameraToMpegTest.java:395)
W/System.err﹕ at CameraToMpegTest.encodeCameraToMpeg(CameraToMpegTest.java:216)
W/System.err﹕ at CameraToMpegTest.access$000(CameraToMpegTest.java:68)
W/System.err﹕ at CameraToMpegTest$CameraToMpegWrapper.run(CameraToMpegTest.java:128)
W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
D/OpenGLRenderer﹕ Enabling debug mode 0

经过一些测试,我试图围绕调用mStManager.awaitNewImage();使用带有异常打印的try-catch块,我得到了这个:

D/CameraToMpegTest﹕ video/avc output 640x480 @6000000
D/CameraToMpegTest﹕ Camera preview size is 640x480
I/OMXClient﹕ Using client-side OMX mux.
E/ACodec﹕ [OMX.Nvidia.h264.encoder] storeMetaDataInBuffers (output) failed w/ err -2147483648
I/ACodec﹕ setupVideoEncoder succeeded
D/libEGL﹕ loaded /system/lib/egl/libEGL_tegra.so
D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_tegra.so
D/libEGL﹕ loaded /system/lib/egl/libGLESv2_tegra.so
I/CameraToMpegTest﹕ Output file is /storage/emulated/0/test.640x480.mp4
I/System.out﹕ java.lang.RuntimeException: Camera frame wait timed out
I/System.out﹕ java.lang.RuntimeException: Camera frame wait timed out
D/CameraToMpegTest﹕ encoder output format changed: {csd-1=java.nio.ByteArrayBuffer[position=0,limit=8,capacity=8], height=480, mime=video/avc, csd-0=java.nio.ByteArrayBuffer[position=0,limit=13,capacity=13], what=1869968451, width=640}
I/MPEG4Writer﹕ limits: 4294967295/0 bytes/us, bit rate: -1 bps and the estimated moov size 3072 bytes
I/MPEG4Writer﹕ setStartTimestampUs: 0
I/MPEG4Writer﹕ Earliest track starting time: 0
I/System.out﹕ java.lang.RuntimeException: Camera frame wait timed out
W/MPEG4Writer﹕ 0-duration samples found: 1
W/MPEG4Writer﹕ 0-duration samples found: 2
I/MPEG4Writer﹕ Received total/0-length (3/0) buffers and encoded 3 frames. - video
D/MPEG4Writer﹕ Stopping Video track
D/MPEG4Writer﹕ Stopping Video track source
D/MPEG4Writer﹕ Video track stopped
D/MPEG4Writer﹕ Stopping writer thread
D/MPEG4Writer﹕ 0 chunks are written in the last batch
D/MPEG4Writer﹕ Writer thread stopped
D/MPEG4Writer﹕ Stopping Video track
D/OpenGLRenderer﹕ Enabling debug mode 0
I/Choreographer﹕ Skipped 90 frames!  The application may be doing too much work on its main thread.

它似乎与此问题(android: SurfaceTexure, camera frame wait time out)中的类似问题相似,但CameraToMpegTest.java使用单独的线程。

我的测试设备是谷歌Nexus 7(2012)32GB 3G,运行4.4.4 Android。

感谢您的帮助。

1 个答案:

答案 0 :(得分:6)

MediaMuxer类如果已经启动它将会抛出“无法停止”异常,但尚未向其提供任何数据。这没有多大意义,但这就是它的工作原理。

根本问题是没有数据进入,这通常是由于您发现的问题(在this answer中详细描述了SurfaceTexture的一个怪癖)。您需要确保执行工作的线程没有Looper。

使用Grafika作为示例代码可能会更好。 “连续捕获”等活动演示了应用环境中的摄像机到MPEG路径,而不是CTS测试环境。