title: MediaCodec encoder using Surface crashes inconsistently on various devices
body: <p>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 <a href="https://android.googlesource.com/platform/cts/+/jb-mr2-release/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java" rel="nofollow">DecodeEditEncodeTest</a> 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:</p>
  
  <pre><code>E/ACodec(17651):  configureCodec multi window instance fail  appPid : 17651
  E/ACodec(17651): [OMX.qcom.video.decoder.avc] configureCodec returning error -38
  E/MediaCodec(17651): Codec reported an error. (omx error 0x80001001, internalError -38)
  </code></pre>
  
  <p>The relevant code:</p>
  
  <pre><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_COLOR_FORMAT,
                  MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
          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);
          output.setMediaFormat(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);
  </code></pre>
  
  <p>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).</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>