package com.google.android.exoplayer2.video.spherical; import android.content.Context; import android.graphics.PointF; import android.graphics.SurfaceTexture; import android.hardware.Sensor; import android.hardware.SensorManager; import android.opengl.GLES20; import android.opengl.GLSurfaceView; import android.opengl.Matrix; import android.os.Handler; import android.os.Looper; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.Surface; import android.view.WindowManager; import androidx.annotation.Nullable; import com.amazonaws.handlers.HandlerChainFactory$$ExternalSyntheticThrowCCEIfNotNull0; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.video.spherical.OrientationListener; import com.google.android.exoplayer2.video.spherical.TouchTracker; import java.util.Iterator; import java.util.concurrent.CopyOnWriteArrayList; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; /* loaded from: classes2.dex */ public final class SphericalGLSurfaceView extends GLSurfaceView { public static final /* synthetic */ int $r8$clinit = 0; public boolean isOrientationListenerRegistered; public boolean isStarted; public final Handler mainHandler; public final OrientationListener orientationListener; public final Sensor orientationSensor; public final SceneRenderer scene; public final SensorManager sensorManager; public Surface surface; public SurfaceTexture surfaceTexture; public final TouchTracker touchTracker; public boolean useSensorRotation; public final CopyOnWriteArrayList videoSurfaceListeners; public SphericalGLSurfaceView(Context context) { this(context, null); } public SphericalGLSurfaceView(Context context, @Nullable AttributeSet attributeSet) { super(context, attributeSet); this.videoSurfaceListeners = new CopyOnWriteArrayList(); this.mainHandler = new Handler(Looper.getMainLooper()); SensorManager sensorManager = (SensorManager) Assertions.checkNotNull(context.getSystemService("sensor")); this.sensorManager = sensorManager; Sensor defaultSensor = Util.SDK_INT >= 18 ? sensorManager.getDefaultSensor(15) : null; this.orientationSensor = defaultSensor == null ? sensorManager.getDefaultSensor(11) : defaultSensor; SceneRenderer sceneRenderer = new SceneRenderer(); this.scene = sceneRenderer; Renderer renderer = new Renderer(sceneRenderer); TouchTracker touchTracker = new TouchTracker(context, renderer, 25.0f); this.touchTracker = touchTracker; this.orientationListener = new OrientationListener(((WindowManager) Assertions.checkNotNull((WindowManager) context.getSystemService("window"))).getDefaultDisplay(), touchTracker, renderer); this.useSensorRotation = true; setEGLContextClientVersion(2); setRenderer(renderer); setOnTouchListener(touchTracker); } public void setDefaultStereoMode(int i) { this.scene.setDefaultStereoMode(i); } public void setUseSensorRotation(boolean z) { this.useSensorRotation = z; updateOrientationListenerRegistration(); } @Override // android.opengl.GLSurfaceView public void onResume() { super.onResume(); this.isStarted = true; updateOrientationListenerRegistration(); } @Override // android.opengl.GLSurfaceView public void onPause() { this.isStarted = false; updateOrientationListenerRegistration(); super.onPause(); } @Override // android.opengl.GLSurfaceView, android.view.SurfaceView, android.view.View public void onDetachedFromWindow() { super.onDetachedFromWindow(); this.mainHandler.post(new Runnable() { // from class: com.google.android.exoplayer2.video.spherical.SphericalGLSurfaceView$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { SphericalGLSurfaceView.this.lambda$onDetachedFromWindow$0(); } }); } public final /* synthetic */ void lambda$onDetachedFromWindow$0() { Surface surface = this.surface; if (surface != null) { Iterator it = this.videoSurfaceListeners.iterator(); if (it.hasNext()) { HandlerChainFactory$$ExternalSyntheticThrowCCEIfNotNull0.m(it.next()); throw null; } } releaseSurface(this.surfaceTexture, surface); this.surfaceTexture = null; this.surface = null; } public final void updateOrientationListenerRegistration() { boolean z = this.useSensorRotation && this.isStarted; Sensor sensor = this.orientationSensor; if (sensor == null || z == this.isOrientationListenerRegistered) { return; } if (z) { this.sensorManager.registerListener(this.orientationListener, sensor, 0); } else { this.sensorManager.unregisterListener(this.orientationListener); } this.isOrientationListenerRegistered = z; } public final void onSurfaceTextureAvailable(final SurfaceTexture surfaceTexture) { this.mainHandler.post(new Runnable() { // from class: com.google.android.exoplayer2.video.spherical.SphericalGLSurfaceView$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { SphericalGLSurfaceView.this.lambda$onSurfaceTextureAvailable$1(surfaceTexture); } }); } public final /* synthetic */ void lambda$onSurfaceTextureAvailable$1(SurfaceTexture surfaceTexture) { SurfaceTexture surfaceTexture2 = this.surfaceTexture; Surface surface = this.surface; Surface surface2 = new Surface(surfaceTexture); this.surfaceTexture = surfaceTexture; this.surface = surface2; Iterator it = this.videoSurfaceListeners.iterator(); if (it.hasNext()) { HandlerChainFactory$$ExternalSyntheticThrowCCEIfNotNull0.m(it.next()); throw null; } releaseSurface(surfaceTexture2, surface); } public static void releaseSurface(SurfaceTexture surfaceTexture, Surface surface) { if (surfaceTexture != null) { surfaceTexture.release(); } if (surface != null) { surface.release(); } } public final class Renderer implements GLSurfaceView.Renderer, TouchTracker.Listener, OrientationListener.Listener { public final float[] deviceOrientationMatrix; public float deviceRoll; public final SceneRenderer scene; public float touchPitch; public final float[] touchPitchMatrix; public final float[] touchYawMatrix; public final float[] projectionMatrix = new float[16]; public final float[] viewProjectionMatrix = new float[16]; public final float[] viewMatrix = new float[16]; public final float[] tempMatrix = new float[16]; public Renderer(SceneRenderer sceneRenderer) { float[] fArr = new float[16]; this.deviceOrientationMatrix = fArr; float[] fArr2 = new float[16]; this.touchPitchMatrix = fArr2; float[] fArr3 = new float[16]; this.touchYawMatrix = fArr3; this.scene = sceneRenderer; Matrix.setIdentityM(fArr, 0); Matrix.setIdentityM(fArr2, 0); Matrix.setIdentityM(fArr3, 0); this.deviceRoll = 3.1415927f; } @Override // android.opengl.GLSurfaceView.Renderer public synchronized void onSurfaceCreated(GL10 gl10, EGLConfig eGLConfig) { SphericalGLSurfaceView.this.onSurfaceTextureAvailable(this.scene.init()); } @Override // android.opengl.GLSurfaceView.Renderer public void onSurfaceChanged(GL10 gl10, int i, int i2) { GLES20.glViewport(0, 0, i, i2); float f = i / i2; Matrix.perspectiveM(this.projectionMatrix, 0, calculateFieldOfViewInYDirection(f), f, 0.1f, 100.0f); } @Override // android.opengl.GLSurfaceView.Renderer public void onDrawFrame(GL10 gl10) { synchronized (this) { Matrix.multiplyMM(this.tempMatrix, 0, this.deviceOrientationMatrix, 0, this.touchYawMatrix, 0); Matrix.multiplyMM(this.viewMatrix, 0, this.touchPitchMatrix, 0, this.tempMatrix, 0); } Matrix.multiplyMM(this.viewProjectionMatrix, 0, this.projectionMatrix, 0, this.viewMatrix, 0); this.scene.drawFrame(this.viewProjectionMatrix, false); } @Override // com.google.android.exoplayer2.video.spherical.OrientationListener.Listener public synchronized void onOrientationChange(float[] fArr, float f) { float[] fArr2 = this.deviceOrientationMatrix; System.arraycopy(fArr, 0, fArr2, 0, fArr2.length); this.deviceRoll = -f; updatePitchMatrix(); } public final void updatePitchMatrix() { Matrix.setRotateM(this.touchPitchMatrix, 0, -this.touchPitch, (float) Math.cos(this.deviceRoll), (float) Math.sin(this.deviceRoll), 0.0f); } @Override // com.google.android.exoplayer2.video.spherical.TouchTracker.Listener public synchronized void onScrollChange(PointF pointF) { this.touchPitch = pointF.y; updatePitchMatrix(); Matrix.setRotateM(this.touchYawMatrix, 0, -pointF.x, 0.0f, 1.0f, 0.0f); } @Override // com.google.android.exoplayer2.video.spherical.TouchTracker.Listener public boolean onSingleTapUp(MotionEvent motionEvent) { return SphericalGLSurfaceView.this.performClick(); } public final float calculateFieldOfViewInYDirection(float f) { if (f > 1.0f) { return (float) (Math.toDegrees(Math.atan(Math.tan(Math.toRadians(45.0d)) / f)) * 2.0d); } return 90.0f; } } }