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