Debian Codegraph @

xbmc/cores/AudioEngine/Sinks/osx/CoreAudioStream.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 "CoreAudioStream.h"
10
11 #include "CoreAudioDevice.h"
12 #include "cores/AudioEngine/Sinks/darwin/CoreAudioHelpers.h"
13 #include "utils/log.h"
14
15 using namespace std::chrono_literals;
16
17 CCoreAudioStream::CCoreAudioStream()
18 {
19 m_OriginalVirtualFormat.mFormatID = 0;
20 m_OriginalPhysicalFormat.mFormatID = 0;
21 }
22
23 CCoreAudioStream::~CCoreAudioStream()
24 {
25 Close();
26 }
27
28 bool CCoreAudioStream::Open(AudioStreamID streamId)
29 {
30 m_StreamId = streamId;
31 CLog::Log(LOGDEBUG, "CCoreAudioStream::Open: Opened stream {:#04x}.", (uint)m_StreamId);
32
33 // watch for physical property changes.
34 AudioObjectPropertyAddress propertyAOPA;
35 propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;
36 propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
37 propertyAOPA.mSelector = kAudioStreamPropertyPhysicalFormat;
38 if (AudioObjectAddPropertyListener(m_StreamId, &propertyAOPA, HardwareStreamListener, this) != noErr)
39 CLog::Log(LOGERROR, "CCoreAudioStream::Open: couldn't set up a physical property listener.");
40
41 // watch for virtual property changes.
42 propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;
43 propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
44 propertyAOPA.mSelector = kAudioStreamPropertyVirtualFormat;
45 if (AudioObjectAddPropertyListener(m_StreamId, &propertyAOPA, HardwareStreamListener, this) != noErr)
46 CLog::Log(LOGERROR, "CCoreAudioStream::Open: couldn't set up a virtual property listener.");
47
48 return true;
49 }
50
51 //! @todo Should it even be possible to change both the
52 //! physical and virtual formats, since the devices do it themselves?
53 void CCoreAudioStream::Close(bool restore)
54 {
55 if (!m_StreamId)
56 return;
57
58 std::string formatString;
59
60 // remove the physical/virtual property listeners before we make changes
61 // that will trigger callbacks that we do not care about.
62 AudioObjectPropertyAddress propertyAOPA;
63 propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;
64 propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
65 propertyAOPA.mSelector = kAudioStreamPropertyPhysicalFormat;
66 if (AudioObjectRemovePropertyListener(m_StreamId, &propertyAOPA, HardwareStreamListener, this) != noErr)
67 CLog::Log(LOGDEBUG, "CCoreAudioStream::Close: Couldn't remove property listener.");
68
69 propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;
70 propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
71 propertyAOPA.mSelector = kAudioStreamPropertyVirtualFormat;
72 if (AudioObjectRemovePropertyListener(m_StreamId, &propertyAOPA, HardwareStreamListener, this) != noErr)
73 CLog::Log(LOGDEBUG, "CCoreAudioStream::Close: Couldn't remove property listener.");
74
75 // Revert any format changes we made
76 if (restore && m_OriginalVirtualFormat.mFormatID && m_StreamId)
77 {
78 CLog::Log(LOGDEBUG,
79 "CCoreAudioStream::Close: "
80 "Restoring original virtual format for stream {:#04x}. ({})",
81 (uint)m_StreamId, StreamDescriptionToString(m_OriginalVirtualFormat, formatString));
82 AudioStreamBasicDescription setFormat = m_OriginalVirtualFormat;
83 SetVirtualFormat(&setFormat);
84 }
85 if (restore && m_OriginalPhysicalFormat.mFormatID && m_StreamId)
86 {
87 CLog::Log(LOGDEBUG,
88 "CCoreAudioStream::Close: "
89 "Restoring original physical format for stream {:#04x}. ({})",
90 (uint)m_StreamId, StreamDescriptionToString(m_OriginalPhysicalFormat, formatString));
91 AudioStreamBasicDescription setFormat = m_OriginalPhysicalFormat;
92 SetPhysicalFormat(&setFormat);
93 }
94
95 m_OriginalVirtualFormat.mFormatID = 0;
96 m_OriginalPhysicalFormat.mFormatID = 0;
97 CLog::Log(LOGDEBUG, "CCoreAudioStream::Close: Closed stream {:#04x}.", (uint)m_StreamId);
98 m_StreamId = 0;
99 }
100
101 UInt32 CCoreAudioStream::GetDirection()
102 {
103 if (!m_StreamId)
104 return 0;
105
106 UInt32 val = 0;
107 UInt32 size = sizeof(UInt32);
108
109 AudioObjectPropertyAddress propertyAddress;
110 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
111 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
112 propertyAddress.mSelector = kAudioStreamPropertyDirection;
113
114 OSStatus ret = AudioObjectGetPropertyData(m_StreamId, &propertyAddress, 0, NULL, &size, &val);
115 if (ret)
116 return 0;
117
118 return val;
119 }
120
121 // WARNING - don't rely on this method - the return value of
122 // GetTerminalType is driver specific - the checked return
123 // values are only recommendations from apple
124 bool CCoreAudioStream::IsDigitalOutput(AudioStreamID id)
125 {
126 UInt32 type = GetTerminalType(id);
127 // yes apple is mixing types here...
128 return (type == kAudioStreamTerminalTypeDigitalAudioInterface ||
129 type == kIOAudioDeviceTransportTypeDisplayPort ||
130 type == kIOAudioDeviceTransportTypeHdmi ||
131 type == kIOAudioDeviceTransportTypeFireWire ||
132 type == kIOAudioDeviceTransportTypeThunderbolt ||
133 type == kIOAudioDeviceTransportTypeUSB);
134 }
135
136 bool CCoreAudioStream::GetStartingChannelInDevice(AudioStreamID id, UInt32 &startingChannel)
137 {
138 if (!id)
139 return false;
140
141 UInt32 i_param_size = sizeof(UInt32);
142 UInt32 i_param;
143 startingChannel = 0;
144 bool ret = false;
145
146 AudioObjectPropertyAddress propertyAddress;
147 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
148 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
149 propertyAddress.mSelector = kAudioStreamPropertyStartingChannel;
150
151 // number of frames of latency in the AudioStream
152 OSStatus status = AudioObjectGetPropertyData(id, &propertyAddress, 0, NULL, &i_param_size, &i_param);
153 if (status == noErr)
154 {
155 startingChannel = i_param;
156 ret = true;
157 }
158
159 return ret;
160 }
161
162 UInt32 CCoreAudioStream::GetTerminalType(AudioStreamID id)
163 {
164 if (!id)
165 return 0;
166
167 UInt32 val = 0;
168 UInt32 size = sizeof(UInt32);
169
170 AudioObjectPropertyAddress propertyAddress;
171 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
172 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
173 propertyAddress.mSelector = kAudioStreamPropertyTerminalType;
174
175 OSStatus ret = AudioObjectGetPropertyData(id, &propertyAddress, 0, NULL, &size, &val);
176 if (ret)
177 return 0;
178 return val;
179 }
180
181 UInt32 CCoreAudioStream::GetNumLatencyFrames()
182 {
183 if (!m_StreamId)
184 return 0;
185
186 UInt32 i_param_size = sizeof(uint32_t);
187 UInt32 i_param, num_latency_frames = 0;
188
189 AudioObjectPropertyAddress propertyAddress;
190 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
191 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
192 propertyAddress.mSelector = kAudioStreamPropertyLatency;
193
194 // number of frames of latency in the AudioStream
195 OSStatus ret = AudioObjectGetPropertyData(m_StreamId, &propertyAddress, 0, NULL, &i_param_size, &i_param);
196 if (ret == noErr)
197 {
198 num_latency_frames += i_param;
199 }
200
201 return num_latency_frames;
202 }
203
204 bool CCoreAudioStream::GetVirtualFormat(AudioStreamBasicDescription* pDesc)
205 {
206 if (!pDesc || !m_StreamId)
207 return false;
208
209 UInt32 size = sizeof(AudioStreamBasicDescription);
210
211 AudioObjectPropertyAddress propertyAddress;
212 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
213 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
214 propertyAddress.mSelector = kAudioStreamPropertyVirtualFormat;
215 OSStatus ret = AudioObjectGetPropertyDataSize(m_StreamId, &propertyAddress, 0, NULL, &size);
216 if (ret)
217 return false;
218
219 ret = AudioObjectGetPropertyData(m_StreamId, &propertyAddress, 0, NULL, &size, pDesc);
220 if (ret)
221 return false;
222 return true;
223 }
224
225 bool CCoreAudioStream::SetVirtualFormat(AudioStreamBasicDescription* pDesc)
226 {
227 if (!pDesc || !m_StreamId)
228 return false;
229
230 std::string formatString;
231
232 // suppress callbacks for the default output device change
233 // for the next 2 seconds because setting format
234 // might trigger a change (when setting/unsetting an encoded
235 // passthrough format)
236 CCoreAudioDevice::SuppressDefaultOutputDeviceCB(2000);
237
238
239 if (!m_OriginalVirtualFormat.mFormatID)
240 {
241 // Store the original format (as we found it) so that it can be restored later
242 if (!GetVirtualFormat(&m_OriginalVirtualFormat))
243 {
244 CLog::Log(LOGERROR,
245 "CCoreAudioStream::SetVirtualFormat: "
246 "Unable to retrieve current virtual format for stream {:#04x}.",
247 (uint)m_StreamId);
248 return false;
249 }
250 }
251 m_virtual_format_event.Reset();
252
253 AudioObjectPropertyAddress propertyAddress;
254 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
255 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
256 propertyAddress.mSelector = kAudioStreamPropertyVirtualFormat;
257
258 UInt32 propertySize = sizeof(AudioStreamBasicDescription);
259 OSStatus ret = AudioObjectSetPropertyData(m_StreamId, &propertyAddress, 0, NULL, propertySize, pDesc);
260 if (ret)
261 {
262 CLog::Log(LOGERROR,
263 "CCoreAudioStream::SetVirtualFormat: "
264 "Unable to set virtual format for stream {:#04x}. Error = {}",
265 (uint)m_StreamId, GetError(ret));
266 return false;
267 }
268
269 // The AudioStreamSetProperty is not only asynchronous,
270 // it is also not Atomic, in its behaviour.
271 // Therefore we check 5 times before we really give up.
272 // FIXME: failing isn't actually implemented yet.
273 for (int i = 0; i < 10; ++i)
274 {
275 AudioStreamBasicDescription checkVirtualFormat;
276 if (!GetVirtualFormat(&checkVirtualFormat))
277 {
278 CLog::Log(LOGERROR,
279 "CCoreAudioStream::SetVirtualFormat: "
280 "Unable to retrieve current physical format for stream {:#04x}.",
281 (uint)m_StreamId);
282 return false;
283 }
284 if (checkVirtualFormat.mSampleRate == pDesc->mSampleRate &&
285 checkVirtualFormat.mFormatID == pDesc->mFormatID &&
286 checkVirtualFormat.mFramesPerPacket == pDesc->mFramesPerPacket)
287 {
288 // The right format is now active.
289 CLog::Log(LOGDEBUG,
290 "CCoreAudioStream::SetVirtualFormat: "
291 "Virtual format for stream {:#04x}. now active ({})",
292 (uint)m_StreamId, StreamDescriptionToString(checkVirtualFormat, formatString));
293 break;
294 }
295 m_virtual_format_event.Wait(100ms);
296 }
297 return true;
298 }
299
300 bool CCoreAudioStream::GetPhysicalFormat(AudioStreamBasicDescription* pDesc)
301 {
302 if (!pDesc || !m_StreamId)
303 return false;
304
305 UInt32 size = sizeof(AudioStreamBasicDescription);
306
307 AudioObjectPropertyAddress propertyAddress;
308 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
309 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
310 propertyAddress.mSelector = kAudioStreamPropertyPhysicalFormat;
311
312 OSStatus ret = AudioObjectGetPropertyData(m_StreamId, &propertyAddress, 0, NULL, &size, pDesc);
313 if (ret)
314 return false;
315 return true;
316 }
317
318 bool CCoreAudioStream::SetPhysicalFormat(AudioStreamBasicDescription* pDesc)
319 {
320 if (!pDesc || !m_StreamId)
321 return false;
322
323 std::string formatString;
324
325 // suppress callbacks for the default output device change
326 // for the next 2 seconds because setting format
327 // might trigger a change (when setting/unsetting an encoded
328 // passthrough format)
329 CCoreAudioDevice::SuppressDefaultOutputDeviceCB(2000);
330
331 if (!m_OriginalPhysicalFormat.mFormatID)
332 {
333 // Store the original format (as we found it) so that it can be restored later
334 if (!GetPhysicalFormat(&m_OriginalPhysicalFormat))
335 {
336 CLog::Log(LOGERROR,
337 "CCoreAudioStream::SetPhysicalFormat: "
338 "Unable to retrieve current physical format for stream {:#04x}.",
339 (uint)m_StreamId);
340 return false;
341 }
342 }
343 m_physical_format_event.Reset();
344
345 AudioObjectPropertyAddress propertyAddress;
346 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
347 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
348 propertyAddress.mSelector = kAudioStreamPropertyPhysicalFormat;
349
350 UInt32 propertySize = sizeof(AudioStreamBasicDescription);
351 OSStatus ret = AudioObjectSetPropertyData(m_StreamId, &propertyAddress, 0, NULL, propertySize, pDesc);
352 if (ret)
353 {
354 CLog::Log(LOGERROR,
355 "CCoreAudioStream::SetPhysicalFormat: "
356 "Unable to set physical format for stream {:#04x}. Error = {}",
357 (uint)m_StreamId, GetError(ret));
358 return false;
359 }
360
361 // The AudioStreamSetProperty is not only asynchronous,
362 // it is also not Atomic, in its behaviour.
363 // Therefore we check 5 times before we really give up.
364 // FIXME: failing isn't actually implemented yet.
365 for(int i = 0; i < 10; ++i)
366 {
367 AudioStreamBasicDescription checkPhysicalFormat;
368 if (!GetPhysicalFormat(&checkPhysicalFormat))
369 {
370 CLog::Log(LOGERROR,
371 "CCoreAudioStream::SetPhysicalFormat: "
372 "Unable to retrieve current physical format for stream {:#04x}.",
373 (uint)m_StreamId);
374 return false;
375 }
376 if (checkPhysicalFormat.mSampleRate == pDesc->mSampleRate &&
377 checkPhysicalFormat.mFormatID == pDesc->mFormatID &&
378 checkPhysicalFormat.mFramesPerPacket == pDesc->mFramesPerPacket &&
379 checkPhysicalFormat.mChannelsPerFrame == pDesc->mChannelsPerFrame)
380 {
381 // The right format is now active.
382 CLog::Log(LOGDEBUG,
383 "CCoreAudioStream::SetPhysicalFormat: "
384 "Physical format for stream {:#04x}. now active ({})",
385 (uint)m_StreamId, StreamDescriptionToString(checkPhysicalFormat, formatString));
386 break;
387 }
388 m_physical_format_event.Wait(100ms);
389 }
390
391 return true;
392 }
393
394 bool CCoreAudioStream::GetAvailableVirtualFormats(StreamFormatList* pList)
395 {
396 return GetAvailableVirtualFormats(m_StreamId, pList);
397 }
398
399 bool CCoreAudioStream::GetAvailableVirtualFormats(AudioStreamID id, StreamFormatList* pList)
400 {
401 if (!pList || !id)
402 return false;
403
404 AudioObjectPropertyAddress propertyAddress;
405 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
406 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
407 propertyAddress.mSelector = kAudioStreamPropertyAvailableVirtualFormats;
408
409 UInt32 propertySize = 0;
410 OSStatus ret = AudioObjectGetPropertyDataSize(id, &propertyAddress, 0, NULL, &propertySize);
411 if (ret)
412 return false;
413
414 UInt32 formatCount = propertySize / sizeof(AudioStreamRangedDescription);
415 AudioStreamRangedDescription *pFormatList = new AudioStreamRangedDescription[formatCount];
416 ret = AudioObjectGetPropertyData(id, &propertyAddress, 0, NULL, &propertySize, pFormatList);
417 if (!ret)
418 {
419 for (UInt32 format = 0; format < formatCount; format++)
420 pList->push_back(pFormatList[format]);
421 }
422 delete[] pFormatList;
423 return (ret == noErr);
424 }
425
426 bool CCoreAudioStream::GetAvailablePhysicalFormats(StreamFormatList* pList)
427 {
428 return GetAvailablePhysicalFormats(m_StreamId, pList);
429 }
430
431 bool CCoreAudioStream::GetAvailablePhysicalFormats(AudioStreamID id, StreamFormatList* pList)
432 {
433 if (!pList || !id)
434 return false;
435
436 AudioObjectPropertyAddress propertyAddress;
437 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
438 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
439 propertyAddress.mSelector = kAudioStreamPropertyAvailablePhysicalFormats;
440
441 UInt32 propertySize = 0;
442 OSStatus ret = AudioObjectGetPropertyDataSize(id, &propertyAddress, 0, NULL, &propertySize);
443 if (ret)
444 return false;
445
446 UInt32 formatCount = propertySize / sizeof(AudioStreamRangedDescription);
447 AudioStreamRangedDescription *pFormatList = new AudioStreamRangedDescription[formatCount];
448 ret = AudioObjectGetPropertyData(id, &propertyAddress, 0, NULL, &propertySize, pFormatList);
449 if (!ret)
450 {
451 for (UInt32 format = 0; format < formatCount; format++)
452 pList->push_back(pFormatList[format]);
453 }
454 delete[] pFormatList;
455 return (ret == noErr);
456 }
457
458 OSStatus CCoreAudioStream::HardwareStreamListener(AudioObjectID inObjectID,
459 UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[], void *inClientData)
460 {
461 CCoreAudioStream *ca_stream = (CCoreAudioStream*)inClientData;
462
463 for (UInt32 i = 0; i < inNumberAddresses; i++)
464 {
465 if (inAddresses[i].mSelector == kAudioStreamPropertyPhysicalFormat)
466 {
467 AudioStreamBasicDescription actualFormat;
468 UInt32 propertySize = sizeof(AudioStreamBasicDescription);
469 // hardware physical format has changed.
470 if (AudioObjectGetPropertyData(ca_stream->m_StreamId, &inAddresses[i], 0, NULL, &propertySize, &actualFormat) == noErr)
471 {
472 std::string formatString;
473 CLog::Log(LOGINFO,
474 "CCoreAudioStream::HardwareStreamListener: "
475 "Hardware physical format changed to {}",
476 StreamDescriptionToString(actualFormat, formatString));
477 ca_stream->m_physical_format_event.Set();
478 }
479 }
480 else if (inAddresses[i].mSelector == kAudioStreamPropertyVirtualFormat)
481 {
482 // hardware virtual format has changed.
483 AudioStreamBasicDescription actualFormat;
484 UInt32 propertySize = sizeof(AudioStreamBasicDescription);
485 if (AudioObjectGetPropertyData(ca_stream->m_StreamId, &inAddresses[i], 0, NULL, &propertySize, &actualFormat) == noErr)
486 {
487 std::string formatString;
488 CLog::Log(LOGINFO,
489 "CCoreAudioStream::HardwareStreamListener: "
490 "Hardware virtual format changed to {}",
491 StreamDescriptionToString(actualFormat, formatString));
492 ca_stream->m_virtual_format_event.Set();
493 }
494 }
495 }
496
497 return noErr;
498 }