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