package androidx.media3.exoplayer.mediacodec; import android.media.MediaCodec; import android.media.MediaCrypto; import android.media.MediaFormat; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.PersistableBundle; import android.view.Surface; import androidx.media3.common.util.TraceUtil; import androidx.media3.decoder.CryptoInfo; import androidx.media3.exoplayer.mediacodec.AsynchronousMediaCodecAdapter; import androidx.media3.exoplayer.mediacodec.MediaCodecAdapter; import com.google.common.base.Supplier; import java.io.IOException; import java.nio.ByteBuffer; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public final class AsynchronousMediaCodecAdapter implements MediaCodecAdapter { private static final int STATE_CREATED = 0; private static final int STATE_INITIALIZED = 1; private static final int STATE_SHUT_DOWN = 2; private final AsynchronousMediaCodecCallback asynchronousMediaCodecCallback; private final AsynchronousMediaCodecBufferEnqueuer bufferEnqueuer; private final MediaCodec codec; private boolean codecReleased; private int state; private final boolean synchronizeCodecInteractionsWithQueueing; @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public boolean needsReconfiguration() { return false; } /* loaded from: classes2.dex */ public static final class Factory implements MediaCodecAdapter.Factory { private final Supplier callbackThreadSupplier; private final Supplier queueingThreadSupplier; private final boolean synchronizeCodecInteractionsWithQueueing; public Factory(final int i, boolean z) { this(new Supplier() { // from class: androidx.media3.exoplayer.mediacodec.AsynchronousMediaCodecAdapter$Factory$$ExternalSyntheticLambda0 @Override // com.google.common.base.Supplier public final Object get() { return AsynchronousMediaCodecAdapter.Factory.lambda$new$0(i); } }, new Supplier() { // from class: androidx.media3.exoplayer.mediacodec.AsynchronousMediaCodecAdapter$Factory$$ExternalSyntheticLambda1 @Override // com.google.common.base.Supplier public final Object get() { return AsynchronousMediaCodecAdapter.Factory.lambda$new$1(i); } }, z); } /* JADX INFO: Access modifiers changed from: package-private */ public static /* synthetic */ HandlerThread lambda$new$0(int i) { return new HandlerThread(AsynchronousMediaCodecAdapter.createCallbackThreadLabel(i)); } /* JADX INFO: Access modifiers changed from: package-private */ public static /* synthetic */ HandlerThread lambda$new$1(int i) { return new HandlerThread(AsynchronousMediaCodecAdapter.createQueueingThreadLabel(i)); } Factory(Supplier supplier, Supplier supplier2, boolean z) { this.callbackThreadSupplier = supplier; this.queueingThreadSupplier = supplier2; this.synchronizeCodecInteractionsWithQueueing = z; } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter.Factory public AsynchronousMediaCodecAdapter createAdapter(MediaCodecAdapter.Configuration configuration) throws IOException { MediaCodec mediaCodec; AsynchronousMediaCodecAdapter asynchronousMediaCodecAdapter; String str = configuration.codecInfo.name; AsynchronousMediaCodecAdapter asynchronousMediaCodecAdapter2 = null; try { TraceUtil.beginSection("createCodec:" + str); mediaCodec = MediaCodec.createByCodecName(str); try { asynchronousMediaCodecAdapter = new AsynchronousMediaCodecAdapter(mediaCodec, this.callbackThreadSupplier.get(), this.queueingThreadSupplier.get(), this.synchronizeCodecInteractionsWithQueueing); } catch (Exception e) { e = e; } } catch (Exception e2) { e = e2; mediaCodec = null; } try { TraceUtil.endSection(); asynchronousMediaCodecAdapter.initialize(configuration.mediaFormat, configuration.surface, configuration.crypto, configuration.flags); return asynchronousMediaCodecAdapter; } catch (Exception e3) { e = e3; asynchronousMediaCodecAdapter2 = asynchronousMediaCodecAdapter; if (asynchronousMediaCodecAdapter2 != null) { asynchronousMediaCodecAdapter2.release(); } else if (mediaCodec != null) { mediaCodec.release(); } throw e; } } } private AsynchronousMediaCodecAdapter(MediaCodec mediaCodec, HandlerThread handlerThread, HandlerThread handlerThread2, boolean z) { this.codec = mediaCodec; this.asynchronousMediaCodecCallback = new AsynchronousMediaCodecCallback(handlerThread); this.bufferEnqueuer = new AsynchronousMediaCodecBufferEnqueuer(mediaCodec, handlerThread2); this.synchronizeCodecInteractionsWithQueueing = z; this.state = 0; } /* JADX INFO: Access modifiers changed from: private */ public void initialize(MediaFormat mediaFormat, Surface surface, MediaCrypto mediaCrypto, int i) { this.asynchronousMediaCodecCallback.initialize(this.codec); TraceUtil.beginSection("configureCodec"); this.codec.configure(mediaFormat, surface, mediaCrypto, i); TraceUtil.endSection(); this.bufferEnqueuer.start(); TraceUtil.beginSection("startCodec"); this.codec.start(); TraceUtil.endSection(); this.state = 1; } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void queueInputBuffer(int i, int i2, int i3, long j, int i4) { this.bufferEnqueuer.queueInputBuffer(i, i2, i3, j, i4); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void queueSecureInputBuffer(int i, int i2, CryptoInfo cryptoInfo, long j, int i3) { this.bufferEnqueuer.queueSecureInputBuffer(i, i2, cryptoInfo, j, i3); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void releaseOutputBuffer(int i, boolean z) { this.codec.releaseOutputBuffer(i, z); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void releaseOutputBuffer(int i, long j) { this.codec.releaseOutputBuffer(i, j); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public int dequeueInputBufferIndex() { this.bufferEnqueuer.maybeThrowException(); return this.asynchronousMediaCodecCallback.dequeueInputBufferIndex(); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public int dequeueOutputBufferIndex(MediaCodec.BufferInfo bufferInfo) { this.bufferEnqueuer.maybeThrowException(); return this.asynchronousMediaCodecCallback.dequeueOutputBufferIndex(bufferInfo); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public MediaFormat getOutputFormat() { return this.asynchronousMediaCodecCallback.getOutputFormat(); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public ByteBuffer getInputBuffer(int i) { return this.codec.getInputBuffer(i); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public ByteBuffer getOutputBuffer(int i) { return this.codec.getOutputBuffer(i); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void flush() { this.bufferEnqueuer.flush(); this.codec.flush(); this.asynchronousMediaCodecCallback.flush(); this.codec.start(); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void release() { try { if (this.state == 1) { this.bufferEnqueuer.shutdown(); this.asynchronousMediaCodecCallback.shutdown(); } this.state = 2; } finally { if (!this.codecReleased) { this.codec.release(); this.codecReleased = true; } } } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void setOnFrameRenderedListener(final MediaCodecAdapter.OnFrameRenderedListener onFrameRenderedListener, Handler handler) { maybeBlockOnQueueing(); this.codec.setOnFrameRenderedListener(new MediaCodec.OnFrameRenderedListener() { // from class: androidx.media3.exoplayer.mediacodec.AsynchronousMediaCodecAdapter$$ExternalSyntheticLambda0 @Override // android.media.MediaCodec.OnFrameRenderedListener public final void onFrameRendered(MediaCodec mediaCodec, long j, long j2) { AsynchronousMediaCodecAdapter.this.m5248x4a2a5e4a(onFrameRenderedListener, mediaCodec, j, j2); } }, handler); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$setOnFrameRenderedListener$0$androidx-media3-exoplayer-mediacodec-AsynchronousMediaCodecAdapter, reason: not valid java name */ public /* synthetic */ void m5248x4a2a5e4a(MediaCodecAdapter.OnFrameRenderedListener onFrameRenderedListener, MediaCodec mediaCodec, long j, long j2) { onFrameRenderedListener.onFrameRendered(this, j, j2); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void setOutputSurface(Surface surface) { maybeBlockOnQueueing(); this.codec.setOutputSurface(surface); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void setParameters(Bundle bundle) { maybeBlockOnQueueing(); this.codec.setParameters(bundle); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public void setVideoScalingMode(int i) { maybeBlockOnQueueing(); this.codec.setVideoScalingMode(i); } @Override // androidx.media3.exoplayer.mediacodec.MediaCodecAdapter public PersistableBundle getMetrics() { maybeBlockOnQueueing(); return this.codec.getMetrics(); } void onError(MediaCodec.CodecException codecException) { this.asynchronousMediaCodecCallback.onError(this.codec, codecException); } void onOutputFormatChanged(MediaFormat mediaFormat) { this.asynchronousMediaCodecCallback.onOutputFormatChanged(this.codec, mediaFormat); } private void maybeBlockOnQueueing() { if (this.synchronizeCodecInteractionsWithQueueing) { try { this.bufferEnqueuer.waitUntilQueueingComplete(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException(e); } } } /* JADX INFO: Access modifiers changed from: private */ public static String createCallbackThreadLabel(int i) { return createThreadLabel(i, "ExoPlayer:MediaCodecAsyncAdapter:"); } /* JADX INFO: Access modifiers changed from: private */ public static String createQueueingThreadLabel(int i) { return createThreadLabel(i, "ExoPlayer:MediaCodecQueueingThread:"); } private static String createThreadLabel(int i, String str) { StringBuilder sb = new StringBuilder(str); if (i == 1) { sb.append("Audio"); } else if (i == 2) { sb.append("Video"); } else { sb.append("Unknown(").append(i).append(")"); } return sb.toString(); } }