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 <stdio.h> |
| 18 | |
| 19 | #define LOG_TAG "DeviceHalHidl" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
Kevin Rocard | 95213bf | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 22 | #include PATH(android/hardware/audio/FILE_VERSION/IPrimaryDevice.h) |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 23 | #include <cutils/native_handle.h> |
| 24 | #include <hwbinder/IPCThreadState.h> |
jiabin | daf4995 | 2019-11-22 14:10:57 -0800 | [diff] [blame] | 25 | #include <media/AudioContainers.h> |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 28 | #include <common/all-versions/VersionUtils.h> |
| 29 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 30 | #include "DeviceHalHidl.h" |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 31 | #include "EffectHalHidl.h" |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 32 | #include "HidlUtils.h" |
| 33 | #include "StreamHalHidl.h" |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 34 | #include "VersionUtils.h" |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 35 | |
Mikhail Naganov | 9ccaa16 | 2018-12-12 10:27:29 -0800 | [diff] [blame] | 36 | using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils; |
Kevin Rocard | 7a9f05a | 2018-11-28 16:52:25 -0800 | [diff] [blame] | 37 | using ::android::hardware::audio::common::utils::EnumBitfield; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 38 | using ::android::hardware::hidl_string; |
| 39 | using ::android::hardware::hidl_vec; |
| 40 | |
| 41 | namespace android { |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 42 | namespace CPP_VERSION { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 43 | |
Mikhail Naganov | 9ccaa16 | 2018-12-12 10:27:29 -0800 | [diff] [blame] | 44 | using namespace ::android::hardware::audio::common::CPP_VERSION; |
| 45 | using namespace ::android::hardware::audio::CPP_VERSION; |
| 46 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 47 | using EffectHalHidl = ::android::effect::CPP_VERSION::EffectHalHidl; |
| 48 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 49 | namespace { |
| 50 | |
Ytai Ben-Tsvi | 0789286 | 2020-11-06 10:09:52 -0800 | [diff] [blame] | 51 | using ::android::hardware::audio::common::CPP_VERSION::AudioPort; |
| 52 | using ::android::hardware::audio::common::CPP_VERSION::AudioPortConfig; |
| 53 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 54 | status_t deviceAddressFromHal( |
| 55 | audio_devices_t device, const char* halAddress, DeviceAddress* address) { |
| 56 | address->device = AudioDevice(device); |
| 57 | |
Kevin Rocard | 5915fa3 | 2018-03-29 10:32:44 -0700 | [diff] [blame] | 58 | if (halAddress == nullptr || strnlen(halAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 59 | return OK; |
| 60 | } |
jiabin | daf4995 | 2019-11-22 14:10:57 -0800 | [diff] [blame] | 61 | if (getAudioDeviceOutAllA2dpSet().count(device) > 0 |
| 62 | || device == AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 63 | int status = sscanf(halAddress, |
| 64 | "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", |
| 65 | &address->address.mac[0], &address->address.mac[1], &address->address.mac[2], |
| 66 | &address->address.mac[3], &address->address.mac[4], &address->address.mac[5]); |
| 67 | return status == 6 ? OK : BAD_VALUE; |
jiabin | daf4995 | 2019-11-22 14:10:57 -0800 | [diff] [blame] | 68 | } else if (device == AUDIO_DEVICE_OUT_IP || device == AUDIO_DEVICE_IN_IP) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 69 | int status = sscanf(halAddress, |
| 70 | "%hhu.%hhu.%hhu.%hhu", |
| 71 | &address->address.ipv4[0], &address->address.ipv4[1], |
| 72 | &address->address.ipv4[2], &address->address.ipv4[3]); |
| 73 | return status == 4 ? OK : BAD_VALUE; |
jiabin | daf4995 | 2019-11-22 14:10:57 -0800 | [diff] [blame] | 74 | } else if (getAudioDeviceOutAllUsbSet().count(device) > 0 |
| 75 | || getAudioDeviceInAllUsbSet().count(device) > 0) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 76 | int status = sscanf(halAddress, |
| 77 | "card=%d;device=%d", |
| 78 | &address->address.alsa.card, &address->address.alsa.device); |
| 79 | return status == 2 ? OK : BAD_VALUE; |
jiabin | daf4995 | 2019-11-22 14:10:57 -0800 | [diff] [blame] | 80 | } else if (device == AUDIO_DEVICE_OUT_BUS || device == AUDIO_DEVICE_IN_BUS) { |
| 81 | address->busAddress = halAddress; |
| 82 | return OK; |
| 83 | } else if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
| 84 | || device == AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
| 85 | address->rSubmixAddress = halAddress; |
| 86 | return OK; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 87 | } |
| 88 | return OK; |
| 89 | } |
| 90 | |
| 91 | } // namespace |
| 92 | |
| 93 | DeviceHalHidl::DeviceHalHidl(const sp<IDevice>& device) |
| 94 | : ConversionHelperHidl("Device"), mDevice(device), |
| 95 | mPrimaryDevice(IPrimaryDevice::castFrom(device)) { |
| 96 | } |
| 97 | |
| 98 | DeviceHalHidl::~DeviceHalHidl() { |
| 99 | if (mDevice != 0) { |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 100 | #if MAJOR_VERSION <= 5 |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 101 | mDevice.clear(); |
| 102 | hardware::IPCThreadState::self()->flushCommands(); |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 103 | #elif MAJOR_VERSION >= 6 |
| 104 | mDevice->close(); |
| 105 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | status_t DeviceHalHidl::getSupportedDevices(uint32_t*) { |
| 110 | // Obsolete. |
| 111 | return INVALID_OPERATION; |
| 112 | } |
| 113 | |
| 114 | status_t DeviceHalHidl::initCheck() { |
| 115 | if (mDevice == 0) return NO_INIT; |
| 116 | return processReturn("initCheck", mDevice->initCheck()); |
| 117 | } |
| 118 | |
| 119 | status_t DeviceHalHidl::setVoiceVolume(float volume) { |
| 120 | if (mDevice == 0) return NO_INIT; |
| 121 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 122 | return processReturn("setVoiceVolume", mPrimaryDevice->setVoiceVolume(volume)); |
| 123 | } |
| 124 | |
| 125 | status_t DeviceHalHidl::setMasterVolume(float volume) { |
| 126 | if (mDevice == 0) return NO_INIT; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 127 | return processReturn("setMasterVolume", mDevice->setMasterVolume(volume)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | status_t DeviceHalHidl::getMasterVolume(float *volume) { |
| 131 | if (mDevice == 0) return NO_INIT; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 132 | Result retval; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 133 | Return<void> ret = mDevice->getMasterVolume( |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 134 | [&](Result r, float v) { |
| 135 | retval = r; |
| 136 | if (retval == Result::OK) { |
| 137 | *volume = v; |
| 138 | } |
| 139 | }); |
| 140 | return processReturn("getMasterVolume", ret, retval); |
| 141 | } |
| 142 | |
| 143 | status_t DeviceHalHidl::setMode(audio_mode_t mode) { |
| 144 | if (mDevice == 0) return NO_INIT; |
| 145 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 146 | return processReturn("setMode", mPrimaryDevice->setMode(AudioMode(mode))); |
| 147 | } |
| 148 | |
| 149 | status_t DeviceHalHidl::setMicMute(bool state) { |
| 150 | if (mDevice == 0) return NO_INIT; |
| 151 | return processReturn("setMicMute", mDevice->setMicMute(state)); |
| 152 | } |
| 153 | |
| 154 | status_t DeviceHalHidl::getMicMute(bool *state) { |
| 155 | if (mDevice == 0) return NO_INIT; |
| 156 | Result retval; |
| 157 | Return<void> ret = mDevice->getMicMute( |
| 158 | [&](Result r, bool mute) { |
| 159 | retval = r; |
| 160 | if (retval == Result::OK) { |
| 161 | *state = mute; |
| 162 | } |
| 163 | }); |
| 164 | return processReturn("getMicMute", ret, retval); |
| 165 | } |
| 166 | |
| 167 | status_t DeviceHalHidl::setMasterMute(bool state) { |
| 168 | if (mDevice == 0) return NO_INIT; |
| 169 | return processReturn("setMasterMute", mDevice->setMasterMute(state)); |
| 170 | } |
| 171 | |
| 172 | status_t DeviceHalHidl::getMasterMute(bool *state) { |
| 173 | if (mDevice == 0) return NO_INIT; |
| 174 | Result retval; |
| 175 | Return<void> ret = mDevice->getMasterMute( |
| 176 | [&](Result r, bool mute) { |
| 177 | retval = r; |
| 178 | if (retval == Result::OK) { |
| 179 | *state = mute; |
| 180 | } |
| 181 | }); |
| 182 | return processReturn("getMasterMute", ret, retval); |
| 183 | } |
| 184 | |
| 185 | status_t DeviceHalHidl::setParameters(const String8& kvPairs) { |
| 186 | if (mDevice == 0) return NO_INIT; |
| 187 | hidl_vec<ParameterValue> hidlParams; |
| 188 | status_t status = parametersFromHal(kvPairs, &hidlParams); |
| 189 | if (status != OK) return status; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 190 | // TODO: change the API so that context and kvPairs are separated |
| 191 | return processReturn("setParameters", |
| 192 | utils::setParameters(mDevice, {} /* context */, hidlParams)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | status_t DeviceHalHidl::getParameters(const String8& keys, String8 *values) { |
| 196 | values->clear(); |
| 197 | if (mDevice == 0) return NO_INIT; |
| 198 | hidl_vec<hidl_string> hidlKeys; |
| 199 | status_t status = keysFromHal(keys, &hidlKeys); |
| 200 | if (status != OK) return status; |
| 201 | Result retval; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 202 | Return<void> ret = utils::getParameters(mDevice, |
| 203 | {} /* context */, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 204 | hidlKeys, |
| 205 | [&](Result r, const hidl_vec<ParameterValue>& parameters) { |
| 206 | retval = r; |
| 207 | if (retval == Result::OK) { |
| 208 | parametersToHal(parameters, values); |
| 209 | } |
| 210 | }); |
| 211 | return processReturn("getParameters", ret, retval); |
| 212 | } |
| 213 | |
| 214 | status_t DeviceHalHidl::getInputBufferSize( |
| 215 | const struct audio_config *config, size_t *size) { |
| 216 | if (mDevice == 0) return NO_INIT; |
| 217 | AudioConfig hidlConfig; |
Mikhail Naganov | 05e0319 | 2020-12-14 23:19:54 +0000 | [diff] [blame] | 218 | HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 219 | Result retval; |
| 220 | Return<void> ret = mDevice->getInputBufferSize( |
| 221 | hidlConfig, |
| 222 | [&](Result r, uint64_t bufferSize) { |
| 223 | retval = r; |
| 224 | if (retval == Result::OK) { |
| 225 | *size = static_cast<size_t>(bufferSize); |
| 226 | } |
| 227 | }); |
| 228 | return processReturn("getInputBufferSize", ret, retval); |
| 229 | } |
| 230 | |
| 231 | status_t DeviceHalHidl::openOutputStream( |
| 232 | audio_io_handle_t handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 233 | audio_devices_t deviceType, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 234 | audio_output_flags_t flags, |
| 235 | struct audio_config *config, |
| 236 | const char *address, |
| 237 | sp<StreamOutHalInterface> *outStream) { |
| 238 | if (mDevice == 0) return NO_INIT; |
| 239 | DeviceAddress hidlDevice; |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 240 | status_t status = deviceAddressFromHal(deviceType, address, &hidlDevice); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 241 | if (status != OK) return status; |
| 242 | AudioConfig hidlConfig; |
Mikhail Naganov | 05e0319 | 2020-12-14 23:19:54 +0000 | [diff] [blame] | 243 | HidlUtils::audioConfigFromHal(*config, false /*isInput*/, &hidlConfig); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 244 | Result retval = Result::NOT_INITIALIZED; |
| 245 | Return<void> ret = mDevice->openOutputStream( |
| 246 | handle, |
| 247 | hidlDevice, |
| 248 | hidlConfig, |
Kevin Rocard | 7a9f05a | 2018-11-28 16:52:25 -0800 | [diff] [blame] | 249 | EnumBitfield<AudioOutputFlag>(flags), |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 250 | #if MAJOR_VERSION >= 4 |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 251 | {} /* metadata */, |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 252 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 253 | [&](Result r, const sp<IStreamOut>& result, const AudioConfig& suggestedConfig) { |
| 254 | retval = r; |
| 255 | if (retval == Result::OK) { |
| 256 | *outStream = new StreamOutHalHidl(result); |
| 257 | } |
| 258 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 259 | }); |
| 260 | return processReturn("openOutputStream", ret, retval); |
| 261 | } |
| 262 | |
| 263 | status_t DeviceHalHidl::openInputStream( |
| 264 | audio_io_handle_t handle, |
| 265 | audio_devices_t devices, |
| 266 | struct audio_config *config, |
| 267 | audio_input_flags_t flags, |
| 268 | const char *address, |
| 269 | audio_source_t source, |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 270 | audio_devices_t outputDevice, |
| 271 | const char *outputDeviceAddress, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 272 | sp<StreamInHalInterface> *inStream) { |
| 273 | if (mDevice == 0) return NO_INIT; |
| 274 | DeviceAddress hidlDevice; |
| 275 | status_t status = deviceAddressFromHal(devices, address, &hidlDevice); |
| 276 | if (status != OK) return status; |
| 277 | AudioConfig hidlConfig; |
Mikhail Naganov | 05e0319 | 2020-12-14 23:19:54 +0000 | [diff] [blame] | 278 | HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 279 | Result retval = Result::NOT_INITIALIZED; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 280 | #if MAJOR_VERSION == 2 |
Mikhail Naganov | d9499eb | 2018-12-17 16:23:22 -0800 | [diff] [blame] | 281 | auto sinkMetadata = AudioSource(source); |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 282 | #elif MAJOR_VERSION >= 4 |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 283 | // TODO: correctly propagate the tracks sources and volume |
| 284 | // for now, only send the main source at 1dbfs |
Mikhail Naganov | d9499eb | 2018-12-17 16:23:22 -0800 | [diff] [blame] | 285 | SinkMetadata sinkMetadata = {{{ .source = AudioSource(source), .gain = 1 }}}; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 286 | #endif |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 287 | #if MAJOR_VERSION < 5 |
| 288 | (void)outputDevice; |
| 289 | (void)outputDeviceAddress; |
| 290 | #else |
| 291 | if (outputDevice != AUDIO_DEVICE_NONE) { |
| 292 | DeviceAddress hidlOutputDevice; |
| 293 | status = deviceAddressFromHal(outputDevice, outputDeviceAddress, &hidlOutputDevice); |
| 294 | if (status != OK) return status; |
| 295 | sinkMetadata.tracks[0].destination.device(std::move(hidlOutputDevice)); |
| 296 | } |
| 297 | #endif |
Mikhail Naganov | bad1510 | 2019-12-10 16:44:19 -0800 | [diff] [blame] | 298 | #if MAJOR_VERSION <= 5 |
| 299 | // Some flags were specific to framework and must not leak to the HAL. |
| 300 | flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FLAG_DIRECT); |
| 301 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 302 | Return<void> ret = mDevice->openInputStream( |
| 303 | handle, |
| 304 | hidlDevice, |
| 305 | hidlConfig, |
Kevin Rocard | 7a9f05a | 2018-11-28 16:52:25 -0800 | [diff] [blame] | 306 | EnumBitfield<AudioInputFlag>(flags), |
Mikhail Naganov | d9499eb | 2018-12-17 16:23:22 -0800 | [diff] [blame] | 307 | sinkMetadata, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 308 | [&](Result r, const sp<IStreamIn>& result, const AudioConfig& suggestedConfig) { |
| 309 | retval = r; |
| 310 | if (retval == Result::OK) { |
| 311 | *inStream = new StreamInHalHidl(result); |
| 312 | } |
| 313 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 314 | }); |
| 315 | return processReturn("openInputStream", ret, retval); |
| 316 | } |
| 317 | |
| 318 | status_t DeviceHalHidl::supportsAudioPatches(bool *supportsPatches) { |
| 319 | if (mDevice == 0) return NO_INIT; |
| 320 | return processReturn("supportsAudioPatches", mDevice->supportsAudioPatches(), supportsPatches); |
| 321 | } |
| 322 | |
| 323 | status_t DeviceHalHidl::createAudioPatch( |
| 324 | unsigned int num_sources, |
| 325 | const struct audio_port_config *sources, |
| 326 | unsigned int num_sinks, |
| 327 | const struct audio_port_config *sinks, |
| 328 | audio_patch_handle_t *patch) { |
| 329 | if (mDevice == 0) return NO_INIT; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 330 | if (patch == nullptr) return BAD_VALUE; |
| 331 | |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 332 | #if MAJOR_VERSION < 6 |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 333 | if (*patch != AUDIO_PATCH_HANDLE_NONE) { |
| 334 | status_t status = releaseAudioPatch(*patch); |
| 335 | ALOGW_IF(status != NO_ERROR, "%s error %d releasing patch handle %d", |
| 336 | __func__, status, *patch); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 337 | *patch = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 338 | } |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 339 | #endif |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 340 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 341 | hidl_vec<AudioPortConfig> hidlSources, hidlSinks; |
| 342 | HidlUtils::audioPortConfigsFromHal(num_sources, sources, &hidlSources); |
| 343 | HidlUtils::audioPortConfigsFromHal(num_sinks, sinks, &hidlSinks); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 344 | Result retval = Result::OK; |
| 345 | Return<void> ret; |
| 346 | std::string methodName = "createAudioPatch"; |
| 347 | if (*patch == AUDIO_PATCH_HANDLE_NONE) { // always true for MAJOR_VERSION < 6 |
| 348 | ret = mDevice->createAudioPatch( |
| 349 | hidlSources, hidlSinks, |
| 350 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 351 | retval = r; |
| 352 | if (retval == Result::OK) { |
| 353 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 354 | } |
| 355 | }); |
| 356 | } else { |
| 357 | #if MAJOR_VERSION >= 6 |
| 358 | ret = mDevice->updateAudioPatch( |
| 359 | *patch, |
| 360 | hidlSources, hidlSinks, |
| 361 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 362 | retval = r; |
| 363 | if (retval == Result::OK) { |
| 364 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 365 | } |
| 366 | }); |
| 367 | methodName = "updateAudioPatch"; |
| 368 | #endif |
| 369 | } |
| 370 | return processReturn(methodName.c_str(), ret, retval); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | status_t DeviceHalHidl::releaseAudioPatch(audio_patch_handle_t patch) { |
| 374 | if (mDevice == 0) return NO_INIT; |
| 375 | return processReturn("releaseAudioPatch", mDevice->releaseAudioPatch(patch)); |
| 376 | } |
| 377 | |
| 378 | status_t DeviceHalHidl::getAudioPort(struct audio_port *port) { |
| 379 | if (mDevice == 0) return NO_INIT; |
| 380 | AudioPort hidlPort; |
| 381 | HidlUtils::audioPortFromHal(*port, &hidlPort); |
| 382 | Result retval; |
| 383 | Return<void> ret = mDevice->getAudioPort( |
| 384 | hidlPort, |
| 385 | [&](Result r, const AudioPort& p) { |
| 386 | retval = r; |
| 387 | if (retval == Result::OK) { |
| 388 | HidlUtils::audioPortToHal(p, port); |
| 389 | } |
| 390 | }); |
| 391 | return processReturn("getAudioPort", ret, retval); |
| 392 | } |
| 393 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 394 | status_t DeviceHalHidl::getAudioPort(struct audio_port_v7 *port) { |
| 395 | if (mDevice == 0) return NO_INIT; |
| 396 | status_t status = NO_ERROR; |
| 397 | #if MAJOR_VERSION >= 7 |
| 398 | AudioPort hidlPort; |
| 399 | HidlUtils::audioPortFromHal(*port, &hidlPort); |
| 400 | Result retval; |
| 401 | Return<void> ret = mDevice->getAudioPort( |
| 402 | hidlPort, |
| 403 | [&](Result r, const AudioPort& p) { |
| 404 | retval = r; |
| 405 | if (retval == Result::OK) { |
| 406 | HidlUtils::audioPortToHal(p, port); |
| 407 | } |
| 408 | }); |
| 409 | status = processReturn("getAudioPort", ret, retval); |
| 410 | #else |
| 411 | struct audio_port audioPort = {}; |
| 412 | audio_populate_audio_port(port, &audioPort); |
| 413 | status = getAudioPort(&audioPort); |
| 414 | if (status == NO_ERROR) { |
| 415 | audio_populate_audio_port_v7(&audioPort, port); |
| 416 | } |
| 417 | #endif |
| 418 | return status; |
| 419 | } |
| 420 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 421 | status_t DeviceHalHidl::setAudioPortConfig(const struct audio_port_config *config) { |
| 422 | if (mDevice == 0) return NO_INIT; |
| 423 | AudioPortConfig hidlConfig; |
| 424 | HidlUtils::audioPortConfigFromHal(*config, &hidlConfig); |
| 425 | return processReturn("setAudioPortConfig", mDevice->setAudioPortConfig(hidlConfig)); |
| 426 | } |
| 427 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 428 | #if MAJOR_VERSION == 2 |
| 429 | status_t DeviceHalHidl::getMicrophones( |
| 430 | std::vector<media::MicrophoneInfo> *microphonesInfo __unused) { |
| 431 | if (mDevice == 0) return NO_INIT; |
| 432 | return INVALID_OPERATION; |
| 433 | } |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 434 | #elif MAJOR_VERSION >= 4 |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 435 | status_t DeviceHalHidl::getMicrophones(std::vector<media::MicrophoneInfo> *microphonesInfo) { |
| 436 | if (mDevice == 0) return NO_INIT; |
| 437 | Result retval; |
| 438 | Return<void> ret = mDevice->getMicrophones( |
| 439 | [&](Result r, hidl_vec<MicrophoneInfo> micArrayHal) { |
| 440 | retval = r; |
| 441 | for (size_t k = 0; k < micArrayHal.size(); k++) { |
| 442 | audio_microphone_characteristic_t dst; |
| 443 | //convert |
| 444 | microphoneInfoToHal(micArrayHal[k], &dst); |
| 445 | media::MicrophoneInfo microphone = media::MicrophoneInfo(dst); |
| 446 | microphonesInfo->push_back(microphone); |
| 447 | } |
| 448 | }); |
| 449 | return processReturn("getMicrophones", ret, retval); |
| 450 | } |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 451 | #endif |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 452 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 453 | #if MAJOR_VERSION >= 6 |
| 454 | status_t DeviceHalHidl::addDeviceEffect( |
| 455 | audio_port_handle_t device, sp<EffectHalInterface> effect) { |
| 456 | if (mDevice == 0) return NO_INIT; |
| 457 | return processReturn("addDeviceEffect", mDevice->addDeviceEffect( |
| 458 | static_cast<AudioPortHandle>(device), |
| 459 | static_cast<EffectHalHidl*>(effect.get())->effectId())); |
| 460 | } |
| 461 | #else |
| 462 | status_t DeviceHalHidl::addDeviceEffect( |
| 463 | audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) { |
| 464 | return INVALID_OPERATION; |
| 465 | } |
| 466 | #endif |
| 467 | |
| 468 | #if MAJOR_VERSION >= 6 |
| 469 | status_t DeviceHalHidl::removeDeviceEffect( |
| 470 | audio_port_handle_t device, sp<EffectHalInterface> effect) { |
| 471 | if (mDevice == 0) return NO_INIT; |
| 472 | return processReturn("removeDeviceEffect", mDevice->removeDeviceEffect( |
| 473 | static_cast<AudioPortHandle>(device), |
| 474 | static_cast<EffectHalHidl*>(effect.get())->effectId())); |
| 475 | } |
| 476 | #else |
| 477 | status_t DeviceHalHidl::removeDeviceEffect( |
| 478 | audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) { |
| 479 | return INVALID_OPERATION; |
| 480 | } |
| 481 | #endif |
| 482 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 483 | status_t DeviceHalHidl::dump(int fd) { |
| 484 | if (mDevice == 0) return NO_INIT; |
| 485 | native_handle_t* hidlHandle = native_handle_create(1, 0); |
| 486 | hidlHandle->data[0] = fd; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 487 | Return<void> ret = mDevice->debug(hidlHandle, {} /* options */); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 488 | native_handle_delete(hidlHandle); |
| 489 | return processReturn("dump", ret); |
| 490 | } |
| 491 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 492 | } // namespace CPP_VERSION |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 493 | } // namespace android |