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