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