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

Side by Side Diff: content/browser/renderer_host/media/video_capture_manager.h

Issue 2738763002: [Mojo Video Capture] Introduce abstraction BuildableVideoCaptureDevice (Closed)
Patch Set: Incorporated suggestions from Patch Set 8 Created 3 years, 9 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 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // VideoCaptureManager is used to open/close, start/stop, enumerate available 5 // VideoCaptureManager is used to open/close, start/stop, enumerate available
6 // video capture devices, and manage VideoCaptureController's. 6 // video capture devices, and manage VideoCaptureController's.
7 // All functions are expected to be called from Browser::IO thread. Some helper 7 // All functions are expected to be called from Browser::IO thread. Some helper
8 // functions (*OnDeviceThread) will dispatch operations to the device thread. 8 // functions (*OnDeviceThread) will dispatch operations to the device thread.
9 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice. 9 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice.
10 // A device can only be opened once. 10 // A device can only be opened once.
11 11
12 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ 12 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
13 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ 13 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
14 14
15 #include <list> 15 #include <list>
16 #include <map> 16 #include <map>
17 #include <set> 17 #include <set>
18 #include <string> 18 #include <string>
19 19
20 #include "base/macros.h" 20 #include "base/macros.h"
21 #include "base/memory/ref_counted.h" 21 #include "base/memory/ref_counted.h"
22 #include "base/memory/weak_ptr.h" 22 #include "base/memory/weak_ptr.h"
23 #include "base/message_loop/message_loop.h" 23 #include "base/message_loop/message_loop.h"
24 #include "base/observer_list.h" 24 #include "base/observer_list.h"
25 #include "base/process/process_handle.h" 25 #include "base/process/process_handle.h"
26 #include "base/threading/thread_checker.h" 26 #include "base/threading/thread_checker.h"
27 #include "base/timer/elapsed_timer.h" 27 #include "base/timer/elapsed_timer.h"
28 #include "build/build_config.h" 28 #include "build/build_config.h"
29 #include "content/browser/renderer_host/media/buildable_video_capture_device.h"
29 #include "content/browser/renderer_host/media/media_stream_provider.h" 30 #include "content/browser/renderer_host/media/media_stream_provider.h"
30 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" 31 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h"
31 #include "content/common/content_export.h" 32 #include "content/common/content_export.h"
32 #include "content/common/media/media_stream_options.h" 33 #include "content/common/media/media_stream_options.h"
33 #include "media/base/video_facing.h" 34 #include "media/base/video_facing.h"
34 #include "media/capture/video/video_capture_device.h" 35 #include "media/capture/video/video_capture_device.h"
35 #include "media/capture/video/video_capture_device_factory.h" 36 #include "media/capture/video/video_capture_device_factory.h"
36 #include "media/capture/video_capture_types.h" 37 #include "media/capture/video_capture_types.h"
37 38
38 #if defined(OS_ANDROID) 39 #if defined(OS_ANDROID)
39 #include "base/android/application_status_listener.h" 40 #include "base/android/application_status_listener.h"
40 #endif 41 #endif
41 42
42 namespace content { 43 namespace content {
43 class VideoCaptureController; 44 class VideoCaptureController;
44 class VideoCaptureControllerEventHandler; 45 class VideoCaptureControllerEventHandler;
45 46
46 // VideoCaptureManager opens/closes and start/stops video capture devices. 47 // VideoCaptureManager opens/closes and start/stops video capture devices.
47 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider { 48 class CONTENT_EXPORT VideoCaptureManager
49 : public MediaStreamProvider,
50 public BuildableVideoCaptureDevice::Callbacks {
48 public: 51 public:
49 using VideoCaptureDevice = media::VideoCaptureDevice; 52 using VideoCaptureDevice = media::VideoCaptureDevice;
50 53
51 // Callback used to signal the completion of a controller lookup. 54 // Callback used to signal the completion of a controller lookup.
52 using DoneCB = 55 using DoneCB =
53 base::Callback<void(const base::WeakPtr<VideoCaptureController>&)>; 56 base::Callback<void(const base::WeakPtr<VideoCaptureController>&)>;
54 57
55 VideoCaptureManager( 58 VideoCaptureManager(
56 std::unique_ptr<media::VideoCaptureDeviceFactory> factory, 59 std::unique_ptr<media::VideoCaptureDeviceFactory> factory,
57 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner); 60 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 // Some devices had troubles when stopped and restarted quickly, so the device 192 // Some devices had troubles when stopped and restarted quickly, so the device
190 // is only stopped when Chrome is sent to background and not when, e.g., a tab 193 // is only stopped when Chrome is sent to background and not when, e.g., a tab
191 // is hidden, see http://crbug.com/582295. 194 // is hidden, see http://crbug.com/582295.
192 void OnApplicationStateChange(base::android::ApplicationState state); 195 void OnApplicationStateChange(base::android::ApplicationState state);
193 #endif 196 #endif
194 197
195 using EnumerationCallback = 198 using EnumerationCallback =
196 base::Callback<void(const media::VideoCaptureDeviceDescriptors&)>; 199 base::Callback<void(const media::VideoCaptureDeviceDescriptors&)>;
197 void EnumerateDevices(const EnumerationCallback& client_callback); 200 void EnumerateDevices(const EnumerationCallback& client_callback);
198 201
202 // Implementation of BuildableVideoCaptureDevice::Callbacks:
203 const media::VideoCaptureDeviceDescriptor* LookupDeviceDescriptor(
204 const std::string& id) override;
205 void WillStartDevice(media::VideoFacingMode facing_mode) override;
206 void DidStartDevice(VideoCaptureController* controller) override;
207 void OnDeviceStartFailed(VideoCaptureController* controller) override;
208
199 // Retrieves camera calibration information for a particular device. Returns 209 // Retrieves camera calibration information for a particular device. Returns
200 // nullopt_t if the |device_id| is not found or camera calibration information 210 // nullopt_t if the |device_id| is not found or camera calibration information
201 // is not available for the device. Camera calibration is cached during 211 // is not available for the device. Camera calibration is cached during
202 // device(s) enumeration. 212 // device(s) enumeration.
203 base::Optional<CameraCalibration> GetCameraCalibration( 213 base::Optional<CameraCalibration> GetCameraCalibration(
204 const std::string& device_id); 214 const std::string& device_id);
205 215
206 private: 216 private:
207 class CaptureDeviceStartRequest; 217 class CaptureDeviceStartRequest;
208 struct DeviceEntry;
209 struct DeviceInfo; 218 struct DeviceInfo;
210 219
211 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; 220 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>;
212 using DeviceEntries = std::vector<std::unique_ptr<DeviceEntry>>;
213 using DeviceInfos = std::vector<DeviceInfo>; 221 using DeviceInfos = std::vector<DeviceInfo>;
214 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; 222 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>;
215 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor; 223 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor;
216 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors; 224 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors;
217 225
218 ~VideoCaptureManager() override; 226 ~VideoCaptureManager() override;
219 227
220 // Helpers to report an event to our Listener. 228 // Helpers to report an event to our Listener.
221 void OnOpened(MediaStreamType type, 229 void OnOpened(MediaStreamType type,
222 media::VideoCaptureSessionId capture_session_id); 230 media::VideoCaptureSessionId capture_session_id);
223 void OnClosed(MediaStreamType type, 231 void OnClosed(MediaStreamType type,
224 media::VideoCaptureSessionId capture_session_id); 232 media::VideoCaptureSessionId capture_session_id);
225 void OnDevicesInfoEnumerated(base::ElapsedTimer* timer, 233 void OnDevicesInfoEnumerated(base::ElapsedTimer* timer,
226 const EnumerationCallback& client_callback, 234 const EnumerationCallback& client_callback,
227 const DeviceInfos& new_devices_info_cache); 235 const DeviceInfos& new_devices_info_cache);
228 236
229 bool IsOnDeviceThread() const;
230
231 // Consolidates the cached devices list with the list of currently connected 237 // Consolidates the cached devices list with the list of currently connected
232 // devices in the system |names_snapshot|. Retrieves the supported formats of 238 // devices in the system |names_snapshot|. Retrieves the supported formats of
233 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). 239 // the new devices and sends the new cache to OnDevicesInfoEnumerated().
234 void ConsolidateDevicesInfoOnDeviceThread( 240 void ConsolidateDevicesInfoOnDeviceThread(
235 base::Callback<void(const DeviceInfos&)> on_devices_enumerated_callback, 241 base::Callback<void(const DeviceInfos&)> on_devices_enumerated_callback,
236 const DeviceInfos& old_device_info_cache, 242 const DeviceInfos& old_device_info_cache,
237 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot); 243 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot);
238 244
239 // Checks to see if |entry| has no clients left on its controller. If so, 245 // Checks to see if |controller| has no clients left. If so, remove it from
240 // remove it from the list of devices, and delete it asynchronously. |entry| 246 // the list of controllers, and delete it asynchronously. |controller| may be
241 // may be freed by this function. 247 // freed by this function.
242 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); 248 void DestroyControllerIfNoClients(VideoCaptureController* controller);
243 249
244 // Finds a DeviceEntry in different ways: by |session_id|, by its |device_id| 250 // Finds a VideoCaptureController in different ways: by |session_id|, by its
245 // and |type| (if it is already opened), by its |controller| or by its 251 // |device_id| and |type| (if it is already opened), by its |controller| or by
246 // |serial_id|. In all cases, if not found, nullptr is returned. 252 // its |serial_id|. In all cases, if not found, nullptr is returned.
247 DeviceEntry* GetDeviceEntryBySessionId(int session_id); 253 VideoCaptureController* LookupControllerBySessionId(int session_id);
248 DeviceEntry* GetDeviceEntryByTypeAndId(MediaStreamType type, 254 VideoCaptureController* LookupControllerByTypeAndId(
249 const std::string& device_id) const; 255 MediaStreamType type,
250 DeviceEntry* GetDeviceEntryByController( 256 const std::string& device_id) const;
251 const VideoCaptureController* controller) const; 257 bool IsControllerPointerValid(const VideoCaptureController* controller) const;
252 DeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; 258 VideoCaptureController* LookupControllerBySerialId(int serial_id) const;
259 scoped_refptr<VideoCaptureController> GetControllerSharedRefFromSerialId(
260 int serial_id) const;
253 261
254 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. 262 // Finds the device info by |id| in |devices_info_cache_|, or nullptr.
255 DeviceInfo* GetDeviceInfoById(const std::string& id); 263 DeviceInfo* GetDeviceInfoById(const std::string& id);
256 264
257 // Finds a DeviceEntry entry for the indicated |capture_session_id|, creating 265 // Finds a VideoCaptureController for the indicated |capture_session_id|,
258 // a fresh one if necessary. Returns nullptr if said |capture_session_id| is 266 // creating a fresh one if necessary. Returns nullptr if said
259 // invalid. 267 // |capture_session_id| is invalid.
260 DeviceEntry* GetOrCreateDeviceEntry( 268 VideoCaptureController* GetOrCreateController(
261 media::VideoCaptureSessionId capture_session_id, 269 media::VideoCaptureSessionId capture_session_id,
262 const media::VideoCaptureParams& params); 270 const media::VideoCaptureParams& params);
263 271
264 // Starting a capture device can take 1-2 seconds. 272 // Starting a capture device can take 1-2 seconds.
265 // To avoid multiple unnecessary start/stop commands to the OS, each start 273 // To avoid multiple unnecessary start/stop commands to the OS, each start
266 // request is queued in |device_start_queue_|. 274 // request is queued in |device_start_queue_|.
267 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a 275 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a
268 // request to start the device on the device thread unless there is 276 // request to start the device on the device thread unless there is
269 // another request pending start. 277 // another request pending start.
270 void QueueStartDevice(media::VideoCaptureSessionId session_id, 278 void QueueStartDevice(media::VideoCaptureSessionId session_id,
271 DeviceEntry* entry, 279 VideoCaptureController* controller,
272 const media::VideoCaptureParams& params); 280 const media::VideoCaptureParams& params);
273 void OnDeviceStarted( 281 void DoStopDevice(VideoCaptureController* controller);
274 int serial_id,
275 std::unique_ptr<VideoCaptureDevice> device);
276 void DoStopDevice(DeviceEntry* entry);
277 void HandleQueuedStartRequest(); 282 void HandleQueuedStartRequest();
278 283
279 // Creates and Starts a new VideoCaptureDevice. The resulting
280 // VideoCaptureDevice is returned to the IO-thread and stored in
281 // a DeviceEntry in |devices_|. Ownership of |client| passes to
282 // the device.
283 std::unique_ptr<VideoCaptureDevice> DoStartDeviceCaptureOnDeviceThread(
284 const VideoCaptureDeviceDescriptor& descriptor,
285 const media::VideoCaptureParams& params,
286 std::unique_ptr<VideoCaptureDevice::Client> client);
287
288 std::unique_ptr<VideoCaptureDevice> DoStartTabCaptureOnDeviceThread(
289 const std::string& device_id,
290 const media::VideoCaptureParams& params,
291 std::unique_ptr<VideoCaptureDevice::Client> client);
292
293 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread(
294 const std::string& device_id,
295 const media::VideoCaptureParams& params,
296 std::unique_ptr<VideoCaptureDevice::Client> client);
297
298 // Stops and destroys the VideoCaptureDevice held in |device|.
299 void DoStopDeviceOnDeviceThread(std::unique_ptr<VideoCaptureDevice> device);
300
301 void MaybePostDesktopCaptureWindowId(media::VideoCaptureSessionId session_id); 284 void MaybePostDesktopCaptureWindowId(media::VideoCaptureSessionId session_id);
302 void SetDesktopCaptureWindowIdOnDeviceThread(
303 media::VideoCaptureDevice* device,
304 gfx::NativeViewId window_id);
305
306 // Internal versions of the Image Capture public ones, for delayed execution.
307 void DoGetPhotoCapabilities(
308 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback,
309 VideoCaptureDevice* device);
310 void DoSetPhotoOptions(
311 VideoCaptureDevice::SetPhotoOptionsCallback callback,
312 media::mojom::PhotoSettingsPtr settings,
313 VideoCaptureDevice* device);
314 void DoTakePhoto(VideoCaptureDevice::TakePhotoCallback callback,
315 VideoCaptureDevice* device);
316 285
317 #if defined(OS_ANDROID) 286 #if defined(OS_ANDROID)
318 void ReleaseDevices(); 287 void ReleaseDevices();
319 void ResumeDevices(); 288 void ResumeDevices();
320 289
321 std::unique_ptr<base::android::ApplicationStatusListener> 290 std::unique_ptr<base::android::ApplicationStatusListener>
322 app_status_listener_; 291 app_status_listener_;
323 bool application_state_has_running_activities_; 292 bool application_state_has_running_activities_;
324 #endif 293 #endif
325 294
326 // The message loop of media stream device thread, where VCD's live. 295 // The message loop of media stream device thread, where VCD's live.
327 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; 296 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_;
328 297
329 // Only accessed on Browser::IO thread. 298 // Only accessed on Browser::IO thread.
330 base::ObserverList<MediaStreamProviderListener> listeners_; 299 base::ObserverList<MediaStreamProviderListener> listeners_;
331 media::VideoCaptureSessionId new_capture_session_id_; 300 media::VideoCaptureSessionId new_capture_session_id_;
332 301
333 // An entry is kept in this map for every session that has been created via 302 // An entry is kept in this map for every session that has been created via
334 // the Open() entry point. The keys are session_id's. This map is used to 303 // the Open() entry point. The keys are session_id's. This map is used to
335 // determine which device to use when StartCaptureForClient() occurs. Used 304 // determine which device to use when StartCaptureForClient() occurs. Used
336 // only on the IO thread. 305 // only on the IO thread.
337 SessionMap sessions_; 306 SessionMap sessions_;
338 307
339 // Currently opened DeviceEntry instances (each owning a VideoCaptureDevice - 308 // Currently opened VideoCaptureController instances. The device may or may
340 // VideoCaptureController pair). The device may or may not be started. This 309 // not be started. This member is only accessed on IO thread.
341 // member is only accessed on IO thread. 310 std::vector<scoped_refptr<VideoCaptureController>> controllers_;
342 DeviceEntries devices_;
343 311
344 DeviceStartQueue device_start_queue_; 312 DeviceStartQueue device_start_queue_;
345 313
346 // Queue to keep photo-associated requests waiting for a device to initialize, 314 // Queue to keep photo-associated requests waiting for a device to initialize,
347 // bundles a session id integer and an associated photo-related request. 315 // bundles a session id integer and an associated photo-related request.
348 std::list<std::pair<int, base::Callback<void(media::VideoCaptureDevice*)>>> 316 std::list<std::pair<int, base::Closure>> photo_request_queue_;
349 photo_request_queue_;
350 317
351 // Device creation factory injected on construction from MediaStreamManager or 318 // Device creation factory injected on construction from MediaStreamManager or
352 // from the test harness. 319 // from the test harness.
353 std::unique_ptr<media::VideoCaptureDeviceFactory> 320 std::unique_ptr<media::VideoCaptureDeviceFactory>
354 video_capture_device_factory_; 321 video_capture_device_factory_;
355 322
356 base::ObserverList<media::VideoCaptureObserver> capture_observers_; 323 base::ObserverList<media::VideoCaptureObserver> capture_observers_;
357 324
358 // Local cache of the enumerated video capture devices' names and capture 325 // Local cache of the enumerated video capture devices' names and capture
359 // supported formats. A snapshot of the current devices and their capabilities 326 // supported formats. A snapshot of the current devices and their capabilities
360 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and 327 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and
361 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update 328 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update
362 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will 329 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will
363 // use this list if the device is not started, otherwise it will retrieve the 330 // use this list if the device is not started, otherwise it will retrieve the
364 // active device capture format from the VideoCaptureController associated. 331 // active device capture format from the VideoCaptureController associated.
365 DeviceInfos devices_info_cache_; 332 DeviceInfos devices_info_cache_;
366 333
367 // Map used by DesktopCapture. 334 // Map used by DesktopCapture.
368 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> 335 std::map<media::VideoCaptureSessionId, gfx::NativeViewId>
369 notification_window_ids_; 336 notification_window_ids_;
370 337
371 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); 338 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager);
372 }; 339 };
373 340
374 } // namespace content 341 } // namespace content
375 342
376 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ 343 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698