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