Home | History | Annotate | Download | only in devices
      1 /*
      2  * libjingle
      3  * Copyright 2008 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_FAKEDEVICEMANAGER_H_
     29 #define TALK_MEDIA_DEVICES_FAKEDEVICEMANAGER_H_
     30 
     31 #include <string>
     32 #include <vector>
     33 
     34 #include "talk/media/base/fakevideocapturer.h"
     35 #include "talk/media/base/mediacommon.h"
     36 #include "talk/media/devices/devicemanager.h"
     37 #include "webrtc/base/scoped_ptr.h"
     38 #include "webrtc/base/window.h"
     39 #include "webrtc/base/windowpicker.h"
     40 
     41 namespace cricket {
     42 
     43 class FakeDeviceManager : public DeviceManagerInterface {
     44  public:
     45   FakeDeviceManager() {}
     46   virtual bool Init() {
     47     return true;
     48   }
     49   virtual void Terminate() {
     50   }
     51   virtual int GetCapabilities() {
     52     std::vector<Device> devices;
     53     int caps = VIDEO_RECV;
     54     if (!input_devices_.empty()) {
     55       caps |= AUDIO_SEND;
     56     }
     57     if (!output_devices_.empty()) {
     58       caps |= AUDIO_RECV;
     59     }
     60     if (!vidcap_devices_.empty()) {
     61       caps |= VIDEO_SEND;
     62     }
     63     return caps;
     64   }
     65   virtual bool GetAudioInputDevices(std::vector<Device>* devs) {
     66     *devs = input_devices_;
     67     return true;
     68   }
     69   virtual bool GetAudioOutputDevices(std::vector<Device>* devs) {
     70     *devs = output_devices_;
     71     return true;
     72   }
     73   virtual bool GetAudioInputDevice(const std::string& name, Device* out) {
     74     return GetAudioDevice(true, name, out);
     75   }
     76   virtual bool GetAudioOutputDevice(const std::string& name, Device* out) {
     77     return GetAudioDevice(false, name, out);
     78   }
     79   virtual bool GetVideoCaptureDevices(std::vector<Device>* devs) {
     80     *devs = vidcap_devices_;
     81     return true;
     82   }
     83   virtual void SetVideoDeviceCapturerFactory(
     84       VideoDeviceCapturerFactory* video_device_capturer_factory) {
     85   }
     86   virtual void SetScreenCapturerFactory(
     87       ScreenCapturerFactory* screen_capturer_factory) {
     88     screen_capturer_factory_.reset(screen_capturer_factory);
     89   }
     90   virtual void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
     91                                               const VideoFormat& max_format) {
     92     max_formats_[usb_id] = max_format;
     93   }
     94   bool IsMaxFormatForDevice(const std::string& usb_id,
     95                             const VideoFormat& max_format) const {
     96     std::map<std::string, VideoFormat>::const_iterator found =
     97         max_formats_.find(usb_id);
     98     return (found != max_formats_.end()) ?
     99         max_format == found->second :
    100         false;
    101   }
    102   virtual void ClearVideoCaptureDeviceMaxFormat(const std::string& usb_id) {
    103     max_formats_.erase(usb_id);
    104   }
    105   virtual VideoCapturer* CreateVideoCapturer(const Device& device) const {
    106     return new FakeVideoCapturer();
    107   }
    108   virtual VideoCapturer* CreateScreenCapturer(
    109       const ScreencastId& screenid) const {
    110     if (!screen_capturer_factory_) {
    111       return new FakeVideoCapturer();
    112     }
    113     return screen_capturer_factory_->Create(screenid);
    114   }
    115   virtual bool GetWindows(
    116       std::vector<rtc::WindowDescription>* descriptions) {
    117     descriptions->clear();
    118     const uint32_t id = 1u;  // Note that 0 is not a valid ID.
    119     const rtc::WindowId window_id =
    120         rtc::WindowId::Cast(id);
    121     std::string title = "FakeWindow";
    122     rtc::WindowDescription window_description(window_id, title);
    123     descriptions->push_back(window_description);
    124     return true;
    125   }
    126   virtual VideoCapturer* CreateWindowCapturer(rtc::WindowId window) {
    127     if (!window.IsValid()) {
    128       return NULL;
    129     }
    130     return new FakeVideoCapturer;
    131   }
    132   virtual bool GetDesktops(
    133       std::vector<rtc::DesktopDescription>* descriptions) {
    134     descriptions->clear();
    135     const int id = 0;
    136     const int valid_index = 0;
    137     const rtc::DesktopId desktop_id =
    138         rtc::DesktopId::Cast(id, valid_index);
    139     std::string title = "FakeDesktop";
    140     rtc::DesktopDescription desktop_description(desktop_id, title);
    141     descriptions->push_back(desktop_description);
    142     return true;
    143   }
    144   virtual VideoCapturer* CreateDesktopCapturer(rtc::DesktopId desktop) {
    145     if (!desktop.IsValid()) {
    146       return NULL;
    147     }
    148     return new FakeVideoCapturer;
    149   }
    150 
    151   virtual bool GetDefaultVideoCaptureDevice(Device* device) {
    152     if (vidcap_devices_.empty()) {
    153       return false;
    154     }
    155     *device = vidcap_devices_[0];
    156     return true;
    157   }
    158 
    159 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
    160   bool QtKitToSgDevice(const std::string& qtkit_name, Device* out) {
    161     out->name = qtkit_name;
    162     out->id = "sg:" + qtkit_name;
    163     return true;
    164   }
    165 #endif
    166 
    167   void SetAudioInputDevices(const std::vector<std::string>& devices) {
    168     input_devices_.clear();
    169     for (size_t i = 0; i < devices.size(); ++i) {
    170       input_devices_.push_back(Device(devices[i],
    171                                       static_cast<int>(i)));
    172     }
    173     SignalDevicesChange();
    174   }
    175   void SetAudioOutputDevices(const std::vector<std::string>& devices) {
    176     output_devices_.clear();
    177     for (size_t i = 0; i < devices.size(); ++i) {
    178       output_devices_.push_back(Device(devices[i],
    179                                        static_cast<int>(i)));
    180     }
    181     SignalDevicesChange();
    182   }
    183   void SetVideoCaptureDevices(const std::vector<std::string>& devices) {
    184     vidcap_devices_.clear();
    185     for (size_t i = 0; i < devices.size(); ++i) {
    186       vidcap_devices_.push_back(Device(devices[i],
    187                                        static_cast<int>(i)));
    188     }
    189     SignalDevicesChange();
    190   }
    191   virtual bool GetVideoCaptureDevice(const std::string& name,
    192                                      Device* out) {
    193     if (vidcap_devices_.empty())
    194       return false;
    195 
    196     // If the name is empty, return the default device.
    197     if (name.empty() || name == kDefaultDeviceName) {
    198       *out = vidcap_devices_[0];
    199       return true;
    200     }
    201 
    202     return FindDeviceByName(vidcap_devices_, name, out);
    203   }
    204   bool GetAudioDevice(bool is_input, const std::string& name,
    205                       Device* out) {
    206     // If the name is empty, return the default device.
    207     if (name.empty() || name == kDefaultDeviceName) {
    208       *out = Device(name, -1);
    209       return true;
    210     }
    211 
    212     return FindDeviceByName((is_input ? input_devices_ : output_devices_),
    213                             name, out);
    214   }
    215   static bool FindDeviceByName(const std::vector<Device>& devices,
    216                                const std::string& name,
    217                                Device* out) {
    218     for (std::vector<Device>::const_iterator it = devices.begin();
    219          it != devices.end(); ++it) {
    220       if (name == it->name) {
    221         *out = *it;
    222         return true;
    223       }
    224     }
    225     return false;
    226   }
    227 
    228  private:
    229   std::vector<Device> input_devices_;
    230   std::vector<Device> output_devices_;
    231   std::vector<Device> vidcap_devices_;
    232   std::map<std::string, VideoFormat> max_formats_;
    233   rtc::scoped_ptr<
    234     ScreenCapturerFactory> screen_capturer_factory_;
    235 };
    236 
    237 }  // namespace cricket
    238 
    239 #endif  // TALK_MEDIA_DEVICES_FAKEDEVICEMANAGER_H_
    240