OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 surfaceTexture = new SurfaceTexture(oesTextureId); | 127 surfaceTexture = new SurfaceTexture(oesTextureId); |
128 surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailab
leListener() { | 128 surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailab
leListener() { |
129 @Override | 129 @Override |
130 public void onFrameAvailable(SurfaceTexture surfaceTexture) { | 130 public void onFrameAvailable(SurfaceTexture surfaceTexture) { |
131 hasPendingTexture = true; | 131 hasPendingTexture = true; |
132 tryDeliverTextureFrame(); | 132 tryDeliverTextureFrame(); |
133 } | 133 } |
134 }); | 134 }); |
135 } | 135 } |
136 | 136 |
137 private YuvConverter getYuvConverter() { | |
138 // yuvConverter is assigned once | |
139 if (yuvConverter != null) | |
140 return yuvConverter; | |
141 | |
142 synchronized (this) { | |
143 if (yuvConverter == null) | |
144 yuvConverter = new YuvConverter(eglBase.getEglBaseContext()); | |
145 return yuvConverter; | |
146 } | |
147 } | |
148 | |
149 /** | 137 /** |
150 * Start to stream textures to the given |listener|. If you need to change lis
tener, you need to | 138 * Start to stream textures to the given |listener|. If you need to change lis
tener, you need to |
151 * call stopListening() first. | 139 * call stopListening() first. |
152 */ | 140 */ |
153 public void startListening(final OnTextureFrameAvailableListener listener) { | 141 public void startListening(final OnTextureFrameAvailableListener listener) { |
154 if (this.listener != null || this.pendingListener != null) { | 142 if (this.listener != null || this.pendingListener != null) { |
155 throw new IllegalStateException("SurfaceTextureHelper listener has already
been set."); | 143 throw new IllegalStateException("SurfaceTextureHelper listener has already
been set."); |
156 } | 144 } |
157 this.pendingListener = listener; | 145 this.pendingListener = listener; |
158 handler.post(setListenerRunnable); | 146 handler.post(setListenerRunnable); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 @Override | 212 @Override |
225 public void run() { | 213 public void run() { |
226 isQuitting = true; | 214 isQuitting = true; |
227 if (!isTextureInUse) { | 215 if (!isTextureInUse) { |
228 release(); | 216 release(); |
229 } | 217 } |
230 } | 218 } |
231 }); | 219 }); |
232 } | 220 } |
233 | 221 |
234 public void textureToYUV( | 222 public void textureToYUV(final ByteBuffer buf, final int width, final int heig
ht, |
235 ByteBuffer buf, int width, int height, int stride, int textureId, float[]
transformMatrix) { | 223 final int stride, final int textureId, final float[] transformMatrix) { |
236 if (textureId != oesTextureId) | 224 if (textureId != oesTextureId) { |
237 throw new IllegalStateException("textureToByteBuffer called with unexpecte
d textureId"); | 225 throw new IllegalStateException("textureToByteBuffer called with unexpecte
d textureId"); |
| 226 } |
238 | 227 |
239 getYuvConverter().convert(buf, width, height, stride, textureId, transformMa
trix); | 228 ThreadUtils.invokeAtFrontUninterruptibly(handler, new Runnable() { |
| 229 @Override |
| 230 public void run() { |
| 231 if (yuvConverter == null) { |
| 232 yuvConverter = new YuvConverter(); |
| 233 } |
| 234 yuvConverter.convert(buf, width, height, stride, textureId, transformMat
rix); |
| 235 } |
| 236 }); |
240 } | 237 } |
241 | 238 |
242 private void updateTexImage() { | 239 private void updateTexImage() { |
243 // SurfaceTexture.updateTexImage apparently can compete and deadlock with eg
lSwapBuffers, | 240 // SurfaceTexture.updateTexImage apparently can compete and deadlock with eg
lSwapBuffers, |
244 // as observed on Nexus 5. Therefore, synchronize it with the EGL functions. | 241 // as observed on Nexus 5. Therefore, synchronize it with the EGL functions. |
245 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=5702 for more inf
o. | 242 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=5702 for more inf
o. |
246 synchronized (EglBase.lock) { | 243 synchronized (EglBase.lock) { |
247 surfaceTexture.updateTexImage(); | 244 surfaceTexture.updateTexImage(); |
248 } | 245 } |
249 } | 246 } |
(...skipping 18 matching lines...) Expand all Loading... |
268 listener.onTextureFrameAvailable(oesTextureId, transformMatrix, timestampNs)
; | 265 listener.onTextureFrameAvailable(oesTextureId, transformMatrix, timestampNs)
; |
269 } | 266 } |
270 | 267 |
271 private void release() { | 268 private void release() { |
272 if (handler.getLooper().getThread() != Thread.currentThread()) { | 269 if (handler.getLooper().getThread() != Thread.currentThread()) { |
273 throw new IllegalStateException("Wrong thread."); | 270 throw new IllegalStateException("Wrong thread."); |
274 } | 271 } |
275 if (isTextureInUse || !isQuitting) { | 272 if (isTextureInUse || !isQuitting) { |
276 throw new IllegalStateException("Unexpected release."); | 273 throw new IllegalStateException("Unexpected release."); |
277 } | 274 } |
278 synchronized (this) { | 275 if (yuvConverter != null) { |
279 if (yuvConverter != null) | 276 yuvConverter.release(); |
280 yuvConverter.release(); | |
281 } | 277 } |
282 GLES20.glDeleteTextures(1, new int[] {oesTextureId}, 0); | 278 GLES20.glDeleteTextures(1, new int[] {oesTextureId}, 0); |
283 surfaceTexture.release(); | 279 surfaceTexture.release(); |
284 eglBase.release(); | 280 eglBase.release(); |
285 handler.getLooper().quit(); | 281 handler.getLooper().quit(); |
286 } | 282 } |
287 } | 283 } |
OLD | NEW |