Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AudioControl" |
| 18 | // #define LOG_NDEBUG 0 |
| 19 | |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 20 | #include "AudioControl.h" |
| 21 | |
| 22 | #include <aidl/android/hardware/automotive/audiocontrol/AudioFocusChange.h> |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 23 | #include <aidl/android/hardware/automotive/audiocontrol/DuckingInfo.h> |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 24 | #include <aidl/android/hardware/automotive/audiocontrol/IFocusListener.h> |
| 25 | |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/parseint.h> |
| 28 | #include <android-base/strings.h> |
| 29 | |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 30 | #include <android_audio_policy_configuration_V7_0-enums.h> |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 31 | #include <private/android_filesystem_config.h> |
| 32 | |
Francois Gaffie | 625a898 | 2022-02-08 17:40:14 +0100 | [diff] [blame] | 33 | #include <numeric> |
| 34 | |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | |
| 37 | namespace aidl::android::hardware::automotive::audiocontrol { |
| 38 | |
| 39 | using ::android::base::EqualsIgnoreCase; |
| 40 | using ::android::base::ParseInt; |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 41 | using ::std::shared_ptr; |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 42 | using ::std::string; |
| 43 | |
| 44 | namespace xsd { |
Hayden Gomes | e502a60 | 2020-11-06 15:15:26 -0800 | [diff] [blame] | 45 | using namespace ::android::audio::policy::configuration::V7_0; |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | namespace { |
| 49 | const float kLowerBound = -1.0f; |
| 50 | const float kUpperBound = 1.0f; |
| 51 | bool checkCallerHasWritePermissions(int fd) { |
| 52 | // Double check that's only called by root - it should be be blocked at debug() level, |
| 53 | // but it doesn't hurt to make sure... |
| 54 | if (AIBinder_getCallingUid() != AID_ROOT) { |
| 55 | dprintf(fd, "Must be root\n"); |
| 56 | return false; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | bool isValidValue(float value) { |
| 62 | return (value >= kLowerBound) && (value <= kUpperBound); |
| 63 | } |
| 64 | |
| 65 | bool safelyParseInt(string s, int* out) { |
| 66 | if (!ParseInt(s, out)) { |
| 67 | return false; |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | } // namespace |
| 72 | |
| 73 | ndk::ScopedAStatus AudioControl::registerFocusListener( |
| 74 | const shared_ptr<IFocusListener>& in_listener) { |
| 75 | LOG(DEBUG) << "registering focus listener"; |
| 76 | |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 77 | if (in_listener) { |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 78 | std::atomic_store(&mFocusListener, in_listener); |
| 79 | } else { |
| 80 | LOG(ERROR) << "Unexpected nullptr for listener resulting in no-op."; |
| 81 | } |
| 82 | return ndk::ScopedAStatus::ok(); |
| 83 | } |
| 84 | |
| 85 | ndk::ScopedAStatus AudioControl::setBalanceTowardRight(float value) { |
| 86 | if (isValidValue(value)) { |
| 87 | // Just log in this default mock implementation |
| 88 | LOG(INFO) << "Balance set to " << value; |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 89 | return ndk::ScopedAStatus::ok(); |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 90 | } |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 91 | |
| 92 | LOG(ERROR) << "Balance value out of range -1 to 1 at " << value; |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 93 | return ndk::ScopedAStatus::ok(); |
| 94 | } |
| 95 | |
| 96 | ndk::ScopedAStatus AudioControl::setFadeTowardFront(float value) { |
| 97 | if (isValidValue(value)) { |
| 98 | // Just log in this default mock implementation |
| 99 | LOG(INFO) << "Fader set to " << value; |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 100 | return ndk::ScopedAStatus::ok(); |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 101 | } |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 102 | |
| 103 | LOG(ERROR) << "Fader value out of range -1 to 1 at " << value; |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 104 | return ndk::ScopedAStatus::ok(); |
| 105 | } |
| 106 | |
| 107 | ndk::ScopedAStatus AudioControl::onAudioFocusChange(const string& in_usage, int32_t in_zoneId, |
| 108 | AudioFocusChange in_focusChange) { |
| 109 | LOG(INFO) << "Focus changed: " << toString(in_focusChange).c_str() << " for usage " |
| 110 | << in_usage.c_str() << " in zone " << in_zoneId; |
| 111 | return ndk::ScopedAStatus::ok(); |
| 112 | } |
| 113 | |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 114 | ndk::ScopedAStatus AudioControl::onDevicesToDuckChange( |
| 115 | const std::vector<DuckingInfo>& in_duckingInfos) { |
| 116 | LOG(INFO) << "AudioControl::onDevicesToDuckChange"; |
| 117 | for (const DuckingInfo& duckingInfo : in_duckingInfos) { |
| 118 | LOG(INFO) << "zone: " << duckingInfo.zoneId; |
| 119 | LOG(INFO) << "Devices to duck:"; |
Oscar Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 120 | for (const auto& addressToDuck : duckingInfo.deviceAddressesToDuck) { |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 121 | LOG(INFO) << addressToDuck; |
| 122 | } |
| 123 | LOG(INFO) << "Devices to unduck:"; |
Oscar Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 124 | for (const auto& addressToUnduck : duckingInfo.deviceAddressesToUnduck) { |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 125 | LOG(INFO) << addressToUnduck; |
| 126 | } |
| 127 | LOG(INFO) << "Usages holding focus:"; |
Oscar Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 128 | for (const auto& usage : duckingInfo.usagesHoldingFocus) { |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 129 | LOG(INFO) << usage; |
| 130 | } |
| 131 | } |
| 132 | return ndk::ScopedAStatus::ok(); |
| 133 | } |
| 134 | |
Oscar Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 135 | ndk::ScopedAStatus AudioControl::onDevicesToMuteChange( |
| 136 | const std::vector<MutingInfo>& in_mutingInfos) { |
| 137 | LOG(INFO) << "AudioControl::onDevicesToMuteChange"; |
| 138 | for (const MutingInfo& mutingInfo : in_mutingInfos) { |
| 139 | LOG(INFO) << "zone: " << mutingInfo.zoneId; |
| 140 | LOG(INFO) << "Devices to mute:"; |
| 141 | for (const auto& addressToMute : mutingInfo.deviceAddressesToMute) { |
| 142 | LOG(INFO) << addressToMute; |
| 143 | } |
| 144 | LOG(INFO) << "Devices to unmute:"; |
| 145 | for (const auto& addressToUnmute : mutingInfo.deviceAddressesToUnmute) { |
| 146 | LOG(INFO) << addressToUnmute; |
| 147 | } |
| 148 | } |
| 149 | return ndk::ScopedAStatus::ok(); |
| 150 | } |
| 151 | |
Francois Gaffie | 625a898 | 2022-02-08 17:40:14 +0100 | [diff] [blame] | 152 | template <typename aidl_type> |
| 153 | static inline std::string toString(const std::vector<aidl_type>& in_values) { |
| 154 | return std::accumulate(std::begin(in_values), std::end(in_values), std::string{}, |
| 155 | [](std::string& ls, const aidl_type& rs) { |
| 156 | return ls += (ls.empty() ? "" : ",") + rs.toString(); |
| 157 | }); |
| 158 | } |
| 159 | template <typename aidl_enum_type> |
| 160 | static inline std::string toEnumString(const std::vector<aidl_enum_type>& in_values) { |
| 161 | return std::accumulate(std::begin(in_values), std::end(in_values), std::string{}, |
| 162 | [](std::string& ls, const aidl_enum_type& rs) { |
| 163 | return ls += (ls.empty() ? "" : ",") + toString(rs); |
| 164 | }); |
| 165 | } |
| 166 | |
| 167 | ndk::ScopedAStatus AudioControl::onAudioFocusChangeWithMetaData( |
| 168 | const audiohalcommon::PlaybackTrackMetadata& in_playbackMetaData, int32_t in_zoneId, |
| 169 | AudioFocusChange in_focusChange) { |
| 170 | LOG(INFO) << "Focus changed: " << toString(in_focusChange).c_str() << " for metadata " |
| 171 | << in_playbackMetaData.toString().c_str() << " in zone " << in_zoneId; |
| 172 | return ndk::ScopedAStatus::ok(); |
| 173 | } |
| 174 | |
| 175 | ndk::ScopedAStatus AudioControl::setAudioDeviceGainsChanged( |
| 176 | const std::vector<Reasons>& in_reasons, const std::vector<AudioGainConfigInfo>& in_gains) { |
| 177 | LOG(INFO) << "Audio Device Gains changed: resons:" << toEnumString(in_reasons).c_str() |
| 178 | << " for devices: " << toString(in_gains).c_str(); |
| 179 | return ndk::ScopedAStatus::ok(); |
| 180 | } |
| 181 | |
| 182 | ndk::ScopedAStatus AudioControl::registerGainCallback( |
| 183 | const std::shared_ptr<IAudioGainCallback>& in_callback) { |
| 184 | LOG(DEBUG) << ": " << __func__; |
| 185 | if (in_callback) { |
| 186 | std::atomic_store(&mAudioGainCallback, in_callback); |
| 187 | } else { |
| 188 | LOG(ERROR) << "Unexpected nullptr for audio gain callback resulting in no-op."; |
| 189 | } |
| 190 | return ndk::ScopedAStatus::ok(); |
| 191 | } |
| 192 | |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 193 | binder_status_t AudioControl::dump(int fd, const char** args, uint32_t numArgs) { |
| 194 | if (numArgs == 0) { |
| 195 | return dumpsys(fd); |
| 196 | } |
| 197 | |
| 198 | string option = string(args[0]); |
| 199 | if (EqualsIgnoreCase(option, "--help")) { |
| 200 | return cmdHelp(fd); |
| 201 | } else if (EqualsIgnoreCase(option, "--request")) { |
| 202 | return cmdRequestFocus(fd, args, numArgs); |
| 203 | } else if (EqualsIgnoreCase(option, "--abandon")) { |
| 204 | return cmdAbandonFocus(fd, args, numArgs); |
Francois Gaffie | 625a898 | 2022-02-08 17:40:14 +0100 | [diff] [blame] | 205 | } else if (EqualsIgnoreCase(option, "--requestFocusWithMetaData")) { |
| 206 | return cmdRequestFocusWithMetaData(fd, args, numArgs); |
| 207 | } else if (EqualsIgnoreCase(option, "--abandonFocusWithMetaData")) { |
| 208 | return cmdAbandonFocusWithMetaData(fd, args, numArgs); |
| 209 | } else if (EqualsIgnoreCase(option, "--audioGainCallback")) { |
| 210 | return cmdOnAudioDeviceGainsChanged(fd, args, numArgs); |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 211 | } else { |
| 212 | dprintf(fd, "Invalid option: %s\n", option.c_str()); |
| 213 | return STATUS_BAD_VALUE; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | binder_status_t AudioControl::dumpsys(int fd) { |
| 218 | if (mFocusListener == nullptr) { |
| 219 | dprintf(fd, "No focus listener registered\n"); |
| 220 | } else { |
| 221 | dprintf(fd, "Focus listener registered\n"); |
| 222 | } |
Francois Gaffie | 625a898 | 2022-02-08 17:40:14 +0100 | [diff] [blame] | 223 | dprintf(fd, "AudioGainCallback %sregistered\n", (mAudioGainCallback == nullptr ? "NOT " : "")); |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 224 | return STATUS_OK; |
| 225 | } |
| 226 | |
| 227 | binder_status_t AudioControl::cmdHelp(int fd) const { |
| 228 | dprintf(fd, "Usage: \n\n"); |
Francois Gaffie | 625a898 | 2022-02-08 17:40:14 +0100 | [diff] [blame] | 229 | dprintf(fd, "[no args]: dumps focus listener / gain callback registered status\n"); |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 230 | dprintf(fd, "--help: shows this help\n"); |
| 231 | dprintf(fd, |
| 232 | "--request <USAGE> <ZONE_ID> <FOCUS_GAIN>: requests audio focus for specified " |
Francois Gaffie | 625a898 | 2022-02-08 17:40:14 +0100 | [diff] [blame] | 233 | "usage (string), audio zone ID (int), and focus gain type (int)\n" |
| 234 | "Deprecated, use MetaData instead\n"); |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 235 | dprintf(fd, |
| 236 | "--abandon <USAGE> <ZONE_ID>: abandons audio focus for specified usage (string) and " |
Francois Gaffie | 625a898 | 2022-02-08 17:40:14 +0100 | [diff] [blame] | 237 | "audio zone ID (int)\n" |
| 238 | "Deprecated, use MetaData instead\n"); |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 239 | dprintf(fd, "See audio_policy_configuration.xsd for valid AudioUsage values.\n"); |
Francois Gaffie | 625a898 | 2022-02-08 17:40:14 +0100 | [diff] [blame] | 240 | |
| 241 | dprintf(fd, |
| 242 | "--requestFocusWithMetaData <METADATA> <ZONE_ID> <FOCUS_GAIN>: " |
| 243 | "requests audio focus for specified metadata, audio zone ID (int), " |
| 244 | "and focus gain type (int)\n"); |
| 245 | dprintf(fd, |
| 246 | "--abandonFocusWithMetaData <METADATA> <ZONE_ID>: " |
| 247 | "abandons audio focus for specified metadata and audio zone ID (int)\n"); |
| 248 | dprintf(fd, |
| 249 | "--audioGainCallback <ZONE_ID> <REASON_1>[,<REASON_N> ...]" |
| 250 | "<DEVICE_ADDRESS_1> <GAIN_INDEX_1> [<DEVICE_ADDRESS_N> <GAIN_INDEX_N> ...]: fire audio " |
| 251 | "gain callback for audio zone ID (int), the given reasons (csv int) for given pairs " |
| 252 | "of device address (string) and gain index (int) \n"); |
| 253 | |
| 254 | dprintf(fd, |
| 255 | "Note on <METADATA>: <USAGE,CONTENT_TYPE[,TAGS]> specified as where (int)usage, " |
| 256 | "(int)content type and tags (string)string)\n"); |
| 257 | dprintf(fd, |
| 258 | "See android/media/audio/common/AudioUsageType.aidl for valid AudioUsage values.\n"); |
| 259 | dprintf(fd, |
| 260 | "See android/media/audio/common/AudioContentType.aidl for valid AudioContentType " |
| 261 | "values.\n"); |
| 262 | dprintf(fd, |
| 263 | "Tags are optional. If provided, it must follow the <key>=<value> pattern, where the " |
| 264 | "value is namespaced (for example com.google.strategy=VR).\n"); |
| 265 | |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 266 | return STATUS_OK; |
| 267 | } |
| 268 | |
| 269 | binder_status_t AudioControl::cmdRequestFocus(int fd, const char** args, uint32_t numArgs) { |
| 270 | if (!checkCallerHasWritePermissions(fd)) { |
| 271 | return STATUS_PERMISSION_DENIED; |
| 272 | } |
| 273 | if (numArgs != 4) { |
| 274 | dprintf(fd, |
| 275 | "Invalid number of arguments: please provide --request <USAGE> <ZONE_ID> " |
| 276 | "<FOCUS_GAIN>\n"); |
| 277 | return STATUS_BAD_VALUE; |
| 278 | } |
| 279 | |
| 280 | string usage = string(args[1]); |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 281 | if (xsd::isUnknownAudioUsage(usage)) { |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 282 | dprintf(fd, |
| 283 | "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 " |
| 284 | "for supported values\n", |
| 285 | usage.c_str()); |
| 286 | return STATUS_BAD_VALUE; |
| 287 | } |
| 288 | |
| 289 | int zoneId; |
| 290 | if (!safelyParseInt(string(args[2]), &zoneId)) { |
| 291 | dprintf(fd, "Non-integer zoneId provided with request: %s\n", string(args[2]).c_str()); |
| 292 | return STATUS_BAD_VALUE; |
| 293 | } |
| 294 | |
| 295 | int focusGainValue; |
| 296 | if (!safelyParseInt(string(args[3]), &focusGainValue)) { |
| 297 | dprintf(fd, "Non-integer focusGain provided with request: %s\n", string(args[3]).c_str()); |
| 298 | return STATUS_BAD_VALUE; |
| 299 | } |
| 300 | AudioFocusChange focusGain = AudioFocusChange(focusGainValue); |
| 301 | |
| 302 | if (mFocusListener == nullptr) { |
| 303 | dprintf(fd, "Unable to request focus - no focus listener registered\n"); |
| 304 | return STATUS_BAD_VALUE; |
| 305 | } |
| 306 | |
| 307 | mFocusListener->requestAudioFocus(usage, zoneId, focusGain); |
| 308 | dprintf(fd, "Requested focus for usage %s, zoneId %d, and focusGain %d\n", usage.c_str(), |
| 309 | zoneId, focusGain); |
| 310 | return STATUS_OK; |
| 311 | } |
| 312 | |
| 313 | binder_status_t AudioControl::cmdAbandonFocus(int fd, const char** args, uint32_t numArgs) { |
| 314 | if (!checkCallerHasWritePermissions(fd)) { |
| 315 | return STATUS_PERMISSION_DENIED; |
| 316 | } |
| 317 | if (numArgs != 3) { |
| 318 | dprintf(fd, "Invalid number of arguments: please provide --abandon <USAGE> <ZONE_ID>\n"); |
| 319 | return STATUS_BAD_VALUE; |
| 320 | } |
| 321 | |
| 322 | string usage = string(args[1]); |
Hayden Gomes | 6333eed | 2021-03-25 11:27:59 -0700 | [diff] [blame] | 323 | if (xsd::isUnknownAudioUsage(usage)) { |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 324 | dprintf(fd, |
| 325 | "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 " |
| 326 | "for supported values\n", |
| 327 | usage.c_str()); |
| 328 | return STATUS_BAD_VALUE; |
| 329 | } |
| 330 | |
| 331 | int zoneId; |
| 332 | if (!safelyParseInt(string(args[2]), &zoneId)) { |
| 333 | dprintf(fd, "Non-integer zoneId provided with abandon: %s\n", string(args[2]).c_str()); |
| 334 | return STATUS_BAD_VALUE; |
| 335 | } |
| 336 | |
| 337 | if (mFocusListener == nullptr) { |
| 338 | dprintf(fd, "Unable to abandon focus - no focus listener registered\n"); |
| 339 | return STATUS_BAD_VALUE; |
| 340 | } |
| 341 | |
| 342 | mFocusListener->abandonAudioFocus(usage, zoneId); |
| 343 | dprintf(fd, "Abandoned focus for usage %s and zoneId %d\n", usage.c_str(), zoneId); |
| 344 | return STATUS_OK; |
| 345 | } |
| 346 | |
Francois Gaffie | 625a898 | 2022-02-08 17:40:14 +0100 | [diff] [blame] | 347 | binder_status_t AudioControl::parseMetaData(int fd, const std::string& metadataLiteral, |
| 348 | audiohalcommon::PlaybackTrackMetadata& trackMetadata) { |
| 349 | std::stringstream csvMetaData(metadataLiteral); |
| 350 | std::vector<std::string> splitMetaData; |
| 351 | std::string attribute; |
| 352 | while (getline(csvMetaData, attribute, ',')) { |
| 353 | splitMetaData.push_back(attribute); |
| 354 | } |
| 355 | if (splitMetaData.size() != 2 && splitMetaData.size() != 3) { |
| 356 | dprintf(fd, |
| 357 | "Invalid metadata: %s, please provide <METADATA> as <USAGE,CONTENT_TYPE[,TAGS]> " |
| 358 | "where (int)usage, (int)content type and tags (string)string)\n", |
| 359 | metadataLiteral.c_str()); |
| 360 | return STATUS_BAD_VALUE; |
| 361 | } |
| 362 | int usage; |
| 363 | if (!safelyParseInt(splitMetaData[0], &usage)) { |
| 364 | dprintf(fd, "Non-integer usage provided with request: %s\n", splitMetaData[0].c_str()); |
| 365 | return STATUS_BAD_VALUE; |
| 366 | } |
| 367 | int contentType; |
| 368 | if (!safelyParseInt(splitMetaData[1], &contentType)) { |
| 369 | dprintf(fd, "Non-integer content type provided with request: %s\n", |
| 370 | splitMetaData[1].c_str()); |
| 371 | return STATUS_BAD_VALUE; |
| 372 | } |
| 373 | const std::string tags = (splitMetaData.size() == 3 ? splitMetaData[2] : ""); |
| 374 | |
| 375 | trackMetadata = {.usage = static_cast<audiomediacommon::AudioUsage>(usage), |
| 376 | .contentType = static_cast<audiomediacommon::AudioContentType>(contentType), |
| 377 | .tags = {tags}}; |
| 378 | return STATUS_OK; |
| 379 | } |
| 380 | |
| 381 | binder_status_t AudioControl::cmdRequestFocusWithMetaData(int fd, const char** args, |
| 382 | uint32_t numArgs) { |
| 383 | if (!checkCallerHasWritePermissions(fd)) { |
| 384 | return STATUS_PERMISSION_DENIED; |
| 385 | } |
| 386 | if (numArgs != 4) { |
| 387 | dprintf(fd, |
| 388 | "Invalid number of arguments: please provide:\n" |
| 389 | "--requestFocusWithMetaData <METADATA> <ZONE_ID> <FOCUS_GAIN>: " |
| 390 | "requests audio focus for specified metadata, audio zone ID (int), " |
| 391 | "and focus gain type (int)\n"); |
| 392 | return STATUS_BAD_VALUE; |
| 393 | } |
| 394 | std::string metadataLiteral = std::string(args[1]); |
| 395 | audiohalcommon::PlaybackTrackMetadata trackMetadata{}; |
| 396 | auto status = parseMetaData(fd, metadataLiteral, trackMetadata); |
| 397 | if (status != STATUS_OK) { |
| 398 | return status; |
| 399 | } |
| 400 | |
| 401 | int zoneId; |
| 402 | if (!safelyParseInt(std::string(args[2]), &zoneId)) { |
| 403 | dprintf(fd, "Non-integer zoneId provided with request: %s\n", std::string(args[2]).c_str()); |
| 404 | return STATUS_BAD_VALUE; |
| 405 | } |
| 406 | |
| 407 | int focusGainValue; |
| 408 | if (!safelyParseInt(std::string(args[3]), &focusGainValue)) { |
| 409 | dprintf(fd, "Non-integer focusGain provided with request: %s\n", |
| 410 | std::string(args[3]).c_str()); |
| 411 | return STATUS_BAD_VALUE; |
| 412 | } |
| 413 | AudioFocusChange focusGain = AudioFocusChange(focusGainValue); |
| 414 | |
| 415 | if (mFocusListener == nullptr) { |
| 416 | dprintf(fd, "Unable to request focus - no focus listener registered\n"); |
| 417 | return STATUS_BAD_VALUE; |
| 418 | } |
| 419 | mFocusListener->requestAudioFocusWithMetaData(trackMetadata, zoneId, focusGain); |
| 420 | dprintf(fd, "Requested focus for metadata %s, zoneId %d, and focusGain %d\n", |
| 421 | trackMetadata.toString().c_str(), zoneId, focusGain); |
| 422 | return STATUS_OK; |
| 423 | } |
| 424 | |
| 425 | binder_status_t AudioControl::cmdAbandonFocusWithMetaData(int fd, const char** args, |
| 426 | uint32_t numArgs) { |
| 427 | if (!checkCallerHasWritePermissions(fd)) { |
| 428 | return STATUS_PERMISSION_DENIED; |
| 429 | } |
| 430 | if (numArgs != 3) { |
| 431 | dprintf(fd, |
| 432 | "Invalid number of arguments: please provide:\n" |
| 433 | "--abandonFocusWithMetaData <METADATA> <ZONE_ID>: " |
| 434 | "abandons audio focus for specified metadata and audio zone ID (int)\n"); |
| 435 | return STATUS_BAD_VALUE; |
| 436 | } |
| 437 | std::string metadataLiteral = std::string(args[1]); |
| 438 | audiohalcommon::PlaybackTrackMetadata trackMetadata{}; |
| 439 | auto status = parseMetaData(fd, metadataLiteral, trackMetadata); |
| 440 | if (status != STATUS_OK) { |
| 441 | return status; |
| 442 | } |
| 443 | int zoneId; |
| 444 | if (!safelyParseInt(std::string(args[2]), &zoneId)) { |
| 445 | dprintf(fd, "Non-integer zoneId provided with request: %s\n", std::string(args[2]).c_str()); |
| 446 | return STATUS_BAD_VALUE; |
| 447 | } |
| 448 | if (mFocusListener == nullptr) { |
| 449 | dprintf(fd, "Unable to abandon focus - no focus listener registered\n"); |
| 450 | return STATUS_BAD_VALUE; |
| 451 | } |
| 452 | |
| 453 | mFocusListener->abandonAudioFocusWithMetaData(trackMetadata, zoneId); |
| 454 | dprintf(fd, "Abandoned focus for metadata %s and zoneId %d\n", trackMetadata.toString().c_str(), |
| 455 | zoneId); |
| 456 | return STATUS_OK; |
| 457 | } |
| 458 | |
| 459 | binder_status_t AudioControl::cmdOnAudioDeviceGainsChanged(int fd, const char** args, |
| 460 | uint32_t numArgs) { |
| 461 | if (!checkCallerHasWritePermissions(fd)) { |
| 462 | return STATUS_PERMISSION_DENIED; |
| 463 | } |
| 464 | if ((numArgs + 1) % 2 != 0) { |
| 465 | dprintf(fd, |
| 466 | "Invalid number of arguments: please provide\n" |
| 467 | "--audioGainCallback <ZONE_ID> <REASON_1>[,<REASON_N> ...]" |
| 468 | "<DEVICE_ADDRESS_1> <GAIN_INDEX_1> [<DEVICE_ADDRESS_N> <GAIN_INDEX_N> ...]: " |
| 469 | "fire audio gain callback for audio zone ID (int), " |
| 470 | "with the given reasons (csv int) for given pairs of device address (string) " |
| 471 | "and gain index (int) \n"); |
| 472 | return STATUS_BAD_VALUE; |
| 473 | } |
| 474 | int zoneId; |
| 475 | if (!safelyParseInt(string(args[1]), &zoneId)) { |
| 476 | dprintf(fd, "Non-integer zoneId provided with request: %s\n", std::string(args[1]).c_str()); |
| 477 | return STATUS_BAD_VALUE; |
| 478 | } |
| 479 | std::string reasonsLiteral = std::string(args[2]); |
| 480 | std::stringstream csvReasonsLiteral(reasonsLiteral); |
| 481 | std::vector<Reasons> reasons; |
| 482 | std::string reasonLiteral; |
| 483 | while (getline(csvReasonsLiteral, reasonLiteral, ',')) { |
| 484 | int reason; |
| 485 | if (!safelyParseInt(reasonLiteral, &reason)) { |
| 486 | dprintf(fd, "Invalid Reason(s) provided %s\n", reasonLiteral.c_str()); |
| 487 | return STATUS_BAD_VALUE; |
| 488 | } |
| 489 | reasons.push_back(static_cast<Reasons>(reason)); |
| 490 | } |
| 491 | |
| 492 | std::vector<AudioGainConfigInfo> agcis{}; |
| 493 | for (uint32_t i = 3; i < numArgs; i += 2) { |
| 494 | std::string deviceAddress = std::string(args[i]); |
| 495 | int32_t index; |
| 496 | if (!safelyParseInt(std::string(args[i + 1]), &index)) { |
| 497 | dprintf(fd, "Non-integer index provided with request: %s\n", |
| 498 | std::string(args[i + 1]).c_str()); |
| 499 | return STATUS_BAD_VALUE; |
| 500 | } |
| 501 | AudioGainConfigInfo agci{zoneId, deviceAddress, index}; |
| 502 | agcis.push_back(agci); |
| 503 | } |
| 504 | if (mAudioGainCallback == nullptr) { |
| 505 | dprintf(fd, |
| 506 | "Unable to trig audio gain callback for reasons=%s and gains=%s\n" |
| 507 | " - no audio gain callback registered\n", |
| 508 | toEnumString(reasons).c_str(), toString(agcis).c_str()); |
| 509 | return STATUS_BAD_VALUE; |
| 510 | } |
| 511 | |
| 512 | mAudioGainCallback->onAudioDeviceGainsChanged(reasons, agcis); |
| 513 | dprintf(fd, "Fired audio gain callback for reasons=%s and gains=%s\n", |
| 514 | toEnumString(reasons).c_str(), toString(agcis).c_str()); |
| 515 | return STATUS_OK; |
| 516 | } |
Hayden Gomes | aeeb9b0 | 2020-10-27 13:08:34 -0700 | [diff] [blame] | 517 | } // namespace aidl::android::hardware::automotive::audiocontrol |