15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// VideoCaptureManager is used to open/close, start/stop, enumerate available 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// video capture devices, and manage VideoCaptureController's. 7d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// All functions are expected to be called from Browser::IO thread. Some helper 8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// functions (*OnDeviceThread) will dispatch operations to the device thread. 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// VideoCaptureManager will open OS dependent instances of VideoCaptureDevice. 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A device can only be opened once. 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map> 16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <set> 17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <string> 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h" 20d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/memory/weak_ptr.h" 2146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/message_loop/message_loop.h" 22d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/process/process_handle.h" 2346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/timer/elapsed_timer.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/renderer_host/media/media_stream_provider.h" 25d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "content/browser/renderer_host/media/video_capture_controller_event_handler.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/content_export.h" 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/media/media_stream_options.h" 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/video/capture/video_capture_device.h" 295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "media/video/capture/video_capture_device_factory.h" 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/video/capture/video_capture_types.h" 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content { 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class VideoCaptureController; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class VideoCaptureControllerEventHandler; 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// VideoCaptureManager opens/closes and start/stops video capture devices. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider { 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 39d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Callback used to signal the completion of a controller lookup. 40d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) typedef base::Callback< 41d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void(const base::WeakPtr<VideoCaptureController>&)> DoneCB; 42d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu explicit VideoCaptureManager( 445c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu scoped_ptr<media::VideoCaptureDeviceFactory> factory); 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Implements MediaStreamProvider. 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Register(MediaStreamProviderListener* listener, 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const scoped_refptr<base::SingleThreadTaskRunner>& 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) device_task_runner) OVERRIDE; 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Unregister() OVERRIDE; 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void EnumerateDevices(MediaStreamType stream_type) OVERRIDE; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int Open(const StreamDeviceInfo& device) OVERRIDE; 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Close(int capture_session_id) OVERRIDE; 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 59d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Called by VideoCaptureHost to locate a capture device for |capture_params|, 60d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // adding the Host as a client of the device's controller if successful. The 61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // value of |session_id| controls which device is selected; 62d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // this value should be a session id previously returned by Open(). 63d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // 64d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // If the device is not already started (i.e., no other client is currently 65d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // capturing from this device), this call will cause a VideoCaptureController 66d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // and VideoCaptureDevice to be created, possibly asynchronously. 67d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // 68d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // On success, the controller is returned via calling |done_cb|, indicating 69d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // that the client was successfully added. A NULL controller is passed to 70d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // the callback on failure. 71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) void StartCaptureForClient(media::VideoCaptureSessionId session_id, 72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const media::VideoCaptureParams& capture_params, 73d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) base::ProcessHandle client_render_process, 74d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) VideoCaptureControllerID client_id, 75d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) VideoCaptureControllerEventHandler* client_handler, 76d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) const DoneCB& done_cb); 77d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 78d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Called by VideoCaptureHost to remove |client_handler|. If this is the last 79d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // client of the device, the |controller| and its VideoCaptureDevice may be 80d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // destroyed. The client must not access |controller| after calling this 81d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // function. 82d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void StopCaptureForClient(VideoCaptureController* controller, 83d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) VideoCaptureControllerID client_id, 845c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu VideoCaptureControllerEventHandler* client_handler, 855c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu bool aborted_due_to_error); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // Called by VideoCaptureHost to pause to update video buffer specified by 8834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // |client_id| and |client_handler|. If all clients of |controller| are 8934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // paused, the corresponding device will be closed. 9034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) void PauseCaptureForClient( 9134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) VideoCaptureController* controller, 9234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) VideoCaptureControllerID client_id, 9334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) VideoCaptureControllerEventHandler* client_handler); 9434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) 9534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // Called by VideoCaptureHost to resume to update video buffer specified by 9634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // |client_id| and |client_handler|. The |session_id| and |params| should be 9734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // same as those used in StartCaptureForClient(). 9834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // If this is first active client of |controller|, device will be allocated 9934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // and it will take a little time to resume. 10034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // Allocating device could failed if other app holds the camera, the error 10134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) // will be notified through VideoCaptureControllerEventHandler::OnError(). 10234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) void ResumeCaptureForClient( 10334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) media::VideoCaptureSessionId session_id, 10434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) const media::VideoCaptureParams& params, 10534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) VideoCaptureController* controller, 10634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) VideoCaptureControllerID client_id, 10734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) VideoCaptureControllerEventHandler* client_handler); 10834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Retrieves all capture supported formats for a particular device. Returns 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // false if the |capture_session_id| is not found. The supported formats are 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // cached during device(s) enumeration, and depending on the underlying 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // implementation, could be an empty list. 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool GetDeviceSupportedFormats( 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) media::VideoCaptureSessionId capture_session_id, 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) media::VideoCaptureFormats* supported_formats); 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Retrieves the format(s) currently in use. Returns false if the 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // |capture_session_id| is not found. Returns true and |formats_in_use| 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // otherwise. |formats_in_use| is empty if the device is not in use. 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool GetDeviceFormatsInUse(media::VideoCaptureSessionId capture_session_id, 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) media::VideoCaptureFormats* formats_in_use); 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 123effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // Sets the platform-dependent window ID for the desktop capture notification 124effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // UI for the given session. 125effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch void SetDesktopCaptureWindowId(media::VideoCaptureSessionId session_id, 126effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch gfx::NativeViewId window_id); 127effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 1285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // Gets a weak reference to the device factory, used for tests. 1295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu media::VideoCaptureDeviceFactory* video_capture_device_factory() const { 1305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu return video_capture_device_factory_.get(); 1315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu } 1325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~VideoCaptureManager(); 135d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) struct DeviceEntry; 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // This data structure is a convenient wrap of a devices' name and associated 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // video capture supported formats. 1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) struct DeviceInfo { 1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DeviceInfo(); 1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DeviceInfo(const media::VideoCaptureDevice::Name& name, 1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const media::VideoCaptureFormats& supported_formats); 1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ~DeviceInfo(); 1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) media::VideoCaptureDevice::Name name; 1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) media::VideoCaptureFormats supported_formats; 1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) }; 1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) typedef std::vector<DeviceInfo> DeviceInfos; 1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 15046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Checks to see if |entry| has no clients left on its controller. If so, 151d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // remove it from the list of devices, and delete it asynchronously. |entry| 152d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // may be freed by this function. 153d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); 154d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 155d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Helpers to report an event to our Listener. 1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void OnOpened(MediaStreamType type, 1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) media::VideoCaptureSessionId capture_session_id); 1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void OnClosed(MediaStreamType type, 1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) media::VideoCaptureSessionId capture_session_id); 16046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) void OnDevicesInfoEnumerated(MediaStreamType stream_type, 16146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) base::ElapsedTimer* timer, 16246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const DeviceInfos& new_devices_info_cache); 163d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 16446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Finds a DeviceEntry by its device ID and type, if it is already opened. 165d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) DeviceEntry* GetDeviceEntryForMediaStreamDevice( 166d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) const MediaStreamDevice& device_info); 167d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 16846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Finds a DeviceEntry entry for the indicated session, creating a fresh one 169d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // if necessary. Returns NULL if the session id is invalid. 1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DeviceEntry* GetOrCreateDeviceEntry( 1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) media::VideoCaptureSessionId capture_session_id); 172d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 17346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Finds the DeviceEntry that owns a particular controller pointer. 174d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) DeviceEntry* GetDeviceEntryForController( 1755c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu const VideoCaptureController* controller) const; 176d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool IsOnDeviceThread() const; 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 17946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Consolidates the cached devices list with the list of currently connected 18046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // devices in the system |names_snapshot|. Retrieves the supported formats of 18146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // the new devices and sends the new cache to OnDevicesInfoEnumerated(). 18246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) void ConsolidateDevicesInfoOnDeviceThread( 18346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) base::Callback<void(const DeviceInfos&)> on_devices_enumerated_callback, 1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MediaStreamType stream_type, 18546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const DeviceInfos& old_device_info_cache, 18646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) scoped_ptr<media::VideoCaptureDevice::Names> names_snapshot); 187d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 18846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Creates and Starts a new VideoCaptureDevice, storing the result in 18968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // |entry->video_capture_device|. Ownership of |client| passes to 190d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // the device. 191d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void DoStartDeviceOnDeviceThread( 192effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch media::VideoCaptureSessionId session_id, 193d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) DeviceEntry* entry, 194f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const media::VideoCaptureParams& params, 19568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) scoped_ptr<media::VideoCaptureDevice::Client> client); 196d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 19746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Stops and destroys the VideoCaptureDevice held in 198d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // |entry->video_capture_device|. 199d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void DoStopDeviceOnDeviceThread(DeviceEntry* entry); 200d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DeviceInfo* FindDeviceInfoById(const std::string& id, 2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DeviceInfos& device_vector); 2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 204effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch void SetDesktopCaptureWindowIdOnDeviceThread(DeviceEntry* entry, 205effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch gfx::NativeViewId window_id); 206effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 207effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch void SaveDesktopCaptureWindowIdOnDeviceThread( 208effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch media::VideoCaptureSessionId session_id, 209effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch gfx::NativeViewId window_id); 210effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 211d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // The message loop of media stream device thread, where VCD's live. 2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Only accessed on Browser::IO thread. 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MediaStreamProviderListener* listener_; 2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) media::VideoCaptureSessionId new_capture_session_id_; 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu typedef std::map<media::VideoCaptureSessionId, MediaStreamDevice> SessionMap; 219d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // An entry is kept in this map for every session that has been created via 220d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // the Open() entry point. The keys are session_id's. This map is used to 221d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // determine which device to use when StartCaptureForClient() occurs. Used 222d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // only on the IO thread. 2235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu SessionMap sessions_; 224d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 225d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // An entry, kept in a map, that owns a VideoCaptureDevice and its associated 226d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices 227d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // and VideoCaptureControllers and is responsible for deleting the instances 228d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // when they are not used any longer. 229d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // 230d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // The set of currently started VideoCaptureDevice and VideoCaptureController 231d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // objects is only accessed from IO thread, though the DeviceEntry instances 232d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // themselves may visit to the device thread for device creation and 233d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // destruction. 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) struct DeviceEntry { 235d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) DeviceEntry(MediaStreamType stream_type, 236d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) const std::string& id, 237d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) scoped_ptr<VideoCaptureController> controller); 238d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) ~DeviceEntry(); 239d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 240d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) const MediaStreamType stream_type; 241d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) const std::string id; 242d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 243d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // The controller. Only used from the IO thread. 244d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) scoped_ptr<VideoCaptureController> video_capture_controller; 245d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 246d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // The capture device. Only used from the device thread. 247d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) scoped_ptr<media::VideoCaptureDevice> video_capture_device; 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 249d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) typedef std::set<DeviceEntry*> DeviceEntries; 250d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) DeviceEntries devices_; 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // Device creation factory injected on construction from MediaStreamManager or 2535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // from the test harness. 2545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu scoped_ptr<media::VideoCaptureDeviceFactory> video_capture_device_factory_; 2555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Local cache of the enumerated video capture devices' names and capture 2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // supported formats. A snapshot of the current devices and their capabilities 25846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and 25946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update 26046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will 2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // use this list if the device is not started, otherwise it will retrieve the 2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // active device capture format from the VideoCaptureController associated. 2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DeviceInfos devices_info_cache_; 2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 265effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // Accessed on the device thread only. 266effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch std::map<media::VideoCaptureSessionId, gfx::NativeViewId> 267effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch notification_window_ids_; 268effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace content 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ 275