package a.c.d.i;

import a.c.d.c.a;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.media.PlaybackParams;
import android.view.Surface;
import com.ucar.vehiclesdk.UCarCommon;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class b {
    private static final String f;
    private static final int g;
    private static final int h;
    private static final ThreadLocal<byte[]> i = ThreadLocal.withInitial($$Lambda$b$Bn_A_D9WUG8mzYGX5R_hx1zeszY.INSTANCE);

    /* renamed from: a */
    private final c f148a;
    private c b;
    private a c = null;
    private final int d;
    private boolean e = false;

    public static class a {
        private static final String g;
        private static final String h;
        private static final String i;
        private static final long j;
        private static final long k;

        /* renamed from: a */
        private final AtomicInteger f149a = new AtomicInteger(0);
        private final AtomicBoolean b = new AtomicBoolean(false);
        private final c c = new c("aac");
        private final c d;
        private Thread e;
        private MediaCodec f;

        /* renamed from: a.c.d.i.b$a$a */
        public class C0017a extends Thread {
            /* JADX INFO: super call moved to the top of the method (can break code semantics) */
            public C0017a(String str) {
                super(str);
                a.this = r1;
            }

            public void run() {
                a.this.f149a.incrementAndGet();
                while (a.this.b.get()) {
                    a.this.b();
                }
                if (a.this.f149a.decrementAndGet() <= 0) {
                    a.this.d();
                }
            }
        }

        /* renamed from: a.c.d.i.b$a$b */
        public class C0018b extends Thread {
            /* JADX INFO: super call moved to the top of the method (can break code semantics) */
            public C0018b(String str) {
                super(str);
                a.this = r1;
            }

            public void run() {
                a.this.f149a.incrementAndGet();
                while (a.this.b.get()) {
                    a.this.c();
                }
                if (a.this.f149a.decrementAndGet() <= 0) {
                    a.this.d();
                }
            }
        }

        public a(c cVar, String str, int i2, int i3) {
            this.d = cVar;
            int i4 = i2 == 4 ? 1 : 2;
            MediaFormat createAudioFormat = MediaFormat.createAudioFormat(str, i3, i4);
            if (str.equals("audio/mp4a-latm")) {
                a.a.a.b.a(g, "init Sync Codec, mimeType is AAC channels = " + i4);
                createAudioFormat.setInteger("aac-profile", 2);
                createAudioFormat.setInteger("is-adts", 1);
                createAudioFormat.setByteBuffer("csd-0", ByteBuffer.wrap(new byte[]{17, -112}));
                try {
                    MediaCodec createDecoderByType = MediaCodec.createDecoderByType(str);
                    this.f = createDecoderByType;
                    createDecoderByType.configure(createAudioFormat, (Surface) null, (MediaCrypto) null, 0);
                    this.f.start();
                } catch (IOException unused) {
                    throw new RuntimeException("failed to create decoder");
                }
            } else {
                throw new RuntimeException("not support " + str);
            }
        }

        private void b() {
            ByteBuffer b2 = this.c.b();
            if (b2 != null && b2.remaining() != 0) {
                int dequeueInputBuffer = this.f.dequeueInputBuffer(j);
                if (dequeueInputBuffer >= 0) {
                    long currentTimeMillis = System.currentTimeMillis();
                    try {
                        int remaining = b2.remaining();
                        ByteBuffer inputBuffer = this.f.getInputBuffer(dequeueInputBuffer);
                        inputBuffer.clear();
                        inputBuffer.limit(remaining);
                        inputBuffer.put(b2);
                        this.f.queueInputBuffer(dequeueInputBuffer, 0, remaining, currentTimeMillis, 0);
                    } catch (Exception e2) {
                        a.a.a.b.b(g, "queueInputBuffer failed.", e2);
                    } catch (Throwable th) {
                        this.c.b(b2);
                        throw th;
                    }
                }
                this.c.b(b2);
            }
        }

        private void c() {
            MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
            int dequeueOutputBuffer = this.f.dequeueOutputBuffer(bufferInfo, j);
            if (dequeueOutputBuffer >= 0 && bufferInfo.size != 0) {
                if (System.currentTimeMillis() - bufferInfo.presentationTimeUs < k) {
                    ByteBuffer outputBuffer = this.f.getOutputBuffer(dequeueOutputBuffer);
                    this.d.a(outputBuffer);
                    outputBuffer.clear();
                }
                this.f.releaseOutputBuffer(dequeueOutputBuffer, false);
            }
        }

        private void d() {
            MediaCodec mediaCodec;
            a.a.a.b.a(g, "release");
            try {
                MediaCodec mediaCodec2 = this.f;
                if (mediaCodec2 != null) {
                    mediaCodec2.stop();
                }
                mediaCodec = this.f;
                if (mediaCodec == null) {
                    return;
                }
            } catch (Exception e2) {
                a.a.a.b.b(g, "release audio decoder exception", e2);
                mediaCodec = this.f;
                if (mediaCodec == null) {
                    return;
                }
            } catch (Throwable th) {
                MediaCodec mediaCodec3 = this.f;
                if (mediaCodec3 != null) {
                    try {
                        mediaCodec3.release();
                    } catch (Exception e3) {
                        a.a.a.b.b(g, "release audio decoder exception", e3);
                    }
                    this.f = null;
                }
                throw th;
            }
            try {
                mediaCodec.release();
            } catch (Exception e4) {
                a.a.a.b.b(g, "release audio decoder exception", e4);
            }
            this.f = null;
        }

        public c a() {
            return this.c;
        }

        public void e() {
            if (!this.b.get()) {
                this.b.set(true);
                C0017a aVar = new C0017a(h);
                this.e = aVar;
                aVar.start();
                new C0018b(i).start();
            }
        }

        public void f() {
            a.a.a.b.a(g, "stop");
            this.b.set(false);
            Thread thread = this.e;
            if (thread != null) {
                try {
                    thread.interrupt();
                    this.e.join(50);
                } catch (InterruptedException e2) {
                    a.a.a.b.b(g, "stop failed.", e2);
                }
                this.e = null;
            }
        }
    }

    /* renamed from: a.c.d.i.b$b */
    public interface AbstractC0019b {
        void a(int i);
    }

    public static class c extends Thread {
        private static final String p;
        private static final int q;
        private static final float r;
        private static final float s;
        private static final int t;
        private static final int u;
        private static final int v;
        private static final int w;
        private static final int x;

        /* renamed from: a */
        private final c f152a;
        private final int b;
        private final int c;
        private final int d;
        private int e = 100;
        private float f = r;
        private volatile boolean g = false;
        private volatile boolean h = false;
        private AudioTrack i;
        private int j;
        private int k;
        private int l = -1;
        private int m = -1;
        private int n;
        private final AbstractC0019b o;

        public c(Context context, int i2, int i3, int i4, AudioAttributes audioAttributes, int i5, AbstractC0019b bVar) {
            this.f152a = new c("pcm@" + audioAttributes.getUsage());
            this.b = a(i2) * Integer.bitCount(i4) * i3;
            this.o = bVar;
            if (audioAttributes.getUsage() == 1) {
                this.c = a.b.d;
                this.d = 200;
            } else {
                this.c = a.b.e;
                this.d = 100;
                a(false, 100, 0, 10);
            }
            AudioTrack a2 = a(i2, i3, i4, audioAttributes, i5);
            this.i = a2;
            a.c.d.l.a.a(context, a2);
        }

        private int a(int i2) {
            if (i2 == 13 || i2 == 1 || i2 == 2) {
                return 2;
            }
            if (i2 == 3) {
                return 1;
            }
            if (i2 == 4) {
                return 4;
            }
            throw new IllegalArgumentException("Bad audio format " + i2);
        }

        private int a(int i2, int i3, int i4) {
            return ((((int) Math.ceil((double) (((float) AudioTrack.getMinBufferSize(i3, i4, i2)) * s))) / 16) + 1) * 16;
        }

        private AudioTrack a(int i2, int i3, int i4, AudioAttributes audioAttributes, int i5) {
            int a2 = a(i2, i3, i4);
            AudioFormat build = new AudioFormat.Builder().setEncoding(i2).setSampleRate(i3).setChannelMask(i4).build();
            a.a.a.b.c(p, "createTrack, encoding: " + i2 + ", sampleRate: " + i3 + ", adjustStep: " + this.e + "ms, channelConfig: " + i4 + ", bufferSize: " + a2 + ", attributes: " + audioAttributes);
            return new AudioTrack.Builder().setAudioAttributes(audioAttributes).setAudioFormat(build).setTransferMode(1).setBufferSizeInBytes(a2).setSessionId(i5).build();
        }

        private void a(float f2) {
            if (f2 != this.f) {
                a.a.a.b.a(p, "try adjust playback speed from x" + this.f + " to x" + f2);
                try {
                    AudioTrack audioTrack = this.i;
                    if (audioTrack != null) {
                        PlaybackParams playbackParams = audioTrack.getPlaybackParams();
                        playbackParams.setSpeed(f2);
                        this.i.setPlaybackParams(playbackParams);
                        this.f = f2;
                    }
                } catch (IllegalArgumentException e2) {
                    a.a.a.b.e(p, "setSpeed failed.", e2);
                }
            }
        }

        public c a() {
            return this.f152a;
        }

        public AudioAttributes a(UCarCommon.AudioType audioType) {
            return new AudioAttributes.Builder().setUsage(1).setContentType(2).build();
        }

        public synchronized void a(boolean z, int i2, int i3, int i4) {
            int i5;
            int i6 = this.c;
            if (i2 <= i6) {
                if (i4 <= this.d) {
                    if (i2 >= 100) {
                        if (i4 >= 10) {
                            if (z || (i5 = this.m) == -1 || i5 == i2) {
                                this.e = i4;
                                int min = Math.min(i3 + i2, i6);
                                this.m = min;
                                int i7 = this.l;
                                if (i7 == -1 || z) {
                                    this.l = i2;
                                } else {
                                    AbstractC0019b bVar = this.o;
                                    if (bVar != null) {
                                        bVar.a(Math.max(min - i7, 0));
                                    }
                                }
                                int i8 = (this.b * this.m) / 1000;
                                this.n = i8;
                                this.j = i8 * 2;
                                this.k = i8 / 2;
                                a.a.a.b.c(p, "updateAudioPlayParams, bytesPerSecond: " + this.b + ", buffingBytes: " + this.n + ", buffingCount: " + this.m + "ms");
                            }
                        }
                    }
                }
            }
        }

        public void b() {
            this.h = true;
            this.f152a.a();
            AudioTrack audioTrack = this.i;
            if (audioTrack != null && audioTrack.getPlayState() == 3) {
                this.i.pause();
                this.i.flush();
            }
        }

        public boolean b(int i2) {
            a.a.a.b.a(p, "wait buffer to fill bytes: " + i2);
            while (this.f152a.c() < i2) {
                this.f152a.f();
                if (!this.g) {
                    return false;
                }
            }
            a.a.a.b.a(p, "buffer fill done, number: " + this.f152a.d() + ", bytes: " + this.f152a.c());
            return true;
        }

        public void c() {
            this.h = false;
            this.f152a.a();
            AudioTrack audioTrack = this.i;
            if (audioTrack == null) {
                a.a.a.b.e(p, "track is null");
            } else if (audioTrack.getPlayState() == 2) {
                this.i.play();
            }
        }

        public void d() {
            this.g = false;
            try {
                interrupt();
                join(50);
            } catch (InterruptedException e2) {
                a.a.a.b.b(p, "stopPlay failed.", e2);
            }
        }

        public void run() {
            this.g = true;
            b(this.n);
            this.i.play();
            this.i.setVolume(r);
            while (this.g && (this.f152a.c() > 0 || b(this.k))) {
                ByteBuffer b2 = this.f152a.b();
                if (b2 != null) {
                    if (this.h) {
                        this.f152a.b(b2);
                    } else {
                        if (b2.hasArray()) {
                            this.i.write(b2.array(), b2.arrayOffset(), b2.remaining(), 0);
                        } else {
                            byte[] b3 = b.b(b2.remaining());
                            b2.get(b3, 0, b3.length);
                            b2.clear();
                            this.i.write(b3, 0, b3.length, 0);
                        }
                        this.f152a.b(b2);
                    }
                }
                if (!this.g) {
                    break;
                }
                int c2 = this.f152a.c();
                if (c2 != 0) {
                    if (c2 > this.b) {
                        this.f152a.a();
                        a(r);
                        a.a.a.b.b(p, "discard call remain data: " + c2);
                    } else if (c2 > this.j) {
                        a(s);
                        a.a.a.b.b(p, "play faster when remain data: " + c2 + ", and try increase buffering");
                        int i2 = this.m;
                        int i3 = this.e;
                        a(false, i2, i3, i3);
                    } else if (c2 < this.k && this.f != r) {
                        a(r);
                    }
                }
            }
            this.i.stop();
            this.i.release();
            this.i = null;
            this.f152a.a();
        }
    }

    public b(Context context, AudioAttributes audioAttributes, UCarCommon.AudioFormat audioFormat, int i2, AbstractC0019b bVar) {
        c cVar;
        this.d = audioAttributes.getUsage();
        int encodingFormat = audioFormat.getEncodingFormat();
        int sampleRate = audioFormat.getSampleRate();
        int channelConfig = audioFormat.getChannelConfig();
        String mimeType = audioFormat.getMimeType();
        this.b = new c(context, encodingFormat, sampleRate, channelConfig, audioAttributes, i2, bVar);
        if (mimeType.equals(UCarCommon.AudioFormat.MIME_TYPE_PCM)) {
            cVar = this.b.a();
        } else {
            a aVar = new a(this.b.a(), mimeType, channelConfig, sampleRate);
            this.c = aVar;
            aVar.e();
            cVar = this.c.a();
        }
        this.f148a = cVar;
    }

    public static /* synthetic */ byte[] a() {
        return new byte[131072];
    }

    public static byte[] b(int i2) {
        if (i2 < 131072) {
            return i.get();
        }
        a.a.a.b.e(f, "getTempBytes not use thread local array, maybe MAX_TEMP_ARRAY_SIZE is too small, require:" + i2);
        return new byte[i2];
    }

    public void a(int i2, int i3, int i4) {
        if (i2 <= 0 || i4 <= 0) {
            a.a.a.b.b(f, "error count = " + i2 + ", step = " + i4);
            return;
        }
        a.a.a.b.a(f, "updateAudioPlayParams, count: " + i2 + ", speedStep: " + i4);
        c cVar = this.b;
        if (cVar != null) {
            cVar.a(true, i2, i3, i4);
        }
    }

    public void a(ByteBuffer byteBuffer) {
        if (!this.e) {
            this.f148a.a(byteBuffer);
        }
    }

    public void b() {
        a.a.a.b.a(f, "pause, usage: " + this.d);
        if (this.b != null && !this.e) {
            this.e = true;
            this.f148a.a();
            this.b.b();
        }
    }

    public void c() {
        this.b.start();
    }

    public void d() {
        a.a.a.b.a(f, "release, usage: " + this.d);
        this.c = null;
        this.b = null;
    }

    public void e() {
        a.a.a.b.a(f, "resume, usage: " + this.d);
        if (this.b != null && this.e) {
            this.e = false;
            this.f148a.a();
            this.b.c();
        }
    }

    public void f() {
        a.a.a.b.a(f, "stop, usage: " + this.d);
        a aVar = this.c;
        if (aVar != null) {
            aVar.f();
        }
        c cVar = this.b;
        if (cVar != null) {
            cVar.d();
        }
    }
}
