package a.c.d.d;

import a.a.a.b;
import a.c.d.i.f;
import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Size;
import android.view.Surface;
import java.io.IOException;
import java.nio.ByteBuffer;

public class e extends MediaCodec.Callback {
    private static final String f = "VideoEncoder";
    private static final int g = 1920;
    private static final int h = 1080;
    private static final int i = 5242880;
    private static final int j = 10485760;
    private static final int k = 60;
    private static final int l = 15;

    /* renamed from: a  reason: collision with root package name */
    private final Size f110a;
    private final int b;
    private final a c;
    private MediaCodec d;
    private HandlerThread e;

    public interface a {
        void a(String str);

        void a(ByteBuffer byteBuffer, int i);
    }

    public e(Size size, int i2, a aVar) {
        this.f110a = size;
        if (i2 <= 0 || i2 >= 60) {
            this.b = 60;
        } else {
            this.b = i2;
        }
        this.c = aVar;
    }

    private MediaFormat b() {
        MediaFormat createVideoFormat = MediaFormat.createVideoFormat(f.z, this.f110a.getWidth(), this.f110a.getHeight());
        createVideoFormat.setInteger("bitrate", (this.f110a.getWidth() < g || this.f110a.getHeight() < h) ? i : j);
        createVideoFormat.setInteger("bitrate-mode", 1);
        createVideoFormat.setInteger("frame-rate", this.b);
        createVideoFormat.setInteger("color-format", 2130708361);
        createVideoFormat.setInteger("i-frame-interval", 15);
        createVideoFormat.setInteger("profile", 1);
        createVideoFormat.setInteger("level", 1);
        if (Build.VERSION.SDK_INT >= 29) {
            createVideoFormat.setInteger("max-bframes", 0);
        }
        return createVideoFormat;
    }

    public final Surface a() {
        try {
            MediaCodec createEncoderByType = MediaCodec.createEncoderByType(f.z);
            this.d = createEncoderByType;
            createEncoderByType.configure(b(), (Surface) null, (MediaCrypto) null, 1);
            return this.d.createInputSurface();
        } catch (IOException e2) {
            b.b(f, "createInputSurface" + e2.getMessage());
            return null;
        }
    }

    public void c() {
        try {
            HandlerThread handlerThread = new HandlerThread(f);
            this.e = handlerThread;
            handlerThread.start();
            this.d.setCallback(this, new Handler(this.e.getLooper()));
            this.d.start();
            b.a(f, "started");
        } catch (Exception unused) {
            b.b(f, "start failed");
        }
    }

    public void d() {
        try {
            MediaCodec mediaCodec = this.d;
            if (mediaCodec != null) {
                mediaCodec.stop();
                this.d.release();
                this.d = null;
            }
            HandlerThread handlerThread = this.e;
            if (handlerThread != null) {
                handlerThread.quitSafely();
                this.e = null;
            }
            b.a(f, "stopped");
        } catch (Exception unused) {
            b.b(f, "stop failed");
        }
    }

    public void onError(MediaCodec mediaCodec, MediaCodec.CodecException codecException) {
        b.a(f, "onError " + codecException.getMessage());
        this.c.a(codecException.getMessage());
    }

    public void onInputBufferAvailable(MediaCodec mediaCodec, int i2) {
        b.a(f, "onInputBufferAvailable");
    }

    public void onOutputBufferAvailable(MediaCodec mediaCodec, int i2, MediaCodec.BufferInfo bufferInfo) {
        try {
            b.a(f, "onOutputBufferAvailable " + i2 + ", timestamps = " + System.currentTimeMillis());
            this.c.a(mediaCodec.getOutputBuffer(i2), bufferInfo.flags);
            mediaCodec.releaseOutputBuffer(i2, false);
        } catch (Exception e2) {
            b.b(f, "onOutputBufferAvailable exception : " + e2.getMessage());
        }
    }

    public void onOutputFormatChanged(MediaCodec mediaCodec, MediaFormat mediaFormat) {
        b.a(f, "onOutputFormatChanged " + mediaFormat);
    }
}
