| 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 |
#pragma once |
| 10 |
|
| 11 |
#include "cores/AudioEngine/Sinks/osx/CoreAudioDevice.h" |
| 12 |
#include "cores/AudioEngine/Utils/AEAudioFormat.h" |
| 13 |
#include "cores/AudioEngine/Utils/AEDeviceInfo.h" |
| 14 |
|
| 15 |
#include <string> |
| 16 |
#include <utility> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
struct CADeviceInstance |
| 20 |
{ |
| 21 |
AudioDeviceID audioDeviceId; |
| 22 |
unsigned int streamIndex; |
| 23 |
unsigned int sourceId; |
| 24 |
}; |
| 25 |
typedef std::vector< std::pair<struct CADeviceInstance, CAEDeviceInfo> > CADeviceList; |
| 26 |
|
| 27 |
//Hierarchy: |
| 28 |
// Device |
| 29 |
// - 1..n streams |
| 30 |
// 1..n formats |
| 31 |
// - 0..n sources |
| 32 |
//on non planar devices we have numstreams * numsources devices for our list |
| 33 |
//on planar devices we have 1 * numsources devices for our list |
| 34 |
class AEDeviceEnumerationOSX |
| 35 |
{ |
| 36 |
public: |
| 37 |
/*! |
| 38 |
* @brief C'tor - initialises the Enumerator and calls Enumerate |
| 39 |
* @param deviceID - the CoreAudio Device ID which will be the base of the enumerated device list |
| 40 |
*/ |
| 41 |
AEDeviceEnumerationOSX(AudioDeviceID deviceID); |
| 42 |
// d'tor |
| 43 |
~AEDeviceEnumerationOSX() = default; |
| 44 |
|
| 45 |
/*! |
| 46 |
* @brief Gets the device list which was enumerated by the last call to Enumerate |
| 47 |
* (which is also called in c'tor). |
| 48 |
* |
| 49 |
* @return Returns the device list. |
| 50 |
*/ |
| 51 |
CADeviceList GetDeviceInfoList() const; |
| 52 |
|
| 53 |
/*! |
| 54 |
* @brief Fetches all metadata from the CoreAudio device which is needed to generate a proper DeviceList for AE |
| 55 |
* This method is always called from C'tor but can be called multiple times if the streams of a device |
| 56 |
* changed. This fills m_caStreamInfos. |
| 57 |
* After this call - GetDeviceInfoList will reflect the Enumerated metadata. |
| 58 |
* @return false when streamlist couldn't be fetched from device - else true |
| 59 |
*/ |
| 60 |
bool Enumerate(); |
| 61 |
|
| 62 |
/*! |
| 63 |
* @brief Returns the number of Planes for a device. This will be 1 for non-planar devices and > 1 for planar devices. |
| 64 |
* @return Number of planes for this device. |
| 65 |
*/ |
| 66 |
unsigned int GetNumPlanes() const; |
| 67 |
|
| 68 |
/*! |
| 69 |
* @brief Checks if the m_deviceID belongs to a planar or non-planar device. |
| 70 |
* @return true if m_deviceID belongs to a planar device - else false. |
| 71 |
*/ |
| 72 |
bool IsPlanar() const { return m_isPlanar; } |
| 73 |
|
| 74 |
/*! |
| 75 |
* @brief Tries to find a suitable CoreAudio format which matches the given AEAudioFormat as close as possible. |
| 76 |
* @param streamIdx [in/out] - if streamIdx != INT_MAX only formats of the given streamIdx are checked |
| 77 |
* if streamIdx == INT_MAX - formats of all streams in the device are considered |
| 78 |
* On success this parameter returns the selected streamIdx. |
| 79 |
* |
| 80 |
* @param format [in] - the requested AE format which should be matched to the stream formats of CA |
| 81 |
* @param outputFormat [out] - the found CA format which matches best to the requested AE format |
| 82 |
* @param outputStream [out] - the coreaudio streamid which contains the coreaudio format returned in outputFormat |
| 83 |
* @return true if a matching corea audio format was found - else false |
| 84 |
*/ |
| 85 |
bool FindSuitableFormatForStream(UInt32 &streamIdx, const AEAudioFormat &format, bool virt, |
| 86 |
AudioStreamBasicDescription &outputFormat, |
| 87 |
AudioStreamID &outputStream) const; |
| 88 |
|
| 89 |
/*! |
| 90 |
* @brief Returns the device name which belongs to m_deviceID without any stream/source suffixes |
| 91 |
* @return the CA device name |
| 92 |
*/ |
| 93 |
std::string GetMasterDeviceName() const { return m_deviceName; } |
| 94 |
|
| 95 |
/*! |
| 96 |
* @brief Tries to return a proper channelmap from CA in a format AE understands |
| 97 |
* @param channelMap [in/out] - returns the found channelmap in AE format |
| 98 |
* if initialised with a map the number of channels is used to determine |
| 99 |
* if stereo or multichannel map should be fetched |
| 100 |
* @param channelsPerFrame [int] - the number of channels which should be mapped |
| 101 |
* (also decides if stereo or multichannel map is fetched similar to channelMap param) |
| 102 |
*/ |
| 103 |
void GetAEChannelMap(CAEChannelInfo &channelMap, unsigned int channelsPerFrame) const; |
| 104 |
|
| 105 |
/*! |
| 106 |
* @brief Scores a format based on: |
| 107 |
* 1. Matching passthrough characteristics (i.e. passthrough flag) |
| 108 |
* 2. Matching sample rate. |
| 109 |
* 3. Matching bits per channel (or higher). |
| 110 |
* 4. Matching number of channels (or higher). |
| 111 |
* |
| 112 |
* @param formatDesc [in] - The CA FormatDescription which should be scored |
| 113 |
* @param format [in] - the AE format which should be matched as good as possible |
| 114 |
* @return - the score of formatDesc - higher scores indicate better matching to "format" |
| 115 |
* (scores > 10000 indicate passthrough formats) |
| 116 |
*/ |
| 117 |
// public because its used in unit tests ... |
| 118 |
float ScoreFormat(const AudioStreamBasicDescription &formatDesc, const AEAudioFormat &format) const; |
| 119 |
|
| 120 |
private: |
| 121 |
|
| 122 |
/*! |
| 123 |
* @brief Checks if this is a digital device based on CA transportType or name |
| 124 |
* @return - true if this is a digital device - else false. |
| 125 |
*/ |
| 126 |
bool isDigitalDevice() const; |
| 127 |
|
| 128 |
/*! |
| 129 |
* @brief Checks if there are passthrough formats or digital formats |
| 130 |
* (the latter are passthrough formats with dedicated format config like AC3/DTS) |
| 131 |
* @param formatList [in] - the format list to be evaluated |
| 132 |
* @param hasPassthroughFormats [out] - true if there were passthrough formats in the list |
| 133 |
* @param hasDigitalFormat [out] - true if there were dedicated passthrough formats in the list |
| 134 |
*/ |
| 135 |
void hasPassthroughOrDigitalFormats(const StreamFormatList &formatList, |
| 136 |
bool &hasPassthroughFormats, |
| 137 |
bool &hasDigitalFormat) const; |
| 138 |
|
| 139 |
/*! |
| 140 |
* @brief Gets the AE devicetype for this device based the given criteria |
| 141 |
* @param hasPassthroughFormats [in] - flag indicating that the device has passthrough formats |
| 142 |
* @param isDigital [in] - flag indicating that the device is digital |
| 143 |
* @param numChannels [in] - the number of channels of the device |
| 144 |
* @param transportType [in] - the transportType of the device |
| 145 |
* @return the AE devicetype |
| 146 |
*/ |
| 147 |
enum AEDeviceType getDeviceType(bool hasPassthroughFormats, bool isDigital, |
| 148 |
UInt32 numChannels, UInt32 transportType) const; |
| 149 |
|
| 150 |
/*! |
| 151 |
* @brief Fetches all ca streams from the ca device and fills m_channelsPerStream and m_streams |
| 152 |
*/ |
| 153 |
void fillStreamList(); |
| 154 |
|
| 155 |
/*! |
| 156 |
* @brief Scores a samplerate based on: |
| 157 |
* 1. Prefer exact match |
| 158 |
* 2. Prefer exact multiple of source samplerate and prefer the lowest |
| 159 |
* |
| 160 |
* @param destinationRate [in] - the destination samplerate to score |
| 161 |
* @param sourceRate [in] - the sourceRate of the audio format - this is the samplerate the score is based on |
| 162 |
* @return the score |
| 163 |
*/ |
| 164 |
float scoreSampleRate(Float64 destinationRate, unsigned int sourceRate) const; |
| 165 |
|
| 166 |
bool hasSampleRate(const AESampleRateList &list, const unsigned int samplerate) const; |
| 167 |
bool hasDataFormat(const AEDataFormatList &list, const enum AEDataFormat format) const; |
| 168 |
bool hasDataType(const AEDataTypeList &list, CAEStreamInfo::DataType type) const; |
| 169 |
|
| 170 |
/*! |
| 171 |
* @brief Converts a CA format description to a list of AEFormat descriptions (as one format can result |
| 172 |
* in more then 1 AE format - e.x. AC3 ca format results in AC3 and DTS AE format |
| 173 |
* |
| 174 |
* @param formatDesc [in] - The CA format description to be converted |
| 175 |
* @param isDigital [in] - Flag indicating if the parent stream of formatDesc is digital |
| 176 |
* (for allowing bitstreaming without dedicated AC3 format in CA) |
| 177 |
* @return The list of converted AE formats. |
| 178 |
*/ |
| 179 |
AEDataFormatList caFormatToAE(const AudioStreamBasicDescription &formatDesc, bool isDigital) const; |
| 180 |
AEDataTypeList caFormatToAEType(const AudioStreamBasicDescription &formatDesc, bool isDigital) const; |
| 181 |
|
| 182 |
|
| 183 |
/*! |
| 184 |
* @brief Convert a CA channel label to an AE channel. |
| 185 |
* @param CAChannelLabel - the CA channel label to be converted |
| 186 |
* @return the corresponding AEChannel |
| 187 |
*/ |
| 188 |
enum AEChannel caChannelToAEChannel(const AudioChannelLabel &CAChannelLabel) const; |
| 189 |
|
| 190 |
// for filling out the AEDeviceInfo object based on |
| 191 |
// the data gathered on the last call to Enumerate() |
| 192 |
/*! |
| 193 |
* @brief Returns all AE formats for the CA stream at the given index |
| 194 |
* @param streamIdx [in] - index into m_caStreamInfos |
| 195 |
* @return - the list of AE formats in that stream. |
| 196 |
*/ |
| 197 |
AEDataFormatList getFormatListForStream(UInt32 streamIdx) const; |
| 198 |
|
| 199 |
AEDataTypeList getTypeListForStream(UInt32 streamIdx) const; |
| 200 |
|
| 201 |
/*! |
| 202 |
* @brief Returns the AE channelinfo/channel map for the CA stream at the given index |
| 203 |
* @param streamIdx [in] - index into m_caStreamInfos |
| 204 |
* @return - the of AE channel info for that stream. |
| 205 |
*/ |
| 206 |
CAEChannelInfo getChannelInfoForStream(UInt32 streamIdx) const; |
| 207 |
|
| 208 |
/*! |
| 209 |
* @brief Returns the AE samplerates for the CA stream at the given index |
| 210 |
* @param streamIdx [in] - index into m_caStreamInfos |
| 211 |
* @return - the list of AE samplerates for that stream. |
| 212 |
*/ |
| 213 |
AESampleRateList getSampleRateListForStream(UInt32 streamIdx) const; |
| 214 |
|
| 215 |
/*! |
| 216 |
* @brief Returns the AE device name for the CA stream at the given index |
| 217 |
* @param streamIdx [in] - index into m_caStreamInfos |
| 218 |
* @return - The devicename for that stream |
| 219 |
*/ |
| 220 |
std::string getDeviceNameForStream(UInt32 streamIdx) const; |
| 221 |
|
| 222 |
/*! |
| 223 |
* @brief - Returns the extra displayname shown to the User in addition to getDisplayNameForStream |
| 224 |
* for the CA stream at the given index |
| 225 |
* @param streamIdx [in] - index into m_caStreamInfos |
| 226 |
* @return - The extra displayname for that stream (might be empty) |
| 227 |
*/ |
| 228 |
std::string getExtraDisplayNameForStream(UInt32 streamIdx) const; |
| 229 |
|
| 230 |
AudioDeviceID m_deviceID; |
| 231 |
bool m_isPlanar = false; |
| 232 |
std::string m_deviceName; |
| 233 |
|
| 234 |
typedef struct |
| 235 |
{ |
| 236 |
AudioStreamID streamID; |
| 237 |
StreamFormatList formatList; |
| 238 |
StreamFormatList formatListVirt; |
| 239 |
UInt32 numChannels; |
| 240 |
bool isDigital; |
| 241 |
bool hasPassthroughFormats; |
| 242 |
enum AEDeviceType deviceType; |
| 243 |
} caStreamInfo; |
| 244 |
std::vector<caStreamInfo> m_caStreamInfos; |
| 245 |
CCoreAudioDevice m_caDevice; |
| 246 |
}; |