package vendor.mediatek;

import android.app.ActivityThread;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.AttributionSource;
import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.media.AudioRouting;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.media.MicrophoneDirection;
import android.media.metrics.LogSessionId;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Parcel;
import android.util.ArrayMap;
import android.util.Log;
import com.android.internal.util.Preconditions;
import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/* JADX INFO: loaded from: classes2.dex */
public class CaptureRecorder implements AudioRouting, MicrophoneDirection {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private long f6685a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private String f6686b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private FileDescriptor f6687c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private File f6688d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private a f6689e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private int f6690f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private LogSessionId f6691g = LogSessionId.LOG_SESSION_ID_NONE;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private OutputSink f6692h = null;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private OutputSink f6693i = null;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private OutputSink f6694j = null;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private final ArrayList f6695k = new ArrayList();

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private AudioDeviceInfo f6696l = null;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private ArrayMap f6697m = new ArrayMap();

    public static final class OutputFrame {

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

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final Object f6698a = new Object();

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private boolean f6699b = false;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private long f6700c = 0;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private ByteBuffer f6702e = null;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private long f6703f = 0;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private int f6704g = 0;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        private MediaFormat f6705h = null;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        private final ArrayList f6706i = new ArrayList();

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        private final Set f6707j = new HashSet();

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

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

        OutputFrame(int i2) {
            this.f6701d = i2;
        }

        private native ByteBuffer native_map();

        private native void native_recycle();

        boolean a() {
            return this.f6708k;
        }

        boolean b() {
            return this.f6709l;
        }

        public ByteBuffer c() {
            ByteBuffer byteBuffer;
            synchronized (this.f6698a) {
                try {
                    if (!this.f6699b) {
                        throw new IllegalStateException("The linear block is invalid");
                    }
                    if (this.f6702e == null) {
                        this.f6702e = native_map();
                    }
                    byteBuffer = this.f6702e;
                } catch (Throwable th) {
                    throw th;
                }
            }
            return byteBuffer;
        }

        public void d() {
            synchronized (this.f6698a) {
                try {
                    if (!this.f6699b) {
                        throw new IllegalStateException("The output frame is invalid");
                    }
                    ByteBuffer byteBuffer = this.f6702e;
                    if (byteBuffer != null) {
                        byteBuffer.setAccessible(false);
                        this.f6702e = null;
                    }
                    native_recycle();
                    this.f6699b = false;
                    this.f6700c = 0L;
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        void e(boolean z2) {
            this.f6708k = z2;
        }

        void f(MediaCodec.BufferInfo bufferInfo) {
            this.f6703f = bufferInfo.presentationTimeUs;
            this.f6704g = bufferInfo.flags;
        }

        protected void finalize() {
            native_recycle();
        }

        void g(boolean z2) {
            this.f6709l = z2;
        }
    }

    public static final class OutputSink {

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

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

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

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        private a f6719j;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final ArrayList f6711b = new ArrayList();

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final Object f6712c = new Object();

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private boolean f6713d = false;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private long f6714e = 0;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private final Object f6715f = new Object();

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final Object f6710a = new Object();

        private class a extends Handler {

            /* JADX INFO: renamed from: a, reason: collision with root package name */
            private OutputSink f6720a;

            a(OutputSink outputSink, Looper looper) {
                super(looper);
                this.f6720a = outputSink;
            }

            private void a(Message message) {
                if (OutputSink.this.f6718i == null) {
                    return;
                }
                int i2 = message.arg1;
                if (i2 == 1) {
                    Log.d("CaptureRecorder", "track(" + this.f6720a.h() + ") CB_END_OF_STREAM");
                    OutputSink.this.f6718i.a(this.f6720a);
                    return;
                }
                if (i2 == 2) {
                    int i3 = message.arg2;
                    MediaCodec.BufferInfo bufferInfoM = OutputSink.m((Map) message.obj);
                    synchronized (OutputSink.this.f6710a) {
                        while (OutputSink.this.f6711b.size() <= i3) {
                            try {
                                OutputSink.this.f6711b.add(null);
                            } catch (Throwable th) {
                                throw th;
                            }
                        }
                        OutputFrame outputFrame = (OutputFrame) OutputSink.this.f6711b.get(i3);
                        if (outputFrame == null) {
                            outputFrame = new OutputFrame(i3);
                            OutputSink.this.f6711b.set(i3, outputFrame);
                        }
                        outputFrame.f(bufferInfoM);
                        outputFrame.e(true);
                    }
                    OutputSink.this.f6718i.c(this.f6720a, i3, bufferInfoM);
                    return;
                }
                if (i2 != 3) {
                    if (i2 != 4) {
                        return;
                    }
                    Map map = (Map) message.obj;
                    Log.d("CaptureRecorder", "track(" + this.f6720a.h() + ") CB_OUTPUT_AVAILABLE: " + map);
                    OutputSink.this.f6718i.d(this.f6720a, OutputSink.n(map));
                    return;
                }
                int i4 = message.arg2;
                String str = (String) message.obj;
                Log.d("CaptureRecorder", "track(" + this.f6720a.h() + ") CB_ERROR: error " + i4 + " message: " + str);
                OutputSink.this.f6718i.b(this.f6720a, i4, str);
            }

            @Override // android.os.Handler
            public void handleMessage(Message message) {
                int i2 = message.what;
                if (i2 == 1) {
                    a(message);
                } else {
                    if (i2 != 2) {
                        return;
                    }
                    OutputSink.this.f6718i = (b) message.obj;
                }
            }
        }

        public static abstract class b {
            public abstract void a(OutputSink outputSink);

            public abstract void b(OutputSink outputSink, int i2, String str);

            public abstract void c(OutputSink outputSink, int i2, MediaCodec.BufferInfo bufferInfo);

            public abstract void d(OutputSink outputSink, MediaFormat mediaFormat);
        }

        OutputSink(int i2, int i3) {
            this.f6716g = i2;
            this.f6717h = i3;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static final MediaCodec.BufferInfo m(Map map) {
            int iIntValue = ((Integer) map.get("offset")).intValue();
            int iIntValue2 = ((Integer) map.get("size")).intValue();
            long jLongValue = ((Long) map.get("presentationTimeUs")).longValue();
            int iIntValue3 = ((Integer) map.get("flags")).intValue();
            MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
            bufferInfo.set(iIntValue, iIntValue2, jLongValue, iIntValue3);
            return bufferInfo;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static final MediaFormat n(Map map) {
            MediaFormat mediaFormat = new MediaFormat();
            for (Map.Entry entry : map.entrySet()) {
                if (entry.getValue() instanceof Integer) {
                    mediaFormat.setInteger((String) entry.getKey(), ((Integer) entry.getValue()).intValue());
                } else if (entry.getValue() instanceof Long) {
                    mediaFormat.setLong((String) entry.getKey(), ((Long) entry.getValue()).longValue());
                } else if (entry.getValue() instanceof Float) {
                    mediaFormat.setFloat((String) entry.getKey(), ((Float) entry.getValue()).floatValue());
                } else if (entry.getValue() instanceof String) {
                    mediaFormat.setString((String) entry.getKey(), (String) entry.getValue());
                } else {
                    if (!(entry.getValue() instanceof ByteBuffer)) {
                        throw new IllegalArgumentException("Unsupported value type: " + entry.getValue().getClass());
                    }
                    mediaFormat.setByteBuffer((String) entry.getKey(), (ByteBuffer) entry.getValue());
                }
            }
            return mediaFormat;
        }

        private native void native_getOutputFrame(OutputFrame outputFrame, int i2);

        private native void native_release();

        @UnsupportedAppUsage
        private final native void native_releaseOutputBuffer(int i2);

        protected void finalize() {
            i();
        }

        public OutputFrame g(int i2) {
            OutputFrame outputFrame;
            synchronized (this.f6710a) {
                if (i2 >= 0) {
                    try {
                        if (i2 < this.f6711b.size()) {
                            outputFrame = (OutputFrame) this.f6711b.get(i2);
                            if (outputFrame == null) {
                                throw new IllegalArgumentException("Unavailable index: " + i2);
                            }
                            if (!outputFrame.a()) {
                                throw new IllegalArgumentException("The output frame is stale at index " + i2);
                            }
                            if (!outputFrame.b()) {
                                native_getOutputFrame(outputFrame, i2);
                                outputFrame.g(true);
                            }
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                throw new IndexOutOfBoundsException("Expected range of index: [0," + (this.f6711b.size() - 1) + "]; actual: " + i2);
            }
            return outputFrame;
        }

        public int h() {
            return this.f6716g;
        }

        public void i() {
            synchronized (this.f6710a) {
                try {
                    Iterator it = this.f6711b.iterator();
                    while (it.hasNext()) {
                        ((OutputFrame) it.next()).d();
                    }
                    this.f6711b.clear();
                } finally {
                }
            }
            synchronized (this.f6712c) {
                try {
                    if (this.f6713d) {
                        native_release();
                    }
                    this.f6713d = false;
                    this.f6714e = 0L;
                } finally {
                }
            }
        }

        public void j(int i2, MediaCodec.BufferInfo bufferInfo) {
            synchronized (this.f6710a) {
                if (i2 >= 0) {
                    try {
                        if (i2 < this.f6711b.size()) {
                            if (this.f6711b.get(i2) == null) {
                                throw new IllegalStateException("Output frame at index(" + i2 + ") is null");
                            }
                            native_releaseOutputBuffer(i2);
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.append("Expected range of index: [0,");
                sb.append(this.f6711b.size() - 1);
                sb.append("]; actual: ");
                sb.append(i2);
                throw new IndexOutOfBoundsException(sb.toString());
            }
        }

        public void k(Handler handler) {
            synchronized (this.f6715f) {
                try {
                    if (handler != null) {
                        this.f6719j = new a(this, handler.getLooper());
                    } else {
                        Looper looperMyLooper = Looper.myLooper();
                        if (looperMyLooper != null) {
                            this.f6719j = new a(this, looperMyLooper);
                        } else {
                            Looper mainLooper = Looper.getMainLooper();
                            if (mainLooper != null) {
                                this.f6719j = new a(this, mainLooper);
                            } else {
                                this.f6719j = null;
                            }
                        }
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        public void l(b bVar) {
            synchronized (this.f6715f) {
                try {
                    a aVar = this.f6719j;
                    if (aVar != null) {
                        this.f6719j.sendMessage(aVar.obtainMessage(2, 0, 0, bVar));
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    private class a extends Handler {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private CaptureRecorder f6722a;

        public a(CaptureRecorder captureRecorder, Looper looper) {
            super(looper);
            this.f6722a = captureRecorder;
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (this.f6722a.f6685a == 0) {
                Log.w("CaptureRecorder", "CaptureRecorder went away with unhandled events");
                return;
            }
            int i2 = message.what;
            if (i2 != 1) {
                if (i2 != 2) {
                    if (i2 != 100) {
                        if (i2 != 101) {
                            if (i2 != 10000) {
                                Log.e("CaptureRecorder", "Unknown message type " + message.what);
                                return;
                            }
                            synchronized (CaptureRecorder.this.f6697m) {
                                try {
                                    Iterator it = CaptureRecorder.this.f6697m.values().iterator();
                                    while (it.hasNext()) {
                                        ((vendor.mediatek.a) it.next()).c();
                                    }
                                } catch (Throwable th) {
                                    throw th;
                                }
                            }
                            return;
                        }
                    }
                }
                CaptureRecorder.c(CaptureRecorder.this);
                return;
            }
            CaptureRecorder.b(CaptureRecorder.this);
        }
    }

    public interface b {
    }

    public interface c {
    }

    static {
        System.loadLibrary("capturerecorder_jni.mediatek");
        native_init();
    }

    public CaptureRecorder(Context context) {
        Objects.requireNonNull(context);
        Looper looperMyLooper = Looper.myLooper();
        if (looperMyLooper != null) {
            this.f6689e = new a(this, looperMyLooper);
        } else {
            Looper mainLooper = Looper.getMainLooper();
            if (mainLooper != null) {
                this.f6689e = new a(this, mainLooper);
            } else {
                this.f6689e = null;
            }
        }
        this.f6690f = 1;
        AttributionSource.ScopedParcelState scopedParcelStateAsScopedParcelState = context.getAttributionSource().asScopedParcelState();
        try {
            native_setup(new WeakReference(this), ActivityThread.currentPackageName(), scopedParcelStateAsScopedParcelState.getParcel());
            scopedParcelStateAsScopedParcelState.close();
        } catch (Throwable th) {
            if (scopedParcelStateAsScopedParcelState != null) {
                try {
                    scopedParcelStateAsScopedParcelState.close();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
            }
            throw th;
        }
    }

    @UnsupportedAppUsage
    private native void _prepare() throws IllegalStateException, IOException;

    private native void _setOutputFile(FileDescriptor fileDescriptor) throws IllegalStateException, IOException;

    static /* bridge */ /* synthetic */ b b(CaptureRecorder captureRecorder) {
        captureRecorder.getClass();
        return null;
    }

    static /* bridge */ /* synthetic */ c c(CaptureRecorder captureRecorder) {
        captureRecorder.getClass();
        return null;
    }

    private void f(boolean z2) {
        if (this.f6697m.size() == 0) {
            native_enableDeviceCallback(z2);
        }
    }

    private final native void native_enableDeviceCallback(boolean z2);

    private final native int native_getRoutedDeviceId();

    @UnsupportedAppUsage
    private static final native void native_init();

    @UnsupportedAppUsage
    private native void native_release();

    private final native boolean native_setMicrophoneInputDevice(int i2);

    private native int native_setMicrophonePreferredDirection(int i2);

    private native int native_setMicrophonePreferredFieldDimension(float f2);

    private native void native_setOutputSink(OutputSink outputSink) throws IllegalStateException;

    private native void native_setup(Object obj, String str, Parcel parcel) throws IllegalStateException;

    @Override // android.media.AudioRouting
    public void addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener onRoutingChangedListener, Handler handler) {
        synchronized (this.f6697m) {
            if (onRoutingChangedListener != null) {
                try {
                    if (!this.f6697m.containsKey(onRoutingChangedListener)) {
                        f(true);
                        ArrayMap arrayMap = this.f6697m;
                        if (handler == null) {
                            handler = this.f6689e;
                        }
                        arrayMap.put(onRoutingChangedListener, new vendor.mediatek.a(this, onRoutingChangedListener, handler));
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    public OutputSink e(int i2, int i3, OutputSink.b bVar, Handler handler) {
        OutputSink outputSink = new OutputSink(i2, i3);
        outputSink.k(handler);
        outputSink.l(bVar);
        this.f6695k.add(outputSink);
        native_setOutputSink(outputSink);
        return outputSink;
    }

    protected void finalize() {
        h();
    }

    public void g() throws IOException {
        if (this.f6695k.size() <= 0) {
            if (this.f6686b != null) {
                RandomAccessFile randomAccessFile = new RandomAccessFile(this.f6686b, "rw");
                try {
                    _setOutputFile(randomAccessFile.getFD());
                } finally {
                    randomAccessFile.close();
                }
            } else {
                FileDescriptor fileDescriptor = this.f6687c;
                if (fileDescriptor != null) {
                    _setOutputFile(fileDescriptor);
                } else {
                    if (this.f6688d == null) {
                        throw new IOException("No valid output file");
                    }
                    RandomAccessFile randomAccessFile2 = new RandomAccessFile(this.f6688d, "rw");
                    try {
                        _setOutputFile(randomAccessFile2.getFD());
                    } finally {
                        randomAccessFile2.close();
                    }
                }
            }
        }
        _prepare();
    }

    @Override // android.media.AudioRouting
    public AudioDeviceInfo getPreferredDevice() {
        AudioDeviceInfo audioDeviceInfo;
        synchronized (this) {
            audioDeviceInfo = this.f6696l;
        }
        return audioDeviceInfo;
    }

    @Override // android.media.AudioRouting
    public AudioDeviceInfo getRoutedDevice() {
        int iNative_getRoutedDeviceId = native_getRoutedDeviceId();
        if (iNative_getRoutedDeviceId == 0) {
            return null;
        }
        return AudioManager.getDeviceForPortId(iNative_getRoutedDeviceId, 1);
    }

    public void h() {
        Iterator it = this.f6695k.iterator();
        while (it.hasNext()) {
            ((OutputSink) it.next()).i();
        }
        native_release();
    }

    @Override // android.media.AudioRouting
    public void removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener onRoutingChangedListener) {
        synchronized (this.f6697m) {
            try {
                if (this.f6697m.containsKey(onRoutingChangedListener)) {
                    this.f6697m.remove(onRoutingChangedListener);
                    f(false);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public native void setAudioCapturePoint(int i2) throws IllegalStateException;

    public native void setAudioEncoder(int i2) throws IllegalStateException;

    public native void setMicrophoneChannels(int i2) throws IllegalStateException;

    public native void setMicrophoneSamplingRate(int i2) throws IllegalStateException;

    public native void setMicrophoneSource(int i2) throws IllegalStateException;

    @Override // android.media.AudioRouting
    public boolean setPreferredDevice(AudioDeviceInfo audioDeviceInfo) {
        if (audioDeviceInfo != null && !audioDeviceInfo.isSource()) {
            return false;
        }
        boolean zNative_setMicrophoneInputDevice = native_setMicrophoneInputDevice(audioDeviceInfo != null ? audioDeviceInfo.getId() : 0);
        if (zNative_setMicrophoneInputDevice) {
            synchronized (this) {
                this.f6696l = audioDeviceInfo;
            }
        }
        return zNative_setMicrophoneInputDevice;
    }

    public boolean setPreferredMicrophoneDirection(int i2) {
        return native_setMicrophonePreferredDirection(i2) == 0;
    }

    public boolean setPreferredMicrophoneFieldDimension(float f2) {
        Preconditions.checkArgument(f2 >= -1.0f && f2 <= 1.0f, "Argument must fall between -1 & 1 (inclusive)");
        return native_setMicrophonePreferredFieldDimension(f2) == 0;
    }

    public native void start() throws IllegalStateException;

    public native void stop() throws IllegalStateException;
}
