blob: 10374f23161cb04e39708a6a57500379533759a2 [file] [log] [blame]
Mikhail Naganovc337a872023-07-07 12:01:17 -07001/*
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 Xu122597a2024-07-18 11:43:48 -070025#include <audio_utils/primitives.h>
26#include <cutils/compiler.h>
Mikhail Naganovc337a872023-07-07 12:01:17 -070027
28#include "Utils.h"
29#include "core-impl/utils.h"
30
31using aidl::android::hardware::audio::common::getChannelCount;
32using aidl::android::media::audio::common::AudioChannelLayout;
33using aidl::android::media::audio::common::AudioDeviceAddress;
34using aidl::android::media::audio::common::AudioFormatDescription;
35using aidl::android::media::audio::common::AudioFormatType;
36using aidl::android::media::audio::common::AudioIoFlags;
37using aidl::android::media::audio::common::AudioPortExt;
38using aidl::android::media::audio::common::PcmType;
39
40namespace aidl::android::hardware::audio::core::alsa {
41
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -070042DeviceProxy::DeviceProxy() : mProfile(nullptr), mProxy(nullptr, alsaProxyDeleter) {}
43
44DeviceProxy::DeviceProxy(const DeviceProfile& deviceProfile)
45 : mProfile(new alsa_device_profile), mProxy(new alsa_device_proxy, alsaProxyDeleter) {
46 profile_init(mProfile.get(), deviceProfile.direction);
47 mProfile->card = deviceProfile.card;
48 mProfile->device = deviceProfile.device;
49 memset(mProxy.get(), 0, sizeof(alsa_device_proxy));
50}
51
52void DeviceProxy::alsaProxyDeleter(alsa_device_proxy* proxy) {
53 if (proxy != nullptr) {
54 proxy_close(proxy);
55 delete proxy;
56 }
57}
58
Mikhail Naganovc337a872023-07-07 12:01:17 -070059namespace {
60
61using AudioChannelCountToMaskMap = std::map<unsigned int, AudioChannelLayout>;
62using AudioFormatDescToPcmFormatMap = std::map<AudioFormatDescription, enum pcm_format>;
63using PcmFormatToAudioFormatDescMap = std::map<enum pcm_format, AudioFormatDescription>;
64
65AudioChannelLayout getInvalidChannelLayout() {
66 static const AudioChannelLayout invalidChannelLayout =
67 AudioChannelLayout::make<AudioChannelLayout::Tag::invalid>(0);
68 return invalidChannelLayout;
69}
70
71static AudioChannelCountToMaskMap make_ChannelCountToMaskMap(
72 const std::set<AudioChannelLayout>& channelMasks) {
73 AudioChannelCountToMaskMap channelMaskToCountMap;
74 for (const auto& channelMask : channelMasks) {
75 channelMaskToCountMap.emplace(getChannelCount(channelMask), channelMask);
76 }
77 return channelMaskToCountMap;
78}
79
80#define DEFINE_CHANNEL_LAYOUT_MASK(n) \
81 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(AudioChannelLayout::LAYOUT_##n)
82
83const AudioChannelCountToMaskMap& getSupportedChannelOutLayoutMap() {
84 static const std::set<AudioChannelLayout> supportedOutChannelLayouts = {
Mikhail Naganov8c27e462024-07-18 17:03:53 -070085 DEFINE_CHANNEL_LAYOUT_MASK(MONO),
86 DEFINE_CHANNEL_LAYOUT_MASK(STEREO),
Mikhail Naganovc337a872023-07-07 12:01:17 -070087 };
88 static const AudioChannelCountToMaskMap outLayouts =
89 make_ChannelCountToMaskMap(supportedOutChannelLayouts);
90 return outLayouts;
91}
92
93const AudioChannelCountToMaskMap& getSupportedChannelInLayoutMap() {
94 static const std::set<AudioChannelLayout> supportedInChannelLayouts = {
95 DEFINE_CHANNEL_LAYOUT_MASK(MONO),
96 DEFINE_CHANNEL_LAYOUT_MASK(STEREO),
97 };
98 static const AudioChannelCountToMaskMap inLayouts =
99 make_ChannelCountToMaskMap(supportedInChannelLayouts);
100 return inLayouts;
101}
102
103#undef DEFINE_CHANNEL_LAYOUT_MASK
104#define DEFINE_CHANNEL_INDEX_MASK(n) \
105 AudioChannelLayout::make<AudioChannelLayout::Tag::indexMask>(AudioChannelLayout::INDEX_MASK_##n)
106
107const AudioChannelCountToMaskMap& getSupportedChannelIndexLayoutMap() {
108 static const std::set<AudioChannelLayout> supportedIndexChannelLayouts = {
109 DEFINE_CHANNEL_INDEX_MASK(1), DEFINE_CHANNEL_INDEX_MASK(2),
110 DEFINE_CHANNEL_INDEX_MASK(3), DEFINE_CHANNEL_INDEX_MASK(4),
111 DEFINE_CHANNEL_INDEX_MASK(5), DEFINE_CHANNEL_INDEX_MASK(6),
112 DEFINE_CHANNEL_INDEX_MASK(7), DEFINE_CHANNEL_INDEX_MASK(8),
113 DEFINE_CHANNEL_INDEX_MASK(9), DEFINE_CHANNEL_INDEX_MASK(10),
114 DEFINE_CHANNEL_INDEX_MASK(11), DEFINE_CHANNEL_INDEX_MASK(12),
115 DEFINE_CHANNEL_INDEX_MASK(13), DEFINE_CHANNEL_INDEX_MASK(14),
116 DEFINE_CHANNEL_INDEX_MASK(15), DEFINE_CHANNEL_INDEX_MASK(16),
117 DEFINE_CHANNEL_INDEX_MASK(17), DEFINE_CHANNEL_INDEX_MASK(18),
118 DEFINE_CHANNEL_INDEX_MASK(19), DEFINE_CHANNEL_INDEX_MASK(20),
119 DEFINE_CHANNEL_INDEX_MASK(21), DEFINE_CHANNEL_INDEX_MASK(22),
120 DEFINE_CHANNEL_INDEX_MASK(23), DEFINE_CHANNEL_INDEX_MASK(24),
121 };
122 static const AudioChannelCountToMaskMap indexLayouts =
123 make_ChannelCountToMaskMap(supportedIndexChannelLayouts);
124 return indexLayouts;
125}
126
127#undef DEFINE_CHANNEL_INDEX_MASK
128
129AudioFormatDescription make_AudioFormatDescription(AudioFormatType type) {
130 AudioFormatDescription result;
131 result.type = type;
132 return result;
133}
134
135AudioFormatDescription make_AudioFormatDescription(PcmType pcm) {
136 auto result = make_AudioFormatDescription(AudioFormatType::PCM);
137 result.pcm = pcm;
138 return result;
139}
140
141const AudioFormatDescToPcmFormatMap& getAudioFormatDescriptorToPcmFormatMap() {
142 static const AudioFormatDescToPcmFormatMap formatDescToPcmFormatMap = {
143 {make_AudioFormatDescription(PcmType::UINT_8_BIT), PCM_FORMAT_S8},
144 {make_AudioFormatDescription(PcmType::INT_16_BIT), PCM_FORMAT_S16_LE},
145 {make_AudioFormatDescription(PcmType::FIXED_Q_8_24), PCM_FORMAT_S24_LE},
146 {make_AudioFormatDescription(PcmType::INT_24_BIT), PCM_FORMAT_S24_3LE},
147 {make_AudioFormatDescription(PcmType::INT_32_BIT), PCM_FORMAT_S32_LE},
148 {make_AudioFormatDescription(PcmType::FLOAT_32_BIT), PCM_FORMAT_FLOAT_LE},
149 };
150 return formatDescToPcmFormatMap;
151}
152
153static PcmFormatToAudioFormatDescMap make_PcmFormatToAudioFormatDescMap(
154 const AudioFormatDescToPcmFormatMap& formatDescToPcmFormatMap) {
155 PcmFormatToAudioFormatDescMap result;
156 for (const auto& formatPair : formatDescToPcmFormatMap) {
157 result.emplace(formatPair.second, formatPair.first);
158 }
159 return result;
160}
161
162const PcmFormatToAudioFormatDescMap& getPcmFormatToAudioFormatDescMap() {
163 static const PcmFormatToAudioFormatDescMap pcmFormatToFormatDescMap =
164 make_PcmFormatToAudioFormatDescMap(getAudioFormatDescriptorToPcmFormatMap());
165 return pcmFormatToFormatDescMap;
166}
167
168} // namespace
169
170std::ostream& operator<<(std::ostream& os, const DeviceProfile& device) {
171 return os << "<" << device.card << "," << device.device << ">";
172}
173
174AudioChannelLayout getChannelLayoutMaskFromChannelCount(unsigned int channelCount, int isInput) {
175 return findValueOrDefault(
176 isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(),
177 channelCount, getInvalidChannelLayout());
178}
179
180AudioChannelLayout getChannelIndexMaskFromChannelCount(unsigned int channelCount) {
181 return findValueOrDefault(getSupportedChannelIndexLayoutMap(), channelCount,
182 getInvalidChannelLayout());
183}
184
185unsigned int getChannelCountFromChannelMask(const AudioChannelLayout& channelMask, bool isInput) {
186 switch (channelMask.getTag()) {
187 case AudioChannelLayout::Tag::layoutMask: {
188 return findKeyOrDefault(
189 isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(),
190 static_cast<unsigned>(getChannelCount(channelMask)), 0u /*defaultValue*/);
191 }
192 case AudioChannelLayout::Tag::indexMask: {
193 return findKeyOrDefault(getSupportedChannelIndexLayoutMap(),
194 static_cast<unsigned>(getChannelCount(channelMask)),
195 0u /*defaultValue*/);
196 }
197 case AudioChannelLayout::Tag::none:
198 case AudioChannelLayout::Tag::invalid:
199 case AudioChannelLayout::Tag::voiceMask:
200 default:
201 return 0;
202 }
203}
204
205std::vector<AudioChannelLayout> getChannelMasksFromProfile(const alsa_device_profile* profile) {
206 const bool isInput = profile->direction == PCM_IN;
207 std::vector<AudioChannelLayout> channels;
208 for (size_t i = 0; i < AUDIO_PORT_MAX_CHANNEL_MASKS && profile->channel_counts[i] != 0; ++i) {
209 auto layoutMask =
210 alsa::getChannelLayoutMaskFromChannelCount(profile->channel_counts[i], isInput);
211 if (layoutMask.getTag() == AudioChannelLayout::Tag::layoutMask) {
212 channels.push_back(layoutMask);
213 }
214 auto indexMask = alsa::getChannelIndexMaskFromChannelCount(profile->channel_counts[i]);
215 if (indexMask.getTag() == AudioChannelLayout::Tag::indexMask) {
216 channels.push_back(indexMask);
217 }
218 }
219 return channels;
220}
221
222std::optional<DeviceProfile> getDeviceProfile(
223 const ::aidl::android::media::audio::common::AudioDevice& audioDevice, bool isInput) {
224 if (audioDevice.address.getTag() != AudioDeviceAddress::Tag::alsa) {
225 LOG(ERROR) << __func__ << ": not alsa address: " << audioDevice.toString();
226 return std::nullopt;
227 }
228 auto& alsaAddress = audioDevice.address.get<AudioDeviceAddress::Tag::alsa>();
229 if (alsaAddress.size() != 2 || alsaAddress[0] < 0 || alsaAddress[1] < 0) {
230 LOG(ERROR) << __func__
231 << ": malformed alsa address: " << ::android::internal::ToString(alsaAddress);
232 return std::nullopt;
233 }
234 return DeviceProfile{.card = alsaAddress[0],
235 .device = alsaAddress[1],
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700236 .direction = isInput ? PCM_IN : PCM_OUT,
237 .isExternal = !audioDevice.type.connection.empty()};
Mikhail Naganovc337a872023-07-07 12:01:17 -0700238}
239
240std::optional<DeviceProfile> getDeviceProfile(
241 const ::aidl::android::media::audio::common::AudioPort& audioPort) {
242 if (audioPort.ext.getTag() != AudioPortExt::Tag::device) {
243 LOG(ERROR) << __func__ << ": port id " << audioPort.id << " is not a device port";
244 return std::nullopt;
245 }
246 auto& devicePort = audioPort.ext.get<AudioPortExt::Tag::device>();
247 return getDeviceProfile(devicePort.device, audioPort.flags.getTag() == AudioIoFlags::input);
248}
249
250std::optional<struct pcm_config> getPcmConfig(const StreamContext& context, bool isInput) {
251 struct pcm_config config;
252 config.channels = alsa::getChannelCountFromChannelMask(context.getChannelLayout(), isInput);
253 if (config.channels == 0) {
254 LOG(ERROR) << __func__ << ": invalid channel=" << context.getChannelLayout().toString();
255 return std::nullopt;
256 }
257 config.format = alsa::aidl2c_AudioFormatDescription_pcm_format(context.getFormat());
258 if (config.format == PCM_FORMAT_INVALID) {
259 LOG(ERROR) << __func__ << ": invalid format=" << context.getFormat().toString();
260 return std::nullopt;
261 }
262 config.rate = context.getSampleRate();
263 if (config.rate == 0) {
264 LOG(ERROR) << __func__ << ": invalid sample rate=" << config.rate;
265 return std::nullopt;
266 }
267 return config;
268}
269
270std::vector<int> getSampleRatesFromProfile(const alsa_device_profile* profile) {
271 std::vector<int> sampleRates;
272 for (int i = 0; i < std::min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) &&
273 profile->sample_rates[i] != 0;
274 i++) {
275 sampleRates.push_back(profile->sample_rates[i]);
276 }
277 return sampleRates;
278}
279
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700280DeviceProxy openProxyForAttachedDevice(const DeviceProfile& deviceProfile,
281 struct pcm_config* pcmConfig, size_t bufferFrameCount) {
282 if (deviceProfile.isExternal) {
283 LOG(FATAL) << __func__ << ": called for an external device, address=" << deviceProfile;
284 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700285 DeviceProxy proxy(deviceProfile);
286 if (!profile_fill_builtin_device_info(proxy.getProfile(), pcmConfig, bufferFrameCount)) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700287 LOG(FATAL) << __func__ << ": failed to init for built-in device, address=" << deviceProfile;
288 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700289 if (int err = proxy_prepare_from_default_config(proxy.get(), proxy.getProfile()); err != 0) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700290 LOG(FATAL) << __func__ << ": fail to prepare for device address=" << deviceProfile
291 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700292 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700293 }
294 if (int err = proxy_open(proxy.get()); err != 0) {
295 LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile
296 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700297 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700298 }
299 return proxy;
300}
301
302DeviceProxy openProxyForExternalDevice(const DeviceProfile& deviceProfile,
303 struct pcm_config* pcmConfig, bool requireExactMatch) {
304 if (!deviceProfile.isExternal) {
305 LOG(FATAL) << __func__ << ": called for an attached device, address=" << deviceProfile;
306 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700307 auto proxy = readAlsaDeviceInfo(deviceProfile);
308 if (proxy.get() == nullptr) {
309 return proxy;
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700310 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700311 if (int err = proxy_prepare(proxy.get(), proxy.getProfile(), pcmConfig, requireExactMatch);
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700312 err != 0) {
313 LOG(ERROR) << __func__ << ": fail to prepare for device address=" << deviceProfile
314 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700315 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700316 }
317 if (int err = proxy_open(proxy.get()); err != 0) {
318 LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile
319 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700320 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700321 }
322 return proxy;
323}
324
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700325DeviceProxy readAlsaDeviceInfo(const DeviceProfile& deviceProfile) {
326 DeviceProxy proxy(deviceProfile);
327 if (!profile_read_device_info(proxy.getProfile())) {
328 LOG(ERROR) << __func__ << ": unable to read device info, device address=" << deviceProfile;
329 return DeviceProxy();
Mikhail Naganovc337a872023-07-07 12:01:17 -0700330 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700331 return proxy;
Mikhail Naganovc337a872023-07-07 12:01:17 -0700332}
333
Mikhail Naganovcf824f62023-07-24 14:51:36 -0700334void resetTransferredFrames(DeviceProxy& proxy, uint64_t frames) {
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700335 if (proxy.get() != nullptr) {
336 proxy.get()->transferred = frames;
Mikhail Naganovcf824f62023-07-24 14:51:36 -0700337 }
338}
339
Mikhail Naganovc337a872023-07-07 12:01:17 -0700340AudioFormatDescription c2aidl_pcm_format_AudioFormatDescription(enum pcm_format legacy) {
341 return findValueOrDefault(getPcmFormatToAudioFormatDescMap(), legacy, AudioFormatDescription());
342}
343
344pcm_format aidl2c_AudioFormatDescription_pcm_format(const AudioFormatDescription& aidl) {
345 return findValueOrDefault(getAudioFormatDescriptorToPcmFormatMap(), aidl, PCM_FORMAT_INVALID);
346}
347
Weilin Xu122597a2024-07-18 11:43:48 -0700348void applyGain(void* buffer, float gain, size_t bytesToTransfer, enum pcm_format pcmFormat,
349 int channelCount) {
350 if (channelCount != 1 && channelCount != 2) {
351 LOG(WARNING) << __func__ << ": unsupported channel count " << channelCount;
352 return;
353 }
354 if (!getPcmFormatToAudioFormatDescMap().contains(pcmFormat)) {
355 LOG(WARNING) << __func__ << ": unsupported pcm format " << pcmFormat;
356 return;
357 }
358 const float unityGainFloat = 1.0f;
359 if (std::abs(gain - unityGainFloat) < 1e-6) {
360 return;
361 }
362 int numFrames;
363 switch (pcmFormat) {
364 case PCM_FORMAT_S16_LE: {
365 const uint16_t unityGainQ4_12 = u4_12_from_float(unityGainFloat);
366 const uint16_t vl = u4_12_from_float(gain);
367 const uint32_t vrl = (vl << 16) | vl;
368 if (channelCount == 2) {
369 numFrames = bytesToTransfer / sizeof(uint32_t);
370 uint32_t* intBuffer = (uint32_t*)buffer;
371 if (CC_UNLIKELY(vl > unityGainQ4_12)) {
372 // volume is boosted, so we might need to clamp even though
373 // we process only one track.
374 do {
375 int32_t l = mulRL(1, *intBuffer, vrl) >> 12;
376 int32_t r = mulRL(0, *intBuffer, vrl) >> 12;
377 l = clamp16(l);
378 r = clamp16(r);
379 *intBuffer++ = (r << 16) | (l & 0xFFFF);
380 } while (--numFrames);
381 } else {
382 do {
383 int32_t l = mulRL(1, *intBuffer, vrl) >> 12;
384 int32_t r = mulRL(0, *intBuffer, vrl) >> 12;
385 *intBuffer++ = (r << 16) | (l & 0xFFFF);
386 } while (--numFrames);
387 }
388 } else {
389 numFrames = bytesToTransfer / sizeof(uint16_t);
390 int16_t* intBuffer = (int16_t*)buffer;
391 if (CC_UNLIKELY(vl > unityGainQ4_12)) {
392 // volume is boosted, so we might need to clamp even though
393 // we process only one track.
394 do {
395 int32_t mono = mulRL(1, *intBuffer, vrl) >> 12;
396 *intBuffer++ = clamp16(mono);
397 } while (--numFrames);
398 } else {
399 do {
400 int32_t mono = mulRL(1, *intBuffer, vrl) >> 12;
401 *intBuffer++ = static_cast<int16_t>(mono & 0xFFFF);
402 } while (--numFrames);
403 }
404 }
405 } break;
406 default:
407 // TODO(336370745): Implement gain for other supported formats
408 break;
409 }
410}
411
Mikhail Naganovc337a872023-07-07 12:01:17 -0700412} // namespace aidl::android::hardware::audio::core::alsa