androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
Apache License 2.0
1.34k stars 315 forks source link

Can you revert the control of MediaCodec.stop() before calling MediaCodec.release()? #1497

Closed pineapplevine closed 5 days ago

pineapplevine commented 6 days ago

Hi! In a previous issue, support for retrying initCodec when the codec initialization process failed. https://github.com/google/ExoPlayer/issues/8696

https://github.com/google/ExoPlayer/commit/07352a4585cd939805bc2dafe53166f02c7e9025

media/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecRenderer.java

        try {
          initCodec(codecInfo, crypto);
        } catch (Exception e) {
          if (codecInfo == preferredCodecInfo) {
            // If creating the preferred decoder failed then sleep briefly before retrying.
            // Workaround for [internal b/191966399].
            // See also https://github.com/google/ExoPlayer/issues/8696.
            Log.w(TAG, "Preferred decoder instantiation failed. Sleeping for 50ms then retrying.");
            Thread.sleep(/* millis= */ 50);
            initCodec(codecInfo, crypto);
          } else {
            throw e;
          }
        }

The above code mean, retries codec initialization after waiting for 50ms if the codec initialization process fails. However, the 50ms wait time may depend on the performance of the device. In other commit, removed the process that called the MediaCodec.stop() before MediaCodec.release(). I think that calling MediaCodec.stop() before MediaCodec.release() will provide sufficient waiting time. https://github.com/google/ExoPlayer/commit/0e1e4ad737cafa3a166815d34ee81c3d8d2e23d4

library/core/src/main/java/com/google/android/exoplayer2/mediacodec/MediaCodecRenderer.java

        try {
          if (!skipMediaCodecStopOnRelease) {
            codec.stop();
          }
        } finally {
          codec.release();
        }
icbaker commented 6 days ago

Thanks for raising this, and the suggestion to try re-adding the MediaCodec.stop() call. I've tried this using my repro example for https://github.com/google/ExoPlayer/issues/8696 on a OnePlus AC2003 device (and removing the Thread.sleep workaround) and I agree it also resolves the issue (and removing both reintroduces it). I also agree the 50ms delay is arbitrary and unlikely to be correct for all devices (too long for some, too short for others). I will submit a change to remove the 50ms sleep and call MediaCodec.stop() before MediaCodec.release() on the affected Android versions (APIs 30, 31 & 32).