MediaCodec encoder using Surface crashes inconsistently on various devices


I’m developing an app that post processes video by applying graphics and text to the video. My code is based on the Android CTS test DecodeEditEncodeTest and it works great on the Nexus 4 and Nexus 5 (4.4) but not on any other device I’ve tried; not even the Nexus 7 II on 4.4. For example, on a Galaxy S3, I get the following errors:

E/ACodec(17651):  configureCodec multi window instance fail  appPid : 17651
E/ACodec(17651): [] configureCodec returning error -38
E/MediaCodec(17651): Codec reported an error. (omx error 0x80001001, internalError -38)

The relevant code:

        MediaCodecInfo codecInfo = selectCodec(MIME_TYPE);
        if (codecInfo == null) {
            // Don't fail CTS if they don't have an AVC codec (not here, anyway).
            Log.e(TAG, "Unable to find an appropriate codec for " + MIME_TYPE);
            return false;
        if (VERBOSE) Log.d(TAG, "found codec: " + codecInfo.getName());

        // We avoid the device-specific limitations on width and height by using values that
        // are multiples of 16, which all tested devices seem to be able to handle.
        MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight);

        // Set some properties.  Failing to specify some of these can cause the MediaCodec
        // configure() call to throw an unhelpful exception.
        format.setInteger(MediaFormat.KEY_BIT_RATE, mBitRate);
        format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
        format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
        if (VERBOSE) Log.d(TAG, "format: " + format);

        // Create a MediaCodec for the desired codec, then configure it as an encoder with
        // our desired properties.
        encoder = MediaCodec.createByCodecName(codecInfo.getName());
        encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

It fails in different ways on other devices; I’ve tried Nexus 7 II, the G3, and HTC One (which didn’t fail but created a garbage video).

Based on this error, it seems that the system is unhappy about the fact that the above code is executed from a Fragment that is displaying the original video using a MediaPlayer and SurfaceView.

I’d like to keep this view visible, so I tried to reset() and destroy() the MediaPlayer and in fact this made the app work correctly on the Nexus 7, but still not on the G3 nor the HTC One.

Is there something else I need to release? Or am I forced to destroy the fragment and use a different fragment for the post processing?


The answer is to not go over 720p on both input and output videos

Answered By – pstoppani

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published