blob: de19faf153a61e7bf0d5371b1ece838fb8b51357 [file] [log] [blame]
Mikhail Naganov1b444a52020-10-29 13:08:05 -07001/*
2 * Copyright (C) 2020 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 <stdio.h>
18#include <string.h>
19
20#define LOG_TAG "HidlUtils"
21#include <log/log.h>
22
23#include <android_audio_policy_configuration_V7_0-enums.h>
Mikhail Naganov3f1457b2020-12-17 15:01:54 -080024#include <common/all-versions/HidlSupport.h>
Mikhail Naganov1b444a52020-10-29 13:08:05 -070025#include <common/all-versions/VersionUtils.h>
26
27#include "HidlUtils.h"
28
29namespace android {
30namespace hardware {
31namespace audio {
32namespace common {
33namespace CPP_VERSION {
34namespace implementation {
35
36namespace xsd {
37using namespace ::android::audio::policy::configuration::V7_0;
38}
39
40#define CONVERT_CHECKED(expr, result) \
41 if (status_t status = (expr); status != NO_ERROR) { \
42 result = status; \
43 }
44
45status_t HidlUtils::audioIndexChannelMaskFromHal(audio_channel_mask_t halChannelMask,
46 AudioChannelMask* channelMask) {
47 *channelMask = audio_channel_index_mask_to_string(halChannelMask);
48 if (!channelMask->empty() && !xsd::isUnknownAudioChannelMask(*channelMask)) {
49 return NO_ERROR;
50 }
51 ALOGE("Unknown index channel mask value 0x%X", halChannelMask);
52 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
53 return BAD_VALUE;
54}
55
56status_t HidlUtils::audioInputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
57 AudioChannelMask* channelMask) {
58 *channelMask = audio_channel_in_mask_to_string(halChannelMask);
59 if (!channelMask->empty() && !xsd::isUnknownAudioChannelMask(*channelMask)) {
60 return NO_ERROR;
61 }
62 ALOGE("Unknown input channel mask value 0x%X", halChannelMask);
63 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
64 return BAD_VALUE;
65}
66
67status_t HidlUtils::audioOutputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
68 AudioChannelMask* channelMask) {
69 *channelMask = audio_channel_out_mask_to_string(halChannelMask);
70 if (!channelMask->empty() && !xsd::isUnknownAudioChannelMask(*channelMask)) {
71 return NO_ERROR;
72 }
73 ALOGE("Unknown output channel mask value 0x%X", halChannelMask);
74 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
75 return BAD_VALUE;
76}
77
78status_t HidlUtils::audioChannelMaskFromHal(audio_channel_mask_t halChannelMask, bool isInput,
79 AudioChannelMask* channelMask) {
80 if (halChannelMask != AUDIO_CHANNEL_NONE) {
81 if (audio_channel_mask_is_valid(halChannelMask)) {
82 switch (audio_channel_mask_get_representation(halChannelMask)) {
83 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
84 return isInput ? audioInputChannelMaskFromHal(halChannelMask, channelMask)
85 : audioOutputChannelMaskFromHal(halChannelMask, channelMask);
86 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
87 // Index masks do not have direction.
88 return audioIndexChannelMaskFromHal(halChannelMask, channelMask);
89 // no default
90 }
91 }
92 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
93 return BAD_VALUE;
94 }
95 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
96 return NO_ERROR;
97}
98
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080099status_t HidlUtils::audioChannelMasksFromHal(const std::vector<std::string>& halChannelMasks,
100 hidl_vec<AudioChannelMask>* channelMasks) {
101 hidl_vec<AudioChannelMask> tempChannelMasks;
102 tempChannelMasks.resize(halChannelMasks.size());
103 size_t tempPos = 0;
104 for (const auto& halChannelMask : halChannelMasks) {
105 if (!halChannelMask.empty() && !xsd::isUnknownAudioChannelMask(halChannelMask)) {
106 tempChannelMasks[tempPos++] = halChannelMask;
107 }
108 }
109 if (tempPos == tempChannelMasks.size()) {
110 *channelMasks = std::move(tempChannelMasks);
111 } else {
112 *channelMasks = hidl_vec<AudioChannelMask>(tempChannelMasks.begin(),
113 tempChannelMasks.begin() + tempPos);
114 }
115 return halChannelMasks.size() == channelMasks->size() ? NO_ERROR : BAD_VALUE;
116}
117
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700118status_t HidlUtils::audioChannelMaskToHal(const AudioChannelMask& channelMask,
119 audio_channel_mask_t* halChannelMask) {
120 if (!xsd::isUnknownAudioChannelMask(channelMask) &&
121 audio_channel_mask_from_string(channelMask.c_str(), halChannelMask)) {
122 return NO_ERROR;
123 }
124 ALOGE("Unknown channel mask \"%s\"", channelMask.c_str());
125 *halChannelMask = AUDIO_CHANNEL_NONE;
126 return BAD_VALUE;
127}
128
129status_t HidlUtils::audioConfigBaseFromHal(const audio_config_base_t& halConfigBase, bool isInput,
130 AudioConfigBase* configBase) {
131 status_t result = NO_ERROR;
132 configBase->sampleRateHz = halConfigBase.sample_rate;
133 CONVERT_CHECKED(
134 audioChannelMaskFromHal(halConfigBase.channel_mask, isInput, &configBase->channelMask),
135 result);
136 CONVERT_CHECKED(audioFormatFromHal(halConfigBase.format, &configBase->format), result);
137 return result;
138}
139
140status_t HidlUtils::audioConfigBaseToHal(const AudioConfigBase& configBase,
141 audio_config_base_t* halConfigBase) {
142 status_t result = NO_ERROR;
143 halConfigBase->sample_rate = configBase.sampleRateHz;
144 CONVERT_CHECKED(audioChannelMaskToHal(configBase.channelMask, &halConfigBase->channel_mask),
145 result);
146 CONVERT_CHECKED(audioFormatToHal(configBase.format, &halConfigBase->format), result);
147 return result;
148}
149
Mikhail Naganovb52e93f2020-12-10 16:10:08 -0800150status_t HidlUtils::audioContentTypeFromHal(const audio_content_type_t halContentType,
151 AudioContentType* contentType) {
152 *contentType = audio_content_type_to_string(halContentType);
153 if (!contentType->empty() && !xsd::isUnknownAudioContentType(*contentType)) {
154 return NO_ERROR;
155 }
156 ALOGE("Unknown audio content type value 0x%X", halContentType);
157 *contentType = toString(xsd::AudioContentType::AUDIO_CONTENT_TYPE_UNKNOWN);
158 return BAD_VALUE;
159}
160
161status_t HidlUtils::audioContentTypeToHal(const AudioContentType& contentType,
162 audio_content_type_t* halContentType) {
163 if (!xsd::isUnknownAudioContentType(contentType) &&
164 audio_content_type_from_string(contentType.c_str(), halContentType)) {
165 return NO_ERROR;
166 }
167 ALOGE("Unknown audio content type \"%s\"", contentType.c_str());
168 *halContentType = AUDIO_CONTENT_TYPE_UNKNOWN;
169 return BAD_VALUE;
170}
171
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700172status_t HidlUtils::audioDeviceTypeFromHal(audio_devices_t halDevice, AudioDevice* device) {
173 *device = audio_device_to_string(halDevice);
174 if (!device->empty() && !xsd::isUnknownAudioDevice(*device)) {
175 return NO_ERROR;
176 }
177 ALOGE("Unknown audio device value 0x%X", halDevice);
178 *device = toString(xsd::AudioDevice::AUDIO_DEVICE_NONE);
179 return BAD_VALUE;
180}
181
182status_t HidlUtils::audioDeviceTypeToHal(const AudioDevice& device, audio_devices_t* halDevice) {
183 if (!xsd::isUnknownAudioDevice(device) && audio_device_from_string(device.c_str(), halDevice)) {
184 return NO_ERROR;
185 }
186 ALOGE("Unknown audio device \"%s\"", device.c_str());
187 *halDevice = AUDIO_DEVICE_NONE;
188 return BAD_VALUE;
189}
190
191status_t HidlUtils::audioFormatFromHal(audio_format_t halFormat, AudioFormat* format) {
192 *format = audio_format_to_string(halFormat);
193 if (!format->empty() && !xsd::isUnknownAudioFormat(*format)) {
194 return NO_ERROR;
195 }
196 ALOGE("Unknown audio format value 0x%X", halFormat);
197 return BAD_VALUE;
198}
199
Mikhail Naganovb52e93f2020-12-10 16:10:08 -0800200status_t HidlUtils::audioFormatsFromHal(const std::vector<std::string>& halFormats,
201 hidl_vec<AudioFormat>* formats) {
202 hidl_vec<AudioFormat> tempFormats;
203 tempFormats.resize(halFormats.size());
204 size_t tempPos = 0;
205 for (const auto& halFormat : halFormats) {
206 if (!halFormat.empty() && !xsd::isUnknownAudioFormat(halFormat)) {
207 tempFormats[tempPos++] = halFormat;
208 }
209 }
210 if (tempPos == tempFormats.size()) {
211 *formats = std::move(tempFormats);
212 } else {
213 *formats = hidl_vec<AudioFormat>(tempFormats.begin(), tempFormats.begin() + tempPos);
214 }
215 return halFormats.size() == formats->size() ? NO_ERROR : BAD_VALUE;
216}
217
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700218status_t HidlUtils::audioFormatToHal(const AudioFormat& format, audio_format_t* halFormat) {
219 if (!xsd::isUnknownAudioFormat(format) && audio_format_from_string(format.c_str(), halFormat)) {
220 return NO_ERROR;
221 }
222 ALOGE("Unknown audio format \"%s\"", format.c_str());
223 *halFormat = AUDIO_FORMAT_DEFAULT;
224 return BAD_VALUE;
225}
226
227status_t HidlUtils::audioGainModeMaskFromHal(audio_gain_mode_t halGainModeMask,
228 hidl_vec<AudioGainMode>* gainModeMask) {
229 status_t status = NO_ERROR;
230 std::vector<AudioGainMode> result;
231 for (uint32_t bit = 0; bit < sizeof(audio_gain_mode_t) * 8; ++bit) {
232 audio_gain_mode_t flag = static_cast<audio_gain_mode_t>(1u << bit);
233 if ((flag & halGainModeMask) == flag) {
234 AudioGainMode flagStr = audio_gain_mode_to_string(flag);
235 if (!flagStr.empty() && !xsd::isUnknownAudioGainMode(flagStr)) {
236 result.push_back(flagStr);
237 } else {
238 ALOGE("Unknown audio gain mode value 0x%X", flag);
239 status = BAD_VALUE;
240 }
241 }
242 }
243 *gainModeMask = result;
244 return status;
245}
246
247status_t HidlUtils::audioGainModeMaskToHal(const hidl_vec<AudioGainMode>& gainModeMask,
248 audio_gain_mode_t* halGainModeMask) {
249 status_t status = NO_ERROR;
250 *halGainModeMask = {};
251 for (const auto& gainMode : gainModeMask) {
252 audio_gain_mode_t halGainMode;
253 if (!xsd::isUnknownAudioGainMode(gainMode) &&
254 audio_gain_mode_from_string(gainMode.c_str(), &halGainMode)) {
255 *halGainModeMask = static_cast<audio_gain_mode_t>(*halGainModeMask | halGainMode);
256 } else {
257 ALOGE("Unknown audio gain mode \"%s\"", gainMode.c_str());
258 status = BAD_VALUE;
259 }
260 }
261 return status;
262}
263
264status_t HidlUtils::audioSourceFromHal(audio_source_t halSource, AudioSource* source) {
265 *source = audio_source_to_string(halSource);
266 if (!source->empty() && !xsd::isUnknownAudioSource(*source)) {
267 return NO_ERROR;
268 }
269 ALOGE("Unknown audio source value 0x%X", halSource);
270 *source = toString(xsd::AudioSource::AUDIO_SOURCE_DEFAULT);
271 return BAD_VALUE;
272}
273
274status_t HidlUtils::audioSourceToHal(const AudioSource& source, audio_source_t* halSource) {
275 if (!xsd::isUnknownAudioSource(source) && audio_source_from_string(source.c_str(), halSource)) {
276 return NO_ERROR;
277 }
278 ALOGE("Unknown audio source \"%s\"", source.c_str());
279 *halSource = AUDIO_SOURCE_DEFAULT;
280 return BAD_VALUE;
281}
282
283status_t HidlUtils::audioStreamTypeFromHal(audio_stream_type_t halStreamType,
284 AudioStreamType* streamType) {
285 *streamType = audio_stream_type_to_string(halStreamType);
286 if (!streamType->empty() && !xsd::isUnknownAudioStreamType(*streamType)) {
287 return NO_ERROR;
288 }
289 ALOGE("Unknown audio stream type value 0x%X", halStreamType);
290 return BAD_VALUE;
291}
292
293status_t HidlUtils::audioStreamTypeToHal(const AudioStreamType& streamType,
294 audio_stream_type_t* halStreamType) {
295 if (!xsd::isUnknownAudioStreamType(streamType) &&
296 audio_stream_type_from_string(streamType.c_str(), halStreamType)) {
297 return NO_ERROR;
298 }
299 ALOGE("Unknown audio stream type \"%s\"", streamType.c_str());
300 *halStreamType = AUDIO_STREAM_DEFAULT;
301 return BAD_VALUE;
302}
303
304status_t HidlUtils::audioConfigFromHal(const audio_config_t& halConfig, bool isInput,
305 AudioConfig* config) {
306 status_t result = NO_ERROR;
307 audio_config_base_t halConfigBase = {halConfig.sample_rate, halConfig.channel_mask,
308 halConfig.format};
309 CONVERT_CHECKED(audioConfigBaseFromHal(halConfigBase, isInput, &config->base), result);
Mikhail Naganov3f1457b2020-12-17 15:01:54 -0800310 if (halConfig.offload_info.sample_rate != 0) {
311 config->offloadInfo.info({});
312 CONVERT_CHECKED(
313 audioOffloadInfoFromHal(halConfig.offload_info, &config->offloadInfo.info()),
314 result);
315 }
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700316 config->frameCount = halConfig.frame_count;
317 return result;
318}
319
320status_t HidlUtils::audioConfigToHal(const AudioConfig& config, audio_config_t* halConfig) {
321 status_t result = NO_ERROR;
322 *halConfig = AUDIO_CONFIG_INITIALIZER;
323 audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
324 CONVERT_CHECKED(audioConfigBaseToHal(config.base, &halConfigBase), result);
325 halConfig->sample_rate = halConfigBase.sample_rate;
326 halConfig->channel_mask = halConfigBase.channel_mask;
327 halConfig->format = halConfigBase.format;
Mikhail Naganov3f1457b2020-12-17 15:01:54 -0800328 if (config.offloadInfo.getDiscriminator() ==
329 AudioConfig::OffloadInfo::hidl_discriminator::info) {
330 CONVERT_CHECKED(audioOffloadInfoToHal(config.offloadInfo.info(), &halConfig->offload_info),
331 result);
332 }
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700333 halConfig->frame_count = config.frameCount;
334 return result;
335}
336
337status_t HidlUtils::audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool isInput,
338 AudioGainConfig* config) {
339 status_t result = NO_ERROR;
340 config->index = halConfig.index;
341 CONVERT_CHECKED(audioGainModeMaskFromHal(halConfig.mode, &config->mode), result);
342 CONVERT_CHECKED(audioChannelMaskFromHal(halConfig.channel_mask, isInput, &config->channelMask),
343 result);
344 if (halConfig.mode & AUDIO_GAIN_MODE_JOINT) {
345 config->values.resize(1);
346 config->values[0] = halConfig.values[0];
347 }
348 if (halConfig.mode & (AUDIO_GAIN_MODE_CHANNELS | AUDIO_GAIN_MODE_RAMP)) {
349 config->values.resize(__builtin_popcount(halConfig.channel_mask));
350 for (size_t i = 0; i < config->values.size(); ++i) {
351 config->values[i] = halConfig.values[i];
352 }
353 }
354 config->rampDurationMs = halConfig.ramp_duration_ms;
355 return result;
356}
357
358status_t HidlUtils::audioGainConfigToHal(const AudioGainConfig& config,
359 struct audio_gain_config* halConfig) {
360 status_t result = NO_ERROR;
361 halConfig->index = config.index;
362 CONVERT_CHECKED(audioGainModeMaskToHal(config.mode, &halConfig->mode), result);
363 CONVERT_CHECKED(audioChannelMaskToHal(config.channelMask, &halConfig->channel_mask), result);
364 memset(halConfig->values, 0, sizeof(halConfig->values));
365 if (halConfig->mode & AUDIO_GAIN_MODE_JOINT) {
366 if (config.values.size() > 0) {
367 halConfig->values[0] = config.values[0];
368 } else {
369 ALOGE("Empty values vector in AudioGainConfig");
370 result = BAD_VALUE;
371 }
372 }
373 if (halConfig->mode & (AUDIO_GAIN_MODE_CHANNELS | AUDIO_GAIN_MODE_RAMP)) {
374 size_t channelCount = __builtin_popcount(halConfig->channel_mask);
375 size_t valuesCount = config.values.size();
376 if (channelCount != valuesCount) {
377 ALOGE("Wrong number of values in AudioGainConfig, expected: %zu, found: %zu",
378 channelCount, valuesCount);
379 result = BAD_VALUE;
380 if (channelCount < valuesCount) {
381 valuesCount = channelCount;
382 }
383 }
384 for (size_t i = 0; i < valuesCount; ++i) {
385 halConfig->values[i] = config.values[i];
386 }
387 }
388 halConfig->ramp_duration_ms = config.rampDurationMs;
389 return result;
390}
391
392status_t HidlUtils::audioGainFromHal(const struct audio_gain& halGain, bool isInput,
393 AudioGain* gain) {
394 status_t result = NO_ERROR;
395 CONVERT_CHECKED(audioGainModeMaskFromHal(halGain.mode, &gain->mode), result);
396 CONVERT_CHECKED(audioChannelMaskFromHal(halGain.channel_mask, isInput, &gain->channelMask),
397 result);
398 gain->minValue = halGain.min_value;
399 gain->maxValue = halGain.max_value;
400 gain->defaultValue = halGain.default_value;
401 gain->stepValue = halGain.step_value;
402 gain->minRampMs = halGain.min_ramp_ms;
403 gain->maxRampMs = halGain.max_ramp_ms;
404 return result;
405}
406
407status_t HidlUtils::audioGainToHal(const AudioGain& gain, struct audio_gain* halGain) {
408 status_t result = NO_ERROR;
409 CONVERT_CHECKED(audioGainModeMaskToHal(gain.mode, &halGain->mode), result);
410 CONVERT_CHECKED(audioChannelMaskToHal(gain.channelMask, &halGain->channel_mask), result);
411 halGain->min_value = gain.minValue;
412 halGain->max_value = gain.maxValue;
413 halGain->default_value = gain.defaultValue;
414 halGain->step_value = gain.stepValue;
415 halGain->min_ramp_ms = gain.minRampMs;
416 halGain->max_ramp_ms = gain.maxRampMs;
417 return result;
418}
419
420status_t HidlUtils::audioUsageFromHal(audio_usage_t halUsage, AudioUsage* usage) {
421 if (halUsage == AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST ||
422 halUsage == AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT ||
423 halUsage == AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED ||
424 halUsage == AUDIO_USAGE_NOTIFICATION_EVENT) {
425 halUsage = AUDIO_USAGE_NOTIFICATION;
426 }
427 *usage = audio_usage_to_string(halUsage);
428 if (!usage->empty() && !xsd::isUnknownAudioUsage(*usage)) {
429 return NO_ERROR;
430 }
431 ALOGE("Unknown audio usage %d", halUsage);
432 *usage = toString(xsd::AudioUsage::AUDIO_USAGE_UNKNOWN);
433 return BAD_VALUE;
434}
435
436status_t HidlUtils::audioUsageToHal(const AudioUsage& usage, audio_usage_t* halUsage) {
437 if (!xsd::isUnknownAudioUsage(usage) && audio_usage_from_string(usage.c_str(), halUsage)) {
438 return NO_ERROR;
439 }
440 ALOGE("Unknown audio usage \"%s\"", usage.c_str());
441 *halUsage = AUDIO_USAGE_UNKNOWN;
442 return BAD_VALUE;
443}
444
445status_t HidlUtils::audioOffloadInfoFromHal(const audio_offload_info_t& halOffload,
446 AudioOffloadInfo* offload) {
447 status_t result = NO_ERROR;
448 audio_config_base_t halConfigBase = {halOffload.sample_rate, halOffload.channel_mask,
449 halOffload.format};
450 CONVERT_CHECKED(audioConfigBaseFromHal(halConfigBase, false /*isInput*/, &offload->base),
451 result);
452 CONVERT_CHECKED(audioStreamTypeFromHal(halOffload.stream_type, &offload->streamType), result);
453 offload->bitRatePerSecond = halOffload.bit_rate;
454 offload->durationMicroseconds = halOffload.duration_us;
455 offload->hasVideo = halOffload.has_video;
456 offload->isStreaming = halOffload.is_streaming;
457 offload->bitWidth = halOffload.bit_width;
458 offload->bufferSize = halOffload.offload_buffer_size;
459 CONVERT_CHECKED(audioUsageFromHal(halOffload.usage, &offload->usage), result);
460 if (halOffload.version >= AUDIO_OFFLOAD_INFO_VERSION_0_2) {
461 offload->encapsulationMode =
462 static_cast<AudioEncapsulationMode>(halOffload.encapsulation_mode);
463 offload->contentId = halOffload.content_id;
464 offload->syncId = halOffload.sync_id;
465 } else {
466 offload->encapsulationMode = AudioEncapsulationMode::NONE;
467 offload->contentId = 0;
468 offload->syncId = 0;
469 }
470 return result;
471}
472
473status_t HidlUtils::audioOffloadInfoToHal(const AudioOffloadInfo& offload,
474 audio_offload_info_t* halOffload) {
475 status_t result = NO_ERROR;
476 *halOffload = AUDIO_INFO_INITIALIZER;
477 audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
478 CONVERT_CHECKED(audioConfigBaseToHal(offload.base, &halConfigBase), result);
479 halOffload->sample_rate = halConfigBase.sample_rate;
480 halOffload->channel_mask = halConfigBase.channel_mask;
481 halOffload->format = halConfigBase.format;
482 CONVERT_CHECKED(audioStreamTypeToHal(offload.streamType, &halOffload->stream_type), result);
483 halOffload->bit_rate = offload.bitRatePerSecond;
484 halOffload->duration_us = offload.durationMicroseconds;
485 halOffload->has_video = offload.hasVideo;
486 halOffload->is_streaming = offload.isStreaming;
487 halOffload->bit_width = offload.bitWidth;
488 halOffload->offload_buffer_size = offload.bufferSize;
489 CONVERT_CHECKED(audioUsageToHal(offload.usage, &halOffload->usage), result);
490 halOffload->encapsulation_mode =
491 static_cast<audio_encapsulation_mode_t>(offload.encapsulationMode);
492 halOffload->content_id = offload.contentId;
493 halOffload->sync_id = offload.syncId;
494 return result;
495}
496
497status_t HidlUtils::audioPortConfigFromHal(const struct audio_port_config& halConfig,
498 AudioPortConfig* config) {
499 status_t result = NO_ERROR;
500 bool isInput = false;
501 config->id = halConfig.id;
502 CONVERT_CHECKED(audioPortExtendedInfoFromHal(halConfig.role, halConfig.type,
503 halConfig.ext.device, halConfig.ext.mix,
504 halConfig.ext.session, &config->ext, &isInput),
505 result);
506 if (audio_port_config_has_input_direction(&halConfig) != isInput) {
507 ALOGE("Inconsistent port config direction data, is input: %d (hal) != %d (converter)",
508 audio_port_config_has_input_direction(&halConfig), isInput);
509 result = BAD_VALUE;
510 }
511 if (halConfig.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
512 config->base.sampleRateHz = halConfig.sample_rate;
513 } else {
514 config->base.sampleRateHz = {};
515 }
516 if (halConfig.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
517 CONVERT_CHECKED(
518 audioChannelMaskFromHal(halConfig.channel_mask, isInput, &config->base.channelMask),
519 result);
520 } else {
521 config->base.channelMask = {};
522 }
523 if (halConfig.config_mask & AUDIO_PORT_CONFIG_FORMAT) {
524 CONVERT_CHECKED(audioFormatFromHal(halConfig.format, &config->base.format), result);
525 } else {
526 config->base.format = {};
527 }
528 if (halConfig.config_mask & AUDIO_PORT_CONFIG_GAIN) {
529 config->gain.config({});
530 CONVERT_CHECKED(audioGainConfigFromHal(halConfig.gain, isInput, &config->gain.config()),
531 result);
532 } else {
533 config->gain.unspecified({});
534 }
535 return result;
536}
537
538status_t HidlUtils::audioPortConfigToHal(const AudioPortConfig& config,
539 struct audio_port_config* halConfig) {
540 status_t result = NO_ERROR;
541 memset(halConfig, 0, sizeof(audio_port_config));
542 halConfig->id = config.id;
543 halConfig->config_mask = {};
544 if (config.base.sampleRateHz != 0) {
545 halConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE;
546 halConfig->sample_rate = config.base.sampleRateHz;
547 }
548 if (!config.base.channelMask.empty()) {
549 halConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK;
550 CONVERT_CHECKED(audioChannelMaskToHal(config.base.channelMask, &halConfig->channel_mask),
551 result);
552 }
553 if (!config.base.format.empty()) {
554 halConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT;
555 CONVERT_CHECKED(audioFormatToHal(config.base.format, &halConfig->format), result);
556 }
557 if (config.gain.getDiscriminator() ==
558 AudioPortConfig::OptionalGain::hidl_discriminator::config) {
559 halConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
560 CONVERT_CHECKED(audioGainConfigToHal(config.gain.config(), &halConfig->gain), result);
561 }
562 CONVERT_CHECKED(audioPortExtendedInfoToHal(config.ext, &halConfig->role, &halConfig->type,
563 &halConfig->ext.device, &halConfig->ext.mix,
564 &halConfig->ext.session),
565 result);
566 return result;
567}
568
569status_t HidlUtils::audioPortExtendedInfoFromHal(
570 audio_port_role_t role, audio_port_type_t type,
571 const struct audio_port_config_device_ext& device,
572 const struct audio_port_config_mix_ext& mix,
573 const struct audio_port_config_session_ext& session, AudioPortExtendedInfo* ext,
574 bool* isInput) {
575 status_t result = NO_ERROR;
576 *isInput = false;
577 switch (type) {
578 case AUDIO_PORT_TYPE_NONE:
579 ext->unspecified({});
580 break;
581 case AUDIO_PORT_TYPE_DEVICE: {
582 *isInput = role == AUDIO_PORT_ROLE_SOURCE;
583 ext->device({});
584 CONVERT_CHECKED(deviceAddressFromHal(device.type, device.address, &ext->device()),
585 result);
586 break;
587 }
588 case AUDIO_PORT_TYPE_MIX: {
589 *isInput = role == AUDIO_PORT_ROLE_SINK;
590 ext->mix({});
591 ext->mix().ioHandle = mix.handle;
592 if (role == AUDIO_PORT_ROLE_SOURCE) {
593 ext->mix().useCase.stream({});
594 CONVERT_CHECKED(
595 audioStreamTypeFromHal(mix.usecase.stream, &ext->mix().useCase.stream()),
596 result);
597 } else if (role == AUDIO_PORT_ROLE_SINK) {
598 ext->mix().useCase.source({});
599 CONVERT_CHECKED(
600 audioSourceFromHal(mix.usecase.source, &ext->mix().useCase.source()),
601 result);
602 }
603 break;
604 }
605 case AUDIO_PORT_TYPE_SESSION: {
606 ext->session(session.session);
607 break;
608 }
609 }
610 return result;
611}
612
613status_t HidlUtils::audioPortExtendedInfoToHal(const AudioPortExtendedInfo& ext,
614 audio_port_role_t* role, audio_port_type_t* type,
615 struct audio_port_config_device_ext* device,
616 struct audio_port_config_mix_ext* mix,
617 struct audio_port_config_session_ext* session) {
618 status_t result = NO_ERROR;
619 switch (ext.getDiscriminator()) {
620 case AudioPortExtendedInfo::hidl_discriminator::unspecified:
621 *role = AUDIO_PORT_ROLE_NONE;
622 *type = AUDIO_PORT_TYPE_NONE;
623 break;
624 case AudioPortExtendedInfo::hidl_discriminator::device:
625 *role = xsd::isOutputDevice(ext.device().deviceType) ? AUDIO_PORT_ROLE_SINK
626 : AUDIO_PORT_ROLE_SOURCE;
627 *type = AUDIO_PORT_TYPE_DEVICE;
628 CONVERT_CHECKED(deviceAddressToHal(ext.device(), &device->type, device->address),
629 result);
630 break;
631 case AudioPortExtendedInfo::hidl_discriminator::mix:
632 *type = AUDIO_PORT_TYPE_MIX;
633 switch (ext.mix().useCase.getDiscriminator()) {
634 case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::stream:
635 *role = AUDIO_PORT_ROLE_SOURCE;
636 CONVERT_CHECKED(
637 audioStreamTypeToHal(ext.mix().useCase.stream(), &mix->usecase.stream),
638 result);
639 break;
640 case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::source:
641 *role = AUDIO_PORT_ROLE_SINK;
642 CONVERT_CHECKED(
643 audioSourceToHal(ext.mix().useCase.source(), &mix->usecase.source),
644 result);
645 break;
646 }
647 mix->handle = ext.mix().ioHandle;
648 break;
649 case AudioPortExtendedInfo::hidl_discriminator::session:
650 *role = AUDIO_PORT_ROLE_NONE;
651 *type = AUDIO_PORT_TYPE_SESSION;
652 session->session = static_cast<audio_session_t>(ext.session());
653 break;
654 }
655 return result;
656}
657
658status_t HidlUtils::audioPortFromHal(const struct audio_port& halPort, AudioPort* port) {
659 struct audio_port_v7 halPortV7 = {};
660 audio_populate_audio_port_v7(&halPort, &halPortV7);
661 return audioPortFromHal(halPortV7, port);
662}
663
664status_t HidlUtils::audioPortToHal(const AudioPort& port, struct audio_port* halPort) {
665 status_t result = NO_ERROR;
666 struct audio_port_v7 halPortV7 = {};
667 CONVERT_CHECKED(audioPortToHal(port, &halPortV7), result);
668 if (!audio_populate_audio_port(&halPortV7, halPort)) {
669 result = BAD_VALUE;
670 }
671 return result;
672}
673
674status_t HidlUtils::audioPortFromHal(const struct audio_port_v7& halPort, AudioPort* port) {
675 status_t result = NO_ERROR;
676 bool isInput = false;
677 port->id = halPort.id;
678 port->name.setToExternal(halPort.name, strlen(halPort.name));
679 // HAL uses slightly different but convertible structures for the extended info in port
680 // and port config structures.
681 struct audio_port_config_device_ext halDevice = {};
682 struct audio_port_config_mix_ext halMix = {};
683 struct audio_port_config_session_ext halSession = {};
684 switch (halPort.type) {
685 case AUDIO_PORT_TYPE_NONE:
686 break;
687 case AUDIO_PORT_TYPE_DEVICE:
688 halDevice.type = halPort.ext.device.type;
689 memcpy(halDevice.address, halPort.ext.device.address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
690 break;
691 case AUDIO_PORT_TYPE_MIX:
692 halMix.handle = halPort.ext.mix.handle;
693 break;
694 case AUDIO_PORT_TYPE_SESSION:
695 halSession.session = halPort.ext.session.session;
696 break;
697 }
698 CONVERT_CHECKED(audioPortExtendedInfoFromHal(halPort.role, halPort.type, halDevice, halMix,
699 halSession, &port->ext, &isInput),
700 result);
701 port->profiles.resize(halPort.num_audio_profiles);
702 for (size_t i = 0; i < halPort.num_audio_profiles; ++i) {
703 CONVERT_CHECKED(audioProfileFromHal(halPort.audio_profiles[i], isInput, &port->profiles[i]),
704 result);
705 }
706 port->gains.resize(halPort.num_gains);
707 for (size_t i = 0; i < halPort.num_gains; ++i) {
708 CONVERT_CHECKED(audioGainFromHal(halPort.gains[i], isInput, &port->gains[i]), result);
709 }
710 CONVERT_CHECKED(audioPortConfigFromHal(halPort.active_config, &port->activeConfig), result);
711 return result;
712}
713
714status_t HidlUtils::audioPortToHal(const AudioPort& port, struct audio_port_v7* halPort) {
715 status_t result = NO_ERROR;
716 halPort->id = port.id;
717 strncpy(halPort->name, port.name.c_str(), AUDIO_PORT_MAX_NAME_LEN);
718 halPort->name[AUDIO_PORT_MAX_NAME_LEN - 1] = '\0';
719 if (port.name.size() >= AUDIO_PORT_MAX_NAME_LEN) {
720 ALOGE("HIDL Audio Port name is too long: %zu", port.name.size());
721 result = BAD_VALUE;
722 }
723 halPort->num_audio_profiles = port.profiles.size();
724 if (halPort->num_audio_profiles > AUDIO_PORT_MAX_AUDIO_PROFILES) {
725 ALOGE("HIDL Audio Port has too many profiles: %u", halPort->num_audio_profiles);
726 halPort->num_audio_profiles = AUDIO_PORT_MAX_AUDIO_PROFILES;
727 result = BAD_VALUE;
728 }
729 for (size_t i = 0; i < halPort->num_audio_profiles; ++i) {
730 CONVERT_CHECKED(audioProfileToHal(port.profiles[i], &halPort->audio_profiles[i]), result);
731 }
732 halPort->num_gains = port.gains.size();
733 if (halPort->num_gains > AUDIO_PORT_MAX_GAINS) {
734 ALOGE("HIDL Audio Port has too many gains: %u", halPort->num_gains);
735 halPort->num_gains = AUDIO_PORT_MAX_GAINS;
736 result = BAD_VALUE;
737 }
738 for (size_t i = 0; i < halPort->num_gains; ++i) {
739 CONVERT_CHECKED(audioGainToHal(port.gains[i], &halPort->gains[i]), result);
740 }
741 // HAL uses slightly different but convertible structures for the extended info in port
742 // and port config structures.
743 struct audio_port_config_device_ext halDevice = {};
744 struct audio_port_config_mix_ext halMix = {};
745 struct audio_port_config_session_ext halSession = {};
746 CONVERT_CHECKED(audioPortExtendedInfoToHal(port.ext, &halPort->role, &halPort->type, &halDevice,
747 &halMix, &halSession),
748 result);
749 switch (halPort->type) {
750 case AUDIO_PORT_TYPE_NONE:
751 break;
752 case AUDIO_PORT_TYPE_DEVICE:
753 halPort->ext.device.type = halDevice.type;
754 memcpy(halPort->ext.device.address, halDevice.address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
755 break;
756 case AUDIO_PORT_TYPE_MIX:
757 halPort->ext.mix.handle = halMix.handle;
758 break;
759 case AUDIO_PORT_TYPE_SESSION:
760 halPort->ext.session.session = halSession.session;
761 break;
762 }
763 CONVERT_CHECKED(audioPortConfigToHal(port.activeConfig, &halPort->active_config), result);
764 return result;
765}
766
767status_t HidlUtils::audioProfileFromHal(const struct audio_profile& halProfile, bool isInput,
768 AudioProfile* profile) {
769 status_t result = NO_ERROR;
770 CONVERT_CHECKED(audioFormatFromHal(halProfile.format, &profile->format), result);
771 profile->sampleRates.resize(halProfile.num_sample_rates);
772 for (size_t i = 0; i < halProfile.num_sample_rates; ++i) {
773 profile->sampleRates[i] = halProfile.sample_rates[i];
774 }
775 profile->channelMasks.resize(halProfile.num_channel_masks);
776 for (size_t i = 0; i < halProfile.num_channel_masks; ++i) {
777 CONVERT_CHECKED(audioChannelMaskFromHal(halProfile.channel_masks[i], isInput,
778 &profile->channelMasks[i]),
779 result);
780 }
781 return result;
782}
783
784status_t HidlUtils::audioProfileToHal(const AudioProfile& profile,
785 struct audio_profile* halProfile) {
786 status_t result = NO_ERROR;
787 CONVERT_CHECKED(audioFormatToHal(profile.format, &halProfile->format), result);
788 memset(halProfile->sample_rates, 0, sizeof(halProfile->sample_rates));
789 halProfile->num_sample_rates = profile.sampleRates.size();
790 if (halProfile->num_sample_rates > AUDIO_PORT_MAX_SAMPLING_RATES) {
791 ALOGE("HIDL Audio profile has too many sample rates: %u", halProfile->num_sample_rates);
792 halProfile->num_sample_rates = AUDIO_PORT_MAX_SAMPLING_RATES;
793 result = BAD_VALUE;
794 }
795 for (size_t i = 0; i < halProfile->num_sample_rates; ++i) {
796 halProfile->sample_rates[i] = profile.sampleRates[i];
797 }
798 memset(halProfile->channel_masks, 0, sizeof(halProfile->channel_masks));
799 halProfile->num_channel_masks = profile.channelMasks.size();
800 if (halProfile->num_channel_masks > AUDIO_PORT_MAX_CHANNEL_MASKS) {
801 ALOGE("HIDL Audio profile has too many channel masks: %u", halProfile->num_channel_masks);
802 halProfile->num_channel_masks = AUDIO_PORT_MAX_CHANNEL_MASKS;
803 result = BAD_VALUE;
804 }
805 for (size_t i = 0; i < halProfile->num_channel_masks; ++i) {
806 CONVERT_CHECKED(
807 audioChannelMaskToHal(profile.channelMasks[i], &halProfile->channel_masks[i]),
808 status);
809 }
810 return result;
811}
812
Mikhail Naganov3f1457b2020-12-17 15:01:54 -0800813status_t HidlUtils::audioTagsFromHal(const char* halTags, hidl_vec<AudioTag>* tags) {
814 std::vector<std::string> strTags = utils::splitString(halTags, sAudioTagSeparator);
815 status_t result = NO_ERROR;
816 tags->resize(strTags.size());
817 size_t to = 0;
818 for (size_t from = 0; from < strTags.size(); ++from) {
819 if (xsd::isVendorExtension(strTags[from])) {
820 (*tags)[to++] = strTags[from];
821 } else {
822 result = BAD_VALUE;
823 }
824 }
825 if (to != strTags.size()) {
826 tags->resize(to);
827 }
828 return result;
829}
830
831status_t HidlUtils::audioTagsToHal(const hidl_vec<AudioTag>& tags, char* halTags) {
832 memset(halTags, 0, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
833 status_t result = NO_ERROR;
834 std::ostringstream halTagsBuffer;
835 bool hasValue = false;
836 for (const auto& tag : tags) {
837 if (hasValue) {
838 halTagsBuffer << sAudioTagSeparator;
839 }
840 if (xsd::isVendorExtension(tag) && strchr(tag.c_str(), sAudioTagSeparator) == nullptr) {
841 halTagsBuffer << tag;
842 hasValue = true;
843 } else {
844 result = BAD_VALUE;
845 }
846 }
847 std::string fullHalTags{std::move(halTagsBuffer.str())};
848 strncpy(halTags, fullHalTags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
849 CONVERT_CHECKED(fullHalTags.length() <= AUDIO_ATTRIBUTES_TAGS_MAX_SIZE ? NO_ERROR : BAD_VALUE,
850 result);
851 return result;
852}
853
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700854status_t HidlUtils::deviceAddressFromHal(audio_devices_t halDeviceType,
855 const char* halDeviceAddress, DeviceAddress* device) {
856 status_t result = NO_ERROR;
857 CONVERT_CHECKED(audioDeviceTypeFromHal(halDeviceType, &device->deviceType), result);
858 if (audio_is_a2dp_out_device(halDeviceType) || audio_is_a2dp_in_device(halDeviceType)) {
859 device->address.mac({});
860 if (halDeviceAddress != nullptr) {
861 auto& mac = device->address.mac();
862 int status = sscanf(halDeviceAddress, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", &mac[0], &mac[1],
863 &mac[2], &mac[3], &mac[4], &mac[5]);
864 if (status != 6) {
865 ALOGE("BT A2DP device \"%s\" MAC address \"%s\" is invalid",
866 device->deviceType.c_str(), halDeviceAddress);
867 result = BAD_VALUE;
868 }
869 } else {
870 ALOGE("BT A2DP device \"%s\" does not have a MAC address", halDeviceAddress);
871 result = BAD_VALUE;
872 }
873 } else if (halDeviceType == AUDIO_DEVICE_OUT_IP || halDeviceType == AUDIO_DEVICE_IN_IP) {
874 device->address.ipv4({});
875 if (halDeviceAddress != nullptr) {
876 auto& ipv4 = device->address.ipv4();
877 int status = sscanf(halDeviceAddress, "%hhu.%hhu.%hhu.%hhu", &ipv4[0], &ipv4[1],
878 &ipv4[2], &ipv4[3]);
879 if (status != 4) {
880 ALOGE("IP device \"%s\" IPv4 address \"%s\" is invalid", device->deviceType.c_str(),
881 halDeviceAddress);
882 result = BAD_VALUE;
883 }
884 } else {
885 ALOGE("IP device \"%s\" does not have an IPv4 address", device->deviceType.c_str());
886 result = BAD_VALUE;
887 }
888 } else if (audio_is_usb_out_device(halDeviceType) || audio_is_usb_in_device(halDeviceType)) {
889 device->address.alsa({});
890 if (halDeviceAddress != nullptr) {
891 auto& alsa = device->address.alsa();
892 int status = sscanf(halDeviceAddress, "card=%d;device=%d", &alsa.card, &alsa.device);
893 if (status != 2) {
894 ALOGE("USB device \"%s\" ALSA address \"%s\" is invalid",
895 device->deviceType.c_str(), halDeviceAddress);
896 result = BAD_VALUE;
897 }
898 } else {
899 ALOGE("USB device \"%s\" does not have ALSA address", device->deviceType.c_str());
900 result = BAD_VALUE;
901 }
902 } else {
903 // Any other device type uses the 'id' field.
904 device->address.id(halDeviceAddress != nullptr ? halDeviceAddress : "");
905 }
906 return result;
907}
908
909status_t HidlUtils::deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType,
910 char* halDeviceAddress) {
911 status_t result = NO_ERROR;
912 CONVERT_CHECKED(audioDeviceTypeToHal(device.deviceType, halDeviceType), result);
913 memset(halDeviceAddress, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN);
914 if (audio_is_a2dp_out_device(*halDeviceType) || audio_is_a2dp_in_device(*halDeviceType)) {
915 if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::mac) {
916 const auto& mac = device.address.mac();
917 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN,
918 "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4],
919 mac[5]);
920 } else {
921 ALOGE("BT A2DP device \"%s\" does not have MAC address set", device.deviceType.c_str());
922 result = BAD_VALUE;
923 }
924 } else if (*halDeviceType == AUDIO_DEVICE_OUT_IP || *halDeviceType == AUDIO_DEVICE_IN_IP) {
925 if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::ipv4) {
926 const auto& ipv4 = device.address.ipv4();
927 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%d.%d.%d.%d", ipv4[0],
928 ipv4[1], ipv4[2], ipv4[3]);
929 } else {
930 ALOGE("IP device \"%s\" does not have IPv4 address set", device.deviceType.c_str());
931 result = BAD_VALUE;
932 }
933 } else if (audio_is_usb_out_device(*halDeviceType) || audio_is_usb_in_device(*halDeviceType)) {
934 if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::alsa) {
935 const auto& alsa = device.address.alsa();
936 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "card=%d;device=%d", alsa.card,
937 alsa.device);
938 } else {
939 ALOGE("USB device \"%s\" does not have ALSA address set", device.deviceType.c_str());
940 result = BAD_VALUE;
941 }
942 } else {
943 // Any other device type uses the 'id' field.
944 if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::id) {
945 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s",
946 device.address.id().c_str());
947 }
948 }
949 return result;
950}
951
952} // namespace implementation
953} // namespace CPP_VERSION
954} // namespace common
955} // namespace audio
956} // namespace hardware
957} // namespace android