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