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