mirror of
https://github.com/samsonjs/media.git
synced 2026-03-26 09:35:47 +00:00
Retry creating a MediaCodec instance in MediaCodecRenderer
It's been observed that some devices fail when releasing a secure codec attached to a surface and immediately trying to create a new codec (secure or insecure) attached to the same surface. This change catches all exceptions thrown during codec creation, sleeps for a short time, and then retries the codec creation. This is observed to fix the problem (we believe this is because it allows enough time for some background part of the previous codec release operation to complete). This change should have no effect on the control flow when codec creation succeeds first time. It will introduce a slight delay when creating the preferred codec fails (while we sleep and retry), which will either delay propagating a permanent error or attempting to initialize a fallback decoder. We can't avoid the extra delay to instantiating the fallback decoder because we can't know whether we expect the second attempt to create the preferred decoder to succeed or fail. The benefit to always retrying the preferred decoder creation (fixing playback failures) outweighs the unfortunate additional delay to instantiating fallback decoders. Issue: google/ExoPlayer#8696 #minor-release PiperOrigin-RevId: 414671743
This commit is contained in:
parent
07fdf0e794
commit
97294f0693
1 changed files with 21 additions and 3 deletions
|
|
@ -978,13 +978,27 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
|
|||
DecoderInitializationException.NO_SUITABLE_DECODER_ERROR);
|
||||
}
|
||||
|
||||
MediaCodecInfo preferredCodecInfo = availableCodecInfos.peekFirst();
|
||||
while (codec == null) {
|
||||
MediaCodecInfo codecInfo = availableCodecInfos.peekFirst();
|
||||
if (!shouldInitCodec(codecInfo)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
initCodec(codecInfo, crypto);
|
||||
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;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "Failed to initialize decoder: " + codecInfo, e);
|
||||
// This codec failed to initialize, so fall back to the next codec in the list (if any). We
|
||||
|
|
@ -1062,13 +1076,17 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
|
|||
codecOperatingRate = CODEC_OPERATING_RATE_UNSET;
|
||||
}
|
||||
codecInitializingTimestamp = SystemClock.elapsedRealtime();
|
||||
TraceUtil.beginSection("createCodec:" + codecName);
|
||||
MediaCodecAdapter.Configuration configuration =
|
||||
getMediaCodecConfiguration(codecInfo, inputFormat, crypto, codecOperatingRate);
|
||||
if (Util.SDK_INT >= 31) {
|
||||
Api31.setLogSessionIdToMediaCodecFormat(configuration, getPlayerId());
|
||||
}
|
||||
codec = codecAdapterFactory.createAdapter(configuration);
|
||||
try {
|
||||
TraceUtil.beginSection("createCodec:" + codecName);
|
||||
codec = codecAdapterFactory.createAdapter(configuration);
|
||||
} finally {
|
||||
TraceUtil.endSection();
|
||||
}
|
||||
codecInitializedTimestamp = SystemClock.elapsedRealtime();
|
||||
|
||||
this.codecInfo = codecInfo;
|
||||
|
|
|
|||
Loading…
Reference in a new issue