| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import android.annotation.TargetApi; |
| 14 | import android.graphics.SurfaceTexture; |
| 15 | import android.opengl.GLES11Ext; |
| 16 | import android.opengl.GLES20; |
| 17 | import android.os.Build; |
| 18 | import android.os.Handler; |
| 19 | import android.os.HandlerThread; |
| 20 | import androidx.annotation.Nullable; |
| 21 | import java.util.concurrent.Callable; |
| 22 | import org.webrtc.EglBase.Context; |
| 23 | import org.webrtc.TextureBufferImpl.RefCountMonitor; |
| 24 | import org.webrtc.VideoFrame.TextureBuffer; |
| 25 |
| 26 | /** |
| 27 | * Helper class for using a SurfaceTexture to create WebRTC VideoFrames. In order to create WebRTC |
| 28 | * VideoFrames, render onto the SurfaceTexture. The frames will be delivered to the listener. Only |
| 29 | * one texture frame can be in flight at once, so the frame must be released in order to receive a |
| 30 | * new frame. Call stopListening() to stop receiveing new frames. Call dispose to release all |
| 31 | * resources once the texture frame is released. |
| 32 | */ |
| 33 | public class SurfaceTextureHelper { |
| 34 | /** |
| 35 | * Interface for monitoring texture buffers created from this SurfaceTexture. Since only one |
| 36 | * texture buffer can exist at a time, this can be used to monitor for stuck frames. |
| 37 | */ |
| 38 | public interface FrameRefMonitor { |
| 39 | /** A new frame was created. New frames start with ref count of 1. */ |
| 40 | void onNewBuffer(TextureBuffer textureBuffer); |
| 41 | /** Ref count of the frame was incremented by the calling thread. */ |
| 42 | void onRetainBuffer(TextureBuffer textureBuffer); |
| 43 | /** Ref count of the frame was decremented by the calling thread. */ |
| 44 | void onReleaseBuffer(TextureBuffer textureBuffer); |
| 45 | /** Frame was destroyed (ref count reached 0). */ |
| 46 | void onDestroyBuffer(TextureBuffer textureBuffer); |
| 47 | } |
| 48 |
| 49 | private static final String TAG = "SurfaceTextureHelper"; |
| 50 | /** |
| 51 | * Construct a new SurfaceTextureHelper sharing OpenGL resources with |sharedContext|. A dedicated |
| 52 | * thread and handler is created for handling the SurfaceTexture. May return null if EGL fails to |
| 53 | * initialize a pixel buffer surface and make it current. If alignTimestamps is true, the frame |
| 54 | * timestamps will be aligned to rtc::TimeNanos(). If frame timestamps are aligned to |
| 55 | * rtc::TimeNanos() there is no need for aligning timestamps again in |
| 56 | * PeerConnectionFactory.createVideoSource(). This makes the timestamps more accurate and |
| 57 | * closer to actual creation time. |
| 58 | */ |
| 59 | public static SurfaceTextureHelper create(final String threadName, |
| 60 | final EglBase.Context sharedContext, boolean alignTimestamps, final YuvConverter yuvConverter, |
| 61 | FrameRefMonitor frameRefMonitor) { |
| 62 | final HandlerThread thread = new HandlerThread(threadName); |
| 63 | thread.start(); |
| 64 | final Handler handler = new Handler(thread.getLooper()); |
| 65 |
| 66 | // The onFrameAvailable() callback will be executed on the SurfaceTexture ctor thread. See: |
| 67 | // http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/graphics/SurfaceTexture.java#195. |
| 68 | // Therefore, in order to control the callback thread on API lvl < 21, the SurfaceTextureHelper |
| 69 | // is constructed on the |handler| thread. |
| 70 | return ThreadUtils.invokeAtFrontUninterruptibly(handler, new Callable<SurfaceTextureHelper>() { |
| 71 | @Nullable |
| 72 | @Override |
| 73 | public SurfaceTextureHelper call() { |
| 74 | try { |
| 75 | return new SurfaceTextureHelper( |
| 76 | sharedContext, handler, alignTimestamps, yuvConverter, frameRefMonitor); |
| 77 | } catch (RuntimeException e) { |
| 78 | Logging.e(TAG, threadName + " create failure", e); |
| 79 | return null; |
| 80 | } |
| 81 | } |
| 82 | }); |
| 83 | } |
| 84 |
| 85 | /** |
| 86 | * Same as above with alignTimestamps set to false and yuvConverter set to new YuvConverter. |
| 87 | * |
| 88 | * @see #create(String, EglBase.Context, boolean, YuvConverter, FrameRefMonitor) |
| 89 | */ |
| 90 | public static SurfaceTextureHelper create( |
| 91 | final String threadName, final EglBase.Context sharedContext) { |
| 92 | return create(threadName, sharedContext, /* alignTimestamps= */ false, new YuvConverter(), |
| 93 | /*frameRefMonitor=*/null); |
| 94 | } |
| 95 |
| 96 | /** |
| 97 | * Same as above with yuvConverter set to new YuvConverter. |
| 98 | * |
| 99 | * @see #create(String, EglBase.Context, boolean, YuvConverter, FrameRefMonitor) |
| 100 | */ |
| 101 | public static SurfaceTextureHelper create( |
| 102 | final String threadName, final EglBase.Context sharedContext, boolean alignTimestamps) { |
| 103 | return create( |
| 104 | threadName, sharedContext, alignTimestamps, new YuvConverter(), /*frameRefMonitor=*/null); |
| 105 | } |
| 106 |
| 107 | /** |
| 108 | * Create a SurfaceTextureHelper without frame ref monitor. |
| 109 | * |
| 110 | * @see #create(String, EglBase.Context, boolean, YuvConverter, FrameRefMonitor) |
| 111 | */ |
| 112 | public static SurfaceTextureHelper create(final String threadName, |
| 113 | final EglBase.Context sharedContext, boolean alignTimestamps, YuvConverter yuvConverter) { |
| 114 | return create( |
| 115 | threadName, sharedContext, alignTimestamps, yuvConverter, /*frameRefMonitor=*/null); |
| 116 | } |
| 117 |
| 118 | private final RefCountMonitor textureRefCountMonitor = new RefCountMonitor() { |
| 119 | @Override |
| 120 | public void onRetain(TextureBufferImpl textureBuffer) { |
| 121 | if (frameRefMonitor != null) { |
| 122 | frameRefMonitor.onRetainBuffer(textureBuffer); |
| 123 | } |
| 124 | } |
| 125 |
| 126 | @Override |
| 127 | public void onRelease(TextureBufferImpl textureBuffer) { |
| 128 | if (frameRefMonitor != null) { |
| 129 | frameRefMonitor.onReleaseBuffer(textureBuffer); |
| 130 | } |
| 131 | } |
| 132 |
| 133 | @Override |
| 134 | public void onDestroy(TextureBufferImpl textureBuffer) { |
| 135 | returnTextureFrame(); |
| 136 | if (frameRefMonitor != null) { |
| 137 | frameRefMonitor.onDestroyBuffer(textureBuffer); |
| 138 | } |
| 139 | } |
| 140 | }; |
| 141 |
| 142 | private final Handler handler; |
| 143 | private final EglBase eglBase; |
| 144 | private final SurfaceTexture surfaceTexture; |
| 145 | private final int oesTextureId; |
| 146 | private final YuvConverter yuvConverter; |
| 147 | @Nullable private final TimestampAligner timestampAligner; |
| 148 | private final FrameRefMonitor frameRefMonitor; |
| 149 |
| 150 | // These variables are only accessed from the |handler| thread. |
| 151 | @Nullable private VideoSink listener; |
| 152 | // The possible states of this class. |
| 153 | private boolean hasPendingTexture; |
| 154 | private volatile boolean isTextureInUse; |
| 155 | private boolean isQuitting; |
| 156 | private int frameRotation; |
| 157 | private int textureWidth; |
| 158 | private int textureHeight; |
| 159 | // |pendingListener| is set in setListener() and the runnable is posted to the handler thread. |
| 160 | // setListener() is not allowed to be called again before stopListening(), so this is thread safe. |
| 161 | @Nullable private VideoSink pendingListener; |
| 162 | final Runnable setListenerRunnable = new Runnable() { |
| 163 | @Override |
| 164 | public void run() { |
| 165 | Logging.d(TAG, "Setting listener to " + pendingListener); |
| 166 | listener = pendingListener; |
| 167 | pendingListener = null; |
| 168 | // May have a pending frame from the previous capture session - drop it. |
| 169 | if (hasPendingTexture) { |
| 170 | // Calling updateTexImage() is neccessary in order to receive new frames. |
| 171 | updateTexImage(); |
| 172 | hasPendingTexture = false; |
| 173 | } |
| 174 | } |
| 175 | }; |
| 176 |
| 177 | private SurfaceTextureHelper(Context sharedContext, Handler handler, boolean alignTimestamps, |
| 178 | YuvConverter yuvConverter, FrameRefMonitor frameRefMonitor) { |
| 179 | if (handler.getLooper().getThread() != Thread.currentThread()) { |
| 180 | throw new IllegalStateException("SurfaceTextureHelper must be created on the handler thread"); |
| 181 | } |
| 182 | this.handler = handler; |
| 183 | this.timestampAligner = alignTimestamps ? new TimestampAligner() : null; |
| 184 | this.yuvConverter = yuvConverter; |
| 185 | this.frameRefMonitor = frameRefMonitor; |
| 186 |
| 187 | eglBase = EglBase.create(sharedContext, EglBase.CONFIG_PIXEL_BUFFER); |
| 188 | try { |
| 189 | // Both these statements have been observed to fail on rare occasions, see BUG=webrtc:5682. |
| 190 | eglBase.createDummyPbufferSurface(); |
| 191 | eglBase.makeCurrent(); |
| 192 | } catch (RuntimeException e) { |
| 193 | // Clean up before rethrowing the exception. |
| 194 | eglBase.release(); |
| 195 | handler.getLooper().quit(); |
| 196 | throw e; |
| 197 | } |
| 198 |
| 199 | oesTextureId = GlUtil.generateTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES); |
| 200 | surfaceTexture = new SurfaceTexture(oesTextureId); |
| 201 | setOnFrameAvailableListener(surfaceTexture, (SurfaceTexture st) -> { |
| 202 | if (hasPendingTexture) { |
| 203 | Logging.d(TAG, "A frame is already pending, dropping frame."); |
| 204 | } |
| 205 |
| 206 | hasPendingTexture = true; |
| 207 | tryDeliverTextureFrame(); |
| 208 | }, handler); |
| 209 | } |
| 210 |
| 211 | @TargetApi(21) |
| 212 | private static void setOnFrameAvailableListener(SurfaceTexture surfaceTexture, |
| 213 | SurfaceTexture.OnFrameAvailableListener listener, Handler handler) { |
| 214 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 215 | surfaceTexture.setOnFrameAvailableListener(listener, handler); |
| 216 | } else { |
| 217 | // The documentation states that the listener will be called on an arbitrary thread, but in |
| 218 | // pratice, it is always the thread on which the SurfaceTexture was constructed. There are |
| 219 | // assertions in place in case this ever changes. For API >= 21, we use the new API to |
| 220 | // explicitly specify the handler. |
| 221 | surfaceTexture.setOnFrameAvailableListener(listener); |
| 222 | } |
| 223 | } |
| 224 |
| 225 | /** |
| 226 | * Start to stream textures to the given |listener|. If you need to change listener, you need to |
| 227 | * call stopListening() first. |
| 228 | */ |
| 229 | public void startListening(final VideoSink listener) { |
| 230 | if (this.listener != null || this.pendingListener != null) { |
| 231 | throw new IllegalStateException("SurfaceTextureHelper listener has already been set."); |
| 232 | } |
| 233 | this.pendingListener = listener; |
| 234 | handler.post(setListenerRunnable); |
| 235 | } |
| 236 |
| 237 | /** |
| 238 | * Stop listening. The listener set in startListening() is guaranteded to not receive any more |
| 239 | * onFrame() callbacks after this function returns. |
| 240 | */ |
| 241 | public void stopListening() { |
| 242 | Logging.d(TAG, "stopListening()"); |
| 243 | handler.removeCallbacks(setListenerRunnable); |
| 244 | ThreadUtils.invokeAtFrontUninterruptibly(handler, () -> { |
| 245 | listener = null; |
| 246 | pendingListener = null; |
| 247 | }); |
| 248 | } |
| 249 |
| 250 | /** |
| 251 | * Use this function to set the texture size. Note, do not call setDefaultBufferSize() yourself |
| 252 | * since this class needs to be aware of the texture size. |
| 253 | */ |
| 254 | public void setTextureSize(int textureWidth, int textureHeight) { |
| 255 | if (textureWidth <= 0) { |
| 256 | throw new IllegalArgumentException("Texture width must be positive, but was " + textureWidth); |
| 257 | } |
| 258 | if (textureHeight <= 0) { |
| 259 | throw new IllegalArgumentException( |
| 260 | "Texture height must be positive, but was " + textureHeight); |
| 261 | } |
| 262 | surfaceTexture.setDefaultBufferSize(textureWidth, textureHeight); |
| 263 | handler.post(() -> { |
| 264 | this.textureWidth = textureWidth; |
| 265 | this.textureHeight = textureHeight; |
| 266 | tryDeliverTextureFrame(); |
| 267 | }); |
| 268 | } |
| 269 |
| 270 | /** |
| 271 | * Forces a frame to be produced. If no new frame is available, the last frame is sent to the |
| 272 | * listener again. |
| 273 | */ |
| 274 | public void forceFrame() { |
| 275 | handler.post(() -> { |
| 276 | hasPendingTexture = true; |
| 277 | tryDeliverTextureFrame(); |
| 278 | }); |
| 279 | } |
| 280 |
| 281 | /** Set the rotation of the delivered frames. */ |
| 282 | public void setFrameRotation(int rotation) { |
| 283 | handler.post(() -> this.frameRotation = rotation); |
| 284 | } |
| 285 |
| 286 | /** |
| 287 | * Retrieve the underlying SurfaceTexture. The SurfaceTexture should be passed in to a video |
| 288 | * producer such as a camera or decoder. |
| 289 | */ |
| 290 | public SurfaceTexture getSurfaceTexture() { |
| 291 | return surfaceTexture; |
| 292 | } |
| 293 |
| 294 | /** Retrieve the handler that calls onFrame(). This handler is valid until dispose() is called. */ |
| 295 | public Handler getHandler() { |
| 296 | return handler; |
| 297 | } |
| 298 |
| 299 | /** |
| 300 | * This function is called when the texture frame is released. Only one texture frame can be in |
| 301 | * flight at once, so this function must be called before a new frame is delivered. |
| 302 | */ |
| 303 | private void returnTextureFrame() { |
| 304 | handler.post(() -> { |
| 305 | isTextureInUse = false; |
| 306 | if (isQuitting) { |
| 307 | release(); |
| 308 | } else { |
| 309 | tryDeliverTextureFrame(); |
| 310 | } |
| 311 | }); |
| 312 | } |
| 313 |
| 314 | public boolean isTextureInUse() { |
| 315 | return isTextureInUse; |
| 316 | } |
| 317 |
| 318 | /** |
| 319 | * Call disconnect() to stop receiving frames. OpenGL resources are released and the handler is |
| 320 | * stopped when the texture frame has been released. You are guaranteed to not receive any more |
| 321 | * onFrame() after this function returns. |
| 322 | */ |
| 323 | public void dispose() { |
| 324 | Logging.d(TAG, "dispose()"); |
| 325 | ThreadUtils.invokeAtFrontUninterruptibly(handler, () -> { |
| 326 | isQuitting = true; |
| 327 | if (!isTextureInUse) { |
| 328 | release(); |
| 329 | } |
| 330 | }); |
| 331 | } |
| 332 |
| 333 | /** |
| 334 | * Posts to the correct thread to convert |textureBuffer| to I420. |
| 335 | * |
| 336 | * @deprecated Use toI420() instead. |
| 337 | */ |
| 338 | @Deprecated |
| 339 | public VideoFrame.I420Buffer textureToYuv(final TextureBuffer textureBuffer) { |
| 340 | return textureBuffer.toI420(); |
| 341 | } |
| 342 |
| 343 | private void updateTexImage() { |
| 344 | // SurfaceTexture.updateTexImage apparently can compete and deadlock with eglSwapBuffers, |
| 345 | // as observed on Nexus 5. Therefore, synchronize it with the EGL functions. |
| 346 | // See https://bugs.chromium.org/p/webrtc/issues/detail?id=5702 for more info. |
| 347 | synchronized (EglBase.lock) { |
| 348 | try { |
| 349 | surfaceTexture.updateTexImage(); |
| 350 | } catch (Throwable ignore) { |
| 351 |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 |
| 356 | private void tryDeliverTextureFrame() { |
| 357 | if (handler.getLooper().getThread() != Thread.currentThread()) { |
| 358 | throw new IllegalStateException("Wrong thread."); |
| 359 | } |
| 360 | if (isQuitting || !hasPendingTexture || isTextureInUse || listener == null) { |
| 361 | return; |
| 362 | } |
| 363 | if (textureWidth == 0 || textureHeight == 0) { |
| 364 | // Information about the resolution needs to be provided by a call to setTextureSize() before |
| 365 | // frames are produced. |
| 366 | Logging.w(TAG, "Texture size has not been set."); |
| 367 | return; |
| 368 | } |
| 369 | isTextureInUse = true; |
| 370 | hasPendingTexture = false; |
| 371 |
| 372 | updateTexImage(); |
| 373 |
| 374 | final float[] transformMatrix = new float[16]; |
| 375 | surfaceTexture.getTransformMatrix(transformMatrix); |
| 376 | long timestampNs = surfaceTexture.getTimestamp(); |
| 377 | if (timestampAligner != null) { |
| 378 | timestampNs = timestampAligner.translateTimestamp(timestampNs); |
| 379 | } |
| 380 | final VideoFrame.TextureBuffer buffer = |
| 381 | new TextureBufferImpl(textureWidth, textureHeight, TextureBuffer.Type.OES, oesTextureId, |
| 382 | RendererCommon.convertMatrixToAndroidGraphicsMatrix(transformMatrix), handler, |
| 383 | yuvConverter, textureRefCountMonitor); |
| 384 | if (frameRefMonitor != null) { |
| 385 | frameRefMonitor.onNewBuffer(buffer); |
| 386 | } |
| 387 | final VideoFrame frame = new VideoFrame(buffer, frameRotation, timestampNs); |
| 388 | listener.onFrame(frame); |
| 389 | frame.release(); |
| 390 | } |
| 391 |
| 392 | private void release() { |
| 393 | if (handler.getLooper().getThread() != Thread.currentThread()) { |
| 394 | throw new IllegalStateException("Wrong thread."); |
| 395 | } |
| 396 | if (isTextureInUse || !isQuitting) { |
| 397 | throw new IllegalStateException("Unexpected release."); |
| 398 | } |
| 399 | yuvConverter.release(); |
| 400 | GLES20.glDeleteTextures(1, new int[] {oesTextureId}, 0); |
| 401 | surfaceTexture.release(); |
| 402 | eglBase.release(); |
| 403 | handler.getLooper().quit(); |
| 404 | if (timestampAligner != null) { |
| 405 | timestampAligner.dispose(); |
| 406 | } |
| 407 | } |
| 408 | } |