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 | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 35 | #include "EffectHalHidl.h" |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 36 | #include "ParameterUtils.h" |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 37 | #include "StreamHalHidl.h" |
| 38 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 39 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 40 | #include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h> |
| 41 | #include <aidl/android/hardware/audio/sounddose/BpSoundDoseFactory.h> |
| 42 | #include <android/binder_manager.h> |
| 43 | |
| 44 | constexpr std::string_view kSoundDoseInterfaceModule = "/default"; |
| 45 | |
| 46 | using aidl::android::hardware::audio::core::sounddose::ISoundDose; |
| 47 | using aidl::android::hardware::audio::sounddose::ISoundDoseFactory; |
| 48 | #endif |
| 49 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 50 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils; |
Kevin Rocard | 7a9f05a | 2018-11-28 16:52:25 -0800 | [diff] [blame] | 51 | using ::android::hardware::audio::common::utils::EnumBitfield; |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 52 | using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::implementation::CoreUtils; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 53 | using ::android::hardware::hidl_string; |
| 54 | using ::android::hardware::hidl_vec; |
| 55 | |
| 56 | namespace android { |
| 57 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 58 | using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; |
| 59 | using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION; |
Mikhail Naganov | 9ccaa16 | 2018-12-12 10:27:29 -0800 | [diff] [blame] | 60 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 61 | class DeviceHalHidl::SoundDoseWrapper { |
| 62 | public: |
| 63 | SoundDoseWrapper() = default; |
| 64 | ~SoundDoseWrapper() = default; |
| 65 | |
| 66 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 67 | std::shared_ptr<ISoundDoseFactory> mSoundDoseFactory; |
| 68 | std::shared_ptr<ISoundDose> mSoundDose; |
| 69 | #endif |
| 70 | }; |
| 71 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 72 | DeviceHalHidl::DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IDevice>& device) |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 73 | : CoreConversionHelperHidl("DeviceHalHidl"), |
| 74 | mDevice(device), |
| 75 | mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) { |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 76 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 77 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 78 | DeviceHalHidl::DeviceHalHidl( |
| 79 | const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice>& device) |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 80 | : CoreConversionHelperHidl("DeviceHalHidl"), |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 81 | #if MAJOR_VERSION <= 6 || (MAJOR_VERSION == 7 && MINOR_VERSION == 0) |
| 82 | mDevice(device), |
| 83 | #endif |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 84 | mPrimaryDevice(device), |
| 85 | mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) { |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 86 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 87 | auto getDeviceRet = mPrimaryDevice->getDevice(); |
| 88 | if (getDeviceRet.isOk()) { |
| 89 | mDevice = getDeviceRet; |
| 90 | } else { |
| 91 | ALOGE("Call to IPrimaryDevice.getDevice has failed: %s", |
| 92 | getDeviceRet.description().c_str()); |
| 93 | } |
| 94 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | DeviceHalHidl::~DeviceHalHidl() { |
| 98 | if (mDevice != 0) { |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 99 | #if MAJOR_VERSION <= 5 |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 100 | mDevice.clear(); |
| 101 | hardware::IPCThreadState::self()->flushCommands(); |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 102 | #elif MAJOR_VERSION >= 6 |
| 103 | mDevice->close(); |
| 104 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 108 | status_t DeviceHalHidl::getAudioPorts( |
| 109 | std::vector<media::audio::common::AudioPort> *ports __unused) { |
| 110 | return INVALID_OPERATION; |
| 111 | } |
| 112 | |
| 113 | status_t DeviceHalHidl::getAudioRoutes(std::vector<media::AudioRoute> *routes __unused) { |
| 114 | return INVALID_OPERATION; |
| 115 | } |
| 116 | |
Mikhail Naganov | ffd9771 | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 117 | status_t DeviceHalHidl::getSupportedModes( |
| 118 | std::vector<media::audio::common::AudioMode> *modes __unused) { |
| 119 | return INVALID_OPERATION; |
| 120 | } |
| 121 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 122 | status_t DeviceHalHidl::getSupportedDevices(uint32_t*) { |
| 123 | // Obsolete. |
| 124 | return INVALID_OPERATION; |
| 125 | } |
| 126 | |
| 127 | status_t DeviceHalHidl::initCheck() { |
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 | return processReturn("initCheck", mDevice->initCheck()); |
| 131 | } |
| 132 | |
| 133 | status_t DeviceHalHidl::setVoiceVolume(float volume) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 134 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 135 | if (mDevice == 0) return NO_INIT; |
| 136 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 137 | return processReturn("setVoiceVolume", mPrimaryDevice->setVoiceVolume(volume)); |
| 138 | } |
| 139 | |
| 140 | status_t DeviceHalHidl::setMasterVolume(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; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 143 | return processReturn("setMasterVolume", mDevice->setMasterVolume(volume)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | status_t DeviceHalHidl::getMasterVolume(float *volume) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 147 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 148 | if (mDevice == 0) return NO_INIT; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 149 | Result retval; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 150 | Return<void> ret = mDevice->getMasterVolume( |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 151 | [&](Result r, float v) { |
| 152 | retval = r; |
| 153 | if (retval == Result::OK) { |
| 154 | *volume = v; |
| 155 | } |
| 156 | }); |
| 157 | return processReturn("getMasterVolume", ret, retval); |
| 158 | } |
| 159 | |
| 160 | status_t DeviceHalHidl::setMode(audio_mode_t mode) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 161 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 162 | if (mDevice == 0) return NO_INIT; |
| 163 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 164 | return processReturn("setMode", mPrimaryDevice->setMode(AudioMode(mode))); |
| 165 | } |
| 166 | |
| 167 | status_t DeviceHalHidl::setMicMute(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 | return processReturn("setMicMute", mDevice->setMicMute(state)); |
| 171 | } |
| 172 | |
| 173 | status_t DeviceHalHidl::getMicMute(bool *state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 174 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 175 | if (mDevice == 0) return NO_INIT; |
| 176 | Result retval; |
| 177 | Return<void> ret = mDevice->getMicMute( |
| 178 | [&](Result r, bool mute) { |
| 179 | retval = r; |
| 180 | if (retval == Result::OK) { |
| 181 | *state = mute; |
| 182 | } |
| 183 | }); |
| 184 | return processReturn("getMicMute", ret, retval); |
| 185 | } |
| 186 | |
| 187 | status_t DeviceHalHidl::setMasterMute(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 | return processReturn("setMasterMute", mDevice->setMasterMute(state)); |
| 191 | } |
| 192 | |
| 193 | status_t DeviceHalHidl::getMasterMute(bool *state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 194 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 195 | if (mDevice == 0) return NO_INIT; |
| 196 | Result retval; |
| 197 | Return<void> ret = mDevice->getMasterMute( |
| 198 | [&](Result r, bool mute) { |
| 199 | retval = r; |
| 200 | if (retval == Result::OK) { |
| 201 | *state = mute; |
| 202 | } |
| 203 | }); |
| 204 | return processReturn("getMasterMute", ret, retval); |
| 205 | } |
| 206 | |
| 207 | status_t DeviceHalHidl::setParameters(const String8& kvPairs) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 208 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 209 | if (mDevice == 0) return NO_INIT; |
| 210 | hidl_vec<ParameterValue> hidlParams; |
| 211 | status_t status = parametersFromHal(kvPairs, &hidlParams); |
| 212 | if (status != OK) return status; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 213 | // TODO: change the API so that context and kvPairs are separated |
| 214 | return processReturn("setParameters", |
| 215 | utils::setParameters(mDevice, {} /* context */, hidlParams)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | status_t DeviceHalHidl::getParameters(const String8& keys, String8 *values) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 219 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 220 | values->clear(); |
| 221 | if (mDevice == 0) return NO_INIT; |
| 222 | hidl_vec<hidl_string> hidlKeys; |
| 223 | status_t status = keysFromHal(keys, &hidlKeys); |
| 224 | if (status != OK) return status; |
| 225 | Result retval; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 226 | Return<void> ret = utils::getParameters(mDevice, |
| 227 | {} /* context */, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 228 | hidlKeys, |
| 229 | [&](Result r, const hidl_vec<ParameterValue>& parameters) { |
| 230 | retval = r; |
| 231 | if (retval == Result::OK) { |
| 232 | parametersToHal(parameters, values); |
| 233 | } |
| 234 | }); |
| 235 | return processReturn("getParameters", ret, retval); |
| 236 | } |
| 237 | |
| 238 | status_t DeviceHalHidl::getInputBufferSize( |
Mikhail Naganov | d5b643f | 2024-02-15 11:51:26 -0800 | [diff] [blame] | 239 | struct audio_config *config, size_t *size) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 240 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 241 | if (mDevice == 0) return NO_INIT; |
| 242 | AudioConfig hidlConfig; |
Mikhail Naganov | 05e0319 | 2020-12-14 23:19:54 +0000 | [diff] [blame] | 243 | HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 244 | Result retval; |
| 245 | Return<void> ret = mDevice->getInputBufferSize( |
| 246 | hidlConfig, |
| 247 | [&](Result r, uint64_t bufferSize) { |
| 248 | retval = r; |
| 249 | if (retval == Result::OK) { |
| 250 | *size = static_cast<size_t>(bufferSize); |
| 251 | } |
| 252 | }); |
| 253 | return processReturn("getInputBufferSize", ret, retval); |
| 254 | } |
| 255 | |
| 256 | status_t DeviceHalHidl::openOutputStream( |
| 257 | audio_io_handle_t handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 258 | audio_devices_t deviceType, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 259 | audio_output_flags_t flags, |
| 260 | struct audio_config *config, |
| 261 | const char *address, |
Haofan Wang | bf44e09 | 2024-07-07 15:29:38 -0700 | [diff] [blame] | 262 | sp<StreamOutHalInterface> *outStream, |
| 263 | const std::vector<playback_track_metadata_v7_t>& sourceMetadata) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 264 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 265 | if (mDevice == 0) return NO_INIT; |
| 266 | DeviceAddress hidlDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 267 | if (status_t status = CoreUtils::deviceAddressFromHal(deviceType, address, &hidlDevice); |
| 268 | status != OK) { |
| 269 | return status; |
| 270 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 271 | AudioConfig hidlConfig; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 272 | if (status_t status = HidlUtils::audioConfigFromHal(*config, false /*isInput*/, &hidlConfig); |
| 273 | status != OK) { |
| 274 | return status; |
| 275 | } |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 276 | |
Haofan Wang | bf44e09 | 2024-07-07 15:29:38 -0700 | [diff] [blame] | 277 | #if MAJOR_VERSION == 4 |
| 278 | ::android::hardware::audio::CORE_TYPES_CPP_VERSION::SourceMetadata hidlMetadata; |
| 279 | #else |
| 280 | ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::SourceMetadata hidlMetadata; |
| 281 | #endif |
| 282 | |
| 283 | if (status_t status = CoreUtils::sourceMetadataFromHalV7( |
| 284 | sourceMetadata, true /*ignoreNonVendorTags*/, &hidlMetadata); |
| 285 | status != OK) { |
| 286 | return status; |
| 287 | } |
| 288 | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 289 | #if !(MAJOR_VERSION == 7 && MINOR_VERSION == 1) |
| 290 | //TODO: b/193496180 use spatializer flag at audio HAL when available |
| 291 | if ((flags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 292 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_SPATIALIZER); |
| 293 | flags = (audio_output_flags_t) |
| 294 | (flags | AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 295 | } |
| 296 | #endif |
| 297 | |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 298 | CoreUtils::AudioOutputFlags hidlFlags; |
| 299 | if (status_t status = CoreUtils::audioOutputFlagsFromHal(flags, &hidlFlags); status != OK) { |
| 300 | return status; |
| 301 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 302 | Result retval = Result::NOT_INITIALIZED; |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 303 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 304 | Return<void> ret = mDevice->openOutputStream_7_1( |
| 305 | #else |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 306 | Return<void> ret = mDevice->openOutputStream( |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 307 | #endif |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 308 | handle, hidlDevice, hidlConfig, hidlFlags, |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 309 | #if MAJOR_VERSION >= 4 |
Haofan Wang | bf44e09 | 2024-07-07 15:29:38 -0700 | [diff] [blame] | 310 | hidlMetadata /* metadata */, |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 311 | #endif |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 312 | [&](Result r, const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& result, |
| 313 | const AudioConfig& suggestedConfig) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 314 | retval = r; |
| 315 | if (retval == Result::OK) { |
| 316 | *outStream = new StreamOutHalHidl(result); |
| 317 | } |
| 318 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 319 | }); |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 320 | const status_t status = processReturn("openOutputStream", ret, retval); |
| 321 | cleanupStreams(); |
| 322 | if (status == NO_ERROR) { |
| 323 | mStreams.insert({handle, *outStream}); |
| 324 | } |
| 325 | return status; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | status_t DeviceHalHidl::openInputStream( |
| 329 | audio_io_handle_t handle, |
| 330 | audio_devices_t devices, |
| 331 | struct audio_config *config, |
| 332 | audio_input_flags_t flags, |
| 333 | const char *address, |
| 334 | audio_source_t source, |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 335 | audio_devices_t outputDevice, |
| 336 | const char *outputDeviceAddress, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 337 | sp<StreamInHalInterface> *inStream) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 338 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 339 | if (mDevice == 0) return NO_INIT; |
| 340 | DeviceAddress hidlDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 341 | if (status_t status = CoreUtils::deviceAddressFromHal(devices, address, &hidlDevice); |
| 342 | status != OK) { |
| 343 | return status; |
| 344 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 345 | AudioConfig hidlConfig; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 346 | if (status_t status = HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
| 347 | status != OK) { |
| 348 | return status; |
| 349 | } |
| 350 | CoreUtils::AudioInputFlags hidlFlags; |
Dean Wheatley | ea18634 | 2021-03-12 16:49:47 +1100 | [diff] [blame] | 351 | #if MAJOR_VERSION <= 5 |
| 352 | // Some flags were specific to framework and must not leak to the HAL. |
| 353 | flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FLAG_DIRECT); |
| 354 | #endif |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 355 | if (status_t status = CoreUtils::audioInputFlagsFromHal(flags, &hidlFlags); status != OK) { |
| 356 | return status; |
| 357 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 358 | Result retval = Result::NOT_INITIALIZED; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 359 | #if MAJOR_VERSION == 2 |
Mikhail Naganov | d9499eb | 2018-12-17 16:23:22 -0800 | [diff] [blame] | 360 | auto sinkMetadata = AudioSource(source); |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 361 | #elif MAJOR_VERSION >= 4 |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 362 | // TODO: correctly propagate the tracks sources and volume |
| 363 | // for now, only send the main source at 1dbfs |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 364 | AudioSource hidlSource; |
| 365 | if (status_t status = HidlUtils::audioSourceFromHal(source, &hidlSource); status != OK) { |
| 366 | return status; |
| 367 | } |
| 368 | SinkMetadata sinkMetadata = {{{ .source = std::move(hidlSource), .gain = 1 }}}; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 369 | #endif |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 370 | #if MAJOR_VERSION < 5 |
| 371 | (void)outputDevice; |
| 372 | (void)outputDeviceAddress; |
| 373 | #else |
Mikhail Naganov | c425880 | 2021-02-08 17:14:17 -0800 | [diff] [blame] | 374 | #if MAJOR_VERSION >= 7 |
| 375 | (void)HidlUtils::audioChannelMaskFromHal( |
| 376 | AUDIO_CHANNEL_NONE, true /*isInput*/, &sinkMetadata.tracks[0].channelMask); |
| 377 | #endif |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 378 | if (outputDevice != AUDIO_DEVICE_NONE) { |
| 379 | DeviceAddress hidlOutputDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 380 | if (status_t status = CoreUtils::deviceAddressFromHal( |
| 381 | outputDevice, outputDeviceAddress, &hidlOutputDevice); status != OK) { |
| 382 | return status; |
| 383 | } |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 384 | sinkMetadata.tracks[0].destination.device(std::move(hidlOutputDevice)); |
| 385 | } |
| 386 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 387 | Return<void> ret = mDevice->openInputStream( |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 388 | handle, hidlDevice, hidlConfig, hidlFlags, sinkMetadata, |
Mikhail Naganov | accbe8a | 2022-02-03 23:45:36 +0000 | [diff] [blame] | 389 | [&](Result r, |
| 390 | const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn>& result, |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 391 | const AudioConfig& suggestedConfig) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 392 | retval = r; |
| 393 | if (retval == Result::OK) { |
| 394 | *inStream = new StreamInHalHidl(result); |
| 395 | } |
| 396 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 397 | }); |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 398 | const status_t status = processReturn("openInputStream", ret, retval); |
| 399 | cleanupStreams(); |
| 400 | if (status == NO_ERROR) { |
| 401 | mStreams.insert({handle, *inStream}); |
| 402 | } |
| 403 | return status; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | status_t DeviceHalHidl::supportsAudioPatches(bool *supportsPatches) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 407 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 408 | if (mDevice == 0) return NO_INIT; |
| 409 | return processReturn("supportsAudioPatches", mDevice->supportsAudioPatches(), supportsPatches); |
| 410 | } |
| 411 | |
| 412 | status_t DeviceHalHidl::createAudioPatch( |
| 413 | unsigned int num_sources, |
| 414 | const struct audio_port_config *sources, |
| 415 | unsigned int num_sinks, |
| 416 | const struct audio_port_config *sinks, |
| 417 | audio_patch_handle_t *patch) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 418 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 419 | if (mDevice == 0) return NO_INIT; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 420 | if (patch == nullptr) return BAD_VALUE; |
| 421 | |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 422 | #if MAJOR_VERSION < 6 |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 423 | if (*patch != AUDIO_PATCH_HANDLE_NONE) { |
| 424 | status_t status = releaseAudioPatch(*patch); |
| 425 | ALOGW_IF(status != NO_ERROR, "%s error %d releasing patch handle %d", |
| 426 | __func__, status, *patch); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 427 | *patch = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 428 | } |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 429 | #endif |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 430 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 431 | hidl_vec<AudioPortConfig> hidlSources, hidlSinks; |
| 432 | HidlUtils::audioPortConfigsFromHal(num_sources, sources, &hidlSources); |
| 433 | HidlUtils::audioPortConfigsFromHal(num_sinks, sinks, &hidlSinks); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 434 | Result retval = Result::OK; |
| 435 | Return<void> ret; |
| 436 | std::string methodName = "createAudioPatch"; |
| 437 | if (*patch == AUDIO_PATCH_HANDLE_NONE) { // always true for MAJOR_VERSION < 6 |
| 438 | ret = mDevice->createAudioPatch( |
| 439 | hidlSources, hidlSinks, |
| 440 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 441 | retval = r; |
| 442 | if (retval == Result::OK) { |
| 443 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 444 | } |
| 445 | }); |
| 446 | } else { |
| 447 | #if MAJOR_VERSION >= 6 |
| 448 | ret = mDevice->updateAudioPatch( |
| 449 | *patch, |
| 450 | hidlSources, hidlSinks, |
| 451 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 452 | retval = r; |
| 453 | if (retval == Result::OK) { |
| 454 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 455 | } |
| 456 | }); |
| 457 | methodName = "updateAudioPatch"; |
| 458 | #endif |
| 459 | } |
| 460 | return processReturn(methodName.c_str(), ret, retval); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | status_t DeviceHalHidl::releaseAudioPatch(audio_patch_handle_t patch) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 464 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 465 | if (mDevice == 0) return NO_INIT; |
| 466 | return processReturn("releaseAudioPatch", mDevice->releaseAudioPatch(patch)); |
| 467 | } |
| 468 | |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 469 | template <typename HalPort> |
| 470 | status_t DeviceHalHidl::getAudioPortImpl(HalPort *port) { |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 471 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 472 | if (mDevice == 0) return NO_INIT; |
| 473 | AudioPort hidlPort; |
| 474 | HidlUtils::audioPortFromHal(*port, &hidlPort); |
| 475 | Result retval; |
| 476 | Return<void> ret = mDevice->getAudioPort( |
| 477 | hidlPort, |
| 478 | [&](Result r, const AudioPort& p) { |
| 479 | retval = r; |
| 480 | if (retval == Result::OK) { |
| 481 | HidlUtils::audioPortToHal(p, port); |
| 482 | } |
| 483 | }); |
| 484 | return processReturn("getAudioPort", ret, retval); |
| 485 | } |
| 486 | |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 487 | status_t DeviceHalHidl::getAudioPort(struct audio_port *port) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 488 | TIME_CHECK(); |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 489 | return getAudioPortImpl(port); |
| 490 | } |
| 491 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 492 | status_t DeviceHalHidl::getAudioPort(struct audio_port_v7 *port) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 493 | TIME_CHECK(); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 494 | #if MAJOR_VERSION >= 7 |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 495 | return getAudioPortImpl(port); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 496 | #else |
| 497 | struct audio_port audioPort = {}; |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 498 | status_t result = NO_ERROR; |
| 499 | if (!audio_populate_audio_port(port, &audioPort)) { |
| 500 | ALOGE("Failed to populate legacy audio port from audio_port_v7"); |
| 501 | result = BAD_VALUE; |
| 502 | } |
| 503 | status_t status = getAudioPort(&audioPort); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 504 | if (status == NO_ERROR) { |
| 505 | audio_populate_audio_port_v7(&audioPort, port); |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 506 | } else { |
| 507 | result = status; |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 508 | } |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 509 | return result; |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 510 | #endif |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 513 | status_t DeviceHalHidl::setAudioPortConfig(const struct audio_port_config *config) { |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 514 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPortConfig; |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 515 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 516 | if (mDevice == 0) return NO_INIT; |
| 517 | AudioPortConfig hidlConfig; |
| 518 | HidlUtils::audioPortConfigFromHal(*config, &hidlConfig); |
| 519 | return processReturn("setAudioPortConfig", mDevice->setAudioPortConfig(hidlConfig)); |
| 520 | } |
| 521 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 522 | #if MAJOR_VERSION == 2 |
| 523 | status_t DeviceHalHidl::getMicrophones( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 524 | std::vector<audio_microphone_characteristic_t> *microphonesInfo __unused) { |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 525 | if (mDevice == 0) return NO_INIT; |
| 526 | return INVALID_OPERATION; |
| 527 | } |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 528 | #elif MAJOR_VERSION >= 4 |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 529 | status_t DeviceHalHidl::getMicrophones( |
| 530 | std::vector<audio_microphone_characteristic_t> *microphonesInfo) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 531 | TIME_CHECK(); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 532 | if (mDevice == 0) return NO_INIT; |
| 533 | Result retval; |
| 534 | Return<void> ret = mDevice->getMicrophones( |
| 535 | [&](Result r, hidl_vec<MicrophoneInfo> micArrayHal) { |
| 536 | retval = r; |
| 537 | for (size_t k = 0; k < micArrayHal.size(); k++) { |
| 538 | audio_microphone_characteristic_t dst; |
| 539 | //convert |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 540 | (void)CoreUtils::microphoneInfoToHal(micArrayHal[k], &dst); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 541 | microphonesInfo->push_back(dst); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 542 | } |
| 543 | }); |
| 544 | return processReturn("getMicrophones", ret, retval); |
| 545 | } |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 546 | #endif |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 547 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 548 | #if MAJOR_VERSION >= 6 |
| 549 | status_t DeviceHalHidl::addDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 550 | const struct audio_port_config *device, sp<EffectHalInterface> effect) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 551 | TIME_CHECK(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 552 | if (mDevice == 0) return NO_INIT; |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 553 | auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 554 | return processReturn("addDeviceEffect", mDevice->addDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 555 | static_cast<AudioPortHandle>(device->id), hidlEffect->effectId())); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 556 | } |
| 557 | #else |
| 558 | status_t DeviceHalHidl::addDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 559 | const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 560 | return INVALID_OPERATION; |
| 561 | } |
| 562 | #endif |
| 563 | |
| 564 | #if MAJOR_VERSION >= 6 |
| 565 | status_t DeviceHalHidl::removeDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 566 | const struct audio_port_config *device, sp<EffectHalInterface> effect) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 567 | TIME_CHECK(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 568 | if (mDevice == 0) return NO_INIT; |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 569 | auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 570 | return processReturn("removeDeviceEffect", mDevice->removeDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 571 | static_cast<AudioPortHandle>(device->id), hidlEffect->effectId())); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 572 | } |
| 573 | #else |
| 574 | status_t DeviceHalHidl::removeDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 575 | const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 576 | return INVALID_OPERATION; |
| 577 | } |
| 578 | #endif |
| 579 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 580 | status_t DeviceHalHidl::prepareToDisconnectExternalDevice(const struct audio_port_v7* port) { |
| 581 | // For HIDL HAL, there is not API to call notify the HAL to prepare for device connected |
| 582 | // state changed. Call `setConnectedState` directly. |
| 583 | const status_t status = setConnectedState(port, false /*connected*/); |
| 584 | if (status == NO_ERROR) { |
| 585 | // Cache the port id so that it won't disconnect twice. |
| 586 | mDeviceDisconnectionNotified.insert(port->id); |
| 587 | } |
| 588 | return status; |
| 589 | } |
| 590 | |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 591 | status_t DeviceHalHidl::setConnectedState(const struct audio_port_v7 *port, bool connected) { |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 592 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort; |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 593 | TIME_CHECK(); |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 594 | if (mDevice == 0) return NO_INIT; |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 595 | if (!connected && mDeviceDisconnectionNotified.erase(port->id) > 0) { |
| 596 | // For device disconnection, APM will first call `prepareToDisconnectExternalDevice` and |
| 597 | // then call `setConnectedState`. However, in HIDL HAL, there is no API for |
| 598 | // `prepareToDisconnectExternalDevice`. In that case, HIDL HAL will call `setConnectedState` |
| 599 | // when calling `prepareToDisconnectExternalDevice`. Do not call to the HAL if previous |
| 600 | // call is successful. Also remove the cache here to avoid a large cache after a long run. |
| 601 | return NO_ERROR; |
| 602 | } |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 603 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 604 | if (supportsSetConnectedState7_1) { |
| 605 | AudioPort hidlPort; |
| 606 | if (status_t result = HidlUtils::audioPortFromHal(*port, &hidlPort); result != NO_ERROR) { |
| 607 | return result; |
| 608 | } |
| 609 | Return<Result> ret = mDevice->setConnectedState_7_1(hidlPort, connected); |
| 610 | if (!ret.isOk() || ret != Result::NOT_SUPPORTED) { |
| 611 | return processReturn("setConnectedState_7_1", ret); |
| 612 | } else if (ret == Result::OK) { |
| 613 | return NO_ERROR; |
| 614 | } |
| 615 | supportsSetConnectedState7_1 = false; |
| 616 | } |
| 617 | #endif |
| 618 | DeviceAddress hidlAddress; |
| 619 | if (status_t result = CoreUtils::deviceAddressFromHal( |
| 620 | port->ext.device.type, port->ext.device.address, &hidlAddress); |
| 621 | result != NO_ERROR) { |
| 622 | return result; |
| 623 | } |
| 624 | return processReturn("setConnectedState", mDevice->setConnectedState(hidlAddress, connected)); |
| 625 | } |
| 626 | |
Ytai Ben-Tsvi | 48287b5 | 2021-12-01 15:07:11 -0800 | [diff] [blame] | 627 | error::Result<audio_hw_sync_t> DeviceHalHidl::getHwAvSync() { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 628 | TIME_CHECK(); |
Ytai Ben-Tsvi | 48287b5 | 2021-12-01 15:07:11 -0800 | [diff] [blame] | 629 | if (mDevice == 0) return NO_INIT; |
| 630 | audio_hw_sync_t value; |
| 631 | Result result; |
| 632 | Return<void> ret = mDevice->getHwAvSync([&value, &result](Result r, audio_hw_sync_t v) { |
| 633 | value = v; |
| 634 | result = r; |
| 635 | }); |
| 636 | RETURN_IF_ERROR(processReturn("getHwAvSync", ret, result)); |
| 637 | return value; |
| 638 | } |
| 639 | |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 640 | status_t DeviceHalHidl::dump(int fd, const Vector<String16>& args) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 641 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 642 | if (mDevice == 0) return NO_INIT; |
| 643 | native_handle_t* hidlHandle = native_handle_create(1, 0); |
| 644 | hidlHandle->data[0] = fd; |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 645 | hidl_vec<hidl_string> hidlArgs; |
| 646 | argsFromHal(args, &hidlArgs); |
| 647 | Return<void> ret = mDevice->debug(hidlHandle, hidlArgs); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 648 | native_handle_delete(hidlHandle); |
Andy Hung | e72ff02 | 2021-08-16 10:16:15 -0700 | [diff] [blame] | 649 | |
| 650 | // TODO(b/111997867, b/177271958) Workaround - remove when fixed. |
| 651 | // A Binder transmitted fd may not close immediately due to a race condition b/111997867 |
| 652 | // when the remote binder thread removes the last refcount to the fd blocks in the |
| 653 | // kernel for binder activity. We send a Binder ping() command to unblock the thread |
| 654 | // and complete the fd close / release. |
| 655 | // |
| 656 | // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(), |
| 657 | // EffectsFactoryHalHidl::dumpEffects(). |
| 658 | |
| 659 | (void)mDevice->ping(); // synchronous Binder call |
| 660 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 661 | return processReturn("dump", ret); |
| 662 | } |
| 663 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 664 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 665 | status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module, |
| 666 | ::ndk::SpAIBinder* soundDoseBinder) { |
| 667 | if (mSoundDoseWrapper->mSoundDose != nullptr) { |
| 668 | *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder(); |
| 669 | return OK; |
| 670 | } |
| 671 | |
| 672 | if (mSoundDoseWrapper->mSoundDoseFactory == nullptr) { |
| 673 | std::string interface = |
| 674 | std::string(ISoundDoseFactory::descriptor) + kSoundDoseInterfaceModule.data(); |
| 675 | AIBinder* binder = AServiceManager_checkService(interface.c_str()); |
| 676 | if (binder == nullptr) { |
| 677 | ALOGW("%s service %s doesn't exist", __func__, interface.c_str()); |
| 678 | return NO_INIT; |
| 679 | } |
| 680 | mSoundDoseWrapper->mSoundDoseFactory = |
| 681 | ISoundDoseFactory::fromBinder(ndk::SpAIBinder(binder)); |
| 682 | } |
| 683 | |
| 684 | auto result = mSoundDoseWrapper->mSoundDoseFactory->getSoundDose( |
| 685 | module, &mSoundDoseWrapper->mSoundDose); |
| 686 | if (!result.isOk()) { |
| 687 | ALOGW("%s could not get sound dose interface: %s", __func__, result.getMessage()); |
| 688 | return BAD_VALUE; |
| 689 | } |
| 690 | |
| 691 | if (mSoundDoseWrapper->mSoundDose == nullptr) { |
| 692 | ALOGW("%s standalone sound dose interface is not implemented", __func__); |
| 693 | *soundDoseBinder = nullptr; |
| 694 | return OK; |
| 695 | } |
| 696 | |
| 697 | *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder(); |
| 698 | ALOGI("%s using standalone sound dose interface", __func__); |
| 699 | return OK; |
| 700 | } |
| 701 | #else |
| 702 | status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module, |
| 703 | ::ndk::SpAIBinder* soundDoseBinder) { |
| 704 | (void)module; // avoid unused param |
| 705 | (void)soundDoseBinder; // avoid unused param |
| 706 | return INVALID_OPERATION; |
| 707 | } |
| 708 | #endif |
| 709 | |
Eric Laurent | 7af6ee7 | 2023-06-29 11:44:54 +0200 | [diff] [blame] | 710 | status_t DeviceHalHidl::supportsBluetoothVariableLatency(bool* supports) { |
| 711 | if (supports == nullptr) { |
| 712 | return BAD_VALUE; |
| 713 | } |
| 714 | *supports = false; |
| 715 | |
| 716 | String8 reply; |
| 717 | status_t status = getParameters( |
| 718 | String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), &reply); |
| 719 | if (status != NO_ERROR) { |
| 720 | return status; |
| 721 | } |
| 722 | AudioParameter replyParams(reply); |
| 723 | String8 trueOrFalse; |
| 724 | status = replyParams.get( |
| 725 | String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), trueOrFalse); |
| 726 | if (status != NO_ERROR) { |
| 727 | return status; |
| 728 | } |
| 729 | *supports = trueOrFalse == AudioParameter::valueTrue; |
| 730 | return NO_ERROR; |
| 731 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 732 | |
| 733 | namespace { |
| 734 | |
| 735 | status_t getParametersFromStream( |
| 736 | sp<StreamHalInterface> stream, |
| 737 | const char* parameters, |
| 738 | const char* extraParameters, |
| 739 | String8* reply) { |
| 740 | String8 request(parameters); |
| 741 | if (extraParameters != nullptr) { |
| 742 | request.append(";"); |
| 743 | request.append(extraParameters); |
| 744 | } |
| 745 | status_t status = stream->getParameters(request, reply); |
| 746 | if (status != NO_ERROR) { |
| 747 | ALOGW("%s, failed to query %s, status=%d", __func__, parameters, status); |
| 748 | return status; |
| 749 | } |
| 750 | AudioParameter repliedParameters(*reply); |
| 751 | status = repliedParameters.get(String8(parameters), *reply); |
| 752 | if (status != NO_ERROR) { |
| 753 | ALOGW("%s: failed to retrieve %s, bailing out", __func__, parameters); |
| 754 | } |
| 755 | return status; |
| 756 | } |
| 757 | |
| 758 | } // namespace |
| 759 | |
| 760 | status_t DeviceHalHidl::getAudioMixPort(const struct audio_port_v7 *devicePort, |
| 761 | struct audio_port_v7 *mixPort) { |
| 762 | // For HIDL HAL, querying mix port information is not supported. If the HAL supports |
| 763 | // `getAudioPort` API to query the device port attributes, use the structured audio profiles |
| 764 | // that have the same attributes reported by the `getParameters` API. Otherwise, only use |
| 765 | // the attributes reported by `getParameters` API. |
| 766 | struct audio_port_v7 temp = *devicePort; |
| 767 | AudioProfileAttributesMultimap attrsFromDevice; |
jiabin | 19a3d72 | 2024-03-15 16:09:36 +0000 | [diff] [blame] | 768 | bool supportsPatches; |
| 769 | if (supportsAudioPatches(&supportsPatches) == OK && supportsPatches) { |
| 770 | // The audio patches are supported since HAL 3.0, which is the same HAL version |
| 771 | // requirement for 'getAudioPort' API. |
| 772 | if (getAudioPort(&temp) == NO_ERROR) { |
| 773 | attrsFromDevice = createAudioProfilesAttrMap(temp.audio_profiles, 0 /*first*/, |
| 774 | temp.num_audio_profiles); |
| 775 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 776 | } |
| 777 | auto streamIt = mStreams.find(mixPort->ext.mix.handle); |
| 778 | if (streamIt == mStreams.end()) { |
| 779 | return BAD_VALUE; |
| 780 | } |
| 781 | auto stream = streamIt->second.promote(); |
| 782 | if (stream == nullptr) { |
| 783 | return BAD_VALUE; |
| 784 | } |
| 785 | |
| 786 | String8 formatsStr; |
jiabin | 19a3d72 | 2024-03-15 16:09:36 +0000 | [diff] [blame] | 787 | status_t status = getParametersFromStream( |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 788 | stream, AudioParameter::keyStreamSupportedFormats, nullptr /*extraParameters*/, |
| 789 | &formatsStr); |
| 790 | if (status != NO_ERROR) { |
| 791 | return status; |
| 792 | } |
| 793 | FormatVector formats = formatsFromString(formatsStr.c_str()); |
| 794 | |
| 795 | mixPort->num_audio_profiles = 0; |
| 796 | for (audio_format_t format : formats) { |
| 797 | if (mixPort->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) { |
| 798 | ALOGW("%s, too many audio profiles", __func__); |
| 799 | break; |
| 800 | } |
| 801 | AudioParameter formatParameter; |
| 802 | formatParameter.addInt(String8(AudioParameter::keyFormat), format); |
| 803 | |
| 804 | String8 samplingRatesStr; |
| 805 | status = getParametersFromStream( |
| 806 | stream, AudioParameter::keyStreamSupportedSamplingRates, |
| 807 | formatParameter.toString(), &samplingRatesStr); |
| 808 | if (status != NO_ERROR) { |
| 809 | // Failed to query supported sample rate for current format, may succeed with |
| 810 | // other formats. |
| 811 | ALOGW("Skip adding format=%#x, status=%d", format, status); |
| 812 | continue; |
| 813 | } |
| 814 | SampleRateSet sampleRatesFromStream = samplingRatesFromString(samplingRatesStr.c_str()); |
| 815 | if (sampleRatesFromStream.empty()) { |
| 816 | ALOGW("Skip adding format=%#x as the returned sampling rates are empty", format); |
| 817 | continue; |
| 818 | } |
| 819 | String8 channelMasksStr; |
| 820 | status = getParametersFromStream( |
| 821 | stream, AudioParameter::keyStreamSupportedChannels, |
| 822 | formatParameter.toString(), &channelMasksStr); |
| 823 | if (status != NO_ERROR) { |
| 824 | // Failed to query supported channel masks for current format, may succeed with |
| 825 | // other formats. |
| 826 | ALOGW("Skip adding format=%#x, status=%d", format, status); |
| 827 | continue; |
| 828 | } |
| 829 | ChannelMaskSet channelMasksFromStream = channelMasksFromString(channelMasksStr.c_str()); |
| 830 | if (channelMasksFromStream.empty()) { |
| 831 | ALOGW("Skip adding format=%#x as the returned channel masks are empty", format); |
| 832 | continue; |
| 833 | } |
| 834 | |
| 835 | // For an audio format, all audio profiles from the device port with the same format will |
| 836 | // be added to mix port after filtering sample rates, channel masks according to the reply |
| 837 | // of getParameters API. If there is any sample rate or channel mask reported by |
| 838 | // getParameters API but not reported by the device, additional audio profiles will be |
| 839 | // added. |
| 840 | populateAudioProfiles(attrsFromDevice, format, channelMasksFromStream, |
| 841 | sampleRatesFromStream, mixPort->audio_profiles, |
| 842 | &mixPort->num_audio_profiles); |
| 843 | } |
| 844 | |
| 845 | return NO_ERROR; |
| 846 | } |
| 847 | |
| 848 | void DeviceHalHidl::cleanupStreams() { |
| 849 | for (auto it = mStreams.begin(); it != mStreams.end();) { |
| 850 | if (it->second.promote() == nullptr) { |
| 851 | it = mStreams.erase(it); |
| 852 | } else { |
| 853 | ++it; |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 858 | } // namespace android |