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