blob: d25259764357b3c9d49703eb482e87bfb3dfef76 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org1745e932012-03-01 16:30:40 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_
12#define MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
14/*
15 * video_capture_impl.h
16 */
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/video_frame.h"
19#include "common_video/libyuv/include/webrtc_libyuv.h"
20#include "modules/video_capture/video_capture.h"
21#include "modules/video_capture/video_capture_config.h"
22#include "rtc_base/criticalsection.h"
23#include "rtc_base/scoped_ref_ptr.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
25namespace webrtc
26{
niklase@google.com470e71d2011-07-07 08:21:25 +000027
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000028namespace videocapturemodule {
niklase@google.com470e71d2011-07-07 08:21:25 +000029// Class definitions
30class VideoCaptureImpl: public VideoCaptureModule, public VideoCaptureExternal
31{
32public:
33
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000034 /*
35 * Create a video capture module object
36 *
37 * id - unique identifier of this video capture module object
38 * deviceUniqueIdUTF8 - name of the device. Available names can be found by using GetDeviceName
39 */
Peter Boström1d194412016-03-21 16:44:31 +010040 static rtc::scoped_refptr<VideoCaptureModule> Create(
Peter Boström1d194412016-03-21 16:44:31 +010041 const char* deviceUniqueIdUTF8);
niklase@google.com470e71d2011-07-07 08:21:25 +000042
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000043 /*
44 * Create a video capture module object used for external capture.
45 *
46 * id - unique identifier of this video capture module object
47 * externalCapture - [out] interface to call when a new frame is captured.
48 */
Peter Boström1d194412016-03-21 16:44:31 +010049 static rtc::scoped_refptr<VideoCaptureModule> Create(
Peter Boström1d194412016-03-21 16:44:31 +010050 VideoCaptureExternal*& externalCapture);
niklase@google.com470e71d2011-07-07 08:21:25 +000051
nisseb29b9c82016-12-12 00:22:56 -080052 static DeviceInfo* CreateDeviceInfo();
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000053
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000054 // Helpers for converting between (integral) degrees and
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000055 // VideoRotation values. Return 0 on success.
56 static int32_t RotationFromDegrees(int degrees, VideoRotation* rotation);
57 static int32_t RotationInDegrees(VideoRotation rotation, int* degrees);
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000058
niklase@google.com470e71d2011-07-07 08:21:25 +000059 //Call backs
nisseb29b9c82016-12-12 00:22:56 -080060 void RegisterCaptureDataCallback(
61 rtc::VideoSinkInterface<VideoFrame>* dataCallback) override;
62 void DeRegisterCaptureDataCallback() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000063
nisseb29b9c82016-12-12 00:22:56 -080064 int32_t SetCaptureRotation(VideoRotation rotation) override;
65 bool SetApplyRotation(bool enable) override;
66 bool GetApplyRotation() override {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000067 return apply_rotation_;
68 }
niklase@google.com470e71d2011-07-07 08:21:25 +000069
nisseb29b9c82016-12-12 00:22:56 -080070 const char* CurrentDeviceName() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000071
72 // Implement VideoCaptureExternal
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000073 // |capture_time| must be specified in NTP time format in milliseconds.
nisseb29b9c82016-12-12 00:22:56 -080074 int32_t IncomingFrame(uint8_t* videoFrame,
75 size_t videoFrameLength,
76 const VideoCaptureCapability& frameInfo,
77 int64_t captureTime = 0) override;
pbos@webrtc.org2ffb1492013-11-22 13:10:13 +000078
niklase@google.com470e71d2011-07-07 08:21:25 +000079 // Platform dependent
nisseb29b9c82016-12-12 00:22:56 -080080 int32_t StartCapture(const VideoCaptureCapability& capability) override
niklase@google.com470e71d2011-07-07 08:21:25 +000081 {
82 _requestedCapability = capability;
83 return -1;
84 }
nisseb29b9c82016-12-12 00:22:56 -080085 int32_t StopCapture() override { return -1; }
86 bool CaptureStarted() override {return false; }
87 int32_t CaptureSettings(VideoCaptureCapability& /*settings*/) override
niklase@google.com470e71d2011-07-07 08:21:25 +000088 { return -1; }
niklase@google.com470e71d2011-07-07 08:21:25 +000089
90protected:
nisseb29b9c82016-12-12 00:22:56 -080091 VideoCaptureImpl();
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000092 virtual ~VideoCaptureImpl();
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070093 int32_t DeliverCapturedFrame(VideoFrame& captureFrame);
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000094
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000095 char* _deviceUniqueId; // current Device unique name;
kthelgasonff046c72017-03-31 02:03:55 -070096 rtc::CriticalSection _apiCs;
niklase@google.com470e71d2011-07-07 08:21:25 +000097 VideoCaptureCapability _requestedCapability; // Should be set by platform dependent code in StartCapture.
98private:
99 void UpdateFrameCount();
Niels Möllerd28db7f2016-05-10 16:31:47 +0200100 uint32_t CalculateFrameRate(int64_t now_ns);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
Niels Möllerd28db7f2016-05-10 16:31:47 +0200102 // last time the module process function was called.
103 int64_t _lastProcessTimeNanos;
104 // last time the frame rate callback function was called.
105 int64_t _lastFrameRateCallbackTimeNanos;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
nisseb29b9c82016-12-12 00:22:56 -0800107 rtc::VideoSinkInterface<VideoFrame>* _dataCallBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
Niels Möllerd28db7f2016-05-10 16:31:47 +0200109 int64_t _lastProcessFrameTimeNanos;
110 // timestamp for local captured frames
111 int64_t _incomingFrameTimesNanos[kFrameRateCountHistorySize];
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000112 VideoRotation _rotateFrame; // Set if the frame should be rotated by the
113 // capture module.
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000115 // Indicate whether rotation should be applied before delivered externally.
116 bool apply_rotation_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000118} // namespace videocapturemodule
119} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200120#endif // MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_