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