blob: cf07a47f1eeabaa45c021004a7980fcac236a820 [file] [log] [blame]
Kevin Rocard4bcd67f2018-02-28 14:33:38 -08001/*
2 * Copyright (C) 2016 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 <string.h>
18
19#define LOG_TAG "HalHidl"
jiabindaf49952019-11-22 14:10:57 -080020#include <media/AudioContainers.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080021#include <media/AudioParameter.h>
22#include <utils/Log.h>
23
24#include "ConversionHelperHidl.h"
25
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080026namespace android {
Kevin Rocard070e7512018-05-22 09:29:13 -070027namespace CPP_VERSION {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080028
Mikhail Naganov04395fa2018-12-14 15:07:26 -080029using namespace ::android::hardware::audio::common::CPP_VERSION;
30using namespace ::android::hardware::audio::CPP_VERSION;
31
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080032// static
33status_t ConversionHelperHidl::keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys) {
34 AudioParameter halKeys(keys);
35 if (halKeys.size() == 0) return BAD_VALUE;
36 hidlKeys->resize(halKeys.size());
37 //FIXME: keyStreamSupportedChannels and keyStreamSupportedSamplingRates come with a
38 // "keyFormat=<value>" pair. We need to transform it into a single key string so that it is
39 // carried over to the legacy HAL via HIDL.
40 String8 value;
41 bool keepFormatValue = halKeys.size() == 2 &&
42 (halKeys.get(String8(AudioParameter::keyStreamSupportedChannels), value) == NO_ERROR ||
43 halKeys.get(String8(AudioParameter::keyStreamSupportedSamplingRates), value) == NO_ERROR);
jiabin1c4794b2020-05-05 10:08:05 -070044 // When querying encapsulation capabilities, "keyRouting=<value>" pair is used to identify
45 // the device. We need to transform it into a single key string so that it is carried over to
46 // the legacy HAL via HIDL.
47 bool keepRoutingValue =
48 halKeys.get(String8(AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_MODES),
49 value) == NO_ERROR ||
50 halKeys.get(String8(AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_METADATA_TYPES),
51 value) == NO_ERROR;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080052
Kuowei Li3bea3a42020-08-13 14:44:25 +080053 const bool keepDelayValue =
54 halKeys.get(String8(AUDIO_PARAMETER_DEVICE_ADDITIONAL_OUTPUT_DELAY),
55 value) == NO_ERROR ||
56 halKeys.get(String8(AUDIO_PARAMETER_DEVICE_MAX_ADDITIONAL_OUTPUT_DELAY),
57 value) == NO_ERROR;
58
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080059 for (size_t i = 0; i < halKeys.size(); ++i) {
60 String8 key;
61 status_t status = halKeys.getAt(i, key);
62 if (status != OK) return status;
jiabin1c4794b2020-05-05 10:08:05 -070063 if ((keepFormatValue && key == AudioParameter::keyFormat) ||
Kuowei Li3bea3a42020-08-13 14:44:25 +080064 (keepRoutingValue && key == AudioParameter::keyRouting) ||
65 (keepDelayValue && key == AUDIO_PARAMETER_DEVICE_ADDITIONAL_OUTPUT_DELAY) ||
66 (keepDelayValue && key == AUDIO_PARAMETER_DEVICE_MAX_ADDITIONAL_OUTPUT_DELAY)) {
jiabin1c4794b2020-05-05 10:08:05 -070067 AudioParameter keepValueParam;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080068 halKeys.getAt(i, key, value);
jiabin1c4794b2020-05-05 10:08:05 -070069 keepValueParam.add(key, value);
70 key = keepValueParam.toString();
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080071 }
72 (*hidlKeys)[i] = key.string();
73 }
74 return OK;
75}
76
77// static
78status_t ConversionHelperHidl::parametersFromHal(
79 const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams) {
80 AudioParameter params(kvPairs);
81 if (params.size() == 0) return BAD_VALUE;
82 hidlParams->resize(params.size());
83 for (size_t i = 0; i < params.size(); ++i) {
84 String8 key, value;
85 status_t status = params.getAt(i, key, value);
86 if (status != OK) return status;
87 (*hidlParams)[i].key = key.string();
88 (*hidlParams)[i].value = value.string();
89 }
90 return OK;
91}
92
93// static
94void ConversionHelperHidl::parametersToHal(
95 const hidl_vec<ParameterValue>& parameters, String8 *values) {
96 AudioParameter params;
97 for (size_t i = 0; i < parameters.size(); ++i) {
98 params.add(String8(parameters[i].key.c_str()), String8(parameters[i].value.c_str()));
99 }
100 values->setTo(params.toString());
101}
102
103ConversionHelperHidl::ConversionHelperHidl(const char* className)
104 : mClassName(className) {
105}
106
107// static
108status_t ConversionHelperHidl::analyzeResult(const Result& result) {
109 switch (result) {
110 case Result::OK: return OK;
111 case Result::INVALID_ARGUMENTS: return BAD_VALUE;
112 case Result::INVALID_STATE: return NOT_ENOUGH_DATA;
113 case Result::NOT_INITIALIZED: return NO_INIT;
114 case Result::NOT_SUPPORTED: return INVALID_OPERATION;
115 default: return NO_INIT;
116 }
117}
118
119void ConversionHelperHidl::emitError(const char* funcName, const char* description) {
120 ALOGE("%s %p %s: %s (from rpc)", mClassName, this, funcName, description);
121}
122
Kevin Rocard3d48dce2018-11-08 17:16:57 -0800123#if MAJOR_VERSION >= 4
jiabin9ff780e2018-03-19 18:19:52 -0700124// TODO: Use the same implementation in the hal when it moves to a util library.
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700125static std::string deviceAddressToHal(const DeviceAddress& address) {
jiabin9ff780e2018-03-19 18:19:52 -0700126 // HAL assumes that the address is NUL-terminated.
127 char halAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
128 memset(halAddress, 0, sizeof(halAddress));
129 audio_devices_t halDevice = static_cast<audio_devices_t>(address.device);
jiabindaf49952019-11-22 14:10:57 -0800130 if (getAudioDeviceOutAllA2dpSet().count(halDevice) > 0 ||
131 halDevice == AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
jiabin9ff780e2018-03-19 18:19:52 -0700132 snprintf(halAddress, sizeof(halAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
133 address.address.mac[0], address.address.mac[1], address.address.mac[2],
134 address.address.mac[3], address.address.mac[4], address.address.mac[5]);
jiabindaf49952019-11-22 14:10:57 -0800135 } else if (halDevice == AUDIO_DEVICE_OUT_IP || halDevice == AUDIO_DEVICE_IN_IP) {
jiabin9ff780e2018-03-19 18:19:52 -0700136 snprintf(halAddress, sizeof(halAddress), "%d.%d.%d.%d", address.address.ipv4[0],
137 address.address.ipv4[1], address.address.ipv4[2], address.address.ipv4[3]);
jiabindaf49952019-11-22 14:10:57 -0800138 } else if (getAudioDeviceOutAllUsbSet().count(halDevice) > 0 ||
139 getAudioDeviceInAllUsbSet().count(halDevice) > 0) {
jiabin9ff780e2018-03-19 18:19:52 -0700140 snprintf(halAddress, sizeof(halAddress), "card=%d;device=%d", address.address.alsa.card,
141 address.address.alsa.device);
jiabindaf49952019-11-22 14:10:57 -0800142 } else if (halDevice == AUDIO_DEVICE_OUT_BUS || halDevice == AUDIO_DEVICE_IN_BUS) {
jiabin9ff780e2018-03-19 18:19:52 -0700143 snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str());
jiabindaf49952019-11-22 14:10:57 -0800144 } else if (halDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
145 halDevice == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
jiabin9ff780e2018-03-19 18:19:52 -0700146 snprintf(halAddress, sizeof(halAddress), "%s", address.rSubmixAddress.c_str());
147 } else {
148 snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str());
149 }
150 return halAddress;
151}
152
153//local conversion helpers
154
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700155static audio_microphone_channel_mapping_t channelMappingToHal(AudioMicrophoneChannelMapping mapping) {
jiabin9ff780e2018-03-19 18:19:52 -0700156 switch (mapping) {
157 case AudioMicrophoneChannelMapping::UNUSED:
158 return AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
159 case AudioMicrophoneChannelMapping::DIRECT:
160 return AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT;
161 case AudioMicrophoneChannelMapping::PROCESSED:
162 return AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED;
163 default:
164 LOG_ALWAYS_FATAL("Unknown channelMappingToHal conversion %d", mapping);
165 }
166}
167
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700168static audio_microphone_location_t locationToHal(AudioMicrophoneLocation location) {
jiabin9ff780e2018-03-19 18:19:52 -0700169 switch (location) {
170 case AudioMicrophoneLocation::UNKNOWN:
171 return AUDIO_MICROPHONE_LOCATION_UNKNOWN;
172 case AudioMicrophoneLocation::MAINBODY:
173 return AUDIO_MICROPHONE_LOCATION_MAINBODY;
174 case AudioMicrophoneLocation::MAINBODY_MOVABLE:
175 return AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE;
176 case AudioMicrophoneLocation::PERIPHERAL:
177 return AUDIO_MICROPHONE_LOCATION_PERIPHERAL;
178 default:
179 LOG_ALWAYS_FATAL("Unknown locationToHal conversion %d", location);
180 }
181}
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700182static audio_microphone_directionality_t directionalityToHal(AudioMicrophoneDirectionality dir) {
jiabin9ff780e2018-03-19 18:19:52 -0700183 switch (dir) {
184 case AudioMicrophoneDirectionality::UNKNOWN:
185 return AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN;
186 case AudioMicrophoneDirectionality::OMNI:
187 return AUDIO_MICROPHONE_DIRECTIONALITY_OMNI;
188 case AudioMicrophoneDirectionality::BI_DIRECTIONAL:
189 return AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL;
190 case AudioMicrophoneDirectionality::CARDIOID:
191 return AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID;
192 case AudioMicrophoneDirectionality::HYPER_CARDIOID:
193 return AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID;
194 case AudioMicrophoneDirectionality::SUPER_CARDIOID:
195 return AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID;
196 default:
197 LOG_ALWAYS_FATAL("Unknown directionalityToHal conversion %d", dir);
198 }
199}
200
Kevin Rocard070e7512018-05-22 09:29:13 -0700201void microphoneInfoToHal(const MicrophoneInfo& src,
202 audio_microphone_characteristic_t *pDst) {
jiabin9ff780e2018-03-19 18:19:52 -0700203 if (pDst != NULL) {
204 snprintf(pDst->device_id, sizeof(pDst->device_id),
205 "%s", src.deviceId.c_str());
206 pDst->device = static_cast<audio_devices_t>(src.deviceAddress.device);
207 snprintf(pDst->address, sizeof(pDst->address),
208 "%s", deviceAddressToHal(src.deviceAddress).c_str());
209 if (src.channelMapping.size() > AUDIO_CHANNEL_COUNT_MAX) {
210 ALOGW("microphoneInfoToStruct found %zu channelMapping elements. Max expected is %d",
211 src.channelMapping.size(), AUDIO_CHANNEL_COUNT_MAX);
212 }
213 size_t ch;
214 for (ch = 0; ch < src.channelMapping.size() && ch < AUDIO_CHANNEL_COUNT_MAX; ch++) {
215 pDst->channel_mapping[ch] = channelMappingToHal(src.channelMapping[ch]);
216 }
217 for (; ch < AUDIO_CHANNEL_COUNT_MAX; ch++) {
218 pDst->channel_mapping[ch] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
219 }
220 pDst->location = locationToHal(src.location);
221 pDst->group = (audio_microphone_group_t)src.group;
222 pDst->index_in_the_group = (unsigned int)src.indexInTheGroup;
223 pDst->sensitivity = src.sensitivity;
224 pDst->max_spl = src.maxSpl;
225 pDst->min_spl = src.minSpl;
226 pDst->directionality = directionalityToHal(src.directionality);
227 pDst->num_frequency_responses = (unsigned int)src.frequencyResponse.size();
228 if (pDst->num_frequency_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
229 ALOGW("microphoneInfoToStruct found %d frequency responses. Max expected is %d",
230 pDst->num_frequency_responses, AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES);
231 pDst->num_frequency_responses = AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES;
232 }
233 for (size_t k = 0; k < pDst->num_frequency_responses; k++) {
234 pDst->frequency_responses[0][k] = src.frequencyResponse[k].frequency;
235 pDst->frequency_responses[1][k] = src.frequencyResponse[k].level;
236 }
237 pDst->geometric_location.x = src.position.x;
238 pDst->geometric_location.y = src.position.y;
239 pDst->geometric_location.z = src.position.z;
240 pDst->orientation.x = src.orientation.x;
241 pDst->orientation.y = src.orientation.y;
242 pDst->orientation.z = src.orientation.z;
243 }
244}
Kevin Rocard070e7512018-05-22 09:29:13 -0700245#endif
jiabin9ff780e2018-03-19 18:19:52 -0700246
Kevin Rocard070e7512018-05-22 09:29:13 -0700247} // namespace CPP_VERSION
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800248} // namespace android