package n;

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/* JADX INFO: loaded from: classes.dex */
public class r extends MediaCodec.Callback implements Handler.Callback {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final String f5639a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final t f5640b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final boolean f5641c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final HandlerThread f5642d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final HandlerThread f5643e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final Handler f5644f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final Handler f5645g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private Lock f5646h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private Lock f5647i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private volatile boolean f5648j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private boolean f5649k = true;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private boolean f5650l = true;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private Lock f5651m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private volatile boolean f5652n;

    public r(t tVar, String str) {
        String str2 = "MediaCodecCallback" + str;
        this.f5639a = str2;
        this.f5640b = tVar;
        boolean z2 = tVar instanceof q;
        this.f5641c = z2;
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(z2 ? "[Audio]" : "[Video]");
        sb.append(":Handler");
        b0.x xVar = new b0.x(sb.toString(), -16);
        this.f5642d = xVar;
        xVar.start();
        Log.i(str2, "MediaCodecCallback[" + z2 + "] thread start");
        this.f5644f = new Handler(xVar.getLooper(), this);
        StringBuilder sb2 = new StringBuilder();
        sb2.append(getClass().getSimpleName());
        sb2.append(z2 ? "[Audio]" : "[Video]");
        sb2.append(":internalHandler");
        b0.x xVar2 = new b0.x(sb2.toString(), -16);
        this.f5643e = xVar2;
        xVar2.start();
        Log.i(str2, "MediaCodecCallback[" + z2 + "] internal thread start");
        this.f5645g = new Handler(xVar2.getLooper(), this);
        this.f5646h = new ReentrantLock();
        this.f5647i = new ReentrantLock();
        this.f5651m = new ReentrantLock();
    }

    public void a() {
        Log.i(this.f5639a, "MediaCodecCallback, flush, isAudio[" + this.f5641c + "]");
        this.f5646h.lock();
        this.f5647i.lock();
        this.f5648j = false;
        this.f5651m.lock();
        this.f5652n = false;
        this.f5645g.removeCallbacksAndMessages(null);
    }

    public void b() {
        Log.i(this.f5639a, "MediaCodecCallback, flushDone, isAudio[" + this.f5641c + "]");
        this.f5646h.unlock();
        this.f5647i.unlock();
        this.f5651m.unlock();
    }

    public Handler c() {
        return this.f5644f;
    }

    public void d() {
        Log.i(this.f5639a, "MediaCodecCallback, isAudio[" + this.f5641c + "] thread release");
        this.f5646h.lock();
        this.f5647i.lock();
        this.f5651m.lock();
        this.f5648j = false;
        this.f5652n = false;
        Log.i(this.f5639a, "MediaCodecCallback, isAudio[" + this.f5641c + "], got release lock");
        this.f5642d.quit();
        this.f5645g.removeCallbacksAndMessages(null);
        this.f5643e.quit();
    }

    @Override // android.os.Handler.Callback
    public boolean handleMessage(Message message) {
        int i2 = message.what;
        if (i2 != 1) {
            if (i2 != 2) {
                return false;
            }
            this.f5652n = true;
            while (this.f5652n) {
                if (!this.f5651m.tryLock()) {
                    this.f5652n = false;
                } else if (this.f5640b.N0()) {
                    this.f5652n = false;
                    this.f5651m.unlock();
                } else {
                    this.f5651m.unlock();
                    try {
                        b0.o.c(this.f5639a, 1, "isAudio[", Boolean.valueOf(this.f5641c), "], do drainOutPutBuffer return false, sleep(5)");
                        Thread.sleep(5L);
                    } catch (InterruptedException unused) {
                        Log.e(this.f5639a, "MSG_RETRY_DRAIN_OUTPUT_BUFFER, catch InterruptedException");
                    }
                }
            }
            return true;
        }
        this.f5648j = true;
        while (this.f5648j) {
            if (!this.f5647i.tryLock()) {
                this.f5648j = false;
            } else if (this.f5640b.Q0(message.arg1)) {
                this.f5648j = false;
                this.f5647i.unlock();
            } else {
                this.f5647i.unlock();
                if (this.f5640b.C0()) {
                    return true;
                }
                try {
                    b0.o.c(this.f5639a, 1, "isAudio[", Boolean.valueOf(this.f5641c), "], do feedInputBuffer(", Integer.valueOf(message.arg1), ") return false, sleep(5)");
                    Thread.sleep(5L);
                } catch (InterruptedException unused2) {
                    Log.e(this.f5639a, "MSG_RETRY_FEED_INPUT_BUFFER, catch InterruptedException");
                }
            }
        }
        return true;
    }

    @Override // android.media.MediaCodec.Callback
    public void onError(MediaCodec mediaCodec, MediaCodec.CodecException codecException) {
        Log.e(this.f5639a, "onError, isAudio[" + this.f5641c + "], CodecException:" + codecException.getMessage() + ", errorCode=" + codecException.getErrorCode());
        this.f5645g.removeMessages(1);
        this.f5645g.removeMessages(2);
        this.f5648j = false;
        this.f5652n = false;
        this.f5640b.O0(codecException, this.f5641c);
    }

    @Override // android.media.MediaCodec.Callback
    public void onInputBufferAvailable(MediaCodec mediaCodec, int i2) {
        b0.o.c(this.f5639a, 1, "onInputBufferAvailable, isAudio[", Boolean.valueOf(this.f5641c), "], inputBufferId:", Integer.valueOf(i2));
        if (this.f5649k) {
            Log.d(this.f5639a, "[Performance]1st onInputBufferAvailable, isAudio[" + this.f5641c + "]");
            this.f5649k = false;
        }
        if (!this.f5646h.tryLock()) {
            Log.i(this.f5639a, "ignore this input buffer when flush/release");
            return;
        }
        this.f5645g.sendMessage(this.f5645g.obtainMessage(1, i2, 0));
        this.f5646h.unlock();
    }

    @Override // android.media.MediaCodec.Callback
    public void onOutputBufferAvailable(MediaCodec mediaCodec, int i2, MediaCodec.BufferInfo bufferInfo) {
        b0.o.c(this.f5639a, 1, "onOutputBufferAvailable, isAudio[", Boolean.valueOf(this.f5641c), "], outputBufferId:", Integer.valueOf(i2), ", PTS:", Long.valueOf(bufferInfo.presentationTimeUs), ", Size:", Integer.valueOf(bufferInfo.size), ", flags:", Integer.valueOf(bufferInfo.flags));
        if (this.f5650l) {
            Log.d(this.f5639a, "[Performance]1st onOutputBufferAvailable, isAudio[" + this.f5641c + "]");
            this.f5650l = false;
        }
        if (!this.f5646h.tryLock()) {
            Log.i(this.f5639a, "ignore this out buffer when flush/release");
            return;
        }
        this.f5640b.S0(i2, bufferInfo);
        if (this.f5640b.v0().m() != 0) {
            this.f5645g.sendEmptyMessage(2);
        }
        this.f5646h.unlock();
    }

    @Override // android.media.MediaCodec.Callback
    public void onOutputFormatChanged(MediaCodec mediaCodec, MediaFormat mediaFormat) {
        b0.o.c(this.f5639a, 1, "onOutputFormatChanged, isAudio[", Boolean.valueOf(this.f5641c), "], format:", mediaFormat.getString("mime"));
        this.f5640b.U0(mediaFormat);
    }
}
