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. 4 5 // TODO(hclam): This class should be renamed to VideoCaptureService. 6 7 // This class provides access to a video capture device in the browser 8 // process through IPC. The main function is to deliver video frames 9 // to a client. 10 // 11 // THREADING 12 // 13 // VideoCaptureImplManager lives only on the render thread. All methods 14 // must be called on this thread. 15 // 16 // VideoFrames are delivered on the IO thread. Callbacks provided by 17 // a client are also called on the IO thread. 18 19 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ 20 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ 21 22 #include <map> 23 24 #include "base/callback.h" 25 #include "base/memory/linked_ptr.h" 26 #include "base/memory/ref_counted.h" 27 #include "base/memory/scoped_ptr.h" 28 #include "base/memory/weak_ptr.h" 29 #include "base/message_loop/message_loop_proxy.h" 30 #include "base/synchronization/lock.h" 31 #include "base/threading/thread_checker.h" 32 #include "content/common/content_export.h" 33 #include "content/common/media/video_capture.h" 34 #include "content/public/renderer/media_stream_video_sink.h" 35 #include "media/video/capture/video_capture_types.h" 36 37 namespace content { 38 39 class VideoCaptureImpl; 40 class VideoCaptureMessageFilter; 41 42 class CONTENT_EXPORT VideoCaptureImplManager { 43 public: 44 VideoCaptureImplManager(); 45 virtual ~VideoCaptureImplManager(); 46 47 // Open a device associated with the session ID. 48 // This method must be called before any methods with the same ID 49 // is used. 50 // Returns a callback that should be used to release the acquired 51 // resources. 52 base::Closure UseDevice(media::VideoCaptureSessionId id); 53 54 // Start receiving video frames for the given session ID. 55 // 56 // |state_update_cb| will be called on the IO thread when capturing 57 // state changes. 58 // States will be one of the following four: 59 // * VIDEO_CAPTURE_STATE_STARTED 60 // * VIDEO_CAPTURE_STATE_STOPPED 61 // * VIDEO_CAPTURE_STATE_PAUSED 62 // * VIDEO_CAPTURE_STATE_ERROR 63 // 64 // |deliver_frame_cb| will be called on the IO thread when a video 65 // frame is ready. 66 // 67 // Returns a callback that is used to stop capturing. Note that stopping 68 // video capture is not synchronous. Client should handle the case where 69 // callbacks are called after capturing is instructed to stop, typically 70 // by binding the passed callbacks on a WeakPtr. 71 base::Closure StartCapture( 72 media::VideoCaptureSessionId id, 73 const media::VideoCaptureParams& params, 74 const VideoCaptureStateUpdateCB& state_update_cb, 75 const VideoCaptureDeliverFrameCB& deliver_frame_cb); 76 77 // Get supported formats supported by the device for the given session 78 // ID. |callback| will be called on the IO thread. 79 void GetDeviceSupportedFormats( 80 media::VideoCaptureSessionId id, 81 const VideoCaptureDeviceFormatsCB& callback); 82 83 // Get supported formats currently in use for the given session ID. 84 // |callback| will be called on the IO thread. 85 void GetDeviceFormatsInUse( 86 media::VideoCaptureSessionId id, 87 const VideoCaptureDeviceFormatsCB& callback); 88 89 // Make all existing VideoCaptureImpl instances stop/resume delivering 90 // video frames to their clients, depends on flag |suspend|. 91 void SuspendDevices(bool suspend); 92 93 VideoCaptureMessageFilter* video_capture_message_filter() const { 94 return filter_.get(); 95 } 96 97 protected: 98 virtual VideoCaptureImpl* CreateVideoCaptureImplForTesting( 99 media::VideoCaptureSessionId id, 100 VideoCaptureMessageFilter* filter) const; 101 102 private: 103 void StopCapture(int client_id, media::VideoCaptureSessionId id); 104 void UnrefDevice(media::VideoCaptureSessionId id); 105 106 // The int is used to count clients of the corresponding VideoCaptureImpl. 107 // VideoCaptureImpl objects are owned by this object. But they are 108 // destroyed on the IO thread. These are raw pointers because we destroy 109 // them manually. 110 typedef std::map<media::VideoCaptureSessionId, 111 std::pair<int, VideoCaptureImpl*> > 112 VideoCaptureDeviceMap; 113 VideoCaptureDeviceMap devices_; 114 115 // This is an internal ID for identifying clients of VideoCaptureImpl. 116 // The ID is global for the render process. 117 int next_client_id_; 118 119 scoped_refptr<VideoCaptureMessageFilter> filter_; 120 121 // Bound to the render thread. 122 base::ThreadChecker thread_checker_; 123 124 // Bound to the render thread. 125 // NOTE: Weak pointers must be invalidated before all other member variables. 126 base::WeakPtrFactory<VideoCaptureImplManager> weak_factory_; 127 128 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager); 129 }; 130 131 } // namespace content 132 133 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ 134