video_capture_impl.h (5472B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 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 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_ 12 #define MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 17 #include "api/scoped_refptr.h" 18 #include "api/sequence_checker.h" 19 #include "api/video/video_frame.h" 20 #include "api/video/video_rotation.h" 21 #include "api/video/video_sink_interface.h" 22 #include "modules/video_capture/raw_video_sink_interface.h" 23 #include "modules/video_capture/video_capture.h" 24 #include "modules/video_capture/video_capture_config.h" 25 #include "modules/video_capture/video_capture_defines.h" 26 #include "rtc_base/race_checker.h" 27 #include "rtc_base/synchronization/mutex.h" 28 #include "rtc_base/system/rtc_export.h" 29 #include "rtc_base/thread_annotations.h" 30 #include "system_wrappers/include/clock.h" 31 32 namespace webrtc { 33 34 class VideoCaptureOptions; 35 36 namespace videocapturemodule { 37 // Class definitions 38 class RTC_EXPORT VideoCaptureImpl : public VideoCaptureModule { 39 public: 40 /* 41 * Create a video capture module object 42 * 43 * id - unique identifier of this video capture module object 44 * deviceUniqueIdUTF8 - name of the device. Available names can be found by 45 * using GetDeviceName 46 */ 47 static scoped_refptr<VideoCaptureModule> Create( 48 Clock* clock, 49 const char* deviceUniqueIdUTF8); 50 static scoped_refptr<VideoCaptureModule> Create( 51 Clock* clock, 52 VideoCaptureOptions* options, 53 const char* deviceUniqueIdUTF8); 54 55 static DeviceInfo* CreateDeviceInfo(); 56 static DeviceInfo* CreateDeviceInfo(VideoCaptureOptions* options); 57 58 // Helpers for converting between (integral) degrees and 59 // VideoRotation values. Return 0 on success. 60 static int32_t RotationFromDegrees(int degrees, VideoRotation* rotation); 61 static int32_t RotationInDegrees(VideoRotation rotation, int* degrees); 62 63 // Call backs 64 void RegisterCaptureDataCallback( 65 VideoSinkInterface<VideoFrame>* dataCallback) override; 66 virtual void RegisterCaptureDataCallback( 67 RawVideoSinkInterface* dataCallback) override; 68 void DeRegisterCaptureDataCallback( 69 webrtc::VideoSinkInterface<VideoFrame>* dataCallback) override; 70 71 int32_t StopCaptureIfAllClientsClose() override; 72 int32_t SetCaptureRotation(VideoRotation rotation) override; 73 bool SetApplyRotation(bool enable) override; 74 bool GetApplyRotation() override; 75 76 const char* CurrentDeviceName() const override; 77 78 // `capture_time` must be specified in NTP time format in milliseconds. 79 int32_t IncomingFrame(uint8_t* videoFrame, 80 size_t videoFrameLength, 81 const VideoCaptureCapability& frameInfo, 82 int64_t captureTime = 0); 83 84 // Platform dependent 85 int32_t StartCapture(const VideoCaptureCapability& capability) override; 86 int32_t StopCapture() override; 87 bool CaptureStarted() override; 88 int32_t CaptureSettings(VideoCaptureCapability& /*settings*/) override; 89 90 protected: 91 explicit VideoCaptureImpl(Clock* clock); 92 ~VideoCaptureImpl() override; 93 94 // Calls to the public API must happen on a single thread. 95 SequenceChecker api_checker_; 96 // RaceChecker for members that can be accessed on the API thread while 97 // capture is not happening, and on a callback thread otherwise. 98 RaceChecker capture_checker_; 99 // current Device unique name; 100 char* _deviceUniqueId RTC_GUARDED_BY(api_checker_); 101 102 // moved DeliverCapturedFrame to protected for VideoCaptureAndroid (mjf) 103 int32_t DeliverCapturedFrame(VideoFrame& captureFrame) 104 RTC_EXCLUSIVE_LOCKS_REQUIRED(api_lock_); 105 Mutex api_lock_; 106 // Should be set by platform dependent code in StartCapture. 107 VideoCaptureCapability _requestedCapability RTC_GUARDED_BY(api_checker_); 108 109 private: 110 void UpdateFrameCount(); 111 uint32_t CalculateFrameRate(int64_t now_ns); 112 void DeliverRawFrame(uint8_t* videoFrame, 113 size_t videoFrameLength, 114 const VideoCaptureCapability& frameInfo, 115 int64_t captureTime) 116 RTC_EXCLUSIVE_LOCKS_REQUIRED(api_lock_); 117 118 // last time the module process function was called. 119 int64_t _lastProcessTimeNanos RTC_GUARDED_BY(capture_checker_); 120 // last time the frame rate callback function was called. 121 int64_t _lastFrameRateCallbackTimeNanos RTC_GUARDED_BY(capture_checker_); 122 123 std::set<VideoSinkInterface<VideoFrame>*> _dataCallBacks RTC_GUARDED_BY(api_lock_); 124 RawVideoSinkInterface* _rawDataCallBack RTC_GUARDED_BY(api_lock_); 125 126 int64_t _lastProcessFrameTimeNanos RTC_GUARDED_BY(capture_checker_); 127 // timestamp for local captured frames 128 int64_t _incomingFrameTimesNanos[kFrameRateCountHistorySize] RTC_GUARDED_BY( 129 capture_checker_); 130 // Set if the frame should be rotated by the capture module. 131 VideoRotation _rotateFrame RTC_GUARDED_BY(api_lock_); 132 133 // Indicate whether rotation should be applied before delivered externally. 134 bool apply_rotation_ RTC_GUARDED_BY(api_lock_); 135 136 Clock* const clock_; 137 }; 138 } // namespace videocapturemodule 139 } // namespace webrtc 140 #endif // MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_