package a.c.d.e;

import a.c.d.i.f;
import a.c.d.i.g;
import android.text.TextUtils;
import android.view.Surface;
import com.ucar.vehiclesdk.MDevice;
import com.ucar.vehiclesdk.UCarCommon;
import com.ucarsink.sink.natives.SinkNative;
import java.nio.ByteBuffer;
import java.util.HashMap;

public class b {
    private static final String m = "CastManager";
    private static final String n = "kWhatAudioLatency";

    /* renamed from: a  reason: collision with root package name */
    private MDevice f111a;
    private int b;
    public a c;
    public int d;
    public int e;
    public String f;
    public boolean g;
    private f h;
    public a.c.d.c.a i;
    private final g j;
    private String k;
    public SinkNative.a l;

    public class a implements SinkNative.a {
        public a() {
        }

        @Override // com.ucarsink.sink.natives.SinkNative.a
        public void a(String str) {
            a aVar = b.this.c;
            if (aVar != null) {
                aVar.a(str);
            }
        }

        @Override // com.ucarsink.sink.natives.SinkNative.a
        public void a(String str, UCarCommon.AudioType audioType, int i, byte[] bArr) {
            a.c.d.c.a aVar = b.this.i;
            if (aVar != null) {
                aVar.a(audioType, i, bArr);
            }
        }

        @Override // com.ucarsink.sink.natives.SinkNative.a
        public void a(String str, UCarCommon.VideoType videoType, int i, byte[] bArr, long j) {
            b.this.j.a(ByteBuffer.wrap(bArr, 0, i), j);
        }

        @Override // com.ucarsink.sink.natives.SinkNative.a
        public void a(String str, String str2, int i, int i2) {
            UCarCommon.AudioFormat audioFormat = new UCarCommon.AudioFormat(str2, 2, i, i2 == 1 ? 16 : 12);
            a aVar = b.this.c;
            if (aVar != null) {
                aVar.a(audioFormat);
            }
        }

        @Override // com.ucarsink.sink.natives.SinkNative.a
        public void a(String str, boolean z) {
            b bVar = b.this;
            bVar.f = str;
            bVar.g = z;
        }

        @Override // com.ucarsink.sink.natives.SinkNative.a
        public void b(String str) {
            b.this.f = str;
        }

        @Override // com.ucarsink.sink.natives.SinkNative.a
        public void b(String str, String str2, int i, int i2) {
            b bVar = b.this;
            bVar.d = i;
            bVar.e = i2;
            bVar.k = str2;
            b.this.j.a(str2);
            b bVar2 = b.this;
            a aVar = bVar2.c;
            if (aVar != null) {
                aVar.a(bVar2.f, i, i2, bVar2.g);
            }
        }
    }

    public b(a.c.d.c.a aVar, a aVar2) {
        this(aVar, null, aVar2);
    }

    public b(a.c.d.c.a aVar, MDevice mDevice, a aVar2) {
        this.j = new g();
        this.l = new a();
        this.f111a = mDevice;
        this.i = aVar;
        this.c = aVar2;
    }

    private synchronized void d() {
        f fVar = this.h;
        if (fVar != null) {
            fVar.g();
            this.h = null;
        }
    }

    public void a() {
        this.j.a();
    }

    public void a(int i2) {
        HashMap hashMap = new HashMap();
        hashMap.put(n, String.valueOf(i2));
        SinkNative.setParameter(this.f, hashMap);
    }

    public void a(int i2, int i3, int i4, int i5, boolean z, boolean z2) {
        MDevice mDevice = this.f111a;
        if (mDevice == null || TextUtils.isEmpty(mDevice.getAddress())) {
            a.a.a.b.b(m, "MDevice is null, can not cast");
            return;
        }
        this.b = i5;
        SinkNative.a(this.l);
        SinkNative.start(this.f111a.getAddress(), this.f111a.getPort(), i2, i3, i4, i5, z, z2);
    }

    public void a(MDevice mDevice) {
        this.f111a = mDevice;
    }

    public void a(String str) {
        SinkNative.pause(str);
    }

    /* JADX WARNING: Removed duplicated region for block: B:11:0x003c A[Catch:{ Exception -> 0x004b }] */
    public synchronized boolean a(Surface surface, int i2, int i3, boolean z, long j2, a.c.d.a aVar) {
        f fVar;
        try {
            f fVar2 = this.h;
            if (fVar2 == null) {
                fVar = new f(this.j, this.b, this.k);
            } else {
                if (fVar2.c()) {
                    this.h.g();
                    fVar = new f(this.j, this.b, this.k);
                }
                this.h.a(surface, i2, i3, z, j2, aVar);
                if (!this.h.e()) {
                    this.h.h();
                }
                this.h.a(true);
            }
            this.h = fVar;
            this.h.a(surface, i2, i3, z, j2, aVar);
            if (!this.h.e()) {
            }
            this.h.a(true);
        } catch (Exception e2) {
            a.a.a.b.b(m, "start cast exception", e2);
            return false;
        }
        return true;
    }

    public synchronized void b() {
        f fVar = this.h;
        if (fVar != null && fVar.d()) {
            this.h.f();
            this.h.a(false);
        }
    }

    public void b(String str) {
        SinkNative.resume(str);
    }

    public void c() {
        SinkNative.b();
    }

    public void e() {
        a.a.a.b.c(m, "stop Cast");
        b();
        d();
        SinkNative.stop(this.f);
        SinkNative.b();
        this.f = null;
    }
}
