1 /*
2 * Copyright (C) 2018 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 "HidlUtils.h"
18
19 #include <common/all-versions/VersionUtils.h>
20 #include <string.h>
21
22 using ::android::hardware::audio::common::utils::EnumBitfield;
23
24 namespace android {
25 namespace hardware {
26 namespace audio {
27 namespace common {
28 namespace CPP_VERSION {
29 namespace implementation {
30
audioConfigFromHal(const audio_config_t & halConfig,bool,AudioConfig * config)31 status_t HidlUtils::audioConfigFromHal(const audio_config_t& halConfig, bool, AudioConfig* config) {
32 config->sampleRateHz = halConfig.sample_rate;
33 config->channelMask = EnumBitfield<AudioChannelMask>(halConfig.channel_mask);
34 config->format = AudioFormat(halConfig.format);
35 status_t status = audioOffloadInfoFromHal(halConfig.offload_info, &config->offloadInfo);
36 config->frameCount = halConfig.frame_count;
37 return status;
38 }
39
audioConfigToHal(const AudioConfig & config,audio_config_t * halConfig)40 status_t HidlUtils::audioConfigToHal(const AudioConfig& config, audio_config_t* halConfig) {
41 memset(halConfig, 0, sizeof(audio_config_t));
42 halConfig->sample_rate = config.sampleRateHz;
43 halConfig->channel_mask = static_cast<audio_channel_mask_t>(config.channelMask);
44 halConfig->format = static_cast<audio_format_t>(config.format);
45 audioOffloadInfoToHal(config.offloadInfo, &halConfig->offload_info);
46 halConfig->frame_count = config.frameCount;
47 return NO_ERROR;
48 }
49
audioGainConfigFromHal(const struct audio_gain_config & halConfig,bool,AudioGainConfig * config)50 status_t HidlUtils::audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool,
51 AudioGainConfig* config) {
52 config->index = halConfig.index;
53 config->mode = EnumBitfield<AudioGainMode>(halConfig.mode);
54 config->channelMask = EnumBitfield<AudioChannelMask>(halConfig.channel_mask);
55 for (size_t i = 0; i < sizeof(audio_channel_mask_t) * 8; ++i) {
56 config->values[i] = halConfig.values[i];
57 }
58 config->rampDurationMs = halConfig.ramp_duration_ms;
59 return NO_ERROR;
60 }
61
audioGainConfigToHal(const AudioGainConfig & config,struct audio_gain_config * halConfig)62 status_t HidlUtils::audioGainConfigToHal(const AudioGainConfig& config,
63 struct audio_gain_config* halConfig) {
64 halConfig->index = config.index;
65 halConfig->mode = static_cast<audio_gain_mode_t>(config.mode);
66 halConfig->channel_mask = static_cast<audio_channel_mask_t>(config.channelMask);
67 memset(halConfig->values, 0, sizeof(halConfig->values));
68 for (size_t i = 0; i < sizeof(audio_channel_mask_t) * 8; ++i) {
69 halConfig->values[i] = config.values[i];
70 }
71 halConfig->ramp_duration_ms = config.rampDurationMs;
72 return NO_ERROR;
73 }
74
audioGainFromHal(const struct audio_gain & halGain,bool,AudioGain * gain)75 status_t HidlUtils::audioGainFromHal(const struct audio_gain& halGain, bool, AudioGain* gain) {
76 gain->mode = EnumBitfield<AudioGainMode>(halGain.mode);
77 gain->channelMask = EnumBitfield<AudioChannelMask>(halGain.channel_mask);
78 gain->minValue = halGain.min_value;
79 gain->maxValue = halGain.max_value;
80 gain->defaultValue = halGain.default_value;
81 gain->stepValue = halGain.step_value;
82 gain->minRampMs = halGain.min_ramp_ms;
83 gain->maxRampMs = halGain.max_ramp_ms;
84 return NO_ERROR;
85 }
86
audioGainToHal(const AudioGain & gain,struct audio_gain * halGain)87 status_t HidlUtils::audioGainToHal(const AudioGain& gain, struct audio_gain* halGain) {
88 halGain->mode = static_cast<audio_gain_mode_t>(gain.mode);
89 halGain->channel_mask = static_cast<audio_channel_mask_t>(gain.channelMask);
90 halGain->min_value = gain.minValue;
91 halGain->max_value = gain.maxValue;
92 halGain->default_value = gain.defaultValue;
93 halGain->step_value = gain.stepValue;
94 halGain->min_ramp_ms = gain.minRampMs;
95 halGain->max_ramp_ms = gain.maxRampMs;
96 return NO_ERROR;
97 }
98
audioUsageFromHal(audio_usage_t halUsage,AudioUsage * usage)99 status_t HidlUtils::audioUsageFromHal(audio_usage_t halUsage, AudioUsage* usage) {
100 switch (halUsage) {
101 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
102 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
103 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
104 case AUDIO_USAGE_NOTIFICATION_EVENT:
105 *usage = AudioUsage::NOTIFICATION;
106 break;
107 default:
108 *usage = static_cast<AudioUsage>(halUsage);
109 }
110 return NO_ERROR;
111 }
112
audioUsageToHal(const AudioUsage & usage,audio_usage_t * halUsage)113 status_t HidlUtils::audioUsageToHal(const AudioUsage& usage, audio_usage_t* halUsage) {
114 *halUsage = static_cast<audio_usage_t>(usage);
115 return NO_ERROR;
116 }
117
audioOffloadInfoFromHal(const audio_offload_info_t & halOffload,AudioOffloadInfo * offload)118 status_t HidlUtils::audioOffloadInfoFromHal(const audio_offload_info_t& halOffload,
119 AudioOffloadInfo* offload) {
120 offload->sampleRateHz = halOffload.sample_rate;
121 offload->channelMask = EnumBitfield<AudioChannelMask>(halOffload.channel_mask);
122 offload->format = AudioFormat(halOffload.format);
123 offload->streamType = AudioStreamType(halOffload.stream_type);
124 offload->bitRatePerSecond = halOffload.bit_rate;
125 offload->durationMicroseconds = halOffload.duration_us;
126 offload->hasVideo = halOffload.has_video;
127 offload->isStreaming = halOffload.is_streaming;
128 offload->bitWidth = halOffload.bit_width;
129 offload->bufferSize = halOffload.offload_buffer_size;
130 audioUsageFromHal(halOffload.usage, &offload->usage);
131 #if MAJOR_VERSION >= 6
132 if (halOffload.version >= AUDIO_OFFLOAD_INFO_VERSION_0_2) {
133 offload->encapsulationMode =
134 static_cast<AudioEncapsulationMode>(halOffload.encapsulation_mode);
135 offload->contentId = halOffload.content_id;
136 offload->syncId = halOffload.sync_id;
137 } else {
138 offload->encapsulationMode = AudioEncapsulationMode::NONE;
139 offload->contentId = 0;
140 offload->syncId = 0;
141 }
142 #else
143 // nonzero values here are not compatible with HAL versions below 6.
144 if (halOffload.version >= AUDIO_OFFLOAD_INFO_VERSION_0_2 &&
145 (halOffload.encapsulation_mode != AUDIO_ENCAPSULATION_MODE_NONE ||
146 halOffload.content_id != 0 || halOffload.sync_id != 0)) {
147 return BAD_VALUE;
148 }
149 #endif
150 return NO_ERROR;
151 }
152
audioOffloadInfoToHal(const AudioOffloadInfo & offload,audio_offload_info_t * halOffload)153 status_t HidlUtils::audioOffloadInfoToHal(const AudioOffloadInfo& offload,
154 audio_offload_info_t* halOffload) {
155 *halOffload = AUDIO_INFO_INITIALIZER;
156 halOffload->sample_rate = offload.sampleRateHz;
157 halOffload->channel_mask = static_cast<audio_channel_mask_t>(offload.channelMask);
158 halOffload->format = static_cast<audio_format_t>(offload.format);
159 halOffload->stream_type = static_cast<audio_stream_type_t>(offload.streamType);
160 halOffload->bit_rate = offload.bitRatePerSecond;
161 halOffload->duration_us = offload.durationMicroseconds;
162 halOffload->has_video = offload.hasVideo;
163 halOffload->is_streaming = offload.isStreaming;
164 halOffload->bit_width = offload.bitWidth;
165 halOffload->offload_buffer_size = offload.bufferSize;
166 audioUsageToHal(offload.usage, &halOffload->usage);
167 #if MAJOR_VERSION >= 6
168 halOffload->encapsulation_mode =
169 static_cast<audio_encapsulation_mode_t>(offload.encapsulationMode);
170 halOffload->content_id = offload.contentId;
171 halOffload->sync_id = offload.syncId;
172 #else
173 // offload doesn't contain encapsulationMode, contentId, syncId, so this is OK.
174 #endif
175 return NO_ERROR;
176 }
177
audioPortConfigFromHal(const struct audio_port_config & halConfig,AudioPortConfig * config)178 status_t HidlUtils::audioPortConfigFromHal(const struct audio_port_config& halConfig,
179 AudioPortConfig* config) {
180 config->id = halConfig.id;
181 config->role = AudioPortRole(halConfig.role);
182 config->type = AudioPortType(halConfig.type);
183 config->configMask = EnumBitfield<AudioPortConfigMask>(halConfig.config_mask);
184 config->sampleRateHz = halConfig.sample_rate;
185 config->channelMask = EnumBitfield<AudioChannelMask>(halConfig.channel_mask);
186 config->format = AudioFormat(halConfig.format);
187 audioGainConfigFromHal(halConfig.gain, false /*isInput--ignored*/, &config->gain);
188 switch (halConfig.type) {
189 case AUDIO_PORT_TYPE_NONE:
190 break;
191 case AUDIO_PORT_TYPE_DEVICE: {
192 config->ext.device.hwModule = halConfig.ext.device.hw_module;
193 config->ext.device.type = AudioDevice(halConfig.ext.device.type);
194 memcpy(config->ext.device.address.data(), halConfig.ext.device.address,
195 AUDIO_DEVICE_MAX_ADDRESS_LEN);
196 break;
197 }
198 case AUDIO_PORT_TYPE_MIX: {
199 config->ext.mix.hwModule = halConfig.ext.mix.hw_module;
200 config->ext.mix.ioHandle = halConfig.ext.mix.handle;
201 if (halConfig.role == AUDIO_PORT_ROLE_SOURCE) {
202 config->ext.mix.useCase.stream = AudioStreamType(halConfig.ext.mix.usecase.stream);
203 } else if (halConfig.role == AUDIO_PORT_ROLE_SINK) {
204 config->ext.mix.useCase.source = AudioSource(halConfig.ext.mix.usecase.source);
205 }
206 break;
207 }
208 case AUDIO_PORT_TYPE_SESSION: {
209 config->ext.session.session = halConfig.ext.session.session;
210 break;
211 }
212 }
213 return NO_ERROR;
214 }
215
audioPortConfigToHal(const AudioPortConfig & config,struct audio_port_config * halConfig)216 status_t HidlUtils::audioPortConfigToHal(const AudioPortConfig& config,
217 struct audio_port_config* halConfig) {
218 memset(halConfig, 0, sizeof(audio_port_config));
219 halConfig->id = config.id;
220 halConfig->role = static_cast<audio_port_role_t>(config.role);
221 halConfig->type = static_cast<audio_port_type_t>(config.type);
222 halConfig->config_mask = static_cast<unsigned int>(config.configMask);
223 halConfig->sample_rate = config.sampleRateHz;
224 halConfig->channel_mask = static_cast<audio_channel_mask_t>(config.channelMask);
225 halConfig->format = static_cast<audio_format_t>(config.format);
226 audioGainConfigToHal(config.gain, &halConfig->gain);
227 switch (config.type) {
228 case AudioPortType::NONE:
229 break;
230 case AudioPortType::DEVICE: {
231 halConfig->ext.device.hw_module = config.ext.device.hwModule;
232 halConfig->ext.device.type = static_cast<audio_devices_t>(config.ext.device.type);
233 memcpy(halConfig->ext.device.address, config.ext.device.address.data(),
234 AUDIO_DEVICE_MAX_ADDRESS_LEN);
235 break;
236 }
237 case AudioPortType::MIX: {
238 halConfig->ext.mix.hw_module = config.ext.mix.hwModule;
239 halConfig->ext.mix.handle = config.ext.mix.ioHandle;
240 if (config.role == AudioPortRole::SOURCE) {
241 halConfig->ext.mix.usecase.stream =
242 static_cast<audio_stream_type_t>(config.ext.mix.useCase.stream);
243 } else if (config.role == AudioPortRole::SINK) {
244 halConfig->ext.mix.usecase.source =
245 static_cast<audio_source_t>(config.ext.mix.useCase.source);
246 }
247 break;
248 }
249 case AudioPortType::SESSION: {
250 halConfig->ext.session.session =
251 static_cast<audio_session_t>(config.ext.session.session);
252 break;
253 }
254 }
255 return NO_ERROR;
256 }
257
audioPortFromHal(const struct audio_port & halPort,AudioPort * port)258 status_t HidlUtils::audioPortFromHal(const struct audio_port& halPort, AudioPort* port) {
259 port->id = halPort.id;
260 port->role = AudioPortRole(halPort.role);
261 port->type = AudioPortType(halPort.type);
262 port->name.setToExternal(halPort.name, strlen(halPort.name));
263 port->sampleRates.resize(halPort.num_sample_rates);
264 for (size_t i = 0; i < halPort.num_sample_rates; ++i) {
265 port->sampleRates[i] = halPort.sample_rates[i];
266 }
267 port->channelMasks.resize(halPort.num_channel_masks);
268 for (size_t i = 0; i < halPort.num_channel_masks; ++i) {
269 port->channelMasks[i] = EnumBitfield<AudioChannelMask>(halPort.channel_masks[i]);
270 }
271 port->formats.resize(halPort.num_formats);
272 for (size_t i = 0; i < halPort.num_formats; ++i) {
273 port->formats[i] = AudioFormat(halPort.formats[i]);
274 }
275 port->gains.resize(halPort.num_gains);
276 for (size_t i = 0; i < halPort.num_gains; ++i) {
277 audioGainFromHal(halPort.gains[i], false /*isInput--ignored*/, &port->gains[i]);
278 }
279 audioPortConfigFromHal(halPort.active_config, &port->activeConfig);
280 switch (halPort.type) {
281 case AUDIO_PORT_TYPE_NONE:
282 break;
283 case AUDIO_PORT_TYPE_DEVICE: {
284 port->ext.device.hwModule = halPort.ext.device.hw_module;
285 port->ext.device.type = AudioDevice(halPort.ext.device.type);
286 memcpy(port->ext.device.address.data(), halPort.ext.device.address,
287 AUDIO_DEVICE_MAX_ADDRESS_LEN);
288 break;
289 }
290 case AUDIO_PORT_TYPE_MIX: {
291 port->ext.mix.hwModule = halPort.ext.mix.hw_module;
292 port->ext.mix.ioHandle = halPort.ext.mix.handle;
293 port->ext.mix.latencyClass = AudioMixLatencyClass(halPort.ext.mix.latency_class);
294 break;
295 }
296 case AUDIO_PORT_TYPE_SESSION: {
297 port->ext.session.session = halPort.ext.session.session;
298 break;
299 }
300 }
301 return NO_ERROR;
302 }
303
audioPortToHal(const AudioPort & port,struct audio_port * halPort)304 status_t HidlUtils::audioPortToHal(const AudioPort& port, struct audio_port* halPort) {
305 memset(halPort, 0, sizeof(audio_port));
306 halPort->id = port.id;
307 halPort->role = static_cast<audio_port_role_t>(port.role);
308 halPort->type = static_cast<audio_port_type_t>(port.type);
309 strncpy(halPort->name, port.name.c_str(), AUDIO_PORT_MAX_NAME_LEN);
310 halPort->name[AUDIO_PORT_MAX_NAME_LEN - 1] = '\0';
311 halPort->num_sample_rates =
312 std::min(port.sampleRates.size(), static_cast<size_t>(AUDIO_PORT_MAX_SAMPLING_RATES));
313 for (size_t i = 0; i < halPort->num_sample_rates; ++i) {
314 halPort->sample_rates[i] = port.sampleRates[i];
315 }
316 halPort->num_channel_masks =
317 std::min(port.channelMasks.size(), static_cast<size_t>(AUDIO_PORT_MAX_CHANNEL_MASKS));
318 for (size_t i = 0; i < halPort->num_channel_masks; ++i) {
319 halPort->channel_masks[i] = static_cast<audio_channel_mask_t>(port.channelMasks[i]);
320 }
321 halPort->num_formats =
322 std::min(port.formats.size(), static_cast<size_t>(AUDIO_PORT_MAX_FORMATS));
323 for (size_t i = 0; i < halPort->num_formats; ++i) {
324 halPort->formats[i] = static_cast<audio_format_t>(port.formats[i]);
325 }
326 halPort->num_gains = std::min(port.gains.size(), static_cast<size_t>(AUDIO_PORT_MAX_GAINS));
327 for (size_t i = 0; i < halPort->num_gains; ++i) {
328 audioGainToHal(port.gains[i], &halPort->gains[i]);
329 }
330 audioPortConfigToHal(port.activeConfig, &halPort->active_config);
331 switch (port.type) {
332 case AudioPortType::NONE:
333 break;
334 case AudioPortType::DEVICE: {
335 halPort->ext.device.hw_module = port.ext.device.hwModule;
336 halPort->ext.device.type = static_cast<audio_devices_t>(port.ext.device.type);
337 memcpy(halPort->ext.device.address, port.ext.device.address.data(),
338 AUDIO_DEVICE_MAX_ADDRESS_LEN);
339 break;
340 }
341 case AudioPortType::MIX: {
342 halPort->ext.mix.hw_module = port.ext.mix.hwModule;
343 halPort->ext.mix.handle = port.ext.mix.ioHandle;
344 halPort->ext.mix.latency_class =
345 static_cast<audio_mix_latency_class_t>(port.ext.mix.latencyClass);
346 break;
347 }
348 case AudioPortType::SESSION: {
349 halPort->ext.session.session = static_cast<audio_session_t>(port.ext.session.session);
350 break;
351 }
352 }
353 return NO_ERROR;
354 }
355
356 #if MAJOR_VERSION >= 5
deviceAddressToHal(const DeviceAddress & device,audio_devices_t * halDeviceType,char * halDeviceAddress)357 status_t HidlUtils::deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType,
358 char* halDeviceAddress) {
359 return deviceAddressToHalImpl(device, halDeviceType, halDeviceAddress);
360 }
361
deviceAddressFromHal(audio_devices_t halDeviceType,const char * halDeviceAddress,DeviceAddress * device)362 status_t HidlUtils::deviceAddressFromHal(audio_devices_t halDeviceType,
363 const char* halDeviceAddress, DeviceAddress* device) {
364 return deviceAddressFromHalImpl(halDeviceType, halDeviceAddress, device);
365 }
366 #endif
367
368 } // namespace implementation
369 } // namespace CPP_VERSION
370 } // namespace common
371 } // namespace audio
372 } // namespace hardware
373 } // namespace android
374