1/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_MEDIA_DEVICES_DEVICEMANAGER_H_
29#define TALK_MEDIA_DEVICES_DEVICEMANAGER_H_
30
31#include <map>
32#include <string>
33#include <vector>
34
35#include "talk/media/base/device.h"
36#include "talk/media/base/screencastid.h"
37#include "talk/media/base/videocapturerfactory.h"
38#include "talk/media/base/videocommon.h"
39#include "webrtc/base/scoped_ptr.h"
40#include "webrtc/base/sigslot.h"
41#include "webrtc/base/stringencode.h"
42#include "webrtc/base/window.h"
43
44namespace rtc {
45
46class DesktopDescription;
47class WindowDescription;
48class WindowPicker;
49
50}
51namespace cricket {
52
53class VideoCapturer;
54
55// DeviceManagerInterface - interface to manage the audio and
56// video devices on the system.
57class DeviceManagerInterface {
58 public:
59  virtual ~DeviceManagerInterface() { }
60
61  // Initialization
62  virtual bool Init() = 0;
63  virtual void Terminate() = 0;
64
65  // Capabilities
66  virtual int GetCapabilities() = 0;
67
68  // Device enumeration
69  virtual bool GetAudioInputDevices(std::vector<Device>* devices) = 0;
70  virtual bool GetAudioOutputDevices(std::vector<Device>* devices) = 0;
71
72  virtual bool GetAudioInputDevice(const std::string& name, Device* out) = 0;
73  virtual bool GetAudioOutputDevice(const std::string& name, Device* out) = 0;
74
75  virtual bool GetVideoCaptureDevices(std::vector<Device>* devs) = 0;
76  virtual bool GetVideoCaptureDevice(const std::string& name, Device* out) = 0;
77
78  // If the device manager needs to create video capturers, here is
79  // how to control which video capturers are created.  These take
80  // ownership of the factories.
81  virtual void SetVideoDeviceCapturerFactory(
82      VideoDeviceCapturerFactory* video_device_capturer_factory) = 0;
83  virtual void SetScreenCapturerFactory(
84      ScreenCapturerFactory* screen_capturer_factory) = 0;
85
86  // Caps the capture format according to max format for capturers created
87  // by CreateVideoCapturer(). See ConstrainSupportedFormats() in
88  // videocapturer.h for more detail.
89  // Note that once a VideoCapturer has been created, calling this API will
90  // not affect it.
91  virtual void SetVideoCaptureDeviceMaxFormat(
92      const std::string& usb_id,
93      const VideoFormat& max_format) = 0;
94  virtual void ClearVideoCaptureDeviceMaxFormat(const std::string& usb_id) = 0;
95
96  // Device creation
97  virtual VideoCapturer* CreateVideoCapturer(const Device& device) const = 0;
98
99  virtual bool GetWindows(
100      std::vector<rtc::WindowDescription>* descriptions) = 0;
101  virtual bool GetDesktops(
102      std::vector<rtc::DesktopDescription>* descriptions) = 0;
103  virtual VideoCapturer* CreateScreenCapturer(
104      const ScreencastId& screenid) const = 0;
105
106  sigslot::signal0<> SignalDevicesChange;
107
108  static const char kDefaultDeviceName[];
109};
110
111class DeviceWatcher {
112 public:
113  explicit DeviceWatcher(DeviceManagerInterface* dm) {}
114  virtual ~DeviceWatcher() {}
115  virtual bool Start() { return true; }
116  virtual void Stop() {}
117};
118
119class DeviceManagerFactory {
120 public:
121  static DeviceManagerInterface* Create();
122
123 private:
124  DeviceManagerFactory() {}
125};
126
127class DeviceManager : public DeviceManagerInterface {
128 public:
129  DeviceManager();
130  virtual ~DeviceManager();
131
132  // Initialization
133  virtual bool Init();
134  virtual void Terminate();
135
136  // Capabilities
137  virtual int GetCapabilities();
138
139  // Device enumeration
140  virtual bool GetAudioInputDevices(std::vector<Device>* devices);
141  virtual bool GetAudioOutputDevices(std::vector<Device>* devices);
142
143  virtual bool GetAudioInputDevice(const std::string& name, Device* out);
144  virtual bool GetAudioOutputDevice(const std::string& name, Device* out);
145
146  virtual bool GetVideoCaptureDevices(std::vector<Device>* devs);
147  virtual bool GetVideoCaptureDevice(const std::string& name, Device* out);
148
149  virtual void SetVideoDeviceCapturerFactory(
150      VideoDeviceCapturerFactory* video_device_capturer_factory) {
151    video_device_capturer_factory_.reset(video_device_capturer_factory);
152  }
153  virtual void SetScreenCapturerFactory(
154      ScreenCapturerFactory* screen_capturer_factory) {
155    screen_capturer_factory_.reset(screen_capturer_factory);
156  }
157
158
159  virtual void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
160                                              const VideoFormat& max_format);
161  virtual void ClearVideoCaptureDeviceMaxFormat(const std::string& usb_id);
162
163  // TODO(pthatcher): Rename to CreateVideoDeviceCapturer.
164  virtual VideoCapturer* CreateVideoCapturer(const Device& device) const;
165
166  virtual bool GetWindows(
167      std::vector<rtc::WindowDescription>* descriptions);
168  virtual bool GetDesktops(
169      std::vector<rtc::DesktopDescription>* descriptions);
170  virtual VideoCapturer* CreateScreenCapturer(
171      const ScreencastId& screenid) const;
172
173  // The exclusion_list MUST be a NULL terminated list.
174  static bool FilterDevices(std::vector<Device>* devices,
175      const char* const exclusion_list[]);
176  bool initialized() const { return initialized_; }
177
178 protected:
179  virtual bool GetAudioDevices(bool input, std::vector<Device>* devs);
180  virtual bool GetAudioDevice(bool is_input, const std::string& name,
181                              Device* out);
182  virtual bool GetDefaultVideoCaptureDevice(Device* device);
183  bool IsInWhitelist(const std::string& key, VideoFormat* video_format) const;
184  virtual bool GetMaxFormat(const Device& device,
185                            VideoFormat* video_format) const;
186
187  void set_initialized(bool initialized) { initialized_ = initialized; }
188
189  void set_watcher(DeviceWatcher* watcher) { watcher_.reset(watcher); }
190  DeviceWatcher* watcher() { return watcher_.get(); }
191
192 private:
193  // The exclusion_list MUST be a NULL terminated list.
194  static bool ShouldDeviceBeIgnored(const std::string& device_name,
195      const char* const exclusion_list[]);
196  bool GetFakeVideoCaptureDevice(const std::string& name, Device* out) const;
197  VideoCapturer* MaybeConstructFakeVideoCapturer(const Device& device) const;
198
199  bool initialized_;
200  rtc::scoped_ptr<
201    VideoDeviceCapturerFactory> video_device_capturer_factory_;
202  rtc::scoped_ptr<
203    ScreenCapturerFactory> screen_capturer_factory_;
204  std::map<std::string, VideoFormat> max_formats_;
205  rtc::scoped_ptr<DeviceWatcher> watcher_;
206  rtc::scoped_ptr<rtc::WindowPicker> window_picker_;
207};
208
209}  // namespace cricket
210
211#endif  // TALK_MEDIA_DEVICES_DEVICEMANAGER_H_
212