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" |
31 | 32 |
32 | 33 |
33 namespace cricket { | 34 namespace cricket { |
34 | 35 |
35 // Current state of the capturer. | 36 // Current state of the capturer. |
36 enum CaptureState { | 37 enum CaptureState { |
37 CS_STOPPED, // The capturer has been stopped or hasn't started yet. | 38 CS_STOPPED, // The capturer has been stopped or hasn't started yet. |
38 CS_STARTING, // The capturer is in the process of starting. Note, it may | 39 CS_STARTING, // The capturer is in the process of starting. Note, it may |
39 // still fail to start. | 40 // still fail to start. |
40 CS_RUNNING, // The capturer has been started successfully and is now | 41 CS_RUNNING, // The capturer has been started successfully and is now |
41 // capturing. | 42 // capturing. |
42 CS_FAILED, // The capturer failed to start. | 43 CS_FAILED, // The capturer failed to start. |
43 }; | 44 }; |
44 | 45 |
45 class VideoFrame; | 46 class VideoFrame; |
46 | 47 |
| 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 |
47 // VideoCapturer is an abstract class that defines the interfaces for video | 80 // VideoCapturer is an abstract class that defines the interfaces for video |
48 // capturing. The subclasses implement the video capturer for various types of | 81 // capturing. The subclasses implement the video capturer for various types of |
49 // capturers and various platforms. | 82 // capturers and various platforms. |
50 // | 83 // |
51 // The captured frames may need to be adapted (for example, cropping). | 84 // The captured frames may need to be adapted (for example, cropping). |
52 // Video adaptation is built into and enabled by default. After a frame has | 85 // Video adaptation is built into and enabled by default. After a frame has |
53 // been captured from the device, it is sent to the video adapter, then out to | 86 // been captured from the device, it is sent to the video adapter, then out to |
54 // the sinks. | 87 // the sinks. |
55 // | 88 // |
56 // Programming model: | 89 // Programming model: |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 void set_enable_camera_list(bool enable_camera_list) { | 197 void set_enable_camera_list(bool enable_camera_list) { |
165 enable_camera_list_ = enable_camera_list; | 198 enable_camera_list_ = enable_camera_list; |
166 } | 199 } |
167 bool enable_camera_list() { | 200 bool enable_camera_list() { |
168 return enable_camera_list_; | 201 return enable_camera_list_; |
169 } | 202 } |
170 | 203 |
171 // Signal all capture state changes that are not a direct result of calling | 204 // Signal all capture state changes that are not a direct result of calling |
172 // Start(). | 205 // Start(). |
173 sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange; | 206 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; |
174 | 214 |
175 // If true, run video adaptation. By default, video adaptation is enabled | 215 // If true, run video adaptation. By default, video adaptation is enabled |
176 // and users must call video_adapter()->OnOutputFormatRequest() | 216 // and users must call video_adapter()->OnOutputFormatRequest() |
177 // to receive frames. | 217 // to receive frames. |
178 bool enable_video_adapter() const { return enable_video_adapter_; } | 218 bool enable_video_adapter() const { return enable_video_adapter_; } |
179 void set_enable_video_adapter(bool enable_video_adapter) { | 219 void set_enable_video_adapter(bool enable_video_adapter) { |
180 enable_video_adapter_ = enable_video_adapter; | 220 enable_video_adapter_ = enable_video_adapter; |
181 } | 221 } |
182 | 222 |
| 223 // Takes ownership. |
| 224 void set_frame_factory(VideoFrameFactory* frame_factory); |
| 225 |
183 bool GetInputSize(int* width, int* height); | 226 bool GetInputSize(int* width, int* height); |
184 | 227 |
185 // Implements VideoSourceInterface | 228 // Implements VideoSourceInterface |
186 void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink, | 229 void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink, |
187 const rtc::VideoSinkWants& wants) override; | 230 const rtc::VideoSinkWants& wants) override; |
188 void RemoveSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override; | 231 void RemoveSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override; |
189 | 232 |
190 protected: | 233 protected: |
191 // OnSinkWantsChanged can be overridden to change the default behavior | 234 // OnSinkWantsChanged can be overridden to change the default behavior |
192 // when a sink changes its VideoSinkWants by calling AddOrUpdateSink. | 235 // when a sink changes its VideoSinkWants by calling AddOrUpdateSink. |
(...skipping 20 matching lines...) Expand all Loading... |
213 int64_t camera_time_us, | 256 int64_t camera_time_us, |
214 int64_t system_time_us, | 257 int64_t system_time_us, |
215 int* out_width, | 258 int* out_width, |
216 int* out_height, | 259 int* out_height, |
217 int* crop_width, | 260 int* crop_width, |
218 int* crop_height, | 261 int* crop_height, |
219 int* crop_x, | 262 int* crop_x, |
220 int* crop_y, | 263 int* crop_y, |
221 int64_t* translated_camera_time_us); | 264 int64_t* translated_camera_time_us); |
222 | 265 |
| 266 // Callback attached to SignalFrameCaptured where SignalVideoFrames is called. |
| 267 void OnFrameCaptured(VideoCapturer* video_capturer, |
| 268 const CapturedFrame* captured_frame); |
| 269 |
223 // Called when a frame has been captured and converted to a | 270 // Called when a frame has been captured and converted to a |
224 // VideoFrame. OnFrame can be called directly by an implementation | 271 // VideoFrame. OnFrame can be called directly by an implementation |
225 // that does not use SignalFrameCaptured or OnFrameCaptured. The | 272 // that does not use SignalFrameCaptured or OnFrameCaptured. The |
226 // orig_width and orig_height are used only to produce stats. | 273 // orig_width and orig_height are used only to produce stats. |
227 void OnFrame(const VideoFrame& frame, int orig_width, int orig_height); | 274 void OnFrame(const VideoFrame& frame, int orig_width, int orig_height); |
228 | 275 |
229 VideoAdapter* video_adapter() { return &video_adapter_; } | 276 VideoAdapter* video_adapter() { return &video_adapter_; } |
230 | 277 |
231 void SetCaptureState(CaptureState state); | 278 void SetCaptureState(CaptureState state); |
232 | 279 |
233 // subclasses override this virtual method to provide a vector of fourccs, in | 280 // subclasses override this virtual method to provide a vector of fourccs, in |
234 // order of preference, that are expected by the media engine. | 281 // order of preference, that are expected by the media engine. |
235 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0; | 282 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0; |
236 | 283 |
237 // mutators to set private attributes | 284 // mutators to set private attributes |
238 void SetId(const std::string& id) { | 285 void SetId(const std::string& id) { |
239 id_ = id; | 286 id_ = id; |
240 } | 287 } |
241 | 288 |
242 void SetCaptureFormat(const VideoFormat* format) { | 289 void SetCaptureFormat(const VideoFormat* format) { |
243 capture_format_.reset(format ? new VideoFormat(*format) : NULL); | 290 capture_format_.reset(format ? new VideoFormat(*format) : NULL); |
244 } | 291 } |
245 | 292 |
246 void SetSupportedFormats(const std::vector<VideoFormat>& formats); | 293 void SetSupportedFormats(const std::vector<VideoFormat>& formats); |
| 294 VideoFrameFactory* frame_factory() { return frame_factory_.get(); } |
247 | 295 |
248 private: | 296 private: |
249 void Construct(); | 297 void Construct(); |
250 // Get the distance between the desired format and the supported format. | 298 // Get the distance between the desired format and the supported format. |
251 // Return the max distance if they mismatch. See the implementation for | 299 // Return the max distance if they mismatch. See the implementation for |
252 // details. | 300 // details. |
253 int64_t GetFormatDistance(const VideoFormat& desired, | 301 int64_t GetFormatDistance(const VideoFormat& desired, |
254 const VideoFormat& supported); | 302 const VideoFormat& supported); |
255 | 303 |
| 304 // Convert captured frame to readable string for LOG messages. |
| 305 std::string ToString(const CapturedFrame* frame) const; |
| 306 |
256 // Updates filtered_supported_formats_ so that it contains the formats in | 307 // Updates filtered_supported_formats_ so that it contains the formats in |
257 // supported_formats_ that fulfill all applied restrictions. | 308 // supported_formats_ that fulfill all applied restrictions. |
258 void UpdateFilteredSupportedFormats(); | 309 void UpdateFilteredSupportedFormats(); |
259 // Returns true if format doesn't fulfill all applied restrictions. | 310 // Returns true if format doesn't fulfill all applied restrictions. |
260 bool ShouldFilterFormat(const VideoFormat& format) const; | 311 bool ShouldFilterFormat(const VideoFormat& format) const; |
261 | 312 |
262 void UpdateInputSize(int width, int height); | 313 void UpdateInputSize(int width, int height); |
263 | 314 |
264 rtc::ThreadChecker thread_checker_; | 315 rtc::ThreadChecker thread_checker_; |
265 std::string id_; | 316 std::string id_; |
266 CaptureState capture_state_; | 317 CaptureState capture_state_; |
| 318 std::unique_ptr<VideoFrameFactory> frame_factory_; |
267 std::unique_ptr<VideoFormat> capture_format_; | 319 std::unique_ptr<VideoFormat> capture_format_; |
268 std::vector<VideoFormat> supported_formats_; | 320 std::vector<VideoFormat> supported_formats_; |
269 std::unique_ptr<VideoFormat> max_format_; | 321 std::unique_ptr<VideoFormat> max_format_; |
270 std::vector<VideoFormat> filtered_supported_formats_; | 322 std::vector<VideoFormat> filtered_supported_formats_; |
271 | 323 |
272 bool enable_camera_list_; | 324 bool enable_camera_list_; |
273 int scaled_width_; // Current output size from ComputeScale. | 325 int scaled_width_; // Current output size from ComputeScale. |
274 int scaled_height_; | 326 int scaled_height_; |
275 | 327 |
276 rtc::VideoBroadcaster broadcaster_; | 328 rtc::VideoBroadcaster broadcaster_; |
277 bool enable_video_adapter_; | 329 bool enable_video_adapter_; |
278 VideoAdapter video_adapter_; | 330 VideoAdapter video_adapter_; |
279 | 331 |
280 rtc::CriticalSection frame_stats_crit_; | 332 rtc::CriticalSection frame_stats_crit_; |
281 // The captured frame size before potential adapation. | 333 // The captured frame size before potential adapation. |
282 bool input_size_valid_ GUARDED_BY(frame_stats_crit_) = false; | 334 bool input_size_valid_ GUARDED_BY(frame_stats_crit_) = false; |
283 int input_width_ GUARDED_BY(frame_stats_crit_); | 335 int input_width_ GUARDED_BY(frame_stats_crit_); |
284 int input_height_ GUARDED_BY(frame_stats_crit_); | 336 int input_height_ GUARDED_BY(frame_stats_crit_); |
285 | 337 |
286 // Whether capturer should apply rotation to the frame before | 338 // Whether capturer should apply rotation to the frame before signaling it. |
287 // passing it on to the registered sinks. | |
288 bool apply_rotation_; | 339 bool apply_rotation_; |
289 | 340 |
290 // State for the timestamp translation. | 341 // State for the timestamp translation. |
291 rtc::TimestampAligner timestamp_aligner_; | 342 rtc::TimestampAligner timestamp_aligner_; |
292 RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer); | 343 RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer); |
293 }; | 344 }; |
294 | 345 |
295 } // namespace cricket | 346 } // namespace cricket |
296 | 347 |
297 #endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ | 348 #endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ |
OLD | NEW |