| 6 | import android.graphics.SurfaceTexture; |
| 7 | import android.os.Looper; |
| 8 | import android.view.TextureView; |
| 9 | import android.view.View; |
| 10 |
| 11 | import androidx.annotation.NonNull; |
| 12 |
| 13 | import org.telegram.messenger.AndroidUtilities; |
| 14 | import org.telegram.messenger.voip.VoIPService; |
| 15 |
| 16 | import java.util.concurrent.CountDownLatch; |
| 17 |
| 18 | public class TextureViewRenderer extends TextureView |
| 19 | implements TextureView.SurfaceTextureListener, VideoSink, RendererCommon.RendererEvents { |
| 20 |
| 21 | private static final String TAG = "TextureViewRenderer"; |
| 22 |
| 23 | // Cached resource name. |
| 24 | private final String resourceName; |
| 25 | private final RendererCommon.VideoLayoutMeasure videoLayoutMeasure = |
| 26 | new RendererCommon.VideoLayoutMeasure(); |
| 27 | private final TextureEglRenderer eglRenderer; |
| 28 |
| 29 | // Callback for reporting renderer events. Read-only after initialization so no lock required. |
| 30 | private RendererCommon.RendererEvents rendererEvents; |
| 31 |
| 32 | // Accessed only on the main thread. |
| 33 | public int rotatedFrameWidth; |
| 34 | public int rotatedFrameHeight; |
| 35 |
| 36 | private int videoWidth; |
| 37 | private int videoHeight; |
| 38 | private boolean enableFixedSize; |
| 39 | private int surfaceWidth; |
| 40 | private int surfaceHeight; |
| 41 | private boolean isCamera; |
| 42 | private boolean mirror; |
| 43 | private boolean rotateTextureWitchScreen; |
| 44 | private int screenRotation; |
| 45 |
| 46 | private OrientationHelper orientationHelper; |
| 47 | private int cameraRotation; |
| 48 | private TextureView backgroundRenderer; |
| 49 | private int maxTextureSize; |
| 50 |
| 51 | private VideoSink parentSink; |
| 52 |
| 53 | Runnable updateScreenRunnable; |
| 54 |
| 55 | public void setBackgroundRenderer(TextureView backgroundRenderer) { |
| 56 | this.backgroundRenderer = backgroundRenderer; |
| 57 | backgroundRenderer.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { |
| 58 | @Override |
| 59 | public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture, int i, int i1) { |
| 60 | createBackgroundSurface(surfaceTexture); |
| 61 | } |
| 62 |
| 63 | @Override |
| 64 | public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture, int i, int i1) { |
| 65 |
| 66 | } |
| 67 |
| 68 | @Override |
| 69 | public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) { |
| 70 | ThreadUtils.checkIsOnMainThread(); |
| 71 | eglRenderer.releaseEglSurface(null, true); |
| 72 | return false; |
| 73 | } |
| 74 |
| 75 | @Override |
| 76 | public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) { |
| 77 |
| 78 | } |
| 79 | }); |
| 80 | } |
| 81 |
| 82 | public void clearFirstFrame() { |
| 83 | eglRenderer.firstFrameRendered = false; |
| 84 | eglRenderer.isFirstFrameRendered = false; |
| 85 | } |
| 86 |
| 87 | public static class TextureEglRenderer extends EglRenderer implements TextureView.SurfaceTextureListener { |
| 88 | private static final String TAG = "TextureEglRenderer"; |
| 89 |
| 90 | // Callback for reporting renderer events. Read-only after initialization so no lock required. |
| 91 | private RendererCommon.RendererEvents rendererEvents; |
| 92 |
| 93 | private final Object layoutLock = new Object(); |
| 94 | private boolean isRenderingPaused; |
| 95 | private boolean isFirstFrameRendered; |
| 96 | private int rotatedFrameWidth; |
| 97 | private int rotatedFrameHeight; |
| 98 | private int frameRotation; |
| 99 |
| 100 | /** |
| 101 | * In order to render something, you must first call init(). |
| 102 | */ |
| 103 | public TextureEglRenderer(String name) { |
| 104 | super(name); |
| 105 | } |
| 106 |
| 107 | /** |
| 108 | * Initialize this class, sharing resources with |sharedContext|. The custom |drawer| will be used |
| 109 | * for drawing frames on the EGLSurface. This class is responsible for calling release() on |
| 110 | * |drawer|. It is allowed to call init() to reinitialize the renderer after a previous |
| 111 | * init()/release() cycle. |
| 112 | */ |
| 113 | public void init(final EglBase.Context sharedContext, |
| 114 | RendererCommon.RendererEvents rendererEvents, final int[] configAttributes, |
| 115 | RendererCommon.GlDrawer drawer) { |
| 116 | ThreadUtils.checkIsOnMainThread(); |
| 117 | this.rendererEvents = rendererEvents; |
| 118 | synchronized (layoutLock) { |
| 119 | isFirstFrameRendered = false; |
| 120 | rotatedFrameWidth = 0; |
| 121 | rotatedFrameHeight = 0; |
| 122 | frameRotation = 0; |
| 123 | } |
| 124 | super.init(sharedContext, configAttributes, drawer); |
| 125 | } |
| 126 |
| 127 | @Override |
| 128 | public void init(final EglBase.Context sharedContext, final int[] configAttributes, |
| 129 | RendererCommon.GlDrawer drawer) { |
| 130 | init(sharedContext, null /* rendererEvents */, configAttributes, drawer); |
| 131 | } |
| 132 |
| 133 | /** |
| 134 | * Limit render framerate. |
| 135 | * |
| 136 | * @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps |
| 137 | * reduction. |
| 138 | */ |
| 139 | @Override |
| 140 | public void setFpsReduction(float fps) { |
| 141 | synchronized (layoutLock) { |
| 142 | isRenderingPaused = fps == 0f; |
| 143 | } |
| 144 | super.setFpsReduction(fps); |
| 145 | } |
| 146 |
| 147 | @Override |
| 148 | public void disableFpsReduction() { |
| 149 | synchronized (layoutLock) { |
| 150 | isRenderingPaused = false; |
| 151 | } |
| 152 | super.disableFpsReduction(); |
| 153 | } |
| 154 |
| 155 | @Override |
| 156 | public void pauseVideo() { |
| 157 | synchronized (layoutLock) { |
| 158 | isRenderingPaused = true; |
| 159 | } |
| 160 | super.pauseVideo(); |
| 161 | } |
| 162 |
| 163 | // VideoSink interface. |
| 164 | @Override |
| 165 | public void onFrame(VideoFrame frame) { |
| 166 | updateFrameDimensionsAndReportEvents(frame); |
| 167 | super.onFrame(frame); |
| 168 | } |
| 169 |
| 170 | @Override |
| 171 | public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { |
| 172 | ThreadUtils.checkIsOnMainThread(); |
| 173 | createEglSurface(surfaceTexture); |
| 174 | } |
| 175 |
| 176 | @Override |
| 177 | public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) { |
| 178 | ThreadUtils.checkIsOnMainThread(); |
| 179 | logD("surfaceChanged: size: " + width + "x" + height); |
| 180 | } |
| 181 |
| 182 | @Override |
| 183 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { |
| 184 | ThreadUtils.checkIsOnMainThread(); |
| 185 | final CountDownLatch completionLatch = new CountDownLatch(1); |
| 186 | releaseEglSurface(completionLatch::countDown, false); |
| 187 | ThreadUtils.awaitUninterruptibly(completionLatch); |
| 188 | return true; |
| 189 | } |
| 190 |
| 191 | @Override |
| 192 | public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { |
| 193 |
| 194 | } |
| 195 |
| 196 | // Update frame dimensions and report any changes to |rendererEvents|. |
| 197 | private void updateFrameDimensionsAndReportEvents(VideoFrame frame) { |
| 198 | synchronized (layoutLock) { |
| 199 | if (isRenderingPaused) { |
| 200 | return; |
| 201 | } |
| 202 | if (rotatedFrameWidth != frame.getRotatedWidth() |
| 203 | || rotatedFrameHeight != frame.getRotatedHeight() |
| 204 | || frameRotation != frame.getRotation()) { |
| 205 | logD("Reporting frame resolution changed to " + frame.getBuffer().getWidth() + "x" |
| 206 | + frame.getBuffer().getHeight() + " with rotation " + frame.getRotation()); |
| 207 | if (rendererEvents != null) { |
| 208 | rendererEvents.onFrameResolutionChanged( |
| 209 | frame.getBuffer().getWidth(), frame.getBuffer().getHeight(), frame.getRotation()); |
| 210 | } |
| 211 | rotatedFrameWidth = frame.getRotatedWidth(); |
| 212 | rotatedFrameHeight = frame.getRotatedHeight(); |
| 213 |
| 214 | frameRotation = frame.getRotation(); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 |
| 219 | private void logD(String string) { |
| 220 | Logging.d(TAG, name + ": " + string); |
| 221 | } |
| 222 |
| 223 | @Override |
| 224 | protected void onFirstFrameRendered() { |
| 225 | AndroidUtilities.runOnUIThread(() -> { |
| 226 | isFirstFrameRendered = true; |
| 227 | rendererEvents.onFirstFrameRendered(); |
| 228 | }); |
| 229 | } |
| 230 | } |
| 231 |
| 232 | /** |
| 233 | * Standard View constructor. In order to render something, you must first call init(). |
| 234 | */ |
| 235 | public TextureViewRenderer(Context context) { |
| 236 | super(context); |
| 237 | this.resourceName = getResourceName(); |
| 238 | eglRenderer = new TextureEglRenderer(resourceName); |
| 239 | setSurfaceTextureListener(this); |
| 240 | } |
| 241 |
| 242 | /** |
| 243 | * Initialize this class, sharing resources with |sharedContext|. It is allowed to call init() to |
| 244 | * reinitialize the renderer after a previous init()/release() cycle. |
| 245 | */ |
| 246 | public void init(EglBase.Context sharedContext, RendererCommon.RendererEvents rendererEvents) { |
| 247 | init(sharedContext, rendererEvents, EglBase.CONFIG_PLAIN, new GlRectDrawer()); |
| 248 | } |
| 249 |
| 250 | /** |
| 251 | * Initialize this class, sharing resources with |sharedContext|. The custom |drawer| will be used |
| 252 | * for drawing frames on the EGLSurface. This class is responsible for calling release() on |
| 253 | * |drawer|. It is allowed to call init() to reinitialize the renderer after a previous |
| 254 | * init()/release() cycle. |
| 255 | */ |
| 256 | public void init(final EglBase.Context sharedContext, |
| 257 | RendererCommon.RendererEvents rendererEvents, final int[] configAttributes, |
| 258 | RendererCommon.GlDrawer drawer) { |
| 259 | ThreadUtils.checkIsOnMainThread(); |
| 260 | this.rendererEvents = rendererEvents; |
| 261 | rotatedFrameWidth = 0; |
| 262 | rotatedFrameHeight = 0; |
| 263 | eglRenderer.init(sharedContext, this /* rendererEvents */, configAttributes, drawer); |
| 264 | } |
| 265 |
| 266 | /** |
| 267 | * Block until any pending frame is returned and all GL resources released, even if an interrupt |
| 268 | * occurs. If an interrupt occurs during release(), the interrupt flag will be set. This function |
| 269 | * should be called before the Activity is destroyed and the EGLContext is still valid. If you |
| 270 | * don't call this function, the GL resources might leak. |
| 271 | */ |
| 272 | public void release() { |
| 273 | eglRenderer.release(); |
| 274 | if (orientationHelper != null) { |
| 275 | orientationHelper.stop(); |
| 276 | } |
| 277 | } |
| 278 |
| 279 | /** |
| 280 | * Register a callback to be invoked when a new video frame has been received. |
| 281 | * |
| 282 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 283 | * It should be lightweight and must not call removeFrameListener. |
| 284 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 285 | * required. |
| 286 | */ |
| 287 | public void addFrameListener( |
| 288 | EglRenderer.FrameListener listener, float scale, RendererCommon.GlDrawer drawerParam) { |
| 289 | eglRenderer.addFrameListener(listener, scale, drawerParam); |
| 290 | } |
| 291 |
| 292 | public void getRenderBufferBitmap(GlGenericDrawer.TextureCallback callback) { |
| 293 | eglRenderer.getTexture(callback); |
| 294 | } |
| 295 |
| 296 | /** |
| 297 | * Register a callback to be invoked when a new video frame has been received. This version uses |
| 298 | * the drawer of the EglRenderer that was passed in init. |
| 299 | * |
| 300 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 301 | * It should be lightweight and must not call removeFrameListener. |
| 302 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 303 | * required. |
| 304 | */ |
| 305 | public void addFrameListener(EglRenderer.FrameListener listener, float scale) { |
| 306 | eglRenderer.addFrameListener(listener, scale); |
| 307 | } |
| 308 |
| 309 | public void removeFrameListener(EglRenderer.FrameListener listener) { |
| 310 | eglRenderer.removeFrameListener(listener); |
| 311 | } |
| 312 |
| 313 | public void setIsCamera(boolean value) { |
| 314 | isCamera = value; |
| 315 | if (!isCamera) { |
| 316 | orientationHelper = new OrientationHelper() { |
| 317 | @Override |
| 318 | protected void onOrientationUpdate(int orientation) { |
| 319 | if (!isCamera) { |
| 320 | updateRotation(); |
| 321 | } |
| 322 | } |
| 323 | }; |
| 324 | orientationHelper.start(); |
| 325 | } |
| 326 | } |
| 327 |
| 328 | /** |
| 329 | * Enables fixed size for the surface. This provides better performance but might be buggy on some |
| 330 | * devices. By default this is turned off. |
| 331 | */ |
| 332 | public void setEnableHardwareScaler(boolean enabled) { |
| 333 | ThreadUtils.checkIsOnMainThread(); |
| 334 | enableFixedSize = enabled; |
| 335 | updateSurfaceSize(); |
| 336 | } |
| 337 |
| 338 | private void updateRotation() { |
| 339 | if (orientationHelper == null || rotatedFrameWidth == 0 || rotatedFrameHeight == 0) { |
| 340 | return; |
| 341 | } |
| 342 | View parentView = (View) getParent(); |
| 343 | if (parentView == null) { |
| 344 | return; |
| 345 | } |
| 346 | int orientation = orientationHelper.getOrientation(); |
| 347 | float viewWidth = getMeasuredWidth(); |
| 348 | float viewHeight = getMeasuredHeight(); |
| 349 | float w; |
| 350 | float h; |
| 351 | float targetWidth = parentView.getMeasuredWidth(); |
| 352 | float targetHeight = parentView.getMeasuredHeight(); |
| 353 | if (orientation == 90 || orientation == 270) { |
| 354 | w = viewHeight; |
| 355 | h = viewWidth; |
| 356 | } else { |
| 357 | w = viewWidth; |
| 358 | h = viewHeight; |
| 359 | } |
| 360 | float scale; |
| 361 | if (w < h) { |
| 362 | scale = Math.max(w / viewWidth, h / viewHeight); |
| 363 | } else { |
| 364 | scale = Math.min(w / viewWidth, h / viewHeight); |
| 365 | } |
| 366 | w *= scale; |
| 367 | h *= scale; |
| 368 | if (Math.abs(w / h - targetWidth / targetHeight) < 0.1f) { |
| 369 | scale *= Math.max(targetWidth / w, targetHeight / h); |
| 370 | } |
| 371 | if (orientation == 270) { |
| 372 | orientation = -90; |
| 373 | } |
| 374 | animate().scaleX(scale).scaleY(scale).rotation(-orientation).setDuration(180).start(); |
| 375 | } |
| 376 |
| 377 | /** |
| 378 | * Set if the video stream should be mirrored or not. |
| 379 | */ |
| 380 | public void setMirror(final boolean mirror) { |
| 381 | if (this.mirror != mirror) { |
| 382 | this.mirror = mirror; |
| 383 | if (rotateTextureWitchScreen) { |
| 384 | onRotationChanged(); |
| 385 | } else { |
| 386 | eglRenderer.setMirror(mirror); |
| 387 | } |
| 388 | updateSurfaceSize(); |
| 389 | requestLayout(); |
| 390 | } |
| 391 | } |
| 392 |
| 393 | /** |
| 394 | * Set how the video will fill the allowed layout area. |
| 395 | */ |
| 396 | public void setScalingType(RendererCommon.ScalingType scalingType) { |
| 397 | ThreadUtils.checkIsOnMainThread(); |
| 398 | videoLayoutMeasure.setScalingType(scalingType); |
| 399 | requestLayout(); |
| 400 | } |
| 401 |
| 402 | public void setScalingType(RendererCommon.ScalingType scalingTypeMatchOrientation, |
| 403 | RendererCommon.ScalingType scalingTypeMismatchOrientation) { |
| 404 | ThreadUtils.checkIsOnMainThread(); |
| 405 | videoLayoutMeasure.setScalingType(scalingTypeMatchOrientation, scalingTypeMismatchOrientation); |
| 406 | requestLayout(); |
| 407 | } |
| 408 |
| 409 | /** |
| 410 | * Limit render framerate. |
| 411 | * |
| 412 | * @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps |
| 413 | * reduction. |
| 414 | */ |
| 415 | public void setFpsReduction(float fps) { |
| 416 | eglRenderer.setFpsReduction(fps); |
| 417 | } |
| 418 |
| 419 | public void disableFpsReduction() { |
| 420 | eglRenderer.disableFpsReduction(); |
| 421 | } |
| 422 |
| 423 | public void pauseVideo() { |
| 424 | eglRenderer.pauseVideo(); |
| 425 | } |
| 426 |
| 427 | // VideoSink interface. |
| 428 | @Override |
| 429 | public void onFrame(VideoFrame frame) { |
| 430 | eglRenderer.onFrame(frame); |
| 431 | } |
| 432 |
| 433 | // View layout interface. |
| 434 | @Override |
| 435 | protected void onMeasure(int widthSpec, int heightSpec) { |
| 436 | ThreadUtils.checkIsOnMainThread(); |
| 437 | if (!isCamera && rotateTextureWitchScreen) { |
| 438 | updateVideoSizes(); |
| 439 | } |
| 440 | Point size; |
| 441 | if (maxTextureSize > 0) { |
| 442 | size = videoLayoutMeasure.measure(isCamera, MeasureSpec.makeMeasureSpec(Math.min(maxTextureSize, MeasureSpec.getSize(widthSpec)), MeasureSpec.getMode(widthSpec)), MeasureSpec.makeMeasureSpec(Math.min(maxTextureSize, MeasureSpec.getSize(heightSpec)), MeasureSpec.getMode(heightSpec)), rotatedFrameWidth, rotatedFrameHeight); |
| 443 | } else { |
| 444 | size = videoLayoutMeasure.measure(isCamera, widthSpec, heightSpec, rotatedFrameWidth, rotatedFrameHeight); |
| 445 | } |
| 446 | setMeasuredDimension(size.x, size.y); |
| 447 | // logD("onMeasure(). New size: " + size.x + "x" + size.y); |
| 448 | if (rotatedFrameWidth != 0 && rotatedFrameHeight != 0) { |
| 449 | eglRenderer.setLayoutAspectRatio(getMeasuredWidth() / (float) getMeasuredHeight()); |
| 450 | } |
| 451 | updateSurfaceSize(); |
| 452 | } |
| 453 |
| 454 | private void updateSurfaceSize() { |
| 455 | ThreadUtils.checkIsOnMainThread(); |
| 456 | if (enableFixedSize && rotatedFrameWidth != 0 && rotatedFrameHeight != 0 && getWidth() != 0 |
| 457 | && getHeight() != 0) { |
| 458 | final float layoutAspectRatio = getWidth() / (float) getHeight(); |
| 459 | final float frameAspectRatio = rotatedFrameWidth / (float) rotatedFrameHeight; |
| 460 | final int drawnFrameWidth; |
| 461 | final int drawnFrameHeight; |
| 462 | if (frameAspectRatio > layoutAspectRatio) { |
| 463 | drawnFrameWidth = (int) (rotatedFrameHeight * layoutAspectRatio); |
| 464 | drawnFrameHeight = rotatedFrameHeight; |
| 465 | } else { |
| 466 | drawnFrameWidth = rotatedFrameWidth; |
| 467 | drawnFrameHeight = (int) (rotatedFrameHeight / layoutAspectRatio); |
| 468 | } |
| 469 | // Aspect ratio of the drawn frame and the view is the same. |
| 470 | final int width = Math.min(getWidth(), drawnFrameWidth); |
| 471 | final int height = Math.min(getHeight(), drawnFrameHeight); |
| 472 | logD("updateSurfaceSize. Layout size: " + getWidth() + "x" + getHeight() + ", frame size: " |
| 473 | + rotatedFrameWidth + "x" + rotatedFrameHeight + ", requested surface size: " + width |
| 474 | + "x" + height + ", old surface size: " + surfaceWidth + "x" + surfaceHeight); |
| 475 | if (width != surfaceWidth || height != surfaceHeight) { |
| 476 | surfaceWidth = width; |
| 477 | surfaceHeight = height; |
| 478 | //getHolder().setFixedSize(width, height); |
| 479 | } |
| 480 | } else { |
| 481 | surfaceWidth = surfaceHeight = 0; |
| 482 | //getHolder().setSizeFromLayout(); |
| 483 | } |
| 484 | } |
| 485 |
| 486 | @Override |
| 487 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { |
| 488 | ThreadUtils.checkIsOnMainThread(); |
| 489 | surfaceWidth = surfaceHeight = 0; |
| 490 | updateSurfaceSize(); |
| 491 | eglRenderer.onSurfaceTextureAvailable(surface, width, height); |
| 492 | } |
| 493 |
| 494 | @Override |
| 495 | public void onSurfaceTextureSizeChanged(SurfaceTexture surface, final int width, final int height) { |
| 496 | surfaceWidth = width; |
| 497 | surfaceHeight = height; |
| 498 | eglRenderer.onSurfaceTextureSizeChanged(surface, width, height); |
| 499 | } |
| 500 |
| 501 | @Override |
| 502 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { |
| 503 | if (parentSink instanceof VoIPService.ProxyVideoSink) { |
| 504 | VoIPService.ProxyVideoSink proxyVideoSink = (VoIPService.ProxyVideoSink) parentSink; |
| 505 | proxyVideoSink.removeTarget(this); |
| 506 | proxyVideoSink.removeBackground(this); |
| 507 | } |
| 508 | eglRenderer.onSurfaceTextureDestroyed(surfaceTexture); |
| 509 | return true; |
| 510 | } |
| 511 |
| 512 | @Override |
| 513 | public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { |
| 514 | eglRenderer.onSurfaceTextureUpdated(surfaceTexture); |
| 515 | } |
| 516 |
| 517 | private String getResourceName() { |
| 518 | try { |
| 519 | return getResources().getResourceEntryName(getId()); |
| 520 | } catch (Resources.NotFoundException e) { |
| 521 | return ""; |
| 522 | } |
| 523 | } |
| 524 |
| 525 | /** |
| 526 | * Post a task to clear the SurfaceView to a transparent uniform color. |
| 527 | */ |
| 528 | public void clearImage() { |
| 529 | eglRenderer.clearImage(); |
| 530 | eglRenderer.isFirstFrameRendered = false; |
| 531 | } |
| 532 |
| 533 | @Override |
| 534 | public void setParentSink(VideoSink parent) { |
| 535 | parentSink = parent; |
| 536 | } |
| 537 |
| 538 | @Override |
| 539 | public void onFirstFrameRendered() { |
| 540 | if (rendererEvents != null) { |
| 541 | rendererEvents.onFirstFrameRendered(); |
| 542 | } |
| 543 | } |
| 544 |
| 545 | public boolean isFirstFrameRendered() { |
| 546 | return eglRenderer.isFirstFrameRendered; |
| 547 | } |
| 548 |
| 549 | int textureRotation; |
| 550 | @Override |
| 551 | public void onFrameResolutionChanged(int videoWidth, int videoHeight, int rotation) { |
| 552 | if (rendererEvents != null) { |
| 553 | rendererEvents.onFrameResolutionChanged(videoWidth, videoHeight, rotation); |
| 554 | } |
| 555 | textureRotation = rotation; |
| 556 | int rotatedWidth, rotatedHeight; |
| 557 |
| 558 | if (rotateTextureWitchScreen) { |
| 559 | if (isCamera) { |
| 560 | onRotationChanged(); |
| 561 | } |
| 562 | if (useCameraRotation) { |
| 563 | rotatedWidth = screenRotation == 0 ? videoHeight : videoWidth; |
| 564 | rotatedHeight = screenRotation == 0 ? videoWidth : videoHeight; |
| 565 | } else { |
| 566 | rotatedWidth = textureRotation == 0 || textureRotation == 180 || textureRotation == -180 ? videoWidth : videoHeight; |
| 567 | rotatedHeight = textureRotation == 0 || textureRotation == 180 || textureRotation == -180 ? videoHeight : videoWidth; |
| 568 | } |
| 569 | } else { |
| 570 | if (isCamera) { |
| 571 | eglRenderer.setRotation(-OrientationHelper.cameraRotation); |
| 572 | } |
| 573 | rotation -= OrientationHelper.cameraOrientation; |
| 574 | rotatedWidth = rotation == 0 || rotation == 180 || rotation == -180 ? videoWidth : videoHeight; |
| 575 | rotatedHeight = rotation == 0 || rotation == 180 || rotation == -180? videoHeight : videoWidth; |
| 576 | } |
| 577 | // run immediately if possible for ui thread tests |
| 578 | synchronized (eglRenderer.layoutLock) { |
| 579 | if (updateScreenRunnable != null) { |
| 580 | AndroidUtilities.cancelRunOnUIThread(updateScreenRunnable); |
| 581 | } |
| 582 | postOrRun(updateScreenRunnable = () -> { |
| 583 | updateScreenRunnable = null; |
| 584 | this.videoWidth = videoWidth; |
| 585 | this.videoHeight = videoHeight; |
| 586 |
| 587 | rotatedFrameWidth = rotatedWidth; |
| 588 | rotatedFrameHeight = rotatedHeight; |
| 589 |
| 590 | updateSurfaceSize(); |
| 591 | requestLayout(); |
| 592 | }); |
| 593 | } |
| 594 | } |
| 595 |
| 596 | public void setScreenRotation(int screenRotation) { |
| 597 | this.screenRotation = screenRotation; |
| 598 | onRotationChanged(); |
| 599 | updateVideoSizes(); |
| 600 | } |
| 601 |
| 602 | private void updateVideoSizes() { |
| 603 | if (videoHeight != 0 && videoWidth != 0) { |
| 604 | int rotatedWidth; |
| 605 | int rotatedHeight; |
| 606 | if (rotateTextureWitchScreen) { |
| 607 | if (useCameraRotation) { |
| 608 | rotatedWidth = screenRotation == 0 ? videoHeight : videoWidth; |
| 609 | rotatedHeight = screenRotation == 0 ? videoWidth : videoHeight; |
| 610 | } else { |
| 611 | rotatedWidth = textureRotation == 0 || textureRotation == 180 || textureRotation == -180 ? videoWidth : videoHeight; |
| 612 | rotatedHeight = textureRotation == 0 || textureRotation == 180 || textureRotation == -180 ? videoHeight : videoWidth; |
| 613 | } |
| 614 | } else { |
| 615 | int rotation = textureRotation; |
| 616 | rotation -= OrientationHelper.cameraOrientation; |
| 617 | rotatedWidth = rotation == 0 || rotation == 180 || rotation == -180 ? videoWidth : videoHeight; |
| 618 | rotatedHeight = rotation == 0 || rotation == 180 || rotation == -180 ? videoHeight : videoWidth; |
| 619 |
| 620 | } |
| 621 | if (rotatedFrameWidth != rotatedWidth || rotatedFrameHeight != rotatedHeight) { |
| 622 | synchronized (eglRenderer.layoutLock) { |
| 623 | if (updateScreenRunnable != null) { |
| 624 | AndroidUtilities.cancelRunOnUIThread(updateScreenRunnable); |
| 625 | } |
| 626 | postOrRun(updateScreenRunnable = () -> { |
| 627 | updateScreenRunnable = null; |
| 628 |
| 629 | rotatedFrameWidth = rotatedWidth; |
| 630 | rotatedFrameHeight = rotatedHeight; |
| 631 |
| 632 | updateSurfaceSize(); |
| 633 | requestLayout(); |
| 634 | }); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 |
| 640 | public void setRotateTextureWitchScreen(boolean rotateTextureWitchScreen) { |
| 641 | if (this.rotateTextureWitchScreen != rotateTextureWitchScreen) { |
| 642 | this.rotateTextureWitchScreen = rotateTextureWitchScreen; |
| 643 | requestLayout(); |
| 644 | } |
| 645 | } |
| 646 |
| 647 | boolean useCameraRotation; |
| 648 |
| 649 | public void setUseCameraRotation(boolean useCameraRotation) { |
| 650 | if (this.useCameraRotation != useCameraRotation) { |
| 651 | this.useCameraRotation = useCameraRotation; |
| 652 | onRotationChanged(); |
| 653 | updateVideoSizes(); |
| 654 | } |
| 655 | } |
| 656 | private void onRotationChanged() { |
| 657 | int rotation = useCameraRotation ? OrientationHelper.cameraOrientation : 0; |
| 658 | if (mirror) { |
| 659 | rotation = 360 - rotation; |
| 660 | } |
| 661 | int r = -rotation; |
| 662 | if (useCameraRotation) { |
| 663 | if (screenRotation == 1) { |
| 664 | r += mirror ? 90 : -90; |
| 665 | } else if (screenRotation == 3) { |
| 666 | r += mirror ? 270 : -270; |
| 667 | } |
| 668 | } |
| 669 |
| 670 | eglRenderer.setRotation(r); |
| 671 | eglRenderer.setMirror(mirror); |
| 672 | } |
| 673 |
| 674 | private void postOrRun(Runnable r) { |
| 675 | if (Thread.currentThread() == Looper.getMainLooper().getThread()) { |
| 676 | r.run(); |
| 677 | } else { |
| 678 | AndroidUtilities.runOnUIThread(r); |
| 679 | } |
| 680 | } |
| 681 |
| 682 | private void logD(String string) { |
| 683 | Logging.d(TAG, resourceName + ": " + string); |
| 684 | } |
| 685 |
| 686 | public void createBackgroundSurface(SurfaceTexture bluSurfaceTexturerRenderer) { |
| 687 | eglRenderer.createBackgroundSurface(bluSurfaceTexturerRenderer); |
| 688 | } |
| 689 |
| 690 | public void setMaxTextureSize(int maxTextureSize) { |
| 691 | this.maxTextureSize = maxTextureSize; |
| 692 | } |
| 693 | } |
| 694 |