Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / video_engine / vie_capturer.h
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 WEBRTC_VIDEO_ENGINE_VIE_CAPTURER_H_
12 #define WEBRTC_VIDEO_ENGINE_VIE_CAPTURER_H_
13
14 #include <vector>
15
16 #include "webrtc/common_types.h"
17 #include "webrtc/engine_configurations.h"
18 #include "webrtc/modules/video_capture/include/video_capture.h"
19 #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h"
20 #include "webrtc/modules/video_coding/main/interface/video_coding.h"
21 #include "webrtc/modules/video_processing/main/interface/video_processing.h"
22 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
23 #include "webrtc/system_wrappers/interface/thread_annotations.h"
24 #include "webrtc/typedefs.h"
25 #include "webrtc/video_engine/include/vie_capture.h"
26 #include "webrtc/video_engine/vie_defines.h"
27 #include "webrtc/video_engine/vie_frame_provider_base.h"
28
29 namespace webrtc {
30
31 class Config;
32 class CriticalSectionWrapper;
33 class EventWrapper;
34 class CpuOveruseObserver;
35 class OveruseFrameDetector;
36 class ProcessThread;
37 class ThreadWrapper;
38 class ViEEffectFilter;
39 class ViEEncoder;
40 struct ViEPicture;
41
42 class ViECapturer
43     : public ViEFrameProviderBase,
44       public ViEExternalCapture,
45       protected VideoCaptureDataCallback,
46       protected VideoCaptureFeedBack {
47  public:
48   static ViECapturer* CreateViECapture(int capture_id,
49                                        int engine_id,
50                                        const Config& config,
51                                        VideoCaptureModule* capture_module,
52                                        ProcessThread& module_process_thread);
53
54   static ViECapturer* CreateViECapture(
55       int capture_id,
56       int engine_id,
57       const Config& config,
58       const char* device_unique_idUTF8,
59       uint32_t device_unique_idUTF8Length,
60       ProcessThread& module_process_thread);
61
62   ~ViECapturer();
63
64   // Implements ViEFrameProviderBase.
65   int FrameCallbackChanged();
66   virtual int DeregisterFrameCallback(const ViEFrameCallback* callbackObject);
67   bool IsFrameCallbackRegistered(const ViEFrameCallback* callbackObject);
68
69   // Implements ExternalCapture.
70   virtual int IncomingFrame(unsigned char* video_frame,
71                             unsigned int video_frame_length,
72                             uint16_t width,
73                             uint16_t height,
74                             RawVideoType video_type,
75                             unsigned long long capture_time = 0);  // NOLINT
76
77   virtual int IncomingFrameI420(const ViEVideoFrameI420& video_frame,
78                                 unsigned long long capture_time = 0);  // NOLINT
79
80   virtual void SwapFrame(I420VideoFrame* frame) OVERRIDE;
81
82   // Start/Stop.
83   int32_t Start(
84       const CaptureCapability& capture_capability = CaptureCapability());
85   int32_t Stop();
86   bool Started();
87
88   // Overrides the capture delay.
89   int32_t SetCaptureDelay(int32_t delay_ms);
90
91   // Sets rotation of the incoming captured frame.
92   int32_t SetRotateCapturedFrames(const RotateCapturedFrame rotation);
93
94   // Effect filter.
95   int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
96   int32_t EnableDenoising(bool enable);
97   int32_t EnableDeflickering(bool enable);
98   int32_t EnableBrightnessAlarm(bool enable);
99
100   // Statistics observer.
101   int32_t RegisterObserver(ViECaptureObserver* observer);
102   int32_t DeRegisterObserver();
103   bool IsObserverRegistered();
104
105   // Information.
106   const char* CurrentDeviceName() const;
107
108   void RegisterCpuOveruseObserver(CpuOveruseObserver* observer);
109
110   void CpuOveruseMeasures(int* capture_jitter_ms,
111                           int* avg_encode_time_ms,
112                           int* encode_usage_percent,
113                           int* capture_queue_delay_ms_per_s) const;
114
115  protected:
116   ViECapturer(int capture_id,
117               int engine_id,
118               const Config& config,
119               ProcessThread& module_process_thread);
120
121   int32_t Init(VideoCaptureModule* capture_module);
122   int32_t Init(const char* device_unique_idUTF8,
123                uint32_t device_unique_idUTF8Length);
124
125   // Implements VideoCaptureDataCallback.
126   virtual void OnIncomingCapturedFrame(const int32_t id,
127                                        I420VideoFrame& video_frame);
128   virtual void OnCaptureDelayChanged(const int32_t id,
129                                      const int32_t delay);
130
131   // Returns true if the capture capability has been set in |StartCapture|
132   // function and may not be changed.
133   bool CaptureCapabilityFixed();
134
135   // Help function used for keeping track of VideoImageProcesingModule.
136   // Creates the module if it is needed, returns 0 on success and guarantees
137   // that the image proc module exist.
138   int32_t IncImageProcRefCount();
139   int32_t DecImageProcRefCount();
140
141   // Implements VideoCaptureFeedBack
142   virtual void OnCaptureFrameRate(const int32_t id,
143                                   const uint32_t frame_rate);
144   virtual void OnNoPictureAlarm(const int32_t id,
145                                 const VideoCaptureAlarm alarm);
146
147   // Thread functions for deliver captured frames to receivers.
148   static bool ViECaptureThreadFunction(void* obj);
149   bool ViECaptureProcess();
150
151   void DeliverI420Frame(I420VideoFrame* video_frame);
152   void DeliverCodedFrame(VideoFrame* video_frame);
153
154  private:
155   bool SwapCapturedAndDeliverFrameIfAvailable();
156
157   // Never take capture_cs_ before deliver_cs_!
158   scoped_ptr<CriticalSectionWrapper> capture_cs_;
159   scoped_ptr<CriticalSectionWrapper> deliver_cs_;
160   VideoCaptureModule* capture_module_;
161   VideoCaptureExternal* external_capture_module_;
162   ProcessThread& module_process_thread_;
163   const int capture_id_;
164
165   // Frame used in IncomingFrameI420.
166   scoped_ptr<CriticalSectionWrapper> incoming_frame_cs_;
167   I420VideoFrame incoming_frame_;
168
169   // Capture thread.
170   ThreadWrapper& capture_thread_;
171   EventWrapper& capture_event_;
172   EventWrapper& deliver_event_;
173
174   I420VideoFrame captured_frame_;
175   I420VideoFrame deliver_frame_;
176
177   // Image processing.
178   ViEEffectFilter* effect_filter_;
179   VideoProcessingModule* image_proc_module_;
180   int image_proc_module_ref_counter_;
181   VideoProcessingModule::FrameStats* deflicker_frame_stats_;
182   VideoProcessingModule::FrameStats* brightness_frame_stats_;
183   Brightness current_brightness_level_;
184   Brightness reported_brightness_level_;
185   bool denoising_enabled_;
186
187   // Statistics observer.
188   scoped_ptr<CriticalSectionWrapper> observer_cs_;
189   ViECaptureObserver* observer_ GUARDED_BY(observer_cs_.get());
190
191   CaptureCapability requested_capability_;
192
193   scoped_ptr<OveruseFrameDetector> overuse_detector_;
194 };
195
196 }  // namespace webrtc
197
198 #endif  // WEBRTC_VIDEO_ENGINE_VIE_CAPTURER_H_