Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: talk/app/webrtc/java/android/org/webrtc/SurfaceTextureHelper.java

Issue 1368093003: Android SurfaceTextureHelper: Don't wait for pending frames in disconnect() (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebased and added more comments Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * libjingle 2 * libjingle
3 * Copyright 2015 Google Inc. 3 * Copyright 2015 Google Inc.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met: 6 * modification, are permitted provided that the following conditions are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright notice, 8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer. 9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice, 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
(...skipping 17 matching lines...) Expand all
28 package org.webrtc; 28 package org.webrtc;
29 29
30 import android.graphics.SurfaceTexture; 30 import android.graphics.SurfaceTexture;
31 import android.opengl.EGLContext; 31 import android.opengl.EGLContext;
32 import android.opengl.GLES11Ext; 32 import android.opengl.GLES11Ext;
33 import android.opengl.GLES20; 33 import android.opengl.GLES20;
34 import android.os.Build; 34 import android.os.Build;
35 import android.os.Handler; 35 import android.os.Handler;
36 import android.os.HandlerThread; 36 import android.os.HandlerThread;
37 import android.os.SystemClock; 37 import android.os.SystemClock;
38 import android.util.Log;
39 38
40 import java.util.concurrent.CountDownLatch; 39 import java.util.concurrent.CountDownLatch;
41 import java.util.concurrent.TimeUnit; 40 import java.util.concurrent.TimeUnit;
42 41
43 /** 42 /**
44 * Helper class to create and synchronize access to a SurfaceTexture. The caller will get notified 43 * Helper class to create and synchronize access to a SurfaceTexture. The caller will get notified
45 * of new frames in onTextureFrameAvailable(), and should call returnTextureFram e() when done with 44 * of new frames in onTextureFrameAvailable(), and should call returnTextureFram e() when done with
46 * the frame. Only one texture frame can be in flight at once, so returnTextureF rame() must be 45 * the frame. Only one texture frame can be in flight at once, so returnTextureF rame() must be
47 * called in order to receive a new frame. Call disconnect() to stop receiveing new frames and 46 * called in order to receive a new frame. Call disconnect() to stop receiveing new frames and
48 * release all resources. 47 * release all resources.
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 final CountDownLatch barrier = new CountDownLatch(1); 151 final CountDownLatch barrier = new CountDownLatch(1);
153 handler.postAtFrontOfQueue(new Runnable() { 152 handler.postAtFrontOfQueue(new Runnable() {
154 @Override public void run() { 153 @Override public void run() {
155 isQuitting = true; 154 isQuitting = true;
156 barrier.countDown(); 155 barrier.countDown();
157 if (!isTextureInUse) { 156 if (!isTextureInUse) {
158 release(); 157 release();
159 } 158 }
160 } 159 }
161 }); 160 });
162 boolean wasInterrupted = true; 161 ThreadUtils.awaitUninterruptibly(barrier);
163 while(true) {
164 try {
165 barrier.await();
166 break;
167 } catch (InterruptedException e) {
168 // Someone is asking us to return early at our convenience. We must wait until the
169 // |isQuitting| flag has been set but we should preserve the information and pass it along.
170 wasInterrupted = true;
171 }
172 }
173 if (wasInterrupted) {
174 Thread.currentThread().interrupt();
175 }
176 } 162 }
177 163
178 private void tryDeliverTextureFrame() { 164 private void tryDeliverTextureFrame() {
179 if (Thread.currentThread() != thread) { 165 if (Thread.currentThread() != thread) {
180 throw new IllegalStateException("Wrong thread."); 166 throw new IllegalStateException("Wrong thread.");
181 } 167 }
182 if (isQuitting || !hasPendingTexture || isTextureInUse) { 168 if (isQuitting || !hasPendingTexture || isTextureInUse) {
183 return; 169 return;
184 } 170 }
185 isTextureInUse = true; 171 isTextureInUse = true;
186 hasPendingTexture = false; 172 hasPendingTexture = false;
187 173
188 surfaceTexture.updateTexImage(); 174 surfaceTexture.updateTexImage();
189 final float[] transformMatrix = new float[16]; 175 final float[] transformMatrix = new float[16];
190 surfaceTexture.getTransformMatrix(transformMatrix); 176 surfaceTexture.getTransformMatrix(transformMatrix);
191 final long timestampNs = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_C REAM_SANDWICH) 177 final long timestampNs = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_C REAM_SANDWICH)
192 ? surfaceTexture.getTimestamp() 178 ? surfaceTexture.getTimestamp()
193 : TimeUnit.MILLISECONDS.toNanos(SystemClock.elapsedRealtime()); 179 : TimeUnit.MILLISECONDS.toNanos(SystemClock.elapsedRealtime());
194 listener.onTextureFrameAvailable(oesTextureId, transformMatrix, timestampNs) ; 180 listener.onTextureFrameAvailable(oesTextureId, transformMatrix, timestampNs) ;
195 } 181 }
196 182
197 private void release() { 183 private void release() {
184 if (Thread.currentThread() != thread) {
185 throw new IllegalStateException("Wrong thread.");
186 }
198 if (isTextureInUse || !isQuitting) { 187 if (isTextureInUse || !isQuitting) {
199 throw new IllegalStateException("Unexpected release."); 188 throw new IllegalStateException("Unexpected release.");
200 } 189 }
201 // Release GL resources on dedicated thread. 190 GLES20.glDeleteTextures(1, new int[] {oesTextureId}, 0);
202 handler.post(new Runnable() { 191 surfaceTexture.release();
203 @Override public void run() { 192 eglBase.release();
204 GLES20.glDeleteTextures(1, new int[] {oesTextureId}, 0);
205 surfaceTexture.release();
206 eglBase.release();
207 }
208 });
209 // Quit safely to make sure the clean-up posted above is executed.
210 thread.quitSafely(); 193 thread.quitSafely();
211 } 194 }
212 } 195 }
OLDNEW
« no previous file with comments | « talk/app/webrtc/java/android/org/webrtc/EglBase.java ('k') | talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698