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