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; |
Byoungchan Lee | 02334e0 | 2021-08-14 11:41:59 +0900 | [diff] [blame] | 22 | import androidx.annotation.Nullable; |
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 | |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 114 | // `renderThreadHandler` is a handler for communicating with `renderThread`, and is synchronized |
| 115 | // on `handlerLock`. |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 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 | |
Byoungchan Lee | 02235d5 | 2020-02-08 16:32:21 +0900 | [diff] [blame] | 131 | // EGL and GL resources for drawing YUV/OES textures. After initialization, these are only |
| 132 | // accessed 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 | |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 139 | // Pending frame to render. Serves as a queue with size 1. Synchronized on `frameLock`. |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 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 | |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 143 | // These variables are synchronized on `layoutLock`. |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 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 | |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 151 | // These variables are synchronized on `statisticsLock`. |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 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 | /** |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 201 | * Initialize this class, sharing resources with `sharedContext`. The custom `drawer` will be used |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 202 | * for drawing frames on the EGLSurface. This class is responsible for calling release() on |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 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. |
Magnus Jedvert | f355e1a | 2020-04-21 13:52:38 +0200 | [diff] [blame] | 293 | synchronized (EglBase.lock) { |
| 294 | GLES20.glUseProgram(/* program= */ 0); |
| 295 | } |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 296 | if (drawer != null) { |
| 297 | drawer.release(); |
| 298 | drawer = null; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 299 | } |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 300 | frameDrawer.release(); |
Magnus Jedvert | 2ed62b3 | 2018-04-11 14:25:14 +0200 | [diff] [blame] | 301 | bitmapTextureFramebuffer.release(); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 302 | if (eglBase != null) { |
| 303 | logD("eglBase detach and release."); |
| 304 | eglBase.detachCurrent(); |
| 305 | eglBase.release(); |
| 306 | eglBase = null; |
| 307 | } |
Sami Kalliomäki | 8ebac24 | 2017-11-08 17:13:13 +0100 | [diff] [blame] | 308 | frameListeners.clear(); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 309 | eglCleanupBarrier.countDown(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 310 | }); |
| 311 | final Looper renderLooper = renderThreadHandler.getLooper(); |
| 312 | // TODO(magjed): Replace this post() with renderLooper.quitSafely() when API support >= 18. |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 313 | renderThreadHandler.post(() -> { |
| 314 | logD("Quitting render thread."); |
| 315 | renderLooper.quit(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 316 | }); |
| 317 | // Don't accept any more frames or messages to the render thread. |
| 318 | renderThreadHandler = null; |
| 319 | } |
| 320 | // Make sure the EGL/GL cleanup posted above is executed. |
| 321 | ThreadUtils.awaitUninterruptibly(eglCleanupBarrier); |
| 322 | synchronized (frameLock) { |
| 323 | if (pendingFrame != null) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 324 | pendingFrame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 325 | pendingFrame = null; |
| 326 | } |
| 327 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 328 | logD("Releasing done."); |
| 329 | } |
| 330 | |
| 331 | /** |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 332 | * Reset the statistics logged in logStatistics(). |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 333 | */ |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 334 | private void resetStatistics(long currentTimeNs) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 335 | synchronized (statisticsLock) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 336 | statisticsStartTimeNs = currentTimeNs; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 337 | framesReceived = 0; |
| 338 | framesDropped = 0; |
| 339 | framesRendered = 0; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 340 | renderTimeNs = 0; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 341 | renderSwapBufferTimeNs = 0; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | public void printStackTrace() { |
| 346 | synchronized (handlerLock) { |
| 347 | final Thread renderThread = |
| 348 | (renderThreadHandler == null) ? null : renderThreadHandler.getLooper().getThread(); |
| 349 | if (renderThread != null) { |
| 350 | final StackTraceElement[] renderStackTrace = renderThread.getStackTrace(); |
| 351 | if (renderStackTrace.length > 0) { |
Magnus Jedvert | 0cc11b4 | 2018-11-27 16:19:55 +0100 | [diff] [blame] | 352 | logW("EglRenderer stack trace:"); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 353 | for (StackTraceElement traceElem : renderStackTrace) { |
Magnus Jedvert | 0cc11b4 | 2018-11-27 16:19:55 +0100 | [diff] [blame] | 354 | logW(traceElem.toString()); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 362 | * Set if the video stream should be mirrored horizontally or not. |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 363 | */ |
| 364 | public void setMirror(final boolean mirror) { |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 365 | logD("setMirrorHorizontally: " + mirror); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 366 | synchronized (layoutLock) { |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 367 | this.mirrorHorizontally = mirror; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Set if the video stream should be mirrored vertically or not. |
| 373 | */ |
| 374 | public void setMirrorVertically(final boolean mirrorVertically) { |
| 375 | logD("setMirrorVertically: " + mirrorVertically); |
| 376 | synchronized (layoutLock) { |
| 377 | this.mirrorVertically = mirrorVertically; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Set layout aspect ratio. This is used to crop frames when rendering to avoid stretched video. |
| 383 | * Set this to 0 to disable cropping. |
| 384 | */ |
| 385 | public void setLayoutAspectRatio(float layoutAspectRatio) { |
| 386 | logD("setLayoutAspectRatio: " + layoutAspectRatio); |
| 387 | synchronized (layoutLock) { |
| 388 | this.layoutAspectRatio = layoutAspectRatio; |
| 389 | } |
| 390 | } |
| 391 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 392 | /** |
| 393 | * Limit render framerate. |
| 394 | * |
| 395 | * @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps |
| 396 | * reduction. |
| 397 | */ |
| 398 | public void setFpsReduction(float fps) { |
| 399 | logD("setFpsReduction: " + fps); |
| 400 | synchronized (fpsReductionLock) { |
| 401 | final long previousRenderPeriodNs = minRenderPeriodNs; |
| 402 | if (fps <= 0) { |
| 403 | minRenderPeriodNs = Long.MAX_VALUE; |
| 404 | } else { |
| 405 | minRenderPeriodNs = (long) (TimeUnit.SECONDS.toNanos(1) / fps); |
| 406 | } |
| 407 | if (minRenderPeriodNs != previousRenderPeriodNs) { |
| 408 | // Fps reduction changed - reset frame time. |
| 409 | nextFrameTimeNs = System.nanoTime(); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | public void disableFpsReduction() { |
| 415 | setFpsReduction(Float.POSITIVE_INFINITY /* fps */); |
| 416 | } |
| 417 | |
| 418 | public void pauseVideo() { |
| 419 | setFpsReduction(0 /* fps */); |
| 420 | } |
| 421 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 422 | /** |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 423 | * Register a callback to be invoked when a new video frame has been received. This version uses |
| 424 | * the drawer of the EglRenderer that was passed in init. |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 425 | * |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 426 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 427 | * It should be lightweight and must not call removeFrameListener. |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 428 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 429 | * required. |
| 430 | */ |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 431 | public void addFrameListener(final FrameListener listener, final float scale) { |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 432 | addFrameListener(listener, scale, null, false /* applyFpsReduction */); |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Register a callback to be invoked when a new video frame has been received. |
| 437 | * |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 438 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 439 | * It should be lightweight and must not call removeFrameListener. |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 440 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 441 | * required. |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 442 | * @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] | 443 | */ |
| 444 | public void addFrameListener( |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 445 | final FrameListener listener, final float scale, final RendererCommon.GlDrawer drawerParam) { |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 446 | addFrameListener(listener, scale, drawerParam, false /* applyFpsReduction */); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Register a callback to be invoked when a new video frame has been received. |
| 451 | * |
| 452 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 453 | * It should be lightweight and must not call removeFrameListener. |
| 454 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 455 | * required. |
| 456 | * @param drawer Custom drawer to use for this frame listener or null to use the default one. |
| 457 | * @param applyFpsReduction This callback will not be called for frames that have been dropped by |
| 458 | * FPS reduction. |
| 459 | */ |
| 460 | public void addFrameListener(final FrameListener listener, final float scale, |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 461 | @Nullable final RendererCommon.GlDrawer drawerParam, final boolean applyFpsReduction) { |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 462 | postToRenderThread(() -> { |
| 463 | final RendererCommon.GlDrawer listenerDrawer = drawerParam == null ? drawer : drawerParam; |
| 464 | frameListeners.add( |
| 465 | new FrameListenerAndParams(listener, scale, listenerDrawer, applyFpsReduction)); |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 466 | }); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /** |
| 470 | * 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] | 471 | * the queue, nothing happens. It is ensured that callback won't be called after this method |
| 472 | * returns. |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 473 | * |
| 474 | * @param runnable The callback to remove. |
| 475 | */ |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 476 | public void removeFrameListener(final FrameListener listener) { |
| 477 | final CountDownLatch latch = new CountDownLatch(1); |
Sami Kalliomäki | 8ebac24 | 2017-11-08 17:13:13 +0100 | [diff] [blame] | 478 | synchronized (handlerLock) { |
| 479 | if (renderThreadHandler == null) { |
| 480 | return; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 481 | } |
Sami Kalliomäki | 8ebac24 | 2017-11-08 17:13:13 +0100 | [diff] [blame] | 482 | if (Thread.currentThread() == renderThreadHandler.getLooper().getThread()) { |
| 483 | throw new RuntimeException("removeFrameListener must not be called on the render thread."); |
| 484 | } |
| 485 | postToRenderThread(() -> { |
| 486 | latch.countDown(); |
| 487 | final Iterator<FrameListenerAndParams> iter = frameListeners.iterator(); |
| 488 | while (iter.hasNext()) { |
| 489 | if (iter.next().listener == listener) { |
| 490 | iter.remove(); |
| 491 | } |
| 492 | } |
| 493 | }); |
| 494 | } |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 495 | ThreadUtils.awaitUninterruptibly(latch); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Magnus Jedvert | ecae9cd | 2019-07-05 14:33:12 +0200 | [diff] [blame] | 498 | /** Can be set in order to be notified about errors encountered during rendering. */ |
| 499 | public void setErrorCallback(ErrorCallback errorCallback) { |
| 500 | this.errorCallback = errorCallback; |
| 501 | } |
| 502 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 503 | // VideoSink interface. |
| 504 | @Override |
| 505 | public void onFrame(VideoFrame frame) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 506 | synchronized (statisticsLock) { |
| 507 | ++framesReceived; |
| 508 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 509 | final boolean dropOldFrame; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 510 | synchronized (handlerLock) { |
| 511 | if (renderThreadHandler == null) { |
| 512 | logD("Dropping frame - Not initialized or already released."); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 513 | return; |
| 514 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 515 | synchronized (frameLock) { |
| 516 | dropOldFrame = (pendingFrame != null); |
| 517 | if (dropOldFrame) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 518 | pendingFrame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 519 | } |
| 520 | pendingFrame = frame; |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 521 | pendingFrame.retain(); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 522 | renderThreadHandler.post(this ::renderFrameOnRenderThread); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 525 | if (dropOldFrame) { |
| 526 | synchronized (statisticsLock) { |
| 527 | ++framesDropped; |
| 528 | } |
| 529 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Release EGL surface. This function will block until the EGL surface is released. |
| 534 | */ |
sakal | 28ec6bd | 2016-11-09 01:47:12 -0800 | [diff] [blame] | 535 | public void releaseEglSurface(final Runnable completionCallback) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 536 | // Ensure that the render thread is no longer touching the Surface before returning from this |
| 537 | // function. |
| 538 | eglSurfaceCreationRunnable.setSurface(null /* surface */); |
| 539 | synchronized (handlerLock) { |
| 540 | if (renderThreadHandler != null) { |
| 541 | renderThreadHandler.removeCallbacks(eglSurfaceCreationRunnable); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 542 | renderThreadHandler.postAtFrontOfQueue(() -> { |
| 543 | if (eglBase != null) { |
| 544 | eglBase.detachCurrent(); |
| 545 | eglBase.releaseSurface(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 546 | } |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 547 | completionCallback.run(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 548 | }); |
sakal | 28ec6bd | 2016-11-09 01:47:12 -0800 | [diff] [blame] | 549 | return; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
sakal | 28ec6bd | 2016-11-09 01:47:12 -0800 | [diff] [blame] | 552 | completionCallback.run(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /** |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 556 | * Private helper function to post tasks safely. |
| 557 | */ |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 558 | private void postToRenderThread(Runnable runnable) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 559 | synchronized (handlerLock) { |
| 560 | if (renderThreadHandler != null) { |
| 561 | renderThreadHandler.post(runnable); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 566 | private void clearSurfaceOnRenderThread(float r, float g, float b, float a) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 567 | if (eglBase != null && eglBase.hasSurface()) { |
| 568 | logD("clearSurface"); |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 569 | GLES20.glClearColor(r, g, b, a); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 570 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
| 571 | eglBase.swapBuffers(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /** |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 576 | * Post a task to clear the surface to a transparent uniform color. |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 577 | */ |
| 578 | public void clearImage() { |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 579 | clearImage(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Post a task to clear the surface to a specific color. |
| 584 | */ |
| 585 | 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] | 586 | synchronized (handlerLock) { |
| 587 | if (renderThreadHandler == null) { |
| 588 | return; |
| 589 | } |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 590 | renderThreadHandler.postAtFrontOfQueue(() -> clearSurfaceOnRenderThread(r, g, b, a)); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | /** |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 595 | * Renders and releases `pendingFrame`. |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 596 | */ |
| 597 | private void renderFrameOnRenderThread() { |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 598 | // Fetch and render `pendingFrame`. |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 599 | final VideoFrame frame; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 600 | synchronized (frameLock) { |
| 601 | if (pendingFrame == null) { |
| 602 | return; |
| 603 | } |
| 604 | frame = pendingFrame; |
| 605 | pendingFrame = null; |
| 606 | } |
| 607 | if (eglBase == null || !eglBase.hasSurface()) { |
| 608 | logD("Dropping frame - No surface"); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 609 | frame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 610 | return; |
| 611 | } |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 612 | // Check if fps reduction is active. |
| 613 | final boolean shouldRenderFrame; |
| 614 | synchronized (fpsReductionLock) { |
| 615 | if (minRenderPeriodNs == Long.MAX_VALUE) { |
| 616 | // Rendering is paused. |
| 617 | shouldRenderFrame = false; |
| 618 | } else if (minRenderPeriodNs <= 0) { |
| 619 | // FPS reduction is disabled. |
| 620 | shouldRenderFrame = true; |
| 621 | } else { |
| 622 | final long currentTimeNs = System.nanoTime(); |
| 623 | if (currentTimeNs < nextFrameTimeNs) { |
| 624 | logD("Skipping frame rendering - fps reduction is active."); |
| 625 | shouldRenderFrame = false; |
| 626 | } else { |
| 627 | nextFrameTimeNs += minRenderPeriodNs; |
| 628 | // The time for the next frame should always be in the future. |
| 629 | nextFrameTimeNs = Math.max(nextFrameTimeNs, currentTimeNs); |
| 630 | shouldRenderFrame = true; |
| 631 | } |
| 632 | } |
| 633 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 634 | |
| 635 | final long startTimeNs = System.nanoTime(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 636 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 637 | final float frameAspectRatio = frame.getRotatedWidth() / (float) frame.getRotatedHeight(); |
| 638 | final float drawnAspectRatio; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 639 | synchronized (layoutLock) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 640 | drawnAspectRatio = layoutAspectRatio != 0f ? layoutAspectRatio : frameAspectRatio; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 641 | } |
| 642 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 643 | final float scaleX; |
| 644 | final float scaleY; |
| 645 | |
| 646 | if (frameAspectRatio > drawnAspectRatio) { |
| 647 | scaleX = drawnAspectRatio / frameAspectRatio; |
| 648 | scaleY = 1f; |
| 649 | } else { |
| 650 | scaleX = 1f; |
| 651 | scaleY = frameAspectRatio / drawnAspectRatio; |
| 652 | } |
| 653 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 654 | drawMatrix.reset(); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 655 | drawMatrix.preTranslate(0.5f, 0.5f); |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 656 | drawMatrix.preScale(mirrorHorizontally ? -1f : 1f, mirrorVertically ? -1f : 1f); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 657 | drawMatrix.preScale(scaleX, scaleY); |
| 658 | drawMatrix.preTranslate(-0.5f, -0.5f); |
| 659 | |
Magnus Jedvert | ecae9cd | 2019-07-05 14:33:12 +0200 | [diff] [blame] | 660 | try { |
| 661 | if (shouldRenderFrame) { |
| 662 | GLES20.glClearColor(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); |
| 663 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
| 664 | frameDrawer.drawFrame(frame, drawer, drawMatrix, 0 /* viewportX */, 0 /* viewportY */, |
| 665 | eglBase.surfaceWidth(), eglBase.surfaceHeight()); |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 666 | |
Magnus Jedvert | ecae9cd | 2019-07-05 14:33:12 +0200 | [diff] [blame] | 667 | final long swapBuffersStartTimeNs = System.nanoTime(); |
| 668 | if (usePresentationTimeStamp) { |
| 669 | eglBase.swapBuffers(frame.getTimestampNs()); |
| 670 | } else { |
| 671 | eglBase.swapBuffers(); |
| 672 | } |
| 673 | |
| 674 | final long currentTimeNs = System.nanoTime(); |
| 675 | synchronized (statisticsLock) { |
| 676 | ++framesRendered; |
| 677 | renderTimeNs += (currentTimeNs - startTimeNs); |
| 678 | renderSwapBufferTimeNs += (currentTimeNs - swapBuffersStartTimeNs); |
| 679 | } |
Magnus Jedvert | 361dbc1 | 2018-11-06 11:32:46 +0100 | [diff] [blame] | 680 | } |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 681 | |
Magnus Jedvert | ecae9cd | 2019-07-05 14:33:12 +0200 | [diff] [blame] | 682 | notifyCallbacks(frame, shouldRenderFrame); |
| 683 | } catch (GlUtil.GlOutOfMemoryException e) { |
| 684 | logE("Error while drawing frame", e); |
| 685 | final ErrorCallback errorCallback = this.errorCallback; |
| 686 | if (errorCallback != null) { |
| 687 | errorCallback.onGlOutOfMemory(); |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 688 | } |
Magnus Jedvert | ecae9cd | 2019-07-05 14:33:12 +0200 | [diff] [blame] | 689 | // Attempt to free up some resources. |
| 690 | drawer.release(); |
| 691 | frameDrawer.release(); |
| 692 | bitmapTextureFramebuffer.release(); |
| 693 | // Continue here on purpose and retry again for next frame. In worst case, this is a continous |
| 694 | // problem and no more frames will be drawn. |
| 695 | } finally { |
| 696 | frame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 697 | } |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 698 | } |
| 699 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 700 | private void notifyCallbacks(VideoFrame frame, boolean wasRendered) { |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 701 | if (frameListeners.isEmpty()) |
| 702 | return; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 703 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 704 | drawMatrix.reset(); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 705 | drawMatrix.preTranslate(0.5f, 0.5f); |
Magnus Jedvert | 3ff71de | 2018-12-17 10:26:12 +0100 | [diff] [blame] | 706 | drawMatrix.preScale(mirrorHorizontally ? -1f : 1f, mirrorVertically ? -1f : 1f); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 707 | drawMatrix.preScale(1f, -1f); // We want the output to be upside down for Bitmap. |
| 708 | drawMatrix.preTranslate(-0.5f, -0.5f); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 709 | |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 710 | Iterator<FrameListenerAndParams> it = frameListeners.iterator(); |
| 711 | while (it.hasNext()) { |
| 712 | FrameListenerAndParams listenerAndParams = it.next(); |
| 713 | if (!wasRendered && listenerAndParams.applyFpsReduction) { |
| 714 | continue; |
| 715 | } |
| 716 | it.remove(); |
| 717 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 718 | final int scaledWidth = (int) (listenerAndParams.scale * frame.getRotatedWidth()); |
| 719 | final int scaledHeight = (int) (listenerAndParams.scale * frame.getRotatedHeight()); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 720 | |
| 721 | if (scaledWidth == 0 || scaledHeight == 0) { |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 722 | listenerAndParams.listener.onFrame(null); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 723 | continue; |
| 724 | } |
| 725 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 726 | bitmapTextureFramebuffer.setSize(scaledWidth, scaledHeight); |
| 727 | |
| 728 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, bitmapTextureFramebuffer.getFrameBufferId()); |
| 729 | GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, |
| 730 | GLES20.GL_TEXTURE_2D, bitmapTextureFramebuffer.getTextureId(), 0); |
| 731 | |
sakal | 103988d | 2017-02-17 09:59:01 -0800 | [diff] [blame] | 732 | GLES20.glClearColor(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); |
| 733 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 734 | frameDrawer.drawFrame(frame, listenerAndParams.drawer, drawMatrix, 0 /* viewportX */, |
| 735 | 0 /* viewportY */, scaledWidth, scaledHeight); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 736 | |
| 737 | final ByteBuffer bitmapBuffer = ByteBuffer.allocateDirect(scaledWidth * scaledHeight * 4); |
| 738 | GLES20.glViewport(0, 0, scaledWidth, scaledHeight); |
| 739 | GLES20.glReadPixels( |
| 740 | 0, 0, scaledWidth, scaledHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, bitmapBuffer); |
| 741 | |
| 742 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); |
| 743 | GlUtil.checkNoGLES2Error("EglRenderer.notifyCallbacks"); |
| 744 | |
| 745 | final Bitmap bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); |
| 746 | bitmap.copyPixelsFromBuffer(bitmapBuffer); |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 747 | listenerAndParams.listener.onFrame(bitmap); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 748 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 749 | } |
| 750 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 751 | private String averageTimeAsString(long sumTimeNs, int count) { |
Magnus Jedvert | 3bc696f | 2018-11-12 11:35:20 +0100 | [diff] [blame] | 752 | return (count <= 0) ? "NA" : TimeUnit.NANOSECONDS.toMicros(sumTimeNs / count) + " us"; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 753 | } |
| 754 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 755 | private void logStatistics() { |
Sami Kalliomäki | 1659e97 | 2018-06-04 14:07:58 +0200 | [diff] [blame] | 756 | final DecimalFormat fpsFormat = new DecimalFormat("#.0"); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 757 | final long currentTimeNs = System.nanoTime(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 758 | synchronized (statisticsLock) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 759 | final long elapsedTimeNs = currentTimeNs - statisticsStartTimeNs; |
Sami Kalliomäki | 9b66114 | 2019-10-21 17:12:25 +0200 | [diff] [blame] | 760 | if (elapsedTimeNs <= 0 || (minRenderPeriodNs == Long.MAX_VALUE && framesReceived == 0)) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 761 | return; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 762 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 763 | final float renderFps = framesRendered * TimeUnit.SECONDS.toNanos(1) / (float) elapsedTimeNs; |
| 764 | logD("Duration: " + TimeUnit.NANOSECONDS.toMillis(elapsedTimeNs) + " ms." |
| 765 | + " Frames received: " + framesReceived + "." |
| 766 | + " Dropped: " + framesDropped + "." |
| 767 | + " Rendered: " + framesRendered + "." |
Sami Kalliomäki | 1659e97 | 2018-06-04 14:07:58 +0200 | [diff] [blame] | 768 | + " Render fps: " + fpsFormat.format(renderFps) + "." |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 769 | + " Average render time: " + averageTimeAsString(renderTimeNs, framesRendered) + "." |
| 770 | + " Average swapBuffer time: " |
| 771 | + averageTimeAsString(renderSwapBufferTimeNs, framesRendered) + "."); |
| 772 | resetStatistics(currentTimeNs); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Magnus Jedvert | ecae9cd | 2019-07-05 14:33:12 +0200 | [diff] [blame] | 776 | private void logE(String string, Throwable e) { |
| 777 | Logging.e(TAG, name + string, e); |
| 778 | } |
| 779 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 780 | private void logD(String string) { |
| 781 | Logging.d(TAG, name + string); |
| 782 | } |
Magnus Jedvert | 0cc11b4 | 2018-11-27 16:19:55 +0100 | [diff] [blame] | 783 | |
| 784 | private void logW(String string) { |
| 785 | Logging.w(TAG, name + string); |
| 786 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 787 | } |