CameraToMpegTest.java does not work, ends with IllegalStateException: cannot be stopped due to improper state

I have a problem with CameraToMpegTest.java from http://bigflake.com/mediacodec/ . When I run it from Activity:

public class MyActivity extends Activity { private CameraToMpegTest ctmt = new CameraToMpegTest(); ... @Override protected void onResume() { super.onResume(); try { ctmt.testEncodeCameraToMp4(); } catch (Throwable throwable) { throwable.printStackTrace(); } } ... 

I finish this 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 

After some testing, I tried to surround the call to mStManager.awaitNewImage (); with a try-catch block with exception printing, and I got the following:

 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/MPEG4Writer0-duration samples found: 1 W/MPEG4Writer0-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/MPEG4Writer0 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. 

There seems to be a similar problem like in this question ( android: SurfaceTexure, camera frame timeout ), but CameraToMpegTest.java uses a separate thread.

My testing device is Google Nexus 7 (2012) 32GB 3G, running on 4.4.4 Android.

Thank you for your help.

+5
source share
1 answer

The MediaMuxer class will throw "cannot stop" exceptions if you started it, but haven’t submitted any data to it yet. It doesn't make much sense, but how it works.

The root problem is that there is no data that usually results from the problem you identified (the SurfaceTexture quirk described in detail in this answer ), you need to make sure that the thread doing the work does not have a Looper.

You might be better off using Grafika as a sample code. Actions such as continuous capture demonstrate the camera’s path to MPEG in the application environment and not in the CTS test environment.

+6
source

Source: https://habr.com/ru/post/1205184/


All Articles