package com.tencent.mm.media.d;

import a.f.b.j;
import a.l;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaCodec.Callback;
import android.media.MediaCodec.CodecException;
import android.media.MediaFormat;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.media.d.d.b;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.bo;
import java.nio.ByteBuffer;
import java.util.List;

@l(dWo = {1, 1, 13}, dWp = {"\u00005\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000*\u0001\u0000\b\n\u0018\u00002\u00020\u0001J\u001c\u0010\u0002\u001a\u00020\u00032\b\u0010\u0004\u001a\u0004\u0018\u00010\u00052\b\u0010\u0006\u001a\u0004\u0018\u00010\u0007H\u0016J\u001a\u0010\b\u001a\u00020\u00032\b\u0010\u0004\u001a\u0004\u0018\u00010\u00052\u0006\u0010\t\u001a\u00020\nH\u0016J$\u0010\u000b\u001a\u00020\u00032\b\u0010\u0004\u001a\u0004\u0018\u00010\u00052\u0006\u0010\t\u001a\u00020\n2\b\u0010\f\u001a\u0004\u0018\u00010\rH\u0016J\u001c\u0010\u000e\u001a\u00020\u00032\b\u0010\u0004\u001a\u0004\u0018\u00010\u00052\b\u0010\u000f\u001a\u0004\u0018\u00010\u0010H\u0016¨\u0006\u0011"}, dWq = {"com/tencent/mm/media/encoder/MediaCodecAACEncoderAsync$aacCallback$1", "Landroid/media/MediaCodec$Callback;", "onError", "", "codec", "Landroid/media/MediaCodec;", "e", "Landroid/media/MediaCodec$CodecException;", "onInputBufferAvailable", "index", "", "onOutputBufferAvailable", "info", "Landroid/media/MediaCodec$BufferInfo;", "onOutputFormatChanged", "format", "Landroid/media/MediaFormat;", "plugin-mediaeditor_release"})
public final class d$c extends Callback {
    final /* synthetic */ d eVI;

    d$c(d dVar) {
        this.eVI = dVar;
    }

    public final void onOutputBufferAvailable(MediaCodec mediaCodec, int i, BufferInfo bufferInfo) {
        AppMethodBeat.i(12938);
        try {
            ab.d("MicroMsg.MediaCodecAACEncoder", "encoderOutputBufferIndex: %s", new Object[]{Integer.valueOf(i)});
            if (i == -1) {
                ab.d("MicroMsg.MediaCodecAACEncoder", "no output available, break");
                AppMethodBeat.o(12938);
            } else if (i == -2) {
                d dVar = this.eVI;
                MediaFormat outputFormat = this.eVI.eVq.getOutputFormat();
                j.o(outputFormat, "encoder.outputFormat");
                dVar.c(outputFormat);
                ab.i("MicroMsg.MediaCodecAACEncoder", "dstMediaFormat change: %s", new Object[]{this.eVI.eVr});
                AppMethodBeat.o(12938);
            } else if (i < 0) {
                ab.e("MicroMsg.MediaCodecAACEncoder", "unexpected encoderOutputBufferIndex: %s", new Object[]{Integer.valueOf(i)});
                AppMethodBeat.o(12938);
            } else {
                if (bufferInfo == null) {
                    j.dWJ();
                }
                if ((bufferInfo.flags & 2) != 0) {
                    ab.v("MicroMsg.MediaCodecAACEncoder", "ignoring BUFFER_FLAG_CODEC_CONFIG,size: %s, %s", new Object[]{Integer.valueOf(bufferInfo.size), Boolean.TRUE});
                    bufferInfo.size = 0;
                }
                ab.v("MicroMsg.MediaCodecAACEncoder", "perform encoding");
                this.eVI.f(this.eVI.eVq.getOutputBuffer(i), bufferInfo);
                this.eVI.eVq.releaseOutputBuffer(i, false);
                if ((bufferInfo.flags & 4) != 0 || d.a(this.eVI)) {
                    ab.i("MicroMsg.MediaCodecAACEncoder", "receive EOS!");
                    this.eVI.releaseEncoder();
                    AppMethodBeat.o(12938);
                    return;
                }
                AppMethodBeat.o(12938);
            }
        } catch (Exception e) {
            ab.printErrStackTrace("MicroMsg.MediaCodecAACEncoder", e, "drainEncoder error: %s", new Object[]{e.getMessage()});
            AppMethodBeat.o(12938);
        }
    }

    public final void onInputBufferAvailable(MediaCodec mediaCodec, int i) {
        AppMethodBeat.i(12939);
        try {
            ab.i("MicroMsg.MediaCodecAACEncoder", "has input buffer ".concat(String.valueOf(i)));
            if (d.c(this.eVI).size() == 0) {
                ab.i("MicroMsg.MediaCodecAACEncoder", "current data is null");
                this.eVI.eVq.queueInputBuffer(i, 0, 0, 0, 0);
                AppMethodBeat.o(12939);
                return;
            }
            ab.i("MicroMsg.MediaCodecAACEncoder", "has input buffer avaiable".concat(String.valueOf(i)));
            if (this.eVI.eTf) {
                ab.i("MicroMsg.MediaCodecAACEncoder", "sendDataToEncoder end now");
                AppMethodBeat.o(12939);
                return;
            }
            this.eVI.eVs = bo.yz();
            if (i < 0) {
                ab.d("MicroMsg.MediaCodecAACEncoder", "encoder no input buffer available, drain first");
                AppMethodBeat.o(12939);
                return;
            }
            List c = d.c(this.eVI);
            j.o(c, "inputDataSource");
            b bVar = (b) a.a.j.fJ(c);
            byte[] bArr = bVar.ckv;
            long j = bVar.eTe;
            boolean z = bVar.eVH;
            if (bArr == null) {
                AppMethodBeat.o(12939);
                return;
            }
            if (i >= 0) {
                int i2;
                ByteBuffer inputBuffer = this.eVI.eVq.getInputBuffer(i);
                if (inputBuffer != null) {
                    inputBuffer.clear();
                }
                if (inputBuffer != null) {
                    inputBuffer.position(0);
                }
                if (inputBuffer != null) {
                    inputBuffer.put(bArr);
                }
                if (z) {
                    ab.i("MicroMsg.MediaCodecAACEncoder", "last, send EOS and try delay stop encoder");
                    i2 = 1;
                } else {
                    i2 = 0;
                }
                if (i2 != 0) {
                    d.b(this.eVI);
                    ab.i("MicroMsg.MediaCodecAACEncoder", "EOS received in sendAudioToEncoder");
                    this.eVI.eVq.queueInputBuffer(i, 0, bArr.length, j, 4);
                } else {
                    this.eVI.eVq.queueInputBuffer(i, 0, bArr.length, j, 0);
                }
                d.c(this.eVI).remove(bVar);
                AppMethodBeat.o(12939);
                return;
            }
            AppMethodBeat.o(12939);
        } catch (Exception e) {
            ab.printErrStackTrace("MicroMsg.MediaCodecAACEncoder", e, "onInputBufferAvailable error", new Object[0]);
        }
    }

    public final void onOutputFormatChanged(MediaCodec mediaCodec, MediaFormat mediaFormat) {
    }

    public final void onError(MediaCodec mediaCodec, CodecException codecException) {
    }
}
