Index: webrtc/api/android/java/src/org/webrtc/Camera2Session.java |
diff --git a/webrtc/api/android/java/src/org/webrtc/Camera2Session.java b/webrtc/api/android/java/src/org/webrtc/Camera2Session.java |
index 5e32c27102f95dc01dcf1b678a5e346aa61c5c82..9bcdfafa204c55907b72af1a789ba900b0ced421 100644 |
--- a/webrtc/api/android/java/src/org/webrtc/Camera2Session.java |
+++ b/webrtc/api/android/java/src/org/webrtc/Camera2Session.java |
@@ -318,21 +318,35 @@ public class Camera2Session implements CameraSession { |
@Override |
public void stop() { |
Logging.d(TAG, "Stop camera2 session on camera " + cameraId); |
- final CountDownLatch stopLatch = new CountDownLatch(1); |
- |
- cameraThreadHandler.post(new Runnable() { |
- @Override |
- public void run() { |
- if (state != SessionState.STOPPED) { |
- state = SessionState.STOPPED; |
- capturerObserver.onCapturerStopped(); |
- stopLatch.countDown(); |
- stopInternal(); |
- } |
+ if (Thread.currentThread() == cameraThreadHandler.getLooper().getThread()) { |
+ if (state != SessionState.STOPPED) { |
+ state = SessionState.STOPPED; |
+ capturerObserver.onCapturerStopped(); |
+ // Post the stopInternal to return earlier. |
+ cameraThreadHandler.post(new Runnable() { |
+ @Override |
+ public void run() { |
+ stopInternal(); |
+ } |
+ }); |
} |
- }); |
+ } else { |
+ final CountDownLatch stopLatch = new CountDownLatch(1); |
+ |
+ cameraThreadHandler.post(new Runnable() { |
+ @Override |
+ public void run() { |
+ if (state != SessionState.STOPPED) { |
+ state = SessionState.STOPPED; |
+ capturerObserver.onCapturerStopped(); |
+ stopLatch.countDown(); |
+ stopInternal(); |
+ } |
+ } |
+ }); |
- ThreadUtils.awaitUninterruptibly(stopLatch); |
+ ThreadUtils.awaitUninterruptibly(stopLatch); |
+ } |
} |
private void stopInternal() { |