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