| 6 | import android.graphics.SurfaceTexture; |
| 7 | import android.os.Looper; |
| 8 | import android.view.TextureView; |
| 9 | import android.view.View; |
| 10 |
| 11 | import org.telegram.messenger.AndroidUtilities; |
| 12 |
| 13 | import java.util.concurrent.CountDownLatch; |
| 14 |
| 15 | public class TextureViewRenderer extends TextureView |
| 16 | implements TextureView.SurfaceTextureListener, VideoSink, RendererCommon.RendererEvents { |
| 17 |
| 18 | private static final String TAG = "TextureViewRenderer"; |
| 19 |
| 20 | // Cached resource name. |
| 21 | private final String resourceName; |
| 22 | private final RendererCommon.VideoLayoutMeasure videoLayoutMeasure = |
| 23 | new RendererCommon.VideoLayoutMeasure(); |
| 24 | private final TextureEglRenderer eglRenderer; |
| 25 |
| 26 | // Callback for reporting renderer events. Read-only after initialization so no lock required. |
| 27 | private RendererCommon.RendererEvents rendererEvents; |
| 28 |
| 29 | // Accessed only on the main thread. |
| 30 | private int rotatedFrameWidth; |
| 31 | private int rotatedFrameHeight; |
| 32 | private boolean enableFixedSize; |
| 33 | private int surfaceWidth; |
| 34 | private int surfaceHeight; |
| 35 | private boolean isCamera; |
| 36 |
| 37 | private OrientationHelper orientationHelper; |
| 38 |
| 39 | public static class TextureEglRenderer extends EglRenderer implements TextureView.SurfaceTextureListener { |
| 40 | private static final String TAG = "TextureEglRenderer"; |
| 41 |
| 42 | // Callback for reporting renderer events. Read-only after initialization so no lock required. |
| 43 | private RendererCommon.RendererEvents rendererEvents; |
| 44 |
| 45 | private final Object layoutLock = new Object(); |
| 46 | private boolean isRenderingPaused; |
| 47 | private boolean isFirstFrameRendered; |
| 48 | private int rotatedFrameWidth; |
| 49 | private int rotatedFrameHeight; |
| 50 | private int frameRotation; |
| 51 |
| 52 | /** |
| 53 | * In order to render something, you must first call init(). |
| 54 | */ |
| 55 | public TextureEglRenderer(String name) { |
| 56 | super(name); |
| 57 | } |
| 58 |
| 59 | /** |
| 60 | * Initialize this class, sharing resources with |sharedContext|. The custom |drawer| will be used |
| 61 | * for drawing frames on the EGLSurface. This class is responsible for calling release() on |
| 62 | * |drawer|. It is allowed to call init() to reinitialize the renderer after a previous |
| 63 | * init()/release() cycle. |
| 64 | */ |
| 65 | public void init(final EglBase.Context sharedContext, |
| 66 | RendererCommon.RendererEvents rendererEvents, final int[] configAttributes, |
| 67 | RendererCommon.GlDrawer drawer) { |
| 68 | ThreadUtils.checkIsOnMainThread(); |
| 69 | this.rendererEvents = rendererEvents; |
| 70 | synchronized (layoutLock) { |
| 71 | isFirstFrameRendered = false; |
| 72 | rotatedFrameWidth = 0; |
| 73 | rotatedFrameHeight = 0; |
| 74 | frameRotation = 0; |
| 75 | } |
| 76 | super.init(sharedContext, configAttributes, drawer); |
| 77 | } |
| 78 |
| 79 | @Override |
| 80 | public void init(final EglBase.Context sharedContext, final int[] configAttributes, |
| 81 | RendererCommon.GlDrawer drawer) { |
| 82 | init(sharedContext, null /* rendererEvents */, configAttributes, drawer); |
| 83 | } |
| 84 |
| 85 | /** |
| 86 | * Limit render framerate. |
| 87 | * |
| 88 | * @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps |
| 89 | * reduction. |
| 90 | */ |
| 91 | @Override |
| 92 | public void setFpsReduction(float fps) { |
| 93 | synchronized (layoutLock) { |
| 94 | isRenderingPaused = fps == 0f; |
| 95 | } |
| 96 | super.setFpsReduction(fps); |
| 97 | } |
| 98 |
| 99 | @Override |
| 100 | public void disableFpsReduction() { |
| 101 | synchronized (layoutLock) { |
| 102 | isRenderingPaused = false; |
| 103 | } |
| 104 | super.disableFpsReduction(); |
| 105 | } |
| 106 |
| 107 | @Override |
| 108 | public void pauseVideo() { |
| 109 | synchronized (layoutLock) { |
| 110 | isRenderingPaused = true; |
| 111 | } |
| 112 | super.pauseVideo(); |
| 113 | } |
| 114 |
| 115 | // VideoSink interface. |
| 116 | @Override |
| 117 | public void onFrame(VideoFrame frame) { |
| 118 | updateFrameDimensionsAndReportEvents(frame); |
| 119 | super.onFrame(frame); |
| 120 | } |
| 121 |
| 122 | @Override |
| 123 | public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { |
| 124 | ThreadUtils.checkIsOnMainThread(); |
| 125 | createEglSurface(surfaceTexture); |
| 126 | } |
| 127 |
| 128 | @Override |
| 129 | public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) { |
| 130 | ThreadUtils.checkIsOnMainThread(); |
| 131 | logD("surfaceChanged: size: " + width + "x" + height); |
| 132 | } |
| 133 |
| 134 | @Override |
| 135 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { |
| 136 | ThreadUtils.checkIsOnMainThread(); |
| 137 | final CountDownLatch completionLatch = new CountDownLatch(1); |
| 138 | releaseEglSurface(completionLatch::countDown); |
| 139 | ThreadUtils.awaitUninterruptibly(completionLatch); |
| 140 | return true; |
| 141 | } |
| 142 |
| 143 | @Override |
| 144 | public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { |
| 145 |
| 146 | } |
| 147 |
| 148 | // Update frame dimensions and report any changes to |rendererEvents|. |
| 149 | private void updateFrameDimensionsAndReportEvents(VideoFrame frame) { |
| 150 | synchronized (layoutLock) { |
| 151 | if (isRenderingPaused) { |
| 152 | return; |
| 153 | } |
| 154 | if (rotatedFrameWidth != frame.getRotatedWidth() |
| 155 | || rotatedFrameHeight != frame.getRotatedHeight() |
| 156 | || frameRotation != frame.getRotation()) { |
| 157 | logD("Reporting frame resolution changed to " + frame.getBuffer().getWidth() + "x" |
| 158 | + frame.getBuffer().getHeight() + " with rotation " + frame.getRotation()); |
| 159 | if (rendererEvents != null) { |
| 160 | rendererEvents.onFrameResolutionChanged( |
| 161 | frame.getBuffer().getWidth(), frame.getBuffer().getHeight(), frame.getRotation()); |
| 162 | } |
| 163 | rotatedFrameWidth = frame.getRotatedWidth(); |
| 164 | rotatedFrameHeight = frame.getRotatedHeight(); |
| 165 | frameRotation = frame.getRotation(); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 |
| 170 | private void logD(String string) { |
| 171 | Logging.d(TAG, name + ": " + string); |
| 172 | } |
| 173 |
| 174 | @Override |
| 175 | protected void onFirstFrameRendered() { |
| 176 | AndroidUtilities.runOnUIThread(() -> { |
| 177 | isFirstFrameRendered = true; |
| 178 | rendererEvents.onFirstFrameRendered(); |
| 179 | }); |
| 180 | } |
| 181 | } |
| 182 |
| 183 | /** |
| 184 | * Standard View constructor. In order to render something, you must first call init(). |
| 185 | */ |
| 186 | public TextureViewRenderer(Context context) { |
| 187 | super(context); |
| 188 | this.resourceName = getResourceName(); |
| 189 | eglRenderer = new TextureEglRenderer(resourceName); |
| 190 | setSurfaceTextureListener(this); |
| 191 | } |
| 192 |
| 193 | /** |
| 194 | * Initialize this class, sharing resources with |sharedContext|. It is allowed to call init() to |
| 195 | * reinitialize the renderer after a previous init()/release() cycle. |
| 196 | */ |
| 197 | public void init(EglBase.Context sharedContext, RendererCommon.RendererEvents rendererEvents) { |
| 198 | init(sharedContext, rendererEvents, EglBase.CONFIG_PLAIN, new GlRectDrawer()); |
| 199 | } |
| 200 |
| 201 | /** |
| 202 | * Initialize this class, sharing resources with |sharedContext|. The custom |drawer| will be used |
| 203 | * for drawing frames on the EGLSurface. This class is responsible for calling release() on |
| 204 | * |drawer|. It is allowed to call init() to reinitialize the renderer after a previous |
| 205 | * init()/release() cycle. |
| 206 | */ |
| 207 | public void init(final EglBase.Context sharedContext, |
| 208 | RendererCommon.RendererEvents rendererEvents, final int[] configAttributes, |
| 209 | RendererCommon.GlDrawer drawer) { |
| 210 | ThreadUtils.checkIsOnMainThread(); |
| 211 | this.rendererEvents = rendererEvents; |
| 212 | rotatedFrameWidth = 0; |
| 213 | rotatedFrameHeight = 0; |
| 214 | eglRenderer.init(sharedContext, this /* rendererEvents */, configAttributes, drawer); |
| 215 | } |
| 216 |
| 217 | /** |
| 218 | * Block until any pending frame is returned and all GL resources released, even if an interrupt |
| 219 | * occurs. If an interrupt occurs during release(), the interrupt flag will be set. This function |
| 220 | * should be called before the Activity is destroyed and the EGLContext is still valid. If you |
| 221 | * don't call this function, the GL resources might leak. |
| 222 | */ |
| 223 | public void release() { |
| 224 | eglRenderer.release(); |
| 225 | if (orientationHelper != null) { |
| 226 | orientationHelper.stop(); |
| 227 | } |
| 228 | } |
| 229 |
| 230 | /** |
| 231 | * Register a callback to be invoked when a new video frame has been received. |
| 232 | * |
| 233 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 234 | * It should be lightweight and must not call removeFrameListener. |
| 235 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 236 | * required. |
| 237 | * @param drawer Custom drawer to use for this frame listener. |
| 238 | */ |
| 239 | public void addFrameListener( |
| 240 | EglRenderer.FrameListener listener, float scale, RendererCommon.GlDrawer drawerParam) { |
| 241 | eglRenderer.addFrameListener(listener, scale, drawerParam); |
| 242 | } |
| 243 |
| 244 | /** |
| 245 | * Register a callback to be invoked when a new video frame has been received. This version uses |
| 246 | * the drawer of the EglRenderer that was passed in init. |
| 247 | * |
| 248 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 249 | * It should be lightweight and must not call removeFrameListener. |
| 250 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 251 | * required. |
| 252 | */ |
| 253 | public void addFrameListener(EglRenderer.FrameListener listener, float scale) { |
| 254 | eglRenderer.addFrameListener(listener, scale); |
| 255 | } |
| 256 |
| 257 | public void removeFrameListener(EglRenderer.FrameListener listener) { |
| 258 | eglRenderer.removeFrameListener(listener); |
| 259 | } |
| 260 |
| 261 | public void setIsCamera(boolean value) { |
| 262 | isCamera = value; |
| 263 | if (!isCamera) { |
| 264 | orientationHelper = new OrientationHelper() { |
| 265 | @Override |
| 266 | protected void onOrientationUpdate(int orientation) { |
| 267 | updateRotation(); |
| 268 | } |
| 269 | }; |
| 270 | orientationHelper.start(); |
| 271 | } |
| 272 | } |
| 273 |
| 274 | /** |
| 275 | * Enables fixed size for the surface. This provides better performance but might be buggy on some |
| 276 | * devices. By default this is turned off. |
| 277 | */ |
| 278 | public void setEnableHardwareScaler(boolean enabled) { |
| 279 | ThreadUtils.checkIsOnMainThread(); |
| 280 | enableFixedSize = enabled; |
| 281 | updateSurfaceSize(); |
| 282 | } |
| 283 |
| 284 | private void updateRotation() { |
| 285 | if (orientationHelper == null || rotatedFrameWidth == 0 || rotatedFrameHeight == 0) { |
| 286 | return; |
| 287 | } |
| 288 | View parentView = (View) getParent(); |
| 289 | if (parentView == null) { |
| 290 | return; |
| 291 | } |
| 292 | int orientation = orientationHelper.getOrientation(); |
| 293 | float viewWidth = getMeasuredWidth(); |
| 294 | float viewHeight = getMeasuredHeight(); |
| 295 | float w; |
| 296 | float h; |
| 297 | float targetWidth = parentView.getMeasuredWidth(); |
| 298 | float targetHeight = parentView.getMeasuredHeight(); |
| 299 | if (orientation == 90 || orientation == 270) { |
| 300 | w = viewHeight; |
| 301 | h = viewWidth; |
| 302 | } else { |
| 303 | w = viewWidth; |
| 304 | h = viewHeight; |
| 305 | } |
| 306 | float scale; |
| 307 | if (w < h) { |
| 308 | scale = Math.max(w / viewWidth, h / viewHeight); |
| 309 | } else { |
| 310 | scale = Math.min(w / viewWidth, h / viewHeight); |
| 311 | } |
| 312 | w *= scale; |
| 313 | h *= scale; |
| 314 | if (Math.abs(w / h - targetWidth / targetHeight) < 0.1f) { |
| 315 | scale *= Math.max(targetWidth / w, targetHeight / h); |
| 316 | } |
| 317 | if (orientation == 270) { |
| 318 | orientation = -90; |
| 319 | } |
| 320 | animate().scaleX(scale).scaleY(scale).rotation(-orientation).setDuration(180).start(); |
| 321 | } |
| 322 |
| 323 | /** |
| 324 | * Set if the video stream should be mirrored or not. |
| 325 | */ |
| 326 | public void setMirror(final boolean mirror) { |
| 327 | eglRenderer.setMirror(mirror); |
| 328 | } |
| 329 |
| 330 | /** |
| 331 | * Set how the video will fill the allowed layout area. |
| 332 | */ |
| 333 | public void setScalingType(RendererCommon.ScalingType scalingType) { |
| 334 | ThreadUtils.checkIsOnMainThread(); |
| 335 | videoLayoutMeasure.setScalingType(scalingType); |
| 336 | requestLayout(); |
| 337 | } |
| 338 |
| 339 | public void setScalingType(RendererCommon.ScalingType scalingTypeMatchOrientation, |
| 340 | RendererCommon.ScalingType scalingTypeMismatchOrientation) { |
| 341 | ThreadUtils.checkIsOnMainThread(); |
| 342 | videoLayoutMeasure.setScalingType(scalingTypeMatchOrientation, scalingTypeMismatchOrientation); |
| 343 | requestLayout(); |
| 344 | } |
| 345 |
| 346 | /** |
| 347 | * Limit render framerate. |
| 348 | * |
| 349 | * @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps |
| 350 | * reduction. |
| 351 | */ |
| 352 | public void setFpsReduction(float fps) { |
| 353 | eglRenderer.setFpsReduction(fps); |
| 354 | } |
| 355 |
| 356 | public void disableFpsReduction() { |
| 357 | eglRenderer.disableFpsReduction(); |
| 358 | } |
| 359 |
| 360 | public void pauseVideo() { |
| 361 | eglRenderer.pauseVideo(); |
| 362 | } |
| 363 |
| 364 | // VideoSink interface. |
| 365 | @Override |
| 366 | public void onFrame(VideoFrame frame) { |
| 367 | eglRenderer.onFrame(frame); |
| 368 | } |
| 369 |
| 370 | // View layout interface. |
| 371 | @Override |
| 372 | protected void onMeasure(int widthSpec, int heightSpec) { |
| 373 | ThreadUtils.checkIsOnMainThread(); |
| 374 | Point size = videoLayoutMeasure.measure(isCamera, widthSpec, heightSpec, rotatedFrameWidth, rotatedFrameHeight); |
| 375 | setMeasuredDimension(size.x, size.y); |
| 376 | if (!isCamera) { |
| 377 | updateRotation(); |
| 378 | } |
| 379 | logD("onMeasure(). New size: " + size.x + "x" + size.y); |
| 380 | } |
| 381 |
| 382 | @Override |
| 383 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 384 | ThreadUtils.checkIsOnMainThread(); |
| 385 | eglRenderer.setLayoutAspectRatio((right - left) / (float) (bottom - top)); |
| 386 | updateSurfaceSize(); |
| 387 | } |
| 388 |
| 389 | private void updateSurfaceSize() { |
| 390 | ThreadUtils.checkIsOnMainThread(); |
| 391 | if (enableFixedSize && rotatedFrameWidth != 0 && rotatedFrameHeight != 0 && getWidth() != 0 |
| 392 | && getHeight() != 0) { |
| 393 | final float layoutAspectRatio = getWidth() / (float) getHeight(); |
| 394 | final float frameAspectRatio = rotatedFrameWidth / (float) rotatedFrameHeight; |
| 395 | final int drawnFrameWidth; |
| 396 | final int drawnFrameHeight; |
| 397 | if (frameAspectRatio > layoutAspectRatio) { |
| 398 | drawnFrameWidth = (int) (rotatedFrameHeight * layoutAspectRatio); |
| 399 | drawnFrameHeight = rotatedFrameHeight; |
| 400 | } else { |
| 401 | drawnFrameWidth = rotatedFrameWidth; |
| 402 | drawnFrameHeight = (int) (rotatedFrameHeight / layoutAspectRatio); |
| 403 | } |
| 404 | // Aspect ratio of the drawn frame and the view is the same. |
| 405 | final int width = Math.min(getWidth(), drawnFrameWidth); |
| 406 | final int height = Math.min(getHeight(), drawnFrameHeight); |
| 407 | logD("updateSurfaceSize. Layout size: " + getWidth() + "x" + getHeight() + ", frame size: " |
| 408 | + rotatedFrameWidth + "x" + rotatedFrameHeight + ", requested surface size: " + width |
| 409 | + "x" + height + ", old surface size: " + surfaceWidth + "x" + surfaceHeight); |
| 410 | if (width != surfaceWidth || height != surfaceHeight) { |
| 411 | surfaceWidth = width; |
| 412 | surfaceHeight = height; |
| 413 | //getHolder().setFixedSize(width, height); |
| 414 | } |
| 415 | } else { |
| 416 | surfaceWidth = surfaceHeight = 0; |
| 417 | //getHolder().setSizeFromLayout(); |
| 418 | } |
| 419 | } |
| 420 |
| 421 | @Override |
| 422 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { |
| 423 | ThreadUtils.checkIsOnMainThread(); |
| 424 | surfaceWidth = surfaceHeight = 0; |
| 425 | updateSurfaceSize(); |
| 426 | eglRenderer.onSurfaceTextureAvailable(surface, width, height); |
| 427 | } |
| 428 |
| 429 | @Override |
| 430 | public void onSurfaceTextureSizeChanged(SurfaceTexture surface, final int width, final int height) { |
| 431 | surfaceWidth = width; |
| 432 | surfaceHeight = height; |
| 433 | eglRenderer.onSurfaceTextureSizeChanged(surface, width, height); |
| 434 | } |
| 435 |
| 436 | @Override |
| 437 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { |
| 438 | eglRenderer.onSurfaceTextureDestroyed(surfaceTexture); |
| 439 | return true; |
| 440 | } |
| 441 |
| 442 | @Override |
| 443 | public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { |
| 444 | eglRenderer.onSurfaceTextureUpdated(surfaceTexture); |
| 445 | } |
| 446 |
| 447 | private String getResourceName() { |
| 448 | try { |
| 449 | return getResources().getResourceEntryName(getId()); |
| 450 | } catch (Resources.NotFoundException e) { |
| 451 | return ""; |
| 452 | } |
| 453 | } |
| 454 |
| 455 | /** |
| 456 | * Post a task to clear the SurfaceView to a transparent uniform color. |
| 457 | */ |
| 458 | public void clearImage() { |
| 459 | eglRenderer.clearImage(); |
| 460 | } |
| 461 |
| 462 | @Override |
| 463 | public void onFirstFrameRendered() { |
| 464 | if (rendererEvents != null) { |
| 465 | rendererEvents.onFirstFrameRendered(); |
| 466 | } |
| 467 | } |
| 468 |
| 469 | public boolean isFirstFrameRendered() { |
| 470 | return eglRenderer.isFirstFrameRendered; |
| 471 | } |
| 472 |
| 473 | @Override |
| 474 | public void onFrameResolutionChanged(int videoWidth, int videoHeight, int rotation) { |
| 475 | if (rendererEvents != null) { |
| 476 | rendererEvents.onFrameResolutionChanged(videoWidth, videoHeight, rotation); |
| 477 | } |
| 478 | if (isCamera) { |
| 479 | eglRenderer.setRotation(-OrientationHelper.cameraRotation); |
| 480 | } |
| 481 | int rotatedWidth = rotation == 0 || rotation == 180 ? videoWidth : videoHeight; |
| 482 | int rotatedHeight = rotation == 0 || rotation == 180 ? videoHeight : videoWidth; |
| 483 | // run immediately if possible for ui thread tests |
| 484 | postOrRun(() -> { |
| 485 | rotatedFrameWidth = rotatedWidth; |
| 486 | rotatedFrameHeight = rotatedHeight; |
| 487 | updateSurfaceSize(); |
| 488 | requestLayout(); |
| 489 | }); |
| 490 | } |
| 491 |
| 492 | private void postOrRun(Runnable r) { |
| 493 | if (Thread.currentThread() == Looper.getMainLooper().getThread()) { |
| 494 | r.run(); |
| 495 | } else { |
| 496 | post(r); |
| 497 | } |
| 498 | } |
| 499 |
| 500 | private void logD(String string) { |
| 501 | Logging.d(TAG, resourceName + ": " + string); |
| 502 | } |
| 503 | } |
| 504 |