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