package com.unity3d.player;

import android.hardware.Camera;
import android.view.SurfaceHolder;
import java.util.List;

/* loaded from: /storage/emulated/0/Documents/jadec/sources/com.UBI.A90.WW/dex-files/2.dex */
final class a {
    Camera a;
    private final Object[] b = new Object[0];
    private final int c;
    private final int d;
    private final int e;
    private final int f;
    private Camera.Parameters g;
    private Camera.Size h;
    private int i;
    private int[] j;
    private c k;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.unity3d.player.a$a  reason: collision with other inner class name */
    /* loaded from: /storage/emulated/0/Documents/jadec/sources/com.UBI.A90.WW/dex-files/2.dex */
    public interface InterfaceC0006a {
        void onCameraFrame(a aVar, byte[] bArr);
    }

    public a(int i, int i2, int i3, int i4) {
        this.c = i;
        this.d = a(i2, 640);
        this.e = a(i3, 480);
        this.f = a(i4, 24);
    }

    private static final int a(int i, int i2) {
        return i != 0 ? i : i2;
    }

    private void b(final InterfaceC0006a interfaceC0006a) {
        Camera open;
        synchronized (this.b) {
            if (p.b) {
                h hVar = p.h;
                open = h.c(this.c);
            } else {
                open = Camera.open();
            }
            this.a = open;
            this.g = this.a.getParameters();
            this.h = f();
            this.j = e();
            this.i = d();
            f fVar = p.f;
            f.b(this.g);
            if (p.b) {
                h hVar2 = p.h;
                h.b(this.g);
            }
            this.g.setPreviewSize(this.h.width, this.h.height);
            if (p.b) {
                h hVar3 = p.h;
                h.a(this.g, this.j);
            } else {
                f fVar2 = p.f;
                f.a(this.g, this.j);
            }
            this.a.setParameters(this.g);
            Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() { // from class: com.unity3d.player.a.1
                @Override // android.hardware.Camera.PreviewCallback
                public final void onPreviewFrame(byte[] bArr, Camera camera) {
                    if (a.this.a != camera) {
                        return;
                    }
                    interfaceC0006a.onCameraFrame(a.this, bArr);
                }
            };
            if (p.a) {
                g gVar = p.g;
                g.a(this.a, this.h, this.i, previewCallback);
            } else {
                f fVar3 = p.f;
                f.a(this.a, previewCallback);
            }
        }
    }

    private final int d() {
        this.g.setPreviewFormat(17);
        if (p.a) {
            g gVar = p.g;
            return g.a(17);
        }
        f fVar = p.f;
        return f.a(17);
    }

    private final int[] e() {
        List<int[]> a;
        double d;
        int[] iArr;
        double d2 = this.f * 1000;
        if (p.b) {
            h hVar = p.h;
            a = h.a(this.g);
        } else {
            f fVar = p.f;
            a = f.a(this.g);
        }
        int[] iArr2 = {this.f * 1000, this.f * 1000};
        double d3 = Double.MAX_VALUE;
        for (int[] iArr3 : a) {
            double abs = Math.abs(Math.log(d2 / iArr3[0])) + Math.abs(Math.log(d2 / iArr3[1]));
            if (abs < d3) {
                iArr = iArr3;
                d = abs;
            } else {
                d = d3;
                iArr = iArr2;
            }
            iArr2 = iArr;
            d3 = d;
        }
        return iArr2;
    }

    private final Camera.Size f() {
        double d;
        Camera.Size size;
        double d2 = this.d;
        double d3 = this.e;
        Camera.Size size2 = null;
        double d4 = Double.MAX_VALUE;
        for (Camera.Size size3 : this.g.getSupportedPreviewSizes()) {
            double abs = Math.abs(Math.log(d2 / size3.width)) + Math.abs(Math.log(d3 / size3.height));
            if (abs < d4) {
                size = size3;
                d = abs;
            } else {
                d = d4;
                size = size2;
            }
            d4 = d;
            size2 = size;
        }
        return size2;
    }

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

    public final void a(InterfaceC0006a interfaceC0006a) {
        synchronized (this.b) {
            if (this.a == null) {
                b(interfaceC0006a);
            }
            if (this.k == null) {
                this.k = new c() { // from class: com.unity3d.player.a.2
                    private Camera a;

                    {
                        this.a = a.this.a;
                    }

                    @Override // android.view.SurfaceHolder.Callback
                    public final void surfaceCreated(SurfaceHolder surfaceHolder) {
                        synchronized (a.this.b) {
                            if (a.this.a != this.a) {
                                return;
                            }
                            try {
                                a.this.a.setPreviewDisplay(surfaceHolder);
                                a.this.a.startPreview();
                            } catch (Exception e) {
                                m.Log(6, "Unable to initialize webcam data stream: " + e.getMessage());
                            }
                        }
                    }

                    @Override // com.unity3d.player.c, android.view.SurfaceHolder.Callback
                    public final void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                        synchronized (a.this.b) {
                            if (a.this.a != this.a) {
                                return;
                            }
                            a.this.a.stopPreview();
                        }
                    }
                };
                this.k.a();
            }
        }
    }

    public final void a(byte[] bArr) {
        synchronized (this.b) {
            if (this.a != null && p.a) {
                g gVar = p.g;
                g.a(this.a, bArr);
            }
        }
    }

    public final Camera.Size b() {
        return this.h;
    }

    public final void c() {
        synchronized (this.b) {
            if (this.a != null) {
                if (p.a) {
                    g gVar = p.g;
                    g.a(this.a);
                } else {
                    f fVar = p.f;
                    f.a(this.a);
                }
                this.a.stopPreview();
                this.a.release();
                this.a = null;
            }
            if (this.k != null) {
                this.k.b();
                this.k = null;
            }
        }
    }
}
