package com.ss.android.ttve.mediacodec;

import X.7Vr;
import X.7cx;
import X.9Cv;
import X.C0347IDi;
import X.II6;
import X.II7;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.text.TextUtils;
import android.view.Surface;
import com.bytedance.covode.number.Covode;
import java.nio.ByteBuffer;
import java.util.concurrent.LinkedBlockingQueue;

public class TEAudioHwEncoder {
    public static long INPUT_DEQUEUE_TIMEOUT_US = 10000;
    public static int TRY_AGAIN_LATER_COUNT_LIMIT = 5;
    public MediaCodec audioEncoder;
    public LinkedBlockingQueue<II6> audioQueue = new LinkedBlockingQueue<>();
    public int curBitrate = -1;
    public int curChannelCount = -1;
    public int curCodecInfoAACProfile = -1;
    public String curMimeType;
    public int curSampleNumPerChannel = -1;
    public int curSampleRate = -1;
    public volatile boolean encoderStarted;
    public volatile long inputCountDequeued;
    public volatile boolean inputEof;
    public MediaFormat inputFormat;
    public byte[] outputAsc;
    public MediaCodec.BufferInfo outputBufferInfo = new MediaCodec.BufferInfo();
    public volatile long outputCountDequeued;
    public volatile boolean outputEof;
    public MediaFormat outputFormat;
    public int recordInputBufferSize = -1;
    public LinkedBlockingQueue<II7> remainingPcmQueue = new LinkedBlockingQueue<>();
    public II6 theOldestAudioData = null;

    private byte[] getOutputAsc() {
        return this.outputAsc;
    }

    private int restartEncoder() {
        releaseEncoder();
        return startEncoder();
    }

    static {
        Covode.recordClassIndex(49324);
    }

    private byte[] getAudioData() {
        II6 ii6 = this.theOldestAudioData;
        if (ii6 != null) {
            return ii6.LIZ;
        }
        return null;
    }

    private long getAudioPts() {
        II6 ii6 = this.theOldestAudioData;
        if (ii6 != null) {
            return ii6.LIZIZ;
        }
        return -1;
    }

    private void resetCodecInfo() {
        this.inputCountDequeued = 0;
        this.outputCountDequeued = 0;
        this.inputEof = false;
        this.outputEof = false;
        this.remainingPcmQueue.clear();
        this.audioQueue.clear();
    }

    private int closeEncoder() {
        C0347IDi.LIZ("TEAudioHwEncoder", "closeEncoder, remainingPcmQueue size: " + this.remainingPcmQueue.size() + ", audioQueue size: " + this.audioQueue.size() + ", inputCountDequeued: " + this.inputCountDequeued + ", outputCountDequeued: " + this.outputCountDequeued);
        releaseEncoder();
        this.curMimeType = null;
        this.curSampleRate = -1;
        this.curChannelCount = -1;
        this.curSampleRate = -1;
        this.curCodecInfoAACProfile = -1;
        this.curSampleNumPerChannel = -1;
        this.outputAsc = null;
        return 0;
    }

    private void releaseEncoder() {
        if (this.audioEncoder != null) {
            try {
                if (this.encoderStarted) {
                    try {
                        this.audioEncoder.stop();
                    } catch (Exception e) {
                        C0347IDi.LIZLLL("TEAudioHwEncoder", "MediaCodec stop exception: " + e.getMessage());
                    }
                    this.encoderStarted = false;
                }
                this.audioEncoder.release();
                this.audioEncoder = null;
            } catch (Exception e2) {
                C0347IDi.LIZLLL("TEAudioHwEncoder", "releaseEncoder: " + e2.getMessage());
            } finally {
                resetCodecInfo();
            }
        }
    }

    private int startEncoder() {
        try {
            MediaCodec selectMediaCodec = selectMediaCodec();
            this.audioEncoder = selectMediaCodec;
            if (selectMediaCodec == null) {
                C0347IDi.LIZLLL("TEAudioHwEncoder", "create encoder failure, mime: " + this.curMimeType);
                return -201;
            }
            selectMediaCodec.configure(this.inputFormat, (Surface) null, (MediaCrypto) null, 1);
            this.audioEncoder.start();
            this.encoderStarted = true;
            return 0;
        } catch (Exception e) {
            C0347IDi.LIZLLL("TEAudioHwEncoder", "startEncoder: " + e.getMessage());
            return -203;
        }
    }

    private MediaCodecInfo getGoogleEncoderCodecInfo() {
        String[] supportedTypes;
        if (TextUtils.isEmpty(this.curMimeType)) {
            return null;
        }
        for (MediaCodecInfo mediaCodecInfo : new MediaCodecList(1).getCodecInfos()) {
            if (mediaCodecInfo.isEncoder() && mediaCodecInfo.getName().contains("OMX.google") && (supportedTypes = mediaCodecInfo.getSupportedTypes()) != null) {
                int length = supportedTypes.length;
                int i = 0;
                while (true) {
                    if (i >= length) {
                        continue;
                        break;
                    } else if (supportedTypes[i].equalsIgnoreCase(this.curMimeType)) {
                        MediaCodecInfo.AudioCapabilities audioCapabilities = mediaCodecInfo.getCapabilitiesForType(this.curMimeType).getAudioCapabilities();
                        if (audioCapabilities == null) {
                            C0347IDi.LIZLLL("TEAudioHwEncoder", mediaCodecInfo.getName() + " null audio capabilities");
                        } else if (!audioCapabilities.isSampleRateSupported(this.curSampleRate)) {
                            C0347IDi.LIZLLL("TEAudioHwEncoder", mediaCodecInfo.getName() + " not support SampleRate: " + this.curSampleRate);
                        } else if (this.curChannelCount <= audioCapabilities.getMaxInputChannelCount()) {
                            return mediaCodecInfo;
                        } else {
                            C0347IDi.LIZLLL("TEAudioHwEncoder", mediaCodecInfo.getName() + " not support ChannelCount: " + this.curChannelCount + ", max channel count is " + audioCapabilities.getMaxInputChannelCount());
                        }
                    } else {
                        i++;
                    }
                }
            }
        }
        return null;
    }

    /* JADX WARNING: Removed duplicated region for block: B:11:0x0036 A[SYNTHETIC, Splitter:B:11:0x0036] */
    /* JADX WARNING: Removed duplicated region for block: B:13:0x003d A[Catch:{ Exception -> 0x003f }] */
    /* JADX WARNING: Removed duplicated region for block: B:17:0x0057  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private android.media.MediaCodec selectMediaCodec() {
        /*
            r8 = this;
            java.lang.String r0 = r8.curMimeType
            boolean r0 = android.text.TextUtils.isEmpty(r0)
            r7 = 0
            if (r0 == 0) goto L_0x000a
            return r7
        L_0x000a:
            android.media.MediaCodecInfo r0 = r8.getGoogleEncoderCodecInfo()
            java.lang.String r6 = "TEAudioHwEncoder"
            if (r0 != 0) goto L_0x0016
        L_0x0012:
            r0 = r7
        L_0x0013:
            if (r0 != 0) goto L_0x003d
            goto L_0x0036
        L_0x0016:
            java.lang.String r0 = r0.getName()     // Catch:{ Exception -> 0x001f }
            android.media.MediaCodec r0 = android.media.MediaCodec.createByCodecName(r0)     // Catch:{ Exception -> 0x001f }
            goto L_0x0013
        L_0x001f:
            r2 = move-exception
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "createByCodecName error: "
            r1.<init>(r0)
            java.lang.String r0 = r2.getMessage()
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            X.C0347IDi.LIZLLL(r6, r0)
            goto L_0x0012
        L_0x0036:
            java.lang.String r0 = r8.curMimeType     // Catch:{ Exception -> 0x003f }
            android.media.MediaCodec r7 = android.media.MediaCodec.createEncoderByType(r0)     // Catch:{ Exception -> 0x003f }
            goto L_0x0055
        L_0x003d:
            r7 = r0
            goto L_0x0055
        L_0x003f:
            r2 = move-exception
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "createEncoderByType error: "
            r1.<init>(r0)
            java.lang.String r0 = r2.getMessage()
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            X.C0347IDi.LIZLLL(r6, r0)
        L_0x0055:
            if (r7 == 0) goto L_0x00aa
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "selectMediaCodec, encoder name: "
            r1.<init>(r0)
            java.lang.String r0 = r7.getName()
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            X.C0347IDi.LIZ(r6, r0)
            android.media.MediaFormat r0 = r8.inputFormat
            if (r0 == 0) goto L_0x00aa
            android.media.MediaCodecInfo r1 = r7.getCodecInfo()
            java.lang.String r0 = r8.curMimeType
            android.media.MediaCodecInfo$CodecCapabilities r0 = r1.getCapabilitiesForType(r0)
            android.media.MediaCodecInfo$CodecProfileLevel[] r5 = r0.profileLevels
            r4 = 0
            if (r5 == 0) goto L_0x008c
            int r3 = r5.length
            r2 = 0
        L_0x0081:
            if (r2 >= r3) goto L_0x008c
            r0 = r5[r2]
            int r1 = r0.profile
            int r0 = r8.curCodecInfoAACProfile
            if (r1 != r0) goto L_0x00ab
            r4 = 1
        L_0x008c:
            if (r4 == 0) goto L_0x00aa
            android.media.MediaFormat r2 = r8.inputFormat
            int r1 = r8.curCodecInfoAACProfile
            java.lang.String r0 = "aac-profile"
            r2.setInteger(r0, r1)
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "final profile: "
            r1.<init>(r0)
            int r0 = r8.curCodecInfoAACProfile
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            X.C0347IDi.LIZ(r6, r0)
        L_0x00aa:
            return r7
        L_0x00ab:
            int r2 = r2 + 1
            goto L_0x0081
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ttve.mediacodec.TEAudioHwEncoder.selectMediaCodec():android.media.MediaCodec");
    }

    private ByteBuffer geOutputBufferByIndex(int i) {
        return this.audioEncoder.getOutputBuffer(i);
    }

    private ByteBuffer getInputBufferByIndex(int i) {
        return this.audioEncoder.getInputBuffer(i);
    }

    private void addAudioData(byte[] bArr) {
        II6 ii6 = new II6((byte) 0);
        ii6.LIZ = bArr;
        ii6.LIZIZ = this.outputBufferInfo.presentationTimeUs;
        ii6.LIZJ = this.outputBufferInfo.flags;
        this.audioQueue.add(ii6);
    }

    /* JADX WARNING: Code restructure failed: missing block: B:12:0x001f, code lost:
        if (r10.outputBufferInfo.size <= 0) goto L_0x00f3;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0021, code lost:
        r6 = geOutputBufferByIndex(r4);
        r6.position(r10.outputBufferInfo.offset);
        r6.limit(r10.outputBufferInfo.offset + r10.outputBufferInfo.size);
        r5 = new byte[r10.outputBufferInfo.size];
        r6.get(r5);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:35:0x008d, code lost:
        if ((r10.outputBufferInfo.flags & 2) == 0) goto L_0x0090;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:38:0x0094, code lost:
        if (r10.outputCountDequeued <= 0) goto L_0x00c7;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:40:0x009c, code lost:
        if (r10.outputBufferInfo.presentationTimeUs > 0) goto L_0x00c7;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:41:0x009e, code lost:
        X.C0347IDi.LIZLLL("TEAudioHwEncoder", "outputCountDequeued: " + r10.outputCountDequeued + ", size: " + r10.outputBufferInfo.size + ", pts: " + r10.outputBufferInfo.presentationTimeUs);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:42:0x00c7, code lost:
        addAudioData(r5);
        r10.outputCountDequeued++;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:43:0x00d2, code lost:
        r10.outputAsc = r5;
        X.C0347IDi.LIZ("TEAudioHwEncoder", "output BUFFER_FLAG_CODEC_CONFIG, asc size: " + r10.outputBufferInfo.size + ", pts: " + r10.outputBufferInfo.presentationTimeUs);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:44:0x00f3, code lost:
        r10.audioEncoder.releaseOutputBuffer(r4, false);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:45:0x00fe, code lost:
        if ((r10.outputBufferInfo.flags & 4) == 0) goto L_0x0003;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:46:0x0100, code lost:
        X.C0347IDi.LIZ("TEAudioHwEncoder", "output buffer eof");
        r10.outputEof = true;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private int drainOutputBuffer(boolean r11) {
        /*
            r10 = this;
            java.lang.String r3 = "TEAudioHwEncoder"
            r2 = 0
        L_0x0003:
            r5 = 0
        L_0x0004:
            boolean r0 = r10.outputEof     // Catch:{ all -> 0x0169 }
            if (r0 != 0) goto L_0x0168
            android.media.MediaCodec r6 = r10.audioEncoder     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r4 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            r8 = 0
            if (r5 <= 0) goto L_0x0013
            r0 = 10000(0x2710, double:4.9407E-320)
            goto L_0x0015
        L_0x0013:
            r0 = 0
        L_0x0015:
            int r4 = r6.dequeueOutputBuffer(r4, r0)     // Catch:{ all -> 0x0169 }
            if (r4 < 0) goto L_0x0046
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            int r0 = r0.size     // Catch:{ all -> 0x0169 }
            if (r0 <= 0) goto L_0x00f3
            java.nio.ByteBuffer r6 = r10.geOutputBufferByIndex(r4)     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            int r0 = r0.offset     // Catch:{ all -> 0x0169 }
            r6.position(r0)     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            int r1 = r0.offset     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            int r0 = r0.size     // Catch:{ all -> 0x0169 }
            int r1 = r1 + r0
            r6.limit(r1)     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            int r0 = r0.size     // Catch:{ all -> 0x0169 }
            byte[] r5 = new byte[r0]     // Catch:{ all -> 0x0169 }
            r6.get(r5)     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            int r0 = r0.flags     // Catch:{ all -> 0x0169 }
            goto L_0x0089
        L_0x0046:
            r0 = -3
            if (r4 != r0) goto L_0x004b
            goto L_0x0109
        L_0x004b:
            r0 = -2
            if (r4 != r0) goto L_0x0050
            goto L_0x0110
        L_0x0050:
            r0 = -1
            if (r4 != r0) goto L_0x0163
            if (r11 == 0) goto L_0x005a
            java.lang.String r0 = "dequeue output buffer timeout, try again later"
            X.C0347IDi.LIZIZ(r3, r0)     // Catch:{ all -> 0x0169 }
        L_0x005a:
            boolean r0 = r10.inputEof     // Catch:{ all -> 0x0169 }
            if (r0 != 0) goto L_0x0067
            if (r11 == 0) goto L_0x0168
            byte[] r0 = r10.outputAsc     // Catch:{ all -> 0x0169 }
            if (r0 == 0) goto L_0x0067
            int r0 = r0.length     // Catch:{ all -> 0x0169 }
            if (r0 > 0) goto L_0x0168
        L_0x0067:
            int r5 = r5 + 1
            java.lang.String r1 = "inputEof, tryAgainLaterCount: "
            java.lang.String r0 = java.lang.String.valueOf(r5)     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = r1.concat(r0)     // Catch:{ all -> 0x0169 }
            X.C0347IDi.LIZ(r3, r0)     // Catch:{ all -> 0x0169 }
            int r0 = TRY_AGAIN_LATER_COUNT_LIMIT     // Catch:{ all -> 0x0169 }
            if (r5 <= r0) goto L_0x0004
            java.lang.String r1 = "dequeue output buffer timeout, tryAgainLaterCount: "
            java.lang.String r0 = java.lang.String.valueOf(r5)     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = r1.concat(r0)     // Catch:{ all -> 0x0169 }
            X.C0347IDi.LIZIZ(r3, r0)     // Catch:{ all -> 0x0169 }
            goto L_0x0168
        L_0x0089:
            r0 = r0 & 2
            java.lang.String r6 = ", pts: "
            if (r0 == 0) goto L_0x0090
            goto L_0x00d2
        L_0x0090:
            long r0 = r10.outputCountDequeued     // Catch:{ all -> 0x0169 }
            int r7 = (r0 > r8 ? 1 : (r0 == r8 ? 0 : -1))
            if (r7 <= 0) goto L_0x00c7
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            long r0 = r0.presentationTimeUs     // Catch:{ all -> 0x0169 }
            int r7 = (r0 > r8 ? 1 : (r0 == r8 ? 0 : -1))
            if (r7 > 0) goto L_0x00c7
            java.lang.StringBuilder r7 = new java.lang.StringBuilder     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = "outputCountDequeued: "
            r7.<init>(r0)     // Catch:{ all -> 0x0169 }
            long r0 = r10.outputCountDequeued     // Catch:{ all -> 0x0169 }
            r7.append(r0)     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = ", size: "
            r7.append(r0)     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            int r0 = r0.size     // Catch:{ all -> 0x0169 }
            r7.append(r0)     // Catch:{ all -> 0x0169 }
            r7.append(r6)     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            long r0 = r0.presentationTimeUs     // Catch:{ all -> 0x0169 }
            r7.append(r0)     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = r7.toString()     // Catch:{ all -> 0x0169 }
            X.C0347IDi.LIZLLL(r3, r0)     // Catch:{ all -> 0x0169 }
        L_0x00c7:
            r10.addAudioData(r5)     // Catch:{ all -> 0x0169 }
            long r0 = r10.outputCountDequeued     // Catch:{ all -> 0x0169 }
            r5 = 1
            long r0 = r0 + r5
            r10.outputCountDequeued = r0     // Catch:{ all -> 0x0169 }
            goto L_0x00f3
        L_0x00d2:
            r10.outputAsc = r5     // Catch:{ all -> 0x0169 }
            java.lang.StringBuilder r5 = new java.lang.StringBuilder     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = "output BUFFER_FLAG_CODEC_CONFIG, asc size: "
            r5.<init>(r0)     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            int r0 = r0.size     // Catch:{ all -> 0x0169 }
            r5.append(r0)     // Catch:{ all -> 0x0169 }
            r5.append(r6)     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            long r0 = r0.presentationTimeUs     // Catch:{ all -> 0x0169 }
            r5.append(r0)     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = r5.toString()     // Catch:{ all -> 0x0169 }
            X.C0347IDi.LIZ(r3, r0)     // Catch:{ all -> 0x0169 }
        L_0x00f3:
            android.media.MediaCodec r0 = r10.audioEncoder     // Catch:{ all -> 0x0169 }
            r0.releaseOutputBuffer(r4, r2)     // Catch:{ all -> 0x0169 }
            android.media.MediaCodec$BufferInfo r0 = r10.outputBufferInfo     // Catch:{ all -> 0x0169 }
            int r0 = r0.flags     // Catch:{ all -> 0x0169 }
            r0 = r0 & 4
            if (r0 == 0) goto L_0x0003
            java.lang.String r0 = "output buffer eof"
            X.C0347IDi.LIZ(r3, r0)     // Catch:{ all -> 0x0169 }
            r0 = 1
            r10.outputEof = r0     // Catch:{ all -> 0x0169 }
            goto L_0x0168
        L_0x0109:
            java.lang.String r0 = "output buffer changed, need to getOutputBuffers again"
            X.C0347IDi.LIZ(r3, r0)     // Catch:{ all -> 0x0169 }
            goto L_0x0003
        L_0x0110:
            android.media.MediaCodec r0 = r10.audioEncoder     // Catch:{ all -> 0x0169 }
            android.media.MediaFormat r0 = r0.getOutputFormat()     // Catch:{ all -> 0x0169 }
            r10.outputFormat = r0     // Catch:{ all -> 0x0169 }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = "output buffer format changed: "
            r1.<init>(r0)     // Catch:{ all -> 0x0169 }
            android.media.MediaFormat r0 = r10.outputFormat     // Catch:{ all -> 0x0169 }
            r1.append(r0)     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = r1.toString()     // Catch:{ all -> 0x0169 }
            X.C0347IDi.LIZ(r3, r0)     // Catch:{ all -> 0x0169 }
            android.media.MediaFormat r1 = r10.outputFormat     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = "sample-rate"
            int r4 = r1.getInteger(r0)     // Catch:{ all -> 0x0169 }
            android.media.MediaFormat r1 = r10.outputFormat     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = "channel-count"
            int r1 = r1.getInteger(r0)     // Catch:{ all -> 0x0169 }
            int r0 = r10.curSampleRate     // Catch:{ all -> 0x0169 }
            if (r0 != r4) goto L_0x0143
            int r0 = r10.curChannelCount     // Catch:{ all -> 0x0169 }
            if (r0 == r1) goto L_0x0148
        L_0x0143:
            java.lang.String r0 = "audio meta info changed, error error error !!!"
            X.C0347IDi.LIZLLL(r3, r0)     // Catch:{ all -> 0x0169 }
        L_0x0148:
            android.media.MediaFormat r1 = r10.outputFormat     // Catch:{ all -> 0x0169 }
            java.lang.String r0 = "csd-0"
            java.nio.ByteBuffer r1 = r1.getByteBuffer(r0)     // Catch:{ all -> 0x0169 }
            int r0 = r1.remaining()     // Catch:{ all -> 0x0169 }
            if (r0 <= 0) goto L_0x0003
            int r0 = r1.remaining()     // Catch:{ all -> 0x0169 }
            byte[] r0 = new byte[r0]     // Catch:{ all -> 0x0169 }
            r1.get(r0)     // Catch:{ all -> 0x0169 }
            r10.outputAsc = r0     // Catch:{ all -> 0x0169 }
            goto L_0x0003
        L_0x0163:
            java.lang.String r0 = "not available output buffer"
            X.C0347IDi.LIZIZ(r3, r0)     // Catch:{ all -> 0x0169 }
        L_0x0168:
            return r2
        L_0x0169:
            r2 = move-exception
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "drainOutputBuffer error: "
            r1.<init>(r0)
            java.lang.String r0 = r2.getMessage()
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            X.C0347IDi.LIZLLL(r3, r0)
            r0 = -60010(0xffffffffffff1596, float:NaN)
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ttve.mediacodec.TEAudioHwEncoder.drainOutputBuffer(boolean):int");
    }

    private void addPcmData(byte[] bArr, long j) {
        II7 ii7 = new II7((byte) 0);
        ii7.LIZ = bArr;
        ii7.LIZIZ = j;
        this.remainingPcmQueue.add(ii7);
    }

    public int encodeFrame(byte[] bArr, long j) {
        II7 poll;
        TEAudioHwEncoder tEAudioHwEncoder = this;
        int i = -50010;
        if (7Vr.LJI == 7cx.AUDIO_ENC_ENCODING_FALLBACK.getValue()) {
            C0347IDi.LIZLLL("TEAudioHwEncoder", "TESTING! HW AUDIO ENC ENCODING FALLBACK");
            return -50010;
        }
        try {
            if (!tEAudioHwEncoder.inputEof) {
                tEAudioHwEncoder.addPcmData(bArr, j);
            }
            int i2 = 0;
            while (true) {
                if (tEAudioHwEncoder.inputEof || tEAudioHwEncoder.remainingPcmQueue.isEmpty() || (poll = tEAudioHwEncoder.remainingPcmQueue.poll()) == null) {
                    break;
                }
                int dequeueInputBuffer = tEAudioHwEncoder.audioEncoder.dequeueInputBuffer(INPUT_DEQUEUE_TIMEOUT_US);
                if (dequeueInputBuffer < 0) {
                    if (dequeueInputBuffer != -1) {
                        C0347IDi.LIZIZ("TEAudioHwEncoder", "not available input buffer");
                        break;
                    }
                    C0347IDi.LIZIZ("TEAudioHwEncoder", "dequeue input buffer timeout, try again later");
                    i2++;
                    if (i2 > TRY_AGAIN_LATER_COUNT_LIMIT) {
                        C0347IDi.LIZIZ("TEAudioHwEncoder", "dequeue input buffer timeout, tryAgainLaterCount: ".concat(String.valueOf(i2)));
                        break;
                    }
                } else if (poll.LIZ == null || poll.LIZ.length <= 0) {
                    tEAudioHwEncoder.audioEncoder.queueInputBuffer(dequeueInputBuffer, 0, 0, poll.LIZIZ, 4);
                    tEAudioHwEncoder.inputEof = true;
                    C0347IDi.LIZ("TEAudioHwEncoder", "input buffer eof, remainingPcmQueue size: " + tEAudioHwEncoder.remainingPcmQueue.size());
                } else {
                    int length = poll.LIZ.length;
                    ByteBuffer inputBufferByIndex = tEAudioHwEncoder.getInputBufferByIndex(dequeueInputBuffer);
                    inputBufferByIndex.clear();
                    int remaining = inputBufferByIndex.remaining();
                    if (length > remaining) {
                        C0347IDi.LIZLLL("TEAudioHwEncoder", "input buffer not enough, audio data size: " + length + ", buffer available size: " + remaining);
                    } else if (remaining != tEAudioHwEncoder.recordInputBufferSize) {
                        C0347IDi.LIZ("TEAudioHwEncoder", "recordInputBufferSize changed, old: " + tEAudioHwEncoder.recordInputBufferSize + ", new: " + remaining);
                        tEAudioHwEncoder.recordInputBufferSize = remaining;
                    }
                    int min = Math.min(length, remaining);
                    inputBufferByIndex.put(poll.LIZ, 0, min);
                    tEAudioHwEncoder.audioEncoder.queueInputBuffer(dequeueInputBuffer, 0, min, poll.LIZIZ, 0);
                    tEAudioHwEncoder.inputCountDequeued++;
                }
            }
            i = tEAudioHwEncoder.drainOutputBuffer(false);
            if (i < 0) {
                return i;
            }
            if (!tEAudioHwEncoder.audioQueue.isEmpty() || !tEAudioHwEncoder.outputEof) {
                II6 poll2 = tEAudioHwEncoder.audioQueue.poll();
                tEAudioHwEncoder.theOldestAudioData = poll2;
                if (poll2 != null) {
                    return poll2.LIZ.length;
                }
                return 0;
            }
            C0347IDi.LIZ("TEAudioHwEncoder", "encoder all EOF");
            return -1024;
        } catch (Throwable th) {
            C0347IDi.LIZLLL("TEAudioHwEncoder", "drainOutputBuffer error: " + th.getMessage());
        }
    }

    private int initEncoder(String str, int i, int i2, int i3, int i4, int i5) {
        int i6;
        boolean z;
        int i7;
        int i8;
        int i9;
        if (7Vr.LJI == 7cx.AUDIO_ENC_INIT_FALLBACK.getValue()) {
            C0347IDi.LIZLLL("TEAudioHwEncoder", "TESTING! HW AUDIO ENC INIT FALLBACK");
            return -203;
        }
        String str2 = str;
        int i10 = i4;
        int i11 = i5;
        int i12 = i2;
        int i13 = i3;
        int i14 = i;
        if (TextUtils.isEmpty(str2) || i14 <= 0 || i12 <= 0 || i13 <= 0 || i10 <= 0 || i11 <= 0) {
            C0347IDi.LIZLLL("TEAudioHwEncoder", "initEncoder invalid param, mimeType: " + str2 + ", profile: " + i14 + ", sampleRate: " + i12 + ", channelCount: " + i13 + ", bitrate: " + i10 + ", sampleNumPerChannel: " + i11);
            9Cv.LIZ(1, "te_composition_audio_create_hw_encoder", 2);
            return -101;
        }
        if (i14 == 2) {
            i6 = 5;
        } else {
            i6 = 2;
        }
        if (str2.equals(this.curMimeType) && this.curCodecInfoAACProfile == i6 && this.curSampleRate == i12 && this.curChannelCount == i13 && this.curSampleNumPerChannel == i11) {
            z = false;
        } else {
            z = true;
        }
        this.curMimeType = str2;
        this.curCodecInfoAACProfile = i6;
        this.curSampleRate = i12;
        this.curChannelCount = i13;
        this.curBitrate = i10;
        this.curSampleNumPerChannel = i11;
        C0347IDi.LIZ("TEAudioHwEncoder", "initEncoder, mimeType: " + str2 + ", codecInfoAACProfile: " + i6 + ", sampleRate: " + i12 + ", channelCount: " + i13 + ", bitrate: " + i10 + ", sampleNumPerChannel: " + i11);
        if (z || !this.encoderStarted) {
            MediaFormat createAudioFormat = MediaFormat.createAudioFormat(str2, i12, i13);
            this.inputFormat = createAudioFormat;
            createAudioFormat.setInteger("bitrate", i10);
            this.inputFormat.setInteger("aac-profile", 2);
            int i15 = this.curChannelCount * i11 * 2 * 2;
            this.inputFormat.setInteger("max-input-size", i15);
            C0347IDi.LIZ("TEAudioHwEncoder", "KEY_MAX_INPUT_SIZE: ".concat(String.valueOf(i15)));
            i7 = restartEncoder();
            if (i7 != 0) {
                i8 = 1;
                i9 = 1;
                9Cv.LIZ(i8, "te_composition_audio_create_hw_encoder", (long) i9);
                C0347IDi.LIZ("TEAudioHwEncoder", "initEncoder, result: " + i7 + ", monitor: " + i9);
                return i7;
            }
        }
        C0347IDi.LIZ("TEAudioHwEncoder", "initEncoder, try to get asc start");
        i8 = 1;
        i7 = drainOutputBuffer(true);
        C0347IDi.LIZ("TEAudioHwEncoder", "initEncoder, try to get asc end");
        if (i7 == 0) {
            i9 = 0;
        } else {
            i9 = 3;
        }
        9Cv.LIZ(i8, "te_composition_audio_create_hw_encoder", (long) i9);
        C0347IDi.LIZ("TEAudioHwEncoder", "initEncoder, result: " + i7 + ", monitor: " + i9);
        return i7;
    }
}
