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