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