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