| 1 |
/* |
| 2 |
* Copyright (C) 2014-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 "AEDeviceEnumerationOSX.h" |
| 10 |
|
| 11 |
#include "cores/AudioEngine/Sinks/darwin/CoreAudioHelpers.h" |
| 12 |
#include "cores/AudioEngine/Sinks/osx/CoreAudioChannelLayout.h" |
| 13 |
#include "cores/AudioEngine/Utils/AEUtil.h" |
| 14 |
#include "utils/StringUtils.h" |
| 15 |
#include "utils/log.h" |
| 16 |
|
| 17 |
#include <sstream> |
| 18 |
|
| 19 |
#define CA_MAX_CHANNELS 72 |
| 20 |
// default channel map - in case it can't be fetched from the device |
| 21 |
static enum AEChannel CAChannelMap[CA_MAX_CHANNELS + 1] = { |
| 22 |
AE_CH_FL , AE_CH_FR , AE_CH_BL , AE_CH_BR , AE_CH_FC , AE_CH_LFE , AE_CH_SL , AE_CH_SR , |
| 23 |
AE_CH_UNKNOWN1 , AE_CH_UNKNOWN2 , AE_CH_UNKNOWN3 , AE_CH_UNKNOWN4 , |
| 24 |
AE_CH_UNKNOWN5 , AE_CH_UNKNOWN6 , AE_CH_UNKNOWN7 , AE_CH_UNKNOWN8 , |
| 25 |
AE_CH_UNKNOWN9 , AE_CH_UNKNOWN10 , AE_CH_UNKNOWN11 , AE_CH_UNKNOWN12 , |
| 26 |
AE_CH_UNKNOWN13 , AE_CH_UNKNOWN14 , AE_CH_UNKNOWN15 , AE_CH_UNKNOWN16 , |
| 27 |
AE_CH_UNKNOWN17 , AE_CH_UNKNOWN18 , AE_CH_UNKNOWN19 , AE_CH_UNKNOWN20 , |
| 28 |
AE_CH_UNKNOWN21 , AE_CH_UNKNOWN22 , AE_CH_UNKNOWN23 , AE_CH_UNKNOWN24 , |
| 29 |
AE_CH_UNKNOWN25 , AE_CH_UNKNOWN26 , AE_CH_UNKNOWN27 , AE_CH_UNKNOWN28 , |
| 30 |
AE_CH_UNKNOWN29 , AE_CH_UNKNOWN30 , AE_CH_UNKNOWN31 , AE_CH_UNKNOWN32 , |
| 31 |
AE_CH_UNKNOWN33 , AE_CH_UNKNOWN34 , AE_CH_UNKNOWN35 , AE_CH_UNKNOWN36 , |
| 32 |
AE_CH_UNKNOWN37 , AE_CH_UNKNOWN38 , AE_CH_UNKNOWN39 , AE_CH_UNKNOWN40 , |
| 33 |
AE_CH_UNKNOWN41 , AE_CH_UNKNOWN42 , AE_CH_UNKNOWN43 , AE_CH_UNKNOWN44 , |
| 34 |
AE_CH_UNKNOWN45 , AE_CH_UNKNOWN46 , AE_CH_UNKNOWN47 , AE_CH_UNKNOWN48 , |
| 35 |
AE_CH_UNKNOWN49 , AE_CH_UNKNOWN50 , AE_CH_UNKNOWN51 , AE_CH_UNKNOWN52 , |
| 36 |
AE_CH_UNKNOWN53 , AE_CH_UNKNOWN54 , AE_CH_UNKNOWN55 , AE_CH_UNKNOWN56 , |
| 37 |
AE_CH_UNKNOWN57 , AE_CH_UNKNOWN58 , AE_CH_UNKNOWN59 , AE_CH_UNKNOWN60 , |
| 38 |
AE_CH_UNKNOWN61 , AE_CH_UNKNOWN62 , AE_CH_UNKNOWN63 , AE_CH_UNKNOWN64 , |
| 39 |
|
| 40 |
AE_CH_NULL |
| 41 |
}; |
| 42 |
|
| 43 |
AEDeviceEnumerationOSX::AEDeviceEnumerationOSX(AudioDeviceID deviceID) |
| 44 |
: m_deviceID(deviceID), m_caDevice(deviceID) |
| 45 |
{ |
| 46 |
Enumerate(); |
| 47 |
} |
| 48 |
|
| 49 |
bool AEDeviceEnumerationOSX::Enumerate() |
| 50 |
{ |
| 51 |
AudioStreamIdList streamList; |
| 52 |
bool isDigital = isDigitalDevice(); |
| 53 |
bool ret = false; |
| 54 |
UInt32 transportType = m_caDevice.GetTransportType(); |
| 55 |
m_caStreamInfos.clear(); |
| 56 |
m_isPlanar = true; |
| 57 |
m_deviceName = m_caDevice.GetName(); |
| 58 |
|
| 59 |
if (m_caDevice.GetStreams(&streamList)) |
| 60 |
{ |
| 61 |
for (UInt32 streamIdx = 0; streamIdx < streamList.size(); streamIdx++) |
| 62 |
{ |
| 63 |
caStreamInfo info; |
| 64 |
info.streamID = streamList[streamIdx]; |
| 65 |
info.numChannels = m_caDevice.GetNumChannelsOfStream(streamIdx); |
| 66 |
// one stream with num channels other then 1 is enough to make this device non-planar |
| 67 |
if (info.numChannels != 1) |
| 68 |
m_isPlanar = false; |
| 69 |
|
| 70 |
CCoreAudioStream::GetAvailablePhysicalFormats(streamList[streamIdx], &info.formatList); |
| 71 |
|
| 72 |
CCoreAudioStream::GetAvailableVirtualFormats(streamList[streamIdx], &info.formatListVirt); |
| 73 |
|
| 74 |
hasPassthroughOrDigitalFormats(info.formatList, info.hasPassthroughFormats, info.isDigital); |
| 75 |
|
| 76 |
info.isDigital |= isDigital; |
| 77 |
info.deviceType = getDeviceType(info.hasPassthroughFormats, info.isDigital, info.numChannels, transportType); |
| 78 |
m_caStreamInfos.push_back(info); |
| 79 |
} |
| 80 |
ret = true; |
| 81 |
} |
| 82 |
|
| 83 |
return ret; |
| 84 |
} |
| 85 |
|
| 86 |
// device/stream is digital if the transportType is digital |
| 87 |
// or the devicename suggests that it is digital |
| 88 |
bool AEDeviceEnumerationOSX::isDigitalDevice() const |
| 89 |
{ |
| 90 |
bool isDigital = m_caDevice.IsDigital(); |
| 91 |
|
| 92 |
// if it is no digital stream per definition |
| 93 |
// check if the device name suggests that it is digital |
| 94 |
// (some hackintonshs are not so smart in announcing correct |
| 95 |
// ca devices ... |
| 96 |
if (!isDigital) |
| 97 |
{ |
| 98 |
std::string devNameLower = m_caDevice.GetName(); |
| 99 |
StringUtils::ToLower(devNameLower); |
| 100 |
isDigital = devNameLower.find("digital") != std::string::npos; |
| 101 |
} |
| 102 |
return isDigital; |
| 103 |
} |
| 104 |
|
| 105 |
void AEDeviceEnumerationOSX::hasPassthroughOrDigitalFormats(const StreamFormatList &formatList, bool &hasPassthroughFormats, bool &hasDigitalFormat) const |
| 106 |
{ |
| 107 |
hasDigitalFormat = false; |
| 108 |
hasPassthroughFormats = false; |
| 109 |
|
| 110 |
for(UInt32 formatIdx = 0; formatIdx < formatList.size(); formatIdx++) |
| 111 |
{ |
| 112 |
const AudioStreamBasicDescription &desc = formatList[formatIdx].mFormat; |
| 113 |
if (desc.mFormatID == kAudioFormatAC3 || desc.mFormatID == kAudioFormat60958AC3) |
| 114 |
{ |
| 115 |
hasDigitalFormat = true; |
| 116 |
hasPassthroughFormats = true; |
| 117 |
break; |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
// PassthroughFormat 2ch 16bit LE 48000Hz / 192000Hz */ |
| 122 |
if (desc.mBitsPerChannel == 16 && |
| 123 |
!(desc.mFormatFlags & kAudioFormatFlagIsBigEndian) && |
| 124 |
desc.mChannelsPerFrame == 2 && |
| 125 |
(desc.mSampleRate == 48000 || desc.mSampleRate == 192000)) |
| 126 |
{ |
| 127 |
hasPassthroughFormats = true; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
enum AEDeviceType AEDeviceEnumerationOSX::getDeviceType(bool hasPassthroughFormats, |
| 134 |
bool isDigital, |
| 135 |
UInt32 numChannels, |
| 136 |
UInt32 transportType) const |
| 137 |
{ |
| 138 |
// flag indicating that the device name "sounds" like HDMI |
| 139 |
bool hasHdmiName = m_deviceName.find("HDMI") != std::string::npos; |
| 140 |
// flag indicating that the device name "sounds" like DisplayPort |
| 141 |
bool hasDisplayPortName = m_deviceName.find("DisplayPort") != std::string::npos; |
| 142 |
enum AEDeviceType deviceType = AE_DEVTYPE_PCM;//default |
| 143 |
|
| 144 |
// decide the type of the device based on the discovered information |
| 145 |
// in the streams |
| 146 |
// device defaults to PCM (see start of the while loop) |
| 147 |
// it can be HDMI, DisplayPort or Optical |
| 148 |
// for all of those types it needs to support |
| 149 |
// passthroughformats and needs to be a digital port |
| 150 |
if (hasPassthroughFormats && isDigital) |
| 151 |
{ |
| 152 |
// if the max number of channels was more then 2 |
| 153 |
// this can be HDMI or DisplayPort or Thunderbolt |
| 154 |
if (numChannels > 2) |
| 155 |
{ |
| 156 |
// either the devicename suggests its HDMI |
| 157 |
// or CA reported the transportType as HDMI |
| 158 |
if (hasHdmiName || transportType == kIOAudioDeviceTransportTypeHdmi) |
| 159 |
deviceType = AE_DEVTYPE_HDMI; |
| 160 |
|
| 161 |
// either the devicename suggests its DisplayPort |
| 162 |
// or CA reported the transportType as DisplayPort or Thunderbolt |
| 163 |
if (hasDisplayPortName || |
| 164 |
transportType == kIOAudioDeviceTransportTypeDisplayPort || |
| 165 |
transportType == kIOAudioDeviceTransportTypeThunderbolt) |
| 166 |
deviceType = AE_DEVTYPE_DP; |
| 167 |
} |
| 168 |
else// treat all other digital passthrough devices as optical |
| 169 |
deviceType = AE_DEVTYPE_IEC958; |
| 170 |
|
| 171 |
//treat all other digital devices as HDMI to let options open to the user |
| 172 |
if (deviceType == AE_DEVTYPE_PCM) |
| 173 |
deviceType = AE_DEVTYPE_HDMI; |
| 174 |
} |
| 175 |
|
| 176 |
// devicename based overwrites from former code - maybe FIXME at some point when we |
| 177 |
// are sure that the upper detection does its job in all[tm] use cases |
| 178 |
if (hasHdmiName) |
| 179 |
deviceType = AE_DEVTYPE_HDMI; |
| 180 |
if (hasDisplayPortName) |
| 181 |
deviceType = AE_DEVTYPE_DP; |
| 182 |
|
| 183 |
return deviceType; |
| 184 |
} |
| 185 |
|
| 186 |
CADeviceList AEDeviceEnumerationOSX::GetDeviceInfoList() const |
| 187 |
{ |
| 188 |
CADeviceList list; |
| 189 |
UInt32 numDevices = m_caStreamInfos.size(); |
| 190 |
if (m_isPlanar) |
| 191 |
numDevices = 1; |
| 192 |
|
| 193 |
for (UInt32 streamIdx = 0; streamIdx < numDevices; streamIdx++) |
| 194 |
{ |
| 195 |
CAEDeviceInfo deviceInfo; |
| 196 |
struct CADeviceInstance devInstance; |
| 197 |
devInstance.audioDeviceId = m_deviceID; |
| 198 |
devInstance.streamIndex = streamIdx; |
| 199 |
devInstance.sourceId = INT_MAX;//don't set audio source by default |
| 200 |
|
| 201 |
deviceInfo.m_deviceName = getDeviceNameForStream(streamIdx); |
| 202 |
deviceInfo.m_displayName = m_deviceName; |
| 203 |
deviceInfo.m_displayNameExtra = getExtraDisplayNameForStream(streamIdx); |
| 204 |
deviceInfo.m_channels = getChannelInfoForStream(streamIdx); |
| 205 |
deviceInfo.m_sampleRates = getSampleRateListForStream(streamIdx); |
| 206 |
deviceInfo.m_dataFormats = getFormatListForStream(streamIdx); |
| 207 |
deviceInfo.m_streamTypes = getTypeListForStream(streamIdx); |
| 208 |
deviceInfo.m_deviceType = m_caStreamInfos[streamIdx].deviceType; |
| 209 |
deviceInfo.m_wantsIECPassthrough = true; |
| 210 |
|
| 211 |
CoreAudioDataSourceList sourceList; |
| 212 |
// if this enumerator contains multiple devices with more then 1 source we add :source suffixes to the |
| 213 |
// device names and overwrite the extraname with the source name |
| 214 |
if (numDevices == 1 && m_caDevice.GetDataSources(&sourceList) && sourceList.size() > 1) |
| 215 |
{ |
| 216 |
for (unsigned sourceIdx = 0; sourceIdx < sourceList.size(); sourceIdx++) |
| 217 |
{ |
| 218 |
std::stringstream sourceIdxStr; |
| 219 |
sourceIdxStr << sourceIdx; |
| 220 |
deviceInfo.m_deviceName = getDeviceNameForStream(streamIdx) + ":source" + sourceIdxStr.str(); |
| 221 |
deviceInfo.m_displayNameExtra = m_caDevice.GetDataSourceName(sourceList[sourceIdx]); |
| 222 |
devInstance.sourceId = sourceList[sourceIdx]; |
| 223 |
list.emplace_back(devInstance, deviceInfo); |
| 224 |
} |
| 225 |
} |
| 226 |
else |
| 227 |
list.emplace_back(devInstance, deviceInfo); |
| 228 |
} |
| 229 |
return list; |
| 230 |
} |
| 231 |
|
| 232 |
unsigned int AEDeviceEnumerationOSX::GetNumPlanes() const |
| 233 |
{ |
| 234 |
if (m_isPlanar) |
| 235 |
return m_caStreamInfos.size(); |
| 236 |
else |
| 237 |
return 1;//interleaved - one plane |
| 238 |
} |
| 239 |
|
| 240 |
bool AEDeviceEnumerationOSX::hasSampleRate(const AESampleRateList &list, const unsigned int samplerate) const |
| 241 |
{ |
| 242 |
for (size_t i = 0; i < list.size(); ++i) |
| 243 |
{ |
| 244 |
if (list[i] == samplerate) |
| 245 |
return true; |
| 246 |
} |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
bool AEDeviceEnumerationOSX::hasDataFormat(const AEDataFormatList &list, const enum AEDataFormat format) const |
| 251 |
{ |
| 252 |
for (size_t i = 0; i < list.size(); ++i) |
| 253 |
{ |
| 254 |
if (list[i] == format) |
| 255 |
return true; |
| 256 |
} |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
bool AEDeviceEnumerationOSX::hasDataType(const AEDataTypeList &list, CAEStreamInfo::DataType type) const |
| 261 |
{ |
| 262 |
for (size_t i = 0; i < list.size(); ++i) |
| 263 |
{ |
| 264 |
if (list[i] == type) |
| 265 |
return true; |
| 266 |
} |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
AEDataFormatList AEDeviceEnumerationOSX::getFormatListForStream(UInt32 streamIdx) const |
| 271 |
{ |
| 272 |
AEDataFormatList returnDataFormatList; |
| 273 |
if (streamIdx >= m_caStreamInfos.size()) |
| 274 |
return returnDataFormatList; |
| 275 |
|
| 276 |
// check the streams |
| 277 |
const StreamFormatList &formatList = m_caStreamInfos[streamIdx].formatList; |
| 278 |
for(UInt32 formatIdx = 0; formatIdx < formatList.size(); formatIdx++) |
| 279 |
{ |
| 280 |
AudioStreamBasicDescription formatDesc = formatList[formatIdx].mFormat; |
| 281 |
AEDataFormatList aeFormatList = caFormatToAE(formatDesc, m_caStreamInfos[streamIdx].isDigital); |
| 282 |
for (UInt32 formatListIdx = 0; formatListIdx < aeFormatList.size(); formatListIdx++) |
| 283 |
{ |
| 284 |
if (!hasDataFormat(returnDataFormatList, aeFormatList[formatListIdx])) |
| 285 |
returnDataFormatList.push_back(aeFormatList[formatListIdx]); |
| 286 |
} |
| 287 |
} |
| 288 |
return returnDataFormatList; |
| 289 |
} |
| 290 |
|
| 291 |
AEDataTypeList AEDeviceEnumerationOSX::getTypeListForStream(UInt32 streamIdx) const |
| 292 |
{ |
| 293 |
AEDataTypeList returnDataTypeList; |
| 294 |
if (streamIdx >= m_caStreamInfos.size()) |
| 295 |
return returnDataTypeList; |
| 296 |
|
| 297 |
// check the streams |
| 298 |
const StreamFormatList &formatList = m_caStreamInfos[streamIdx].formatList; |
| 299 |
for(UInt32 formatIdx = 0; formatIdx < formatList.size(); formatIdx++) |
| 300 |
{ |
| 301 |
AudioStreamBasicDescription formatDesc = formatList[formatIdx].mFormat; |
| 302 |
AEDataTypeList aeTypeList = caFormatToAEType(formatDesc, m_caStreamInfos[streamIdx].isDigital); |
| 303 |
for (UInt32 typeListIdx = 0; typeListIdx < aeTypeList.size(); typeListIdx++) |
| 304 |
{ |
| 305 |
if (!hasDataType(returnDataTypeList, aeTypeList[typeListIdx])) |
| 306 |
returnDataTypeList.push_back(aeTypeList[typeListIdx]); |
| 307 |
} |
| 308 |
} |
| 309 |
return returnDataTypeList; |
| 310 |
} |
| 311 |
|
| 312 |
CAEChannelInfo AEDeviceEnumerationOSX::getChannelInfoForStream(UInt32 streamIdx) const |
| 313 |
{ |
| 314 |
CAEChannelInfo channelInfo; |
| 315 |
if (streamIdx >= m_caStreamInfos.size()) |
| 316 |
return channelInfo; |
| 317 |
|
| 318 |
if (m_isPlanar) |
| 319 |
{ |
| 320 |
//get channel map to match the devices channel layout as set in audio-midi-setup |
| 321 |
GetAEChannelMap(channelInfo, GetNumPlanes()); |
| 322 |
} |
| 323 |
else |
| 324 |
{ |
| 325 |
//get channel map to match the devices channel layout as set in audio-midi-setup |
| 326 |
GetAEChannelMap(channelInfo, m_caDevice.GetNumChannelsOfStream(streamIdx)); |
| 327 |
} |
| 328 |
return channelInfo; |
| 329 |
} |
| 330 |
|
| 331 |
AEDataFormatList AEDeviceEnumerationOSX::caFormatToAE(const AudioStreamBasicDescription &formatDesc, bool isDigital) const |
| 332 |
{ |
| 333 |
AEDataFormatList formatList; |
| 334 |
// add stream format info |
| 335 |
switch (formatDesc.mFormatID) |
| 336 |
{ |
| 337 |
case kAudioFormatAC3: |
| 338 |
case kAudioFormat60958AC3: |
| 339 |
break; |
| 340 |
default: |
| 341 |
switch(formatDesc.mBitsPerChannel) |
| 342 |
{ |
| 343 |
case 16: |
| 344 |
if (formatDesc.mFormatFlags & kAudioFormatFlagIsBigEndian) |
| 345 |
{ |
| 346 |
formatList.push_back(AE_FMT_S16BE); |
| 347 |
} |
| 348 |
else |
| 349 |
{ |
| 350 |
formatList.push_back(AE_FMT_S16LE); |
| 351 |
} |
| 352 |
formatList.push_back(AE_FMT_S16NE); // if we support either LE or BE - we always support NE too... |
| 353 |
break; |
| 354 |
case 24: |
| 355 |
if (formatDesc.mFormatFlags & kAudioFormatFlagIsBigEndian) |
| 356 |
{ |
| 357 |
formatList.push_back(AE_FMT_S24BE3); |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
formatList.push_back(AE_FMT_S24LE3); |
| 362 |
} |
| 363 |
formatList.push_back(AE_FMT_S24NE3); // if we support either LE or BE - we always support NE too... |
| 364 |
break; |
| 365 |
case 32: |
| 366 |
if (formatDesc.mFormatFlags & kAudioFormatFlagIsFloat) |
| 367 |
{ |
| 368 |
formatList.push_back(AE_FMT_FLOAT); |
| 369 |
} |
| 370 |
else |
| 371 |
{ |
| 372 |
if (formatDesc.mFormatFlags & kAudioFormatFlagIsBigEndian) |
| 373 |
{ |
| 374 |
formatList.push_back(AE_FMT_S32BE); |
| 375 |
} |
| 376 |
else |
| 377 |
{ |
| 378 |
formatList.push_back(AE_FMT_S32LE); |
| 379 |
} |
| 380 |
formatList.push_back(AE_FMT_S32NE); // if we support either LE or BE - we always support NE too... |
| 381 |
} |
| 382 |
break; |
| 383 |
} |
| 384 |
break; |
| 385 |
} |
| 386 |
return formatList; |
| 387 |
} |
| 388 |
|
| 389 |
AEDataTypeList AEDeviceEnumerationOSX::caFormatToAEType(const AudioStreamBasicDescription &formatDesc, bool isDigital) const |
| 390 |
{ |
| 391 |
AEDataTypeList typeList; |
| 392 |
// add stream format info |
| 393 |
switch (formatDesc.mFormatID) |
| 394 |
{ |
| 395 |
case kAudioFormatAC3: |
| 396 |
case kAudioFormat60958AC3: |
| 397 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_AC3); |
| 398 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_DTS_512); |
| 399 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_DTS_1024); |
| 400 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_DTS_2048); |
| 401 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_DTSHD_CORE); |
| 402 |
break; |
| 403 |
default: |
| 404 |
switch(formatDesc.mBitsPerChannel) |
| 405 |
{ |
| 406 |
case 16: |
| 407 |
if (formatDesc.mFormatFlags & kAudioFormatFlagIsBigEndian) |
| 408 |
; |
| 409 |
else |
| 410 |
{ |
| 411 |
/* Passthrough is possible with a 2ch digital output */ |
| 412 |
if (formatDesc.mChannelsPerFrame == 2 && isDigital) |
| 413 |
{ |
| 414 |
if (formatDesc.mSampleRate == 48000) |
| 415 |
{ |
| 416 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_AC3); |
| 417 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_DTS_512); |
| 418 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_DTS_1024); |
| 419 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_DTS_2048); |
| 420 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_DTSHD_CORE); |
| 421 |
} |
| 422 |
else if (formatDesc.mSampleRate == 192000) |
| 423 |
{ |
| 424 |
typeList.push_back(CAEStreamInfo::STREAM_TYPE_EAC3); |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
break; |
| 429 |
} |
| 430 |
break; |
| 431 |
} |
| 432 |
return typeList; |
| 433 |
} |
| 434 |
|
| 435 |
AESampleRateList AEDeviceEnumerationOSX::getSampleRateListForStream(UInt32 streamIdx) const |
| 436 |
{ |
| 437 |
AESampleRateList returnSampleRateList; |
| 438 |
if (streamIdx >= m_caStreamInfos.size()) |
| 439 |
return returnSampleRateList; |
| 440 |
|
| 441 |
// check the streams |
| 442 |
const StreamFormatList &formatList = m_caStreamInfos[streamIdx].formatList; |
| 443 |
for(UInt32 formatIdx = 0; formatIdx < formatList.size(); formatIdx++) |
| 444 |
{ |
| 445 |
AudioStreamBasicDescription formatDesc = formatList[formatIdx].mFormat; |
| 446 |
// add sample rate info |
| 447 |
// for devices which return kAudioStreamAnyRatee |
| 448 |
// we add 44.1khz and 48khz - user can use |
| 449 |
// the "fixed" audio config to force one of them |
| 450 |
if (formatDesc.mSampleRate == kAudioStreamAnyRate) |
| 451 |
{ |
| 452 |
CLog::Log(LOGINFO, "{} reported samplerate is kAudioStreamAnyRate adding 44.1khz and 48khz", |
| 453 |
__FUNCTION__); |
| 454 |
formatDesc.mSampleRate = 44100; |
| 455 |
if (!hasSampleRate(returnSampleRateList, formatDesc.mSampleRate)) |
| 456 |
returnSampleRateList.push_back(formatDesc.mSampleRate); |
| 457 |
formatDesc.mSampleRate = 48000; |
| 458 |
} |
| 459 |
|
| 460 |
if (!hasSampleRate(returnSampleRateList, formatDesc.mSampleRate)) |
| 461 |
returnSampleRateList.push_back(formatDesc.mSampleRate); |
| 462 |
} |
| 463 |
|
| 464 |
return returnSampleRateList; |
| 465 |
} |
| 466 |
|
| 467 |
std::string AEDeviceEnumerationOSX::getDeviceNameForStream(UInt32 streamIdx) const |
| 468 |
{ |
| 469 |
std::stringstream deviceIdStr; |
| 470 |
deviceIdStr << m_deviceID; |
| 471 |
|
| 472 |
// device name is the devicename from coreaudio + the device id to make it unique |
| 473 |
// for example devicename could be DisplayPort and couldn't be distinguished if multiple |
| 474 |
// DisplayPort devices are available |
| 475 |
std::string deviceName = m_deviceName + "-" + deviceIdStr.str(); |
| 476 |
if (m_isPlanar)// planar devices are saved as :stream0 |
| 477 |
{ |
| 478 |
deviceName += ":stream0"; |
| 479 |
} |
| 480 |
else |
| 481 |
{ |
| 482 |
std::stringstream streamIdxStr; |
| 483 |
streamIdxStr << streamIdx; |
| 484 |
deviceName += ":stream" + streamIdxStr.str(); |
| 485 |
} |
| 486 |
return deviceName; |
| 487 |
} |
| 488 |
|
| 489 |
std::string AEDeviceEnumerationOSX::getExtraDisplayNameForStream(UInt32 streamIdx) const |
| 490 |
{ |
| 491 |
// for distinguishing the streams inside one device we add |
| 492 |
// the corresponding channels to the extraDisplayName |
| 493 |
// planar devices are ignored here as their streams are |
| 494 |
// the channels not different subdevices |
| 495 |
if (m_caStreamInfos.size() > 1 && !m_isPlanar) |
| 496 |
{ |
| 497 |
// build a string with the channels for this stream |
| 498 |
UInt32 startChannel = 0; |
| 499 |
CCoreAudioStream::GetStartingChannelInDevice(m_caStreamInfos[streamIdx].streamID, startChannel); |
| 500 |
UInt32 numChannels = m_caDevice.GetNumChannelsOfStream(streamIdx); |
| 501 |
std::stringstream extraName; |
| 502 |
extraName << "Channels "; |
| 503 |
extraName << startChannel; |
| 504 |
extraName << " - "; |
| 505 |
extraName << startChannel + numChannels - 1; |
| 506 |
CLog::Log(LOGINFO, |
| 507 |
"{} adding stream {} as pseudo device with start channel {} and {} channels total", |
| 508 |
__FUNCTION__, (unsigned int)streamIdx, (unsigned int)startChannel, |
| 509 |
(unsigned int)numChannels); |
| 510 |
return extraName.str(); |
| 511 |
} |
| 512 |
|
| 513 |
//for all other devices use the datasource as extraname |
| 514 |
return m_caDevice.GetCurrentDataSourceName(); |
| 515 |
} |
| 516 |
|
| 517 |
float AEDeviceEnumerationOSX::scoreSampleRate(Float64 destinationRate, unsigned int sourceRate) const |
| 518 |
{ |
| 519 |
float score = 0; |
| 520 |
double intPortion; |
| 521 |
double fracPortion = modf(destinationRate / sourceRate, &intPortion); |
| 522 |
|
| 523 |
score += (1 - fracPortion) * 1000; // prefer sample rates that are multiples of the source sample rate |
| 524 |
score += (intPortion == 1.0) ? 500 : 0; // prefer exact matches over other multiples |
| 525 |
score += (intPortion > 1 && intPortion < 100) ? (100 - intPortion) / 100 * 100 : 0; // prefer smaller multiples otherwise |
| 526 |
|
| 527 |
return score; |
| 528 |
} |
| 529 |
|
| 530 |
float AEDeviceEnumerationOSX::ScoreFormat(const AudioStreamBasicDescription &formatDesc, const AEAudioFormat &format) const |
| 531 |
{ |
| 532 |
float score = 0; |
| 533 |
|
| 534 |
if (format.m_streamInfo.m_type != CAEStreamInfo::STREAM_TYPE_NULL) |
| 535 |
{ |
| 536 |
if (formatDesc.mBitsPerChannel != 16) |
| 537 |
return score; |
| 538 |
if (formatDesc.mSampleRate != format.m_sampleRate) |
| 539 |
return score; |
| 540 |
if (formatDesc.mChannelsPerFrame != format.m_channelLayout.Count()) |
| 541 |
return score; |
| 542 |
score += 5; |
| 543 |
|
| 544 |
if (formatDesc.mFormatID == kAudioFormat60958AC3 || |
| 545 |
formatDesc.mFormatID == 'IAC3' || |
| 546 |
formatDesc.mFormatID == kAudioFormatAC3) |
| 547 |
{ |
| 548 |
score += 1; |
| 549 |
} |
| 550 |
} |
| 551 |
// non-passthrough, whatever works is fine |
| 552 |
else if (formatDesc.mFormatID == kAudioFormatLinearPCM) |
| 553 |
{ |
| 554 |
score += scoreSampleRate(formatDesc.mSampleRate, format.m_sampleRate); |
| 555 |
|
| 556 |
if (formatDesc.mChannelsPerFrame == format.m_channelLayout.Count()) |
| 557 |
score += 5; |
| 558 |
else if (formatDesc.mChannelsPerFrame > format.m_channelLayout.Count()) |
| 559 |
score += 1; |
| 560 |
if (format.m_dataFormat == AE_FMT_FLOAT || format.m_dataFormat == AE_FMT_FLOATP) |
| 561 |
{ // for float, prefer the highest bitdepth we have |
| 562 |
if (formatDesc.mBitsPerChannel >= 16) |
| 563 |
score += (formatDesc.mBitsPerChannel / 8); |
| 564 |
} |
| 565 |
else |
| 566 |
{ |
| 567 |
if (formatDesc.mBitsPerChannel == CAEUtil::DataFormatToBits(format.m_dataFormat)) |
| 568 |
score += 5; |
| 569 |
else if (formatDesc.mBitsPerChannel > CAEUtil::DataFormatToBits(format.m_dataFormat)) |
| 570 |
score += 1; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
return score; |
| 575 |
} |
| 576 |
|
| 577 |
bool AEDeviceEnumerationOSX::FindSuitableFormatForStream(UInt32 &streamIdx, const AEAudioFormat &format, bool virt, AudioStreamBasicDescription &outputFormat, AudioStreamID &outputStream) const |
| 578 |
{ |
| 579 |
CLog::Log(LOGDEBUG, "{}: Finding stream for format {}", __FUNCTION__, |
| 580 |
CAEUtil::DataFormatToStr(format.m_dataFormat)); |
| 581 |
|
| 582 |
bool formatFound = false; |
| 583 |
float outputScore = 0; |
| 584 |
UInt32 streamIdxStart = streamIdx; |
| 585 |
UInt32 streamIdxEnd = streamIdx + 1; |
| 586 |
UInt32 streamIdxCurrent = streamIdx; |
| 587 |
|
| 588 |
if (streamIdx == INT_MAX) |
| 589 |
{ |
| 590 |
streamIdxStart = 0; |
| 591 |
streamIdxEnd = m_caStreamInfos.size(); |
| 592 |
streamIdxCurrent = 0; |
| 593 |
} |
| 594 |
|
| 595 |
if (streamIdxCurrent >= m_caStreamInfos.size()) |
| 596 |
return false; |
| 597 |
|
| 598 |
// loop over all streams or over given streams (depends on initial value of param streamIdx |
| 599 |
for(streamIdxCurrent = streamIdxStart; streamIdxCurrent < streamIdxEnd; streamIdxCurrent++) |
| 600 |
{ |
| 601 |
|
| 602 |
// Probe physical or virtual formats |
| 603 |
const StreamFormatList *formats = &m_caStreamInfos[streamIdxCurrent].formatList; |
| 604 |
if (virt) |
| 605 |
formats = &m_caStreamInfos[streamIdxCurrent].formatListVirt; |
| 606 |
|
| 607 |
for (StreamFormatList::const_iterator j = formats->begin(); j != formats->end(); ++j) |
| 608 |
{ |
| 609 |
AudioStreamBasicDescription formatDesc = j->mFormat; |
| 610 |
|
| 611 |
// for devices with kAudioStreamAnyRate |
| 612 |
// assume that the user uses a fixed config |
| 613 |
// and knows what he is doing - so we use |
| 614 |
// the requested samplerate here |
| 615 |
if (formatDesc.mSampleRate == kAudioStreamAnyRate) |
| 616 |
formatDesc.mSampleRate = format.m_sampleRate; |
| 617 |
|
| 618 |
float score = ScoreFormat(formatDesc, format); |
| 619 |
|
| 620 |
std::string formatString; |
| 621 |
if (!virt) |
| 622 |
CLog::Log(LOGDEBUG, "{}: Physical Format: {} rated {:f}", __FUNCTION__, |
| 623 |
StreamDescriptionToString(formatDesc, formatString), score); |
| 624 |
else |
| 625 |
CLog::Log(LOGDEBUG, "{}: Virtual Format: {} rated {:f}", __FUNCTION__, |
| 626 |
StreamDescriptionToString(formatDesc, formatString), score); |
| 627 |
|
| 628 |
if (score > outputScore) |
| 629 |
{ |
| 630 |
outputScore = score; |
| 631 |
outputFormat = formatDesc; |
| 632 |
outputStream = m_caStreamInfos[streamIdxCurrent].streamID; |
| 633 |
streamIdx = streamIdxCurrent;// return the streamIdx for the caller |
| 634 |
formatFound = true; |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
if (m_isPlanar) |
| 640 |
outputFormat.mChannelsPerFrame = std::min((size_t)format.m_channelLayout.Count(), m_caStreamInfos.size()); |
| 641 |
|
| 642 |
return formatFound; |
| 643 |
} |
| 644 |
|
| 645 |
// map coraudio channel labels to activeae channel labels |
| 646 |
enum AEChannel AEDeviceEnumerationOSX::caChannelToAEChannel(const AudioChannelLabel &CAChannelLabel) const |
| 647 |
{ |
| 648 |
enum AEChannel ret = AE_CH_NULL; |
| 649 |
static unsigned int unknownChannel = AE_CH_UNKNOWN1; |
| 650 |
switch(CAChannelLabel) |
| 651 |
{ |
| 652 |
case kAudioChannelLabel_Left: |
| 653 |
ret = AE_CH_FL; |
| 654 |
break; |
| 655 |
case kAudioChannelLabel_Right: |
| 656 |
ret = AE_CH_FR; |
| 657 |
break; |
| 658 |
case kAudioChannelLabel_Center: |
| 659 |
ret = AE_CH_FC; |
| 660 |
break; |
| 661 |
case kAudioChannelLabel_LFEScreen: |
| 662 |
ret = AE_CH_LFE; |
| 663 |
break; |
| 664 |
case kAudioChannelLabel_LeftSurroundDirect: |
| 665 |
ret = AE_CH_SL; |
| 666 |
break; |
| 667 |
case kAudioChannelLabel_RightSurroundDirect: |
| 668 |
ret = AE_CH_SR; |
| 669 |
break; |
| 670 |
case kAudioChannelLabel_LeftCenter: |
| 671 |
ret = AE_CH_FLOC; |
| 672 |
break; |
| 673 |
case kAudioChannelLabel_RightCenter: |
| 674 |
ret = AE_CH_FROC; |
| 675 |
break; |
| 676 |
case kAudioChannelLabel_CenterSurround: |
| 677 |
ret = AE_CH_TC; |
| 678 |
break; |
| 679 |
case kAudioChannelLabel_LeftSurround: |
| 680 |
ret = AE_CH_SL; |
| 681 |
break; |
| 682 |
case kAudioChannelLabel_RightSurround: |
| 683 |
ret = AE_CH_SR; |
| 684 |
break; |
| 685 |
case kAudioChannelLabel_VerticalHeightLeft: |
| 686 |
ret = AE_CH_TFL; |
| 687 |
break; |
| 688 |
case kAudioChannelLabel_VerticalHeightRight: |
| 689 |
ret = AE_CH_TFR; |
| 690 |
break; |
| 691 |
case kAudioChannelLabel_VerticalHeightCenter: |
| 692 |
ret = AE_CH_TFC; |
| 693 |
break; |
| 694 |
case kAudioChannelLabel_TopCenterSurround: |
| 695 |
ret = AE_CH_TC; |
| 696 |
break; |
| 697 |
case kAudioChannelLabel_TopBackLeft: |
| 698 |
ret = AE_CH_TBL; |
| 699 |
break; |
| 700 |
case kAudioChannelLabel_TopBackRight: |
| 701 |
ret = AE_CH_TBR; |
| 702 |
break; |
| 703 |
case kAudioChannelLabel_TopBackCenter: |
| 704 |
ret = AE_CH_TBC; |
| 705 |
break; |
| 706 |
case kAudioChannelLabel_RearSurroundLeft: |
| 707 |
ret = AE_CH_BL; |
| 708 |
break; |
| 709 |
case kAudioChannelLabel_RearSurroundRight: |
| 710 |
ret = AE_CH_BR; |
| 711 |
break; |
| 712 |
case kAudioChannelLabel_LeftWide: |
| 713 |
ret = AE_CH_BLOC; |
| 714 |
break; |
| 715 |
case kAudioChannelLabel_RightWide: |
| 716 |
ret = AE_CH_BROC; |
| 717 |
break; |
| 718 |
case kAudioChannelLabel_LFE2: |
| 719 |
ret = AE_CH_LFE; |
| 720 |
break; |
| 721 |
case kAudioChannelLabel_LeftTotal: |
| 722 |
ret = AE_CH_FL; |
| 723 |
break; |
| 724 |
case kAudioChannelLabel_RightTotal: |
| 725 |
ret = AE_CH_FR; |
| 726 |
break; |
| 727 |
case kAudioChannelLabel_HearingImpaired: |
| 728 |
ret = AE_CH_FC; |
| 729 |
break; |
| 730 |
case kAudioChannelLabel_Narration: |
| 731 |
ret = AE_CH_FC; |
| 732 |
break; |
| 733 |
case kAudioChannelLabel_Mono: |
| 734 |
ret = AE_CH_FC; |
| 735 |
break; |
| 736 |
case kAudioChannelLabel_DialogCentricMix: |
| 737 |
ret = AE_CH_FC; |
| 738 |
break; |
| 739 |
case kAudioChannelLabel_CenterSurroundDirect: |
| 740 |
ret = AE_CH_TC; |
| 741 |
break; |
| 742 |
case kAudioChannelLabel_Haptic: |
| 743 |
ret = AE_CH_FC; |
| 744 |
break; |
| 745 |
default: |
| 746 |
ret = (enum AEChannel)unknownChannel++; |
| 747 |
} |
| 748 |
if (unknownChannel == AE_CH_MAX) |
| 749 |
unknownChannel = AE_CH_UNKNOWN1; |
| 750 |
|
| 751 |
return ret; |
| 752 |
} |
| 753 |
|
| 754 |
//Note: in multichannel mode CA will either pull 2 channels of data (stereo) or 6/8 channels of data |
| 755 |
//(every speaker setup with more then 2 speakers). The difference between the number of real speakers |
| 756 |
//and 6/8 channels needs to be padded with unknown channels so that the sample size fits 6/8 channels |
| 757 |
// |
| 758 |
//device [in] - the device whose channel layout should be used |
| 759 |
//channelMap [in/out] - if filled it will it indicates that we are called from initialize and we log the requested map, out returns the channelMap for device |
| 760 |
//channelsPerFrame [in] - the number of channels this device is configured to (e.x. 2 or 6/8) |
| 761 |
void AEDeviceEnumerationOSX::GetAEChannelMap(CAEChannelInfo &channelMap, unsigned int channelsPerFrame) const |
| 762 |
{ |
| 763 |
CCoreAudioChannelLayout calayout; |
| 764 |
bool logMapping = channelMap.Count() > 0; // only log if the engine requests a layout during init |
| 765 |
bool mapAvailable = false; |
| 766 |
unsigned int numberChannelsInDeviceLayout = CA_MAX_CHANNELS; // default number of channels from CAChannelMap |
| 767 |
AudioChannelLayout *layout = NULL; |
| 768 |
|
| 769 |
// try to fetch either the multichannel or the stereo channel layout from the device |
| 770 |
if (channelsPerFrame == 2 || channelMap.Count() == 2) |
| 771 |
mapAvailable = m_caDevice.GetPreferredChannelLayoutForStereo(calayout); |
| 772 |
else |
| 773 |
mapAvailable = m_caDevice.GetPreferredChannelLayout(calayout); |
| 774 |
|
| 775 |
// if a map was fetched - check if it is usable |
| 776 |
if (mapAvailable) |
| 777 |
{ |
| 778 |
layout = calayout; |
| 779 |
if (layout == NULL || layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions) |
| 780 |
mapAvailable = false;// wrong map format |
| 781 |
else |
| 782 |
numberChannelsInDeviceLayout = layout->mNumberChannelDescriptions; |
| 783 |
} |
| 784 |
|
| 785 |
// start the mapping action |
| 786 |
// the number of channels to be added to the outgoing channelmap |
| 787 |
// this is CA_MAX_CHANNELS at max and might be lower for some output devices (channelsPerFrame) |
| 788 |
unsigned int numChannelsToMap = std::min((unsigned int)CA_MAX_CHANNELS, (unsigned int)channelsPerFrame); |
| 789 |
|
| 790 |
// if there was a map fetched we force the number of |
| 791 |
// channels to map to channelsPerFrame (this allows mapping |
| 792 |
// of more then CA_MAX_CHANNELS if needed) |
| 793 |
if (mapAvailable) |
| 794 |
numChannelsToMap = channelsPerFrame; |
| 795 |
|
| 796 |
std::string layoutStr; |
| 797 |
|
| 798 |
if (logMapping) |
| 799 |
{ |
| 800 |
CLog::Log(LOGDEBUG, "{} Engine requests layout {}", __FUNCTION__, ((std::string)channelMap)); |
| 801 |
|
| 802 |
if (mapAvailable) |
| 803 |
CLog::Log(LOGDEBUG, "{} trying to map to {} layout: {}", __FUNCTION__, |
| 804 |
channelsPerFrame == 2 ? "stereo" : "multichannel", |
| 805 |
calayout.ChannelLayoutToString(*layout, layoutStr)); |
| 806 |
else |
| 807 |
CLog::Log(LOGDEBUG, "{} no map available - using static multichannel map layout", |
| 808 |
__FUNCTION__); |
| 809 |
} |
| 810 |
|
| 811 |
channelMap.Reset();// start with an empty map |
| 812 |
|
| 813 |
for (unsigned int channel = 0; channel < numChannelsToMap; channel++) |
| 814 |
{ |
| 815 |
// we only try to map channels which are defined in the device layout |
| 816 |
enum AEChannel currentChannel; |
| 817 |
if (channel < numberChannelsInDeviceLayout) |
| 818 |
{ |
| 819 |
// get the channel from the fetched map |
| 820 |
if (mapAvailable) |
| 821 |
currentChannel = caChannelToAEChannel(layout->mChannelDescriptions[channel].mChannelLabel); |
| 822 |
else// get the channel from the default map |
| 823 |
currentChannel = CAChannelMap[channel]; |
| 824 |
|
| 825 |
} |
| 826 |
else// fill with unknown channels |
| 827 |
currentChannel = caChannelToAEChannel(kAudioChannelLabel_Unknown); |
| 828 |
|
| 829 |
if(!channelMap.HasChannel(currentChannel))// only add if not already added |
| 830 |
channelMap += currentChannel; |
| 831 |
} |
| 832 |
|
| 833 |
if (logMapping) |
| 834 |
CLog::Log(LOGDEBUG, "{} mapped channels to layout {}", __FUNCTION__, ((std::string)channelMap)); |
| 835 |
} |