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

Side by Side Diff: webrtc/api/android/java/src/org/webrtc/CameraCapturer.java

Issue 2257123002: Add a timeout for starting the camera on CameraCapturer. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2016 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
11 package org.webrtc; 11 package org.webrtc;
12 12
13 import android.content.Context; 13 import android.content.Context;
14 import android.os.Handler; 14 import android.os.Handler;
15 import android.os.Looper;
15 16
16 import java.util.Arrays; 17 import java.util.Arrays;
17 18
18 @SuppressWarnings("deprecation") 19 @SuppressWarnings("deprecation")
19 public abstract class CameraCapturer implements CameraVideoCapturer { 20 public abstract class CameraCapturer implements CameraVideoCapturer {
20 enum SwitchState { 21 enum SwitchState {
21 IDLE, // No switch requested. 22 IDLE, // No switch requested.
22 PENDING, // Waiting for previous capture session to open. 23 PENDING, // Waiting for previous capture session to open.
23 IN_PROGRESS, // Waiting for new switched capture session to start. 24 IN_PROGRESS, // Waiting for new switched capture session to start.
24 } 25 }
25 26
26 private static final String TAG = "CameraCapturer"; 27 private static final String TAG = "CameraCapturer";
27 private final static int MAX_OPEN_CAMERA_ATTEMPTS = 3; 28 private final static int MAX_OPEN_CAMERA_ATTEMPTS = 3;
28 private final static int OPEN_CAMERA_DELAY_MS = 500; 29 private final static int OPEN_CAMERA_DELAY_MS = 500;
30 private final static int OPEN_CAMERA_TIMEOUT = 10000;
29 31
30 private final CameraEnumerator cameraEnumerator; 32 private final CameraEnumerator cameraEnumerator;
31 private final CameraEventsHandler eventsHandler; 33 private final CameraEventsHandler eventsHandler;
34 private final Handler uiThreadHandler;
32 35
33 private final CameraSession.CreateSessionCallback createSessionCallback = 36 private final CameraSession.CreateSessionCallback createSessionCallback =
34 new CameraSession.CreateSessionCallback() { 37 new CameraSession.CreateSessionCallback() {
35 @Override 38 @Override
36 public void onDone(CameraSession session) { 39 public void onDone(CameraSession session) {
37 Logging.d(TAG, "Create session done"); 40 Logging.d(TAG, "Create session done");
38 41 uiThreadHandler.removeCallbacks(openCameraTimeoutRunnable);
39 synchronized (stateLock) { 42 synchronized (stateLock) {
40 sessionOpening = false; 43 sessionOpening = false;
41 currentSession = session; 44 currentSession = session;
42 stateLock.notifyAll(); 45 stateLock.notifyAll();
43 46
44 if (switchState == SwitchState.IN_PROGRESS) { 47 if (switchState == SwitchState.IN_PROGRESS) {
45 if (switchEventsHandler != null) { 48 if (switchEventsHandler != null) {
46 switchEventsHandler.onCameraSwitchDone( 49 switchEventsHandler.onCameraSwitchDone(
47 cameraEnumerator.isFrontFacing(cameraName)); 50 cameraEnumerator.isFrontFacing(cameraName));
48 switchEventsHandler = null; 51 switchEventsHandler = null;
49 } 52 }
50 switchState = SwitchState.IDLE; 53 switchState = SwitchState.IDLE;
51 } else if (switchState == SwitchState.PENDING) { 54 } else if (switchState == SwitchState.PENDING) {
52 switchState = SwitchState.IDLE; 55 switchState = SwitchState.IDLE;
53 switchCameraInternal(switchEventsHandler); 56 switchCameraInternal(switchEventsHandler);
54 } 57 }
55 } 58 }
56 } 59 }
57 60
58 @Override 61 @Override
59 public void onFailure(String error) { 62 public void onFailure(String error) {
63 uiThreadHandler.removeCallbacks(openCameraTimeoutRunnable);
60 synchronized (stateLock) { 64 synchronized (stateLock) {
61 openAttemptsRemaining--; 65 openAttemptsRemaining--;
62 66
63 if (openAttemptsRemaining <= 0) { 67 if (openAttemptsRemaining <= 0) {
64 Logging.w(TAG, "Opening camera failed, passing: " + error); 68 Logging.w(TAG, "Opening camera failed, passing: " + error);
65 sessionOpening = false; 69 sessionOpening = false;
66 stateLock.notifyAll(); 70 stateLock.notifyAll();
67 71
68 if (switchState != SwitchState.IDLE) { 72 if (switchState != SwitchState.IDLE) {
69 if (switchEventsHandler != null) { 73 if (switchEventsHandler != null) {
70 switchEventsHandler.onCameraSwitchError(error); 74 switchEventsHandler.onCameraSwitchError(error);
71 switchEventsHandler = null; 75 switchEventsHandler = null;
72 } 76 }
73 switchState = SwitchState.IDLE; 77 switchState = SwitchState.IDLE;
74 } 78 }
75 79
76 eventsHandler.onCameraError(error); 80 eventsHandler.onCameraError(error);
77 } else { 81 } else {
78 Logging.w(TAG, "Opening camera failed, retry: " + error); 82 Logging.w(TAG, "Opening camera failed, retry: " + error);
79 83
80 createSessionInternal(OPEN_CAMERA_DELAY_MS); 84 createSessionInternal(OPEN_CAMERA_DELAY_MS);
81 } 85 }
82 } 86 }
83 } 87 }
84 }; 88 };
85 89
90 private final Runnable openCameraTimeoutRunnable = new Runnable() {
91 @Override
92 public void run() {
93 eventsHandler.onCameraError("Camera failed to start within timeout.");
94 }
95 };
96
86 // Initialized on initialize 97 // Initialized on initialize
87 // ------------------------- 98 // -------------------------
88 private Handler cameraThreadHandler; 99 private Handler cameraThreadHandler;
89 private Context applicationContext; 100 private Context applicationContext;
90 private CapturerObserver capturerObserver; 101 private CapturerObserver capturerObserver;
91 private SurfaceTextureHelper surfaceHelper; 102 private SurfaceTextureHelper surfaceHelper;
92 103
93 private final Object stateLock = new Object(); 104 private final Object stateLock = new Object();
94 private boolean sessionOpening; /* guarded by stateLock */ 105 private boolean sessionOpening; /* guarded by stateLock */
95 private CameraSession currentSession; /* guarded by stateLock */ 106 private CameraSession currentSession; /* guarded by stateLock */
(...skipping 18 matching lines...) Expand all
114 @Override 125 @Override
115 public void onFirstFrameAvailable() {} 126 public void onFirstFrameAvailable() {}
116 @Override 127 @Override
117 public void onCameraClosed() {} 128 public void onCameraClosed() {}
118 }; 129 };
119 } 130 }
120 131
121 this.eventsHandler = eventsHandler; 132 this.eventsHandler = eventsHandler;
122 this.cameraEnumerator = cameraEnumerator; 133 this.cameraEnumerator = cameraEnumerator;
123 this.cameraName = cameraName; 134 this.cameraName = cameraName;
135 uiThreadHandler = new Handler(Looper.getMainLooper());
124 136
125 final String[] deviceNames = cameraEnumerator.getDeviceNames(); 137 final String[] deviceNames = cameraEnumerator.getDeviceNames();
126 138
127 if (deviceNames.length == 0) { 139 if (deviceNames.length == 0) {
128 throw new RuntimeException("No cameras attached."); 140 throw new RuntimeException("No cameras attached.");
129 } 141 }
130 if (!Arrays.asList(deviceNames).contains(this.cameraName)) { 142 if (!Arrays.asList(deviceNames).contains(this.cameraName)) {
131 throw new IllegalArgumentException( 143 throw new IllegalArgumentException(
132 "Camera name " + this.cameraName + " does not match any known camera d evice."); 144 "Camera name " + this.cameraName + " does not match any known camera d evice.");
133 } 145 }
(...skipping 23 matching lines...) Expand all
157 this.height = height; 169 this.height = height;
158 this.framerate = framerate; 170 this.framerate = framerate;
159 171
160 sessionOpening = true; 172 sessionOpening = true;
161 openAttemptsRemaining = MAX_OPEN_CAMERA_ATTEMPTS; 173 openAttemptsRemaining = MAX_OPEN_CAMERA_ATTEMPTS;
162 createSessionInternal(0); 174 createSessionInternal(0);
163 } 175 }
164 } 176 }
165 177
166 private void createSessionInternal(int delayMs) { 178 private void createSessionInternal(int delayMs) {
179 uiThreadHandler.postDelayed(openCameraTimeoutRunnable, delayMs + OPEN_CAMERA _TIMEOUT);
magjed_webrtc 2016/08/18 13:46:36 Maybe post it inside the runnable below together w
sakal 2016/08/18 13:48:17 That would break the purpose of this CL. If the ca
magjed_webrtc 2016/08/18 14:19:42 Ah, right :)
167 cameraThreadHandler.postDelayed(new Runnable() { 180 cameraThreadHandler.postDelayed(new Runnable() {
168 @Override 181 @Override
169 public void run() { 182 public void run() {
170 createCameraSession( 183 createCameraSession(
171 createSessionCallback, 184 createSessionCallback,
172 eventsHandler, applicationContext, capturerObserver, surfaceHelper, 185 eventsHandler, applicationContext, capturerObserver, surfaceHelper,
173 cameraName, width, height, framerate); 186 cameraName, width, height, framerate);
174 } 187 }
175 }, delayMs); 188 }, delayMs);
176 } 189 }
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 } 304 }
292 } 305 }
293 306
294 abstract protected void createCameraSession( 307 abstract protected void createCameraSession(
295 CameraSession.CreateSessionCallback createSessionCallback, 308 CameraSession.CreateSessionCallback createSessionCallback,
296 CameraEventsHandler eventsHandler, Context applicationContext, 309 CameraEventsHandler eventsHandler, Context applicationContext,
297 CameraVideoCapturer.CapturerObserver capturerObserver, 310 CameraVideoCapturer.CapturerObserver capturerObserver,
298 SurfaceTextureHelper surfaceTextureHelper, 311 SurfaceTextureHelper surfaceTextureHelper,
299 String cameraName, int width, int height, int framerate); 312 String cameraName, int width, int height, int framerate);
300 } 313 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698