blob: 20f77978fde91cf7006ff0135339c85a016acf16 [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>
25
26#include "Utils.h"
27#include "core-impl/utils.h"
28
29using aidl::android::hardware::audio::common::getChannelCount;
30using aidl::android::media::audio::common::AudioChannelLayout;
31using aidl::android::media::audio::common::AudioDeviceAddress;
32using aidl::android::media::audio::common::AudioFormatDescription;
33using aidl::android::media::audio::common::AudioFormatType;
34using aidl::android::media::audio::common::AudioIoFlags;
35using aidl::android::media::audio::common::AudioPortExt;
36using aidl::android::media::audio::common::PcmType;
37
38namespace aidl::android::hardware::audio::core::alsa {
39
40namespace {
41
42using AudioChannelCountToMaskMap = std::map<unsigned int, AudioChannelLayout>;
43using AudioFormatDescToPcmFormatMap = std::map<AudioFormatDescription, enum pcm_format>;
44using PcmFormatToAudioFormatDescMap = std::map<enum pcm_format, AudioFormatDescription>;
45
46AudioChannelLayout getInvalidChannelLayout() {
47 static const AudioChannelLayout invalidChannelLayout =
48 AudioChannelLayout::make<AudioChannelLayout::Tag::invalid>(0);
49 return invalidChannelLayout;
50}
51
52static 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
64const 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
77const 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
91const 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
113AudioFormatDescription make_AudioFormatDescription(AudioFormatType type) {
114 AudioFormatDescription result;
115 result.type = type;
116 return result;
117}
118
119AudioFormatDescription make_AudioFormatDescription(PcmType pcm) {
120 auto result = make_AudioFormatDescription(AudioFormatType::PCM);
121 result.pcm = pcm;
122 return result;
123}
124
125const 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
137static 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
146const PcmFormatToAudioFormatDescMap& getPcmFormatToAudioFormatDescMap() {
147 static const PcmFormatToAudioFormatDescMap pcmFormatToFormatDescMap =
148 make_PcmFormatToAudioFormatDescMap(getAudioFormatDescriptorToPcmFormatMap());
149 return pcmFormatToFormatDescMap;
150}
151
152} // namespace
153
154std::ostream& operator<<(std::ostream& os, const DeviceProfile& device) {
155 return os << "<" << device.card << "," << device.device << ">";
156}
157
158AudioChannelLayout getChannelLayoutMaskFromChannelCount(unsigned int channelCount, int isInput) {
159 return findValueOrDefault(
160 isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(),
161 channelCount, getInvalidChannelLayout());
162}
163
164AudioChannelLayout getChannelIndexMaskFromChannelCount(unsigned int channelCount) {
165 return findValueOrDefault(getSupportedChannelIndexLayoutMap(), channelCount,
166 getInvalidChannelLayout());
167}
168
169unsigned 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
189std::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
206std::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 Naganov422f7e62023-07-13 16:32:08 -0700220 .direction = isInput ? PCM_IN : PCM_OUT,
221 .isExternal = !audioDevice.type.connection.empty()};
Mikhail Naganovc337a872023-07-07 12:01:17 -0700222}
223
224std::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
234std::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
254std::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
264DeviceProxy makeDeviceProxy() {
265 return DeviceProxy(new alsa_device_proxy, [](alsa_device_proxy* proxy) {
266 if (proxy != nullptr) {
267 proxy_close(proxy);
268 delete proxy;
269 }
270 });
271}
272
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700273DeviceProxy openProxyForAttachedDevice(const DeviceProfile& deviceProfile,
274 struct pcm_config* pcmConfig, size_t bufferFrameCount) {
275 if (deviceProfile.isExternal) {
276 LOG(FATAL) << __func__ << ": called for an external device, address=" << deviceProfile;
277 }
278 alsa_device_profile profile;
279 profile_init(&profile, deviceProfile.direction);
280 profile.card = deviceProfile.card;
281 profile.device = deviceProfile.device;
282 if (!profile_fill_builtin_device_info(&profile, pcmConfig, bufferFrameCount)) {
283 LOG(FATAL) << __func__ << ": failed to init for built-in device, address=" << deviceProfile;
284 }
285 auto proxy = makeDeviceProxy();
286 if (int err = proxy_prepare_from_default_config(proxy.get(), &profile); err != 0) {
287 LOG(FATAL) << __func__ << ": fail to prepare for device address=" << deviceProfile
288 << " error=" << err;
289 return nullptr;
290 }
291 if (int err = proxy_open(proxy.get()); err != 0) {
292 LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile
293 << " error=" << err;
294 return nullptr;
295 }
296 return proxy;
297}
298
299DeviceProxy openProxyForExternalDevice(const DeviceProfile& deviceProfile,
300 struct pcm_config* pcmConfig, bool requireExactMatch) {
301 if (!deviceProfile.isExternal) {
302 LOG(FATAL) << __func__ << ": called for an attached device, address=" << deviceProfile;
303 }
304 auto profile = readAlsaDeviceInfo(deviceProfile);
305 if (!profile.has_value()) {
306 LOG(ERROR) << __func__ << ": unable to read device info, device address=" << deviceProfile;
307 return nullptr;
308 }
309 auto proxy = makeDeviceProxy();
310 if (int err = proxy_prepare(proxy.get(), &profile.value(), pcmConfig, requireExactMatch);
311 err != 0) {
312 LOG(ERROR) << __func__ << ": fail to prepare for device address=" << deviceProfile
313 << " error=" << err;
314 return nullptr;
315 }
316 if (int err = proxy_open(proxy.get()); err != 0) {
317 LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile
318 << " error=" << err;
319 return nullptr;
320 }
321 return proxy;
322}
323
Mikhail Naganovc337a872023-07-07 12:01:17 -0700324std::optional<alsa_device_profile> readAlsaDeviceInfo(const DeviceProfile& deviceProfile) {
325 alsa_device_profile profile;
326 profile_init(&profile, deviceProfile.direction);
327 profile.card = deviceProfile.card;
328 profile.device = deviceProfile.device;
329 if (!profile_read_device_info(&profile)) {
330 LOG(ERROR) << __func__ << ": failed to read device info, card=" << profile.card
331 << ", device=" << profile.device;
332 return std::nullopt;
333 }
334 return profile;
335}
336
337AudioFormatDescription c2aidl_pcm_format_AudioFormatDescription(enum pcm_format legacy) {
338 return findValueOrDefault(getPcmFormatToAudioFormatDescMap(), legacy, AudioFormatDescription());
339}
340
341pcm_format aidl2c_AudioFormatDescription_pcm_format(const AudioFormatDescription& aidl) {
342 return findValueOrDefault(getAudioFormatDescriptorToPcmFormatMap(), aidl, PCM_FORMAT_INVALID);
343}
344
345} // namespace aidl::android::hardware::audio::core::alsa