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_VIDEO_CAPTURE_H_
12 #define MODULES_VIDEO_CAPTURE_VIDEO_CAPTURE_H_
13 
14 #include "modules/audio_processing/include/config.h"
15 #include "api/video/video_rotation.h"
16 #include "api/video/video_sink_interface.h"
17 #include "modules/include/module.h"
18 #include "modules/desktop_capture/desktop_capture_types.h"
19 #include "modules/video_capture/video_capture_defines.h"
20 #include <set>
21 
22 #if defined(ANDROID)
23 #include <jni.h>
24 #endif
25 
26 namespace webrtc {
27 
28 // Mozilla addition
29 enum class CaptureDeviceType {
30   Camera,
31   Screen,
32   Window,
33   Browser
34 };
35 // Mozilla addition
36 
37 struct CaptureDeviceInfo {
38   CaptureDeviceType type;
39 
CaptureDeviceInfoCaptureDeviceInfo40   CaptureDeviceInfo() : type(CaptureDeviceType::Camera) {}
CaptureDeviceInfoCaptureDeviceInfo41   CaptureDeviceInfo(CaptureDeviceType t) : type(t) {}
42 
43   static const ConfigOptionID identifier = ConfigOptionID::kCaptureDeviceInfo;
TypeNameCaptureDeviceInfo44   const char * TypeName() const
45   {
46     switch(type) {
47     case CaptureDeviceType::Camera: {
48       return "Camera";
49     }
50     case CaptureDeviceType::Screen: {
51       return "Screen";
52     }
53     case CaptureDeviceType::Window: {
54       return "Window";
55     }
56     case CaptureDeviceType::Browser: {
57       return "Browser";
58     }
59     }
60     assert(false);
61     return "UNKOWN-CaptureDeviceType!";
62   }
63 };
64 
65 class VideoInputFeedBack
66 {
67 public:
68     virtual void OnDeviceChange() = 0;
69 protected:
~VideoInputFeedBack()70     virtual ~VideoInputFeedBack(){}
71 };
72 
73 #if defined(ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD)
74   int32_t SetCaptureAndroidVM(JavaVM* javaVM);
75 #endif
76 
77 class VideoCaptureModule : public rtc::RefCountInterface {
78  public:
79   // Interface for receiving information about available camera devices.
80   class DeviceInfo {
81    public:
82     virtual uint32_t NumberOfDevices() = 0;
83     virtual int32_t Refresh() = 0;
DeviceChange()84     virtual void DeviceChange() {
85       for (auto inputCallBack : _inputCallBacks) {
86         inputCallBack->OnDeviceChange();
87       }
88     }
RegisterVideoInputFeedBack(VideoInputFeedBack * callBack)89     virtual void RegisterVideoInputFeedBack(VideoInputFeedBack* callBack) {
90       _inputCallBacks.insert(callBack);
91     }
92 
DeRegisterVideoInputFeedBack(VideoInputFeedBack * callBack)93     virtual void DeRegisterVideoInputFeedBack(VideoInputFeedBack* callBack) {
94       auto it = _inputCallBacks.find(callBack);
95       if (it != _inputCallBacks.end()) {
96         _inputCallBacks.erase(it);
97       }
98     }
99 
100     // Returns the available capture devices.
101     // deviceNumber   - Index of capture device.
102     // deviceNameUTF8 - Friendly name of the capture device.
103     // deviceUniqueIdUTF8 - Unique name of the capture device if it exist.
104     //                      Otherwise same as deviceNameUTF8.
105     // productUniqueIdUTF8 - Unique product id if it exist.
106     //                       Null terminated otherwise.
107     virtual int32_t GetDeviceName(uint32_t deviceNumber,
108                                   char* deviceNameUTF8,
109                                   uint32_t deviceNameLength,
110                                   char* deviceUniqueIdUTF8,
111                                   uint32_t deviceUniqueIdUTF8Length,
112                                   char* productUniqueIdUTF8 = 0,
113                                   uint32_t productUniqueIdUTF8Length = 0,
114                                   pid_t* pid = 0) = 0;
115 
116     // Returns the number of capabilities this device.
117     virtual int32_t NumberOfCapabilities(const char* deviceUniqueIdUTF8) = 0;
118 
119     // Gets the capabilities of the named device.
120     virtual int32_t GetCapability(const char* deviceUniqueIdUTF8,
121                                   const uint32_t deviceCapabilityNumber,
122                                   VideoCaptureCapability& capability) = 0;
123 
124     // Gets clockwise angle the captured frames should be rotated in order
125     // to be displayed correctly on a normally rotated display.
126     virtual int32_t GetOrientation(const char* deviceUniqueIdUTF8,
127                                    VideoRotation& orientation) = 0;
128 
129     // Gets the capability that best matches the requested width, height and
130     // frame rate.
131     // Returns the deviceCapabilityNumber on success.
132     virtual int32_t GetBestMatchedCapability(
133         const char* deviceUniqueIdUTF8,
134         const VideoCaptureCapability& requested,
135         VideoCaptureCapability& resulting) = 0;
136 
137     // Display OS /capture device specific settings dialog
138     virtual int32_t DisplayCaptureSettingsDialogBox(
139         const char* deviceUniqueIdUTF8,
140         const char* dialogTitleUTF8,
141         void* parentWindow,
142         uint32_t positionX,
143         uint32_t positionY) = 0;
144 
~DeviceInfo()145     virtual ~DeviceInfo() {}
146    private:
147     std::set<VideoInputFeedBack*> _inputCallBacks;
148   };
149 
150   //   Register capture data callback
151   virtual void RegisterCaptureDataCallback(
152       rtc::VideoSinkInterface<VideoFrame>* dataCallback) = 0;
153 
154   //  Remove capture data callback
155   virtual void DeRegisterCaptureDataCallback(
156       rtc::VideoSinkInterface<VideoFrame> *dataCallback) = 0;
157 
158   // Start capture device
159   virtual int32_t StartCapture(const VideoCaptureCapability& capability) = 0;
160 
161   virtual int32_t StopCaptureIfAllClientsClose() = 0;
162 
FocusOnSelectedSource()163   virtual bool FocusOnSelectedSource() { return false; }
164 
165   virtual int32_t StopCapture() = 0;
166 
167   // Returns the name of the device used by this module.
168   virtual const char* CurrentDeviceName() const = 0;
169 
170   // Returns true if the capture device is running
171   virtual bool CaptureStarted() = 0;
172 
173   // Gets the current configuration.
174   virtual int32_t CaptureSettings(VideoCaptureCapability& settings) = 0;
175 
176   // Set the rotation of the captured frames.
177   // If the rotation is set to the same as returned by
178   // DeviceInfo::GetOrientation the captured frames are
179   // displayed correctly if rendered.
180   virtual int32_t SetCaptureRotation(VideoRotation rotation) = 0;
181 
182   // Tells the capture module whether to apply the pending rotation. By default,
183   // the rotation is applied and the generated frame is up right. When set to
184   // false, generated frames will carry the rotation information from
185   // SetCaptureRotation. Return value indicates whether this operation succeeds.
186   virtual bool SetApplyRotation(bool enable) = 0;
187 
188   // Return whether the rotation is applied or left pending.
189   virtual bool GetApplyRotation() = 0;
190 
191  protected:
~VideoCaptureModule()192   ~VideoCaptureModule() override {}
193 };
194 
195 }  // namespace webrtc
196 #endif  // MODULES_VIDEO_CAPTURE_VIDEO_CAPTURE_H_
197