package defpackage;

import android.media.AudioTimestamp;
import android.media.AudioTrack;
import android.media.metrics.LogSessionId;
import android.os.Build;
import androidx.media3.exoplayer.audio.AudioOutput$WriteException;
import java.lang.reflect.Method;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.concurrent.ScheduledExecutorService;

/* JADX INFO: renamed from: ᵫᵬˏ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C4000 {

    /* JADX INFO: renamed from: ˊᵨᵧ, reason: contains not printable characters */
    public static ScheduledExecutorService f15308;

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public static int f15309;

    /* JADX INFO: renamed from: ˏːˆ, reason: contains not printable characters */
    public static final Object f15310 = new Object();

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final C2558 f15311;

    /* JADX INFO: renamed from: ˉᵲ, reason: contains not printable characters */
    public boolean f15312;

    /* JADX INFO: renamed from: ˊᵙᵾᵲ, reason: contains not printable characters */
    public int f15313;

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public final int f15314;

    /* JADX INFO: renamed from: ˋˈ, reason: contains not printable characters */
    public long f15315;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final float f15316;

    /* JADX INFO: renamed from: ᵝᵶ, reason: contains not printable characters */
    public long f15317;

    /* JADX INFO: renamed from: ᵟᵹᵙ, reason: contains not printable characters */
    public int f15318;

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public C2201 f15319;

    /* JADX INFO: renamed from: ᵪᵹᵥᵻ, reason: contains not printable characters */
    public ByteBuffer f15320;

    /* JADX INFO: renamed from: ᵮᵣᵗˈ, reason: contains not printable characters */
    public long f15321;

    /* JADX INFO: renamed from: ᵱʡˏ, reason: contains not printable characters */
    public int f15322;

    /* JADX INFO: renamed from: ᵱᵽʡᵚ, reason: contains not printable characters */
    public final C3252 f15323 = new C3252(Thread.currentThread());

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final AudioTrack f15324;

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final boolean f15325;

    /* JADX INFO: renamed from: ᵶᵥᵝ, reason: contains not printable characters */
    public boolean f15326;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final ᵟˑʽ f15327;

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public final C1704 f15328;

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final C1371 f15329;

    public C4000(AudioTrack audioTrack, C2558 c2558, ᵟˑʽ r10, float f, C2353 c2353) {
        this.f15324 = audioTrack;
        this.f15311 = c2558;
        this.f15316 = f;
        this.f15327 = r10;
        boolean zM3253 = AbstractC0956.m3253(c2558.f9895);
        this.f15325 = zM3253;
        if (zM3253) {
            this.f15314 = AbstractC0956.m3227(c2558.f9895) * Integer.bitCount(c2558.f9892);
        } else {
            this.f15314 = -1;
        }
        this.f15329 = new C1371(new ᵧᵙᵤ(5, this), c2353, audioTrack, c2558.f9895, this.f15314, c2558.f9900);
        if (Build.VERSION.SDK_INT >= 24 && r10 != null) {
            this.f15319 = new C2201(audioTrack, r10);
        }
        this.f15328 = m8777() ? new C1704(this) : null;
    }

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final long m8776() {
        if (!this.f15325) {
            return this.f15317;
        }
        long j = this.f15321;
        long j2 = this.f15314;
        String str = AbstractC0956.f4222;
        return ((j + j2) - 1) / j2;
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final boolean m8777() {
        return Build.VERSION.SDK_INT >= 29 && this.f15324.isOffloadedPlayback();
    }

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final void m8778() {
        if (Build.VERSION.SDK_INT < 29) {
            return;
        }
        AudioTrack audioTrack = this.f15324;
        if (audioTrack.getPlayState() != 3) {
            return;
        }
        audioTrack.setOffloadEndOfStream();
        C1371 c1371 = this.f15329;
        c1371.f5685 = true;
        c1371.f5667.f8192.f14159 = true;
    }

    /* JADX WARN: Removed duplicated region for block: B:105:0x02cc  */
    /* JADX WARN: Removed duplicated region for block: B:106:0x02ce  */
    /* JADX WARN: Removed duplicated region for block: B:108:0x02d1  */
    /* JADX WARN: Removed duplicated region for block: B:109:0x02ed  */
    /* JADX WARN: Removed duplicated region for block: B:112:0x02fa  */
    /* JADX WARN: Removed duplicated region for block: B:127:0x0347  */
    /* JADX WARN: Removed duplicated region for block: B:87:0x026a  */
    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long m8779() {
        C1371 c1371;
        C2353 c2353;
        AudioTrack audioTrack;
        long j;
        long j2;
        boolean z;
        boolean z2;
        C1371 c13712;
        long jM3915;
        int playState;
        int i;
        C3718 c3718;
        float f;
        AudioTimestamp audioTimestamp;
        boolean z3;
        float f2;
        int i2;
        float f3;
        Method method;
        C1371 c13713 = this.f15329;
        C2353 c23532 = c13713.f5662;
        C2068 c2068 = c13713.f5667;
        AudioTrack audioTrack2 = c13713.f5687;
        if (audioTrack2.getPlayState() == 3) {
            long[] jArr = c13713.f5671;
            c23532.getClass();
            long jNanoTime = System.nanoTime() / 1000;
            if (jNanoTime - c13713.f5679 >= 30000) {
                j = 1000;
                long jM3230 = AbstractC0956.m3230(c13713.m3917(), c13713.f5676);
                if (jM3230 != 0) {
                    jArr[c13713.f5664] = AbstractC0956.m3223(jM3230, c13713.f5688) - jNanoTime;
                    c13713.f5664 = (c13713.f5664 + 1) % 10;
                    int i3 = c13713.f5670;
                    if (i3 < 10) {
                        c13713.f5670 = i3 + 1;
                    }
                    c13713.f5679 = jNanoTime;
                    c13713.f5686 = 0L;
                    int i4 = 0;
                    while (true) {
                        int i5 = c13713.f5670;
                        if (i4 >= i5) {
                            break;
                        }
                        int i6 = i4;
                        c13713.f5686 = (jArr[i6] / ((long) i5)) + c13713.f5686;
                        i4 = i6 + 1;
                    }
                } else {
                    c1371 = c13713;
                    c2353 = c23532;
                    audioTrack = audioTrack2;
                }
            } else {
                j = 1000;
            }
            long j3 = c13713.f5668;
            if (c13713.f5684 && (method = c13713.f5674) != null && jNanoTime - c13713.f5678 >= 500000) {
                try {
                    Integer num = (Integer) method.invoke(audioTrack2, null);
                    String str = AbstractC0956.f4222;
                    long jIntValue = (((long) num.intValue()) * j) - c13713.f5689;
                    c13713.f5668 = jIntValue;
                    long jMax = Math.max(jIntValue, 0L);
                    c13713.f5668 = jMax;
                    if (jMax > 10000000) {
                        ˋˀᵧ.ʿٴˇ("Ignoring impossibly large audio latency: " + jMax);
                        c13713.f5668 = 0L;
                    }
                } catch (Exception unused) {
                    c13713.f5674 = null;
                }
                c13713.f5678 = jNanoTime;
            }
            boolean z4 = j3 != c13713.f5668;
            float f4 = c13713.f5688;
            long jM39152 = c13713.m3915(jNanoTime);
            C3718 c37182 = c2068.f8192;
            C3718 c37183 = c2068.f8192;
            int i7 = c2068.f8188;
            if (!z4) {
                j2 = 0;
                c3718 = c37182;
                if (jNanoTime - c2068.f8193 < c2068.f8196) {
                    c1371 = c13713;
                    c2353 = c23532;
                    audioTrack = audioTrack2;
                }
                z = false;
                c2353.getClass();
                long jNanoTime2 = System.nanoTime() / j;
                z2 = c2068.f8194 == 2 ? true : z;
                if (z2) {
                    c13712 = c1371;
                    float f5 = c13712.f5688;
                    C3718 c37184 = c2068.f8192;
                    jM3915 = AbstractC0956.m3269(jNanoTime2 - (c37184.f14153.nanoTime / j), f5) + AbstractC0956.m3230(c37184.f14155, c2068.f8188);
                } else {
                    c13712 = c1371;
                    jM3915 = c13712.m3915(jNanoTime2);
                }
                playState = audioTrack.getPlayState();
                if (playState == 3) {
                    if (z2 || ((i = c2068.f8194) != 0 && i != 1)) {
                        c13712.m3918(jM3915);
                    }
                    long j4 = c13712.f5680;
                    if (j4 != -9223372036854775807L) {
                        long j5 = jM3915 - c13712.f5677;
                        long jM3269 = AbstractC0956.m3269(jNanoTime2 - j4, c13712.f5688);
                        long j6 = c13712.f5677 + jM3269;
                        long jAbs = Math.abs(j6 - jM3915);
                        if (j5 != j2 && jAbs < 1000000) {
                            long j7 = (jM3269 * 10) / 100;
                            jM3915 = AbstractC0956.m3267(jM3915, j6 - j7, j6 + j7);
                        }
                    }
                    c13712.f5680 = jNanoTime2;
                    c13712.f5677 = jM3915;
                } else if (playState == 1) {
                    c13712.m3918(jM3915);
                }
                return jM3915;
            }
            c3718 = c37182;
            j2 = 0;
            c2068.f8193 = jNanoTime;
            C3718 c37185 = c3718;
            AudioTrack audioTrack3 = c37185.f14156;
            AudioTimestamp audioTimestamp2 = c37185.f14153;
            boolean timestamp = audioTrack3.getTimestamp(audioTimestamp2);
            if (timestamp) {
                f = f4;
                long j8 = audioTimestamp2.framePosition;
                c1371 = c13713;
                c2353 = c23532;
                long j9 = c37185.f14158;
                if (j9 > j8) {
                    if (c37185.f14159) {
                        c37185.f14157 += j9;
                        c37185.f14159 = false;
                    } else {
                        c37185.f14154++;
                    }
                }
                c37185.f14158 = j8;
                c37185.f14155 = j8 + c37185.f14157 + (c37185.f14154 << 32);
            } else {
                c1371 = c13713;
                c2353 = c23532;
                f = f4;
            }
            if (timestamp) {
                ᵧᵙᵤ r1 = c2068.f8190;
                long j10 = audioTimestamp2.nanoTime / j;
                audioTimestamp = audioTimestamp2;
                z3 = timestamp;
                float f6 = f;
                long jM32692 = AbstractC0956.m3269(jNanoTime - (c37183.f14153.nanoTime / j), f6) + AbstractC0956.m3230(c37183.f14155, i7);
                if (Math.abs(j10 - jNanoTime) > 5000000) {
                    long j11 = c37185.f14155;
                    r1.getClass();
                    audioTrack = audioTrack2;
                    StringBuilder sb = new StringBuilder("Spurious audio timestamp (system clock mismatch): ");
                    sb.append(j11);
                    sb.append(", ");
                    sb.append(j10);
                    AbstractC3653.m8236(sb, ", ", jNanoTime, ", ");
                    sb.append(jM39152);
                    sb.append(", ");
                    sb.append(((C4000) r1.ᵜᵪʾ).m8776());
                    ˋˀᵧ.ʿٴˇ(sb.toString());
                    i2 = 4;
                    c2068.m5511(4);
                    f2 = f6;
                } else {
                    audioTrack = audioTrack2;
                    if (Math.abs(jM32692 - jM39152) > 5000000) {
                        long j12 = c37185.f14155;
                        r1.getClass();
                        f3 = f6;
                        StringBuilder sb2 = new StringBuilder("Spurious audio timestamp (frame position mismatch): ");
                        sb2.append(j12);
                        sb2.append(", ");
                        sb2.append(j10);
                        AbstractC3653.m8236(sb2, ", ", jNanoTime, ", ");
                        sb2.append(jM39152);
                        sb2.append(", ");
                        sb2.append(((C4000) r1.ᵜᵪʾ).m8776());
                        ˋˀᵧ.ʿٴˇ(sb2.toString());
                        i2 = 4;
                        c2068.m5511(4);
                    } else {
                        f3 = f6;
                        i2 = 4;
                        if (c2068.f8194 == 4) {
                            c2068.m5511(0);
                        }
                    }
                    f2 = f3;
                }
            } else {
                audioTimestamp = audioTimestamp2;
                z3 = timestamp;
                f2 = f;
                i2 = 4;
                audioTrack = audioTrack2;
            }
            int i8 = c2068.f8194;
            if (i8 == 0) {
                AudioTimestamp audioTimestamp3 = audioTimestamp;
                z = false;
                if (z3) {
                    long j13 = audioTimestamp3.nanoTime;
                    if (j13 / j >= c2068.f8191) {
                        c2068.f8189 = c37185.f14155;
                        c2068.f8195 = j13 / j;
                        c2068.m5511(1);
                    }
                } else if (jNanoTime - c2068.f8191 > 500000) {
                    c2068.m5511(3);
                }
            } else if (i8 != 1) {
                if (i8 != 2) {
                    if (i8 != 3) {
                        if (i8 != i2) {
                            ᵷᵝˎ.ᵱʡˏ();
                            return j2;
                        }
                    } else if (z3) {
                        z = false;
                        c2068.m5511(0);
                    }
                } else if (!z3) {
                    c2068.m5511(0);
                }
                z = false;
            } else if (z3) {
                long j14 = c37185.f14155;
                long j15 = c2068.f8189;
                if (j14 <= j15) {
                    if (jNanoTime - c2068.f8191 > 2000000) {
                        c2068.m5511(3);
                    } else {
                        c2068.f8189 = c37185.f14155;
                        c2068.f8195 = audioTimestamp.nanoTime / j;
                    }
                    z = false;
                } else {
                    if (Math.abs((AbstractC0956.m3269(jNanoTime - (c37183.f14153.nanoTime / j), f2) + AbstractC0956.m3230(c37183.f14155, i7)) - (AbstractC0956.m3269(jNanoTime - c2068.f8195, f2) + AbstractC0956.m3230(j15, i7))) < j) {
                        c2068.m5511(2);
                    }
                    z = false;
                }
            } else {
                z = false;
                c2068.m5511(0);
            }
            c2353.getClass();
            long jNanoTime22 = System.nanoTime() / j;
            if (c2068.f8194 == 2) {
            }
            if (z2) {
            }
            playState = audioTrack.getPlayState();
            if (playState == 3) {
            }
            return jM3915;
        }
        c1371 = c13713;
        c2353 = c23532;
        audioTrack = audioTrack2;
        j = 1000;
        j2 = 0;
        z = false;
        c2353.getClass();
        long jNanoTime222 = System.nanoTime() / j;
        if (c2068.f8194 == 2) {
        }
        if (z2) {
        }
        playState = audioTrack.getPlayState();
        if (playState == 3) {
        }
        return jM3915;
    }

    /* JADX WARN: Removed duplicated region for block: B:53:0x00ff  */
    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean m8780(int i, long j, ByteBuffer byteBuffer) {
        AudioTrack audioTrack;
        int iWrite;
        boolean z;
        ᵟˑʽ r0;
        C0453 c0453;
        C2939 c2939;
        int underrunCount;
        long j2 = j;
        C2558 c2558 = this.f15311;
        boolean z2 = this.f15325;
        if (!z2 && this.f15322 == 0) {
            this.f15322 = C0784.m2799(c2558.f9895, byteBuffer);
        }
        C3252 c3252 = this.f15323;
        c3252.getClass();
        Thread threadCurrentThread = Thread.currentThread();
        Thread thread = c3252.f12370;
        AudioTrack audioTrack2 = this.f15324;
        if (threadCurrentThread == thread) {
            long jM8776 = m8776();
            if (Build.VERSION.SDK_INT >= 24) {
                underrunCount = audioTrack2.getUnderrunCount();
                audioTrack = audioTrack2;
            } else {
                boolean z3 = this.f15312;
                long jM8779 = m8779();
                int sampleRate = audioTrack2.getSampleRate();
                String str = AbstractC0956.f4222;
                audioTrack = audioTrack2;
                boolean z4 = jM8776 > AbstractC0956.m3218(jM8779, (long) sampleRate, 1000000L, RoundingMode.UP);
                this.f15312 = z4;
                underrunCount = (!z3 || z4 || audioTrack.getPlayState() == 1) ? this.f15313 : this.f15313 + 1;
            }
            boolean z5 = underrunCount > this.f15313;
            this.f15313 = underrunCount;
            if (z5) {
                c3252.m7412(-1, new ᵕᵩʽᵲ(0));
            }
        } else {
            audioTrack = audioTrack2;
        }
        int iRemaining = byteBuffer.remaining();
        if (c2558.f9898) {
            if (j2 == Long.MIN_VALUE) {
                j2 = this.f15315;
            } else {
                this.f15315 = j2;
            }
            int iRemaining2 = byteBuffer.remaining();
            if (Build.VERSION.SDK_INT >= 26) {
                iWrite = audioTrack.write(byteBuffer, iRemaining2, 1, j2 * 1000);
            } else {
                AudioTrack audioTrack3 = audioTrack;
                if (this.f15320 == null) {
                    ByteBuffer byteBufferAllocate = ByteBuffer.allocate(16);
                    this.f15320 = byteBufferAllocate;
                    byteBufferAllocate.order(ByteOrder.BIG_ENDIAN);
                    this.f15320.putInt(1431633921);
                }
                if (this.f15318 == 0) {
                    this.f15320.putInt(4, iRemaining2);
                    this.f15320.putLong(8, j2 * 1000);
                    this.f15320.position(0);
                    this.f15318 = iRemaining2;
                }
                int iRemaining3 = this.f15320.remaining();
                if (iRemaining3 <= 0) {
                    iWrite = audioTrack3.write(byteBuffer, iRemaining2, 1);
                    if (iWrite < 0) {
                        this.f15318 = 0;
                    } else {
                        this.f15318 -= iWrite;
                    }
                } else {
                    int iWrite2 = audioTrack3.write(this.f15320, iRemaining3, 1);
                    if (iWrite2 < 0) {
                        this.f15318 = 0;
                        iWrite = iWrite2;
                    } else if (iWrite2 < iRemaining3) {
                        iWrite = 0;
                    }
                }
            }
        } else {
            iWrite = audioTrack.write(byteBuffer, byteBuffer.remaining(), 1);
        }
        if (iWrite >= 0) {
            z = iWrite == iRemaining;
            if (z2) {
                this.f15321 += (long) iWrite;
                return z;
            }
            if (z) {
                this.f15317 = (((long) this.f15322) * ((long) i)) + this.f15317;
            }
            return z;
        }
        z = (Build.VERSION.SDK_INT >= 24 && iWrite == -6) || iWrite == -32;
        if (z && (r0 = this.f15327) != null && (c2939 = (c0453 = (C0453) r0.ˌᴵ).f2475) != null) {
            C5015 c5015 = C5015.f19007;
            c0453.f2467 = c5015;
            c2939.m7034(c5015);
        }
        throw new AudioOutput$WriteException(iWrite, z);
    }

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final void m8781(int i, int i2) {
        if (Build.VERSION.SDK_INT < 29) {
            return;
        }
        this.f15324.setOffloadDelayPadding(i, i2);
    }

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final void m8782(C2136 c2136) {
        if (Build.VERSION.SDK_INT < 31) {
            return;
        }
        LogSessionId logSessionIdM5644 = c2136.m5644();
        if (logSessionIdM5644.equals(ᵷᵝˎ.ːˏᵟᵯ())) {
            return;
        }
        this.f15324.setLogSessionId(logSessionIdM5644);
    }
}
