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 | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 23 | #include <cutils/properties.h> |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 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 | |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 28 | #include PATH(android/hardware/audio/FILE_VERSION/IPrimaryDevice.h) |
| 29 | #include <HidlUtils.h> |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 30 | #include <common/all-versions/VersionUtils.h> |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 31 | #include <util/CoreUtils.h> |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 32 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 33 | #include "DeviceHalHidl.h" |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 34 | #include "ParameterUtils.h" |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 35 | #include "StreamHalHidl.h" |
| 36 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 37 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils; |
Kevin Rocard | 7a9f05a | 2018-11-28 16:52:25 -0800 | [diff] [blame] | 38 | using ::android::hardware::audio::common::utils::EnumBitfield; |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 39 | using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::implementation::CoreUtils; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 40 | using ::android::hardware::hidl_string; |
| 41 | using ::android::hardware::hidl_vec; |
| 42 | |
| 43 | namespace android { |
| 44 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 45 | using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; |
| 46 | using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION; |
Mikhail Naganov | 9ccaa16 | 2018-12-12 10:27:29 -0800 | [diff] [blame] | 47 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 48 | DeviceHalHidl::DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IDevice>& device) |
| 49 | : ConversionHelperHidl("Device"), mDevice(device) { |
| 50 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 51 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 52 | DeviceHalHidl::DeviceHalHidl( |
| 53 | const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice>& device) |
| 54 | : ConversionHelperHidl("Device"), |
| 55 | #if MAJOR_VERSION <= 6 || (MAJOR_VERSION == 7 && MINOR_VERSION == 0) |
| 56 | mDevice(device), |
| 57 | #endif |
| 58 | mPrimaryDevice(device) { |
| 59 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 60 | auto getDeviceRet = mPrimaryDevice->getDevice(); |
| 61 | if (getDeviceRet.isOk()) { |
| 62 | mDevice = getDeviceRet; |
| 63 | } else { |
| 64 | ALOGE("Call to IPrimaryDevice.getDevice has failed: %s", |
| 65 | getDeviceRet.description().c_str()); |
| 66 | } |
| 67 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | DeviceHalHidl::~DeviceHalHidl() { |
| 71 | if (mDevice != 0) { |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 72 | #if MAJOR_VERSION <= 5 |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 73 | mDevice.clear(); |
| 74 | hardware::IPCThreadState::self()->flushCommands(); |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 75 | #elif MAJOR_VERSION >= 6 |
| 76 | mDevice->close(); |
| 77 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | status_t DeviceHalHidl::getSupportedDevices(uint32_t*) { |
| 82 | // Obsolete. |
| 83 | return INVALID_OPERATION; |
| 84 | } |
| 85 | |
| 86 | status_t DeviceHalHidl::initCheck() { |
| 87 | if (mDevice == 0) return NO_INIT; |
| 88 | return processReturn("initCheck", mDevice->initCheck()); |
| 89 | } |
| 90 | |
| 91 | status_t DeviceHalHidl::setVoiceVolume(float volume) { |
| 92 | if (mDevice == 0) return NO_INIT; |
| 93 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 94 | return processReturn("setVoiceVolume", mPrimaryDevice->setVoiceVolume(volume)); |
| 95 | } |
| 96 | |
| 97 | status_t DeviceHalHidl::setMasterVolume(float volume) { |
| 98 | if (mDevice == 0) return NO_INIT; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 99 | return processReturn("setMasterVolume", mDevice->setMasterVolume(volume)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | status_t DeviceHalHidl::getMasterVolume(float *volume) { |
| 103 | if (mDevice == 0) return NO_INIT; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 104 | Result retval; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 105 | Return<void> ret = mDevice->getMasterVolume( |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 106 | [&](Result r, float v) { |
| 107 | retval = r; |
| 108 | if (retval == Result::OK) { |
| 109 | *volume = v; |
| 110 | } |
| 111 | }); |
| 112 | return processReturn("getMasterVolume", ret, retval); |
| 113 | } |
| 114 | |
| 115 | status_t DeviceHalHidl::setMode(audio_mode_t mode) { |
| 116 | if (mDevice == 0) return NO_INIT; |
| 117 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 118 | return processReturn("setMode", mPrimaryDevice->setMode(AudioMode(mode))); |
| 119 | } |
| 120 | |
| 121 | status_t DeviceHalHidl::setMicMute(bool state) { |
| 122 | if (mDevice == 0) return NO_INIT; |
| 123 | return processReturn("setMicMute", mDevice->setMicMute(state)); |
| 124 | } |
| 125 | |
| 126 | status_t DeviceHalHidl::getMicMute(bool *state) { |
| 127 | if (mDevice == 0) return NO_INIT; |
| 128 | Result retval; |
| 129 | Return<void> ret = mDevice->getMicMute( |
| 130 | [&](Result r, bool mute) { |
| 131 | retval = r; |
| 132 | if (retval == Result::OK) { |
| 133 | *state = mute; |
| 134 | } |
| 135 | }); |
| 136 | return processReturn("getMicMute", ret, retval); |
| 137 | } |
| 138 | |
| 139 | status_t DeviceHalHidl::setMasterMute(bool state) { |
| 140 | if (mDevice == 0) return NO_INIT; |
| 141 | return processReturn("setMasterMute", mDevice->setMasterMute(state)); |
| 142 | } |
| 143 | |
| 144 | status_t DeviceHalHidl::getMasterMute(bool *state) { |
| 145 | if (mDevice == 0) return NO_INIT; |
| 146 | Result retval; |
| 147 | Return<void> ret = mDevice->getMasterMute( |
| 148 | [&](Result r, bool mute) { |
| 149 | retval = r; |
| 150 | if (retval == Result::OK) { |
| 151 | *state = mute; |
| 152 | } |
| 153 | }); |
| 154 | return processReturn("getMasterMute", ret, retval); |
| 155 | } |
| 156 | |
| 157 | status_t DeviceHalHidl::setParameters(const String8& kvPairs) { |
| 158 | if (mDevice == 0) return NO_INIT; |
| 159 | hidl_vec<ParameterValue> hidlParams; |
| 160 | status_t status = parametersFromHal(kvPairs, &hidlParams); |
| 161 | if (status != OK) return status; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 162 | // TODO: change the API so that context and kvPairs are separated |
| 163 | return processReturn("setParameters", |
| 164 | utils::setParameters(mDevice, {} /* context */, hidlParams)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | status_t DeviceHalHidl::getParameters(const String8& keys, String8 *values) { |
| 168 | values->clear(); |
| 169 | if (mDevice == 0) return NO_INIT; |
| 170 | hidl_vec<hidl_string> hidlKeys; |
| 171 | status_t status = keysFromHal(keys, &hidlKeys); |
| 172 | if (status != OK) return status; |
| 173 | Result retval; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 174 | Return<void> ret = utils::getParameters(mDevice, |
| 175 | {} /* context */, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 176 | hidlKeys, |
| 177 | [&](Result r, const hidl_vec<ParameterValue>& parameters) { |
| 178 | retval = r; |
| 179 | if (retval == Result::OK) { |
| 180 | parametersToHal(parameters, values); |
| 181 | } |
| 182 | }); |
| 183 | return processReturn("getParameters", ret, retval); |
| 184 | } |
| 185 | |
| 186 | status_t DeviceHalHidl::getInputBufferSize( |
| 187 | const struct audio_config *config, size_t *size) { |
| 188 | if (mDevice == 0) return NO_INIT; |
| 189 | AudioConfig hidlConfig; |
Mikhail Naganov | 05e0319 | 2020-12-14 23:19:54 +0000 | [diff] [blame] | 190 | HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 191 | Result retval; |
| 192 | Return<void> ret = mDevice->getInputBufferSize( |
| 193 | hidlConfig, |
| 194 | [&](Result r, uint64_t bufferSize) { |
| 195 | retval = r; |
| 196 | if (retval == Result::OK) { |
| 197 | *size = static_cast<size_t>(bufferSize); |
| 198 | } |
| 199 | }); |
| 200 | return processReturn("getInputBufferSize", ret, retval); |
| 201 | } |
| 202 | |
| 203 | status_t DeviceHalHidl::openOutputStream( |
| 204 | audio_io_handle_t handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 205 | audio_devices_t deviceType, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 206 | audio_output_flags_t flags, |
| 207 | struct audio_config *config, |
| 208 | const char *address, |
| 209 | sp<StreamOutHalInterface> *outStream) { |
| 210 | if (mDevice == 0) return NO_INIT; |
| 211 | DeviceAddress hidlDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 212 | if (status_t status = CoreUtils::deviceAddressFromHal(deviceType, address, &hidlDevice); |
| 213 | status != OK) { |
| 214 | return status; |
| 215 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 216 | AudioConfig hidlConfig; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 217 | if (status_t status = HidlUtils::audioConfigFromHal(*config, false /*isInput*/, &hidlConfig); |
| 218 | status != OK) { |
| 219 | return status; |
| 220 | } |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 221 | |
| 222 | #if !(MAJOR_VERSION == 7 && MINOR_VERSION == 1) |
| 223 | //TODO: b/193496180 use spatializer flag at audio HAL when available |
| 224 | if ((flags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 225 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_SPATIALIZER); |
| 226 | flags = (audio_output_flags_t) |
| 227 | (flags | AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 228 | } |
| 229 | #endif |
| 230 | |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 231 | CoreUtils::AudioOutputFlags hidlFlags; |
| 232 | if (status_t status = CoreUtils::audioOutputFlagsFromHal(flags, &hidlFlags); status != OK) { |
| 233 | return status; |
| 234 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 235 | Result retval = Result::NOT_INITIALIZED; |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 236 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 237 | Return<void> ret = mDevice->openOutputStream_7_1( |
| 238 | #else |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 239 | Return<void> ret = mDevice->openOutputStream( |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 240 | #endif |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 241 | handle, hidlDevice, hidlConfig, hidlFlags, |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 242 | #if MAJOR_VERSION >= 4 |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 243 | {} /* metadata */, |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 244 | #endif |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 245 | [&](Result r, const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& result, |
| 246 | const AudioConfig& suggestedConfig) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 247 | retval = r; |
| 248 | if (retval == Result::OK) { |
| 249 | *outStream = new StreamOutHalHidl(result); |
| 250 | } |
| 251 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 252 | }); |
| 253 | return processReturn("openOutputStream", ret, retval); |
| 254 | } |
| 255 | |
| 256 | status_t DeviceHalHidl::openInputStream( |
| 257 | audio_io_handle_t handle, |
| 258 | audio_devices_t devices, |
| 259 | struct audio_config *config, |
| 260 | audio_input_flags_t flags, |
| 261 | const char *address, |
| 262 | audio_source_t source, |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 263 | audio_devices_t outputDevice, |
| 264 | const char *outputDeviceAddress, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 265 | sp<StreamInHalInterface> *inStream) { |
| 266 | if (mDevice == 0) return NO_INIT; |
| 267 | DeviceAddress hidlDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 268 | if (status_t status = CoreUtils::deviceAddressFromHal(devices, address, &hidlDevice); |
| 269 | status != OK) { |
| 270 | return status; |
| 271 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 272 | AudioConfig hidlConfig; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 273 | if (status_t status = HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
| 274 | status != OK) { |
| 275 | return status; |
| 276 | } |
| 277 | CoreUtils::AudioInputFlags hidlFlags; |
Dean Wheatley | ea18634 | 2021-03-12 16:49:47 +1100 | [diff] [blame] | 278 | #if MAJOR_VERSION <= 5 |
| 279 | // Some flags were specific to framework and must not leak to the HAL. |
| 280 | flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FLAG_DIRECT); |
| 281 | #endif |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 282 | if (status_t status = CoreUtils::audioInputFlagsFromHal(flags, &hidlFlags); status != OK) { |
| 283 | return status; |
| 284 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 285 | Result retval = Result::NOT_INITIALIZED; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 286 | #if MAJOR_VERSION == 2 |
Mikhail Naganov | d9499eb | 2018-12-17 16:23:22 -0800 | [diff] [blame] | 287 | auto sinkMetadata = AudioSource(source); |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 288 | #elif MAJOR_VERSION >= 4 |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 289 | // TODO: correctly propagate the tracks sources and volume |
| 290 | // for now, only send the main source at 1dbfs |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 291 | AudioSource hidlSource; |
| 292 | if (status_t status = HidlUtils::audioSourceFromHal(source, &hidlSource); status != OK) { |
| 293 | return status; |
| 294 | } |
| 295 | SinkMetadata sinkMetadata = {{{ .source = std::move(hidlSource), .gain = 1 }}}; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 296 | #endif |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 297 | #if MAJOR_VERSION < 5 |
| 298 | (void)outputDevice; |
| 299 | (void)outputDeviceAddress; |
| 300 | #else |
Mikhail Naganov | c425880 | 2021-02-08 17:14:17 -0800 | [diff] [blame] | 301 | #if MAJOR_VERSION >= 7 |
| 302 | (void)HidlUtils::audioChannelMaskFromHal( |
| 303 | AUDIO_CHANNEL_NONE, true /*isInput*/, &sinkMetadata.tracks[0].channelMask); |
| 304 | #endif |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 305 | if (outputDevice != AUDIO_DEVICE_NONE) { |
| 306 | DeviceAddress hidlOutputDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 307 | if (status_t status = CoreUtils::deviceAddressFromHal( |
| 308 | outputDevice, outputDeviceAddress, &hidlOutputDevice); status != OK) { |
| 309 | return status; |
| 310 | } |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 311 | sinkMetadata.tracks[0].destination.device(std::move(hidlOutputDevice)); |
| 312 | } |
| 313 | #endif |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 314 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 315 | Return<void> ret = mDevice->openInputStream_7_1( |
| 316 | #else |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 317 | Return<void> ret = mDevice->openInputStream( |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 318 | #endif |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 319 | handle, hidlDevice, hidlConfig, hidlFlags, sinkMetadata, |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 320 | [&](Result r, const sp<::android::hardware::audio::CPP_VERSION::IStreamIn>& result, |
| 321 | const AudioConfig& suggestedConfig) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 322 | retval = r; |
| 323 | if (retval == Result::OK) { |
| 324 | *inStream = new StreamInHalHidl(result); |
| 325 | } |
| 326 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 327 | }); |
| 328 | return processReturn("openInputStream", ret, retval); |
| 329 | } |
| 330 | |
| 331 | status_t DeviceHalHidl::supportsAudioPatches(bool *supportsPatches) { |
| 332 | if (mDevice == 0) return NO_INIT; |
| 333 | return processReturn("supportsAudioPatches", mDevice->supportsAudioPatches(), supportsPatches); |
| 334 | } |
| 335 | |
| 336 | status_t DeviceHalHidl::createAudioPatch( |
| 337 | unsigned int num_sources, |
| 338 | const struct audio_port_config *sources, |
| 339 | unsigned int num_sinks, |
| 340 | const struct audio_port_config *sinks, |
| 341 | audio_patch_handle_t *patch) { |
| 342 | if (mDevice == 0) return NO_INIT; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 343 | if (patch == nullptr) return BAD_VALUE; |
| 344 | |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 345 | #if MAJOR_VERSION < 6 |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 346 | if (*patch != AUDIO_PATCH_HANDLE_NONE) { |
| 347 | status_t status = releaseAudioPatch(*patch); |
| 348 | ALOGW_IF(status != NO_ERROR, "%s error %d releasing patch handle %d", |
| 349 | __func__, status, *patch); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 350 | *patch = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 351 | } |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 352 | #endif |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 353 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 354 | hidl_vec<AudioPortConfig> hidlSources, hidlSinks; |
| 355 | HidlUtils::audioPortConfigsFromHal(num_sources, sources, &hidlSources); |
| 356 | HidlUtils::audioPortConfigsFromHal(num_sinks, sinks, &hidlSinks); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 357 | Result retval = Result::OK; |
| 358 | Return<void> ret; |
| 359 | std::string methodName = "createAudioPatch"; |
| 360 | if (*patch == AUDIO_PATCH_HANDLE_NONE) { // always true for MAJOR_VERSION < 6 |
| 361 | ret = mDevice->createAudioPatch( |
| 362 | hidlSources, hidlSinks, |
| 363 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 364 | retval = r; |
| 365 | if (retval == Result::OK) { |
| 366 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 367 | } |
| 368 | }); |
| 369 | } else { |
| 370 | #if MAJOR_VERSION >= 6 |
| 371 | ret = mDevice->updateAudioPatch( |
| 372 | *patch, |
| 373 | hidlSources, hidlSinks, |
| 374 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 375 | retval = r; |
| 376 | if (retval == Result::OK) { |
| 377 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 378 | } |
| 379 | }); |
| 380 | methodName = "updateAudioPatch"; |
| 381 | #endif |
| 382 | } |
| 383 | return processReturn(methodName.c_str(), ret, retval); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | status_t DeviceHalHidl::releaseAudioPatch(audio_patch_handle_t patch) { |
| 387 | if (mDevice == 0) return NO_INIT; |
| 388 | return processReturn("releaseAudioPatch", mDevice->releaseAudioPatch(patch)); |
| 389 | } |
| 390 | |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 391 | template <typename HalPort> |
| 392 | status_t DeviceHalHidl::getAudioPortImpl(HalPort *port) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 393 | if (mDevice == 0) return NO_INIT; |
| 394 | AudioPort hidlPort; |
| 395 | HidlUtils::audioPortFromHal(*port, &hidlPort); |
| 396 | Result retval; |
| 397 | Return<void> ret = mDevice->getAudioPort( |
| 398 | hidlPort, |
| 399 | [&](Result r, const AudioPort& p) { |
| 400 | retval = r; |
| 401 | if (retval == Result::OK) { |
| 402 | HidlUtils::audioPortToHal(p, port); |
| 403 | } |
| 404 | }); |
| 405 | return processReturn("getAudioPort", ret, retval); |
| 406 | } |
| 407 | |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 408 | status_t DeviceHalHidl::getAudioPort(struct audio_port *port) { |
| 409 | return getAudioPortImpl(port); |
| 410 | } |
| 411 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 412 | status_t DeviceHalHidl::getAudioPort(struct audio_port_v7 *port) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 413 | #if MAJOR_VERSION >= 7 |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 414 | return getAudioPortImpl(port); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 415 | #else |
| 416 | struct audio_port audioPort = {}; |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 417 | status_t result = NO_ERROR; |
| 418 | if (!audio_populate_audio_port(port, &audioPort)) { |
| 419 | ALOGE("Failed to populate legacy audio port from audio_port_v7"); |
| 420 | result = BAD_VALUE; |
| 421 | } |
| 422 | status_t status = getAudioPort(&audioPort); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 423 | if (status == NO_ERROR) { |
| 424 | audio_populate_audio_port_v7(&audioPort, port); |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 425 | } else { |
| 426 | result = status; |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 427 | } |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 428 | return result; |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 429 | #endif |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 432 | status_t DeviceHalHidl::setAudioPortConfig(const struct audio_port_config *config) { |
| 433 | if (mDevice == 0) return NO_INIT; |
| 434 | AudioPortConfig hidlConfig; |
| 435 | HidlUtils::audioPortConfigFromHal(*config, &hidlConfig); |
| 436 | return processReturn("setAudioPortConfig", mDevice->setAudioPortConfig(hidlConfig)); |
| 437 | } |
| 438 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 439 | #if MAJOR_VERSION == 2 |
| 440 | status_t DeviceHalHidl::getMicrophones( |
| 441 | std::vector<media::MicrophoneInfo> *microphonesInfo __unused) { |
| 442 | if (mDevice == 0) return NO_INIT; |
| 443 | return INVALID_OPERATION; |
| 444 | } |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 445 | #elif MAJOR_VERSION >= 4 |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 446 | status_t DeviceHalHidl::getMicrophones(std::vector<media::MicrophoneInfo> *microphonesInfo) { |
| 447 | if (mDevice == 0) return NO_INIT; |
| 448 | Result retval; |
| 449 | Return<void> ret = mDevice->getMicrophones( |
| 450 | [&](Result r, hidl_vec<MicrophoneInfo> micArrayHal) { |
| 451 | retval = r; |
| 452 | for (size_t k = 0; k < micArrayHal.size(); k++) { |
| 453 | audio_microphone_characteristic_t dst; |
| 454 | //convert |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 455 | (void)CoreUtils::microphoneInfoToHal(micArrayHal[k], &dst); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 456 | media::MicrophoneInfo microphone = media::MicrophoneInfo(dst); |
| 457 | microphonesInfo->push_back(microphone); |
| 458 | } |
| 459 | }); |
| 460 | return processReturn("getMicrophones", ret, retval); |
| 461 | } |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 462 | #endif |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 463 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 464 | #if MAJOR_VERSION >= 6 |
| 465 | status_t DeviceHalHidl::addDeviceEffect( |
| 466 | audio_port_handle_t device, sp<EffectHalInterface> effect) { |
| 467 | if (mDevice == 0) return NO_INIT; |
| 468 | return processReturn("addDeviceEffect", mDevice->addDeviceEffect( |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 469 | static_cast<AudioPortHandle>(device), effect->effectId())); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 470 | } |
| 471 | #else |
| 472 | status_t DeviceHalHidl::addDeviceEffect( |
| 473 | audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) { |
| 474 | return INVALID_OPERATION; |
| 475 | } |
| 476 | #endif |
| 477 | |
| 478 | #if MAJOR_VERSION >= 6 |
| 479 | status_t DeviceHalHidl::removeDeviceEffect( |
| 480 | audio_port_handle_t device, sp<EffectHalInterface> effect) { |
| 481 | if (mDevice == 0) return NO_INIT; |
| 482 | return processReturn("removeDeviceEffect", mDevice->removeDeviceEffect( |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 483 | static_cast<AudioPortHandle>(device), effect->effectId())); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 484 | } |
| 485 | #else |
| 486 | status_t DeviceHalHidl::removeDeviceEffect( |
| 487 | audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) { |
| 488 | return INVALID_OPERATION; |
| 489 | } |
| 490 | #endif |
| 491 | |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame^] | 492 | status_t DeviceHalHidl::setConnectedState(const struct audio_port_v7 *port, bool connected) { |
| 493 | if (mDevice == 0) return NO_INIT; |
| 494 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 495 | if (supportsSetConnectedState7_1) { |
| 496 | AudioPort hidlPort; |
| 497 | if (status_t result = HidlUtils::audioPortFromHal(*port, &hidlPort); result != NO_ERROR) { |
| 498 | return result; |
| 499 | } |
| 500 | Return<Result> ret = mDevice->setConnectedState_7_1(hidlPort, connected); |
| 501 | if (!ret.isOk() || ret != Result::NOT_SUPPORTED) { |
| 502 | return processReturn("setConnectedState_7_1", ret); |
| 503 | } else if (ret == Result::OK) { |
| 504 | return NO_ERROR; |
| 505 | } |
| 506 | supportsSetConnectedState7_1 = false; |
| 507 | } |
| 508 | #endif |
| 509 | DeviceAddress hidlAddress; |
| 510 | if (status_t result = CoreUtils::deviceAddressFromHal( |
| 511 | port->ext.device.type, port->ext.device.address, &hidlAddress); |
| 512 | result != NO_ERROR) { |
| 513 | return result; |
| 514 | } |
| 515 | return processReturn("setConnectedState", mDevice->setConnectedState(hidlAddress, connected)); |
| 516 | } |
| 517 | |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 518 | status_t DeviceHalHidl::dump(int fd, const Vector<String16>& args) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 519 | if (mDevice == 0) return NO_INIT; |
| 520 | native_handle_t* hidlHandle = native_handle_create(1, 0); |
| 521 | hidlHandle->data[0] = fd; |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 522 | hidl_vec<hidl_string> hidlArgs; |
| 523 | argsFromHal(args, &hidlArgs); |
| 524 | Return<void> ret = mDevice->debug(hidlHandle, hidlArgs); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 525 | native_handle_delete(hidlHandle); |
Andy Hung | e72ff02 | 2021-08-16 10:16:15 -0700 | [diff] [blame] | 526 | |
| 527 | // TODO(b/111997867, b/177271958) Workaround - remove when fixed. |
| 528 | // A Binder transmitted fd may not close immediately due to a race condition b/111997867 |
| 529 | // when the remote binder thread removes the last refcount to the fd blocks in the |
| 530 | // kernel for binder activity. We send a Binder ping() command to unblock the thread |
| 531 | // and complete the fd close / release. |
| 532 | // |
| 533 | // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(), |
| 534 | // EffectsFactoryHalHidl::dumpEffects(). |
| 535 | |
| 536 | (void)mDevice->ping(); // synchronous Binder call |
| 537 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 538 | return processReturn("dump", ret); |
| 539 | } |
| 540 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 541 | } // namespace android |