Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 1 | /* |
| 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" |
| 20 | #include <media/AudioParameter.h> |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include "ConversionHelperHidl.h" |
| 24 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 25 | namespace android { |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 26 | namespace CPP_VERSION { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 27 | |
Mikhail Naganov | 04395fa | 2018-12-14 15:07:26 -0800 | [diff] [blame] | 28 | using namespace ::android::hardware::audio::common::CPP_VERSION; |
| 29 | using namespace ::android::hardware::audio::CPP_VERSION; |
| 30 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 31 | // static |
| 32 | status_t ConversionHelperHidl::keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys) { |
| 33 | AudioParameter halKeys(keys); |
| 34 | if (halKeys.size() == 0) return BAD_VALUE; |
| 35 | hidlKeys->resize(halKeys.size()); |
| 36 | //FIXME: keyStreamSupportedChannels and keyStreamSupportedSamplingRates come with a |
| 37 | // "keyFormat=<value>" pair. We need to transform it into a single key string so that it is |
| 38 | // carried over to the legacy HAL via HIDL. |
| 39 | String8 value; |
| 40 | bool keepFormatValue = halKeys.size() == 2 && |
| 41 | (halKeys.get(String8(AudioParameter::keyStreamSupportedChannels), value) == NO_ERROR || |
| 42 | halKeys.get(String8(AudioParameter::keyStreamSupportedSamplingRates), value) == NO_ERROR); |
| 43 | |
| 44 | for (size_t i = 0; i < halKeys.size(); ++i) { |
| 45 | String8 key; |
| 46 | status_t status = halKeys.getAt(i, key); |
| 47 | if (status != OK) return status; |
| 48 | if (keepFormatValue && key == AudioParameter::keyFormat) { |
| 49 | AudioParameter formatParam; |
| 50 | halKeys.getAt(i, key, value); |
| 51 | formatParam.add(key, value); |
| 52 | key = formatParam.toString(); |
| 53 | } |
| 54 | (*hidlKeys)[i] = key.string(); |
| 55 | } |
| 56 | return OK; |
| 57 | } |
| 58 | |
| 59 | // static |
| 60 | status_t ConversionHelperHidl::parametersFromHal( |
| 61 | const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams) { |
| 62 | AudioParameter params(kvPairs); |
| 63 | if (params.size() == 0) return BAD_VALUE; |
| 64 | hidlParams->resize(params.size()); |
| 65 | for (size_t i = 0; i < params.size(); ++i) { |
| 66 | String8 key, value; |
| 67 | status_t status = params.getAt(i, key, value); |
| 68 | if (status != OK) return status; |
| 69 | (*hidlParams)[i].key = key.string(); |
| 70 | (*hidlParams)[i].value = value.string(); |
| 71 | } |
| 72 | return OK; |
| 73 | } |
| 74 | |
| 75 | // static |
| 76 | void ConversionHelperHidl::parametersToHal( |
| 77 | const hidl_vec<ParameterValue>& parameters, String8 *values) { |
| 78 | AudioParameter params; |
| 79 | for (size_t i = 0; i < parameters.size(); ++i) { |
| 80 | params.add(String8(parameters[i].key.c_str()), String8(parameters[i].value.c_str())); |
| 81 | } |
| 82 | values->setTo(params.toString()); |
| 83 | } |
| 84 | |
| 85 | ConversionHelperHidl::ConversionHelperHidl(const char* className) |
| 86 | : mClassName(className) { |
| 87 | } |
| 88 | |
| 89 | // static |
| 90 | status_t ConversionHelperHidl::analyzeResult(const Result& result) { |
| 91 | switch (result) { |
| 92 | case Result::OK: return OK; |
| 93 | case Result::INVALID_ARGUMENTS: return BAD_VALUE; |
| 94 | case Result::INVALID_STATE: return NOT_ENOUGH_DATA; |
| 95 | case Result::NOT_INITIALIZED: return NO_INIT; |
| 96 | case Result::NOT_SUPPORTED: return INVALID_OPERATION; |
| 97 | default: return NO_INIT; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void ConversionHelperHidl::emitError(const char* funcName, const char* description) { |
| 102 | ALOGE("%s %p %s: %s (from rpc)", mClassName, this, funcName, description); |
| 103 | } |
| 104 | |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 105 | #if MAJOR_VERSION >= 4 |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 106 | // TODO: Use the same implementation in the hal when it moves to a util library. |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 107 | static std::string deviceAddressToHal(const DeviceAddress& address) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 108 | // HAL assumes that the address is NUL-terminated. |
| 109 | char halAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN]; |
| 110 | memset(halAddress, 0, sizeof(halAddress)); |
| 111 | audio_devices_t halDevice = static_cast<audio_devices_t>(address.device); |
| 112 | const bool isInput = (halDevice & AUDIO_DEVICE_BIT_IN) != 0; |
| 113 | if (isInput) halDevice &= ~AUDIO_DEVICE_BIT_IN; |
| 114 | if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) || |
| 115 | (isInput && (halDevice & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) != 0)) { |
| 116 | snprintf(halAddress, sizeof(halAddress), "%02X:%02X:%02X:%02X:%02X:%02X", |
| 117 | address.address.mac[0], address.address.mac[1], address.address.mac[2], |
| 118 | address.address.mac[3], address.address.mac[4], address.address.mac[5]); |
| 119 | } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_IP) != 0) || |
| 120 | (isInput && (halDevice & AUDIO_DEVICE_IN_IP) != 0)) { |
| 121 | snprintf(halAddress, sizeof(halAddress), "%d.%d.%d.%d", address.address.ipv4[0], |
| 122 | address.address.ipv4[1], address.address.ipv4[2], address.address.ipv4[3]); |
| 123 | } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_ALL_USB) != 0) || |
| 124 | (isInput && (halDevice & AUDIO_DEVICE_IN_ALL_USB) != 0)) { |
| 125 | snprintf(halAddress, sizeof(halAddress), "card=%d;device=%d", address.address.alsa.card, |
| 126 | address.address.alsa.device); |
| 127 | } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_BUS) != 0) || |
| 128 | (isInput && (halDevice & AUDIO_DEVICE_IN_BUS) != 0)) { |
| 129 | snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str()); |
| 130 | } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) != 0 || |
| 131 | (isInput && (halDevice & AUDIO_DEVICE_IN_REMOTE_SUBMIX) != 0)) { |
| 132 | snprintf(halAddress, sizeof(halAddress), "%s", address.rSubmixAddress.c_str()); |
| 133 | } else { |
| 134 | snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str()); |
| 135 | } |
| 136 | return halAddress; |
| 137 | } |
| 138 | |
| 139 | //local conversion helpers |
| 140 | |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 141 | static audio_microphone_channel_mapping_t channelMappingToHal(AudioMicrophoneChannelMapping mapping) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 142 | switch (mapping) { |
| 143 | case AudioMicrophoneChannelMapping::UNUSED: |
| 144 | return AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED; |
| 145 | case AudioMicrophoneChannelMapping::DIRECT: |
| 146 | return AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT; |
| 147 | case AudioMicrophoneChannelMapping::PROCESSED: |
| 148 | return AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED; |
| 149 | default: |
| 150 | LOG_ALWAYS_FATAL("Unknown channelMappingToHal conversion %d", mapping); |
| 151 | } |
| 152 | } |
| 153 | |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 154 | static audio_microphone_location_t locationToHal(AudioMicrophoneLocation location) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 155 | switch (location) { |
| 156 | case AudioMicrophoneLocation::UNKNOWN: |
| 157 | return AUDIO_MICROPHONE_LOCATION_UNKNOWN; |
| 158 | case AudioMicrophoneLocation::MAINBODY: |
| 159 | return AUDIO_MICROPHONE_LOCATION_MAINBODY; |
| 160 | case AudioMicrophoneLocation::MAINBODY_MOVABLE: |
| 161 | return AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE; |
| 162 | case AudioMicrophoneLocation::PERIPHERAL: |
| 163 | return AUDIO_MICROPHONE_LOCATION_PERIPHERAL; |
| 164 | default: |
| 165 | LOG_ALWAYS_FATAL("Unknown locationToHal conversion %d", location); |
| 166 | } |
| 167 | } |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 168 | static audio_microphone_directionality_t directionalityToHal(AudioMicrophoneDirectionality dir) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 169 | switch (dir) { |
| 170 | case AudioMicrophoneDirectionality::UNKNOWN: |
| 171 | return AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN; |
| 172 | case AudioMicrophoneDirectionality::OMNI: |
| 173 | return AUDIO_MICROPHONE_DIRECTIONALITY_OMNI; |
| 174 | case AudioMicrophoneDirectionality::BI_DIRECTIONAL: |
| 175 | return AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL; |
| 176 | case AudioMicrophoneDirectionality::CARDIOID: |
| 177 | return AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID; |
| 178 | case AudioMicrophoneDirectionality::HYPER_CARDIOID: |
| 179 | return AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID; |
| 180 | case AudioMicrophoneDirectionality::SUPER_CARDIOID: |
| 181 | return AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID; |
| 182 | default: |
| 183 | LOG_ALWAYS_FATAL("Unknown directionalityToHal conversion %d", dir); |
| 184 | } |
| 185 | } |
| 186 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 187 | void microphoneInfoToHal(const MicrophoneInfo& src, |
| 188 | audio_microphone_characteristic_t *pDst) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 189 | if (pDst != NULL) { |
| 190 | snprintf(pDst->device_id, sizeof(pDst->device_id), |
| 191 | "%s", src.deviceId.c_str()); |
| 192 | pDst->device = static_cast<audio_devices_t>(src.deviceAddress.device); |
| 193 | snprintf(pDst->address, sizeof(pDst->address), |
| 194 | "%s", deviceAddressToHal(src.deviceAddress).c_str()); |
| 195 | if (src.channelMapping.size() > AUDIO_CHANNEL_COUNT_MAX) { |
| 196 | ALOGW("microphoneInfoToStruct found %zu channelMapping elements. Max expected is %d", |
| 197 | src.channelMapping.size(), AUDIO_CHANNEL_COUNT_MAX); |
| 198 | } |
| 199 | size_t ch; |
| 200 | for (ch = 0; ch < src.channelMapping.size() && ch < AUDIO_CHANNEL_COUNT_MAX; ch++) { |
| 201 | pDst->channel_mapping[ch] = channelMappingToHal(src.channelMapping[ch]); |
| 202 | } |
| 203 | for (; ch < AUDIO_CHANNEL_COUNT_MAX; ch++) { |
| 204 | pDst->channel_mapping[ch] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED; |
| 205 | } |
| 206 | pDst->location = locationToHal(src.location); |
| 207 | pDst->group = (audio_microphone_group_t)src.group; |
| 208 | pDst->index_in_the_group = (unsigned int)src.indexInTheGroup; |
| 209 | pDst->sensitivity = src.sensitivity; |
| 210 | pDst->max_spl = src.maxSpl; |
| 211 | pDst->min_spl = src.minSpl; |
| 212 | pDst->directionality = directionalityToHal(src.directionality); |
| 213 | pDst->num_frequency_responses = (unsigned int)src.frequencyResponse.size(); |
| 214 | if (pDst->num_frequency_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) { |
| 215 | ALOGW("microphoneInfoToStruct found %d frequency responses. Max expected is %d", |
| 216 | pDst->num_frequency_responses, AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES); |
| 217 | pDst->num_frequency_responses = AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES; |
| 218 | } |
| 219 | for (size_t k = 0; k < pDst->num_frequency_responses; k++) { |
| 220 | pDst->frequency_responses[0][k] = src.frequencyResponse[k].frequency; |
| 221 | pDst->frequency_responses[1][k] = src.frequencyResponse[k].level; |
| 222 | } |
| 223 | pDst->geometric_location.x = src.position.x; |
| 224 | pDst->geometric_location.y = src.position.y; |
| 225 | pDst->geometric_location.z = src.position.z; |
| 226 | pDst->orientation.x = src.orientation.x; |
| 227 | pDst->orientation.y = src.orientation.y; |
| 228 | pDst->orientation.z = src.orientation.z; |
| 229 | } |
| 230 | } |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 231 | #endif |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 232 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 233 | } // namespace CPP_VERSION |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 234 | } // namespace android |