package org.webrtc; import android.graphics.SurfaceTexture; import android.opengl.GLES20; import android.os.Handler; import android.os.HandlerThread; import java.util.concurrent.Callable; import org.webrtc.EglBase; import org.webrtc.TextureBufferImpl; import org.webrtc.VideoFrame; /* loaded from: classes3.dex */ public class SurfaceTextureHelper { private static final String TAG = "SurfaceTextureHelper"; private final EglBase eglBase; private final FrameRefMonitor frameRefMonitor; private int frameRotation; private final Handler handler; private boolean hasPendingTexture; private boolean isQuitting; private volatile boolean isTextureInUse; private VideoSink listener; private final int oesTextureId; private VideoSink pendingListener; final Runnable setListenerRunnable; private final SurfaceTexture surfaceTexture; private int textureHeight; private final TextureBufferImpl.RefCountMonitor textureRefCountMonitor; private int textureWidth; private final TimestampAligner timestampAligner; private final YuvConverter yuvConverter; /* loaded from: classes3.dex */ public interface FrameRefMonitor { void onDestroyBuffer(VideoFrame.TextureBuffer textureBuffer); void onNewBuffer(VideoFrame.TextureBuffer textureBuffer); void onReleaseBuffer(VideoFrame.TextureBuffer textureBuffer); void onRetainBuffer(VideoFrame.TextureBuffer textureBuffer); } public Handler getHandler() { return this.handler; } public SurfaceTexture getSurfaceTexture() { return this.surfaceTexture; } public boolean isTextureInUse() { return this.isTextureInUse; } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$setFrameRotation$4$org-webrtc-SurfaceTextureHelper, reason: not valid java name */ public /* synthetic */ void m7444lambda$setFrameRotation$4$orgwebrtcSurfaceTextureHelper(int i) { this.frameRotation = i; } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$stopListening$1$org-webrtc-SurfaceTextureHelper, reason: not valid java name */ public /* synthetic */ void m7446lambda$stopListening$1$orgwebrtcSurfaceTextureHelper() { this.listener = null; this.pendingListener = null; } public static SurfaceTextureHelper create(final String str, final EglBase.Context context, final boolean z, final YuvConverter yuvConverter, final FrameRefMonitor frameRefMonitor) { HandlerThread handlerThread = new HandlerThread(str); handlerThread.start(); final Handler handler = new Handler(handlerThread.getLooper()); return (SurfaceTextureHelper) ThreadUtils.invokeAtFrontUninterruptibly(handler, new Callable() { // from class: org.webrtc.SurfaceTextureHelper.1 @Override // java.util.concurrent.Callable public SurfaceTextureHelper call() { try { return new SurfaceTextureHelper(EglBase.Context.this, handler, z, yuvConverter, frameRefMonitor); } catch (RuntimeException e) { Logging.e(SurfaceTextureHelper.TAG, str + " create failure", e); return null; } } }); } public static SurfaceTextureHelper create(String str, EglBase.Context context) { return create(str, context, false, new YuvConverter(), null); } public static SurfaceTextureHelper create(String str, EglBase.Context context, boolean z) { return create(str, context, z, new YuvConverter(), null); } public static SurfaceTextureHelper create(String str, EglBase.Context context, boolean z, YuvConverter yuvConverter) { return create(str, context, z, yuvConverter, null); } private SurfaceTextureHelper(EglBase.Context context, Handler handler, boolean z, YuvConverter yuvConverter, FrameRefMonitor frameRefMonitor) { this.textureRefCountMonitor = new TextureBufferImpl.RefCountMonitor() { // from class: org.webrtc.SurfaceTextureHelper.2 @Override // org.webrtc.TextureBufferImpl.RefCountMonitor public void onRetain(TextureBufferImpl textureBufferImpl) { if (SurfaceTextureHelper.this.frameRefMonitor != null) { SurfaceTextureHelper.this.frameRefMonitor.onRetainBuffer(textureBufferImpl); } } @Override // org.webrtc.TextureBufferImpl.RefCountMonitor public void onRelease(TextureBufferImpl textureBufferImpl) { if (SurfaceTextureHelper.this.frameRefMonitor != null) { SurfaceTextureHelper.this.frameRefMonitor.onReleaseBuffer(textureBufferImpl); } } @Override // org.webrtc.TextureBufferImpl.RefCountMonitor public void onDestroy(TextureBufferImpl textureBufferImpl) { SurfaceTextureHelper.this.returnTextureFrame(); if (SurfaceTextureHelper.this.frameRefMonitor != null) { SurfaceTextureHelper.this.frameRefMonitor.onDestroyBuffer(textureBufferImpl); } } }; this.setListenerRunnable = new Runnable() { // from class: org.webrtc.SurfaceTextureHelper.3 @Override // java.lang.Runnable public void run() { Logging.d(SurfaceTextureHelper.TAG, "Setting listener to " + SurfaceTextureHelper.this.pendingListener); SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.this; surfaceTextureHelper.listener = surfaceTextureHelper.pendingListener; SurfaceTextureHelper.this.pendingListener = null; if (SurfaceTextureHelper.this.hasPendingTexture) { SurfaceTextureHelper.this.updateTexImage(); SurfaceTextureHelper.this.hasPendingTexture = false; } } }; if (handler.getLooper().getThread() != Thread.currentThread()) { throw new IllegalStateException("SurfaceTextureHelper must be created on the handler thread"); } this.handler = handler; this.timestampAligner = z ? new TimestampAligner() : null; this.yuvConverter = yuvConverter; this.frameRefMonitor = frameRefMonitor; EglBase create = EglBase.create(context, EglBase.CONFIG_PIXEL_BUFFER); this.eglBase = create; try { create.createDummyPbufferSurface(); create.makeCurrent(); int generateTexture = GlUtil.generateTexture(36197); this.oesTextureId = generateTexture; SurfaceTexture surfaceTexture = new SurfaceTexture(generateTexture); this.surfaceTexture = surfaceTexture; setOnFrameAvailableListener(surfaceTexture, new SurfaceTexture.OnFrameAvailableListener() { // from class: org.webrtc.SurfaceTextureHelper$$ExternalSyntheticLambda5 @Override // android.graphics.SurfaceTexture.OnFrameAvailableListener public final void onFrameAvailable(SurfaceTexture surfaceTexture2) { SurfaceTextureHelper.this.m7442lambda$new$0$orgwebrtcSurfaceTextureHelper(surfaceTexture2); } }, handler); } catch (RuntimeException e) { this.eglBase.release(); handler.getLooper().quit(); throw e; } } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$new$0$org-webrtc-SurfaceTextureHelper, reason: not valid java name */ public /* synthetic */ void m7442lambda$new$0$orgwebrtcSurfaceTextureHelper(SurfaceTexture surfaceTexture) { if (this.hasPendingTexture) { Logging.d(TAG, "A frame is already pending, dropping frame."); } this.hasPendingTexture = true; tryDeliverTextureFrame(); } private static void setOnFrameAvailableListener(SurfaceTexture surfaceTexture, SurfaceTexture.OnFrameAvailableListener onFrameAvailableListener, Handler handler) { surfaceTexture.setOnFrameAvailableListener(onFrameAvailableListener, handler); } public void startListening(VideoSink videoSink) { if (this.listener != null || this.pendingListener != null) { throw new IllegalStateException("SurfaceTextureHelper listener has already been set."); } this.pendingListener = videoSink; this.handler.post(this.setListenerRunnable); } public void stopListening() { Logging.d(TAG, "stopListening()"); this.handler.removeCallbacks(this.setListenerRunnable); ThreadUtils.invokeAtFrontUninterruptibly(this.handler, new Runnable() { // from class: org.webrtc.SurfaceTextureHelper$$ExternalSyntheticLambda3 @Override // java.lang.Runnable public final void run() { SurfaceTextureHelper.this.m7446lambda$stopListening$1$orgwebrtcSurfaceTextureHelper(); } }); } public void setTextureSize(final int i, final int i2) { if (i <= 0) { throw new IllegalArgumentException("Texture width must be positive, but was " + i); } if (i2 <= 0) { throw new IllegalArgumentException("Texture height must be positive, but was " + i2); } this.surfaceTexture.setDefaultBufferSize(i, i2); this.handler.post(new Runnable() { // from class: org.webrtc.SurfaceTextureHelper$$ExternalSyntheticLambda2 @Override // java.lang.Runnable public final void run() { SurfaceTextureHelper.this.m7445lambda$setTextureSize$2$orgwebrtcSurfaceTextureHelper(i, i2); } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$setTextureSize$2$org-webrtc-SurfaceTextureHelper, reason: not valid java name */ public /* synthetic */ void m7445lambda$setTextureSize$2$orgwebrtcSurfaceTextureHelper(int i, int i2) { this.textureWidth = i; this.textureHeight = i2; tryDeliverTextureFrame(); } public void forceFrame() { this.handler.post(new Runnable() { // from class: org.webrtc.SurfaceTextureHelper$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { SurfaceTextureHelper.this.m7441lambda$forceFrame$3$orgwebrtcSurfaceTextureHelper(); } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$forceFrame$3$org-webrtc-SurfaceTextureHelper, reason: not valid java name */ public /* synthetic */ void m7441lambda$forceFrame$3$orgwebrtcSurfaceTextureHelper() { this.hasPendingTexture = true; tryDeliverTextureFrame(); } public void setFrameRotation(final int i) { this.handler.post(new Runnable() { // from class: org.webrtc.SurfaceTextureHelper$$ExternalSyntheticLambda4 @Override // java.lang.Runnable public final void run() { SurfaceTextureHelper.this.m7444lambda$setFrameRotation$4$orgwebrtcSurfaceTextureHelper(i); } }); } /* JADX INFO: Access modifiers changed from: private */ public void returnTextureFrame() { this.handler.post(new Runnable() { // from class: org.webrtc.SurfaceTextureHelper$$ExternalSyntheticLambda6 @Override // java.lang.Runnable public final void run() { SurfaceTextureHelper.this.m7443lambda$returnTextureFrame$5$orgwebrtcSurfaceTextureHelper(); } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$returnTextureFrame$5$org-webrtc-SurfaceTextureHelper, reason: not valid java name */ public /* synthetic */ void m7443lambda$returnTextureFrame$5$orgwebrtcSurfaceTextureHelper() { this.isTextureInUse = false; if (this.isQuitting) { release(); } else { tryDeliverTextureFrame(); } } public void dispose() { Logging.d(TAG, "dispose()"); ThreadUtils.invokeAtFrontUninterruptibly(this.handler, new Runnable() { // from class: org.webrtc.SurfaceTextureHelper$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { SurfaceTextureHelper.this.m7440lambda$dispose$6$orgwebrtcSurfaceTextureHelper(); } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$dispose$6$org-webrtc-SurfaceTextureHelper, reason: not valid java name */ public /* synthetic */ void m7440lambda$dispose$6$orgwebrtcSurfaceTextureHelper() { this.isQuitting = true; if (this.isTextureInUse) { return; } release(); } @Deprecated public VideoFrame.I420Buffer textureToYuv(VideoFrame.TextureBuffer textureBuffer) { return textureBuffer.toI420(); } /* JADX INFO: Access modifiers changed from: private */ public void updateTexImage() { synchronized (EglBase.lock) { this.surfaceTexture.updateTexImage(); } } private void tryDeliverTextureFrame() { if (this.handler.getLooper().getThread() != Thread.currentThread()) { throw new IllegalStateException("Wrong thread."); } if (this.isQuitting || !this.hasPendingTexture || this.isTextureInUse || this.listener == null) { return; } if (this.textureWidth == 0 || this.textureHeight == 0) { Logging.w(TAG, "Texture size has not been set."); return; } this.isTextureInUse = true; this.hasPendingTexture = false; updateTexImage(); float[] fArr = new float[16]; this.surfaceTexture.getTransformMatrix(fArr); long timestamp = this.surfaceTexture.getTimestamp(); TimestampAligner timestampAligner = this.timestampAligner; if (timestampAligner != null) { timestamp = timestampAligner.translateTimestamp(timestamp); } TextureBufferImpl textureBufferImpl = new TextureBufferImpl(this.textureWidth, this.textureHeight, VideoFrame.TextureBuffer.Type.OES, this.oesTextureId, RendererCommon.convertMatrixToAndroidGraphicsMatrix(fArr), this.handler, this.yuvConverter, this.textureRefCountMonitor); FrameRefMonitor frameRefMonitor = this.frameRefMonitor; if (frameRefMonitor != null) { frameRefMonitor.onNewBuffer(textureBufferImpl); } VideoFrame videoFrame = new VideoFrame(textureBufferImpl, this.frameRotation, timestamp); this.listener.onFrame(videoFrame); videoFrame.release(); } private void release() { if (this.handler.getLooper().getThread() != Thread.currentThread()) { throw new IllegalStateException("Wrong thread."); } if (this.isTextureInUse || !this.isQuitting) { throw new IllegalStateException("Unexpected release."); } this.yuvConverter.release(); GLES20.glDeleteTextures(1, new int[]{this.oesTextureId}, 0); this.surfaceTexture.release(); this.eglBase.release(); this.handler.getLooper().quit(); TimestampAligner timestampAligner = this.timestampAligner; if (timestampAligner != null) { timestampAligner.dispose(); } } }