Debian Codegraph @

xbmc/cores/AudioEngine/Sinks/osx/CoreAudioHardware.cpp

raw
1 /*
2 * Copyright (C) 2011-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "CoreAudioHardware.h"
10
11 #include "cores/AudioEngine/Sinks/darwin/CoreAudioHelpers.h"
12 #include "utils/log.h"
13
14 #include "platform/darwin/DarwinUtils.h"
15
16 bool CCoreAudioHardware::GetAutoHogMode()
17 {
18 AudioObjectPropertyAddress propertyAddress;
19 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
20 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
21 propertyAddress.mSelector = kAudioHardwarePropertyHogModeIsAllowed;
22
23 UInt32 val = 0;
24 UInt32 size = sizeof(val);
25 OSStatus ret = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, &val);
26 if (ret != noErr)
27 {
28 CLog::Log(LOGERROR,
29 "CCoreAudioHardware::GetAutoHogMode: "
30 "Unable to get auto 'hog' mode. Error = {}",
31 GetError(ret));
32 return false;
33 }
34 return (val == 1);
35 }
36
37 void CCoreAudioHardware::SetAutoHogMode(bool enable)
38 {
39 AudioObjectPropertyAddress propertyAddress;
40 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
41 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
42 propertyAddress.mSelector = kAudioHardwarePropertyHogModeIsAllowed;
43
44 UInt32 val = enable ? 1 : 0;
45 UInt32 size = sizeof(val);
46 OSStatus ret = AudioObjectSetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, size, &val);
47 if (ret != noErr)
48 CLog::Log(LOGERROR,
49 "CCoreAudioHardware::SetAutoHogMode: "
50 "Unable to set auto 'hog' mode. Error = {}",
51 GetError(ret));
52 }
53
54 void CCoreAudioHardware::ResetAudioDevices()
55 {
56 CLog::Log(LOGDEBUG, "CCoreAudioHardware::ResetAudioDevices resetting our devices to LPCM");
57 CoreAudioDeviceList list;
58 if (GetOutputDevices(&list))
59 {
60 for (CoreAudioDeviceList::iterator it = list.begin(); it != list.end(); ++it)
61 {
62 CCoreAudioDevice device(*it);
63
64 AudioStreamIdList streams;
65 if (device.GetStreams(&streams))
66 {
67 CLog::Log(LOGDEBUG, "CCoreAudioHardware::ResetAudioDevices {} streams for device {}",
68 streams.size(), device.GetName());
69 for (AudioStreamIdList::iterator ait = streams.begin(); ait != streams.end(); ++ait)
70 ResetStream(*ait);
71 }
72 }
73 }
74 }
75
76 void CCoreAudioHardware::ResetStream(AudioStreamID streamId)
77 {
78 CCoreAudioStream stream;
79 stream.Open(streamId);
80
81 AudioStreamBasicDescription desc;
82 if (stream.GetPhysicalFormat(&desc))
83 {
84 if (desc.mFormatID == 'IAC3' || desc.mFormatID == kAudioFormat60958AC3)
85 {
86 CLog::Log(
87 LOGDEBUG,
88 "CCoreAudioHardware::ResetStream stream 0x{:x} is in encoded format.. setting to LPCM",
89 (unsigned int)streamId);
90
91 StreamFormatList availableFormats;
92 if (stream.GetAvailablePhysicalFormats(&availableFormats))
93 {
94 for (StreamFormatList::iterator fmtIt = availableFormats.begin(); fmtIt != availableFormats.end() ; ++fmtIt)
95 {
96 AudioStreamRangedDescription fmtDesc = *fmtIt;
97 if (fmtDesc.mFormat.mFormatID == kAudioFormatLinearPCM)
98 {
99 AudioStreamBasicDescription newFmt = fmtDesc.mFormat;
100
101 if (stream.SetPhysicalFormat(&newFmt))
102 break;
103 }
104 }
105 }
106 }
107 }
108
109 stream.Close(false);
110 }
111
112 AudioDeviceID CCoreAudioHardware::FindAudioDevice(const std::string &searchName)
113 {
114 AudioDeviceID deviceId = 0;
115
116 if (!searchName.length())
117 return deviceId;
118
119 std::string searchNameLowerCase = searchName;
120 std::transform(searchNameLowerCase.begin(), searchNameLowerCase.end(), searchNameLowerCase.begin(), ::tolower );
121 if (searchNameLowerCase.compare("default") == 0)
122 {
123 AudioDeviceID defaultDevice = GetDefaultOutputDevice();
124 CLog::Log(LOGDEBUG,
125 "CCoreAudioHardware::FindAudioDevice: "
126 "Returning default device [{:#04x}].",
127 (uint)defaultDevice);
128 return defaultDevice;
129 }
130 CLog::Log(LOGDEBUG,
131 "CCoreAudioHardware::FindAudioDevice: "
132 "Searching for device - {}.",
133 searchName);
134
135 // Obtain a list of all available audio devices
136 AudioObjectPropertyAddress propertyAddress;
137 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
138 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
139 propertyAddress.mSelector = kAudioHardwarePropertyDevices;
140
141 UInt32 size = 0;
142 OSStatus ret = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size);
143 if (ret != noErr)
144 {
145 CLog::Log(LOGERROR,
146 "CCoreAudioHardware::FindAudioDevice: "
147 "Unable to retrieve the size of the list of available devices. Error = {}",
148 GetError(ret));
149 return 0;
150 }
151
152 size_t deviceCount = size / sizeof(AudioDeviceID);
153 AudioDeviceID* pDevices = new AudioDeviceID[deviceCount];
154 ret = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, pDevices);
155 if (ret != noErr)
156 {
157 CLog::Log(LOGERROR,
158 "CCoreAudioHardware::FindAudioDevice: "
159 "Unable to retrieve the list of available devices. Error = {}",
160 GetError(ret));
161 delete[] pDevices;
162 return 0;
163 }
164
165 // Attempt to locate the requested device
166 std::string deviceName;
167 for (size_t dev = 0; dev < deviceCount; dev++)
168 {
169 CCoreAudioDevice device;
170 device.Open((pDevices[dev]));
171 deviceName = device.GetName();
172 std::transform( deviceName.begin(), deviceName.end(), deviceName.begin(), ::tolower );
173 if (searchNameLowerCase.compare(deviceName) == 0)
174 deviceId = pDevices[dev];
175 if (deviceId)
176 break;
177 }
178 delete[] pDevices;
179
180 return deviceId;
181 }
182
183 AudioDeviceID CCoreAudioHardware::GetDefaultOutputDevice()
184 {
185 AudioDeviceID deviceId = 0;
186 static AudioDeviceID lastDeviceId = 0;
187
188 AudioObjectPropertyAddress propertyAddress;
189 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
190 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
191 propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
192
193 UInt32 size = sizeof(AudioDeviceID);
194 OSStatus ret = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, &deviceId);
195
196 // outputDevice is set to 0 if there is no audio device available
197 // or if the default device is set to an encoded format
198 if (ret != noErr || !deviceId)
199 {
200 CLog::Log(LOGERROR,
201 "CCoreAudioHardware::GetDefaultOutputDevice:"
202 " Unable to identify default output device. Error = {}",
203 GetError(ret));
204 // if there was no error and no deviceId was returned
205 // return the last known default device
206 if (ret == noErr && !deviceId)
207 return lastDeviceId;
208 else
209 return 0;
210 }
211
212 lastDeviceId = deviceId;
213
214 return deviceId;
215 }
216
217 void CCoreAudioHardware::GetOutputDeviceName(std::string& name)
218 {
219 name = "Default";
220 AudioDeviceID deviceId = GetDefaultOutputDevice();
221
222 if (deviceId)
223 {
224 AudioObjectPropertyAddress propertyAddress;
225 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
226 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
227 propertyAddress.mSelector = kAudioObjectPropertyName;
228
229 CFStringRef theDeviceName = NULL;
230 UInt32 propertySize = sizeof(CFStringRef);
231 OSStatus ret = AudioObjectGetPropertyData(deviceId, &propertyAddress, 0, NULL, &propertySize, &theDeviceName);
232 if (ret != noErr)
233 return;
234
235 CDarwinUtils::CFStringRefToUTF8String(theDeviceName, name);
236
237 CFRelease(theDeviceName);
238 }
239 }
240
241 UInt32 CCoreAudioHardware::GetOutputDevices(CoreAudioDeviceList *pList)
242 {
243 UInt32 found = 0;
244 if (!pList)
245 return found;
246
247 // Obtain a list of all available audio devices
248 AudioObjectPropertyAddress propertyAddress;
249 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
250 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
251 propertyAddress.mSelector = kAudioHardwarePropertyDevices;
252
253 UInt32 size = 0;
254 OSStatus ret = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size);
255 if (ret != noErr)
256 {
257 CLog::Log(LOGERROR,
258 "CCoreAudioHardware::GetOutputDevices:"
259 " Unable to retrieve the size of the list of available devices. Error = {}",
260 GetError(ret));
261 return found;
262 }
263
264 size_t deviceCount = size / sizeof(AudioDeviceID);
265 AudioDeviceID* pDevices = new AudioDeviceID[deviceCount];
266 ret = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, pDevices);
267 if (ret != noErr)
268 CLog::Log(LOGERROR,
269 "CCoreAudioHardware::GetOutputDevices:"
270 " Unable to retrieve the list of available devices. Error = {}",
271 GetError(ret));
272 else
273 {
274 for (size_t dev = 0; dev < deviceCount; dev++)
275 {
276 CCoreAudioDevice device(pDevices[dev]);
277 if (device.GetTotalOutputChannels() == 0)
278 continue;
279 found++;
280 pList->push_back(pDevices[dev]);
281 }
282 }
283 delete[] pDevices;
284
285 return found;
286 }