| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_CONTENT_VIDEO_CAPTURE_DEVICE_CORE_H_ | 5 #ifndef MEDIA_CAPTURE_SCREEN_CAPTURE_DEVICE_CORE_H_ |
| 6 #define CONTENT_BROWSER_MEDIA_CAPTURE_CONTENT_VIDEO_CAPTURE_DEVICE_CORE_H_ | 6 #define MEDIA_CAPTURE_SCREEN_CAPTURE_DEVICE_CORE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/threading/thread.h" | |
| 13 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 14 #include "content/browser/media/capture/capture_resolution_chooser.h" | 13 #include "media/base/media_export.h" |
| 15 #include "content/browser/media/capture/video_capture_oracle.h" | 14 #include "media/capture/thread_safe_capture_oracle.h" |
| 16 #include "content/common/content_export.h" | |
| 17 #include "media/base/video_frame.h" | |
| 18 #include "media/video/capture/video_capture_device.h" | 15 #include "media/video/capture/video_capture_device.h" |
| 19 | 16 |
| 20 namespace media { | 17 namespace media { |
| 18 |
| 21 struct VideoCaptureParams; | 19 struct VideoCaptureParams; |
| 22 class VideoFrame; | |
| 23 } // namespace media | |
| 24 | 20 |
| 25 namespace content { | 21 class ThreadSafeCaptureOracle; |
| 26 | 22 |
| 27 class VideoCaptureMachine; | 23 // Keeps track of the video capture source frames and executes copying. |
| 28 | |
| 29 // Thread-safe, refcounted proxy to the VideoCaptureOracle. This proxy wraps | |
| 30 // the VideoCaptureOracle, which decides which frames to capture, and a | |
| 31 // VideoCaptureDevice::Client, which allocates and receives the captured | |
| 32 // frames, in a lock to synchronize state between the two. | |
| 33 class ThreadSafeCaptureOracle | |
| 34 : public base::RefCountedThreadSafe<ThreadSafeCaptureOracle> { | |
| 35 public: | |
| 36 ThreadSafeCaptureOracle(scoped_ptr<media::VideoCaptureDevice::Client> client, | |
| 37 const media::VideoCaptureParams& params); | |
| 38 | |
| 39 // Called when a captured frame is available or an error has occurred. | |
| 40 // If |success| is true then |frame| is valid and |timestamp| indicates when | |
| 41 // the frame was painted. | |
| 42 // If |success| is false, all other parameters are invalid. | |
| 43 typedef base::Callback<void(const scoped_refptr<media::VideoFrame>& frame, | |
| 44 base::TimeTicks timestamp, | |
| 45 bool success)> CaptureFrameCallback; | |
| 46 | |
| 47 bool ObserveEventAndDecideCapture(VideoCaptureOracle::Event event, | |
| 48 const gfx::Rect& damage_rect, | |
| 49 base::TimeTicks event_time, | |
| 50 scoped_refptr<media::VideoFrame>* storage, | |
| 51 CaptureFrameCallback* callback); | |
| 52 | |
| 53 base::TimeDelta min_capture_period() const { | |
| 54 return oracle_.min_capture_period(); | |
| 55 } | |
| 56 | |
| 57 gfx::Size max_frame_size() const { | |
| 58 return params_.requested_format.frame_size; | |
| 59 } | |
| 60 | |
| 61 // Returns the current capture resolution. | |
| 62 gfx::Size GetCaptureSize() const; | |
| 63 | |
| 64 // Updates capture resolution based on the supplied source size and the | |
| 65 // maximum frame size. | |
| 66 void UpdateCaptureSize(const gfx::Size& source_size); | |
| 67 | |
| 68 // Stop new captures from happening (but doesn't forget the client). | |
| 69 void Stop(); | |
| 70 | |
| 71 // Signal an error to the client. | |
| 72 void ReportError(const std::string& reason); | |
| 73 | |
| 74 private: | |
| 75 friend class base::RefCountedThreadSafe<ThreadSafeCaptureOracle>; | |
| 76 virtual ~ThreadSafeCaptureOracle(); | |
| 77 | |
| 78 // Callback invoked on completion of all captures. | |
| 79 void DidCaptureFrame( | |
| 80 int frame_number, | |
| 81 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | |
| 82 base::TimeTicks capture_begin_time, | |
| 83 base::TimeDelta estimated_frame_duration, | |
| 84 const scoped_refptr<media::VideoFrame>& frame, | |
| 85 base::TimeTicks timestamp, | |
| 86 bool success); | |
| 87 | |
| 88 // Callback invoked once all consumers have finished with a delivered video | |
| 89 // frame. Consumer feedback signals are scanned from the frame's |metadata|. | |
| 90 void DidConsumeFrame(int frame_number, | |
| 91 const media::VideoFrameMetadata* metadata); | |
| 92 | |
| 93 // Protects everything below it. | |
| 94 mutable base::Lock lock_; | |
| 95 | |
| 96 // Recipient of our capture activity. | |
| 97 scoped_ptr<media::VideoCaptureDevice::Client> client_; | |
| 98 | |
| 99 // Makes the decision to capture a frame. | |
| 100 VideoCaptureOracle oracle_; | |
| 101 | |
| 102 // The video capture parameters used to construct the oracle proxy. | |
| 103 const media::VideoCaptureParams params_; | |
| 104 | |
| 105 // Determines video capture frame sizes. | |
| 106 CaptureResolutionChooser resolution_chooser_; | |
| 107 }; | |
| 108 | |
| 109 // Keeps track of the video capture source frames and executes copying on the | |
| 110 // UI BrowserThread. | |
| 111 class VideoCaptureMachine { | 24 class VideoCaptureMachine { |
| 112 public: | 25 public: |
| 113 VideoCaptureMachine() {} | 26 VideoCaptureMachine() {} |
| 114 virtual ~VideoCaptureMachine() {} | 27 virtual ~VideoCaptureMachine() {} |
| 115 | 28 |
| 116 // Starts capturing. Returns true if succeeded. | 29 // Starts capturing. |
| 117 // Must be run on the UI BrowserThread. | 30 // |callback| is invoked with true if succeeded. Otherwise, with false. |
| 118 virtual bool Start(const scoped_refptr<ThreadSafeCaptureOracle>& oracle_proxy, | 31 virtual void Start(const scoped_refptr<ThreadSafeCaptureOracle>& oracle_proxy, |
| 119 const media::VideoCaptureParams& params) = 0; | 32 const VideoCaptureParams& params, |
| 33 const base::Callback<void(bool)> callback) = 0; |
| 120 | 34 |
| 121 // Stops capturing. Must be run on the UI BrowserThread. | 35 // Stops capturing. |
| 122 // |callback| is invoked after the capturing has stopped. | 36 // |callback| is invoked after the capturing has stopped. |
| 123 virtual void Stop(const base::Closure& callback) = 0; | 37 virtual void Stop(const base::Closure& callback) = 0; |
| 124 | 38 |
| 125 private: | 39 private: |
| 126 DISALLOW_COPY_AND_ASSIGN(VideoCaptureMachine); | 40 DISALLOW_COPY_AND_ASSIGN(VideoCaptureMachine); |
| 127 }; | 41 }; |
| 128 | 42 |
| 129 // The "meat" of a content video capturer. | 43 // The "meat" of a content video capturer. |
| 130 // | 44 // |
| 131 // Separating this from the "shell classes" WebContentsVideoCaptureDevice and | 45 // Separating this from the "shell classes" WebContentsVideoCaptureDevice and |
| 132 // DesktopCaptureDeviceAura allows safe destruction without needing to block any | 46 // DesktopCaptureDeviceAura allows safe destruction without needing to block any |
| 133 // threads, as well as code sharing. | 47 // threads, as well as code sharing. |
| 134 // | 48 // |
| 135 // ContentVideoCaptureDeviceCore manages a simple state machine and the pipeline | 49 // ScreenCaptureDeviceCore manages a simple state machine and the pipeline |
| 136 // (see notes at top of this file). It times the start of successive captures | 50 // (see notes at top of this file). It times the start of successive captures |
| 137 // and facilitates the processing of each through the stages of the | 51 // and facilitates the processing of each through the stages of the |
| 138 // pipeline. | 52 // pipeline. |
| 139 class CONTENT_EXPORT ContentVideoCaptureDeviceCore | 53 class MEDIA_EXPORT ScreenCaptureDeviceCore |
| 140 : public base::SupportsWeakPtr<ContentVideoCaptureDeviceCore> { | 54 : public base::SupportsWeakPtr<ScreenCaptureDeviceCore> { |
| 141 public: | 55 public: |
| 142 ContentVideoCaptureDeviceCore( | 56 ScreenCaptureDeviceCore( |
| 143 scoped_ptr<VideoCaptureMachine> capture_machine); | 57 scoped_ptr<VideoCaptureMachine> capture_machine); |
| 144 virtual ~ContentVideoCaptureDeviceCore(); | 58 virtual ~ScreenCaptureDeviceCore(); |
| 145 | 59 |
| 146 // Asynchronous requests to change ContentVideoCaptureDeviceCore state. | 60 // Asynchronous requests to change ScreenCaptureDeviceCore state. |
| 147 void AllocateAndStart(const media::VideoCaptureParams& params, | 61 void AllocateAndStart(const VideoCaptureParams& params, |
| 148 scoped_ptr<media::VideoCaptureDevice::Client> client); | 62 scoped_ptr<VideoCaptureDevice::Client> client); |
| 149 void StopAndDeAllocate(); | 63 void StopAndDeAllocate(); |
| 150 | 64 |
| 151 private: | 65 private: |
| 152 // Flag indicating current state. | 66 // Flag indicating current state. |
| 153 enum State { | 67 enum State { |
| 154 kIdle, | 68 kIdle, |
| 155 kCapturing, | 69 kCapturing, |
| 156 kError | 70 kError |
| 157 }; | 71 }; |
| 158 | 72 |
| 159 void TransitionStateTo(State next_state); | 73 void TransitionStateTo(State next_state); |
| 160 | 74 |
| 161 // Called back in response to StartCaptureMachine(). |success| is true if | 75 // Called back in response to StartCaptureMachine(). |success| is true if |
| 162 // capture machine succeeded to start. | 76 // capture machine succeeded to start. |
| 163 void CaptureStarted(bool success); | 77 void CaptureStarted(bool success); |
| 164 | 78 |
| 165 // Stops capturing and notifies client_ of an error state. | 79 // Stops capturing and notifies client_ of an error state. |
| 166 void Error(const std::string& reason); | 80 void Error(const std::string& reason); |
| 167 | 81 |
| 168 // Tracks that all activity occurs on the media stream manager's thread. | 82 // Tracks that all activity occurs on the media stream manager's thread. |
| 169 base::ThreadChecker thread_checker_; | 83 base::ThreadChecker thread_checker_; |
| 170 | 84 |
| 171 // Current lifecycle state. | 85 // Current lifecycle state. |
| 172 State state_; | 86 State state_; |
| 173 | 87 |
| 174 // Tracks the CaptureMachine that's doing work on our behalf on the UI thread. | 88 // Tracks the CaptureMachine that's doing work on our behalf |
| 175 // This value should never be dereferenced by this class, other than to | 89 // on the device thread or UI thread. |
| 176 // create and destroy it on the UI thread. | 90 // This value should never be dereferenced by this class. |
| 177 scoped_ptr<VideoCaptureMachine> capture_machine_; | 91 scoped_ptr<VideoCaptureMachine> capture_machine_; |
| 178 | 92 |
| 179 // Our thread-safe capture oracle which serves as the gateway to the video | 93 // Our thread-safe capture oracle which serves as the gateway to the video |
| 180 // capture pipeline. Besides the VideoCaptureDevice itself, it is the only | 94 // capture pipeline. Besides the VideoCaptureDevice itself, it is the only |
| 181 // component of the system with direct access to |client_|. | 95 // component of the system with direct access to |client_|. |
| 182 scoped_refptr<ThreadSafeCaptureOracle> oracle_proxy_; | 96 scoped_refptr<ThreadSafeCaptureOracle> oracle_proxy_; |
| 183 | 97 |
| 184 DISALLOW_COPY_AND_ASSIGN(ContentVideoCaptureDeviceCore); | 98 DISALLOW_COPY_AND_ASSIGN(ScreenCaptureDeviceCore); |
| 185 }; | 99 }; |
| 186 | 100 |
| 187 | 101 |
| 188 } // namespace content | 102 } // namespace media |
| 189 | 103 |
| 190 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_CONTENT_VIDEO_CAPTURE_DEVICE_CORE_H_ | 104 #endif // MEDIA_CAPTURE_SCREEN_CAPTURE_DEVICE_CORE_H_ |
| OLD | NEW |