MediaCodec.createInputSurface() throws IllegalStateException in Android emulator (Error -38)

倖福魔咒の 提交于 2019-12-13 15:27:00

问题


I have MediaMuxer:

MediaMuxer mMediaMuxer = new MediaMuxer(new File(Environment.getExternalStorageDirectory(), "video.mp4").getPath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

And code, which records video:

MediaFormat mMediaFormat = MediaFormat.createVideoFormat("video/avc", width, height);
mMediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, CodecCapabilities.COLOR_FormatSurface);
mMediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
mMediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, fps);
mMediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 10);
MediaCodec mMediaCodec = MediaCodec.createEncoderByType("video/avc");
mMediaCodec.configure(mMediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
Surface mSurface = mMediaCodec.createInputSurface();
...

Last string mMediaCodec.createInputSurface(), throws me java.lang.IllegalStateException. Stack trace:

java.lang.IllegalStateException
    at android.media.MediaCodec.createInputSurface(Native Method)
    at com.mypackage.Cam$Recorder$VideoRecorder.run(Cam.java:307)

And logs of emulator:

02-05 07:42:55.024: I/OMXClient(2643): Using client-side OMX mux.
02-05 07:42:55.024: I/SoftAVCEncoder(2643): Construct SoftAVCEncoder
02-05 07:42:55.024: E/SoftAVCEncoder(2643): internalSetParameter: StoreMetadataInBuffersParams.nPortIndex not zero!
02-05 07:42:55.024: E/OMXNodeInstance(2643): OMX_SetParameter() failed for StoreMetaDataInBuffers: 0x80001001
02-05 07:42:55.024: E/ACodec(2643): [OMX.google.h264.encoder] storeMetaDataInBuffers (output) failed w/ err -2147483648
02-05 07:42:55.024: I/ACodec(2643): setupVideoEncoder succeeded
02-05 07:42:55.024: E/OMXNodeInstance(2643): createInputSurface requires COLOR_FormatSurface (AndroidOpaque) color format
02-05 07:42:55.024: E/ACodec(2643): [OMX.google.h264.encoder] onCreateInputSurface returning error -38
02-05 07:42:55.024: W/MediaCodec(2643): createInputSurface failed, err=-38
02-05 07:42:55.024: E/TAG(2643): java.lang.IllegalStateException
02-05 07:42:55.224: I/OMXClient(2643): Using client-side OMX mux.
02-05 07:42:55.254: W/SoftAACEncoder2(2643): Requested bitrate 128000 unsupported, using 48000

I observed this string

E/OMXNodeInstance(2643): createInputSurface requires COLOR_FormatSurface (AndroidOpaque) color format

says my Surface must be in COLOR_FormatSurface, but I have already set it

mMediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, CodecCapabilities.COLOR_FormatSurface);

What's wrong? How to solve this problem? Help.


回答1:


I had the same problem.
I think it won't work on emulators : https://github.com/google/grafika/issues/2. In the AVD I set 'Emulated performance' to 'Hardware', but it didn't solve the problem. You should try on a real device. EDIT : well, it seems to work on emulators with API >= 23 (even though your code should run with API 18), but you probably will have to change the directory during your tests, because of : https://developer.android.com/training/permissions/requesting.html



来源:https://stackoverflow.com/questions/42052567/mediacodec-createinputsurface-throws-illegalstateexception-in-android-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!