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 // VideoCaptureController is the glue between VideoCaptureHost, 6 // VideoCaptureManager and VideoCaptureDevice. 7 // It provides functions for VideoCaptureHost to start a VideoCaptureDevice and 8 // is responsible for keeping track of shared DIBs and filling them with I420 9 // video frames for IPC communication between VideoCaptureHost and 10 // VideoCaptureMessageFilter. 11 // It implements media::VideoCaptureDevice::EventHandler to get video frames 12 // from a VideoCaptureDevice object and do color conversion straight into the 13 // shared DIBs to avoid a memory copy. 14 // It serves multiple VideoCaptureControllerEventHandlers. 15 16 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ 17 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ 18 19 #include <list> 20 #include <map> 21 22 #include "base/compiler_specific.h" 23 #include "base/memory/ref_counted.h" 24 #include "base/process/process.h" 25 #include "base/synchronization/lock.h" 26 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" 27 #include "content/browser/renderer_host/media/video_capture_controller_event_handler.h" 28 #include "content/common/content_export.h" 29 #include "content/common/media/video_capture.h" 30 #include "media/video/capture/video_capture.h" 31 #include "media/video/capture/video_capture_device.h" 32 #include "media/video/capture/video_capture_types.h" 33 34 namespace content { 35 class VideoCaptureManager; 36 class VideoCaptureBufferPool; 37 38 class CONTENT_EXPORT VideoCaptureController 39 : public base::RefCountedThreadSafe<VideoCaptureController>, 40 public media::VideoCaptureDevice::EventHandler { 41 public: 42 VideoCaptureController(VideoCaptureManager* video_capture_manager); 43 44 // Start video capturing and try to use the resolution specified in 45 // |params|. 46 // When capturing has started, the |event_handler| receives a call OnFrameInfo 47 // with resolution that best matches the requested that the video 48 // capture device support. 49 void StartCapture(const VideoCaptureControllerID& id, 50 VideoCaptureControllerEventHandler* event_handler, 51 base::ProcessHandle render_process, 52 const media::VideoCaptureParams& params); 53 54 // Stop video capture. 55 // This will take back all buffers held by by |event_handler|, and 56 // |event_handler| shouldn't use those buffers any more. 57 void StopCapture(const VideoCaptureControllerID& id, 58 VideoCaptureControllerEventHandler* event_handler); 59 60 // API called directly by VideoCaptureManager in case the device is 61 // prematurely closed. 62 void StopSession(int session_id); 63 64 // Return a buffer previously given in 65 // VideoCaptureControllerEventHandler::OnBufferReady. 66 void ReturnBuffer(const VideoCaptureControllerID& id, 67 VideoCaptureControllerEventHandler* event_handler, 68 int buffer_id); 69 70 // Implement media::VideoCaptureDevice::EventHandler. 71 virtual scoped_refptr<media::VideoFrame> ReserveOutputBuffer() OVERRIDE; 72 virtual void OnIncomingCapturedFrame(const uint8* data, 73 int length, 74 base::Time timestamp, 75 int rotation, 76 bool flip_vert, 77 bool flip_horiz) OVERRIDE; 78 virtual void OnIncomingCapturedVideoFrame( 79 const scoped_refptr<media::VideoFrame>& frame, 80 base::Time timestamp) OVERRIDE; 81 virtual void OnError() OVERRIDE; 82 virtual void OnFrameInfo(const media::VideoCaptureCapability& info) OVERRIDE; 83 virtual void OnFrameInfoChanged( 84 const media::VideoCaptureCapability& info) OVERRIDE; 85 86 protected: 87 virtual ~VideoCaptureController(); 88 89 private: 90 friend class base::RefCountedThreadSafe<VideoCaptureController>; 91 92 struct ControllerClient; 93 typedef std::list<ControllerClient*> ControllerClients; 94 95 // Callback when manager has stopped device. 96 void OnDeviceStopped(); 97 98 // Worker functions on IO thread. 99 void DoIncomingCapturedFrameOnIOThread( 100 const scoped_refptr<media::VideoFrame>& captured_frame, 101 base::Time timestamp); 102 void DoFrameInfoOnIOThread(); 103 void DoFrameInfoChangedOnIOThread(const media::VideoCaptureCapability& info); 104 void DoErrorOnIOThread(); 105 void DoDeviceStoppedOnIOThread(); 106 107 // Send frame info and init buffers to |client|. 108 void SendFrameInfoAndBuffers(ControllerClient* client); 109 110 // Find a client of |id| and |handler| in |clients|. 111 ControllerClient* FindClient( 112 const VideoCaptureControllerID& id, 113 VideoCaptureControllerEventHandler* handler, 114 const ControllerClients& clients); 115 116 // Find a client of |session_id| in |clients|. 117 ControllerClient* FindClient( 118 int session_id, 119 const ControllerClients& clients); 120 121 // Decide what to do after kStopping state. Dependent on events, controller 122 // can stay in kStopping state, or go to kStopped, or restart capture. 123 void PostStopping(); 124 125 // Protects access to the |buffer_pool_| pointer on non-IO threads. IO thread 126 // must hold this lock when modifying the |buffer_pool_| pointer itself. 127 // TODO(nick): Make it so that this lock isn't required. 128 base::Lock buffer_pool_lock_; 129 130 // The pool of shared-memory buffers used for capturing. 131 scoped_refptr<VideoCaptureBufferPool> buffer_pool_; 132 133 // All clients served by this controller. 134 ControllerClients controller_clients_; 135 136 // All clients waiting for service. 137 ControllerClients pending_clients_; 138 139 // The parameter that currently used for the capturing. 140 media::VideoCaptureParams current_params_; 141 142 // It's modified on caller thread, assuming there is only one OnFrameInfo() 143 // call per StartCapture(). 144 media::VideoCaptureCapability frame_info_; 145 146 // Chopped pixels in width/height in case video capture device has odd numbers 147 // for width/height. 148 int chopped_width_; 149 int chopped_height_; 150 151 // It's accessed only on IO thread. 152 bool frame_info_available_; 153 154 VideoCaptureManager* video_capture_manager_; 155 156 bool device_in_use_; 157 VideoCaptureState state_; 158 159 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureController); 160 }; 161 162 } // namespace content 163 164 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ 165