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