magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 13 | import android.graphics.Bitmap; |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 14 | import android.graphics.Matrix; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 15 | import android.graphics.SurfaceTexture; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 16 | import android.opengl.GLES20; |
| 17 | import android.os.Handler; |
| 18 | import android.os.HandlerThread; |
| 19 | import android.os.Looper; |
Sami Kalliomäki | 0d26c99 | 2018-10-19 12:53:21 +0200 | [diff] [blame] | 20 | import android.os.Message; |
Artem Titarenko | 69540f4 | 2018-12-10 12:30:46 +0100 | [diff] [blame] | 21 | import android.support.annotation.Nullable; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 22 | import android.view.Surface; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 23 | import java.nio.ByteBuffer; |
Sami Kalliomäki | 1659e97 | 2018-06-04 14:07:58 +0200 | [diff] [blame] | 24 | import java.text.DecimalFormat; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.Iterator; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 27 | import java.util.concurrent.CountDownLatch; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | |
| 30 | /** |
Magnus Jedvert | 431f14e | 2018-06-09 19:41:58 +0200 | [diff] [blame] | 31 | * Implements VideoSink by displaying the video stream on an EGL Surface. This class is intended to |
| 32 | * be used as a helper class for rendering on SurfaceViews and TextureViews. |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 33 | */ |
Magnus Jedvert | 431f14e | 2018-06-09 19:41:58 +0200 | [diff] [blame] | 34 | public class EglRenderer implements VideoSink { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 35 | private static final String TAG = "EglRenderer"; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 36 | private static final long LOG_INTERVAL_SEC = 4; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 37 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 38 | public interface FrameListener { void onFrame(Bitmap frame); } |
| 39 | |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 40 | private static class FrameListenerAndParams { |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 41 | public final FrameListener listener; |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 42 | public final float scale; |
| 43 | public final RendererCommon.GlDrawer drawer; |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 44 | public final boolean applyFpsReduction; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 45 | |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 46 | public FrameListenerAndParams(FrameListener listener, float scale, |
| 47 | RendererCommon.GlDrawer drawer, boolean applyFpsReduction) { |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 48 | this.listener = listener; |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 49 | this.scale = scale; |
| 50 | this.drawer = drawer; |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 51 | this.applyFpsReduction = applyFpsReduction; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 55 | private class EglSurfaceCreation implements Runnable { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 56 | private Object surface; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 57 | |
Mirko Bonadei | 12251b6 | 2017-11-05 19:35:31 -0800 | [diff] [blame] | 58 | // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression. |
| 59 | @SuppressWarnings("NoSynchronizedMethodCheck") |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 60 | public synchronized void setSurface(Object surface) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 61 | this.surface = surface; |
| 62 | } |
| 63 | |
| 64 | @Override |
Mirko Bonadei | 12251b6 | 2017-11-05 19:35:31 -0800 | [diff] [blame] | 65 | // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression. |
| 66 | @SuppressWarnings("NoSynchronizedMethodCheck") |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 67 | public synchronized void run() { |
| 68 | if (surface != null && eglBase != null && !eglBase.hasSurface()) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 69 | if (surface instanceof Surface) { |
| 70 | eglBase.createSurface((Surface) surface); |
| 71 | } else if (surface instanceof SurfaceTexture) { |
| 72 | eglBase.createSurface((SurfaceTexture) surface); |
| 73 | } else { |
| 74 | throw new IllegalStateException("Invalid surface: " + surface); |
| 75 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 76 | eglBase.makeCurrent(); |
| 77 | // Necessary for YUV frames with odd width. |
| 78 | GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Sami Kalliomäki | 0d26c99 | 2018-10-19 12:53:21 +0200 | [diff] [blame] | 83 | /** |
| 84 | * Handler that triggers a callback when an uncaught exception happens when handling a message. |
| 85 | */ |
| 86 | private static class HandlerWithExceptionCallback extends Handler { |
| 87 | private final Runnable exceptionCallback; |
| 88 | |
| 89 | public HandlerWithExceptionCallback(Looper looper, Runnable exceptionCallback) { |
| 90 | super(looper); |
| 91 | this.exceptionCallback = exceptionCallback; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void dispatchMessage(Message msg) { |
| 96 | try { |
| 97 | super.dispatchMessage(msg); |
| 98 | } catch (Exception e) { |
| 99 | Logging.e(TAG, "Exception on EglRenderer thread", e); |
| 100 | exceptionCallback.run(); |
| 101 | throw e; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Xiaolei Yu | 149533a | 2017-11-03 07:55:01 +0800 | [diff] [blame] | 106 | protected final String name; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 107 | |
| 108 | // |renderThreadHandler| is a handler for communicating with |renderThread|, and is synchronized |
| 109 | // on |handlerLock|. |
| 110 | private final Object handlerLock = new Object(); |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 111 | @Nullable private Handler renderThreadHandler; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 112 | |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 113 | private final ArrayList<FrameListenerAndParams> frameListeners = new ArrayList<>(); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 114 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 115 | // Variables for fps reduction. |
| 116 | private final Object fpsReductionLock = new Object(); |
| 117 | // Time for when next frame should be rendered. |
| 118 | private long nextFrameTimeNs; |
| 119 | // Minimum duration between frames when fps reduction is active, or -1 if video is completely |
| 120 | // paused. |
| 121 | private long minRenderPeriodNs; |
| 122 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 123 | // EGL and GL resources for drawing YUV/OES textures. After initilization, these are only accessed |
| 124 | // from the render thread. |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 125 | @Nullable private EglBase eglBase; |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame^] | 126 | private final VideoFrameDrawer frameDrawer; |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 127 | @Nullable private RendererCommon.GlDrawer drawer; |
Magnus Jedvert | 361dbc1 | 2018-11-06 11:32:46 +0100 | [diff] [blame] | 128 | private boolean usePresentationTimeStamp; |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 129 | private final Matrix drawMatrix = new Matrix(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 130 | |
| 131 | // Pending frame to render. Serves as a queue with size 1. Synchronized on |frameLock|. |
| 132 | private final Object frameLock = new Object(); |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 133 | @Nullable private VideoFrame pendingFrame; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 134 | |
| 135 | // These variables are synchronized on |layoutLock|. |
| 136 | private final Object layoutLock = new Object(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 137 | private float layoutAspectRatio; |
| 138 | // If true, mirrors the video stream horizontally. |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 139 | private boolean mirrorHorizontally; |
| 140 | // If true, mirrors the video stream vertically. |
| 141 | private boolean mirrorVertically; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 142 | |
| 143 | // These variables are synchronized on |statisticsLock|. |
| 144 | private final Object statisticsLock = new Object(); |
| 145 | // Total number of video frames received in renderFrame() call. |
| 146 | private int framesReceived; |
| 147 | // Number of video frames dropped by renderFrame() because previous frame has not been rendered |
| 148 | // yet. |
| 149 | private int framesDropped; |
| 150 | // Number of rendered video frames. |
| 151 | private int framesRendered; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 152 | // Start time for counting these statistics, or 0 if we haven't started measuring yet. |
| 153 | private long statisticsStartTimeNs; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 154 | // Time in ns spent in renderFrameOnRenderThread() function. |
| 155 | private long renderTimeNs; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 156 | // Time in ns spent by the render thread in the swapBuffers() function. |
| 157 | private long renderSwapBufferTimeNs; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 158 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 159 | // Used for bitmap capturing. |
Magnus Jedvert | 2ed62b3 | 2018-04-11 14:25:14 +0200 | [diff] [blame] | 160 | private final GlTextureFrameBuffer bitmapTextureFramebuffer = |
| 161 | new GlTextureFrameBuffer(GLES20.GL_RGBA); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 162 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 163 | private final Runnable logStatisticsRunnable = new Runnable() { |
| 164 | @Override |
| 165 | public void run() { |
| 166 | logStatistics(); |
| 167 | synchronized (handlerLock) { |
| 168 | if (renderThreadHandler != null) { |
| 169 | renderThreadHandler.removeCallbacks(logStatisticsRunnable); |
| 170 | renderThreadHandler.postDelayed( |
| 171 | logStatisticsRunnable, TimeUnit.SECONDS.toMillis(LOG_INTERVAL_SEC)); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | }; |
| 176 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 177 | private final EglSurfaceCreation eglSurfaceCreationRunnable = new EglSurfaceCreation(); |
| 178 | |
| 179 | /** |
| 180 | * Standard constructor. The name will be used for the render thread name and included when |
| 181 | * logging. In order to render something, you must first call init() and createEglSurface. |
| 182 | */ |
| 183 | public EglRenderer(String name) { |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame^] | 184 | this(name, new VideoFrameDrawer()); |
| 185 | } |
| 186 | |
| 187 | public EglRenderer(String name, VideoFrameDrawer videoFrameDrawer) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 188 | this.name = name; |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame^] | 189 | this.frameDrawer = videoFrameDrawer; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Initialize this class, sharing resources with |sharedContext|. The custom |drawer| will be used |
| 194 | * for drawing frames on the EGLSurface. This class is responsible for calling release() on |
| 195 | * |drawer|. It is allowed to call init() to reinitialize the renderer after a previous |
Magnus Jedvert | 361dbc1 | 2018-11-06 11:32:46 +0100 | [diff] [blame] | 196 | * init()/release() cycle. If usePresentationTimeStamp is true, eglPresentationTimeANDROID will be |
| 197 | * set with the frame timestamps, which specifies desired presentation time and might be useful |
| 198 | * for e.g. syncing audio and video. |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 199 | */ |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 200 | public void init(@Nullable final EglBase.Context sharedContext, final int[] configAttributes, |
Magnus Jedvert | 361dbc1 | 2018-11-06 11:32:46 +0100 | [diff] [blame] | 201 | RendererCommon.GlDrawer drawer, boolean usePresentationTimeStamp) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 202 | synchronized (handlerLock) { |
| 203 | if (renderThreadHandler != null) { |
| 204 | throw new IllegalStateException(name + "Already initialized"); |
| 205 | } |
| 206 | logD("Initializing EglRenderer"); |
| 207 | this.drawer = drawer; |
Magnus Jedvert | 361dbc1 | 2018-11-06 11:32:46 +0100 | [diff] [blame] | 208 | this.usePresentationTimeStamp = usePresentationTimeStamp; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 209 | |
| 210 | final HandlerThread renderThread = new HandlerThread(name + "EglRenderer"); |
| 211 | renderThread.start(); |
Sami Kalliomäki | 0d26c99 | 2018-10-19 12:53:21 +0200 | [diff] [blame] | 212 | renderThreadHandler = |
| 213 | new HandlerWithExceptionCallback(renderThread.getLooper(), new Runnable() { |
| 214 | @Override |
| 215 | public void run() { |
| 216 | synchronized (handlerLock) { |
| 217 | renderThreadHandler = null; |
| 218 | } |
| 219 | } |
| 220 | }); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 221 | // Create EGL context on the newly created render thread. It should be possibly to create the |
| 222 | // context on this thread and make it current on the render thread, but this causes failure on |
| 223 | // some Marvel based JB devices. https://bugs.chromium.org/p/webrtc/issues/detail?id=6350. |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 224 | ThreadUtils.invokeAtFrontUninterruptibly(renderThreadHandler, () -> { |
| 225 | // If sharedContext is null, then texture frames are disabled. This is typically for old |
| 226 | // devices that might not be fully spec compliant, so force EGL 1.0 since EGL 1.4 has |
| 227 | // caused trouble on some weird devices. |
| 228 | if (sharedContext == null) { |
| 229 | logD("EglBase10.create context"); |
| 230 | eglBase = EglBase.createEgl10(configAttributes); |
| 231 | } else { |
| 232 | logD("EglBase.create shared context"); |
| 233 | eglBase = EglBase.create(sharedContext, configAttributes); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 234 | } |
| 235 | }); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 236 | renderThreadHandler.post(eglSurfaceCreationRunnable); |
| 237 | final long currentTimeNs = System.nanoTime(); |
| 238 | resetStatistics(currentTimeNs); |
| 239 | renderThreadHandler.postDelayed( |
| 240 | logStatisticsRunnable, TimeUnit.SECONDS.toMillis(LOG_INTERVAL_SEC)); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Magnus Jedvert | 361dbc1 | 2018-11-06 11:32:46 +0100 | [diff] [blame] | 244 | /** |
| 245 | * Same as above with usePresentationTimeStamp set to false. |
| 246 | * |
| 247 | * @see #init(EglBase.Context, int[], RendererCommon.GlDrawer, boolean) |
| 248 | */ |
| 249 | public void init(@Nullable final EglBase.Context sharedContext, final int[] configAttributes, |
| 250 | RendererCommon.GlDrawer drawer) { |
| 251 | init(sharedContext, configAttributes, drawer, /* usePresentationTimeStamp= */ false); |
| 252 | } |
| 253 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 254 | public void createEglSurface(Surface surface) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 255 | createEglSurfaceInternal(surface); |
| 256 | } |
| 257 | |
| 258 | public void createEglSurface(SurfaceTexture surfaceTexture) { |
| 259 | createEglSurfaceInternal(surfaceTexture); |
| 260 | } |
| 261 | |
| 262 | private void createEglSurfaceInternal(Object surface) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 263 | eglSurfaceCreationRunnable.setSurface(surface); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 264 | postToRenderThread(eglSurfaceCreationRunnable); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Block until any pending frame is returned and all GL resources released, even if an interrupt |
| 269 | * occurs. If an interrupt occurs during release(), the interrupt flag will be set. This function |
| 270 | * should be called before the Activity is destroyed and the EGLContext is still valid. If you |
| 271 | * don't call this function, the GL resources might leak. |
| 272 | */ |
| 273 | public void release() { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 274 | logD("Releasing."); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 275 | final CountDownLatch eglCleanupBarrier = new CountDownLatch(1); |
| 276 | synchronized (handlerLock) { |
| 277 | if (renderThreadHandler == null) { |
| 278 | logD("Already released"); |
| 279 | return; |
| 280 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 281 | renderThreadHandler.removeCallbacks(logStatisticsRunnable); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 282 | // Release EGL and GL resources on render thread. |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 283 | renderThreadHandler.postAtFrontOfQueue(() -> { |
Magnus Jedvert | 94c0f26 | 2018-12-12 17:35:28 +0100 | [diff] [blame] | 284 | // Detach current shader program. |
| 285 | GLES20.glUseProgram(/* program= */ 0); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 286 | if (drawer != null) { |
| 287 | drawer.release(); |
| 288 | drawer = null; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 289 | } |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 290 | frameDrawer.release(); |
Magnus Jedvert | 2ed62b3 | 2018-04-11 14:25:14 +0200 | [diff] [blame] | 291 | bitmapTextureFramebuffer.release(); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 292 | if (eglBase != null) { |
| 293 | logD("eglBase detach and release."); |
| 294 | eglBase.detachCurrent(); |
| 295 | eglBase.release(); |
| 296 | eglBase = null; |
| 297 | } |
Sami Kalliomäki | 8ebac24 | 2017-11-08 17:13:13 +0100 | [diff] [blame] | 298 | frameListeners.clear(); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 299 | eglCleanupBarrier.countDown(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 300 | }); |
| 301 | final Looper renderLooper = renderThreadHandler.getLooper(); |
| 302 | // TODO(magjed): Replace this post() with renderLooper.quitSafely() when API support >= 18. |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 303 | renderThreadHandler.post(() -> { |
| 304 | logD("Quitting render thread."); |
| 305 | renderLooper.quit(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 306 | }); |
| 307 | // Don't accept any more frames or messages to the render thread. |
| 308 | renderThreadHandler = null; |
| 309 | } |
| 310 | // Make sure the EGL/GL cleanup posted above is executed. |
| 311 | ThreadUtils.awaitUninterruptibly(eglCleanupBarrier); |
| 312 | synchronized (frameLock) { |
| 313 | if (pendingFrame != null) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 314 | pendingFrame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 315 | pendingFrame = null; |
| 316 | } |
| 317 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 318 | logD("Releasing done."); |
| 319 | } |
| 320 | |
| 321 | /** |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 322 | * Reset the statistics logged in logStatistics(). |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 323 | */ |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 324 | private void resetStatistics(long currentTimeNs) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 325 | synchronized (statisticsLock) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 326 | statisticsStartTimeNs = currentTimeNs; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 327 | framesReceived = 0; |
| 328 | framesDropped = 0; |
| 329 | framesRendered = 0; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 330 | renderTimeNs = 0; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 331 | renderSwapBufferTimeNs = 0; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | public void printStackTrace() { |
| 336 | synchronized (handlerLock) { |
| 337 | final Thread renderThread = |
| 338 | (renderThreadHandler == null) ? null : renderThreadHandler.getLooper().getThread(); |
| 339 | if (renderThread != null) { |
| 340 | final StackTraceElement[] renderStackTrace = renderThread.getStackTrace(); |
| 341 | if (renderStackTrace.length > 0) { |
Magnus Jedvert | 0cc11b4 | 2018-11-27 16:19:55 +0100 | [diff] [blame] | 342 | logW("EglRenderer stack trace:"); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 343 | for (StackTraceElement traceElem : renderStackTrace) { |
Magnus Jedvert | 0cc11b4 | 2018-11-27 16:19:55 +0100 | [diff] [blame] | 344 | logW(traceElem.toString()); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 352 | * Set if the video stream should be mirrored horizontally or not. |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 353 | */ |
| 354 | public void setMirror(final boolean mirror) { |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 355 | logD("setMirrorHorizontally: " + mirror); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 356 | synchronized (layoutLock) { |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 357 | this.mirrorHorizontally = mirror; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Set if the video stream should be mirrored vertically or not. |
| 363 | */ |
| 364 | public void setMirrorVertically(final boolean mirrorVertically) { |
| 365 | logD("setMirrorVertically: " + mirrorVertically); |
| 366 | synchronized (layoutLock) { |
| 367 | this.mirrorVertically = mirrorVertically; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Set layout aspect ratio. This is used to crop frames when rendering to avoid stretched video. |
| 373 | * Set this to 0 to disable cropping. |
| 374 | */ |
| 375 | public void setLayoutAspectRatio(float layoutAspectRatio) { |
| 376 | logD("setLayoutAspectRatio: " + layoutAspectRatio); |
| 377 | synchronized (layoutLock) { |
| 378 | this.layoutAspectRatio = layoutAspectRatio; |
| 379 | } |
| 380 | } |
| 381 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Limit render framerate. |
| 384 | * |
| 385 | * @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps |
| 386 | * reduction. |
| 387 | */ |
| 388 | public void setFpsReduction(float fps) { |
| 389 | logD("setFpsReduction: " + fps); |
| 390 | synchronized (fpsReductionLock) { |
| 391 | final long previousRenderPeriodNs = minRenderPeriodNs; |
| 392 | if (fps <= 0) { |
| 393 | minRenderPeriodNs = Long.MAX_VALUE; |
| 394 | } else { |
| 395 | minRenderPeriodNs = (long) (TimeUnit.SECONDS.toNanos(1) / fps); |
| 396 | } |
| 397 | if (minRenderPeriodNs != previousRenderPeriodNs) { |
| 398 | // Fps reduction changed - reset frame time. |
| 399 | nextFrameTimeNs = System.nanoTime(); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | public void disableFpsReduction() { |
| 405 | setFpsReduction(Float.POSITIVE_INFINITY /* fps */); |
| 406 | } |
| 407 | |
| 408 | public void pauseVideo() { |
| 409 | setFpsReduction(0 /* fps */); |
| 410 | } |
| 411 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 412 | /** |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 413 | * Register a callback to be invoked when a new video frame has been received. This version uses |
| 414 | * the drawer of the EglRenderer that was passed in init. |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 415 | * |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 416 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 417 | * It should be lightweight and must not call removeFrameListener. |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 418 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 419 | * required. |
| 420 | */ |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 421 | public void addFrameListener(final FrameListener listener, final float scale) { |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 422 | addFrameListener(listener, scale, null, false /* applyFpsReduction */); |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Register a callback to be invoked when a new video frame has been received. |
| 427 | * |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 428 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 429 | * It should be lightweight and must not call removeFrameListener. |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 430 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 431 | * required. |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 432 | * @param drawer Custom drawer to use for this frame listener or null to use the default one. |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 433 | */ |
| 434 | public void addFrameListener( |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 435 | final FrameListener listener, final float scale, final RendererCommon.GlDrawer drawerParam) { |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 436 | addFrameListener(listener, scale, drawerParam, false /* applyFpsReduction */); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Register a callback to be invoked when a new video frame has been received. |
| 441 | * |
| 442 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 443 | * It should be lightweight and must not call removeFrameListener. |
| 444 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 445 | * required. |
| 446 | * @param drawer Custom drawer to use for this frame listener or null to use the default one. |
| 447 | * @param applyFpsReduction This callback will not be called for frames that have been dropped by |
| 448 | * FPS reduction. |
| 449 | */ |
| 450 | public void addFrameListener(final FrameListener listener, final float scale, |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 451 | @Nullable final RendererCommon.GlDrawer drawerParam, final boolean applyFpsReduction) { |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 452 | postToRenderThread(() -> { |
| 453 | final RendererCommon.GlDrawer listenerDrawer = drawerParam == null ? drawer : drawerParam; |
| 454 | frameListeners.add( |
| 455 | new FrameListenerAndParams(listener, scale, listenerDrawer, applyFpsReduction)); |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 456 | }); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Remove any pending callback that was added with addFrameListener. If the callback is not in |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 461 | * the queue, nothing happens. It is ensured that callback won't be called after this method |
| 462 | * returns. |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 463 | * |
| 464 | * @param runnable The callback to remove. |
| 465 | */ |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 466 | public void removeFrameListener(final FrameListener listener) { |
| 467 | final CountDownLatch latch = new CountDownLatch(1); |
Sami Kalliomäki | 8ebac24 | 2017-11-08 17:13:13 +0100 | [diff] [blame] | 468 | synchronized (handlerLock) { |
| 469 | if (renderThreadHandler == null) { |
| 470 | return; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 471 | } |
Sami Kalliomäki | 8ebac24 | 2017-11-08 17:13:13 +0100 | [diff] [blame] | 472 | if (Thread.currentThread() == renderThreadHandler.getLooper().getThread()) { |
| 473 | throw new RuntimeException("removeFrameListener must not be called on the render thread."); |
| 474 | } |
| 475 | postToRenderThread(() -> { |
| 476 | latch.countDown(); |
| 477 | final Iterator<FrameListenerAndParams> iter = frameListeners.iterator(); |
| 478 | while (iter.hasNext()) { |
| 479 | if (iter.next().listener == listener) { |
| 480 | iter.remove(); |
| 481 | } |
| 482 | } |
| 483 | }); |
| 484 | } |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 485 | ThreadUtils.awaitUninterruptibly(latch); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 486 | } |
| 487 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 488 | // VideoSink interface. |
| 489 | @Override |
| 490 | public void onFrame(VideoFrame frame) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 491 | synchronized (statisticsLock) { |
| 492 | ++framesReceived; |
| 493 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 494 | final boolean dropOldFrame; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 495 | synchronized (handlerLock) { |
| 496 | if (renderThreadHandler == null) { |
| 497 | logD("Dropping frame - Not initialized or already released."); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 498 | return; |
| 499 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 500 | synchronized (frameLock) { |
| 501 | dropOldFrame = (pendingFrame != null); |
| 502 | if (dropOldFrame) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 503 | pendingFrame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 504 | } |
| 505 | pendingFrame = frame; |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 506 | pendingFrame.retain(); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 507 | renderThreadHandler.post(this ::renderFrameOnRenderThread); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 510 | if (dropOldFrame) { |
| 511 | synchronized (statisticsLock) { |
| 512 | ++framesDropped; |
| 513 | } |
| 514 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Release EGL surface. This function will block until the EGL surface is released. |
| 519 | */ |
sakal | 28ec6bd | 2016-11-09 01:47:12 -0800 | [diff] [blame] | 520 | public void releaseEglSurface(final Runnable completionCallback) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 521 | // Ensure that the render thread is no longer touching the Surface before returning from this |
| 522 | // function. |
| 523 | eglSurfaceCreationRunnable.setSurface(null /* surface */); |
| 524 | synchronized (handlerLock) { |
| 525 | if (renderThreadHandler != null) { |
| 526 | renderThreadHandler.removeCallbacks(eglSurfaceCreationRunnable); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 527 | renderThreadHandler.postAtFrontOfQueue(() -> { |
| 528 | if (eglBase != null) { |
| 529 | eglBase.detachCurrent(); |
| 530 | eglBase.releaseSurface(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 531 | } |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 532 | completionCallback.run(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 533 | }); |
sakal | 28ec6bd | 2016-11-09 01:47:12 -0800 | [diff] [blame] | 534 | return; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
sakal | 28ec6bd | 2016-11-09 01:47:12 -0800 | [diff] [blame] | 537 | completionCallback.run(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /** |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 541 | * Private helper function to post tasks safely. |
| 542 | */ |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 543 | private void postToRenderThread(Runnable runnable) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 544 | synchronized (handlerLock) { |
| 545 | if (renderThreadHandler != null) { |
| 546 | renderThreadHandler.post(runnable); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 551 | private void clearSurfaceOnRenderThread(float r, float g, float b, float a) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 552 | if (eglBase != null && eglBase.hasSurface()) { |
| 553 | logD("clearSurface"); |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 554 | GLES20.glClearColor(r, g, b, a); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 555 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
| 556 | eglBase.swapBuffers(); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /** |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 561 | * Post a task to clear the surface to a transparent uniform color. |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 562 | */ |
| 563 | public void clearImage() { |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 564 | clearImage(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Post a task to clear the surface to a specific color. |
| 569 | */ |
| 570 | public void clearImage(final float r, final float g, final float b, final float a) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 571 | synchronized (handlerLock) { |
| 572 | if (renderThreadHandler == null) { |
| 573 | return; |
| 574 | } |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 575 | renderThreadHandler.postAtFrontOfQueue(() -> clearSurfaceOnRenderThread(r, g, b, a)); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | /** |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 580 | * Renders and releases |pendingFrame|. |
| 581 | */ |
| 582 | private void renderFrameOnRenderThread() { |
| 583 | // Fetch and render |pendingFrame|. |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 584 | final VideoFrame frame; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 585 | synchronized (frameLock) { |
| 586 | if (pendingFrame == null) { |
| 587 | return; |
| 588 | } |
| 589 | frame = pendingFrame; |
| 590 | pendingFrame = null; |
| 591 | } |
| 592 | if (eglBase == null || !eglBase.hasSurface()) { |
| 593 | logD("Dropping frame - No surface"); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 594 | frame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 595 | return; |
| 596 | } |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 597 | // Check if fps reduction is active. |
| 598 | final boolean shouldRenderFrame; |
| 599 | synchronized (fpsReductionLock) { |
| 600 | if (minRenderPeriodNs == Long.MAX_VALUE) { |
| 601 | // Rendering is paused. |
| 602 | shouldRenderFrame = false; |
| 603 | } else if (minRenderPeriodNs <= 0) { |
| 604 | // FPS reduction is disabled. |
| 605 | shouldRenderFrame = true; |
| 606 | } else { |
| 607 | final long currentTimeNs = System.nanoTime(); |
| 608 | if (currentTimeNs < nextFrameTimeNs) { |
| 609 | logD("Skipping frame rendering - fps reduction is active."); |
| 610 | shouldRenderFrame = false; |
| 611 | } else { |
| 612 | nextFrameTimeNs += minRenderPeriodNs; |
| 613 | // The time for the next frame should always be in the future. |
| 614 | nextFrameTimeNs = Math.max(nextFrameTimeNs, currentTimeNs); |
| 615 | shouldRenderFrame = true; |
| 616 | } |
| 617 | } |
| 618 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 619 | |
| 620 | final long startTimeNs = System.nanoTime(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 621 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 622 | final float frameAspectRatio = frame.getRotatedWidth() / (float) frame.getRotatedHeight(); |
| 623 | final float drawnAspectRatio; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 624 | synchronized (layoutLock) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 625 | drawnAspectRatio = layoutAspectRatio != 0f ? layoutAspectRatio : frameAspectRatio; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 626 | } |
| 627 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 628 | final float scaleX; |
| 629 | final float scaleY; |
| 630 | |
| 631 | if (frameAspectRatio > drawnAspectRatio) { |
| 632 | scaleX = drawnAspectRatio / frameAspectRatio; |
| 633 | scaleY = 1f; |
| 634 | } else { |
| 635 | scaleX = 1f; |
| 636 | scaleY = frameAspectRatio / drawnAspectRatio; |
| 637 | } |
| 638 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 639 | drawMatrix.reset(); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 640 | drawMatrix.preTranslate(0.5f, 0.5f); |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 641 | drawMatrix.preScale(mirrorHorizontally ? -1f : 1f, mirrorVertically ? -1f : 1f); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 642 | drawMatrix.preScale(scaleX, scaleY); |
| 643 | drawMatrix.preTranslate(-0.5f, -0.5f); |
| 644 | |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 645 | if (shouldRenderFrame) { |
| 646 | GLES20.glClearColor(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); |
| 647 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 648 | frameDrawer.drawFrame(frame, drawer, drawMatrix, 0 /* viewportX */, 0 /* viewportY */, |
| 649 | eglBase.surfaceWidth(), eglBase.surfaceHeight()); |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 650 | |
| 651 | final long swapBuffersStartTimeNs = System.nanoTime(); |
Magnus Jedvert | 361dbc1 | 2018-11-06 11:32:46 +0100 | [diff] [blame] | 652 | if (usePresentationTimeStamp) { |
| 653 | eglBase.swapBuffers(frame.getTimestampNs()); |
| 654 | } else { |
| 655 | eglBase.swapBuffers(); |
| 656 | } |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 657 | |
| 658 | final long currentTimeNs = System.nanoTime(); |
| 659 | synchronized (statisticsLock) { |
| 660 | ++framesRendered; |
| 661 | renderTimeNs += (currentTimeNs - startTimeNs); |
| 662 | renderSwapBufferTimeNs += (currentTimeNs - swapBuffersStartTimeNs); |
| 663 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 664 | } |
| 665 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 666 | notifyCallbacks(frame, shouldRenderFrame); |
| 667 | frame.release(); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 668 | } |
| 669 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 670 | private void notifyCallbacks(VideoFrame frame, boolean wasRendered) { |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 671 | if (frameListeners.isEmpty()) |
| 672 | return; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 673 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 674 | drawMatrix.reset(); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 675 | drawMatrix.preTranslate(0.5f, 0.5f); |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 676 | drawMatrix.preScale(mirrorHorizontally ? -1f : 1f, mirrorVertically ? -1f : 1f); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 677 | drawMatrix.preScale(1f, -1f); // We want the output to be upside down for Bitmap. |
| 678 | drawMatrix.preTranslate(-0.5f, -0.5f); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 679 | |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 680 | Iterator<FrameListenerAndParams> it = frameListeners.iterator(); |
| 681 | while (it.hasNext()) { |
| 682 | FrameListenerAndParams listenerAndParams = it.next(); |
| 683 | if (!wasRendered && listenerAndParams.applyFpsReduction) { |
| 684 | continue; |
| 685 | } |
| 686 | it.remove(); |
| 687 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 688 | final int scaledWidth = (int) (listenerAndParams.scale * frame.getRotatedWidth()); |
| 689 | final int scaledHeight = (int) (listenerAndParams.scale * frame.getRotatedHeight()); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 690 | |
| 691 | if (scaledWidth == 0 || scaledHeight == 0) { |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 692 | listenerAndParams.listener.onFrame(null); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 693 | continue; |
| 694 | } |
| 695 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 696 | bitmapTextureFramebuffer.setSize(scaledWidth, scaledHeight); |
| 697 | |
| 698 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, bitmapTextureFramebuffer.getFrameBufferId()); |
| 699 | GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, |
| 700 | GLES20.GL_TEXTURE_2D, bitmapTextureFramebuffer.getTextureId(), 0); |
| 701 | |
sakal | 103988d | 2017-02-17 09:59:01 -0800 | [diff] [blame] | 702 | GLES20.glClearColor(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); |
| 703 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 704 | frameDrawer.drawFrame(frame, listenerAndParams.drawer, drawMatrix, 0 /* viewportX */, |
| 705 | 0 /* viewportY */, scaledWidth, scaledHeight); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 706 | |
| 707 | final ByteBuffer bitmapBuffer = ByteBuffer.allocateDirect(scaledWidth * scaledHeight * 4); |
| 708 | GLES20.glViewport(0, 0, scaledWidth, scaledHeight); |
| 709 | GLES20.glReadPixels( |
| 710 | 0, 0, scaledWidth, scaledHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, bitmapBuffer); |
| 711 | |
| 712 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); |
| 713 | GlUtil.checkNoGLES2Error("EglRenderer.notifyCallbacks"); |
| 714 | |
| 715 | final Bitmap bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); |
| 716 | bitmap.copyPixelsFromBuffer(bitmapBuffer); |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 717 | listenerAndParams.listener.onFrame(bitmap); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 718 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 719 | } |
| 720 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 721 | private String averageTimeAsString(long sumTimeNs, int count) { |
Magnus Jedvert | 3bc696f | 2018-11-12 11:35:20 +0100 | [diff] [blame] | 722 | return (count <= 0) ? "NA" : TimeUnit.NANOSECONDS.toMicros(sumTimeNs / count) + " us"; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 723 | } |
| 724 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 725 | private void logStatistics() { |
Sami Kalliomäki | 1659e97 | 2018-06-04 14:07:58 +0200 | [diff] [blame] | 726 | final DecimalFormat fpsFormat = new DecimalFormat("#.0"); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 727 | final long currentTimeNs = System.nanoTime(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 728 | synchronized (statisticsLock) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 729 | final long elapsedTimeNs = currentTimeNs - statisticsStartTimeNs; |
| 730 | if (elapsedTimeNs <= 0) { |
| 731 | return; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 732 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 733 | final float renderFps = framesRendered * TimeUnit.SECONDS.toNanos(1) / (float) elapsedTimeNs; |
| 734 | logD("Duration: " + TimeUnit.NANOSECONDS.toMillis(elapsedTimeNs) + " ms." |
| 735 | + " Frames received: " + framesReceived + "." |
| 736 | + " Dropped: " + framesDropped + "." |
| 737 | + " Rendered: " + framesRendered + "." |
Sami Kalliomäki | 1659e97 | 2018-06-04 14:07:58 +0200 | [diff] [blame] | 738 | + " Render fps: " + fpsFormat.format(renderFps) + "." |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 739 | + " Average render time: " + averageTimeAsString(renderTimeNs, framesRendered) + "." |
| 740 | + " Average swapBuffer time: " |
| 741 | + averageTimeAsString(renderSwapBufferTimeNs, framesRendered) + "."); |
| 742 | resetStatistics(currentTimeNs); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | private void logD(String string) { |
| 747 | Logging.d(TAG, name + string); |
| 748 | } |
Magnus Jedvert | 0cc11b4 | 2018-11-27 16:19:55 +0100 | [diff] [blame] | 749 | |
| 750 | private void logW(String string) { |
| 751 | Logging.w(TAG, name + string); |
| 752 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 753 | } |