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> |
Weilin Xu | 122597a | 2024-07-18 11:43:48 -0700 | [diff] [blame] | 25 | #include <audio_utils/primitives.h> |
| 26 | #include <cutils/compiler.h> |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 27 | |
| 28 | #include "Utils.h" |
| 29 | #include "core-impl/utils.h" |
| 30 | |
| 31 | using aidl::android::hardware::audio::common::getChannelCount; |
| 32 | using aidl::android::media::audio::common::AudioChannelLayout; |
| 33 | using aidl::android::media::audio::common::AudioDeviceAddress; |
| 34 | using aidl::android::media::audio::common::AudioFormatDescription; |
| 35 | using aidl::android::media::audio::common::AudioFormatType; |
| 36 | using aidl::android::media::audio::common::AudioIoFlags; |
| 37 | using aidl::android::media::audio::common::AudioPortExt; |
| 38 | using aidl::android::media::audio::common::PcmType; |
| 39 | |
| 40 | namespace aidl::android::hardware::audio::core::alsa { |
| 41 | |
Weilin Xu | 550a244 | 2024-09-19 20:33:36 -0700 | [diff] [blame^] | 42 | const float kUnityGainFloat = 1.0f; |
| 43 | |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 44 | DeviceProxy::DeviceProxy() : mProfile(nullptr), mProxy(nullptr, alsaProxyDeleter) {} |
| 45 | |
| 46 | DeviceProxy::DeviceProxy(const DeviceProfile& deviceProfile) |
| 47 | : mProfile(new alsa_device_profile), mProxy(new alsa_device_proxy, alsaProxyDeleter) { |
| 48 | profile_init(mProfile.get(), deviceProfile.direction); |
| 49 | mProfile->card = deviceProfile.card; |
| 50 | mProfile->device = deviceProfile.device; |
| 51 | memset(mProxy.get(), 0, sizeof(alsa_device_proxy)); |
| 52 | } |
| 53 | |
| 54 | void DeviceProxy::alsaProxyDeleter(alsa_device_proxy* proxy) { |
| 55 | if (proxy != nullptr) { |
| 56 | proxy_close(proxy); |
| 57 | delete proxy; |
| 58 | } |
| 59 | } |
| 60 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | using AudioChannelCountToMaskMap = std::map<unsigned int, AudioChannelLayout>; |
| 64 | using AudioFormatDescToPcmFormatMap = std::map<AudioFormatDescription, enum pcm_format>; |
| 65 | using PcmFormatToAudioFormatDescMap = std::map<enum pcm_format, AudioFormatDescription>; |
| 66 | |
| 67 | AudioChannelLayout getInvalidChannelLayout() { |
| 68 | static const AudioChannelLayout invalidChannelLayout = |
| 69 | AudioChannelLayout::make<AudioChannelLayout::Tag::invalid>(0); |
| 70 | return invalidChannelLayout; |
| 71 | } |
| 72 | |
| 73 | static AudioChannelCountToMaskMap make_ChannelCountToMaskMap( |
| 74 | const std::set<AudioChannelLayout>& channelMasks) { |
| 75 | AudioChannelCountToMaskMap channelMaskToCountMap; |
| 76 | for (const auto& channelMask : channelMasks) { |
| 77 | channelMaskToCountMap.emplace(getChannelCount(channelMask), channelMask); |
| 78 | } |
| 79 | return channelMaskToCountMap; |
| 80 | } |
| 81 | |
| 82 | #define DEFINE_CHANNEL_LAYOUT_MASK(n) \ |
| 83 | AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(AudioChannelLayout::LAYOUT_##n) |
| 84 | |
| 85 | const AudioChannelCountToMaskMap& getSupportedChannelOutLayoutMap() { |
| 86 | static const std::set<AudioChannelLayout> supportedOutChannelLayouts = { |
Mikhail Naganov | 8c27e46 | 2024-07-18 17:03:53 -0700 | [diff] [blame] | 87 | DEFINE_CHANNEL_LAYOUT_MASK(MONO), |
| 88 | DEFINE_CHANNEL_LAYOUT_MASK(STEREO), |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 89 | }; |
| 90 | static const AudioChannelCountToMaskMap outLayouts = |
| 91 | make_ChannelCountToMaskMap(supportedOutChannelLayouts); |
| 92 | return outLayouts; |
| 93 | } |
| 94 | |
| 95 | const AudioChannelCountToMaskMap& getSupportedChannelInLayoutMap() { |
| 96 | static const std::set<AudioChannelLayout> supportedInChannelLayouts = { |
| 97 | DEFINE_CHANNEL_LAYOUT_MASK(MONO), |
| 98 | DEFINE_CHANNEL_LAYOUT_MASK(STEREO), |
| 99 | }; |
| 100 | static const AudioChannelCountToMaskMap inLayouts = |
| 101 | make_ChannelCountToMaskMap(supportedInChannelLayouts); |
| 102 | return inLayouts; |
| 103 | } |
| 104 | |
| 105 | #undef DEFINE_CHANNEL_LAYOUT_MASK |
| 106 | #define DEFINE_CHANNEL_INDEX_MASK(n) \ |
| 107 | AudioChannelLayout::make<AudioChannelLayout::Tag::indexMask>(AudioChannelLayout::INDEX_MASK_##n) |
| 108 | |
| 109 | const AudioChannelCountToMaskMap& getSupportedChannelIndexLayoutMap() { |
| 110 | static const std::set<AudioChannelLayout> supportedIndexChannelLayouts = { |
| 111 | DEFINE_CHANNEL_INDEX_MASK(1), DEFINE_CHANNEL_INDEX_MASK(2), |
| 112 | DEFINE_CHANNEL_INDEX_MASK(3), DEFINE_CHANNEL_INDEX_MASK(4), |
| 113 | DEFINE_CHANNEL_INDEX_MASK(5), DEFINE_CHANNEL_INDEX_MASK(6), |
| 114 | DEFINE_CHANNEL_INDEX_MASK(7), DEFINE_CHANNEL_INDEX_MASK(8), |
| 115 | DEFINE_CHANNEL_INDEX_MASK(9), DEFINE_CHANNEL_INDEX_MASK(10), |
| 116 | DEFINE_CHANNEL_INDEX_MASK(11), DEFINE_CHANNEL_INDEX_MASK(12), |
| 117 | DEFINE_CHANNEL_INDEX_MASK(13), DEFINE_CHANNEL_INDEX_MASK(14), |
| 118 | DEFINE_CHANNEL_INDEX_MASK(15), DEFINE_CHANNEL_INDEX_MASK(16), |
| 119 | DEFINE_CHANNEL_INDEX_MASK(17), DEFINE_CHANNEL_INDEX_MASK(18), |
| 120 | DEFINE_CHANNEL_INDEX_MASK(19), DEFINE_CHANNEL_INDEX_MASK(20), |
| 121 | DEFINE_CHANNEL_INDEX_MASK(21), DEFINE_CHANNEL_INDEX_MASK(22), |
| 122 | DEFINE_CHANNEL_INDEX_MASK(23), DEFINE_CHANNEL_INDEX_MASK(24), |
| 123 | }; |
| 124 | static const AudioChannelCountToMaskMap indexLayouts = |
| 125 | make_ChannelCountToMaskMap(supportedIndexChannelLayouts); |
| 126 | return indexLayouts; |
| 127 | } |
| 128 | |
| 129 | #undef DEFINE_CHANNEL_INDEX_MASK |
| 130 | |
| 131 | AudioFormatDescription make_AudioFormatDescription(AudioFormatType type) { |
| 132 | AudioFormatDescription result; |
| 133 | result.type = type; |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | AudioFormatDescription make_AudioFormatDescription(PcmType pcm) { |
| 138 | auto result = make_AudioFormatDescription(AudioFormatType::PCM); |
| 139 | result.pcm = pcm; |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | const AudioFormatDescToPcmFormatMap& getAudioFormatDescriptorToPcmFormatMap() { |
| 144 | static const AudioFormatDescToPcmFormatMap formatDescToPcmFormatMap = { |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 145 | {make_AudioFormatDescription(PcmType::INT_16_BIT), PCM_FORMAT_S16_LE}, |
| 146 | {make_AudioFormatDescription(PcmType::FIXED_Q_8_24), PCM_FORMAT_S24_LE}, |
| 147 | {make_AudioFormatDescription(PcmType::INT_24_BIT), PCM_FORMAT_S24_3LE}, |
| 148 | {make_AudioFormatDescription(PcmType::INT_32_BIT), PCM_FORMAT_S32_LE}, |
| 149 | {make_AudioFormatDescription(PcmType::FLOAT_32_BIT), PCM_FORMAT_FLOAT_LE}, |
| 150 | }; |
| 151 | return formatDescToPcmFormatMap; |
| 152 | } |
| 153 | |
| 154 | static PcmFormatToAudioFormatDescMap make_PcmFormatToAudioFormatDescMap( |
| 155 | const AudioFormatDescToPcmFormatMap& formatDescToPcmFormatMap) { |
| 156 | PcmFormatToAudioFormatDescMap result; |
| 157 | for (const auto& formatPair : formatDescToPcmFormatMap) { |
| 158 | result.emplace(formatPair.second, formatPair.first); |
| 159 | } |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | const PcmFormatToAudioFormatDescMap& getPcmFormatToAudioFormatDescMap() { |
| 164 | static const PcmFormatToAudioFormatDescMap pcmFormatToFormatDescMap = |
| 165 | make_PcmFormatToAudioFormatDescMap(getAudioFormatDescriptorToPcmFormatMap()); |
| 166 | return pcmFormatToFormatDescMap; |
| 167 | } |
| 168 | |
Weilin Xu | 550a244 | 2024-09-19 20:33:36 -0700 | [diff] [blame^] | 169 | void applyGainToInt16Buffer(void* buffer, const size_t bufferSizeBytes, const float gain, |
| 170 | int channelCount) { |
| 171 | const uint16_t unityGainQ4_12 = u4_12_from_float(kUnityGainFloat); |
| 172 | const uint16_t vl = u4_12_from_float(gain); |
| 173 | const uint32_t vrl = (vl << 16) | vl; |
| 174 | int numFrames = 0; |
| 175 | if (channelCount == 2) { |
| 176 | numFrames = bufferSizeBytes / sizeof(uint32_t); |
| 177 | if (numFrames == 0) { |
| 178 | return; |
| 179 | } |
| 180 | uint32_t* intBuffer = (uint32_t*)buffer; |
| 181 | if (CC_UNLIKELY(vl > unityGainQ4_12)) { |
| 182 | do { |
| 183 | int32_t l = mulRL(1, *intBuffer, vrl) >> 12; |
| 184 | int32_t r = mulRL(0, *intBuffer, vrl) >> 12; |
| 185 | l = clamp16(l); |
| 186 | r = clamp16(r); |
| 187 | *intBuffer++ = (r << 16) | (l & 0xFFFF); |
| 188 | } while (--numFrames); |
| 189 | } else { |
| 190 | do { |
| 191 | int32_t l = mulRL(1, *intBuffer, vrl) >> 12; |
| 192 | int32_t r = mulRL(0, *intBuffer, vrl) >> 12; |
| 193 | *intBuffer++ = (r << 16) | (l & 0xFFFF); |
| 194 | } while (--numFrames); |
| 195 | } |
| 196 | } else { |
| 197 | numFrames = bufferSizeBytes / sizeof(uint16_t); |
| 198 | if (numFrames == 0) { |
| 199 | return; |
| 200 | } |
| 201 | int16_t* intBuffer = (int16_t*)buffer; |
| 202 | if (CC_UNLIKELY(vl > unityGainQ4_12)) { |
| 203 | do { |
| 204 | int32_t mono = mul(*intBuffer, static_cast<int16_t>(vl)) >> 12; |
| 205 | *intBuffer++ = clamp16(mono); |
| 206 | } while (--numFrames); |
| 207 | } else { |
| 208 | do { |
| 209 | int32_t mono = mul(*intBuffer, static_cast<int16_t>(vl)) >> 12; |
| 210 | *intBuffer++ = static_cast<int16_t>(mono & 0xFFFF); |
| 211 | } while (--numFrames); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void applyGainToInt32Buffer(int32_t* typedBuffer, const size_t bufferSizeBytes, const float gain) { |
| 217 | int numSamples = bufferSizeBytes / sizeof(int32_t); |
| 218 | if (numSamples == 0) { |
| 219 | return; |
| 220 | } |
| 221 | if (CC_UNLIKELY(gain > kUnityGainFloat)) { |
| 222 | do { |
| 223 | float multiplied = (*typedBuffer) * gain; |
| 224 | if (multiplied > INT32_MAX) { |
| 225 | *typedBuffer++ = INT32_MAX; |
| 226 | } else if (multiplied < INT32_MIN) { |
| 227 | *typedBuffer++ = INT32_MIN; |
| 228 | } else { |
| 229 | *typedBuffer++ = multiplied; |
| 230 | } |
| 231 | } while (--numSamples); |
| 232 | } else { |
| 233 | do { |
| 234 | *typedBuffer++ = (*typedBuffer) * gain; |
| 235 | } while (--numSamples); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void applyGainToFloatBuffer(float* floatBuffer, const size_t bufferSizeBytes, const float gain) { |
| 240 | int numSamples = bufferSizeBytes / sizeof(float); |
| 241 | if (numSamples == 0) { |
| 242 | return; |
| 243 | } |
| 244 | if (CC_UNLIKELY(gain > kUnityGainFloat)) { |
| 245 | do { |
| 246 | *floatBuffer++ = std::clamp((*floatBuffer) * gain, -kUnityGainFloat, kUnityGainFloat); |
| 247 | } while (--numSamples); |
| 248 | } else { |
| 249 | do { |
| 250 | *floatBuffer++ = (*floatBuffer) * gain; |
| 251 | } while (--numSamples); |
| 252 | } |
| 253 | } |
| 254 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 255 | } // namespace |
| 256 | |
| 257 | std::ostream& operator<<(std::ostream& os, const DeviceProfile& device) { |
| 258 | return os << "<" << device.card << "," << device.device << ">"; |
| 259 | } |
| 260 | |
| 261 | AudioChannelLayout getChannelLayoutMaskFromChannelCount(unsigned int channelCount, int isInput) { |
| 262 | return findValueOrDefault( |
| 263 | isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(), |
| 264 | channelCount, getInvalidChannelLayout()); |
| 265 | } |
| 266 | |
| 267 | AudioChannelLayout getChannelIndexMaskFromChannelCount(unsigned int channelCount) { |
| 268 | return findValueOrDefault(getSupportedChannelIndexLayoutMap(), channelCount, |
| 269 | getInvalidChannelLayout()); |
| 270 | } |
| 271 | |
| 272 | unsigned int getChannelCountFromChannelMask(const AudioChannelLayout& channelMask, bool isInput) { |
| 273 | switch (channelMask.getTag()) { |
| 274 | case AudioChannelLayout::Tag::layoutMask: { |
| 275 | return findKeyOrDefault( |
| 276 | isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(), |
| 277 | static_cast<unsigned>(getChannelCount(channelMask)), 0u /*defaultValue*/); |
| 278 | } |
| 279 | case AudioChannelLayout::Tag::indexMask: { |
| 280 | return findKeyOrDefault(getSupportedChannelIndexLayoutMap(), |
| 281 | static_cast<unsigned>(getChannelCount(channelMask)), |
| 282 | 0u /*defaultValue*/); |
| 283 | } |
| 284 | case AudioChannelLayout::Tag::none: |
| 285 | case AudioChannelLayout::Tag::invalid: |
| 286 | case AudioChannelLayout::Tag::voiceMask: |
| 287 | default: |
| 288 | return 0; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | std::vector<AudioChannelLayout> getChannelMasksFromProfile(const alsa_device_profile* profile) { |
| 293 | const bool isInput = profile->direction == PCM_IN; |
| 294 | std::vector<AudioChannelLayout> channels; |
| 295 | for (size_t i = 0; i < AUDIO_PORT_MAX_CHANNEL_MASKS && profile->channel_counts[i] != 0; ++i) { |
| 296 | auto layoutMask = |
| 297 | alsa::getChannelLayoutMaskFromChannelCount(profile->channel_counts[i], isInput); |
| 298 | if (layoutMask.getTag() == AudioChannelLayout::Tag::layoutMask) { |
| 299 | channels.push_back(layoutMask); |
| 300 | } |
| 301 | auto indexMask = alsa::getChannelIndexMaskFromChannelCount(profile->channel_counts[i]); |
| 302 | if (indexMask.getTag() == AudioChannelLayout::Tag::indexMask) { |
| 303 | channels.push_back(indexMask); |
| 304 | } |
| 305 | } |
| 306 | return channels; |
| 307 | } |
| 308 | |
| 309 | std::optional<DeviceProfile> getDeviceProfile( |
| 310 | const ::aidl::android::media::audio::common::AudioDevice& audioDevice, bool isInput) { |
| 311 | if (audioDevice.address.getTag() != AudioDeviceAddress::Tag::alsa) { |
| 312 | LOG(ERROR) << __func__ << ": not alsa address: " << audioDevice.toString(); |
| 313 | return std::nullopt; |
| 314 | } |
| 315 | auto& alsaAddress = audioDevice.address.get<AudioDeviceAddress::Tag::alsa>(); |
| 316 | if (alsaAddress.size() != 2 || alsaAddress[0] < 0 || alsaAddress[1] < 0) { |
| 317 | LOG(ERROR) << __func__ |
| 318 | << ": malformed alsa address: " << ::android::internal::ToString(alsaAddress); |
| 319 | return std::nullopt; |
| 320 | } |
| 321 | return DeviceProfile{.card = alsaAddress[0], |
| 322 | .device = alsaAddress[1], |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 323 | .direction = isInput ? PCM_IN : PCM_OUT, |
| 324 | .isExternal = !audioDevice.type.connection.empty()}; |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | std::optional<DeviceProfile> getDeviceProfile( |
| 328 | const ::aidl::android::media::audio::common::AudioPort& audioPort) { |
| 329 | if (audioPort.ext.getTag() != AudioPortExt::Tag::device) { |
| 330 | LOG(ERROR) << __func__ << ": port id " << audioPort.id << " is not a device port"; |
| 331 | return std::nullopt; |
| 332 | } |
| 333 | auto& devicePort = audioPort.ext.get<AudioPortExt::Tag::device>(); |
| 334 | return getDeviceProfile(devicePort.device, audioPort.flags.getTag() == AudioIoFlags::input); |
| 335 | } |
| 336 | |
| 337 | std::optional<struct pcm_config> getPcmConfig(const StreamContext& context, bool isInput) { |
| 338 | struct pcm_config config; |
| 339 | config.channels = alsa::getChannelCountFromChannelMask(context.getChannelLayout(), isInput); |
| 340 | if (config.channels == 0) { |
| 341 | LOG(ERROR) << __func__ << ": invalid channel=" << context.getChannelLayout().toString(); |
| 342 | return std::nullopt; |
| 343 | } |
| 344 | config.format = alsa::aidl2c_AudioFormatDescription_pcm_format(context.getFormat()); |
| 345 | if (config.format == PCM_FORMAT_INVALID) { |
| 346 | LOG(ERROR) << __func__ << ": invalid format=" << context.getFormat().toString(); |
| 347 | return std::nullopt; |
| 348 | } |
| 349 | config.rate = context.getSampleRate(); |
| 350 | if (config.rate == 0) { |
| 351 | LOG(ERROR) << __func__ << ": invalid sample rate=" << config.rate; |
| 352 | return std::nullopt; |
| 353 | } |
| 354 | return config; |
| 355 | } |
| 356 | |
| 357 | std::vector<int> getSampleRatesFromProfile(const alsa_device_profile* profile) { |
| 358 | std::vector<int> sampleRates; |
| 359 | for (int i = 0; i < std::min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) && |
| 360 | profile->sample_rates[i] != 0; |
| 361 | i++) { |
| 362 | sampleRates.push_back(profile->sample_rates[i]); |
| 363 | } |
| 364 | return sampleRates; |
| 365 | } |
| 366 | |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 367 | DeviceProxy openProxyForAttachedDevice(const DeviceProfile& deviceProfile, |
| 368 | struct pcm_config* pcmConfig, size_t bufferFrameCount) { |
| 369 | if (deviceProfile.isExternal) { |
| 370 | LOG(FATAL) << __func__ << ": called for an external device, address=" << deviceProfile; |
| 371 | } |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 372 | DeviceProxy proxy(deviceProfile); |
| 373 | if (!profile_fill_builtin_device_info(proxy.getProfile(), pcmConfig, bufferFrameCount)) { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 374 | LOG(FATAL) << __func__ << ": failed to init for built-in device, address=" << deviceProfile; |
| 375 | } |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 376 | if (int err = proxy_prepare_from_default_config(proxy.get(), proxy.getProfile()); err != 0) { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 377 | LOG(FATAL) << __func__ << ": fail to prepare for device address=" << deviceProfile |
| 378 | << " error=" << err; |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 379 | return DeviceProxy(); |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 380 | } |
| 381 | if (int err = proxy_open(proxy.get()); err != 0) { |
| 382 | LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile |
| 383 | << " error=" << err; |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 384 | return DeviceProxy(); |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 385 | } |
| 386 | return proxy; |
| 387 | } |
| 388 | |
| 389 | DeviceProxy openProxyForExternalDevice(const DeviceProfile& deviceProfile, |
| 390 | struct pcm_config* pcmConfig, bool requireExactMatch) { |
| 391 | if (!deviceProfile.isExternal) { |
| 392 | LOG(FATAL) << __func__ << ": called for an attached device, address=" << deviceProfile; |
| 393 | } |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 394 | auto proxy = readAlsaDeviceInfo(deviceProfile); |
| 395 | if (proxy.get() == nullptr) { |
| 396 | return proxy; |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 397 | } |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 398 | if (int err = proxy_prepare(proxy.get(), proxy.getProfile(), pcmConfig, requireExactMatch); |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 399 | err != 0) { |
| 400 | LOG(ERROR) << __func__ << ": fail to prepare for device address=" << deviceProfile |
| 401 | << " error=" << err; |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 402 | return DeviceProxy(); |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 403 | } |
| 404 | if (int err = proxy_open(proxy.get()); err != 0) { |
| 405 | LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile |
| 406 | << " error=" << err; |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 407 | return DeviceProxy(); |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 408 | } |
| 409 | return proxy; |
| 410 | } |
| 411 | |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 412 | DeviceProxy readAlsaDeviceInfo(const DeviceProfile& deviceProfile) { |
| 413 | DeviceProxy proxy(deviceProfile); |
| 414 | if (!profile_read_device_info(proxy.getProfile())) { |
| 415 | LOG(ERROR) << __func__ << ": unable to read device info, device address=" << deviceProfile; |
| 416 | return DeviceProxy(); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 417 | } |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 418 | return proxy; |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 421 | void resetTransferredFrames(DeviceProxy& proxy, uint64_t frames) { |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 422 | if (proxy.get() != nullptr) { |
| 423 | proxy.get()->transferred = frames; |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 427 | AudioFormatDescription c2aidl_pcm_format_AudioFormatDescription(enum pcm_format legacy) { |
| 428 | return findValueOrDefault(getPcmFormatToAudioFormatDescMap(), legacy, AudioFormatDescription()); |
| 429 | } |
| 430 | |
| 431 | pcm_format aidl2c_AudioFormatDescription_pcm_format(const AudioFormatDescription& aidl) { |
| 432 | return findValueOrDefault(getAudioFormatDescriptorToPcmFormatMap(), aidl, PCM_FORMAT_INVALID); |
| 433 | } |
| 434 | |
Weilin Xu | 550a244 | 2024-09-19 20:33:36 -0700 | [diff] [blame^] | 435 | void applyGain(void* buffer, float gain, size_t bufferSizeBytes, enum pcm_format pcmFormat, |
Weilin Xu | 122597a | 2024-07-18 11:43:48 -0700 | [diff] [blame] | 436 | int channelCount) { |
| 437 | if (channelCount != 1 && channelCount != 2) { |
| 438 | LOG(WARNING) << __func__ << ": unsupported channel count " << channelCount; |
| 439 | return; |
| 440 | } |
| 441 | if (!getPcmFormatToAudioFormatDescMap().contains(pcmFormat)) { |
| 442 | LOG(WARNING) << __func__ << ": unsupported pcm format " << pcmFormat; |
| 443 | return; |
| 444 | } |
Weilin Xu | 550a244 | 2024-09-19 20:33:36 -0700 | [diff] [blame^] | 445 | if (std::abs(gain - kUnityGainFloat) < 1e-6) { |
Weilin Xu | 122597a | 2024-07-18 11:43:48 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
Weilin Xu | 122597a | 2024-07-18 11:43:48 -0700 | [diff] [blame] | 448 | switch (pcmFormat) { |
Weilin Xu | 550a244 | 2024-09-19 20:33:36 -0700 | [diff] [blame^] | 449 | case PCM_FORMAT_S16_LE: |
| 450 | applyGainToInt16Buffer(buffer, bufferSizeBytes, gain, channelCount); |
| 451 | break; |
| 452 | case PCM_FORMAT_FLOAT_LE: { |
| 453 | float* floatBuffer = (float*)buffer; |
| 454 | applyGainToFloatBuffer(floatBuffer, bufferSizeBytes, gain); |
| 455 | } break; |
| 456 | case PCM_FORMAT_S24_LE: |
| 457 | // PCM_FORMAT_S24_LE buffer is composed of signed fixed-point 32-bit Q8.23 data with |
| 458 | // min and max limits of the same bit representation as min and max limits of |
| 459 | // PCM_FORMAT_S32_LE buffer. |
| 460 | case PCM_FORMAT_S32_LE: { |
| 461 | int32_t* typedBuffer = (int32_t*)buffer; |
| 462 | applyGainToInt32Buffer(typedBuffer, bufferSizeBytes, gain); |
| 463 | } break; |
| 464 | case PCM_FORMAT_S24_3LE: { |
| 465 | int numSamples = bufferSizeBytes / (sizeof(uint8_t) * 3); |
| 466 | if (numSamples == 0) { |
| 467 | return; |
Weilin Xu | 122597a | 2024-07-18 11:43:48 -0700 | [diff] [blame] | 468 | } |
Weilin Xu | 550a244 | 2024-09-19 20:33:36 -0700 | [diff] [blame^] | 469 | std::unique_ptr<int32_t[]> typedBuffer(new int32_t[numSamples]); |
| 470 | memcpy_to_i32_from_p24(typedBuffer.get(), (uint8_t*)buffer, numSamples); |
| 471 | applyGainToInt32Buffer(typedBuffer.get(), numSamples * sizeof(int32_t), gain); |
| 472 | memcpy_to_p24_from_i32((uint8_t*)buffer, typedBuffer.get(), numSamples); |
Weilin Xu | 122597a | 2024-07-18 11:43:48 -0700 | [diff] [blame] | 473 | } break; |
| 474 | default: |
Weilin Xu | 550a244 | 2024-09-19 20:33:36 -0700 | [diff] [blame^] | 475 | LOG(FATAL) << __func__ << ": unsupported pcm format " << pcmFormat; |
Weilin Xu | 122597a | 2024-07-18 11:43:48 -0700 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 480 | } // namespace aidl::android::hardware::audio::core::alsa |