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" |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 20 | // #define LOG_NDEBUG 0 |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 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> |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 26 | #include <mediautils/TimeCheck.h> |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 29 | #include PATH(android/hardware/audio/FILE_VERSION/IPrimaryDevice.h) |
| 30 | #include <HidlUtils.h> |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 31 | #include <common/all-versions/VersionUtils.h> |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 32 | #include <util/CoreUtils.h> |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 33 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 34 | #include "DeviceHalHidl.h" |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 35 | #include "ParameterUtils.h" |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 36 | #include "StreamHalHidl.h" |
| 37 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 38 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 39 | #include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h> |
| 40 | #include <aidl/android/hardware/audio/sounddose/BpSoundDoseFactory.h> |
| 41 | #include <android/binder_manager.h> |
| 42 | |
| 43 | constexpr std::string_view kSoundDoseInterfaceModule = "/default"; |
| 44 | |
| 45 | using aidl::android::hardware::audio::core::sounddose::ISoundDose; |
| 46 | using aidl::android::hardware::audio::sounddose::ISoundDoseFactory; |
| 47 | #endif |
| 48 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 49 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils; |
Kevin Rocard | 7a9f05a | 2018-11-28 16:52:25 -0800 | [diff] [blame] | 50 | using ::android::hardware::audio::common::utils::EnumBitfield; |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 51 | using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::implementation::CoreUtils; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 52 | using ::android::hardware::hidl_string; |
| 53 | using ::android::hardware::hidl_vec; |
| 54 | |
| 55 | namespace android { |
| 56 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 57 | using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; |
| 58 | using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION; |
Mikhail Naganov | 9ccaa16 | 2018-12-12 10:27:29 -0800 | [diff] [blame] | 59 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 60 | class DeviceHalHidl::SoundDoseWrapper { |
| 61 | public: |
| 62 | SoundDoseWrapper() = default; |
| 63 | ~SoundDoseWrapper() = default; |
| 64 | |
| 65 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 66 | std::shared_ptr<ISoundDoseFactory> mSoundDoseFactory; |
| 67 | std::shared_ptr<ISoundDose> mSoundDose; |
| 68 | #endif |
| 69 | }; |
| 70 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 71 | DeviceHalHidl::DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IDevice>& device) |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 72 | : CoreConversionHelperHidl("DeviceHalHidl"), |
| 73 | mDevice(device), |
| 74 | mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) { |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 75 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 76 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 77 | DeviceHalHidl::DeviceHalHidl( |
| 78 | const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice>& device) |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 79 | : CoreConversionHelperHidl("DeviceHalHidl"), |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 80 | #if MAJOR_VERSION <= 6 || (MAJOR_VERSION == 7 && MINOR_VERSION == 0) |
| 81 | mDevice(device), |
| 82 | #endif |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 83 | mPrimaryDevice(device), |
| 84 | mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) { |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 85 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 86 | auto getDeviceRet = mPrimaryDevice->getDevice(); |
| 87 | if (getDeviceRet.isOk()) { |
| 88 | mDevice = getDeviceRet; |
| 89 | } else { |
| 90 | ALOGE("Call to IPrimaryDevice.getDevice has failed: %s", |
| 91 | getDeviceRet.description().c_str()); |
| 92 | } |
| 93 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | DeviceHalHidl::~DeviceHalHidl() { |
| 97 | if (mDevice != 0) { |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 98 | #if MAJOR_VERSION <= 5 |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 99 | mDevice.clear(); |
| 100 | hardware::IPCThreadState::self()->flushCommands(); |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 101 | #elif MAJOR_VERSION >= 6 |
| 102 | mDevice->close(); |
| 103 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 107 | status_t DeviceHalHidl::getAudioPorts( |
| 108 | std::vector<media::audio::common::AudioPort> *ports __unused) { |
| 109 | return INVALID_OPERATION; |
| 110 | } |
| 111 | |
| 112 | status_t DeviceHalHidl::getAudioRoutes(std::vector<media::AudioRoute> *routes __unused) { |
| 113 | return INVALID_OPERATION; |
| 114 | } |
| 115 | |
Mikhail Naganov | ffd9771 | 2023-05-03 17:45:36 -0700 | [diff] [blame^] | 116 | status_t DeviceHalHidl::getSupportedModes( |
| 117 | std::vector<media::audio::common::AudioMode> *modes __unused) { |
| 118 | return INVALID_OPERATION; |
| 119 | } |
| 120 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 121 | status_t DeviceHalHidl::getSupportedDevices(uint32_t*) { |
| 122 | // Obsolete. |
| 123 | return INVALID_OPERATION; |
| 124 | } |
| 125 | |
| 126 | status_t DeviceHalHidl::initCheck() { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 127 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 128 | if (mDevice == 0) return NO_INIT; |
| 129 | return processReturn("initCheck", mDevice->initCheck()); |
| 130 | } |
| 131 | |
| 132 | status_t DeviceHalHidl::setVoiceVolume(float volume) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 133 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 134 | if (mDevice == 0) return NO_INIT; |
| 135 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 136 | return processReturn("setVoiceVolume", mPrimaryDevice->setVoiceVolume(volume)); |
| 137 | } |
| 138 | |
| 139 | status_t DeviceHalHidl::setMasterVolume(float volume) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 140 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 141 | if (mDevice == 0) return NO_INIT; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 142 | return processReturn("setMasterVolume", mDevice->setMasterVolume(volume)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | status_t DeviceHalHidl::getMasterVolume(float *volume) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 146 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 147 | if (mDevice == 0) return NO_INIT; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 148 | Result retval; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 149 | Return<void> ret = mDevice->getMasterVolume( |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 150 | [&](Result r, float v) { |
| 151 | retval = r; |
| 152 | if (retval == Result::OK) { |
| 153 | *volume = v; |
| 154 | } |
| 155 | }); |
| 156 | return processReturn("getMasterVolume", ret, retval); |
| 157 | } |
| 158 | |
| 159 | status_t DeviceHalHidl::setMode(audio_mode_t mode) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 160 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 161 | if (mDevice == 0) return NO_INIT; |
| 162 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 163 | return processReturn("setMode", mPrimaryDevice->setMode(AudioMode(mode))); |
| 164 | } |
| 165 | |
| 166 | status_t DeviceHalHidl::setMicMute(bool state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 167 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 168 | if (mDevice == 0) return NO_INIT; |
| 169 | return processReturn("setMicMute", mDevice->setMicMute(state)); |
| 170 | } |
| 171 | |
| 172 | status_t DeviceHalHidl::getMicMute(bool *state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 173 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 174 | if (mDevice == 0) return NO_INIT; |
| 175 | Result retval; |
| 176 | Return<void> ret = mDevice->getMicMute( |
| 177 | [&](Result r, bool mute) { |
| 178 | retval = r; |
| 179 | if (retval == Result::OK) { |
| 180 | *state = mute; |
| 181 | } |
| 182 | }); |
| 183 | return processReturn("getMicMute", ret, retval); |
| 184 | } |
| 185 | |
| 186 | status_t DeviceHalHidl::setMasterMute(bool state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 187 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 188 | if (mDevice == 0) return NO_INIT; |
| 189 | return processReturn("setMasterMute", mDevice->setMasterMute(state)); |
| 190 | } |
| 191 | |
| 192 | status_t DeviceHalHidl::getMasterMute(bool *state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 193 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 194 | if (mDevice == 0) return NO_INIT; |
| 195 | Result retval; |
| 196 | Return<void> ret = mDevice->getMasterMute( |
| 197 | [&](Result r, bool mute) { |
| 198 | retval = r; |
| 199 | if (retval == Result::OK) { |
| 200 | *state = mute; |
| 201 | } |
| 202 | }); |
| 203 | return processReturn("getMasterMute", ret, retval); |
| 204 | } |
| 205 | |
| 206 | status_t DeviceHalHidl::setParameters(const String8& kvPairs) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 207 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 208 | if (mDevice == 0) return NO_INIT; |
| 209 | hidl_vec<ParameterValue> hidlParams; |
| 210 | status_t status = parametersFromHal(kvPairs, &hidlParams); |
| 211 | if (status != OK) return status; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 212 | // TODO: change the API so that context and kvPairs are separated |
| 213 | return processReturn("setParameters", |
| 214 | utils::setParameters(mDevice, {} /* context */, hidlParams)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | status_t DeviceHalHidl::getParameters(const String8& keys, String8 *values) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 218 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 219 | values->clear(); |
| 220 | if (mDevice == 0) return NO_INIT; |
| 221 | hidl_vec<hidl_string> hidlKeys; |
| 222 | status_t status = keysFromHal(keys, &hidlKeys); |
| 223 | if (status != OK) return status; |
| 224 | Result retval; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 225 | Return<void> ret = utils::getParameters(mDevice, |
| 226 | {} /* context */, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 227 | hidlKeys, |
| 228 | [&](Result r, const hidl_vec<ParameterValue>& parameters) { |
| 229 | retval = r; |
| 230 | if (retval == Result::OK) { |
| 231 | parametersToHal(parameters, values); |
| 232 | } |
| 233 | }); |
| 234 | return processReturn("getParameters", ret, retval); |
| 235 | } |
| 236 | |
| 237 | status_t DeviceHalHidl::getInputBufferSize( |
| 238 | const struct audio_config *config, size_t *size) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 239 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 240 | if (mDevice == 0) return NO_INIT; |
| 241 | AudioConfig hidlConfig; |
Mikhail Naganov | 05e0319 | 2020-12-14 23:19:54 +0000 | [diff] [blame] | 242 | HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 243 | Result retval; |
| 244 | Return<void> ret = mDevice->getInputBufferSize( |
| 245 | hidlConfig, |
| 246 | [&](Result r, uint64_t bufferSize) { |
| 247 | retval = r; |
| 248 | if (retval == Result::OK) { |
| 249 | *size = static_cast<size_t>(bufferSize); |
| 250 | } |
| 251 | }); |
| 252 | return processReturn("getInputBufferSize", ret, retval); |
| 253 | } |
| 254 | |
| 255 | status_t DeviceHalHidl::openOutputStream( |
| 256 | audio_io_handle_t handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 257 | audio_devices_t deviceType, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 258 | audio_output_flags_t flags, |
| 259 | struct audio_config *config, |
| 260 | const char *address, |
| 261 | sp<StreamOutHalInterface> *outStream) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 262 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 263 | if (mDevice == 0) return NO_INIT; |
| 264 | DeviceAddress hidlDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 265 | if (status_t status = CoreUtils::deviceAddressFromHal(deviceType, address, &hidlDevice); |
| 266 | status != OK) { |
| 267 | return status; |
| 268 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 269 | AudioConfig hidlConfig; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 270 | if (status_t status = HidlUtils::audioConfigFromHal(*config, false /*isInput*/, &hidlConfig); |
| 271 | status != OK) { |
| 272 | return status; |
| 273 | } |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 274 | |
| 275 | #if !(MAJOR_VERSION == 7 && MINOR_VERSION == 1) |
| 276 | //TODO: b/193496180 use spatializer flag at audio HAL when available |
| 277 | if ((flags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 278 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_SPATIALIZER); |
| 279 | flags = (audio_output_flags_t) |
| 280 | (flags | AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 281 | } |
| 282 | #endif |
| 283 | |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 284 | CoreUtils::AudioOutputFlags hidlFlags; |
| 285 | if (status_t status = CoreUtils::audioOutputFlagsFromHal(flags, &hidlFlags); status != OK) { |
| 286 | return status; |
| 287 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 288 | Result retval = Result::NOT_INITIALIZED; |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 289 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 290 | Return<void> ret = mDevice->openOutputStream_7_1( |
| 291 | #else |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 292 | Return<void> ret = mDevice->openOutputStream( |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 293 | #endif |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 294 | handle, hidlDevice, hidlConfig, hidlFlags, |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 295 | #if MAJOR_VERSION >= 4 |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 296 | {} /* metadata */, |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 297 | #endif |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 298 | [&](Result r, const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& result, |
| 299 | const AudioConfig& suggestedConfig) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 300 | retval = r; |
| 301 | if (retval == Result::OK) { |
| 302 | *outStream = new StreamOutHalHidl(result); |
| 303 | } |
| 304 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 305 | }); |
| 306 | return processReturn("openOutputStream", ret, retval); |
| 307 | } |
| 308 | |
| 309 | status_t DeviceHalHidl::openInputStream( |
| 310 | audio_io_handle_t handle, |
| 311 | audio_devices_t devices, |
| 312 | struct audio_config *config, |
| 313 | audio_input_flags_t flags, |
| 314 | const char *address, |
| 315 | audio_source_t source, |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 316 | audio_devices_t outputDevice, |
| 317 | const char *outputDeviceAddress, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 318 | sp<StreamInHalInterface> *inStream) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 319 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 320 | if (mDevice == 0) return NO_INIT; |
| 321 | DeviceAddress hidlDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 322 | if (status_t status = CoreUtils::deviceAddressFromHal(devices, address, &hidlDevice); |
| 323 | status != OK) { |
| 324 | return status; |
| 325 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 326 | AudioConfig hidlConfig; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 327 | if (status_t status = HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
| 328 | status != OK) { |
| 329 | return status; |
| 330 | } |
| 331 | CoreUtils::AudioInputFlags hidlFlags; |
Dean Wheatley | ea18634 | 2021-03-12 16:49:47 +1100 | [diff] [blame] | 332 | #if MAJOR_VERSION <= 5 |
| 333 | // Some flags were specific to framework and must not leak to the HAL. |
| 334 | flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FLAG_DIRECT); |
| 335 | #endif |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 336 | if (status_t status = CoreUtils::audioInputFlagsFromHal(flags, &hidlFlags); status != OK) { |
| 337 | return status; |
| 338 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 339 | Result retval = Result::NOT_INITIALIZED; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 340 | #if MAJOR_VERSION == 2 |
Mikhail Naganov | d9499eb | 2018-12-17 16:23:22 -0800 | [diff] [blame] | 341 | auto sinkMetadata = AudioSource(source); |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 342 | #elif MAJOR_VERSION >= 4 |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 343 | // TODO: correctly propagate the tracks sources and volume |
| 344 | // for now, only send the main source at 1dbfs |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 345 | AudioSource hidlSource; |
| 346 | if (status_t status = HidlUtils::audioSourceFromHal(source, &hidlSource); status != OK) { |
| 347 | return status; |
| 348 | } |
| 349 | SinkMetadata sinkMetadata = {{{ .source = std::move(hidlSource), .gain = 1 }}}; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 350 | #endif |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 351 | #if MAJOR_VERSION < 5 |
| 352 | (void)outputDevice; |
| 353 | (void)outputDeviceAddress; |
| 354 | #else |
Mikhail Naganov | c425880 | 2021-02-08 17:14:17 -0800 | [diff] [blame] | 355 | #if MAJOR_VERSION >= 7 |
| 356 | (void)HidlUtils::audioChannelMaskFromHal( |
| 357 | AUDIO_CHANNEL_NONE, true /*isInput*/, &sinkMetadata.tracks[0].channelMask); |
| 358 | #endif |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 359 | if (outputDevice != AUDIO_DEVICE_NONE) { |
| 360 | DeviceAddress hidlOutputDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 361 | if (status_t status = CoreUtils::deviceAddressFromHal( |
| 362 | outputDevice, outputDeviceAddress, &hidlOutputDevice); status != OK) { |
| 363 | return status; |
| 364 | } |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 365 | sinkMetadata.tracks[0].destination.device(std::move(hidlOutputDevice)); |
| 366 | } |
| 367 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 368 | Return<void> ret = mDevice->openInputStream( |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 369 | handle, hidlDevice, hidlConfig, hidlFlags, sinkMetadata, |
Mikhail Naganov | accbe8a | 2022-02-03 23:45:36 +0000 | [diff] [blame] | 370 | [&](Result r, |
| 371 | const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn>& result, |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 372 | const AudioConfig& suggestedConfig) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 373 | retval = r; |
| 374 | if (retval == Result::OK) { |
| 375 | *inStream = new StreamInHalHidl(result); |
| 376 | } |
| 377 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 378 | }); |
| 379 | return processReturn("openInputStream", ret, retval); |
| 380 | } |
| 381 | |
| 382 | status_t DeviceHalHidl::supportsAudioPatches(bool *supportsPatches) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 383 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 384 | if (mDevice == 0) return NO_INIT; |
| 385 | return processReturn("supportsAudioPatches", mDevice->supportsAudioPatches(), supportsPatches); |
| 386 | } |
| 387 | |
| 388 | status_t DeviceHalHidl::createAudioPatch( |
| 389 | unsigned int num_sources, |
| 390 | const struct audio_port_config *sources, |
| 391 | unsigned int num_sinks, |
| 392 | const struct audio_port_config *sinks, |
| 393 | audio_patch_handle_t *patch) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 394 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 395 | if (mDevice == 0) return NO_INIT; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 396 | if (patch == nullptr) return BAD_VALUE; |
| 397 | |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 398 | #if MAJOR_VERSION < 6 |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 399 | if (*patch != AUDIO_PATCH_HANDLE_NONE) { |
| 400 | status_t status = releaseAudioPatch(*patch); |
| 401 | ALOGW_IF(status != NO_ERROR, "%s error %d releasing patch handle %d", |
| 402 | __func__, status, *patch); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 403 | *patch = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 404 | } |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 405 | #endif |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 406 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 407 | hidl_vec<AudioPortConfig> hidlSources, hidlSinks; |
| 408 | HidlUtils::audioPortConfigsFromHal(num_sources, sources, &hidlSources); |
| 409 | HidlUtils::audioPortConfigsFromHal(num_sinks, sinks, &hidlSinks); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 410 | Result retval = Result::OK; |
| 411 | Return<void> ret; |
| 412 | std::string methodName = "createAudioPatch"; |
| 413 | if (*patch == AUDIO_PATCH_HANDLE_NONE) { // always true for MAJOR_VERSION < 6 |
| 414 | ret = mDevice->createAudioPatch( |
| 415 | hidlSources, hidlSinks, |
| 416 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 417 | retval = r; |
| 418 | if (retval == Result::OK) { |
| 419 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 420 | } |
| 421 | }); |
| 422 | } else { |
| 423 | #if MAJOR_VERSION >= 6 |
| 424 | ret = mDevice->updateAudioPatch( |
| 425 | *patch, |
| 426 | hidlSources, hidlSinks, |
| 427 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 428 | retval = r; |
| 429 | if (retval == Result::OK) { |
| 430 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 431 | } |
| 432 | }); |
| 433 | methodName = "updateAudioPatch"; |
| 434 | #endif |
| 435 | } |
| 436 | return processReturn(methodName.c_str(), ret, retval); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | status_t DeviceHalHidl::releaseAudioPatch(audio_patch_handle_t patch) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 440 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 441 | if (mDevice == 0) return NO_INIT; |
| 442 | return processReturn("releaseAudioPatch", mDevice->releaseAudioPatch(patch)); |
| 443 | } |
| 444 | |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 445 | template <typename HalPort> |
| 446 | status_t DeviceHalHidl::getAudioPortImpl(HalPort *port) { |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 447 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 448 | if (mDevice == 0) return NO_INIT; |
| 449 | AudioPort hidlPort; |
| 450 | HidlUtils::audioPortFromHal(*port, &hidlPort); |
| 451 | Result retval; |
| 452 | Return<void> ret = mDevice->getAudioPort( |
| 453 | hidlPort, |
| 454 | [&](Result r, const AudioPort& p) { |
| 455 | retval = r; |
| 456 | if (retval == Result::OK) { |
| 457 | HidlUtils::audioPortToHal(p, port); |
| 458 | } |
| 459 | }); |
| 460 | return processReturn("getAudioPort", ret, retval); |
| 461 | } |
| 462 | |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 463 | status_t DeviceHalHidl::getAudioPort(struct audio_port *port) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 464 | TIME_CHECK(); |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 465 | return getAudioPortImpl(port); |
| 466 | } |
| 467 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 468 | status_t DeviceHalHidl::getAudioPort(struct audio_port_v7 *port) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 469 | TIME_CHECK(); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 470 | #if MAJOR_VERSION >= 7 |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 471 | return getAudioPortImpl(port); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 472 | #else |
| 473 | struct audio_port audioPort = {}; |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 474 | status_t result = NO_ERROR; |
| 475 | if (!audio_populate_audio_port(port, &audioPort)) { |
| 476 | ALOGE("Failed to populate legacy audio port from audio_port_v7"); |
| 477 | result = BAD_VALUE; |
| 478 | } |
| 479 | status_t status = getAudioPort(&audioPort); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 480 | if (status == NO_ERROR) { |
| 481 | audio_populate_audio_port_v7(&audioPort, port); |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 482 | } else { |
| 483 | result = status; |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 484 | } |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 485 | return result; |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 486 | #endif |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 489 | status_t DeviceHalHidl::setAudioPortConfig(const struct audio_port_config *config) { |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 490 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPortConfig; |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 491 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 492 | if (mDevice == 0) return NO_INIT; |
| 493 | AudioPortConfig hidlConfig; |
| 494 | HidlUtils::audioPortConfigFromHal(*config, &hidlConfig); |
| 495 | return processReturn("setAudioPortConfig", mDevice->setAudioPortConfig(hidlConfig)); |
| 496 | } |
| 497 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 498 | #if MAJOR_VERSION == 2 |
| 499 | status_t DeviceHalHidl::getMicrophones( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 500 | std::vector<audio_microphone_characteristic_t> *microphonesInfo __unused) { |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 501 | if (mDevice == 0) return NO_INIT; |
| 502 | return INVALID_OPERATION; |
| 503 | } |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 504 | #elif MAJOR_VERSION >= 4 |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 505 | status_t DeviceHalHidl::getMicrophones( |
| 506 | std::vector<audio_microphone_characteristic_t> *microphonesInfo) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 507 | TIME_CHECK(); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 508 | if (mDevice == 0) return NO_INIT; |
| 509 | Result retval; |
| 510 | Return<void> ret = mDevice->getMicrophones( |
| 511 | [&](Result r, hidl_vec<MicrophoneInfo> micArrayHal) { |
| 512 | retval = r; |
| 513 | for (size_t k = 0; k < micArrayHal.size(); k++) { |
| 514 | audio_microphone_characteristic_t dst; |
| 515 | //convert |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 516 | (void)CoreUtils::microphoneInfoToHal(micArrayHal[k], &dst); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 517 | microphonesInfo->push_back(dst); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 518 | } |
| 519 | }); |
| 520 | return processReturn("getMicrophones", ret, retval); |
| 521 | } |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 522 | #endif |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 523 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 524 | #if MAJOR_VERSION >= 6 |
| 525 | status_t DeviceHalHidl::addDeviceEffect( |
| 526 | audio_port_handle_t device, sp<EffectHalInterface> effect) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 527 | TIME_CHECK(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 528 | if (mDevice == 0) return NO_INIT; |
| 529 | return processReturn("addDeviceEffect", mDevice->addDeviceEffect( |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 530 | static_cast<AudioPortHandle>(device), effect->effectId())); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 531 | } |
| 532 | #else |
| 533 | status_t DeviceHalHidl::addDeviceEffect( |
| 534 | audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) { |
| 535 | return INVALID_OPERATION; |
| 536 | } |
| 537 | #endif |
| 538 | |
| 539 | #if MAJOR_VERSION >= 6 |
| 540 | status_t DeviceHalHidl::removeDeviceEffect( |
| 541 | audio_port_handle_t device, sp<EffectHalInterface> effect) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 542 | TIME_CHECK(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 543 | if (mDevice == 0) return NO_INIT; |
| 544 | return processReturn("removeDeviceEffect", mDevice->removeDeviceEffect( |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 545 | static_cast<AudioPortHandle>(device), effect->effectId())); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 546 | } |
| 547 | #else |
| 548 | status_t DeviceHalHidl::removeDeviceEffect( |
| 549 | audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) { |
| 550 | return INVALID_OPERATION; |
| 551 | } |
| 552 | #endif |
| 553 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 554 | status_t DeviceHalHidl::prepareToDisconnectExternalDevice(const struct audio_port_v7* port) { |
| 555 | // For HIDL HAL, there is not API to call notify the HAL to prepare for device connected |
| 556 | // state changed. Call `setConnectedState` directly. |
| 557 | const status_t status = setConnectedState(port, false /*connected*/); |
| 558 | if (status == NO_ERROR) { |
| 559 | // Cache the port id so that it won't disconnect twice. |
| 560 | mDeviceDisconnectionNotified.insert(port->id); |
| 561 | } |
| 562 | return status; |
| 563 | } |
| 564 | |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 565 | status_t DeviceHalHidl::setConnectedState(const struct audio_port_v7 *port, bool connected) { |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 566 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort; |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 567 | TIME_CHECK(); |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 568 | if (mDevice == 0) return NO_INIT; |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 569 | if (!connected && mDeviceDisconnectionNotified.erase(port->id) > 0) { |
| 570 | // For device disconnection, APM will first call `prepareToDisconnectExternalDevice` and |
| 571 | // then call `setConnectedState`. However, in HIDL HAL, there is no API for |
| 572 | // `prepareToDisconnectExternalDevice`. In that case, HIDL HAL will call `setConnectedState` |
| 573 | // when calling `prepareToDisconnectExternalDevice`. Do not call to the HAL if previous |
| 574 | // call is successful. Also remove the cache here to avoid a large cache after a long run. |
| 575 | return NO_ERROR; |
| 576 | } |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 577 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 578 | if (supportsSetConnectedState7_1) { |
| 579 | AudioPort hidlPort; |
| 580 | if (status_t result = HidlUtils::audioPortFromHal(*port, &hidlPort); result != NO_ERROR) { |
| 581 | return result; |
| 582 | } |
| 583 | Return<Result> ret = mDevice->setConnectedState_7_1(hidlPort, connected); |
| 584 | if (!ret.isOk() || ret != Result::NOT_SUPPORTED) { |
| 585 | return processReturn("setConnectedState_7_1", ret); |
| 586 | } else if (ret == Result::OK) { |
| 587 | return NO_ERROR; |
| 588 | } |
| 589 | supportsSetConnectedState7_1 = false; |
| 590 | } |
| 591 | #endif |
| 592 | DeviceAddress hidlAddress; |
| 593 | if (status_t result = CoreUtils::deviceAddressFromHal( |
| 594 | port->ext.device.type, port->ext.device.address, &hidlAddress); |
| 595 | result != NO_ERROR) { |
| 596 | return result; |
| 597 | } |
| 598 | return processReturn("setConnectedState", mDevice->setConnectedState(hidlAddress, connected)); |
| 599 | } |
| 600 | |
Ytai Ben-Tsvi | 48287b5 | 2021-12-01 15:07:11 -0800 | [diff] [blame] | 601 | error::Result<audio_hw_sync_t> DeviceHalHidl::getHwAvSync() { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 602 | TIME_CHECK(); |
Ytai Ben-Tsvi | 48287b5 | 2021-12-01 15:07:11 -0800 | [diff] [blame] | 603 | if (mDevice == 0) return NO_INIT; |
| 604 | audio_hw_sync_t value; |
| 605 | Result result; |
| 606 | Return<void> ret = mDevice->getHwAvSync([&value, &result](Result r, audio_hw_sync_t v) { |
| 607 | value = v; |
| 608 | result = r; |
| 609 | }); |
| 610 | RETURN_IF_ERROR(processReturn("getHwAvSync", ret, result)); |
| 611 | return value; |
| 612 | } |
| 613 | |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 614 | status_t DeviceHalHidl::dump(int fd, const Vector<String16>& args) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 615 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 616 | if (mDevice == 0) return NO_INIT; |
| 617 | native_handle_t* hidlHandle = native_handle_create(1, 0); |
| 618 | hidlHandle->data[0] = fd; |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 619 | hidl_vec<hidl_string> hidlArgs; |
| 620 | argsFromHal(args, &hidlArgs); |
| 621 | Return<void> ret = mDevice->debug(hidlHandle, hidlArgs); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 622 | native_handle_delete(hidlHandle); |
Andy Hung | e72ff02 | 2021-08-16 10:16:15 -0700 | [diff] [blame] | 623 | |
| 624 | // TODO(b/111997867, b/177271958) Workaround - remove when fixed. |
| 625 | // A Binder transmitted fd may not close immediately due to a race condition b/111997867 |
| 626 | // when the remote binder thread removes the last refcount to the fd blocks in the |
| 627 | // kernel for binder activity. We send a Binder ping() command to unblock the thread |
| 628 | // and complete the fd close / release. |
| 629 | // |
| 630 | // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(), |
| 631 | // EffectsFactoryHalHidl::dumpEffects(). |
| 632 | |
| 633 | (void)mDevice->ping(); // synchronous Binder call |
| 634 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 635 | return processReturn("dump", ret); |
| 636 | } |
| 637 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 638 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 639 | status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module, |
| 640 | ::ndk::SpAIBinder* soundDoseBinder) { |
| 641 | if (mSoundDoseWrapper->mSoundDose != nullptr) { |
| 642 | *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder(); |
| 643 | return OK; |
| 644 | } |
| 645 | |
| 646 | if (mSoundDoseWrapper->mSoundDoseFactory == nullptr) { |
| 647 | std::string interface = |
| 648 | std::string(ISoundDoseFactory::descriptor) + kSoundDoseInterfaceModule.data(); |
| 649 | AIBinder* binder = AServiceManager_checkService(interface.c_str()); |
| 650 | if (binder == nullptr) { |
| 651 | ALOGW("%s service %s doesn't exist", __func__, interface.c_str()); |
| 652 | return NO_INIT; |
| 653 | } |
| 654 | mSoundDoseWrapper->mSoundDoseFactory = |
| 655 | ISoundDoseFactory::fromBinder(ndk::SpAIBinder(binder)); |
| 656 | } |
| 657 | |
| 658 | auto result = mSoundDoseWrapper->mSoundDoseFactory->getSoundDose( |
| 659 | module, &mSoundDoseWrapper->mSoundDose); |
| 660 | if (!result.isOk()) { |
| 661 | ALOGW("%s could not get sound dose interface: %s", __func__, result.getMessage()); |
| 662 | return BAD_VALUE; |
| 663 | } |
| 664 | |
| 665 | if (mSoundDoseWrapper->mSoundDose == nullptr) { |
| 666 | ALOGW("%s standalone sound dose interface is not implemented", __func__); |
| 667 | *soundDoseBinder = nullptr; |
| 668 | return OK; |
| 669 | } |
| 670 | |
| 671 | *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder(); |
| 672 | ALOGI("%s using standalone sound dose interface", __func__); |
| 673 | return OK; |
| 674 | } |
| 675 | #else |
| 676 | status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module, |
| 677 | ::ndk::SpAIBinder* soundDoseBinder) { |
| 678 | (void)module; // avoid unused param |
| 679 | (void)soundDoseBinder; // avoid unused param |
| 680 | return INVALID_OPERATION; |
| 681 | } |
| 682 | #endif |
| 683 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 684 | } // namespace android |