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