1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "cores/AudioEngine/Sinks/AESinkDARWINOSX.h"
#include "ServiceBroker.h"
#include "cores/AudioEngine/AESinkFactory.h"
#include "cores/AudioEngine/Sinks/darwin/CoreAudioHelpers.h"
#include "cores/AudioEngine/Sinks/osx/AEDeviceEnumerationOSX.h"
#include "cores/AudioEngine/Sinks/osx/CoreAudioHardware.h"
#include "cores/AudioEngine/Utils/AERingBuffer.h"
#include "threads/SystemClock.h"
#include "utils/MemUtils.h"
#include "utils/StringUtils.h"
#include "utils/TimeUtils.h"
#include "utils/log.h"
#include <mutex>
using namespace std::chrono_literals;
static void EnumerateDevices(CADeviceList &list)
{
std::string defaultDeviceName;
CCoreAudioHardware::GetOutputDeviceName(defaultDeviceName);
AudioDeviceID defaultID = CCoreAudioHardware::GetDefaultOutputDevice();
CoreAudioDeviceList deviceIDList;
CCoreAudioHardware::GetOutputDevices(&deviceIDList);
while (!deviceIDList.empty())
{
AudioDeviceID deviceID = deviceIDList.front();
AEDeviceEnumerationOSX devEnum(deviceID);
CADeviceList listForDevice = devEnum.GetDeviceInfoList();
for (UInt32 devIdx = 0; devIdx < listForDevice.size(); devIdx++)
list.push_back(listForDevice[devIdx]);
//in the first place of the list add the default device
//with name "default" - if this is selected
//we will output to whatever osx claims to be default
//(allows transition from headphones to speaker and stuff
//like that)
//fixme taking the first stream device is wrong here
//we rather might need the concatenation of all streams *sucks*
if(defaultID == deviceID && defaultDeviceName == devEnum.GetMasterDeviceName())
{
struct CADeviceInstance deviceInstance;
deviceInstance.audioDeviceId = deviceID;
deviceInstance.streamIndex = INT_MAX;//don't limit streamidx for the raw device
deviceInstance.sourceId = INT_MAX;
CAEDeviceInfo firstDevice = listForDevice.front().second;
firstDevice.m_deviceName = "default";
firstDevice.m_displayName = "Default";
firstDevice.m_displayNameExtra = defaultDeviceName;
list.insert(list.begin(), std::make_pair(deviceInstance, firstDevice));
}
deviceIDList.pop_front();
}
}
/* static, threadsafe access to the device list */
static CADeviceList s_devices;
static CCriticalSection s_devicesLock;
static void EnumerateDevices()
{
CADeviceList devices;
EnumerateDevices(devices);
{
std::unique_lock<CCriticalSection> lock(s_devicesLock);
s_devices = devices;
}
}
static CADeviceList GetDevices()
{
CADeviceList list;
{
std::unique_lock<CCriticalSection> lock(s_devicesLock);
list = s_devices;
}
return list;
}
OSStatus deviceChangedCB(AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData)
{
bool deviceChanged = false;
static AudioDeviceID oldDefaultDevice = 0;
AudioDeviceID currentDefaultOutputDevice = 0;
for (unsigned int i = 0; i < inNumberAddresses; i++)
{
switch (inAddresses[i].mSelector)
{
case kAudioHardwarePropertyDefaultOutputDevice:
currentDefaultOutputDevice = CCoreAudioHardware::GetDefaultOutputDevice();
// This listener is called on every change of the hardware
// device. So check if the default device has really changed.
if (oldDefaultDevice != currentDefaultOutputDevice)
{
deviceChanged = true;
oldDefaultDevice = currentDefaultOutputDevice;
}
break;
default:
deviceChanged = true;
break;
}
if (deviceChanged)
break;
}
if (deviceChanged)
{
CLog::Log(LOGDEBUG, "CoreAudio: audiodevicelist changed - reenumerating");
IAE* ae = CServiceBroker::GetActiveAE();
if (ae)
ae->DeviceChange();
CLog::Log(LOGDEBUG, "CoreAudio: audiodevicelist changed - done");
}
return noErr;
}
////////////////////////////////////////////////////////////////////////////////////////////
CAESinkDARWINOSX::CAESinkDARWINOSX()
{
// By default, kAudioHardwarePropertyRunLoop points at the process's main thread on SnowLeopard,
// If your process lacks such a run loop, you can set kAudioHardwarePropertyRunLoop to NULL which
// tells the HAL to run its own thread for notifications (which was the default prior to SnowLeopard).
// So tell the HAL to use its own thread for similar behavior under all supported versions of OSX.
CFRunLoopRef theRunLoop = NULL;
AudioObjectPropertyAddress theAddress = {
kAudioHardwarePropertyRunLoop,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
OSStatus theError = AudioObjectSetPropertyData(kAudioObjectSystemObject,
&theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
if (theError != noErr)
{
CLog::Log(LOGERROR, "CCoreAudioAE::constructor: kAudioHardwarePropertyRunLoop error.");
}
CCoreAudioDevice::RegisterDeviceChangedCB(true, deviceChangedCB, this);
CCoreAudioDevice::RegisterDefaultOutputDeviceChangedCB(true, deviceChangedCB, this);
}
CAESinkDARWINOSX::~CAESinkDARWINOSX()
{
CCoreAudioDevice::RegisterDeviceChangedCB(false, deviceChangedCB, this);
CCoreAudioDevice::RegisterDefaultOutputDeviceChangedCB(false, deviceChangedCB, this);
}
void CAESinkDARWINOSX::Register()
{
AE::AESinkRegEntry reg;
reg.sinkName = "DARWINOSX";
reg.createFunc = CAESinkDARWINOSX::Create;
reg.enumerateFunc = CAESinkDARWINOSX::EnumerateDevicesEx;
AE::CAESinkFactory::RegisterSink(reg);
}
IAESink* CAESinkDARWINOSX::Create(std::string &device, AEAudioFormat &desiredFormat)
{
IAESink *sink = new CAESinkDARWINOSX();
if (sink->Initialize(desiredFormat, device))
return sink;
delete sink;
return nullptr;
}
bool CAESinkDARWINOSX::Initialize(AEAudioFormat &format, std::string &device)
{
AudioDeviceID deviceID = 0;
UInt32 requestedStreamIndex = INT_MAX;
UInt32 requestedSourceId = INT_MAX;
bool passthrough = false;
// this sink needs IEC packing for RAW data
if (format.m_dataFormat == AE_FMT_RAW)
{
format.m_dataFormat= AE_FMT_S16NE;
passthrough = true;
}
CADeviceList devices = GetDevices();
if (StringUtils::EqualsNoCase(device, "default"))
{
CCoreAudioHardware::GetOutputDeviceName(device);
deviceID = CCoreAudioHardware::GetDefaultOutputDevice();
CLog::Log(LOGINFO, "{}: Opening default device {}", __PRETTY_FUNCTION__, device);
}
else
{
for (size_t i = 0; i < devices.size(); i++)
{
if (device == devices[i].second.m_deviceName)
{
const struct CADeviceInstance &deviceInstance = devices[i].first;
deviceID = deviceInstance.audioDeviceId;
requestedStreamIndex = deviceInstance.streamIndex;
requestedSourceId = deviceInstance.sourceId;
if (requestedStreamIndex != INT_MAX)
CLog::Log(LOGINFO, "{} pseudo device - requesting stream {}", __FUNCTION__,
(unsigned int)requestedStreamIndex);
if (requestedSourceId != INT_MAX)
CLog::Log(LOGINFO, "{} device - requesting audiosource {}", __FUNCTION__,
(unsigned int)requestedSourceId);
break;
}
}
}
if (!deviceID)
{
CLog::Log(LOGERROR, "{}: Unable to find device {}", __FUNCTION__, device);
return false;
}
AEDeviceEnumerationOSX devEnum(deviceID);
AudioStreamBasicDescription outputFormat = {};
AudioStreamID outputStream = 0;
UInt32 numOutputChannels = 0;
m_planes = 1;
// after FindSuitableFormatForStream requestedStreamIndex will have a valid index and no INT_MAX anymore ...
if (devEnum.FindSuitableFormatForStream(requestedStreamIndex, format, false, outputFormat, outputStream))
{
numOutputChannels = outputFormat.mChannelsPerFrame;
if (devEnum.IsPlanar())
{
numOutputChannels = std::min((size_t)format.m_channelLayout.Count(), (size_t)devEnum.GetNumPlanes());
m_planes = numOutputChannels;
CLog::Log(LOGDEBUG, "{} Found planar audio with {} channels using {} of them.", __FUNCTION__,
(unsigned int)devEnum.GetNumPlanes(), (unsigned int)numOutputChannels);
}
}
else
{
CLog::Log(LOGERROR, "{}, Unable to find suitable stream", __FUNCTION__);
return false;
}
AudioStreamBasicDescription outputFormatVirt = {};
AudioStreamID outputStreamVirt = 0;
UInt32 numOutputChannelsVirt = 0;
if (passthrough)
{
if (devEnum.FindSuitableFormatForStream(requestedStreamIndex, format, true, outputFormatVirt, outputStreamVirt))
{
numOutputChannelsVirt = outputFormatVirt.mChannelsPerFrame;
}
else
{
CLog::Log(LOGERROR, "{}, Unable to find suitable virtual stream", __FUNCTION__);
//return false;
numOutputChannelsVirt = 0;
}
}
/* Update our AE format */
format.m_sampleRate = outputFormat.mSampleRate;
m_outputBufferIndex = requestedStreamIndex;
// if we are in passthrough but didn't have a matching
// virtual format - enable bitstream which deals with
// backconverting from float to 16bit
if (passthrough && numOutputChannelsVirt == 0)
{
m_outputBitstream = true;
CLog::Log(LOGDEBUG, "{}: Bitstream passthrough with float -> int16 conversion enabled",
__FUNCTION__);
}
std::string formatString;
CLog::Log(LOGDEBUG, "{}: Selected stream[{}] - id: {:#04X}, Physical Format: {} {}", __FUNCTION__,
(unsigned int)m_outputBufferIndex, (unsigned int)outputStream,
StreamDescriptionToString(outputFormat, formatString),
passthrough ? "passthrough" : "");
m_device.Open(deviceID);
SetHogMode(passthrough);
// Configure the output stream object
m_outputStream.Open(outputStream);
AudioStreamBasicDescription virtualFormat, previousPhysicalFormat;
m_outputStream.GetVirtualFormat(&virtualFormat);
m_outputStream.GetPhysicalFormat(&previousPhysicalFormat);
CLog::Log(LOGDEBUG, "{}: Previous Virtual Format: {}", __FUNCTION__,
StreamDescriptionToString(virtualFormat, formatString));
CLog::Log(LOGDEBUG, "{}: Previous Physical Format: {}", __FUNCTION__,
StreamDescriptionToString(previousPhysicalFormat, formatString));
m_outputStream.SetPhysicalFormat(&outputFormat); // Set the active format (the old one will be reverted when we close)
if (passthrough && numOutputChannelsVirt > 0)
m_outputStream.SetVirtualFormat(&outputFormatVirt);
m_outputStream.GetVirtualFormat(&virtualFormat);
CLog::Log(LOGDEBUG, "{}: New Virtual Format: {}", __FUNCTION__,
StreamDescriptionToString(virtualFormat, formatString));
CLog::Log(LOGDEBUG, "{}: New Physical Format: {}", __FUNCTION__,
StreamDescriptionToString(outputFormat, formatString));
if (requestedSourceId != INT_MAX && !m_device.SetDataSource(requestedSourceId))
CLog::Log(LOGERROR, "{}: Error setting requested audio source.", __FUNCTION__);
m_latentFrames = m_device.GetNumLatencyFrames();
m_latentFrames += m_outputStream.GetNumLatencyFrames();
// update the channel map based on the new stream format
devEnum.GetAEChannelMap(format.m_channelLayout, numOutputChannels);
//! @todo Should we use the virtual format to determine our data format?
format.m_frameSize = format.m_channelLayout.Count() * (CAEUtil::DataFormatToBits(format.m_dataFormat) >> 3);
format.m_frames = m_device.GetBufferSize();
m_frameSizePerPlane = format.m_frameSize / m_planes;
m_framesPerSecond = format.m_sampleRate;
unsigned int num_buffers = 4;
m_buffer = new AERingBuffer(num_buffers * format.m_frames * m_frameSizePerPlane, m_planes);
CLog::Log(LOGDEBUG, "{}: using buffer size: {} ({:f} ms)", __FUNCTION__, m_buffer->GetMaxSize(),
(float)m_buffer->GetMaxSize() / (m_framesPerSecond * m_frameSizePerPlane));
if (!passthrough)
format.m_dataFormat = (m_planes > 1) ? AE_FMT_FLOATP : AE_FMT_FLOAT;
// Register for data request callbacks from the driver and start
m_device.AddIOProc(renderCallback, this);
m_device.Start();
return true;
}
void CAESinkDARWINOSX::SetHogMode(bool on)
{
//! @todo Auto hogging sets this for us. Figure out how/when to turn it off or use it
//! It appears that leaving this set will also restore the previous stream format when the
//! Application exits. If auto hogging is set and we try to set hog mode, we will deadlock
//! From the SDK docs: "If the AudioDevice is in a non-mixable mode, the HAL will automatically take hog mode on behalf of the first process to start an IOProc."
//!
//! Lock down the device. This MUST be done PRIOR to switching to a non-mixable format, if it is done at all
//! If it is attempted after the format change, there is a high likelihood of a deadlock
//! We may need to do this sooner to enable mix-disable (i.e. before setting the stream format)
if (on)
{
// Auto-Hog does not always un-hog the device when changing back to a mixable mode.
// Handle this on our own until it is fixed.
CCoreAudioHardware::SetAutoHogMode(false);
bool autoHog = CCoreAudioHardware::GetAutoHogMode();
CLog::Log(LOGDEBUG,
" CoreAudioRenderer::InitializeEncoded: "
"Auto 'hog' mode is set to '{}'.",
autoHog ? "On" : "Off");
if (autoHog)
return;
}
m_device.SetHogStatus(on);
m_device.SetMixingSupport(!on);
}
void CAESinkDARWINOSX::Deinitialize()
{
m_device.Stop();
m_device.RemoveIOProc();
m_outputStream.Close();
m_device.Close();
if (m_buffer)
{
delete m_buffer;
m_buffer = NULL;
}
m_outputBufferIndex = 0;
m_outputBitstream = false;
m_planes = 1;
m_started = false;
}
void CAESinkDARWINOSX::GetDelay(AEDelayStatus& status)
{
/* lockless way of guaranteeing consistency of tick/delay/buffer,
* this work since render callback is short and quick and higher
* priority compared to this thread, unsigned int are assumed
* aligned and having atomic read/write */
unsigned int size;
CAESpinLock lock(m_render_locker);
do
{
status.tick = m_render_tick;
status.delay = m_render_delay;
if(m_buffer)
size = m_buffer->GetReadSize();
else
size = 0;
} while(lock.retry());
status.delay += (double)size / (double)m_frameSizePerPlane / (double)m_framesPerSecond;
status.delay += (double)m_latentFrames / (double)m_framesPerSecond;
}
double CAESinkDARWINOSX::GetCacheTotal()
{
return (double)m_buffer->GetMaxSize() / (double)(m_frameSizePerPlane * m_framesPerSecond);
}
CCriticalSection mutex;
XbmcThreads::ConditionVariable condVar;
unsigned int CAESinkDARWINOSX::AddPackets(uint8_t **data, unsigned int frames, unsigned int offset)
{
if (m_buffer->GetWriteSize() < frames * m_frameSizePerPlane)
{ // no space to write - wait for a bit
std::unique_lock<CCriticalSection> lock(mutex);
auto timeout = std::chrono::milliseconds(900 * frames / m_framesPerSecond);
if (!m_started)
timeout = 4500ms;
// we are using a timer here for being sure for timeouts
// condvar can be woken spuriously as signaled
XbmcThreads::EndTime<> timer(timeout);
condVar.wait(mutex, timeout);
if (!m_started && timer.IsTimePast())
{
CLog::Log(LOGERROR, "{} engine didn't start in {} ms!", __FUNCTION__, timeout.count());
return INT_MAX;
}
}
unsigned int write_frames = std::min(frames, m_buffer->GetWriteSize() / m_frameSizePerPlane);
if (write_frames)
{
for (unsigned int i = 0; i < m_buffer->NumPlanes(); i++)
m_buffer->Write(data[i] + offset * m_frameSizePerPlane, write_frames * m_frameSizePerPlane, i);
}
return write_frames;
}
void CAESinkDARWINOSX::Drain()
{
int bytes = m_buffer->GetReadSize();
int totalBytes = bytes;
int maxNumTimeouts = 3;
auto timeout = std::chrono::milliseconds(900 * bytes / (m_framesPerSecond * m_frameSizePerPlane));
while (bytes && maxNumTimeouts > 0)
{
std::unique_lock<CCriticalSection> lock(mutex);
XbmcThreads::EndTime<> timer(timeout);
condVar.wait(mutex, timeout);
bytes = m_buffer->GetReadSize();
// if we timeout and don't
// consume bytes - decrease maxNumTimeouts
if (timer.IsTimePast() && bytes == totalBytes)
maxNumTimeouts--;
totalBytes = bytes;
}
}
void CAESinkDARWINOSX::EnumerateDevicesEx(AEDeviceInfoList &list, bool force)
{
EnumerateDevices();
list.clear();
for (CADeviceList::const_iterator i = s_devices.begin(); i != s_devices.end(); ++i)
list.push_back(i->second);
}
inline void LogLevel(unsigned int got, unsigned int wanted)
{
static unsigned int lastReported = INT_MAX;
if (got != wanted)
{
if (got != lastReported)
{
CLog::Log(LOGWARNING, "DARWINOSX: {}flow ({} vs {} bytes)", got > wanted ? "over" : "under",
got, wanted);
lastReported = got;
}
}
else
lastReported = INT_MAX; // indicate we were good at least once
}
OSStatus CAESinkDARWINOSX::renderCallback(AudioDeviceID inDevice, const AudioTimeStamp* inNow, const AudioBufferList* inInputData, const AudioTimeStamp* inInputTime, AudioBufferList* outOutputData, const AudioTimeStamp* inOutputTime, void* inClientData)
{
CAESinkDARWINOSX *sink = (CAESinkDARWINOSX*)inClientData;
sink->m_render_locker.enter(); /* grab lock */
sink->m_started = true;
if (outOutputData->mNumberBuffers)
{
//planar always starts at outputbuffer/streamidx 0
unsigned int startIdx = sink->m_buffer->NumPlanes() == 1 ? sink->m_outputBufferIndex : 0;
unsigned int endIdx = startIdx + sink->m_buffer->NumPlanes();
/* NOTE: We assume that the buffers are all the same size... */
if (sink->m_outputBitstream)
{
/* HACK for bitstreaming AC3/DTS via PCM.
We reverse the float->S16LE conversion done in the stream or device */
static const float mul = 1.0f / (INT16_MAX + 1);
size_t wanted = outOutputData->mBuffers[0].mDataByteSize / sizeof(float) * sizeof(int16_t);
size_t bytes = std::min((size_t)sink->m_buffer->GetReadSize(), wanted);
for (unsigned int j = 0; j < bytes / sizeof(int16_t); j++)
{
for (unsigned int i = startIdx; i < endIdx; i++)
{
int16_t src = 0;
sink->m_buffer->Read((unsigned char *)&src, sizeof(int16_t), i);
if (i < outOutputData->mNumberBuffers && outOutputData->mBuffers[i].mData)
{
float *dest = (float *)outOutputData->mBuffers[i].mData;
dest[j] = src * mul;
}
}
}
LogLevel(bytes, wanted);
}
else
{
/* buffers appear to come from CA already zero'd, so just copy what is wanted */
unsigned int wanted = outOutputData->mBuffers[0].mDataByteSize;
unsigned int bytes = std::min(sink->m_buffer->GetReadSize(), wanted);
for (unsigned int i = startIdx; i < endIdx; i++)
{
if (i < outOutputData->mNumberBuffers && outOutputData->mBuffers[i].mData)
sink->m_buffer->Read((unsigned char *)outOutputData->mBuffers[i].mData, bytes, i);
else
sink->m_buffer->Read(NULL, bytes, i);
}
LogLevel(bytes, wanted);
}
// tell the sink we're good for more data
condVar.notifyAll();
}
sink->m_render_delay = (double)(inOutputTime->mHostTime - inNow->mHostTime) / CurrentHostFrequency();
sink->m_render_tick = inNow->mHostTime;
sink->m_render_locker.leave();
return noErr;
}
|