package g0;

import android.content.Context;
import android.media.AudioRecord;
import android.media.audiofx.NoiseSuppressor;
import android.media.projection.MediaProjection;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import f0.C0463a;
import g0.C0488l;
import j0.C0551a;

/* JADX INFO: renamed from: g0.l, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0488l extends AbstractC0487k {

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final a f4057k = new a(null);

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

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final MediaProjection.Callback f4061i = new b();

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final Handler f4062j = new Handler(Looper.getMainLooper());

    /* JADX INFO: renamed from: g0.l$a */
    public static final class a {
        public /* synthetic */ a(kotlin.jvm.internal.g gVar) {
            this();
        }

        private a() {
        }
    }

    /* JADX INFO: renamed from: g0.l$b */
    public static final class b extends MediaProjection.Callback {
        b() {
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static final void b(C0488l this$0) {
            kotlin.jvm.internal.l.e(this$0, "this$0");
            Log.d("SystemAudioRecorder", "onStop: MediaProjection, restart");
            this$0.startRecording();
            this$0.d();
        }

        @Override // android.media.projection.MediaProjection.Callback
        public void onStop() {
            super.onStop();
            Log.d("SystemAudioRecorder", "onStop: MediaProjection, " + C0488l.this.b());
            if (C0488l.this.b() && o0.e.f5989a.a(C0551a.a())) {
                C0488l.this.c(2);
                MediaProjection mediaProjection = C0488l.this.f4060h;
                if (mediaProjection != null) {
                    mediaProjection.unregisterCallback(this);
                }
                j0.f.f4519a.e();
                C0488l.this.stop();
                Handler handler = C0488l.this.f4062j;
                final C0488l c0488l = C0488l.this;
                handler.postDelayed(new Runnable() { // from class: g0.m
                    @Override // java.lang.Runnable
                    public final void run() {
                        C0488l.b.b(c0488l);
                    }
                }, 500L);
            }
        }
    }

    @Override // g0.InterfaceC0480d
    public int configure(int i2, int i3, int i4) {
        this.f4058f = i2;
        this.f4059g = i3 == 1 ? 16 : 12;
        return 0;
    }

    @Override // g0.AbstractC0487k, g0.InterfaceC0480d
    public int read(byte[] audioData, int i2, int i3) {
        kotlin.jvm.internal.l.e(audioData, "audioData");
        int i4 = super.read(audioData, i2, i3);
        if (i4 > 0) {
            return i4;
        }
        try {
            AudioRecord audioRecordA = a();
            if (audioRecordA == null) {
                return -1;
            }
            int i5 = audioRecordA.read(audioData, i2, i3);
            o0.g.I.saveData(audioData, 1);
            if (i5 < 0) {
                Q0.e.b("SystemAudioRecorder", "read audio data failed, " + i5);
            }
            return i5;
        } catch (Throwable th) {
            Log.e("SystemAudioRecorder", "read: error, " + th);
            return -1;
        }
    }

    @Override // g0.InterfaceC0480d
    public int release() {
        try {
            f(false);
            MediaProjection mediaProjection = this.f4060h;
            if (mediaProjection != null) {
                mediaProjection.unregisterCallback(this.f4061i);
            }
            AudioRecord audioRecordA = a();
            if (audioRecordA != null) {
                audioRecordA.release();
            }
            e(null);
            Log.d("SystemAudioRecorder", "release: end");
            return 0;
        } catch (Throwable th) {
            Log.e("SystemAudioRecorder", "release: error, " + th);
            return -1;
        }
    }

    @Override // g0.InterfaceC0480d
    public int startRecording() {
        try {
            if (o0.h.f5991a.p()) {
                MediaProjection mediaProjectionC = j0.f.b().c(C0551a.a());
                this.f4060h = mediaProjectionC;
                if (mediaProjectionC != null) {
                    mediaProjectionC.registerCallback(this.f4061i, this.f4062j);
                }
            }
            f0.c cVar = f0.c.f3903a;
            Context contextA = C0551a.a();
            MediaProjection mediaProjection = this.f4060h;
            int i2 = this.f4058f;
            int i3 = this.f4059g;
            e(cVar.a(contextA, mediaProjection, new C0463a.C0071a(i2, i3, i3 == 12 ? 2 : 1)));
            AudioRecord audioRecordA = a();
            if (audioRecordA != null) {
                audioRecordA.startRecording();
            }
            AudioRecord audioRecordA2 = a();
            if (audioRecordA2 != null) {
                NoiseSuppressor.create(audioRecordA2.getAudioSessionId());
            }
            f(true);
            o0.g.I.start(1);
            Log.d("SystemAudioRecorder", "startRecording: success");
            return 0;
        } catch (Throwable th) {
            Log.e("SystemAudioRecorder", "startRecording: error, " + th);
            return -1;
        }
    }

    @Override // g0.InterfaceC0480d
    public int stop() {
        try {
            o0.g.I.stop(1);
            AudioRecord audioRecordA = a();
            if (audioRecordA != null) {
                audioRecordA.stop();
            }
            f(false);
            Log.d("SystemAudioRecorder", "stop: end");
            return 0;
        } catch (Throwable th) {
            Log.e("SystemAudioRecorder", "stop: error, " + th);
            return -1;
        }
    }
}
