package com.ifpdos.factorytest.mtr;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioDeviceInfo;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.util.Log;
import kotlin.Metadata;
import kotlin.ResultKt;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlin.coroutines.intrinsics.IntrinsicsKt;
import kotlin.coroutines.jvm.internal.Boxing;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.Intrinsics;
import kotlinx.coroutines.BuildersKt__Builders_commonKt;
import kotlinx.coroutines.CoroutineScope;
import kotlinx.coroutines.CoroutineScopeKt;
import kotlinx.coroutines.Dispatchers;

/* JADX INFO: compiled from: HDMIIngestAudioHandler.kt */
/* JADX INFO: loaded from: classes.dex */
@Metadata(d1 = {"\u0000J\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\b\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\u0002\n\u0002\b\u0005\u0018\u0000 \u001f2\u00020\u0001:\u0001\u001fB\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\n\u0010\u0015\u001a\u0004\u0018\u00010\u0016H\u0002J\u0010\u0010\u0017\u001a\u00020\u00102\u0006\u0010\u0018\u001a\u00020\u0016H\u0002J\u0010\u0010\u0019\u001a\u00020\u00102\u0006\u0010\u0018\u001a\u00020\u0016H\u0002J\b\u0010\u001a\u001a\u00020\u001bH\u0002J\u0006\u0010\u001c\u001a\u00020\u001bJ\b\u0010\u001d\u001a\u00020\u001bH\u0003J\u0006\u0010\u001e\u001a\u00020\u001bR\u000e\u0010\u0005\u001a\u00020\u0006X\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0007\u001a\u0004\u0018\u00010\bX\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\t\u001a\u0004\u0018\u00010\nX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u0012\u0010\u000b\u001a\u00020\fX\u0096\u0005¢\u0006\u0006\u001a\u0004\b\r\u0010\u000eR\u000e\u0010\u000f\u001a\u00020\u0010X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0011\u001a\u00020\u0012X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0013\u001a\u00020\u0010X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0014\u001a\u00020\u0010X\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006 "}, d2 = {"Lcom/ifpdos/factorytest/mtr/HDMIIngestAudioHandler;", "Lkotlinx/coroutines/CoroutineScope;", "context", "Landroid/content/Context;", "(Landroid/content/Context;)V", "audioManager", "Landroid/media/AudioManager;", "audioRecord", "Landroid/media/AudioRecord;", "audioTrack", "Landroid/media/AudioTrack;", "coroutineContext", "Lkotlin/coroutines/CoroutineContext;", "getCoroutineContext", "()Lkotlin/coroutines/CoroutineContext;", "firstRead", "", "hasReadSize", "", "isStartedPlay", "isStartedRecord", "getHDMIAudio", "Landroid/media/AudioDeviceInfo;", "isHDMIAudioDevice", "audioDeviceInfo", "isUSBAudioDevice", "play", "", "start", "startRecord", "stop", "Companion", "app_generalRelease"}, k = 1, mv = {1, 8, 0}, xi = 48)
public final class HDMIIngestAudioHandler implements CoroutineScope {
    private static final int DEFAULT_AUDIO_FORMAT = 2;
    private static final int DEFAULT_BITS_PER_SAMPLE = 16;
    private static final int DEFAULT_CHANNEL_CONFIG = 12;
    private static final int DEFAULT_FORMAT_TAG = 1;
    private static final int DEFAULT_SAMPLE_RATE = 48000;
    private static final String PRODUCT_NAME_HDMI_LABEL = "HDMI";
    private static final String TAG = "HDMIIngestAudioHandler";
    public static final int recordSampleRteHz = 48000;
    private final /* synthetic */ CoroutineScope $$delegate_0;
    private final AudioManager audioManager;
    private AudioRecord audioRecord;
    private AudioTrack audioTrack;
    private final Context context;
    private boolean firstRead;
    private int hasReadSize;
    private boolean isStartedPlay;
    private boolean isStartedRecord;

    @Override // kotlinx.coroutines.CoroutineScope
    public CoroutineContext getCoroutineContext() {
        return this.$$delegate_0.getCoroutineContext();
    }

    public HDMIIngestAudioHandler(Context context) {
        Intrinsics.checkNotNullParameter(context, "context");
        this.context = context;
        this.$$delegate_0 = CoroutineScopeKt.MainScope();
        Object systemService = context.getSystemService("audio");
        Intrinsics.checkNotNull(systemService, "null cannot be cast to non-null type android.media.AudioManager");
        this.audioManager = (AudioManager) systemService;
        this.firstRead = true;
        Log.d(TAG, "getHDMIAudio: audioDeviceInfo = " + getHDMIAudio());
    }

    public final void start() {
        Log.d(TAG, "start");
        play();
        startRecord();
    }

    private final void startRecord() {
        Log.d(TAG, "startRecord");
        if (this.isStartedRecord) {
            return;
        }
        BuildersKt__Builders_commonKt.launch$default(this, Dispatchers.getIO(), null, new AnonymousClass1(null), 2, null);
    }

    /* JADX INFO: renamed from: com.ifpdos.factorytest.mtr.HDMIIngestAudioHandler$startRecord$1, reason: invalid class name */
    /* JADX INFO: compiled from: HDMIIngestAudioHandler.kt */
    @Metadata(d1 = {"\u0000\n\n\u0000\n\u0002\u0010\u0002\n\u0002\u0018\u0002\u0010\u0000\u001a\u00020\u0001*\u00020\u0002H\u008a@"}, d2 = {"<anonymous>", "", "Lkotlinx/coroutines/CoroutineScope;"}, k = 3, mv = {1, 8, 0}, xi = 48)
    @DebugMetadata(c = "com.ifpdos.factorytest.mtr.HDMIIngestAudioHandler$startRecord$1", f = "HDMIIngestAudioHandler.kt", i = {}, l = {}, m = "invokeSuspend", n = {}, s = {})
    static final class AnonymousClass1 extends SuspendLambda implements Function2<CoroutineScope, Continuation<? super Unit>, Object> {
        int label;

        AnonymousClass1(Continuation<? super AnonymousClass1> continuation) {
            super(2, continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Continuation<Unit> create(Object obj, Continuation<?> continuation) {
            return HDMIIngestAudioHandler.this.new AnonymousClass1(continuation);
        }

        @Override // kotlin.jvm.functions.Function2
        public final Object invoke(CoroutineScope coroutineScope, Continuation<? super Unit> continuation) {
            return ((AnonymousClass1) create(coroutineScope, continuation)).invokeSuspend(Unit.INSTANCE);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Object invokeSuspend(Object obj) throws Throwable {
            StringBuilder sb;
            int i;
            int i2;
            AudioTrack audioTrack;
            IntrinsicsKt.getCOROUTINE_SUSPENDED();
            if (this.label != 0) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            ResultKt.throwOnFailure(obj);
            char c = 2;
            int i3 = 0;
            try {
                try {
                    int minBufferSize = AudioRecord.getMinBufferSize(48000, 16, 2);
                    AudioRecord audioRecord = HDMIIngestAudioHandler.this.audioRecord;
                    if (audioRecord != null) {
                        audioRecord.stop();
                    }
                    AudioRecord audioRecord2 = HDMIIngestAudioHandler.this.audioRecord;
                    if (audioRecord2 != null) {
                        audioRecord2.release();
                    }
                    HDMIIngestAudioHandler.this.audioRecord = new AudioRecord(1, 48000, 16, 2, minBufferSize);
                    AudioDeviceInfo hDMIAudio = HDMIIngestAudioHandler.this.getHDMIAudio();
                    AudioRecord audioRecord3 = HDMIIngestAudioHandler.this.audioRecord;
                    if (audioRecord3 != null) {
                        audioRecord3.setPreferredDevice(hDMIAudio);
                    }
                    short[] sArr = new short[minBufferSize];
                    AudioRecord audioRecord4 = HDMIIngestAudioHandler.this.audioRecord;
                    if (audioRecord4 != null) {
                        audioRecord4.startRecording();
                    }
                    char c2 = 1;
                    HDMIIngestAudioHandler.this.isStartedRecord = true;
                    Log.d(HDMIIngestAudioHandler.TAG, "startRecord. success. isStarted: " + HDMIIngestAudioHandler.this.isStartedRecord);
                    while (HDMIIngestAudioHandler.this.audioRecord != null && HDMIIngestAudioHandler.this.isStartedRecord) {
                        AudioRecord audioRecord5 = HDMIIngestAudioHandler.this.audioRecord;
                        Intrinsics.checkNotNull(audioRecord5);
                        int i4 = audioRecord5.read(sArr, i3, minBufferSize);
                        HDMIIngestAudioHandler.this.hasReadSize += i4;
                        if (HDMIIngestAudioHandler.this.hasReadSize >= 1000000 || HDMIIngestAudioHandler.this.firstRead) {
                            Log.d(HDMIIngestAudioHandler.TAG, "log once readSize: " + i4 + ". hasReadSize: " + HDMIIngestAudioHandler.this.hasReadSize + ". ");
                            if (minBufferSize >= 8) {
                                i = minBufferSize;
                                Log.d(HDMIIngestAudioHandler.TAG, "data: " + ((int) sArr[i3]) + ", " + ((int) sArr[c2]) + ", " + ((int) sArr[c]) + ", " + ((int) sArr[3]) + ", " + ((int) sArr[4]) + ", " + ((int) sArr[5]) + ", " + ((int) sArr[6]) + ", " + ((int) sArr[7]));
                            } else {
                                i = minBufferSize;
                            }
                            i2 = 0;
                            HDMIIngestAudioHandler.this.hasReadSize = 0;
                            HDMIIngestAudioHandler.this.firstRead = false;
                        } else {
                            i = minBufferSize;
                            i2 = i3;
                        }
                        if (i4 <= 0 || (audioTrack = HDMIIngestAudioHandler.this.audioTrack) == null) {
                            i3 = i2;
                            minBufferSize = i;
                            c = 2;
                        } else {
                            Boxing.boxInt(audioTrack.write(sArr, i2, i4));
                            minBufferSize = i;
                            c = 2;
                            i3 = 0;
                        }
                        c2 = 1;
                    }
                    HDMIIngestAudioHandler.this.isStartedRecord = false;
                    try {
                        AudioRecord audioRecord6 = HDMIIngestAudioHandler.this.audioRecord;
                        if (audioRecord6 != null) {
                            audioRecord6.stop();
                        }
                        AudioRecord audioRecord7 = HDMIIngestAudioHandler.this.audioRecord;
                        if (audioRecord7 != null) {
                            audioRecord7.release();
                        }
                    } catch (Exception e) {
                        e = e;
                        sb = new StringBuilder("startRecord finally error: ");
                        Log.e(HDMIIngestAudioHandler.TAG, sb.append(e).toString());
                    }
                } catch (Exception e2) {
                    Log.e(HDMIIngestAudioHandler.TAG, "startRecord error: " + e2);
                    HDMIIngestAudioHandler.this.isStartedRecord = false;
                    try {
                        AudioRecord audioRecord8 = HDMIIngestAudioHandler.this.audioRecord;
                        if (audioRecord8 != null) {
                            audioRecord8.stop();
                        }
                        AudioRecord audioRecord9 = HDMIIngestAudioHandler.this.audioRecord;
                        if (audioRecord9 != null) {
                            audioRecord9.release();
                        }
                    } catch (Exception e3) {
                        e = e3;
                        sb = new StringBuilder("startRecord finally error: ");
                        Log.e(HDMIIngestAudioHandler.TAG, sb.append(e).toString());
                    }
                }
                HDMIIngestAudioHandler.this.audioRecord = null;
                return Unit.INSTANCE;
            } catch (Throwable th) {
                HDMIIngestAudioHandler.this.isStartedRecord = false;
                try {
                    AudioRecord audioRecord10 = HDMIIngestAudioHandler.this.audioRecord;
                    if (audioRecord10 != null) {
                        audioRecord10.stop();
                    }
                    AudioRecord audioRecord11 = HDMIIngestAudioHandler.this.audioRecord;
                    if (audioRecord11 != null) {
                        audioRecord11.release();
                    }
                } catch (Exception e4) {
                    Log.e(HDMIIngestAudioHandler.TAG, "startRecord finally error: " + e4);
                }
                HDMIIngestAudioHandler.this.audioRecord = null;
                throw th;
            }
        }
    }

    private final void play() {
        Log.d(TAG, "play. isStartedPlay: " + this.isStartedPlay);
        if (this.isStartedPlay) {
            return;
        }
        AudioAttributes audioAttributesBuild = new AudioAttributes.Builder().setUsage(1).setContentType(2).build();
        AudioTrack audioTrackBuild = new AudioTrack.Builder().setAudioAttributes(audioAttributesBuild).setAudioFormat(new AudioFormat.Builder().setEncoding(2).setSampleRate(48000).setChannelMask(12).build()).setBufferSizeInBytes(AudioTrack.getMinBufferSize(48000, 12, 2)).setTransferMode(1).build();
        this.audioTrack = audioTrackBuild;
        if (audioTrackBuild != null) {
            audioTrackBuild.play();
        }
    }

    public final void stop() {
        Log.d(TAG, "stop");
        this.isStartedRecord = false;
        this.isStartedPlay = false;
        AudioTrack audioTrack = this.audioTrack;
        if (audioTrack != null) {
            audioTrack.stop();
        }
        this.audioTrack = null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final AudioDeviceInfo getHDMIAudio() {
        AudioDeviceInfo[] devices = this.audioManager.getDevices(1);
        if (devices != null) {
            for (AudioDeviceInfo audioDeviceInfo : devices) {
                Log.d(TAG, "getHDMIAudio: type = " + audioDeviceInfo.getType() + ", productName = " + ((Object) audioDeviceInfo.getProductName()));
                Intrinsics.checkNotNullExpressionValue(audioDeviceInfo, "audioDeviceInfo");
                if (isHDMIAudioDevice(audioDeviceInfo) || isUSBAudioDevice(audioDeviceInfo)) {
                    return audioDeviceInfo;
                }
            }
        }
        return null;
    }

    private final boolean isHDMIAudioDevice(AudioDeviceInfo audioDeviceInfo) {
        return audioDeviceInfo.getType() == 9;
    }

    private final boolean isUSBAudioDevice(AudioDeviceInfo audioDeviceInfo) {
        return audioDeviceInfo.getType() == 11;
    }
}
