blob: 8eaf162aa81f4f7c3b55895da2d3d2945bc60e9d [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
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -070040DeviceProxy::DeviceProxy() : mProfile(nullptr), mProxy(nullptr, alsaProxyDeleter) {}
41
42DeviceProxy::DeviceProxy(const DeviceProfile& deviceProfile)
43 : mProfile(new alsa_device_profile), mProxy(new alsa_device_proxy, alsaProxyDeleter) {
44 profile_init(mProfile.get(), deviceProfile.direction);
45 mProfile->card = deviceProfile.card;
46 mProfile->device = deviceProfile.device;
47 memset(mProxy.get(), 0, sizeof(alsa_device_proxy));
48}
49
50void DeviceProxy::alsaProxyDeleter(alsa_device_proxy* proxy) {
51 if (proxy != nullptr) {
52 proxy_close(proxy);
53 delete proxy;
54 }
55}
56
Mikhail Naganovc337a872023-07-07 12:01:17 -070057namespace {
58
59using AudioChannelCountToMaskMap = std::map<unsigned int, AudioChannelLayout>;
60using AudioFormatDescToPcmFormatMap = std::map<AudioFormatDescription, enum pcm_format>;
61using PcmFormatToAudioFormatDescMap = std::map<enum pcm_format, AudioFormatDescription>;
62
63AudioChannelLayout getInvalidChannelLayout() {
64 static const AudioChannelLayout invalidChannelLayout =
65 AudioChannelLayout::make<AudioChannelLayout::Tag::invalid>(0);
66 return invalidChannelLayout;
67}
68
69static AudioChannelCountToMaskMap make_ChannelCountToMaskMap(
70 const std::set<AudioChannelLayout>& channelMasks) {
71 AudioChannelCountToMaskMap channelMaskToCountMap;
72 for (const auto& channelMask : channelMasks) {
73 channelMaskToCountMap.emplace(getChannelCount(channelMask), channelMask);
74 }
75 return channelMaskToCountMap;
76}
77
78#define DEFINE_CHANNEL_LAYOUT_MASK(n) \
79 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(AudioChannelLayout::LAYOUT_##n)
80
81const AudioChannelCountToMaskMap& getSupportedChannelOutLayoutMap() {
82 static const std::set<AudioChannelLayout> supportedOutChannelLayouts = {
Mikhail Naganov8c27e462024-07-18 17:03:53 -070083 DEFINE_CHANNEL_LAYOUT_MASK(MONO),
84 DEFINE_CHANNEL_LAYOUT_MASK(STEREO),
Mikhail Naganovc337a872023-07-07 12:01:17 -070085 };
86 static const AudioChannelCountToMaskMap outLayouts =
87 make_ChannelCountToMaskMap(supportedOutChannelLayouts);
88 return outLayouts;
89}
90
91const AudioChannelCountToMaskMap& getSupportedChannelInLayoutMap() {
92 static const std::set<AudioChannelLayout> supportedInChannelLayouts = {
93 DEFINE_CHANNEL_LAYOUT_MASK(MONO),
94 DEFINE_CHANNEL_LAYOUT_MASK(STEREO),
95 };
96 static const AudioChannelCountToMaskMap inLayouts =
97 make_ChannelCountToMaskMap(supportedInChannelLayouts);
98 return inLayouts;
99}
100
101#undef DEFINE_CHANNEL_LAYOUT_MASK
102#define DEFINE_CHANNEL_INDEX_MASK(n) \
103 AudioChannelLayout::make<AudioChannelLayout::Tag::indexMask>(AudioChannelLayout::INDEX_MASK_##n)
104
105const AudioChannelCountToMaskMap& getSupportedChannelIndexLayoutMap() {
106 static const std::set<AudioChannelLayout> supportedIndexChannelLayouts = {
107 DEFINE_CHANNEL_INDEX_MASK(1), DEFINE_CHANNEL_INDEX_MASK(2),
108 DEFINE_CHANNEL_INDEX_MASK(3), DEFINE_CHANNEL_INDEX_MASK(4),
109 DEFINE_CHANNEL_INDEX_MASK(5), DEFINE_CHANNEL_INDEX_MASK(6),
110 DEFINE_CHANNEL_INDEX_MASK(7), DEFINE_CHANNEL_INDEX_MASK(8),
111 DEFINE_CHANNEL_INDEX_MASK(9), DEFINE_CHANNEL_INDEX_MASK(10),
112 DEFINE_CHANNEL_INDEX_MASK(11), DEFINE_CHANNEL_INDEX_MASK(12),
113 DEFINE_CHANNEL_INDEX_MASK(13), DEFINE_CHANNEL_INDEX_MASK(14),
114 DEFINE_CHANNEL_INDEX_MASK(15), DEFINE_CHANNEL_INDEX_MASK(16),
115 DEFINE_CHANNEL_INDEX_MASK(17), DEFINE_CHANNEL_INDEX_MASK(18),
116 DEFINE_CHANNEL_INDEX_MASK(19), DEFINE_CHANNEL_INDEX_MASK(20),
117 DEFINE_CHANNEL_INDEX_MASK(21), DEFINE_CHANNEL_INDEX_MASK(22),
118 DEFINE_CHANNEL_INDEX_MASK(23), DEFINE_CHANNEL_INDEX_MASK(24),
119 };
120 static const AudioChannelCountToMaskMap indexLayouts =
121 make_ChannelCountToMaskMap(supportedIndexChannelLayouts);
122 return indexLayouts;
123}
124
125#undef DEFINE_CHANNEL_INDEX_MASK
126
127AudioFormatDescription make_AudioFormatDescription(AudioFormatType type) {
128 AudioFormatDescription result;
129 result.type = type;
130 return result;
131}
132
133AudioFormatDescription make_AudioFormatDescription(PcmType pcm) {
134 auto result = make_AudioFormatDescription(AudioFormatType::PCM);
135 result.pcm = pcm;
136 return result;
137}
138
139const AudioFormatDescToPcmFormatMap& getAudioFormatDescriptorToPcmFormatMap() {
140 static const AudioFormatDescToPcmFormatMap formatDescToPcmFormatMap = {
141 {make_AudioFormatDescription(PcmType::UINT_8_BIT), PCM_FORMAT_S8},
142 {make_AudioFormatDescription(PcmType::INT_16_BIT), PCM_FORMAT_S16_LE},
143 {make_AudioFormatDescription(PcmType::FIXED_Q_8_24), PCM_FORMAT_S24_LE},
144 {make_AudioFormatDescription(PcmType::INT_24_BIT), PCM_FORMAT_S24_3LE},
145 {make_AudioFormatDescription(PcmType::INT_32_BIT), PCM_FORMAT_S32_LE},
146 {make_AudioFormatDescription(PcmType::FLOAT_32_BIT), PCM_FORMAT_FLOAT_LE},
147 };
148 return formatDescToPcmFormatMap;
149}
150
151static PcmFormatToAudioFormatDescMap make_PcmFormatToAudioFormatDescMap(
152 const AudioFormatDescToPcmFormatMap& formatDescToPcmFormatMap) {
153 PcmFormatToAudioFormatDescMap result;
154 for (const auto& formatPair : formatDescToPcmFormatMap) {
155 result.emplace(formatPair.second, formatPair.first);
156 }
157 return result;
158}
159
160const PcmFormatToAudioFormatDescMap& getPcmFormatToAudioFormatDescMap() {
161 static const PcmFormatToAudioFormatDescMap pcmFormatToFormatDescMap =
162 make_PcmFormatToAudioFormatDescMap(getAudioFormatDescriptorToPcmFormatMap());
163 return pcmFormatToFormatDescMap;
164}
165
166} // namespace
167
168std::ostream& operator<<(std::ostream& os, const DeviceProfile& device) {
169 return os << "<" << device.card << "," << device.device << ">";
170}
171
172AudioChannelLayout getChannelLayoutMaskFromChannelCount(unsigned int channelCount, int isInput) {
173 return findValueOrDefault(
174 isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(),
175 channelCount, getInvalidChannelLayout());
176}
177
178AudioChannelLayout getChannelIndexMaskFromChannelCount(unsigned int channelCount) {
179 return findValueOrDefault(getSupportedChannelIndexLayoutMap(), channelCount,
180 getInvalidChannelLayout());
181}
182
183unsigned int getChannelCountFromChannelMask(const AudioChannelLayout& channelMask, bool isInput) {
184 switch (channelMask.getTag()) {
185 case AudioChannelLayout::Tag::layoutMask: {
186 return findKeyOrDefault(
187 isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(),
188 static_cast<unsigned>(getChannelCount(channelMask)), 0u /*defaultValue*/);
189 }
190 case AudioChannelLayout::Tag::indexMask: {
191 return findKeyOrDefault(getSupportedChannelIndexLayoutMap(),
192 static_cast<unsigned>(getChannelCount(channelMask)),
193 0u /*defaultValue*/);
194 }
195 case AudioChannelLayout::Tag::none:
196 case AudioChannelLayout::Tag::invalid:
197 case AudioChannelLayout::Tag::voiceMask:
198 default:
199 return 0;
200 }
201}
202
203std::vector<AudioChannelLayout> getChannelMasksFromProfile(const alsa_device_profile* profile) {
204 const bool isInput = profile->direction == PCM_IN;
205 std::vector<AudioChannelLayout> channels;
206 for (size_t i = 0; i < AUDIO_PORT_MAX_CHANNEL_MASKS && profile->channel_counts[i] != 0; ++i) {
207 auto layoutMask =
208 alsa::getChannelLayoutMaskFromChannelCount(profile->channel_counts[i], isInput);
209 if (layoutMask.getTag() == AudioChannelLayout::Tag::layoutMask) {
210 channels.push_back(layoutMask);
211 }
212 auto indexMask = alsa::getChannelIndexMaskFromChannelCount(profile->channel_counts[i]);
213 if (indexMask.getTag() == AudioChannelLayout::Tag::indexMask) {
214 channels.push_back(indexMask);
215 }
216 }
217 return channels;
218}
219
220std::optional<DeviceProfile> getDeviceProfile(
221 const ::aidl::android::media::audio::common::AudioDevice& audioDevice, bool isInput) {
222 if (audioDevice.address.getTag() != AudioDeviceAddress::Tag::alsa) {
223 LOG(ERROR) << __func__ << ": not alsa address: " << audioDevice.toString();
224 return std::nullopt;
225 }
226 auto& alsaAddress = audioDevice.address.get<AudioDeviceAddress::Tag::alsa>();
227 if (alsaAddress.size() != 2 || alsaAddress[0] < 0 || alsaAddress[1] < 0) {
228 LOG(ERROR) << __func__
229 << ": malformed alsa address: " << ::android::internal::ToString(alsaAddress);
230 return std::nullopt;
231 }
232 return DeviceProfile{.card = alsaAddress[0],
233 .device = alsaAddress[1],
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700234 .direction = isInput ? PCM_IN : PCM_OUT,
235 .isExternal = !audioDevice.type.connection.empty()};
Mikhail Naganovc337a872023-07-07 12:01:17 -0700236}
237
238std::optional<DeviceProfile> getDeviceProfile(
239 const ::aidl::android::media::audio::common::AudioPort& audioPort) {
240 if (audioPort.ext.getTag() != AudioPortExt::Tag::device) {
241 LOG(ERROR) << __func__ << ": port id " << audioPort.id << " is not a device port";
242 return std::nullopt;
243 }
244 auto& devicePort = audioPort.ext.get<AudioPortExt::Tag::device>();
245 return getDeviceProfile(devicePort.device, audioPort.flags.getTag() == AudioIoFlags::input);
246}
247
248std::optional<struct pcm_config> getPcmConfig(const StreamContext& context, bool isInput) {
249 struct pcm_config config;
250 config.channels = alsa::getChannelCountFromChannelMask(context.getChannelLayout(), isInput);
251 if (config.channels == 0) {
252 LOG(ERROR) << __func__ << ": invalid channel=" << context.getChannelLayout().toString();
253 return std::nullopt;
254 }
255 config.format = alsa::aidl2c_AudioFormatDescription_pcm_format(context.getFormat());
256 if (config.format == PCM_FORMAT_INVALID) {
257 LOG(ERROR) << __func__ << ": invalid format=" << context.getFormat().toString();
258 return std::nullopt;
259 }
260 config.rate = context.getSampleRate();
261 if (config.rate == 0) {
262 LOG(ERROR) << __func__ << ": invalid sample rate=" << config.rate;
263 return std::nullopt;
264 }
265 return config;
266}
267
268std::vector<int> getSampleRatesFromProfile(const alsa_device_profile* profile) {
269 std::vector<int> sampleRates;
270 for (int i = 0; i < std::min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) &&
271 profile->sample_rates[i] != 0;
272 i++) {
273 sampleRates.push_back(profile->sample_rates[i]);
274 }
275 return sampleRates;
276}
277
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700278DeviceProxy openProxyForAttachedDevice(const DeviceProfile& deviceProfile,
279 struct pcm_config* pcmConfig, size_t bufferFrameCount) {
280 if (deviceProfile.isExternal) {
281 LOG(FATAL) << __func__ << ": called for an external device, address=" << deviceProfile;
282 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700283 DeviceProxy proxy(deviceProfile);
284 if (!profile_fill_builtin_device_info(proxy.getProfile(), pcmConfig, bufferFrameCount)) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700285 LOG(FATAL) << __func__ << ": failed to init for built-in device, address=" << deviceProfile;
286 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700287 if (int err = proxy_prepare_from_default_config(proxy.get(), proxy.getProfile()); err != 0) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700288 LOG(FATAL) << __func__ << ": fail to prepare for device address=" << deviceProfile
289 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700290 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700291 }
292 if (int err = proxy_open(proxy.get()); err != 0) {
293 LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile
294 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700295 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700296 }
297 return proxy;
298}
299
300DeviceProxy openProxyForExternalDevice(const DeviceProfile& deviceProfile,
301 struct pcm_config* pcmConfig, bool requireExactMatch) {
302 if (!deviceProfile.isExternal) {
303 LOG(FATAL) << __func__ << ": called for an attached device, address=" << deviceProfile;
304 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700305 auto proxy = readAlsaDeviceInfo(deviceProfile);
306 if (proxy.get() == nullptr) {
307 return proxy;
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700308 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700309 if (int err = proxy_prepare(proxy.get(), proxy.getProfile(), pcmConfig, requireExactMatch);
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700310 err != 0) {
311 LOG(ERROR) << __func__ << ": fail to prepare for device address=" << deviceProfile
312 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700313 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700314 }
315 if (int err = proxy_open(proxy.get()); err != 0) {
316 LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile
317 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700318 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700319 }
320 return proxy;
321}
322
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700323DeviceProxy readAlsaDeviceInfo(const DeviceProfile& deviceProfile) {
324 DeviceProxy proxy(deviceProfile);
325 if (!profile_read_device_info(proxy.getProfile())) {
326 LOG(ERROR) << __func__ << ": unable to read device info, device address=" << deviceProfile;
327 return DeviceProxy();
Mikhail Naganovc337a872023-07-07 12:01:17 -0700328 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700329 return proxy;
Mikhail Naganovc337a872023-07-07 12:01:17 -0700330}
331
Mikhail Naganovcf824f62023-07-24 14:51:36 -0700332void resetTransferredFrames(DeviceProxy& proxy, uint64_t frames) {
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700333 if (proxy.get() != nullptr) {
334 proxy.get()->transferred = frames;
Mikhail Naganovcf824f62023-07-24 14:51:36 -0700335 }
336}
337
Mikhail Naganovc337a872023-07-07 12:01:17 -0700338AudioFormatDescription c2aidl_pcm_format_AudioFormatDescription(enum pcm_format legacy) {
339 return findValueOrDefault(getPcmFormatToAudioFormatDescMap(), legacy, AudioFormatDescription());
340}
341
342pcm_format aidl2c_AudioFormatDescription_pcm_format(const AudioFormatDescription& aidl) {
343 return findValueOrDefault(getAudioFormatDescriptorToPcmFormatMap(), aidl, PCM_FORMAT_INVALID);
344}
345
346} // namespace aidl::android::hardware::audio::core::alsa