OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "webrtc/base/basictypes.h" | 21 #include "webrtc/base/basictypes.h" |
22 #include "webrtc/base/constructormagic.h" | 22 #include "webrtc/base/constructormagic.h" |
23 #include "webrtc/base/criticalsection.h" | 23 #include "webrtc/base/criticalsection.h" |
24 #include "webrtc/media/base/videosourceinterface.h" | 24 #include "webrtc/media/base/videosourceinterface.h" |
25 #include "webrtc/base/sigslot.h" | 25 #include "webrtc/base/sigslot.h" |
26 #include "webrtc/base/thread_checker.h" | 26 #include "webrtc/base/thread_checker.h" |
27 #include "webrtc/base/timestampaligner.h" | 27 #include "webrtc/base/timestampaligner.h" |
28 #include "webrtc/media/base/videoadapter.h" | 28 #include "webrtc/media/base/videoadapter.h" |
29 #include "webrtc/media/base/videobroadcaster.h" | 29 #include "webrtc/media/base/videobroadcaster.h" |
30 #include "webrtc/media/base/videocommon.h" | 30 #include "webrtc/media/base/videocommon.h" |
31 #include "webrtc/media/base/videoframefactory.h" | |
32 | 31 |
33 | 32 |
34 namespace cricket { | 33 namespace cricket { |
35 | 34 |
36 // Current state of the capturer. | 35 // Current state of the capturer. |
37 enum CaptureState { | 36 enum CaptureState { |
38 CS_STOPPED, // The capturer has been stopped or hasn't started yet. | 37 CS_STOPPED, // The capturer has been stopped or hasn't started yet. |
39 CS_STARTING, // The capturer is in the process of starting. Note, it may | 38 CS_STARTING, // The capturer is in the process of starting. Note, it may |
40 // still fail to start. | 39 // still fail to start. |
41 CS_RUNNING, // The capturer has been started successfully and is now | 40 CS_RUNNING, // The capturer has been started successfully and is now |
42 // capturing. | 41 // capturing. |
43 CS_FAILED, // The capturer failed to start. | 42 CS_FAILED, // The capturer failed to start. |
44 }; | 43 }; |
45 | 44 |
46 class VideoFrame; | 45 class VideoFrame; |
47 | 46 |
48 struct CapturedFrame { | |
49 static const uint32_t kFrameHeaderSize = 40; // Size from width to data_size. | |
50 static const uint32_t kUnknownDataSize = 0xFFFFFFFF; | |
51 | |
52 CapturedFrame(); | |
53 | |
54 // Get the number of bytes of the frame data. If data_size is known, return | |
55 // it directly. Otherwise, calculate the size based on width, height, and | |
56 // fourcc. Return true if succeeded. | |
57 bool GetDataSize(uint32_t* size) const; | |
58 | |
59 // The width and height of the captured frame could be different from those | |
60 // of VideoFormat. Once the first frame is captured, the width, height, | |
61 // fourcc, pixel_width, and pixel_height should keep the same over frames. | |
62 int width; // in number of pixels | |
63 int height; // in number of pixels | |
64 uint32_t fourcc; // compression | |
65 uint32_t pixel_width; // width of a pixel, default is 1 | |
66 uint32_t pixel_height; // height of a pixel, default is 1 | |
67 int64_t time_stamp; // timestamp of when the frame was captured, in unix | |
68 // time with nanosecond units. | |
69 uint32_t data_size; // number of bytes of the frame data | |
70 | |
71 webrtc::VideoRotation rotation; // rotation in degrees of the frame. | |
72 | |
73 void* data; // pointer to the frame data. This object allocates the | |
74 // memory or points to an existing memory. | |
75 | |
76 private: | |
77 RTC_DISALLOW_COPY_AND_ASSIGN(CapturedFrame); | |
78 }; | |
79 | |
80 // VideoCapturer is an abstract class that defines the interfaces for video | 47 // VideoCapturer is an abstract class that defines the interfaces for video |
81 // capturing. The subclasses implement the video capturer for various types of | 48 // capturing. The subclasses implement the video capturer for various types of |
82 // capturers and various platforms. | 49 // capturers and various platforms. |
83 // | 50 // |
84 // The captured frames may need to be adapted (for example, cropping). | 51 // The captured frames may need to be adapted (for example, cropping). |
85 // Video adaptation is built into and enabled by default. After a frame has | 52 // Video adaptation is built into and enabled by default. After a frame has |
86 // been captured from the device, it is sent to the video adapter, then out to | 53 // been captured from the device, it is sent to the video adapter, then out to |
87 // the sinks. | 54 // the sinks. |
88 // | 55 // |
89 // Programming model: | 56 // Programming model: |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 void set_enable_camera_list(bool enable_camera_list) { | 164 void set_enable_camera_list(bool enable_camera_list) { |
198 enable_camera_list_ = enable_camera_list; | 165 enable_camera_list_ = enable_camera_list; |
199 } | 166 } |
200 bool enable_camera_list() { | 167 bool enable_camera_list() { |
201 return enable_camera_list_; | 168 return enable_camera_list_; |
202 } | 169 } |
203 | 170 |
204 // Signal all capture state changes that are not a direct result of calling | 171 // Signal all capture state changes that are not a direct result of calling |
205 // Start(). | 172 // Start(). |
206 sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange; | 173 sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange; |
207 // Frame callbacks are multithreaded to allow disconnect and connect to be | |
208 // called concurrently. It also ensures that it is safe to call disconnect | |
209 // at any time which is needed since the signal may be called from an | |
210 // unmarshalled thread owned by the VideoCapturer. | |
211 // Signal the captured frame to downstream. | |
212 sigslot::signal2<VideoCapturer*, const CapturedFrame*, | |
213 sigslot::multi_threaded_local> SignalFrameCaptured; | |
214 | 174 |
215 // If true, run video adaptation. By default, video adaptation is enabled | 175 // If true, run video adaptation. By default, video adaptation is enabled |
216 // and users must call video_adapter()->OnOutputFormatRequest() | 176 // and users must call video_adapter()->OnOutputFormatRequest() |
217 // to receive frames. | 177 // to receive frames. |
218 bool enable_video_adapter() const { return enable_video_adapter_; } | 178 bool enable_video_adapter() const { return enable_video_adapter_; } |
219 void set_enable_video_adapter(bool enable_video_adapter) { | 179 void set_enable_video_adapter(bool enable_video_adapter) { |
220 enable_video_adapter_ = enable_video_adapter; | 180 enable_video_adapter_ = enable_video_adapter; |
221 } | 181 } |
222 | 182 |
223 // Takes ownership. | |
224 void set_frame_factory(VideoFrameFactory* frame_factory); | |
225 | |
226 bool GetInputSize(int* width, int* height); | 183 bool GetInputSize(int* width, int* height); |
227 | 184 |
228 // Implements VideoSourceInterface | 185 // Implements VideoSourceInterface |
229 void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink, | 186 void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink, |
230 const rtc::VideoSinkWants& wants) override; | 187 const rtc::VideoSinkWants& wants) override; |
231 void RemoveSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override; | 188 void RemoveSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override; |
232 | 189 |
233 protected: | 190 protected: |
234 // OnSinkWantsChanged can be overridden to change the default behavior | 191 // OnSinkWantsChanged can be overridden to change the default behavior |
235 // when a sink changes its VideoSinkWants by calling AddOrUpdateSink. | 192 // when a sink changes its VideoSinkWants by calling AddOrUpdateSink. |
(...skipping 20 matching lines...) Expand all Loading... |
256 int64_t camera_time_us, | 213 int64_t camera_time_us, |
257 int64_t system_time_us, | 214 int64_t system_time_us, |
258 int* out_width, | 215 int* out_width, |
259 int* out_height, | 216 int* out_height, |
260 int* crop_width, | 217 int* crop_width, |
261 int* crop_height, | 218 int* crop_height, |
262 int* crop_x, | 219 int* crop_x, |
263 int* crop_y, | 220 int* crop_y, |
264 int64_t* translated_camera_time_us); | 221 int64_t* translated_camera_time_us); |
265 | 222 |
266 // Callback attached to SignalFrameCaptured where SignalVideoFrames is called. | |
267 void OnFrameCaptured(VideoCapturer* video_capturer, | |
268 const CapturedFrame* captured_frame); | |
269 | |
270 // Called when a frame has been captured and converted to a | 223 // Called when a frame has been captured and converted to a |
271 // VideoFrame. OnFrame can be called directly by an implementation | 224 // VideoFrame. OnFrame can be called directly by an implementation |
272 // that does not use SignalFrameCaptured or OnFrameCaptured. The | 225 // that does not use SignalFrameCaptured or OnFrameCaptured. The |
273 // orig_width and orig_height are used only to produce stats. | 226 // orig_width and orig_height are used only to produce stats. |
274 void OnFrame(const VideoFrame& frame, int orig_width, int orig_height); | 227 void OnFrame(const VideoFrame& frame, int orig_width, int orig_height); |
275 | 228 |
276 VideoAdapter* video_adapter() { return &video_adapter_; } | 229 VideoAdapter* video_adapter() { return &video_adapter_; } |
277 | 230 |
278 void SetCaptureState(CaptureState state); | 231 void SetCaptureState(CaptureState state); |
279 | 232 |
280 // subclasses override this virtual method to provide a vector of fourccs, in | 233 // subclasses override this virtual method to provide a vector of fourccs, in |
281 // order of preference, that are expected by the media engine. | 234 // order of preference, that are expected by the media engine. |
282 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0; | 235 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0; |
283 | 236 |
284 // mutators to set private attributes | 237 // mutators to set private attributes |
285 void SetId(const std::string& id) { | 238 void SetId(const std::string& id) { |
286 id_ = id; | 239 id_ = id; |
287 } | 240 } |
288 | 241 |
289 void SetCaptureFormat(const VideoFormat* format) { | 242 void SetCaptureFormat(const VideoFormat* format) { |
290 capture_format_.reset(format ? new VideoFormat(*format) : NULL); | 243 capture_format_.reset(format ? new VideoFormat(*format) : NULL); |
291 } | 244 } |
292 | 245 |
293 void SetSupportedFormats(const std::vector<VideoFormat>& formats); | 246 void SetSupportedFormats(const std::vector<VideoFormat>& formats); |
294 VideoFrameFactory* frame_factory() { return frame_factory_.get(); } | |
295 | 247 |
296 private: | 248 private: |
297 void Construct(); | 249 void Construct(); |
298 // Get the distance between the desired format and the supported format. | 250 // Get the distance between the desired format and the supported format. |
299 // Return the max distance if they mismatch. See the implementation for | 251 // Return the max distance if they mismatch. See the implementation for |
300 // details. | 252 // details. |
301 int64_t GetFormatDistance(const VideoFormat& desired, | 253 int64_t GetFormatDistance(const VideoFormat& desired, |
302 const VideoFormat& supported); | 254 const VideoFormat& supported); |
303 | 255 |
304 // Convert captured frame to readable string for LOG messages. | |
305 std::string ToString(const CapturedFrame* frame) const; | |
306 | |
307 // Updates filtered_supported_formats_ so that it contains the formats in | 256 // Updates filtered_supported_formats_ so that it contains the formats in |
308 // supported_formats_ that fulfill all applied restrictions. | 257 // supported_formats_ that fulfill all applied restrictions. |
309 void UpdateFilteredSupportedFormats(); | 258 void UpdateFilteredSupportedFormats(); |
310 // Returns true if format doesn't fulfill all applied restrictions. | 259 // Returns true if format doesn't fulfill all applied restrictions. |
311 bool ShouldFilterFormat(const VideoFormat& format) const; | 260 bool ShouldFilterFormat(const VideoFormat& format) const; |
312 | 261 |
313 void UpdateInputSize(int width, int height); | 262 void UpdateInputSize(int width, int height); |
314 | 263 |
315 rtc::ThreadChecker thread_checker_; | 264 rtc::ThreadChecker thread_checker_; |
316 std::string id_; | 265 std::string id_; |
317 CaptureState capture_state_; | 266 CaptureState capture_state_; |
318 std::unique_ptr<VideoFrameFactory> frame_factory_; | |
319 std::unique_ptr<VideoFormat> capture_format_; | 267 std::unique_ptr<VideoFormat> capture_format_; |
320 std::vector<VideoFormat> supported_formats_; | 268 std::vector<VideoFormat> supported_formats_; |
321 std::unique_ptr<VideoFormat> max_format_; | 269 std::unique_ptr<VideoFormat> max_format_; |
322 std::vector<VideoFormat> filtered_supported_formats_; | 270 std::vector<VideoFormat> filtered_supported_formats_; |
323 | 271 |
324 bool enable_camera_list_; | 272 bool enable_camera_list_; |
325 int scaled_width_; // Current output size from ComputeScale. | 273 int scaled_width_; // Current output size from ComputeScale. |
326 int scaled_height_; | 274 int scaled_height_; |
327 | 275 |
328 rtc::VideoBroadcaster broadcaster_; | 276 rtc::VideoBroadcaster broadcaster_; |
329 bool enable_video_adapter_; | 277 bool enable_video_adapter_; |
330 VideoAdapter video_adapter_; | 278 VideoAdapter video_adapter_; |
331 | 279 |
332 rtc::CriticalSection frame_stats_crit_; | 280 rtc::CriticalSection frame_stats_crit_; |
333 // The captured frame size before potential adapation. | 281 // The captured frame size before potential adapation. |
334 bool input_size_valid_ GUARDED_BY(frame_stats_crit_) = false; | 282 bool input_size_valid_ GUARDED_BY(frame_stats_crit_) = false; |
335 int input_width_ GUARDED_BY(frame_stats_crit_); | 283 int input_width_ GUARDED_BY(frame_stats_crit_); |
336 int input_height_ GUARDED_BY(frame_stats_crit_); | 284 int input_height_ GUARDED_BY(frame_stats_crit_); |
337 | 285 |
338 // Whether capturer should apply rotation to the frame before signaling it. | 286 // Whether capturer should apply rotation to the frame before |
| 287 // passing it on to the registered sinks. |
339 bool apply_rotation_; | 288 bool apply_rotation_; |
340 | 289 |
341 // State for the timestamp translation. | 290 // State for the timestamp translation. |
342 rtc::TimestampAligner timestamp_aligner_; | 291 rtc::TimestampAligner timestamp_aligner_; |
343 RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer); | 292 RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer); |
344 }; | 293 }; |
345 | 294 |
346 } // namespace cricket | 295 } // namespace cricket |
347 | 296 |
348 #endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ | 297 #endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ |
OLD | NEW |