Rabbit-R1/android (non root)/java/sources/com/google/android/exoplayer2/mediacodec/AsynchronousMediaCodecCallback.java
2024-05-21 17:08:36 -04:00

208 lines
7 KiB
Java

package com.google.android.exoplayer2.mediacodec;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Handler;
import android.os.HandlerThread;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.util.ArrayDeque;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class AsynchronousMediaCodecCallback extends MediaCodec.Callback {
private final HandlerThread callbackThread;
private MediaFormat currentFormat;
private Handler handler;
private IllegalStateException internalException;
private MediaCodec.CodecException mediaCodecException;
private long pendingFlushCount;
private MediaFormat pendingOutputFormat;
private boolean shutDown;
private final Object lock = new Object();
private final IntArrayQueue availableInputBuffers = new IntArrayQueue();
private final IntArrayQueue availableOutputBuffers = new IntArrayQueue();
private final ArrayDeque<MediaCodec.BufferInfo> bufferInfos = new ArrayDeque<>();
private final ArrayDeque<MediaFormat> formats = new ArrayDeque<>();
private boolean isFlushingOrShutdown() {
return this.pendingFlushCount > 0 || this.shutDown;
}
/* JADX INFO: Access modifiers changed from: package-private */
public AsynchronousMediaCodecCallback(HandlerThread handlerThread) {
this.callbackThread = handlerThread;
}
public void initialize(MediaCodec mediaCodec) {
Assertions.checkState(this.handler == null);
this.callbackThread.start();
Handler handler = new Handler(this.callbackThread.getLooper());
mediaCodec.setCallback(this, handler);
this.handler = handler;
}
public void shutdown() {
synchronized (this.lock) {
this.shutDown = true;
this.callbackThread.quit();
flushInternal();
}
}
public int dequeueInputBufferIndex() {
synchronized (this.lock) {
int i = -1;
if (isFlushingOrShutdown()) {
return -1;
}
maybeThrowException();
if (!this.availableInputBuffers.isEmpty()) {
i = this.availableInputBuffers.remove();
}
return i;
}
}
public int dequeueOutputBufferIndex(MediaCodec.BufferInfo bufferInfo) {
synchronized (this.lock) {
if (isFlushingOrShutdown()) {
return -1;
}
maybeThrowException();
if (this.availableOutputBuffers.isEmpty()) {
return -1;
}
int remove = this.availableOutputBuffers.remove();
if (remove >= 0) {
Assertions.checkStateNotNull(this.currentFormat);
MediaCodec.BufferInfo remove2 = this.bufferInfos.remove();
bufferInfo.set(remove2.offset, remove2.size, remove2.presentationTimeUs, remove2.flags);
} else if (remove == -2) {
this.currentFormat = this.formats.remove();
}
return remove;
}
}
public MediaFormat getOutputFormat() {
MediaFormat mediaFormat;
synchronized (this.lock) {
mediaFormat = this.currentFormat;
if (mediaFormat == null) {
throw new IllegalStateException();
}
}
return mediaFormat;
}
public void flush() {
synchronized (this.lock) {
this.pendingFlushCount++;
((Handler) Util.castNonNull(this.handler)).post(new Runnable() { // from class: com.google.android.exoplayer2.mediacodec.AsynchronousMediaCodecCallback$$ExternalSyntheticLambda0
@Override // java.lang.Runnable
public final void run() {
AsynchronousMediaCodecCallback.this.onFlushCompleted();
}
});
}
}
@Override // android.media.MediaCodec.Callback
public void onInputBufferAvailable(MediaCodec mediaCodec, int i) {
synchronized (this.lock) {
this.availableInputBuffers.add(i);
}
}
@Override // android.media.MediaCodec.Callback
public void onOutputBufferAvailable(MediaCodec mediaCodec, int i, MediaCodec.BufferInfo bufferInfo) {
synchronized (this.lock) {
MediaFormat mediaFormat = this.pendingOutputFormat;
if (mediaFormat != null) {
addOutputFormat(mediaFormat);
this.pendingOutputFormat = null;
}
this.availableOutputBuffers.add(i);
this.bufferInfos.add(bufferInfo);
}
}
@Override // android.media.MediaCodec.Callback
public void onError(MediaCodec mediaCodec, MediaCodec.CodecException codecException) {
synchronized (this.lock) {
this.mediaCodecException = codecException;
}
}
@Override // android.media.MediaCodec.Callback
public void onOutputFormatChanged(MediaCodec mediaCodec, MediaFormat mediaFormat) {
synchronized (this.lock) {
addOutputFormat(mediaFormat);
this.pendingOutputFormat = null;
}
}
/* JADX INFO: Access modifiers changed from: private */
public void onFlushCompleted() {
synchronized (this.lock) {
if (this.shutDown) {
return;
}
long j = this.pendingFlushCount - 1;
this.pendingFlushCount = j;
if (j > 0) {
return;
}
if (j < 0) {
setInternalException(new IllegalStateException());
} else {
flushInternal();
}
}
}
private void flushInternal() {
if (!this.formats.isEmpty()) {
this.pendingOutputFormat = this.formats.getLast();
}
this.availableInputBuffers.clear();
this.availableOutputBuffers.clear();
this.bufferInfos.clear();
this.formats.clear();
}
private void addOutputFormat(MediaFormat mediaFormat) {
this.availableOutputBuffers.add(-2);
this.formats.add(mediaFormat);
}
private void maybeThrowException() {
maybeThrowInternalException();
maybeThrowMediaCodecException();
}
private void maybeThrowInternalException() {
IllegalStateException illegalStateException = this.internalException;
if (illegalStateException == null) {
return;
}
this.internalException = null;
throw illegalStateException;
}
private void maybeThrowMediaCodecException() {
MediaCodec.CodecException codecException = this.mediaCodecException;
if (codecException == null) {
return;
}
this.mediaCodecException = null;
throw codecException;
}
private void setInternalException(IllegalStateException illegalStateException) {
synchronized (this.lock) {
this.internalException = illegalStateException;
}
}
}