Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
Jonathan Yu | 2238441 | 2017-08-16 13:25:45 -0700 | [diff] [blame] | 13 | import android.annotation.TargetApi; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 14 | import android.graphics.Matrix; |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 15 | import android.graphics.SurfaceTexture; |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 16 | import android.opengl.GLES11Ext; |
| 17 | import android.opengl.GLES20; |
| 18 | import android.os.Build; |
| 19 | import android.os.Handler; |
| 20 | import android.os.HandlerThread; |
Artem Titarenko | 69540f4 | 2018-12-10 12:30:46 +0100 | [diff] [blame] | 21 | import android.support.annotation.Nullable; |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 22 | import java.nio.ByteBuffer; |
Magnus Jedvert | 747c1bc | 2015-10-12 09:27:48 +0200 | [diff] [blame] | 23 | import java.util.concurrent.Callable; |
Magnus Jedvert | 6199a37 | 2017-11-14 13:03:08 +0100 | [diff] [blame] | 24 | import org.webrtc.EglBase; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 25 | import org.webrtc.VideoFrame.TextureBuffer; |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 26 | |
| 27 | /** |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 28 | * Helper class for using a SurfaceTexture to create WebRTC VideoFrames. In order to create WebRTC |
| 29 | * VideoFrames, render onto the SurfaceTexture. The frames will be delivered to the listener. Only |
| 30 | * one texture frame can be in flight at once, so the frame must be released in order to receive a |
| 31 | * new frame. Call stopListening() to stop receiveing new frames. Call dispose to release all |
| 32 | * resources once the texture frame is released. |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 33 | */ |
skvlad | ed6e077 | 2016-11-30 14:40:18 -0800 | [diff] [blame] | 34 | public class SurfaceTextureHelper { |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 35 | private static final String TAG = "SurfaceTextureHelper"; |
| 36 | /** |
magjed | 9e69dfd | 2016-02-26 07:45:44 -0800 | [diff] [blame] | 37 | * Construct a new SurfaceTextureHelper sharing OpenGL resources with |sharedContext|. A dedicated |
magjed | 2aa8426 | 2016-05-09 08:28:45 -0700 | [diff] [blame] | 38 | * thread and handler is created for handling the SurfaceTexture. May return null if EGL fails to |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 39 | * initialize a pixel buffer surface and make it current. If alignTimestamps is true, the frame |
| 40 | * timestamps will be aligned to rtc::TimeNanos(). If frame timestamps are aligned to |
| 41 | * rtc::TimeNanos() there is no need for aligning timestamps again in |
| 42 | * PeerConnectionFactory.createVideoSource(). This makes the timestamps more accurate and |
| 43 | * closer to actual creation time. |
Magnus Jedvert | 747c1bc | 2015-10-12 09:27:48 +0200 | [diff] [blame] | 44 | */ |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 45 | public static SurfaceTextureHelper create(final String threadName, |
| 46 | final EglBase.Context sharedContext, boolean alignTimestamps, |
| 47 | final YuvConverter yuvConverter) { |
magjed | 82b750b | 2016-03-31 00:54:15 -0700 | [diff] [blame] | 48 | final HandlerThread thread = new HandlerThread(threadName); |
magjed | 9e69dfd | 2016-02-26 07:45:44 -0800 | [diff] [blame] | 49 | thread.start(); |
| 50 | final Handler handler = new Handler(thread.getLooper()); |
| 51 | |
Magnus Jedvert | 747c1bc | 2015-10-12 09:27:48 +0200 | [diff] [blame] | 52 | // The onFrameAvailable() callback will be executed on the SurfaceTexture ctor thread. See: |
| 53 | // http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/graphics/SurfaceTexture.java#195. |
| 54 | // Therefore, in order to control the callback thread on API lvl < 21, the SurfaceTextureHelper |
| 55 | // is constructed on the |handler| thread. |
nisse | a44e72c | 2016-05-27 00:27:59 -0700 | [diff] [blame] | 56 | return ThreadUtils.invokeAtFrontUninterruptibly(handler, new Callable<SurfaceTextureHelper>() { |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 57 | @Nullable |
magjed | 9e69dfd | 2016-02-26 07:45:44 -0800 | [diff] [blame] | 58 | @Override |
| 59 | public SurfaceTextureHelper call() { |
magjed | 2aa8426 | 2016-05-09 08:28:45 -0700 | [diff] [blame] | 60 | try { |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 61 | return new SurfaceTextureHelper(sharedContext, handler, alignTimestamps, yuvConverter); |
magjed | 2aa8426 | 2016-05-09 08:28:45 -0700 | [diff] [blame] | 62 | } catch (RuntimeException e) { |
| 63 | Logging.e(TAG, threadName + " create failure", e); |
| 64 | return null; |
| 65 | } |
Magnus Jedvert | 747c1bc | 2015-10-12 09:27:48 +0200 | [diff] [blame] | 66 | } |
| 67 | }); |
| 68 | } |
| 69 | |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 70 | /** |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 71 | * Same as above with alignTimestamps set to false and yuvConverter set to new YuvConverter. |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 72 | * |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 73 | * @see #create(String, EglBase.Context, boolean, YuvConverter) |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 74 | */ |
| 75 | public static SurfaceTextureHelper create( |
| 76 | final String threadName, final EglBase.Context sharedContext) { |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 77 | return create(threadName, sharedContext, /* alignTimestamps= */ false, new YuvConverter()); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Same as above with yuvConverter set to new YuvConverter. |
| 82 | * |
| 83 | * @see #create(String, EglBase.Context, boolean, YuvConverter) |
| 84 | */ |
| 85 | public static SurfaceTextureHelper create( |
| 86 | final String threadName, final EglBase.Context sharedContext, boolean alignTimestamps) { |
| 87 | return create(threadName, sharedContext, alignTimestamps, new YuvConverter()); |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 90 | private final Handler handler; |
| 91 | private final EglBase eglBase; |
| 92 | private final SurfaceTexture surfaceTexture; |
| 93 | private final int oesTextureId; |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 94 | private final YuvConverter yuvConverter; |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 95 | @Nullable private final TimestampAligner timestampAligner; |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 96 | |
magjed | 81e8e37 | 2016-03-03 02:11:44 -0800 | [diff] [blame] | 97 | // These variables are only accessed from the |handler| thread. |
Magnus Jedvert | 814f99c | 2018-08-03 11:15:14 +0200 | [diff] [blame] | 98 | @Nullable private VideoSink listener; |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 99 | // The possible states of this class. |
Sami Kalliomäki | 3d50a31 | 2018-09-11 11:11:47 +0200 | [diff] [blame] | 100 | private boolean hasPendingTexture; |
| 101 | private volatile boolean isTextureInUse; |
| 102 | private boolean isQuitting; |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 103 | private int frameRotation; |
| 104 | private int textureWidth; |
| 105 | private int textureHeight; |
magjed | d8ddb79 | 2016-03-17 03:13:43 -0700 | [diff] [blame] | 106 | // |pendingListener| is set in setListener() and the runnable is posted to the handler thread. |
| 107 | // setListener() is not allowed to be called again before stopListening(), so this is thread safe. |
Magnus Jedvert | 814f99c | 2018-08-03 11:15:14 +0200 | [diff] [blame] | 108 | @Nullable private VideoSink pendingListener; |
magjed | d8ddb79 | 2016-03-17 03:13:43 -0700 | [diff] [blame] | 109 | final Runnable setListenerRunnable = new Runnable() { |
| 110 | @Override |
| 111 | public void run() { |
| 112 | Logging.d(TAG, "Setting listener to " + pendingListener); |
| 113 | listener = pendingListener; |
| 114 | pendingListener = null; |
Magnus Jedvert | 181310f | 2016-05-23 16:26:47 +0200 | [diff] [blame] | 115 | // May have a pending frame from the previous capture session - drop it. |
| 116 | if (hasPendingTexture) { |
| 117 | // Calling updateTexImage() is neccessary in order to receive new frames. |
| 118 | updateTexImage(); |
| 119 | hasPendingTexture = false; |
| 120 | } |
magjed | d8ddb79 | 2016-03-17 03:13:43 -0700 | [diff] [blame] | 121 | } |
| 122 | }; |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 123 | |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 124 | private SurfaceTextureHelper(EglBase.Context sharedContext, Handler handler, |
| 125 | boolean alignTimestamps, YuvConverter yuvConverter) { |
Alex Glaznev | a5b62d9 | 2015-10-12 13:56:20 -0700 | [diff] [blame] | 126 | if (handler.getLooper().getThread() != Thread.currentThread()) { |
Magnus Jedvert | 747c1bc | 2015-10-12 09:27:48 +0200 | [diff] [blame] | 127 | throw new IllegalStateException("SurfaceTextureHelper must be created on the handler thread"); |
perkj | 1b33da1 | 2015-10-05 21:06:41 +0200 | [diff] [blame] | 128 | } |
Magnus Jedvert | 747c1bc | 2015-10-12 09:27:48 +0200 | [diff] [blame] | 129 | this.handler = handler; |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 130 | this.timestampAligner = alignTimestamps ? new TimestampAligner() : null; |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 131 | this.yuvConverter = yuvConverter; |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 132 | |
nisse | 03f80eb | 2015-12-07 01:17:16 -0800 | [diff] [blame] | 133 | eglBase = EglBase.create(sharedContext, EglBase.CONFIG_PIXEL_BUFFER); |
magjed | 2aa8426 | 2016-05-09 08:28:45 -0700 | [diff] [blame] | 134 | try { |
| 135 | // Both these statements have been observed to fail on rare occasions, see BUG=webrtc:5682. |
| 136 | eglBase.createDummyPbufferSurface(); |
| 137 | eglBase.makeCurrent(); |
| 138 | } catch (RuntimeException e) { |
| 139 | // Clean up before rethrowing the exception. |
| 140 | eglBase.release(); |
| 141 | handler.getLooper().quit(); |
| 142 | throw e; |
| 143 | } |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 144 | |
| 145 | oesTextureId = GlUtil.generateTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES); |
| 146 | surfaceTexture = new SurfaceTexture(oesTextureId); |
Jonathan Yu | 2238441 | 2017-08-16 13:25:45 -0700 | [diff] [blame] | 147 | setOnFrameAvailableListener(surfaceTexture, (SurfaceTexture st) -> { |
| 148 | hasPendingTexture = true; |
| 149 | tryDeliverTextureFrame(); |
| 150 | }, handler); |
| 151 | } |
| 152 | |
| 153 | @TargetApi(21) |
| 154 | private static void setOnFrameAvailableListener(SurfaceTexture surfaceTexture, |
| 155 | SurfaceTexture.OnFrameAvailableListener listener, Handler handler) { |
| 156 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 157 | surfaceTexture.setOnFrameAvailableListener(listener, handler); |
| 158 | } else { |
| 159 | // The documentation states that the listener will be called on an arbitrary thread, but in |
| 160 | // pratice, it is always the thread on which the SurfaceTexture was constructed. There are |
| 161 | // assertions in place in case this ever changes. For API >= 21, we use the new API to |
| 162 | // explicitly specify the handler. |
| 163 | surfaceTexture.setOnFrameAvailableListener(listener); |
| 164 | } |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
magjed | 81e8e37 | 2016-03-03 02:11:44 -0800 | [diff] [blame] | 168 | * Start to stream textures to the given |listener|. If you need to change listener, you need to |
| 169 | * call stopListening() first. |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 170 | */ |
| 171 | public void startListening(final VideoSink listener) { |
magjed | d8ddb79 | 2016-03-17 03:13:43 -0700 | [diff] [blame] | 172 | if (this.listener != null || this.pendingListener != null) { |
Per | 3e9eb4b | 2015-09-28 10:52:22 +0200 | [diff] [blame] | 173 | throw new IllegalStateException("SurfaceTextureHelper listener has already been set."); |
| 174 | } |
magjed | d8ddb79 | 2016-03-17 03:13:43 -0700 | [diff] [blame] | 175 | this.pendingListener = listener; |
| 176 | handler.post(setListenerRunnable); |
Per | 3e9eb4b | 2015-09-28 10:52:22 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
magjed | 81e8e37 | 2016-03-03 02:11:44 -0800 | [diff] [blame] | 180 | * Stop listening. The listener set in startListening() is guaranteded to not receive any more |
Magnus Jedvert | 814f99c | 2018-08-03 11:15:14 +0200 | [diff] [blame] | 181 | * onFrame() callbacks after this function returns. |
magjed | 81e8e37 | 2016-03-03 02:11:44 -0800 | [diff] [blame] | 182 | */ |
| 183 | public void stopListening() { |
magjed | d8ddb79 | 2016-03-17 03:13:43 -0700 | [diff] [blame] | 184 | Logging.d(TAG, "stopListening()"); |
| 185 | handler.removeCallbacks(setListenerRunnable); |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 186 | ThreadUtils.invokeAtFrontUninterruptibly(handler, () -> { |
| 187 | listener = null; |
| 188 | pendingListener = null; |
nisse | a44e72c | 2016-05-27 00:27:59 -0700 | [diff] [blame] | 189 | }); |
magjed | 81e8e37 | 2016-03-03 02:11:44 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 193 | * Use this function to set the texture size. Note, do not call setDefaultBufferSize() yourself |
| 194 | * since this class needs to be aware of the texture size. |
| 195 | */ |
| 196 | public void setTextureSize(int textureWidth, int textureHeight) { |
| 197 | if (textureWidth <= 0) { |
| 198 | throw new IllegalArgumentException("Texture width must be positive, but was " + textureWidth); |
| 199 | } |
| 200 | if (textureHeight <= 0) { |
| 201 | throw new IllegalArgumentException( |
| 202 | "Texture height must be positive, but was " + textureHeight); |
| 203 | } |
| 204 | surfaceTexture.setDefaultBufferSize(textureWidth, textureHeight); |
| 205 | handler.post(() -> { |
| 206 | this.textureWidth = textureWidth; |
| 207 | this.textureHeight = textureHeight; |
Magnus Jedvert | 31f18e1 | 2019-06-04 11:11:11 +0200 | [diff] [blame^] | 208 | tryDeliverTextureFrame(); |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 209 | }); |
| 210 | } |
| 211 | |
| 212 | /** Set the rotation of the delivered frames. */ |
| 213 | public void setFrameRotation(int rotation) { |
| 214 | handler.post(() -> this.frameRotation = rotation); |
| 215 | } |
| 216 | |
| 217 | /** |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 218 | * Retrieve the underlying SurfaceTexture. The SurfaceTexture should be passed in to a video |
| 219 | * producer such as a camera or decoder. |
| 220 | */ |
| 221 | public SurfaceTexture getSurfaceTexture() { |
| 222 | return surfaceTexture; |
| 223 | } |
| 224 | |
Magnus Jedvert | 814f99c | 2018-08-03 11:15:14 +0200 | [diff] [blame] | 225 | /** Retrieve the handler that calls onFrame(). This handler is valid until dispose() is called. */ |
magjed | 9e69dfd | 2016-02-26 07:45:44 -0800 | [diff] [blame] | 226 | public Handler getHandler() { |
| 227 | return handler; |
| 228 | } |
| 229 | |
| 230 | /** |
Magnus Jedvert | 814f99c | 2018-08-03 11:15:14 +0200 | [diff] [blame] | 231 | * This function is called when the texture frame is released. Only one texture frame can be in |
| 232 | * flight at once, so this function must be called before a new frame is delivered. |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 233 | */ |
Magnus Jedvert | 814f99c | 2018-08-03 11:15:14 +0200 | [diff] [blame] | 234 | private void returnTextureFrame() { |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 235 | handler.post(() -> { |
| 236 | isTextureInUse = false; |
| 237 | if (isQuitting) { |
| 238 | release(); |
| 239 | } else { |
| 240 | tryDeliverTextureFrame(); |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 241 | } |
| 242 | }); |
| 243 | } |
| 244 | |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 245 | public boolean isTextureInUse() { |
| 246 | return isTextureInUse; |
| 247 | } |
| 248 | |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 249 | /** |
magjed | 9e69dfd | 2016-02-26 07:45:44 -0800 | [diff] [blame] | 250 | * Call disconnect() to stop receiving frames. OpenGL resources are released and the handler is |
Magnus Jedvert | 814f99c | 2018-08-03 11:15:14 +0200 | [diff] [blame] | 251 | * stopped when the texture frame has been released. You are guaranteed to not receive any more |
| 252 | * onFrame() after this function returns. |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 253 | */ |
magjed | 81e8e37 | 2016-03-03 02:11:44 -0800 | [diff] [blame] | 254 | public void dispose() { |
magjed | d8ddb79 | 2016-03-17 03:13:43 -0700 | [diff] [blame] | 255 | Logging.d(TAG, "dispose()"); |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 256 | ThreadUtils.invokeAtFrontUninterruptibly(handler, () -> { |
| 257 | isQuitting = true; |
| 258 | if (!isTextureInUse) { |
| 259 | release(); |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 260 | } |
| 261 | }); |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 264 | /** |
Magnus Jedvert | c040dae | 2017-11-17 16:50:17 +0100 | [diff] [blame] | 265 | * Posts to the correct thread to convert |textureBuffer| to I420. |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 266 | * |
| 267 | * @deprecated Use toI420() instead. |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 268 | */ |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 269 | @Deprecated |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 270 | public VideoFrame.I420Buffer textureToYuv(final TextureBuffer textureBuffer) { |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 271 | return textureBuffer.toI420(); |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Magnus Jedvert | 181310f | 2016-05-23 16:26:47 +0200 | [diff] [blame] | 274 | private void updateTexImage() { |
| 275 | // SurfaceTexture.updateTexImage apparently can compete and deadlock with eglSwapBuffers, |
| 276 | // as observed on Nexus 5. Therefore, synchronize it with the EGL functions. |
| 277 | // See https://bugs.chromium.org/p/webrtc/issues/detail?id=5702 for more info. |
| 278 | synchronized (EglBase.lock) { |
| 279 | surfaceTexture.updateTexImage(); |
| 280 | } |
| 281 | } |
| 282 | |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 283 | private void tryDeliverTextureFrame() { |
Alex Glaznev | a5b62d9 | 2015-10-12 13:56:20 -0700 | [diff] [blame] | 284 | if (handler.getLooper().getThread() != Thread.currentThread()) { |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 285 | throw new IllegalStateException("Wrong thread."); |
| 286 | } |
magjed | 81e8e37 | 2016-03-03 02:11:44 -0800 | [diff] [blame] | 287 | if (isQuitting || !hasPendingTexture || isTextureInUse || listener == null) { |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 288 | return; |
| 289 | } |
Magnus Jedvert | 31f18e1 | 2019-06-04 11:11:11 +0200 | [diff] [blame^] | 290 | if (textureWidth == 0 || textureHeight == 0) { |
| 291 | // Information about the resolution needs to be provided by a call to setTextureSize() before |
| 292 | // frames are produced. |
| 293 | Logging.w(TAG, "Texture size has not been set."); |
| 294 | return; |
| 295 | } |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 296 | isTextureInUse = true; |
| 297 | hasPendingTexture = false; |
| 298 | |
Magnus Jedvert | 181310f | 2016-05-23 16:26:47 +0200 | [diff] [blame] | 299 | updateTexImage(); |
perkj | 1b33da1 | 2015-10-05 21:06:41 +0200 | [diff] [blame] | 300 | |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 301 | final float[] transformMatrix = new float[16]; |
| 302 | surfaceTexture.getTransformMatrix(transformMatrix); |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 303 | long timestampNs = surfaceTexture.getTimestamp(); |
| 304 | if (timestampAligner != null) { |
| 305 | timestampNs = timestampAligner.translateTimestamp(timestampNs); |
| 306 | } |
Magnus Jedvert | 814f99c | 2018-08-03 11:15:14 +0200 | [diff] [blame] | 307 | final VideoFrame.Buffer buffer = |
| 308 | new TextureBufferImpl(textureWidth, textureHeight, TextureBuffer.Type.OES, oesTextureId, |
| 309 | RendererCommon.convertMatrixToAndroidGraphicsMatrix(transformMatrix), handler, |
| 310 | yuvConverter, this ::returnTextureFrame); |
| 311 | final VideoFrame frame = new VideoFrame(buffer, frameRotation, timestampNs); |
| 312 | ((VideoSink) listener).onFrame(frame); |
| 313 | frame.release(); |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | private void release() { |
Alex Glaznev | a5b62d9 | 2015-10-12 13:56:20 -0700 | [diff] [blame] | 317 | if (handler.getLooper().getThread() != Thread.currentThread()) { |
Magnus Jedvert | 1ab271c | 2015-09-28 11:05:44 +0200 | [diff] [blame] | 318 | throw new IllegalStateException("Wrong thread."); |
| 319 | } |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 320 | if (isTextureInUse || !isQuitting) { |
| 321 | throw new IllegalStateException("Unexpected release."); |
| 322 | } |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 323 | yuvConverter.release(); |
Magnus Jedvert | 1ab271c | 2015-09-28 11:05:44 +0200 | [diff] [blame] | 324 | GLES20.glDeleteTextures(1, new int[] {oesTextureId}, 0); |
| 325 | surfaceTexture.release(); |
| 326 | eglBase.release(); |
perkj | 9237559 | 2015-11-24 03:03:13 -0800 | [diff] [blame] | 327 | handler.getLooper().quit(); |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 328 | if (timestampAligner != null) { |
| 329 | timestampAligner.dispose(); |
| 330 | } |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 331 | } |
Magnus Jedvert | 4ae28a1 | 2015-09-15 09:44:07 +0200 | [diff] [blame] | 332 | } |