package a.c.d.c;

import a.c.b.g;
import a.c.b.s;
import a.c.b.w;
import a.c.b.z.e;
import android.content.Context;
import com.ucar.databus.proto.UCarProto;
import com.ucar.vehiclesdk.UCarAdapter;
import com.ucar.vehiclesdk.UCarCommon;
import com.ucar.vehiclesdk.recorder.AudioConfig;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.GregorianCalendar;
import java.util.concurrent.Future;

public class a {
    private static final String g = "UCarAudioManager";

    /* renamed from: a  reason: collision with root package name */
    private a.b.a.g.a f96a;
    private boolean b = false;
    private Future<Boolean> c;
    private a.c.d.i.a d;
    private a.c.d.j.a e = null;
    private boolean f;

    /* renamed from: a.c.d.c.a$a  reason: collision with other inner class name */
    public class C0009a extends a.b.a.g.a {
        public C0009a(a.c.b.z.b bVar, boolean z, boolean z2) {
            super(bVar, z, z2);
        }

        @Override // a.c.b.z.g.e, a.c.b.z.f
        public void a(s sVar) {
            super.a(sVar);
            if (sVar != null && w.b(sVar)) {
                UCarCommon.AudioType fromInt = UCarCommon.AudioType.fromInt(w.e(sVar).getNumber());
                byte[] d = w.d(sVar);
                a.this.a(fromInt, d.length, d);
            }
        }

        @Override // a.c.b.z.g.e, a.c.b.z.f
        public void a(boolean z) {
            a.a.a.b.a(a.g, "audio channel closed.");
            a.this.b = false;
        }

        @Override // a.c.b.z.g.e, a.c.b.z.f
        public void h() {
            a.a.a.b.a(a.g, "audio channel ready.");
            a.this.b = true;
        }
    }

    public static class b {
        public static final int d = 600;
        public static final int e = 300;
        public static final int f = 100;
        public static final int g = 200;
        public static final int h = 100;
        public static final int i = 10;

        /* renamed from: a  reason: collision with root package name */
        private UCarCommon.AudioType f97a = UCarCommon.AudioType.STREAM_UNDEFINED;
        private int b = -1;
        private int c = -1;

        public UCarCommon.AudioType a() {
            return this.f97a;
        }

        public void a(int i2) {
            this.b = i2;
        }

        public void a(UCarCommon.AudioType audioType) {
            this.f97a = audioType;
        }

        public int b() {
            return this.b;
        }

        public void b(int i2) {
            this.c = i2;
        }

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

        public String toString() {
            return "AudioPlayerControl{mAudioType=" + this.f97a + ", mBufferingCount=" + this.b + ", mSpeedAdjustStep=" + this.c + '}';
        }
    }

    public interface c {
        void onAudioFocusChanged(UCarCommon.AudioType audioType, int i);
    }

    public enum d {
        START_PLAYER(0),
        STOP_PLAYER(1),
        PAUSE_PLAYER(2),
        RESUME_PLAYER(3);
        
        private static d[] f = null;

        /* renamed from: a  reason: collision with root package name */
        private final int f98a;

        private d(int i) {
            this.f98a = i;
        }

        public static d a(int i) {
            if (f == null) {
                f = values();
            }
            int i2 = 0;
            while (true) {
                d[] dVarArr = f;
                if (i2 >= dVarArr.length) {
                    return STOP_PLAYER;
                }
                if (dVarArr[i2].f98a == i) {
                    return dVarArr[i2];
                }
                i2++;
            }
        }

        public int a() {
            return this.f98a;
        }
    }

    public a(Context context, boolean z, c cVar) {
        this.f = z;
        C0009a aVar = new C0009a(a.c.b.z.b.MEDIA, false, true);
        this.f96a = aVar;
        aVar.c(true);
        this.d = new a.c.d.i.a(context, cVar);
    }

    /* access modifiers changed from: private */
    public static /* synthetic */ void a(short[] sArr, int i) {
        a.a.a.b.a(g, "onCarAudioRtpDataCallback, bodyLen: " + i);
        UCarAdapter.getInstance().sendMicRecordData(i, sArr, (long) ((int) (new GregorianCalendar().getTimeInMillis() / 1000)));
    }

    private void c() {
        a.a.a.b.a(g, "clearAudioResources");
        g();
        f();
    }

    private boolean d() {
        Future<Boolean> future;
        if (!this.b && (future = this.c) != null) {
            try {
                future.get();
            } catch (Exception e2) {
                a.a.a.b.b(g, "wait start audio channel error.", e2);
            }
        }
        return this.f96a.b();
    }

    private void f() {
        a.a.a.b.a(g, "stopAndClearAudioPlayers");
        this.d.f();
    }

    public void a(b bVar) {
        if (bVar != null) {
            this.d.b(bVar);
        }
    }

    public void a(UCarCommon.AudioFormat audioFormat, boolean z) {
        a.a.a.b.a(g, "startAudioRecord");
        a.c.d.j.a aVar = this.e;
        if (aVar != null) {
            aVar.f();
            this.e = null;
        }
        AudioConfig carConfig = AudioConfig.getCarConfig(audioFormat);
        if (z) {
            carConfig.setSource(7);
        }
        a.c.d.j.a aVar2 = new a.c.d.j.a(carConfig, this.f, $$Lambda$a$Z3lri1gbOfnSTXrJLIH7gPff5GE.INSTANCE);
        this.e = aVar2;
        aVar2.e();
    }

    public void a(UCarCommon.AudioFormat audioFormat, boolean z, boolean z2) {
        a.a.a.b.c(g, "onMicRecordRequest, isTurnOn: " + z);
        if (z) {
            a(audioFormat, z2);
        } else {
            g();
        }
    }

    public void a(UCarCommon.AudioType audioType, int i, byte[] bArr) {
        this.d.b(audioType, ByteBuffer.wrap(bArr, 0, i));
    }

    public void a(UCarCommon.AudioType audioType, UCarCommon.AudioFormat audioFormat, d dVar, UCarCommon.AudioAttributes audioAttributes) {
        a.a.a.b.a(g, "handleAudioStateChanged, type: " + audioType + ", format: " + audioFormat + ", state: " + dVar + ", attributes: " + audioAttributes);
        this.d.a(audioType, audioFormat, dVar, audioAttributes);
    }

    public void a(String str) {
        a.a.a.b.a(g, "start, address: " + str);
        try {
            this.c = this.f96a.a(0, str);
        } catch (IOException e2) {
            a.a.a.b.b(g, "start audio channel error.", e2);
        }
    }

    public void a(boolean z) {
        this.d.a(z);
    }

    public boolean a() {
        return this.d.a();
    }

    public boolean a(int i, short[] sArr, long j) {
        if (!d()) {
            a.a.a.b.b(g, "sendMicRecordData, Channel not ready");
            return false;
        }
        int i2 = i * 2;
        byte[] a2 = g.a(i2);
        ByteBuffer.wrap(a2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(sArr);
        this.f96a.a(w.a(ByteBuffer.wrap(a2, 0, i2), UCarProto.AudioType.STREAM_MICROPHONE), (e) $$Lambda$a$AD9JYKe71TdzoEEbjxKrFzzfLwI.INSTANCE);
        return true;
    }

    public void b() {
        this.d.b();
    }

    public void e() {
        a.a.a.b.a(g, "stop");
        try {
            c();
            this.f96a.i();
        } catch (Exception e2) {
            a.a.a.b.b(g, "stop audio channel error: " + e2);
        }
    }

    public void g() {
        a.a.a.b.a(g, "stopAudioRecord");
        a.c.d.j.a aVar = this.e;
        if (aVar != null) {
            aVar.f();
            this.e = null;
        }
    }
}
