Xiaolei Yu | 149533a | 2017-11-03 07:55:01 +0800 | [diff] [blame] | 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 | import android.view.SurfaceHolder; |
| 14 | import java.util.concurrent.CountDownLatch; |
| 15 | |
| 16 | /** |
| 17 | * Display the video stream on a Surface. |
| 18 | * renderFrame() is asynchronous to avoid blocking the calling thread. |
| 19 | * This class is thread safe and handles access from potentially three different threads: |
| 20 | * Interaction from the main app in init, release and setMirror. |
| 21 | * Interaction from C++ rtc::VideoSinkInterface in renderFrame. |
| 22 | * Interaction from SurfaceHolder lifecycle in surfaceCreated, surfaceChanged, and surfaceDestroyed. |
| 23 | */ |
| 24 | public class SurfaceEglRenderer extends EglRenderer implements SurfaceHolder.Callback { |
| 25 | private static final String TAG = "SurfaceEglRenderer"; |
| 26 | |
| 27 | // Callback for reporting renderer events. Read-only after initilization so no lock required. |
| 28 | private RendererCommon.RendererEvents rendererEvents; |
| 29 | |
| 30 | private final Object layoutLock = new Object(); |
| 31 | private boolean isRenderingPaused = false; |
| 32 | private boolean isFirstFrameRendered; |
| 33 | private int rotatedFrameWidth; |
| 34 | private int rotatedFrameHeight; |
| 35 | private int frameRotation; |
| 36 | |
| 37 | /** |
| 38 | * In order to render something, you must first call init(). |
| 39 | */ |
| 40 | public SurfaceEglRenderer(String name) { |
| 41 | super(name); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Initialize this class, sharing resources with |sharedContext|. The custom |drawer| will be used |
| 46 | * for drawing frames on the EGLSurface. This class is responsible for calling release() on |
| 47 | * |drawer|. It is allowed to call init() to reinitialize the renderer after a previous |
| 48 | * init()/release() cycle. |
| 49 | */ |
| 50 | public void init(final EglBase.Context sharedContext, |
| 51 | RendererCommon.RendererEvents rendererEvents, final int[] configAttributes, |
| 52 | RendererCommon.GlDrawer drawer) { |
| 53 | ThreadUtils.checkIsOnMainThread(); |
| 54 | this.rendererEvents = rendererEvents; |
| 55 | synchronized (layoutLock) { |
| 56 | isFirstFrameRendered = false; |
| 57 | rotatedFrameWidth = 0; |
| 58 | rotatedFrameHeight = 0; |
| 59 | frameRotation = 0; |
| 60 | } |
| 61 | super.init(sharedContext, configAttributes, drawer); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void init(final EglBase.Context sharedContext, final int[] configAttributes, |
| 66 | RendererCommon.GlDrawer drawer) { |
| 67 | init(sharedContext, null /* rendererEvents */, configAttributes, drawer); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Limit render framerate. |
| 72 | * |
| 73 | * @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps |
| 74 | * reduction. |
| 75 | */ |
| 76 | @Override |
| 77 | public void setFpsReduction(float fps) { |
| 78 | synchronized (layoutLock) { |
| 79 | isRenderingPaused = fps == 0f; |
| 80 | } |
| 81 | super.setFpsReduction(fps); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void disableFpsReduction() { |
| 86 | synchronized (layoutLock) { |
| 87 | isRenderingPaused = false; |
| 88 | } |
| 89 | super.disableFpsReduction(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void pauseVideo() { |
| 94 | synchronized (layoutLock) { |
| 95 | isRenderingPaused = true; |
| 96 | } |
| 97 | super.pauseVideo(); |
| 98 | } |
| 99 | |
| 100 | // VideoRenderer.Callbacks interface. |
| 101 | @Override |
| 102 | public void renderFrame(VideoRenderer.I420Frame frame) { |
| 103 | updateFrameDimensionsAndReportEvents(frame); |
| 104 | super.renderFrame(frame); |
| 105 | } |
| 106 | |
| 107 | // VideoSink interface. |
| 108 | @Override |
| 109 | public void onFrame(VideoFrame frame) { |
| 110 | updateFrameDimensionsAndReportEvents(frame); |
| 111 | super.onFrame(frame); |
| 112 | } |
| 113 | |
| 114 | // SurfaceHolder.Callback interface. |
| 115 | @Override |
| 116 | public void surfaceCreated(final SurfaceHolder holder) { |
| 117 | ThreadUtils.checkIsOnMainThread(); |
| 118 | createEglSurface(holder.getSurface()); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void surfaceDestroyed(SurfaceHolder holder) { |
| 123 | ThreadUtils.checkIsOnMainThread(); |
| 124 | final CountDownLatch completionLatch = new CountDownLatch(1); |
| 125 | releaseEglSurface(completionLatch::countDown); |
| 126 | ThreadUtils.awaitUninterruptibly(completionLatch); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
| 131 | ThreadUtils.checkIsOnMainThread(); |
| 132 | logD("surfaceChanged: format: " + format + " size: " + width + "x" + height); |
| 133 | } |
| 134 | |
| 135 | // Update frame dimensions and report any changes to |rendererEvents|. |
| 136 | private void updateFrameDimensionsAndReportEvents(VideoRenderer.I420Frame frame) { |
| 137 | synchronized (layoutLock) { |
| 138 | if (isRenderingPaused) { |
| 139 | return; |
| 140 | } |
| 141 | if (!isFirstFrameRendered) { |
| 142 | isFirstFrameRendered = true; |
| 143 | logD("Reporting first rendered frame."); |
| 144 | if (rendererEvents != null) { |
| 145 | rendererEvents.onFirstFrameRendered(); |
| 146 | } |
| 147 | } |
| 148 | if (rotatedFrameWidth != frame.rotatedWidth() || rotatedFrameHeight != frame.rotatedHeight() |
| 149 | || frameRotation != frame.rotationDegree) { |
| 150 | logD("Reporting frame resolution changed to " + frame.width + "x" + frame.height |
| 151 | + " with rotation " + frame.rotationDegree); |
| 152 | if (rendererEvents != null) { |
| 153 | rendererEvents.onFrameResolutionChanged(frame.width, frame.height, frame.rotationDegree); |
| 154 | } |
| 155 | rotatedFrameWidth = frame.rotatedWidth(); |
| 156 | rotatedFrameHeight = frame.rotatedHeight(); |
| 157 | frameRotation = frame.rotationDegree; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Update frame dimensions and report any changes to |rendererEvents|. |
| 163 | private void updateFrameDimensionsAndReportEvents(VideoFrame frame) { |
| 164 | synchronized (layoutLock) { |
| 165 | if (isRenderingPaused) { |
| 166 | return; |
| 167 | } |
| 168 | if (!isFirstFrameRendered) { |
| 169 | isFirstFrameRendered = true; |
| 170 | logD("Reporting first rendered frame."); |
| 171 | if (rendererEvents != null) { |
| 172 | rendererEvents.onFirstFrameRendered(); |
| 173 | } |
| 174 | } |
| 175 | if (rotatedFrameWidth != frame.getRotatedWidth() |
| 176 | || rotatedFrameHeight != frame.getRotatedHeight() |
| 177 | || frameRotation != frame.getRotation()) { |
| 178 | logD("Reporting frame resolution changed to " + frame.getBuffer().getWidth() + "x" |
| 179 | + frame.getBuffer().getHeight() + " with rotation " + frame.getRotation()); |
| 180 | if (rendererEvents != null) { |
| 181 | rendererEvents.onFrameResolutionChanged( |
| 182 | frame.getBuffer().getWidth(), frame.getBuffer().getHeight(), frame.getRotation()); |
| 183 | } |
| 184 | rotatedFrameWidth = frame.getRotatedWidth(); |
| 185 | rotatedFrameHeight = frame.getRotatedHeight(); |
| 186 | frameRotation = frame.getRotation(); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | private void logD(String string) { |
| 192 | Logging.d(TAG, name + ": " + string); |
| 193 | } |
| 194 | } |