aboutsummaryrefslogtreecommitdiff
path: root/talk/media/devices/fakedevicemanager.h
blob: 77a83424b2f12ba063369953586d02c7fbe6fe04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * libjingle
 * Copyright 2008 Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef TALK_MEDIA_DEVICES_FAKEDEVICEMANAGER_H_
#define TALK_MEDIA_DEVICES_FAKEDEVICEMANAGER_H_

#include <string>
#include <vector>

#include "talk/media/base/fakevideocapturer.h"
#include "talk/media/base/mediacommon.h"
#include "talk/media/devices/devicemanager.h"
#include "webrtc/base/scoped_ptr.h"
#include "webrtc/base/window.h"
#include "webrtc/base/windowpicker.h"

namespace cricket {

class FakeDeviceManager : public DeviceManagerInterface {
 public:
  FakeDeviceManager() {}
  virtual bool Init() {
    return true;
  }
  virtual void Terminate() {
  }
  virtual int GetCapabilities() {
    std::vector<Device> devices;
    int caps = VIDEO_RECV;
    if (!input_devices_.empty()) {
      caps |= AUDIO_SEND;
    }
    if (!output_devices_.empty()) {
      caps |= AUDIO_RECV;
    }
    if (!vidcap_devices_.empty()) {
      caps |= VIDEO_SEND;
    }
    return caps;
  }
  virtual bool GetAudioInputDevices(std::vector<Device>* devs) {
    *devs = input_devices_;
    return true;
  }
  virtual bool GetAudioOutputDevices(std::vector<Device>* devs) {
    *devs = output_devices_;
    return true;
  }
  virtual bool GetAudioInputDevice(const std::string& name, Device* out) {
    return GetAudioDevice(true, name, out);
  }
  virtual bool GetAudioOutputDevice(const std::string& name, Device* out) {
    return GetAudioDevice(false, name, out);
  }
  virtual bool GetVideoCaptureDevices(std::vector<Device>* devs) {
    *devs = vidcap_devices_;
    return true;
  }
  virtual void SetVideoDeviceCapturerFactory(
      VideoDeviceCapturerFactory* video_device_capturer_factory) {
  }
  virtual void SetScreenCapturerFactory(
      ScreenCapturerFactory* screen_capturer_factory) {
    screen_capturer_factory_.reset(screen_capturer_factory);
  }
  virtual void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
                                              const VideoFormat& max_format) {
    max_formats_[usb_id] = max_format;
  }
  bool IsMaxFormatForDevice(const std::string& usb_id,
                            const VideoFormat& max_format) const {
    std::map<std::string, VideoFormat>::const_iterator found =
        max_formats_.find(usb_id);
    return (found != max_formats_.end()) ?
        max_format == found->second :
        false;
  }
  virtual void ClearVideoCaptureDeviceMaxFormat(const std::string& usb_id) {
    max_formats_.erase(usb_id);
  }
  virtual VideoCapturer* CreateVideoCapturer(const Device& device) const {
    return new FakeVideoCapturer();
  }
  virtual VideoCapturer* CreateScreenCapturer(
      const ScreencastId& screenid) const {
    if (!screen_capturer_factory_) {
      return new FakeVideoCapturer();
    }
    return screen_capturer_factory_->Create(screenid);
  }
  virtual bool GetWindows(
      std::vector<rtc::WindowDescription>* descriptions) {
    descriptions->clear();
    const uint32_t id = 1u;  // Note that 0 is not a valid ID.
    const rtc::WindowId window_id =
        rtc::WindowId::Cast(id);
    std::string title = "FakeWindow";
    rtc::WindowDescription window_description(window_id, title);
    descriptions->push_back(window_description);
    return true;
  }
  virtual VideoCapturer* CreateWindowCapturer(rtc::WindowId window) {
    if (!window.IsValid()) {
      return NULL;
    }
    return new FakeVideoCapturer;
  }
  virtual bool GetDesktops(
      std::vector<rtc::DesktopDescription>* descriptions) {
    descriptions->clear();
    const int id = 0;
    const int valid_index = 0;
    const rtc::DesktopId desktop_id =
        rtc::DesktopId::Cast(id, valid_index);
    std::string title = "FakeDesktop";
    rtc::DesktopDescription desktop_description(desktop_id, title);
    descriptions->push_back(desktop_description);
    return true;
  }
  virtual VideoCapturer* CreateDesktopCapturer(rtc::DesktopId desktop) {
    if (!desktop.IsValid()) {
      return NULL;
    }
    return new FakeVideoCapturer;
  }

  virtual bool GetDefaultVideoCaptureDevice(Device* device) {
    if (vidcap_devices_.empty()) {
      return false;
    }
    *device = vidcap_devices_[0];
    return true;
  }

#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
  bool QtKitToSgDevice(const std::string& qtkit_name, Device* out) {
    out->name = qtkit_name;
    out->id = "sg:" + qtkit_name;
    return true;
  }
#endif

  void SetAudioInputDevices(const std::vector<std::string>& devices) {
    input_devices_.clear();
    for (size_t i = 0; i < devices.size(); ++i) {
      input_devices_.push_back(Device(devices[i],
                                      static_cast<int>(i)));
    }
    SignalDevicesChange();
  }
  void SetAudioOutputDevices(const std::vector<std::string>& devices) {
    output_devices_.clear();
    for (size_t i = 0; i < devices.size(); ++i) {
      output_devices_.push_back(Device(devices[i],
                                       static_cast<int>(i)));
    }
    SignalDevicesChange();
  }
  void SetVideoCaptureDevices(const std::vector<std::string>& devices) {
    vidcap_devices_.clear();
    for (size_t i = 0; i < devices.size(); ++i) {
      vidcap_devices_.push_back(Device(devices[i],
                                       static_cast<int>(i)));
    }
    SignalDevicesChange();
  }
  virtual bool GetVideoCaptureDevice(const std::string& name,
                                     Device* out) {
    if (vidcap_devices_.empty())
      return false;

    // If the name is empty, return the default device.
    if (name.empty() || name == kDefaultDeviceName) {
      *out = vidcap_devices_[0];
      return true;
    }

    return FindDeviceByName(vidcap_devices_, name, out);
  }
  bool GetAudioDevice(bool is_input, const std::string& name,
                      Device* out) {
    // If the name is empty, return the default device.
    if (name.empty() || name == kDefaultDeviceName) {
      *out = Device(name, -1);
      return true;
    }

    return FindDeviceByName((is_input ? input_devices_ : output_devices_),
                            name, out);
  }
  static bool FindDeviceByName(const std::vector<Device>& devices,
                               const std::string& name,
                               Device* out) {
    for (std::vector<Device>::const_iterator it = devices.begin();
         it != devices.end(); ++it) {
      if (name == it->name) {
        *out = *it;
        return true;
      }
    }
    return false;
  }

 private:
  std::vector<Device> input_devices_;
  std::vector<Device> output_devices_;
  std::vector<Device> vidcap_devices_;
  std::map<std::string, VideoFormat> max_formats_;
  rtc::scoped_ptr<
    ScreenCapturerFactory> screen_capturer_factory_;
};

}  // namespace cricket

#endif  // TALK_MEDIA_DEVICES_FAKEDEVICEMANAGER_H_