You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/media/devices/macdevicemanager.h"
  28. #include <CoreAudio/CoreAudio.h>
  29. #include <QuickTime/QuickTime.h>
  30. #include "talk/base/logging.h"
  31. #include "talk/base/stringutils.h"
  32. #include "talk/base/thread.h"
  33. #include "talk/media/base/mediacommon.h"
  34. class DeviceWatcherImpl;
  35. namespace cricket {
  36. DeviceManagerInterface* DeviceManagerFactory::Create() {
  37. return new MacDeviceManager();
  38. }
  39. class MacDeviceWatcher : public DeviceWatcher {
  40. public:
  41. explicit MacDeviceWatcher(DeviceManagerInterface* dm);
  42. virtual ~MacDeviceWatcher();
  43. virtual bool Start();
  44. virtual void Stop();
  45. private:
  46. DeviceManagerInterface* manager_;
  47. DeviceWatcherImpl* impl_;
  48. };
  49. static const char* kFilteredAudioDevicesName[] = {
  50. NULL,
  51. };
  52. // TODO(tommyw): Try to get hold of a copy of Final Cut to understand why we
  53. // crash while scanning their components on OS X.
  54. static const char* const kFilteredVideoDevicesName[] = {
  55. "DVCPRO HD", // Final cut
  56. "Sonix SN9C201p", // Crashes in OpenAComponent and CloseComponent
  57. NULL,
  58. };
  59. static const int kVideoDeviceOpenAttempts = 3;
  60. static const UInt32 kAudioDeviceNameLength = 64;
  61. // Obj-C functions defined in macdevicemanagermm.mm
  62. // TODO(ronghuawu): have a shared header for these function defines.
  63. extern DeviceWatcherImpl* CreateDeviceWatcherCallback(
  64. DeviceManagerInterface* dm);
  65. extern void ReleaseDeviceWatcherCallback(DeviceWatcherImpl* impl);
  66. extern bool GetQTKitVideoDevices(std::vector<Device>* out);
  67. static bool GetAudioDeviceIDs(bool inputs, std::vector<AudioDeviceID>* out);
  68. static bool GetAudioDeviceName(AudioDeviceID id, bool input, std::string* out);
  69. MacDeviceManager::MacDeviceManager() {
  70. set_watcher(new MacDeviceWatcher(this));
  71. }
  72. MacDeviceManager::~MacDeviceManager() {
  73. }
  74. bool MacDeviceManager::GetVideoCaptureDevices(std::vector<Device>* devices) {
  75. devices->clear();
  76. if (!GetQTKitVideoDevices(devices)) {
  77. return false;
  78. }
  79. return FilterDevices(devices, kFilteredVideoDevicesName);
  80. }
  81. bool MacDeviceManager::GetAudioDevices(bool input,
  82. std::vector<Device>* devs) {
  83. devs->clear();
  84. std::vector<AudioDeviceID> dev_ids;
  85. bool ret = GetAudioDeviceIDs(input, &dev_ids);
  86. if (!ret) {
  87. return false;
  88. }
  89. for (size_t i = 0; i < dev_ids.size(); ++i) {
  90. std::string name;
  91. if (GetAudioDeviceName(dev_ids[i], input, &name)) {
  92. devs->push_back(Device(name, dev_ids[i]));
  93. }
  94. }
  95. return FilterDevices(devs, kFilteredAudioDevicesName);
  96. }
  97. static bool GetAudioDeviceIDs(bool input,
  98. std::vector<AudioDeviceID>* out_dev_ids) {
  99. UInt32 propsize;
  100. OSErr err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices,
  101. &propsize, NULL);
  102. if (0 != err) {
  103. LOG(LS_ERROR) << "Couldn't get information about property, "
  104. << "so no device list acquired.";
  105. return false;
  106. }
  107. size_t num_devices = propsize / sizeof(AudioDeviceID);
  108. talk_base::scoped_array<AudioDeviceID> device_ids(
  109. new AudioDeviceID[num_devices]);
  110. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
  111. &propsize, device_ids.get());
  112. if (0 != err) {
  113. LOG(LS_ERROR) << "Failed to get device ids, "
  114. << "so no device listing acquired.";
  115. return false;
  116. }
  117. for (size_t i = 0; i < num_devices; ++i) {
  118. AudioDeviceID an_id = device_ids[i];
  119. // find out the number of channels for this direction
  120. // (input/output) on this device -
  121. // we'll ignore anything with no channels.
  122. err = AudioDeviceGetPropertyInfo(an_id, 0, input,
  123. kAudioDevicePropertyStreams,
  124. &propsize, NULL);
  125. if (0 == err) {
  126. unsigned num_channels = propsize / sizeof(AudioStreamID);
  127. if (0 < num_channels) {
  128. out_dev_ids->push_back(an_id);
  129. }
  130. } else {
  131. LOG(LS_ERROR) << "No property info for stream property for device id "
  132. << an_id << "(is_input == " << input
  133. << "), so not including it in the list.";
  134. }
  135. }
  136. return true;
  137. }
  138. static bool GetAudioDeviceName(AudioDeviceID id,
  139. bool input,
  140. std::string* out_name) {
  141. UInt32 nameLength = kAudioDeviceNameLength;
  142. char name[kAudioDeviceNameLength + 1];
  143. OSErr err = AudioDeviceGetProperty(id, 0, input,
  144. kAudioDevicePropertyDeviceName,
  145. &nameLength, name);
  146. if (0 != err) {
  147. LOG(LS_ERROR) << "No name acquired for device id " << id;
  148. return false;
  149. }
  150. *out_name = name;
  151. return true;
  152. }
  153. MacDeviceWatcher::MacDeviceWatcher(DeviceManagerInterface* manager)
  154. : DeviceWatcher(manager),
  155. manager_(manager),
  156. impl_(NULL) {
  157. }
  158. MacDeviceWatcher::~MacDeviceWatcher() {
  159. }
  160. bool MacDeviceWatcher::Start() {
  161. if (!impl_) {
  162. impl_ = CreateDeviceWatcherCallback(manager_);
  163. }
  164. return impl_ != NULL;
  165. }
  166. void MacDeviceWatcher::Stop() {
  167. if (impl_) {
  168. ReleaseDeviceWatcherCallback(impl_);
  169. impl_ = NULL;
  170. }
  171. }
  172. }; // namespace cricket