Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <map> |
| 18 | #include <set> |
| 19 | |
| 20 | #define LOG_TAG "AHAL_AlsaUtils" |
| 21 | #include <Utils.h> |
| 22 | #include <aidl/android/media/audio/common/AudioFormatType.h> |
| 23 | #include <aidl/android/media/audio/common/PcmType.h> |
| 24 | #include <android-base/logging.h> |
| 25 | |
| 26 | #include "Utils.h" |
| 27 | #include "core-impl/utils.h" |
| 28 | |
| 29 | using aidl::android::hardware::audio::common::getChannelCount; |
| 30 | using aidl::android::media::audio::common::AudioChannelLayout; |
| 31 | using aidl::android::media::audio::common::AudioDeviceAddress; |
| 32 | using aidl::android::media::audio::common::AudioFormatDescription; |
| 33 | using aidl::android::media::audio::common::AudioFormatType; |
| 34 | using aidl::android::media::audio::common::AudioIoFlags; |
| 35 | using aidl::android::media::audio::common::AudioPortExt; |
| 36 | using aidl::android::media::audio::common::PcmType; |
| 37 | |
| 38 | namespace aidl::android::hardware::audio::core::alsa { |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | using AudioChannelCountToMaskMap = std::map<unsigned int, AudioChannelLayout>; |
| 43 | using AudioFormatDescToPcmFormatMap = std::map<AudioFormatDescription, enum pcm_format>; |
| 44 | using PcmFormatToAudioFormatDescMap = std::map<enum pcm_format, AudioFormatDescription>; |
| 45 | |
| 46 | AudioChannelLayout getInvalidChannelLayout() { |
| 47 | static const AudioChannelLayout invalidChannelLayout = |
| 48 | AudioChannelLayout::make<AudioChannelLayout::Tag::invalid>(0); |
| 49 | return invalidChannelLayout; |
| 50 | } |
| 51 | |
| 52 | static AudioChannelCountToMaskMap make_ChannelCountToMaskMap( |
| 53 | const std::set<AudioChannelLayout>& channelMasks) { |
| 54 | AudioChannelCountToMaskMap channelMaskToCountMap; |
| 55 | for (const auto& channelMask : channelMasks) { |
| 56 | channelMaskToCountMap.emplace(getChannelCount(channelMask), channelMask); |
| 57 | } |
| 58 | return channelMaskToCountMap; |
| 59 | } |
| 60 | |
| 61 | #define DEFINE_CHANNEL_LAYOUT_MASK(n) \ |
| 62 | AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(AudioChannelLayout::LAYOUT_##n) |
| 63 | |
| 64 | const AudioChannelCountToMaskMap& getSupportedChannelOutLayoutMap() { |
| 65 | static const std::set<AudioChannelLayout> supportedOutChannelLayouts = { |
| 66 | DEFINE_CHANNEL_LAYOUT_MASK(MONO), DEFINE_CHANNEL_LAYOUT_MASK(STEREO), |
| 67 | DEFINE_CHANNEL_LAYOUT_MASK(2POINT1), DEFINE_CHANNEL_LAYOUT_MASK(QUAD), |
| 68 | DEFINE_CHANNEL_LAYOUT_MASK(PENTA), DEFINE_CHANNEL_LAYOUT_MASK(5POINT1), |
| 69 | DEFINE_CHANNEL_LAYOUT_MASK(6POINT1), DEFINE_CHANNEL_LAYOUT_MASK(7POINT1), |
| 70 | DEFINE_CHANNEL_LAYOUT_MASK(7POINT1POINT4), DEFINE_CHANNEL_LAYOUT_MASK(22POINT2), |
| 71 | }; |
| 72 | static const AudioChannelCountToMaskMap outLayouts = |
| 73 | make_ChannelCountToMaskMap(supportedOutChannelLayouts); |
| 74 | return outLayouts; |
| 75 | } |
| 76 | |
| 77 | const AudioChannelCountToMaskMap& getSupportedChannelInLayoutMap() { |
| 78 | static const std::set<AudioChannelLayout> supportedInChannelLayouts = { |
| 79 | DEFINE_CHANNEL_LAYOUT_MASK(MONO), |
| 80 | DEFINE_CHANNEL_LAYOUT_MASK(STEREO), |
| 81 | }; |
| 82 | static const AudioChannelCountToMaskMap inLayouts = |
| 83 | make_ChannelCountToMaskMap(supportedInChannelLayouts); |
| 84 | return inLayouts; |
| 85 | } |
| 86 | |
| 87 | #undef DEFINE_CHANNEL_LAYOUT_MASK |
| 88 | #define DEFINE_CHANNEL_INDEX_MASK(n) \ |
| 89 | AudioChannelLayout::make<AudioChannelLayout::Tag::indexMask>(AudioChannelLayout::INDEX_MASK_##n) |
| 90 | |
| 91 | const AudioChannelCountToMaskMap& getSupportedChannelIndexLayoutMap() { |
| 92 | static const std::set<AudioChannelLayout> supportedIndexChannelLayouts = { |
| 93 | DEFINE_CHANNEL_INDEX_MASK(1), DEFINE_CHANNEL_INDEX_MASK(2), |
| 94 | DEFINE_CHANNEL_INDEX_MASK(3), DEFINE_CHANNEL_INDEX_MASK(4), |
| 95 | DEFINE_CHANNEL_INDEX_MASK(5), DEFINE_CHANNEL_INDEX_MASK(6), |
| 96 | DEFINE_CHANNEL_INDEX_MASK(7), DEFINE_CHANNEL_INDEX_MASK(8), |
| 97 | DEFINE_CHANNEL_INDEX_MASK(9), DEFINE_CHANNEL_INDEX_MASK(10), |
| 98 | DEFINE_CHANNEL_INDEX_MASK(11), DEFINE_CHANNEL_INDEX_MASK(12), |
| 99 | DEFINE_CHANNEL_INDEX_MASK(13), DEFINE_CHANNEL_INDEX_MASK(14), |
| 100 | DEFINE_CHANNEL_INDEX_MASK(15), DEFINE_CHANNEL_INDEX_MASK(16), |
| 101 | DEFINE_CHANNEL_INDEX_MASK(17), DEFINE_CHANNEL_INDEX_MASK(18), |
| 102 | DEFINE_CHANNEL_INDEX_MASK(19), DEFINE_CHANNEL_INDEX_MASK(20), |
| 103 | DEFINE_CHANNEL_INDEX_MASK(21), DEFINE_CHANNEL_INDEX_MASK(22), |
| 104 | DEFINE_CHANNEL_INDEX_MASK(23), DEFINE_CHANNEL_INDEX_MASK(24), |
| 105 | }; |
| 106 | static const AudioChannelCountToMaskMap indexLayouts = |
| 107 | make_ChannelCountToMaskMap(supportedIndexChannelLayouts); |
| 108 | return indexLayouts; |
| 109 | } |
| 110 | |
| 111 | #undef DEFINE_CHANNEL_INDEX_MASK |
| 112 | |
| 113 | AudioFormatDescription make_AudioFormatDescription(AudioFormatType type) { |
| 114 | AudioFormatDescription result; |
| 115 | result.type = type; |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | AudioFormatDescription make_AudioFormatDescription(PcmType pcm) { |
| 120 | auto result = make_AudioFormatDescription(AudioFormatType::PCM); |
| 121 | result.pcm = pcm; |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | const AudioFormatDescToPcmFormatMap& getAudioFormatDescriptorToPcmFormatMap() { |
| 126 | static const AudioFormatDescToPcmFormatMap formatDescToPcmFormatMap = { |
| 127 | {make_AudioFormatDescription(PcmType::UINT_8_BIT), PCM_FORMAT_S8}, |
| 128 | {make_AudioFormatDescription(PcmType::INT_16_BIT), PCM_FORMAT_S16_LE}, |
| 129 | {make_AudioFormatDescription(PcmType::FIXED_Q_8_24), PCM_FORMAT_S24_LE}, |
| 130 | {make_AudioFormatDescription(PcmType::INT_24_BIT), PCM_FORMAT_S24_3LE}, |
| 131 | {make_AudioFormatDescription(PcmType::INT_32_BIT), PCM_FORMAT_S32_LE}, |
| 132 | {make_AudioFormatDescription(PcmType::FLOAT_32_BIT), PCM_FORMAT_FLOAT_LE}, |
| 133 | }; |
| 134 | return formatDescToPcmFormatMap; |
| 135 | } |
| 136 | |
| 137 | static PcmFormatToAudioFormatDescMap make_PcmFormatToAudioFormatDescMap( |
| 138 | const AudioFormatDescToPcmFormatMap& formatDescToPcmFormatMap) { |
| 139 | PcmFormatToAudioFormatDescMap result; |
| 140 | for (const auto& formatPair : formatDescToPcmFormatMap) { |
| 141 | result.emplace(formatPair.second, formatPair.first); |
| 142 | } |
| 143 | return result; |
| 144 | } |
| 145 | |
| 146 | const PcmFormatToAudioFormatDescMap& getPcmFormatToAudioFormatDescMap() { |
| 147 | static const PcmFormatToAudioFormatDescMap pcmFormatToFormatDescMap = |
| 148 | make_PcmFormatToAudioFormatDescMap(getAudioFormatDescriptorToPcmFormatMap()); |
| 149 | return pcmFormatToFormatDescMap; |
| 150 | } |
| 151 | |
| 152 | } // namespace |
| 153 | |
| 154 | std::ostream& operator<<(std::ostream& os, const DeviceProfile& device) { |
| 155 | return os << "<" << device.card << "," << device.device << ">"; |
| 156 | } |
| 157 | |
| 158 | AudioChannelLayout getChannelLayoutMaskFromChannelCount(unsigned int channelCount, int isInput) { |
| 159 | return findValueOrDefault( |
| 160 | isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(), |
| 161 | channelCount, getInvalidChannelLayout()); |
| 162 | } |
| 163 | |
| 164 | AudioChannelLayout getChannelIndexMaskFromChannelCount(unsigned int channelCount) { |
| 165 | return findValueOrDefault(getSupportedChannelIndexLayoutMap(), channelCount, |
| 166 | getInvalidChannelLayout()); |
| 167 | } |
| 168 | |
| 169 | unsigned int getChannelCountFromChannelMask(const AudioChannelLayout& channelMask, bool isInput) { |
| 170 | switch (channelMask.getTag()) { |
| 171 | case AudioChannelLayout::Tag::layoutMask: { |
| 172 | return findKeyOrDefault( |
| 173 | isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(), |
| 174 | static_cast<unsigned>(getChannelCount(channelMask)), 0u /*defaultValue*/); |
| 175 | } |
| 176 | case AudioChannelLayout::Tag::indexMask: { |
| 177 | return findKeyOrDefault(getSupportedChannelIndexLayoutMap(), |
| 178 | static_cast<unsigned>(getChannelCount(channelMask)), |
| 179 | 0u /*defaultValue*/); |
| 180 | } |
| 181 | case AudioChannelLayout::Tag::none: |
| 182 | case AudioChannelLayout::Tag::invalid: |
| 183 | case AudioChannelLayout::Tag::voiceMask: |
| 184 | default: |
| 185 | return 0; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | std::vector<AudioChannelLayout> getChannelMasksFromProfile(const alsa_device_profile* profile) { |
| 190 | const bool isInput = profile->direction == PCM_IN; |
| 191 | std::vector<AudioChannelLayout> channels; |
| 192 | for (size_t i = 0; i < AUDIO_PORT_MAX_CHANNEL_MASKS && profile->channel_counts[i] != 0; ++i) { |
| 193 | auto layoutMask = |
| 194 | alsa::getChannelLayoutMaskFromChannelCount(profile->channel_counts[i], isInput); |
| 195 | if (layoutMask.getTag() == AudioChannelLayout::Tag::layoutMask) { |
| 196 | channels.push_back(layoutMask); |
| 197 | } |
| 198 | auto indexMask = alsa::getChannelIndexMaskFromChannelCount(profile->channel_counts[i]); |
| 199 | if (indexMask.getTag() == AudioChannelLayout::Tag::indexMask) { |
| 200 | channels.push_back(indexMask); |
| 201 | } |
| 202 | } |
| 203 | return channels; |
| 204 | } |
| 205 | |
| 206 | std::optional<DeviceProfile> getDeviceProfile( |
| 207 | const ::aidl::android::media::audio::common::AudioDevice& audioDevice, bool isInput) { |
| 208 | if (audioDevice.address.getTag() != AudioDeviceAddress::Tag::alsa) { |
| 209 | LOG(ERROR) << __func__ << ": not alsa address: " << audioDevice.toString(); |
| 210 | return std::nullopt; |
| 211 | } |
| 212 | auto& alsaAddress = audioDevice.address.get<AudioDeviceAddress::Tag::alsa>(); |
| 213 | if (alsaAddress.size() != 2 || alsaAddress[0] < 0 || alsaAddress[1] < 0) { |
| 214 | LOG(ERROR) << __func__ |
| 215 | << ": malformed alsa address: " << ::android::internal::ToString(alsaAddress); |
| 216 | return std::nullopt; |
| 217 | } |
| 218 | return DeviceProfile{.card = alsaAddress[0], |
| 219 | .device = alsaAddress[1], |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 220 | .direction = isInput ? PCM_IN : PCM_OUT, |
| 221 | .isExternal = !audioDevice.type.connection.empty()}; |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | std::optional<DeviceProfile> getDeviceProfile( |
| 225 | const ::aidl::android::media::audio::common::AudioPort& audioPort) { |
| 226 | if (audioPort.ext.getTag() != AudioPortExt::Tag::device) { |
| 227 | LOG(ERROR) << __func__ << ": port id " << audioPort.id << " is not a device port"; |
| 228 | return std::nullopt; |
| 229 | } |
| 230 | auto& devicePort = audioPort.ext.get<AudioPortExt::Tag::device>(); |
| 231 | return getDeviceProfile(devicePort.device, audioPort.flags.getTag() == AudioIoFlags::input); |
| 232 | } |
| 233 | |
| 234 | std::optional<struct pcm_config> getPcmConfig(const StreamContext& context, bool isInput) { |
| 235 | struct pcm_config config; |
| 236 | config.channels = alsa::getChannelCountFromChannelMask(context.getChannelLayout(), isInput); |
| 237 | if (config.channels == 0) { |
| 238 | LOG(ERROR) << __func__ << ": invalid channel=" << context.getChannelLayout().toString(); |
| 239 | return std::nullopt; |
| 240 | } |
| 241 | config.format = alsa::aidl2c_AudioFormatDescription_pcm_format(context.getFormat()); |
| 242 | if (config.format == PCM_FORMAT_INVALID) { |
| 243 | LOG(ERROR) << __func__ << ": invalid format=" << context.getFormat().toString(); |
| 244 | return std::nullopt; |
| 245 | } |
| 246 | config.rate = context.getSampleRate(); |
| 247 | if (config.rate == 0) { |
| 248 | LOG(ERROR) << __func__ << ": invalid sample rate=" << config.rate; |
| 249 | return std::nullopt; |
| 250 | } |
| 251 | return config; |
| 252 | } |
| 253 | |
| 254 | std::vector<int> getSampleRatesFromProfile(const alsa_device_profile* profile) { |
| 255 | std::vector<int> sampleRates; |
| 256 | for (int i = 0; i < std::min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) && |
| 257 | profile->sample_rates[i] != 0; |
| 258 | i++) { |
| 259 | sampleRates.push_back(profile->sample_rates[i]); |
| 260 | } |
| 261 | return sampleRates; |
| 262 | } |
| 263 | |
| 264 | DeviceProxy makeDeviceProxy() { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame^] | 265 | DeviceProxy proxy(new alsa_device_proxy, [](alsa_device_proxy* proxy) { |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 266 | if (proxy != nullptr) { |
| 267 | proxy_close(proxy); |
| 268 | delete proxy; |
| 269 | } |
| 270 | }); |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame^] | 271 | memset(proxy.get(), 0, sizeof(alsa_device_proxy)); |
| 272 | return proxy; |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 275 | DeviceProxy openProxyForAttachedDevice(const DeviceProfile& deviceProfile, |
| 276 | struct pcm_config* pcmConfig, size_t bufferFrameCount) { |
| 277 | if (deviceProfile.isExternal) { |
| 278 | LOG(FATAL) << __func__ << ": called for an external device, address=" << deviceProfile; |
| 279 | } |
| 280 | alsa_device_profile profile; |
| 281 | profile_init(&profile, deviceProfile.direction); |
| 282 | profile.card = deviceProfile.card; |
| 283 | profile.device = deviceProfile.device; |
| 284 | if (!profile_fill_builtin_device_info(&profile, pcmConfig, bufferFrameCount)) { |
| 285 | LOG(FATAL) << __func__ << ": failed to init for built-in device, address=" << deviceProfile; |
| 286 | } |
| 287 | auto proxy = makeDeviceProxy(); |
| 288 | if (int err = proxy_prepare_from_default_config(proxy.get(), &profile); err != 0) { |
| 289 | LOG(FATAL) << __func__ << ": fail to prepare for device address=" << deviceProfile |
| 290 | << " error=" << err; |
| 291 | return nullptr; |
| 292 | } |
| 293 | if (int err = proxy_open(proxy.get()); err != 0) { |
| 294 | LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile |
| 295 | << " error=" << err; |
| 296 | return nullptr; |
| 297 | } |
| 298 | return proxy; |
| 299 | } |
| 300 | |
| 301 | DeviceProxy openProxyForExternalDevice(const DeviceProfile& deviceProfile, |
| 302 | struct pcm_config* pcmConfig, bool requireExactMatch) { |
| 303 | if (!deviceProfile.isExternal) { |
| 304 | LOG(FATAL) << __func__ << ": called for an attached device, address=" << deviceProfile; |
| 305 | } |
| 306 | auto profile = readAlsaDeviceInfo(deviceProfile); |
| 307 | if (!profile.has_value()) { |
| 308 | LOG(ERROR) << __func__ << ": unable to read device info, device address=" << deviceProfile; |
| 309 | return nullptr; |
| 310 | } |
| 311 | auto proxy = makeDeviceProxy(); |
| 312 | if (int err = proxy_prepare(proxy.get(), &profile.value(), pcmConfig, requireExactMatch); |
| 313 | err != 0) { |
| 314 | LOG(ERROR) << __func__ << ": fail to prepare for device address=" << deviceProfile |
| 315 | << " error=" << err; |
| 316 | return nullptr; |
| 317 | } |
| 318 | if (int err = proxy_open(proxy.get()); err != 0) { |
| 319 | LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile |
| 320 | << " error=" << err; |
| 321 | return nullptr; |
| 322 | } |
| 323 | return proxy; |
| 324 | } |
| 325 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 326 | std::optional<alsa_device_profile> readAlsaDeviceInfo(const DeviceProfile& deviceProfile) { |
| 327 | alsa_device_profile profile; |
| 328 | profile_init(&profile, deviceProfile.direction); |
| 329 | profile.card = deviceProfile.card; |
| 330 | profile.device = deviceProfile.device; |
| 331 | if (!profile_read_device_info(&profile)) { |
| 332 | LOG(ERROR) << __func__ << ": failed to read device info, card=" << profile.card |
| 333 | << ", device=" << profile.device; |
| 334 | return std::nullopt; |
| 335 | } |
| 336 | return profile; |
| 337 | } |
| 338 | |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame^] | 339 | void resetTransferredFrames(DeviceProxy& proxy, uint64_t frames) { |
| 340 | if (proxy != nullptr) { |
| 341 | proxy->transferred = frames; |
| 342 | } |
| 343 | } |
| 344 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 345 | AudioFormatDescription c2aidl_pcm_format_AudioFormatDescription(enum pcm_format legacy) { |
| 346 | return findValueOrDefault(getPcmFormatToAudioFormatDescMap(), legacy, AudioFormatDescription()); |
| 347 | } |
| 348 | |
| 349 | pcm_format aidl2c_AudioFormatDescription_pcm_format(const AudioFormatDescription& aidl) { |
| 350 | return findValueOrDefault(getAudioFormatDescriptorToPcmFormatMap(), aidl, PCM_FORMAT_INVALID); |
| 351 | } |
| 352 | |
| 353 | } // namespace aidl::android::hardware::audio::core::alsa |