Debian Codegraph @

xbmc/cores/AudioEngine/Sinks/osx/CoreAudioDevice.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 "CoreAudioDevice.h"
10
11 #include "CoreAudioChannelLayout.h"
12 #include "CoreAudioHardware.h"
13 #include "cores/AudioEngine/Sinks/darwin/CoreAudioHelpers.h"
14 #include "utils/log.h"
15
16 #include "platform/darwin/DarwinUtils.h"
17
18 #include <unistd.h>
19
20 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
21 // CCoreAudioDevice
22 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
23 CCoreAudioDevice::CCoreAudioDevice(AudioDeviceID deviceId) : m_DeviceId(deviceId)
24 {
25 }
26
27 CCoreAudioDevice::~CCoreAudioDevice()
28 {
29 Close();
30 }
31
32 bool CCoreAudioDevice::Open(AudioDeviceID deviceId)
33 {
34 m_DeviceId = deviceId;
35 m_BufferSizeRestore = GetBufferSize();
36 return true;
37 }
38
39 void CCoreAudioDevice::Close()
40 {
41 if (!m_DeviceId)
42 return;
43
44 // Stop the device if it was started
45 Stop();
46
47 // Unregister the IOProc if we have one
48 RemoveIOProc();
49
50 SetHogStatus(false);
51 CCoreAudioHardware::SetAutoHogMode(false);
52
53 if (m_MixerRestore > -1) // We changed the mixer status
54 SetMixingSupport((m_MixerRestore ? true : false));
55 m_MixerRestore = -1;
56
57 if (m_SampleRateRestore != 0.0)
58 SetNominalSampleRate(m_SampleRateRestore);
59
60 if (m_BufferSizeRestore && m_BufferSizeRestore != GetBufferSize())
61 {
62 SetBufferSize(m_BufferSizeRestore);
63 m_BufferSizeRestore = 0;
64 }
65
66 m_IoProc = NULL;
67 m_DeviceId = 0;
68 m_ObjectListenerProc = NULL;
69 }
70
71 void CCoreAudioDevice::Start()
72 {
73 if (!m_DeviceId || m_Started)
74 return;
75
76 OSStatus ret = AudioDeviceStart(m_DeviceId, m_IoProc);
77 if (ret)
78 CLog::Log(LOGERROR,
79 "CCoreAudioDevice::Start: "
80 "Unable to start device. Error = {}",
81 GetError(ret));
82 else
83 m_Started = true;
84 }
85
86 void CCoreAudioDevice::Stop()
87 {
88 if (!m_DeviceId || !m_Started)
89 return;
90
91 OSStatus ret = AudioDeviceStop(m_DeviceId, m_IoProc);
92 if (ret)
93 CLog::Log(LOGERROR,
94 "CCoreAudioDevice::Stop: "
95 "Unable to stop device. Error = {}",
96 GetError(ret));
97 m_Started = false;
98 }
99
100 void CCoreAudioDevice::RemoveObjectListenerProc(AudioObjectPropertyListenerProc callback, void* pClientData)
101 {
102 if (!m_DeviceId)
103 return;
104
105 AudioObjectPropertyAddress audioProperty;
106 audioProperty.mSelector = kAudioObjectPropertySelectorWildcard;
107 audioProperty.mScope = kAudioObjectPropertyScopeWildcard;
108 audioProperty.mElement = kAudioObjectPropertyElementWildcard;
109
110 OSStatus ret = AudioObjectRemovePropertyListener(m_DeviceId, &audioProperty, callback, pClientData);
111 if (ret)
112 {
113 CLog::Log(LOGERROR,
114 "CCoreAudioDevice::RemoveObjectListenerProc: "
115 "Unable to set ObjectListener callback. Error = {}",
116 GetError(ret));
117 }
118 m_ObjectListenerProc = NULL;
119 }
120
121 bool CCoreAudioDevice::SetObjectListenerProc(AudioObjectPropertyListenerProc callback, void* pClientData)
122 {
123 // Allow only one ObjectListener at a time
124 if (!m_DeviceId || m_ObjectListenerProc)
125 return false;
126
127 AudioObjectPropertyAddress audioProperty;
128 audioProperty.mSelector = kAudioObjectPropertySelectorWildcard;
129 audioProperty.mScope = kAudioObjectPropertyScopeWildcard;
130 audioProperty.mElement = kAudioObjectPropertyElementWildcard;
131
132 OSStatus ret = AudioObjectAddPropertyListener(m_DeviceId, &audioProperty, callback, pClientData);
133
134 if (ret)
135 {
136 CLog::Log(LOGERROR,
137 "CCoreAudioDevice::SetObjectListenerProc: "
138 "Unable to remove ObjectListener callback. Error = {}",
139 GetError(ret));
140 return false;
141 }
142
143 m_ObjectListenerProc = callback;
144 return true;
145 }
146 bool CCoreAudioDevice::AddIOProc(AudioDeviceIOProc ioProc, void* pCallbackData)
147 {
148 // Allow only one IOProc at a time
149 if (!m_DeviceId || m_IoProc)
150 return false;
151
152 OSStatus ret = AudioDeviceCreateIOProcID(m_DeviceId, ioProc, pCallbackData, &m_IoProc);
153 if (ret)
154 {
155 CLog::Log(LOGERROR,
156 "CCoreAudioDevice::AddIOProc: "
157 "Unable to add IOProc. Error = {}",
158 GetError(ret));
159 m_IoProc = NULL;
160 return false;
161 }
162
163 Start();
164
165 return true;
166 }
167
168 bool CCoreAudioDevice::RemoveIOProc()
169 {
170 if (!m_DeviceId || !m_IoProc)
171 return false;
172
173 Stop();
174
175 OSStatus ret = AudioDeviceDestroyIOProcID(m_DeviceId, m_IoProc);
176 if (ret)
177 CLog::Log(LOGERROR,
178 "CCoreAudioDevice::RemoveIOProc: "
179 "Unable to remove IOProc. Error = {}",
180 GetError(ret));
181
182 m_IoProc = NULL; // Clear the reference no matter what
183
184 usleep(100000);
185
186 return true;
187 }
188
189 std::string CCoreAudioDevice::GetName() const
190 {
191 if (!m_DeviceId)
192 return "";
193
194 AudioObjectPropertyAddress propertyAddress;
195 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
196 propertyAddress.mElement = 0;
197 propertyAddress.mSelector = kAudioDevicePropertyDeviceNameCFString;
198
199 std::string name;
200 CFStringRef deviceName = NULL;
201 UInt32 propertySize = sizeof(deviceName);
202
203 OSStatus ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, &deviceName);
204
205 if (ret != noErr)
206 {
207 CLog::Log(LOGERROR,
208 "CCoreAudioDevice::GetName: "
209 "Unable to get device name - id: {:#04x}. Error = {}",
210 (uint)m_DeviceId, GetError(ret));
211 }
212 else
213 {
214 CDarwinUtils::CFStringRefToUTF8String(deviceName, name);
215 CFRelease(deviceName);
216 }
217
218 return name;
219 }
220
221 bool CCoreAudioDevice::IsDigital() const
222 {
223 bool isDigital = false;
224 UInt32 transportType = 0;
225 if (!m_DeviceId)
226 return false;
227
228 transportType = GetTransportType();
229 if (transportType == INT_MAX)
230 return false;
231
232 if (transportType == kIOAudioDeviceTransportTypeFireWire)
233 isDigital = true;
234 if (transportType == kIOAudioDeviceTransportTypeUSB)
235 isDigital = true;
236 if (transportType == kIOAudioDeviceTransportTypeHdmi)
237 isDigital = true;
238 if (transportType == kIOAudioDeviceTransportTypeDisplayPort)
239 isDigital = true;
240 if (transportType == kIOAudioDeviceTransportTypeThunderbolt)
241 isDigital = true;
242 if (transportType == kAudioStreamTerminalTypeDigitalAudioInterface)
243 isDigital = true;
244
245 return isDigital;
246 }
247
248 UInt32 CCoreAudioDevice::GetTransportType() const
249 {
250 UInt32 transportType = 0;
251 if (!m_DeviceId)
252 return INT_MAX;
253
254 AudioObjectPropertyAddress propertyAddress;
255 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
256 propertyAddress.mElement = 0;
257 propertyAddress.mSelector = kAudioDevicePropertyTransportType;
258
259 UInt32 propertySize = sizeof(transportType);
260 OSStatus ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, &transportType);
261 if (ret != noErr)
262 return INT_MAX;
263 return transportType;
264 }
265
266 UInt32 CCoreAudioDevice::GetTotalOutputChannels() const
267 {
268 UInt32 channels = 0;
269
270 if (!m_DeviceId)
271 return channels;
272
273 AudioObjectPropertyAddress propertyAddress;
274 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
275 propertyAddress.mElement = 0;
276 propertyAddress.mSelector = kAudioDevicePropertyStreamConfiguration;
277
278 UInt32 size = 0;
279 OSStatus ret = AudioObjectGetPropertyDataSize(m_DeviceId, &propertyAddress, 0, NULL, &size);
280 if (ret != noErr)
281 return channels;
282
283 AudioBufferList* pList = (AudioBufferList*)malloc(size);
284 ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &size, pList);
285 if (ret == noErr)
286 {
287 for(UInt32 buffer = 0; buffer < pList->mNumberBuffers; ++buffer)
288 channels += pList->mBuffers[buffer].mNumberChannels;
289 }
290 else
291 {
292 CLog::Log(LOGERROR,
293 "CCoreAudioDevice::GetTotalOutputChannels: "
294 "Unable to get total device output channels - id: {:#04x}. Error = {}",
295 (uint)m_DeviceId, GetError(ret));
296 }
297
298 free(pList);
299
300 return channels;
301 }
302
303 UInt32 CCoreAudioDevice::GetNumChannelsOfStream(UInt32 streamIdx) const
304 {
305 UInt32 channels = 0;
306
307 if (!m_DeviceId)
308 return channels;
309
310 AudioObjectPropertyAddress propertyAddress;
311 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
312 propertyAddress.mElement = 0;
313 propertyAddress.mSelector = kAudioDevicePropertyStreamConfiguration;
314
315 UInt32 size = 0;
316 OSStatus ret = AudioObjectGetPropertyDataSize(m_DeviceId, &propertyAddress, 0, NULL, &size);
317 if (ret != noErr)
318 return channels;
319
320 AudioBufferList* pList = (AudioBufferList*)malloc(size);
321 ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &size, pList);
322 if (ret == noErr)
323 {
324 if (streamIdx < pList->mNumberBuffers)
325 channels = pList->mBuffers[streamIdx].mNumberChannels;
326 }
327 else
328 {
329 CLog::Log(LOGERROR,
330 "CCoreAudioDevice::GetNumChannelsOfStream: "
331 "Unable to get number of stream output channels - id: {:#04x}. Error = {}",
332 (uint)m_DeviceId, GetError(ret));
333 }
334
335 free(pList);
336
337 return channels;
338 }
339
340 bool CCoreAudioDevice::GetStreams(AudioStreamIdList* pList)
341 {
342 if (!pList || !m_DeviceId)
343 return false;
344
345 AudioObjectPropertyAddress propertyAddress;
346 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
347 propertyAddress.mElement = 0;
348 propertyAddress.mSelector = kAudioDevicePropertyStreams;
349
350 UInt32 propertySize = 0;
351 OSStatus ret = AudioObjectGetPropertyDataSize(m_DeviceId, &propertyAddress, 0, NULL, &propertySize);
352 if (ret != noErr)
353 return false;
354
355 UInt32 streamCount = propertySize / sizeof(AudioStreamID);
356 AudioStreamID* pStreamList = new AudioStreamID[streamCount];
357 ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, pStreamList);
358 if (ret == noErr)
359 {
360 for (UInt32 stream = 0; stream < streamCount; stream++)
361 pList->push_back(pStreamList[stream]);
362 }
363 delete[] pStreamList;
364
365 return ret == noErr;
366 }
367
368
369 bool CCoreAudioDevice::IsRunning()
370 {
371 AudioObjectPropertyAddress propertyAddress;
372 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
373 propertyAddress.mElement = 0;
374 propertyAddress.mSelector = kAudioDevicePropertyDeviceIsRunning;
375
376 UInt32 isRunning = 0;
377 UInt32 propertySize = sizeof(isRunning);
378 OSStatus ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, &isRunning);
379 if (ret != noErr)
380 return false;
381
382 return isRunning != 0;
383 }
384
385 bool CCoreAudioDevice::SetHogStatus(bool hog)
386 {
387 // According to Jeff Moore (Core Audio, Apple), Setting kAudioDevicePropertyHogMode
388 // is a toggle and the only way to tell if you do get hog mode is to compare
389 // the returned pid against getpid, if the match, you have hog mode, if not you don't.
390 if (!m_DeviceId)
391 return false;
392
393 AudioObjectPropertyAddress propertyAddress;
394 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
395 propertyAddress.mElement = 0;
396 propertyAddress.mSelector = kAudioDevicePropertyHogMode;
397
398 if (hog)
399 {
400 // Not already set
401 if (m_HogPid == -1)
402 {
403 OSStatus ret = AudioObjectSetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, sizeof(m_HogPid), &m_HogPid);
404
405 // even if setting hogmode was successful our PID might not get written
406 // into m_HogPid (so it stays -1). Readback hogstatus for judging if we
407 // had success on getting hog status
408 // We do this only when AudioObjectSetPropertyData didn't set m_HogPid because
409 // it seems that in the other cases the GetHogStatus could return -1
410 // which would overwrite our valid m_HogPid again
411 // Man we should never touch this shit again ;)
412 if (m_HogPid == -1)
413 m_HogPid = GetHogStatus();
414
415 if (ret || m_HogPid != getpid())
416 {
417 CLog::Log(LOGERROR,
418 "CCoreAudioDevice::SetHogStatus: "
419 "Unable to set 'hog' status. Error = {}",
420 GetError(ret));
421 return false;
422 }
423 }
424 }
425 else
426 {
427 // Currently Set
428 if (m_HogPid > -1)
429 {
430 pid_t hogPid = -1;
431 OSStatus ret = AudioObjectSetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, sizeof(hogPid), &hogPid);
432 if (ret || hogPid == getpid())
433 {
434 CLog::Log(LOGERROR,
435 "CCoreAudioDevice::SetHogStatus: "
436 "Unable to release 'hog' status. Error = {}",
437 GetError(ret));
438 return false;
439 }
440 // Reset internal state
441 m_HogPid = hogPid;
442 }
443 }
444 return true;
445 }
446
447 pid_t CCoreAudioDevice::GetHogStatus()
448 {
449 if (!m_DeviceId)
450 return false;
451
452 AudioObjectPropertyAddress propertyAddress;
453 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
454 propertyAddress.mElement = 0;
455 propertyAddress.mSelector = kAudioDevicePropertyHogMode;
456
457 pid_t hogPid = -1;
458 UInt32 size = sizeof(hogPid);
459 AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &size, &hogPid);
460
461 return hogPid;
462 }
463
464 bool CCoreAudioDevice::SetMixingSupport(UInt32 mix)
465 {
466 if (!m_DeviceId)
467 return false;
468
469 if (!GetMixingSupport())
470 return false;
471
472 int restore = -1;
473 if (m_MixerRestore == -1)
474 {
475 // This is our first change to this setting. Store the original setting for restore
476 restore = (GetMixingSupport() ? 1 : 0);
477 }
478
479 AudioObjectPropertyAddress propertyAddress;
480 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
481 propertyAddress.mElement = 0;
482 propertyAddress.mSelector = kAudioDevicePropertySupportsMixing;
483
484 UInt32 mixEnable = mix ? 1 : 0;
485 OSStatus ret = AudioObjectSetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, sizeof(mixEnable), &mixEnable);
486 if (ret != noErr)
487 {
488 CLog::Log(LOGERROR,
489 "CCoreAudioDevice::SetMixingSupport: "
490 "Unable to set MixingSupport to {}. Error = {}",
491 mix ? "'On'" : "'Off'", GetError(ret));
492 return false;
493 }
494 if (m_MixerRestore == -1)
495 m_MixerRestore = restore;
496 return true;
497 }
498
499 bool CCoreAudioDevice::GetMixingSupport()
500 {
501 if (!m_DeviceId)
502 return false;
503
504 UInt32 size;
505 UInt32 mix = 0;
506 Boolean writable = false;
507
508 AudioObjectPropertyAddress propertyAddress;
509 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
510 propertyAddress.mElement = 0;
511 propertyAddress.mSelector = kAudioDevicePropertySupportsMixing;
512
513 if( AudioObjectHasProperty( m_DeviceId, &propertyAddress ) )
514 {
515 OSStatus ret = AudioObjectIsPropertySettable(m_DeviceId, &propertyAddress, &writable);
516 if (ret)
517 {
518 CLog::Log(LOGERROR,
519 "CCoreAudioDevice::SupportsMixing: "
520 "Unable to get propertyinfo mixing support. Error = {}",
521 GetError(ret));
522 writable = false;
523 }
524
525 if (writable)
526 {
527 size = sizeof(mix);
528 ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &size, &mix);
529 if (ret != noErr)
530 mix = 0;
531 }
532 }
533 CLog::Log(LOGDEBUG,
534 "CCoreAudioDevice::SupportsMixing: "
535 "Device mixing support : {}.",
536 mix ? "'Yes'" : "'No'");
537
538 return (mix > 0);
539 }
540
541 bool CCoreAudioDevice::SetCurrentVolume(Float32 vol)
542 {
543 if (!m_DeviceId)
544 return false;
545
546 AudioObjectPropertyAddress propertyAddress;
547 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
548 propertyAddress.mElement = 0;
549 propertyAddress.mSelector = kHALOutputParam_Volume;
550
551 OSStatus ret = AudioObjectSetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, sizeof(Float32), &vol);
552 if (ret != noErr)
553 {
554 CLog::Log(LOGERROR,
555 "CCoreAudioDevice::SetCurrentVolume: "
556 "Unable to set AudioUnit volume. Error = {}",
557 GetError(ret));
558 return false;
559 }
560 return true;
561 }
562
563 bool CCoreAudioDevice::GetPreferredChannelLayout(CCoreAudioChannelLayout& layout) const
564 {
565 if (!m_DeviceId)
566 return false;
567
568 AudioObjectPropertyAddress propertyAddress;
569 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
570 propertyAddress.mElement = 0;
571 propertyAddress.mSelector = kAudioDevicePropertyPreferredChannelLayout;
572
573 UInt32 propertySize = 0;
574 OSStatus ret = AudioObjectGetPropertyDataSize(m_DeviceId, &propertyAddress, 0, NULL, &propertySize);
575 if (ret)
576 return false;
577
578 void* pBuf = malloc(propertySize);
579 ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, pBuf);
580 if (ret != noErr)
581 CLog::Log(LOGERROR,
582 "CCoreAudioDevice::GetPreferredChannelLayout: "
583 "Unable to retrieve preferred channel layout. Error = {}",
584 GetError(ret));
585 else
586 {
587 // Copy the result into the caller's instance
588 layout.CopyLayout(*((AudioChannelLayout*)pBuf));
589 }
590 free(pBuf);
591 return (ret == noErr);
592 }
593
594 bool CCoreAudioDevice::GetPreferredChannelLayoutForStereo(CCoreAudioChannelLayout &layout) const
595 {
596 if (!m_DeviceId)
597 return false;
598
599 AudioObjectPropertyAddress propertyAddress;
600 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
601 propertyAddress.mElement = 0;
602 propertyAddress.mSelector = kAudioDevicePropertyPreferredChannelsForStereo;
603
604 UInt32 channels[2];// this will receive the channel labels
605 UInt32 propertySize = sizeof(channels);
606
607 OSStatus ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, &channels);
608 if (ret != noErr)
609 CLog::Log(LOGERROR,
610 "CCoreAudioDevice::GetPreferredChannelLayoutForStereo: "
611 "Unable to retrieve preferred channel layout. Error = {}",
612 GetError(ret));
613 else
614 {
615 // Copy/generate a layout into the result into the caller's instance
616 layout.CopyLayoutForStereo(channels);
617 }
618 return (ret == noErr);
619 }
620
621 std::string CCoreAudioDevice::GetCurrentDataSourceName() const
622 {
623 UInt32 dataSourceId = 0;
624 std::string dataSourceName = "";
625 if(GetDataSource(dataSourceId))
626 {
627 dataSourceName = GetDataSourceName(dataSourceId);
628 }
629 return dataSourceName;
630 }
631
632 std::string CCoreAudioDevice::GetDataSourceName(UInt32 dataSourceId) const
633 {
634 UInt32 propertySize = 0;
635 CFStringRef dataSourceNameCF;
636 std::string dataSourceName;
637 std::string ret = "";
638
639 if (!m_DeviceId)
640 return ret;
641
642 AudioObjectPropertyAddress propertyAddress;
643 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
644 propertyAddress.mElement = 0;
645 propertyAddress.mSelector = kAudioDevicePropertyDataSourceNameForIDCFString;
646
647 AudioValueTranslation translation;
648 translation.mInputData = &dataSourceId;
649 translation.mInputDataSize = sizeof(UInt32);
650 translation.mOutputData = &dataSourceNameCF;
651 translation.mOutputDataSize = sizeof ( CFStringRef );
652 propertySize = sizeof(AudioValueTranslation);
653 OSStatus status = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, &translation);
654
655 if (( status == noErr ) && dataSourceNameCF )
656 {
657 if (CDarwinUtils::CFStringRefToUTF8String(dataSourceNameCF, dataSourceName))
658 {
659 ret = dataSourceName;
660 }
661 CFRelease ( dataSourceNameCF );
662 }
663
664 return ret;
665 }
666
667 bool CCoreAudioDevice::GetDataSource(UInt32 &dataSourceId) const
668 {
669 bool ret = false;
670
671 if (!m_DeviceId)
672 return false;
673
674 AudioObjectPropertyAddress propertyAddress;
675 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
676 propertyAddress.mElement = 0;
677 propertyAddress.mSelector = kAudioDevicePropertyDataSource;
678
679 UInt32 size = sizeof(dataSourceId);
680 OSStatus status = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &size, &dataSourceId);
681 if(status == noErr)
682 ret = true;
683
684 return ret;
685 }
686
687 bool CCoreAudioDevice::SetDataSource(UInt32 &dataSourceId)
688 {
689 bool ret = false;
690
691 if (!m_DeviceId)
692 return false;
693
694 AudioObjectPropertyAddress propertyAddress;
695 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
696 propertyAddress.mElement = 0;
697 propertyAddress.mSelector = kAudioDevicePropertyDataSource;
698
699 UInt32 size = sizeof(dataSourceId);
700 OSStatus status = AudioObjectSetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, size, &dataSourceId);
701 if(status == noErr)
702 ret = true;
703
704 return ret;
705 }
706
707 bool CCoreAudioDevice::GetDataSources(CoreAudioDataSourceList* pList) const
708 {
709 if (!pList || !m_DeviceId)
710 return false;
711
712 AudioObjectPropertyAddress propertyAddress;
713 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
714 propertyAddress.mElement = 0;
715 propertyAddress.mSelector = kAudioDevicePropertyDataSources;
716
717 UInt32 propertySize = 0;
718 OSStatus ret = AudioObjectGetPropertyDataSize(m_DeviceId, &propertyAddress, 0, NULL, &propertySize);
719 if (ret != noErr)
720 return false;
721
722 UInt32 sources = propertySize / sizeof(UInt32);
723 UInt32* pSources = new UInt32[sources];
724 ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, pSources);
725 if (ret == noErr)
726 {
727 for (UInt32 i = 0; i < sources; i++)
728 pList->push_back(pSources[i]);
729 }
730 delete[] pSources;
731 return (!ret);
732 }
733
734 Float64 CCoreAudioDevice::GetNominalSampleRate()
735 {
736 if (!m_DeviceId)
737 return 0.0;
738
739 AudioObjectPropertyAddress propertyAddress;
740 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
741 propertyAddress.mElement = 0;
742 propertyAddress.mSelector = kAudioDevicePropertyNominalSampleRate;
743
744 Float64 sampleRate = 0.0;
745 UInt32 propertySize = sizeof(Float64);
746 OSStatus ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, &sampleRate);
747 if (ret != noErr)
748 {
749 CLog::Log(LOGERROR,
750 "CCoreAudioDevice::GetNominalSampleRate: "
751 "Unable to retrieve current device sample rate. Error = {}",
752 GetError(ret));
753
754 return 0.0;
755 }
756 return sampleRate;
757 }
758
759 bool CCoreAudioDevice::SetNominalSampleRate(Float64 sampleRate)
760 {
761 if (!m_DeviceId || sampleRate == 0.0)
762 return false;
763
764 Float64 currentRate = GetNominalSampleRate();
765 if (currentRate == sampleRate)
766 return true; //No need to change
767
768 AudioObjectPropertyAddress propertyAddress;
769 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
770 propertyAddress.mElement = 0;
771 propertyAddress.mSelector = kAudioDevicePropertyNominalSampleRate;
772
773 OSStatus ret = AudioObjectSetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, sizeof(Float64), &sampleRate);
774 if (ret != noErr)
775 {
776 CLog::Log(LOGERROR,
777 "CCoreAudioDevice::SetNominalSampleRate: "
778 "Unable to set current device sample rate to {:0.0f}. Error = {}",
779 (float)sampleRate, GetError(ret));
780 return false;
781 }
782 if (m_SampleRateRestore == 0.0)
783 m_SampleRateRestore = currentRate;
784
785 return true;
786 }
787
788 UInt32 CCoreAudioDevice::GetNumLatencyFrames()
789 {
790 UInt32 num_latency_frames = 0;
791 if (!m_DeviceId)
792 return 0;
793
794 // number of frames of latency in the AudioDevice
795 AudioObjectPropertyAddress propertyAddress;
796 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
797 propertyAddress.mElement = 0;
798 propertyAddress.mSelector = kAudioDevicePropertyLatency;
799
800 UInt32 i_param = 0;
801 UInt32 i_param_size = sizeof(uint32_t);
802 OSStatus ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &i_param_size, &i_param);
803 if (ret == noErr)
804 num_latency_frames += i_param;
805
806 // number of frames in the IO buffers
807 propertyAddress.mSelector = kAudioDevicePropertyBufferFrameSize;
808 ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &i_param_size, &i_param);
809 if (ret == noErr)
810 num_latency_frames += i_param;
811
812 // number for frames in ahead the current hardware position that is safe to do IO
813 propertyAddress.mSelector = kAudioDevicePropertySafetyOffset;
814 ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &i_param_size, &i_param);
815 if (ret == noErr)
816 num_latency_frames += i_param;
817
818 return (num_latency_frames);
819 }
820
821 UInt32 CCoreAudioDevice::GetBufferSize()
822 {
823 if (!m_DeviceId)
824 return false;
825
826 AudioObjectPropertyAddress propertyAddress;
827 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
828 propertyAddress.mElement = 0;
829 propertyAddress.mSelector = kAudioDevicePropertyBufferFrameSize;
830
831 UInt32 size = 0;
832 UInt32 propertySize = sizeof(size);
833 OSStatus ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, &size);
834 if (ret != noErr)
835 CLog::Log(LOGERROR,
836 "CCoreAudioDevice::GetBufferSize: "
837 "Unable to retrieve buffer size. Error = {}",
838 GetError(ret));
839 return size;
840 }
841
842 bool CCoreAudioDevice::SetBufferSize(UInt32 size)
843 {
844 if (!m_DeviceId)
845 return false;
846
847 AudioObjectPropertyAddress propertyAddress;
848 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
849 propertyAddress.mElement = 0;
850 propertyAddress.mSelector = kAudioDevicePropertyBufferFrameSize;
851
852 UInt32 propertySize = sizeof(size);
853 OSStatus ret = AudioObjectSetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, propertySize, &size);
854 if (ret != noErr)
855 {
856 CLog::Log(LOGERROR,
857 "CCoreAudioDevice::SetBufferSize: "
858 "Unable to set buffer size. Error = {}",
859 GetError(ret));
860 }
861
862 if (GetBufferSize() != size)
863 CLog::Log(LOGERROR, "CCoreAudioDevice::SetBufferSize: Buffer size change not applied.");
864
865 return (ret == noErr);
866 }
867
868 XbmcThreads::EndTime<> CCoreAudioDevice::m_callbackSuppressTimer;
869 AudioObjectPropertyListenerProc CCoreAudioDevice::m_defaultOutputDeviceChangedCB = NULL;
870
871
872 OSStatus CCoreAudioDevice::defaultOutputDeviceChanged(AudioObjectID inObjectID,
873 UInt32 inNumberAddresses,
874 const AudioObjectPropertyAddress inAddresses[],
875 void* inClientData)
876 {
877 if (m_callbackSuppressTimer.IsTimePast() && m_defaultOutputDeviceChangedCB != NULL)
878 return m_defaultOutputDeviceChangedCB(inObjectID, inNumberAddresses, inAddresses, inClientData);
879 return 0;
880 }
881
882 void CCoreAudioDevice::RegisterDeviceChangedCB(bool bRegister, AudioObjectPropertyListenerProc callback, void *ref)
883 {
884 OSStatus ret = noErr;
885 AudioObjectPropertyAddress inAdr =
886 {
887 kAudioHardwarePropertyDevices,
888 kAudioObjectPropertyScopeGlobal,
889 kAudioObjectPropertyElementMaster
890 };
891
892 if (bRegister)
893 ret = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &inAdr, callback, ref);
894 else
895 ret = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &inAdr, callback, ref);
896
897 if (ret != noErr)
898 CLog::Log(LOGERROR,
899 "CCoreAudioAE::Deinitialize - error {} a listener callback for device changes!",
900 bRegister ? "attaching" : "removing");
901 }
902
903 void CCoreAudioDevice::RegisterDefaultOutputDeviceChangedCB(bool bRegister, AudioObjectPropertyListenerProc callback, void *ref)
904 {
905 OSStatus ret = noErr;
906 static int registered = -1;
907
908 //only allow registration once
909 if (bRegister == (registered == 1))
910 return;
911
912 AudioObjectPropertyAddress inAdr =
913 {
914 kAudioHardwarePropertyDefaultOutputDevice,
915 kAudioObjectPropertyScopeGlobal,
916 kAudioObjectPropertyElementMaster
917 };
918
919 if (bRegister)
920 {
921 ret = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &inAdr, defaultOutputDeviceChanged, ref);
922 m_defaultOutputDeviceChangedCB = callback;
923 }
924 else
925 {
926 ret = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &inAdr, defaultOutputDeviceChanged, ref);
927 m_defaultOutputDeviceChangedCB = NULL;
928 }
929
930 if (ret != noErr)
931 CLog::Log(LOGERROR,
932 "CCoreAudioAE::Deinitialize - error {} a listener callback for default output "
933 "device changes!",
934 bRegister ? "attaching" : "removing");
935 else
936 registered = bRegister ? 1 : 0;
937 }
938