|
| 1 | +/* |
| 2 | + * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Use of this source code is governed by a BSD-style license |
| 5 | + * that can be found in the LICENSE file in the root of the source |
| 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 | +/** |
| 14 | + * Display the video stream on a Surface. |
| 15 | + * renderFrame() is asynchronous to avoid blocking the calling thread. |
| 16 | + * This class is thread safe and handles access from potentially three different threads: |
| 17 | + * Interaction from the main app in init, release and setMirror. |
| 18 | + * Interaction from C++ rtc::VideoSinkInterface in renderFrame. |
| 19 | + * Interaction from SurfaceHolder lifecycle in surfaceCreated, surfaceChanged, and surfaceDestroyed. |
| 20 | + */ |
| 21 | +public class TextureEglRenderer extends EglRenderer { |
| 22 | + private static final String TAG = "TextureEglRenderer"; |
| 23 | + |
| 24 | + // Callback for reporting renderer events. Read-only after initilization so no lock required. |
| 25 | + private RendererCommon.RendererEvents rendererEvents; |
| 26 | + |
| 27 | + private final Object layoutLock = new Object(); |
| 28 | + private boolean isRenderingPaused; |
| 29 | + private boolean isFirstFrameRendered; |
| 30 | + private int rotatedFrameWidth; |
| 31 | + private int rotatedFrameHeight; |
| 32 | + private int frameRotation; |
| 33 | + |
| 34 | + /** |
| 35 | + * In order to render something, you must first call init(). |
| 36 | + */ |
| 37 | + public TextureEglRenderer(String name) { |
| 38 | + super(name); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Initialize this class, sharing resources with |sharedContext|. The custom |drawer| will be used |
| 43 | + * for drawing frames on the EGLSurface. This class is responsible for calling release() on |
| 44 | + * |drawer|. It is allowed to call init() to reinitialize the renderer after a previous |
| 45 | + * init()/release() cycle. |
| 46 | + */ |
| 47 | + public void init(final EglBase.Context sharedContext, |
| 48 | + RendererCommon.RendererEvents rendererEvents, final int[] configAttributes, |
| 49 | + RendererCommon.GlDrawer drawer) { |
| 50 | + ThreadUtils.checkIsOnMainThread(); |
| 51 | + this.rendererEvents = rendererEvents; |
| 52 | + synchronized (layoutLock) { |
| 53 | + isFirstFrameRendered = false; |
| 54 | + rotatedFrameWidth = 0; |
| 55 | + rotatedFrameHeight = 0; |
| 56 | + frameRotation = 0; |
| 57 | + } |
| 58 | + super.init(sharedContext, configAttributes, drawer); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void init(final EglBase.Context sharedContext, final int[] configAttributes, |
| 63 | + RendererCommon.GlDrawer drawer) { |
| 64 | + init(sharedContext, null /* rendererEvents */, configAttributes, drawer); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Limit render framerate. |
| 69 | + * |
| 70 | + * @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps |
| 71 | + * reduction. |
| 72 | + */ |
| 73 | + @Override |
| 74 | + public void setFpsReduction(float fps) { |
| 75 | + synchronized (layoutLock) { |
| 76 | + isRenderingPaused = fps == 0f; |
| 77 | + } |
| 78 | + super.setFpsReduction(fps); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void disableFpsReduction() { |
| 83 | + synchronized (layoutLock) { |
| 84 | + isRenderingPaused = false; |
| 85 | + } |
| 86 | + super.disableFpsReduction(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void pauseVideo() { |
| 91 | + synchronized (layoutLock) { |
| 92 | + isRenderingPaused = true; |
| 93 | + } |
| 94 | + super.pauseVideo(); |
| 95 | + } |
| 96 | + |
| 97 | + // VideoSink interface. |
| 98 | + @Override |
| 99 | + public void onFrame(VideoFrame frame) { |
| 100 | + updateFrameDimensionsAndReportEvents(frame); |
| 101 | + super.onFrame(frame); |
| 102 | + } |
| 103 | + |
| 104 | + // Update frame dimensions and report any changes to |rendererEvents|. |
| 105 | + private void updateFrameDimensionsAndReportEvents(VideoFrame frame) { |
| 106 | + synchronized (layoutLock) { |
| 107 | + if (isRenderingPaused) { |
| 108 | + return; |
| 109 | + } |
| 110 | + if (!isFirstFrameRendered) { |
| 111 | + isFirstFrameRendered = true; |
| 112 | + logD("Reporting first rendered frame."); |
| 113 | + if (rendererEvents != null) { |
| 114 | + rendererEvents.onFirstFrameRendered(); |
| 115 | + } |
| 116 | + } |
| 117 | + if (rotatedFrameWidth != frame.getRotatedWidth() |
| 118 | + || rotatedFrameHeight != frame.getRotatedHeight() |
| 119 | + || frameRotation != frame.getRotation()) { |
| 120 | + logD("Reporting frame resolution changed to " + frame.getBuffer().getWidth() + "x" |
| 121 | + + frame.getBuffer().getHeight() + " with rotation " + frame.getRotation()); |
| 122 | + if (rendererEvents != null) { |
| 123 | + rendererEvents.onFrameResolutionChanged( |
| 124 | + frame.getBuffer().getWidth(), frame.getBuffer().getHeight(), frame.getRotation()); |
| 125 | + } |
| 126 | + rotatedFrameWidth = frame.getRotatedWidth(); |
| 127 | + rotatedFrameHeight = frame.getRotatedHeight(); |
| 128 | + frameRotation = frame.getRotation(); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private void logD(String string) { |
| 134 | + Logging.d(TAG, name + ": " + string); |
| 135 | + } |
| 136 | +} |
0 commit comments