Ignore non-active fullscreen windows for shelf state.
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_manager.h
blob82c61537ddf4ae17c735ca8c04dc14b4c41ecf13
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
3 // found in the LICENSE file.
5 // VideoCaptureManager is used to open/close, start/stop, enumerate available
6 // video capture devices, and manage VideoCaptureController's.
7 // All functions are expected to be called from Browser::IO thread. Some helper
8 // functions (*OnDeviceThread) will dispatch operations to the device thread.
9 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice.
10 // A device can only be opened once.
12 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
13 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
15 #include <map>
16 #include <set>
17 #include <string>
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/process/process_handle.h"
22 #include "content/browser/renderer_host/media/media_stream_provider.h"
23 #include "content/browser/renderer_host/media/video_capture_controller_event_handler.h"
24 #include "content/common/content_export.h"
25 #include "content/common/media/media_stream_options.h"
26 #include "media/video/capture/video_capture_device.h"
27 #include "media/video/capture/video_capture_types.h"
29 namespace content {
30 class VideoCaptureController;
31 class VideoCaptureControllerEventHandler;
33 // VideoCaptureManager opens/closes and start/stops video capture devices.
34 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider {
35 public:
36 // Callback used to signal the completion of a controller lookup.
37 typedef base::Callback<
38 void(const base::WeakPtr<VideoCaptureController>&)> DoneCB;
40 VideoCaptureManager();
42 // Implements MediaStreamProvider.
43 virtual void Register(MediaStreamProviderListener* listener,
44 base::MessageLoopProxy* device_thread_loop) OVERRIDE;
46 virtual void Unregister() OVERRIDE;
48 virtual void EnumerateDevices(MediaStreamType stream_type) OVERRIDE;
50 virtual int Open(const StreamDeviceInfo& device) OVERRIDE;
52 virtual void Close(int capture_session_id) OVERRIDE;
54 // Used by unit test to make sure a fake device is used instead of a real
55 // video capture device. Due to timing requirements, the function must be
56 // called before EnumerateDevices and Open.
57 void UseFakeDevice();
59 // Called by VideoCaptureHost to locate a capture device for |capture_params|,
60 // adding the Host as a client of the device's controller if successful. The
61 // value of |session_id| controls which device is selected;
62 // this value should be a session id previously returned by Open().
64 // If the device is not already started (i.e., no other client is currently
65 // capturing from this device), this call will cause a VideoCaptureController
66 // and VideoCaptureDevice to be created, possibly asynchronously.
68 // On success, the controller is returned via calling |done_cb|, indicating
69 // that the client was successfully added. A NULL controller is passed to
70 // the callback on failure.
71 void StartCaptureForClient(media::VideoCaptureSessionId session_id,
72 const media::VideoCaptureParams& capture_params,
73 base::ProcessHandle client_render_process,
74 VideoCaptureControllerID client_id,
75 VideoCaptureControllerEventHandler* client_handler,
76 const DoneCB& done_cb);
78 // Called by VideoCaptureHost to remove |client_handler|. If this is the last
79 // client of the device, the |controller| and its VideoCaptureDevice may be
80 // destroyed. The client must not access |controller| after calling this
81 // function.
82 void StopCaptureForClient(VideoCaptureController* controller,
83 VideoCaptureControllerID client_id,
84 VideoCaptureControllerEventHandler* client_handler);
86 private:
87 virtual ~VideoCaptureManager();
88 struct DeviceEntry;
90 // Check to see if |entry| has no clients left on its controller. If so,
91 // remove it from the list of devices, and delete it asynchronously. |entry|
92 // may be freed by this function.
93 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry);
95 // Helpers to report an event to our Listener.
96 void OnOpened(MediaStreamType type, int capture_session_id);
97 void OnClosed(MediaStreamType type, int capture_session_id);
98 void OnDevicesEnumerated(MediaStreamType stream_type,
99 const media::VideoCaptureDevice::Names& names);
101 // Find a DeviceEntry by its device ID and type, if it is already opened.
102 DeviceEntry* GetDeviceEntryForMediaStreamDevice(
103 const MediaStreamDevice& device_info);
105 // Find a DeviceEntry entry for the indicated session, creating a fresh one
106 // if necessary. Returns NULL if the session id is invalid.
107 DeviceEntry* GetOrCreateDeviceEntry(int capture_session_id);
109 // Find the DeviceEntry that owns a particular controller pointer.
110 DeviceEntry* GetDeviceEntryForController(
111 const VideoCaptureController* controller);
113 bool IsOnDeviceThread() const;
115 // Queries and returns the available device IDs.
116 media::VideoCaptureDevice::Names GetAvailableDevicesOnDeviceThread(
117 MediaStreamType stream_type);
119 // Create and Start a new VideoCaptureDevice, storing the result in
120 // |entry->video_capture_device|. Ownership of |client| passes to
121 // the device.
122 void DoStartDeviceOnDeviceThread(
123 DeviceEntry* entry,
124 const media::VideoCaptureParams& params,
125 scoped_ptr<media::VideoCaptureDevice::Client> client);
127 // Stop and destroy the VideoCaptureDevice held in
128 // |entry->video_capture_device|.
129 void DoStopDeviceOnDeviceThread(DeviceEntry* entry);
131 // The message loop of media stream device thread, where VCD's live.
132 scoped_refptr<base::MessageLoopProxy> device_loop_;
134 // Only accessed on Browser::IO thread.
135 MediaStreamProviderListener* listener_;
136 int new_capture_session_id_;
138 // An entry is kept in this map for every session that has been created via
139 // the Open() entry point. The keys are session_id's. This map is used to
140 // determine which device to use when StartCaptureForClient() occurs. Used
141 // only on the IO thread.
142 std::map<int, MediaStreamDevice> sessions_;
144 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated
145 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices
146 // and VideoCaptureControllers and is responsible for deleting the instances
147 // when they are not used any longer.
149 // The set of currently started VideoCaptureDevice and VideoCaptureController
150 // objects is only accessed from IO thread, though the DeviceEntry instances
151 // themselves may visit to the device thread for device creation and
152 // destruction.
153 struct DeviceEntry {
154 DeviceEntry(MediaStreamType stream_type,
155 const std::string& id,
156 scoped_ptr<VideoCaptureController> controller);
157 ~DeviceEntry();
159 const MediaStreamType stream_type;
160 const std::string id;
162 // The controller. Only used from the IO thread.
163 scoped_ptr<VideoCaptureController> video_capture_controller;
165 // The capture device. Only used from the device thread.
166 scoped_ptr<media::VideoCaptureDevice> video_capture_device;
168 typedef std::set<DeviceEntry*> DeviceEntries;
169 DeviceEntries devices_;
171 // For unit testing and for performance/quality tests, a test device can be
172 // used instead of a real one. The device can be a simple fake device (a
173 // rolling pacman), or a file that is played in a loop continuously. This only
174 // applies to the MEDIA_DEVICE_VIDEO_CAPTURE device type.
175 enum {
176 DISABLED,
177 TEST_PATTERN,
178 Y4M_FILE
179 } artificial_device_source_for_testing_;
181 // We cache the enumerated video capture devices in
182 // GetAvailableDevicesOnDeviceThread() and then later look up the requested ID
183 // when a device is created in DoStartDeviceOnDeviceThread(). Used only on the
184 // device thread.
185 media::VideoCaptureDevice::Names video_capture_devices_;
187 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager);
190 } // namespace content
192 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_