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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_CONTENT_VIDEO_CAPTURE_DEVICE_CORE_H_
6 #define CONTENT_BROWSER_MEDIA_CAPTURE_CONTENT_VIDEO_CAPTURE_DEVICE_CORE_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/thread.h"
13 #include "base/threading/thread_checker.h"
14 #include "content/browser/media/capture/video_capture_oracle.h"
15 #include "content/common/content_export.h"
16 #include "media/base/video_frame.h"
17 #include "media/video/capture/video_capture_device.h"
20 class VideoCaptureParams;
26 const int kMinFrameWidth = 2;
27 const int kMinFrameHeight = 2;
29 // Returns the nearest even integer closer to zero.
30 template<typename IntType>
31 IntType MakeEven(IntType x) {
32 return x & static_cast<IntType>(-2);
35 // TODO(nick): Remove this once frame subscription is supported on Aura and
37 #if (defined(OS_WIN) || defined(OS_MACOSX)) || defined(USE_AURA)
38 const bool kAcceleratedSubscriberIsSupported = true;
40 const bool kAcceleratedSubscriberIsSupported = false;
43 class VideoCaptureMachine;
45 // Thread-safe, refcounted proxy to the VideoCaptureOracle. This proxy wraps
46 // the VideoCaptureOracle, which decides which frames to capture, and a
47 // VideoCaptureDevice::Client, which allocates and receives the captured
48 // frames, in a lock to synchronize state between the two.
49 class ThreadSafeCaptureOracle
50 : public base::RefCountedThreadSafe<ThreadSafeCaptureOracle> {
52 ThreadSafeCaptureOracle(scoped_ptr<media::VideoCaptureDevice::Client> client,
53 scoped_ptr<VideoCaptureOracle> oracle,
54 const media::VideoCaptureParams& params);
56 // Called when a captured frame is available or an error has occurred.
57 // If |success| is true then |frame| is valid and |timestamp| indicates when
58 // the frame was painted.
59 // If |success| is false, all other parameters are invalid.
60 typedef base::Callback<void(const scoped_refptr<media::VideoFrame>& frame,
61 base::TimeTicks timestamp,
62 bool success)> CaptureFrameCallback;
64 bool ObserveEventAndDecideCapture(VideoCaptureOracle::Event event,
65 const gfx::Rect& damage_rect,
66 base::TimeTicks event_time,
67 scoped_refptr<media::VideoFrame>* storage,
68 CaptureFrameCallback* callback);
70 base::TimeDelta min_capture_period() const {
71 return oracle_->min_capture_period();
74 // Returns the current capture resolution.
75 gfx::Size GetCaptureSize() const;
77 // Updates capture resolution based on the supplied source size and the
78 // maximum frame size.
79 void UpdateCaptureSize(const gfx::Size& source_size);
81 // Stop new captures from happening (but doesn't forget the client).
84 // Signal an error to the client.
85 void ReportError(const std::string& reason);
88 friend class base::RefCountedThreadSafe<ThreadSafeCaptureOracle>;
89 virtual ~ThreadSafeCaptureOracle();
91 // Callback invoked on completion of all captures.
94 const scoped_refptr<media::VideoCaptureDevice::Client::Buffer>& buffer,
95 const scoped_refptr<media::VideoFrame>& frame,
96 base::TimeTicks timestamp,
99 // Protects everything below it.
100 mutable base::Lock lock_;
102 // Recipient of our capture activity.
103 scoped_ptr<media::VideoCaptureDevice::Client> client_;
105 // Makes the decision to capture a frame.
106 const scoped_ptr<VideoCaptureOracle> oracle_;
108 // The video capture parameters used to construct the oracle proxy.
109 media::VideoCaptureParams params_;
111 // Indicates if capture size has been updated after construction.
112 bool capture_size_updated_;
114 // The current capturing format, as a media::VideoFrame::Format.
115 media::VideoFrame::Format video_frame_format_;
118 // Keeps track of the video capture source frames and executes copying on the
120 class VideoCaptureMachine {
122 VideoCaptureMachine() : started_(false) {}
123 virtual ~VideoCaptureMachine() {}
125 // This should only be checked on the UI thread.
126 bool started() const { return started_; }
128 // Starts capturing. Returns true if succeeded.
129 // Must be run on the UI BrowserThread.
130 virtual bool Start(const scoped_refptr<ThreadSafeCaptureOracle>& oracle_proxy,
131 const media::VideoCaptureParams& params) = 0;
133 // Stops capturing. Must be run on the UI BrowserThread.
134 // |callback| is invoked after the capturing has stopped.
135 virtual void Stop(const base::Closure& callback) = 0;
141 DISALLOW_COPY_AND_ASSIGN(VideoCaptureMachine);
144 // The "meat" of a content video capturer.
146 // Separating this from the "shell classes" WebContentsVideoCaptureDevice and
147 // DesktopCaptureDeviceAura allows safe destruction without needing to block any
148 // threads (e.g., the IO BrowserThread), as well as code sharing.
150 // ContentVideoCaptureDeviceCore manages a simple state machine and the pipeline
151 // (see notes at top of this file). It times the start of successive captures
152 // and facilitates the processing of each through the stages of the
154 class CONTENT_EXPORT ContentVideoCaptureDeviceCore
155 : public base::SupportsWeakPtr<ContentVideoCaptureDeviceCore> {
157 ContentVideoCaptureDeviceCore(
158 scoped_ptr<VideoCaptureMachine> capture_machine);
159 virtual ~ContentVideoCaptureDeviceCore();
161 // Asynchronous requests to change ContentVideoCaptureDeviceCore state.
162 void AllocateAndStart(const media::VideoCaptureParams& params,
163 scoped_ptr<media::VideoCaptureDevice::Client> client);
164 void StopAndDeAllocate();
167 // Flag indicating current state.
174 void TransitionStateTo(State next_state);
176 // Called on the IO thread in response to StartCaptureMachine().
177 // |success| is true if capture machine succeeded to start.
178 void CaptureStarted(bool success);
180 // Stops capturing and notifies client_ of an error state.
181 void Error(const std::string& reason);
183 // Tracks that all activity occurs on the media stream manager's thread.
184 base::ThreadChecker thread_checker_;
186 // Current lifecycle state.
189 // Tracks the CaptureMachine that's doing work on our behalf on the UI thread.
190 // This value should never be dereferenced by this class, other than to
191 // create and destroy it on the UI thread.
192 scoped_ptr<VideoCaptureMachine> capture_machine_;
194 // Our thread-safe capture oracle which serves as the gateway to the video
195 // capture pipeline. Besides the VideoCaptureDevice itself, it is the only
196 // component of the/ system with direct access to |client_|.
197 scoped_refptr<ThreadSafeCaptureOracle> oracle_proxy_;
199 DISALLOW_COPY_AND_ASSIGN(ContentVideoCaptureDeviceCore);
203 } // namespace content
205 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_CONTENT_VIDEO_CAPTURE_DEVICE_CORE_H_