1// libjingle
2// Copyright 2004 Google Inc.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7//  1. Redistributions of source code must retain the above copyright notice,
8//     this list of conditions and the following disclaimer.
9//  2. Redistributions in binary form must reproduce the above copyright notice,
10//     this list of conditions and the following disclaimer in the documentation
11//     and/or other materials provided with the distribution.
12//  3. The name of the author may not be used to endorse or promote products
13//     derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26#ifndef TALK_SESSION_PHONE_FAKEWEBRTCDEVICEINFO_H_
27#define TALK_SESSION_PHONE_FAKEWEBRTCDEVICEINFO_H_
28
29#include <vector>
30
31#include "talk/base/stringutils.h"
32#include "talk/media/webrtc/webrtcvideocapturer.h"
33
34// Fake class for mocking out webrtc::VideoCaptureModule::DeviceInfo.
35class FakeWebRtcDeviceInfo : public webrtc::VideoCaptureModule::DeviceInfo {
36 public:
37  struct Device {
38    Device(const std::string& n, const std::string& i) : name(n), id(i) {}
39    std::string name;
40    std::string id;
41    std::string product;
42    std::vector<webrtc::VideoCaptureCapability> caps;
43  };
44  FakeWebRtcDeviceInfo() {}
45  void AddDevice(const std::string& device_name, const std::string& device_id) {
46    devices_.push_back(Device(device_name, device_id));
47  }
48  void AddCapability(const std::string& device_id,
49                     const webrtc::VideoCaptureCapability& cap) {
50    Device* dev = GetDeviceById(
51        reinterpret_cast<const char*>(device_id.c_str()));
52    if (!dev) return;
53    dev->caps.push_back(cap);
54  }
55  virtual uint32_t NumberOfDevices() {
56    return static_cast<int>(devices_.size());
57  }
58  virtual int32_t GetDeviceName(uint32_t device_num,
59                                char* device_name,
60                                uint32_t device_name_len,
61                                char* device_id,
62                                uint32_t device_id_len,
63                                char* product_id,
64                                uint32_t product_id_len) {
65    Device* dev = GetDeviceByIndex(device_num);
66    if (!dev) return -1;
67    talk_base::strcpyn(reinterpret_cast<char*>(device_name), device_name_len,
68                       dev->name.c_str());
69    talk_base::strcpyn(reinterpret_cast<char*>(device_id), device_id_len,
70                       dev->id.c_str());
71    if (product_id) {
72      talk_base::strcpyn(reinterpret_cast<char*>(product_id), product_id_len,
73                         dev->product.c_str());
74    }
75    return 0;
76  }
77  virtual int32_t NumberOfCapabilities(const char* device_id) {
78    Device* dev = GetDeviceById(device_id);
79    if (!dev) return -1;
80    return static_cast<int32_t>(dev->caps.size());
81  }
82  virtual int32_t GetCapability(const char* device_id,
83                                const uint32_t device_cap_num,
84                                webrtc::VideoCaptureCapability& cap) {
85    Device* dev = GetDeviceById(device_id);
86    if (!dev) return -1;
87    if (device_cap_num >= dev->caps.size()) return -1;
88    cap = dev->caps[device_cap_num];
89    return 0;
90  }
91  virtual int32_t GetOrientation(const char* device_id,
92                                 webrtc::VideoCaptureRotation& rotation) {
93    return -1;  // not implemented
94  }
95  virtual int32_t GetBestMatchedCapability(
96      const char* device_id,
97      const webrtc::VideoCaptureCapability& requested,
98      webrtc::VideoCaptureCapability& resulting) {
99    return -1;  // not implemented
100  }
101  virtual int32_t DisplayCaptureSettingsDialogBox(
102      const char* device_id, const char* dialog_title,
103      void* parent, uint32_t x, uint32_t y) {
104    return -1;  // not implemented
105  }
106
107  Device* GetDeviceByIndex(size_t num) {
108    return (num < devices_.size()) ? &devices_[num] : NULL;
109  }
110  Device* GetDeviceById(const char* device_id) {
111    for (size_t i = 0; i < devices_.size(); ++i) {
112      if (devices_[i].id == reinterpret_cast<const char*>(device_id)) {
113        return &devices_[i];
114      }
115    }
116    return NULL;
117  }
118
119 private:
120  std::vector<Device> devices_;
121};
122
123#endif  // TALK_SESSION_PHONE_FAKEWEBRTCDEVICEINFO_H_
124