package com.dowse.base.audio;

import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.view.Surface;
import com.dowse.base.log.DSLog;
import com.dowse.base.param.AACAudioParam;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/audio/AACEncode.class */
public class AACEncode {
    private static final String TAG = "AACEncode";
    private MediaCodec mediaCodec;
    private AACAudioParam mAACAudioParam;
    MediaCodec.BufferInfo bufferInfo;
    long presentationTimeUs;
    private String mediaType = "OMX.google.aac.encoder";
    ByteBuffer[] inputBuffers = null;
    ByteBuffer[] outputBuffers = null;
    private boolean isInit = false;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    public AACEncode() {
        this.presentationTimeUs = 0L;
        this.presentationTimeUs = 0L;
    }

    public void init(AACAudioParam mAACAudioParam) {
        if (this.isInit) {
            DSLog.d(TAG, "AACEncode is initiated");
            return;
        }
        if (mAACAudioParam == null) {
            mAACAudioParam = new AACAudioParam();
        }
        try {
            this.mediaCodec = MediaCodec.createEncoderByType("audio/mp4a-latm");
            this.isInit = true;
            MediaFormat mediaFormat = MediaFormat.createAudioFormat("audio/mp4a-latm", mAACAudioParam.getSample_rate(), mAACAudioParam.getChannel_count());
            mediaFormat.setString("mime", "audio/mp4a-latm");
            mediaFormat.setInteger("aac-profile", 2);
            mediaFormat.setInteger("bitrate", mAACAudioParam.getBit_rate());
            mediaFormat.setInteger("max-input-size", mAACAudioParam.getMax_input_size());
            this.mediaCodec.configure(mediaFormat, (Surface) null, (MediaCrypto) null, 1);
            this.mediaCodec.start();
            this.inputBuffers = this.mediaCodec.getInputBuffers();
            this.outputBuffers = this.mediaCodec.getOutputBuffers();
            this.bufferInfo = new MediaCodec.BufferInfo();
        } catch (Exception e) {
            e.printStackTrace();
            DSLog.d(TAG, "AACEncode exception msg=" + e.getMessage());
        }
    }

    public void release() {
        try {
            if (!this.isInit) {
                DSLog.d(TAG, "AACEncode is not init");
                return;
            }
            this.mediaCodec.stop();
            this.mediaCodec.release();
            this.outputStream.flush();
            this.outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public byte[] encode(byte[] input) throws Exception {
        if (!this.isInit) {
            DSLog.d(TAG, "AACEncode is not init");
            return null;
        }
        DSLog.e(TAG, "encode:" + input.length + " is coming");
        int inputBufferIndex = this.mediaCodec.dequeueInputBuffer(-1L);
        if (inputBufferIndex >= 0) {
            ByteBuffer inputBuffer = this.inputBuffers[inputBufferIndex];
            inputBuffer.clear();
            inputBuffer.put(input);
            inputBuffer.limit(input.length);
            long pts = computePresentationTime(this.presentationTimeUs);
            this.mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, pts, 0);
            this.presentationTimeUs++;
        }
        int dequeueOutputBuffer = this.mediaCodec.dequeueOutputBuffer(this.bufferInfo, 0L);
        while (true) {
            int outputBufferIndex = dequeueOutputBuffer;
            if (outputBufferIndex >= 0) {
                int outBitsSize = this.bufferInfo.size;
                int outPacketSize = outBitsSize + 7;
                ByteBuffer outputBuffer = this.outputBuffers[outputBufferIndex];
                outputBuffer.position(this.bufferInfo.offset);
                outputBuffer.limit(this.bufferInfo.offset + outBitsSize);
                byte[] outData = new byte[outPacketSize];
                addADTStoPacket(outData, outPacketSize);
                outputBuffer.get(outData, 7, outBitsSize);
                outputBuffer.position(this.bufferInfo.offset);
                this.outputStream.write(outData);
                DSLog.d(TAG, "encode:" + outData.length + " bytes written");
                this.mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
                dequeueOutputBuffer = this.mediaCodec.dequeueOutputBuffer(this.bufferInfo, 0L);
            } else {
                byte[] out = this.outputStream.toByteArray();
                this.outputStream.flush();
                this.outputStream.reset();
                return out;
            }
        }
    }

    private void addADTStoPacket(byte[] packet, int packetLen) {
        if (this.mAACAudioParam == null) {
            this.mAACAudioParam = new AACAudioParam();
        }
        int profile = this.mAACAudioParam.getProfile();
        int freqIdx = this.mAACAudioParam.getSample_frequency_index();
        int chanCfg = this.mAACAudioParam.getChannel_count();
        packet[0] = -1;
        packet[1] = -7;
        packet[2] = (byte) (((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
        packet[3] = (byte) (((chanCfg & 3) << 6) + (packetLen >> 11));
        packet[4] = (byte) ((packetLen & 2047) >> 3);
        packet[5] = (byte) (((packetLen & 7) << 5) + 31);
        packet[6] = -4;
    }

    private long computePresentationTime(long frameIndex) {
        if (this.mAACAudioParam == null) {
            this.mAACAudioParam = new AACAudioParam();
        }
        return ((frameIndex * 90000) * 1024) / this.mAACAudioParam.getSample_rate();
    }
}
