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