blob: 9ae422b1cf171a3ccb5b851f735e4bb7b8dc2739 [file] [log] [blame]
Hayden Gomesaeeb9b02020-10-27 13:08:34 -07001/*
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 Gomes6333eed2021-03-25 11:27:59 -070017#define LOG_TAG "AudioControl"
18// #define LOG_NDEBUG 0
19
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070020#include "AudioControl.h"
21
Oscar Azucena2744dea2024-09-20 19:35:03 -070022#include <aidl/android/hardware/automotive/audiocontrol/AudioDeviceConfiguration.h>
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070023#include <aidl/android/hardware/automotive/audiocontrol/AudioFocusChange.h>
Hayden Gomesad816702020-12-14 15:29:29 -080024#include <aidl/android/hardware/automotive/audiocontrol/DuckingInfo.h>
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070025#include <aidl/android/hardware/automotive/audiocontrol/IFocusListener.h>
26
27#include <android-base/logging.h>
Raj Goparaju27a02e12023-01-30 16:17:14 -080028#include <android-base/parsebool.h>
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070029#include <android-base/parseint.h>
30#include <android-base/strings.h>
Hayden Gomes6333eed2021-03-25 11:27:59 -070031#include <android_audio_policy_configuration_V7_0-enums.h>
Oscar Azucena2bf55fb2024-09-27 23:18:57 -070032
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070033#include <private/android_filesystem_config.h>
34
Francois Gaffie625a8982022-02-08 17:40:14 +010035#include <numeric>
36
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070037#include <stdio.h>
38
39namespace aidl::android::hardware::automotive::audiocontrol {
40
41using ::android::base::EqualsIgnoreCase;
Raj Goparaju27a02e12023-01-30 16:17:14 -080042using ::android::base::ParseBool;
43using ::android::base::ParseBoolResult;
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070044using ::android::base::ParseInt;
Oscar Azucena2bf55fb2024-09-27 23:18:57 -070045using ::android::hardware::audiocontrol::internal::CarAudioConfigurationXmlConverter;
Hayden Gomes6333eed2021-03-25 11:27:59 -070046using ::std::shared_ptr;
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070047using ::std::string;
48
Oscar Azucena2bf55fb2024-09-27 23:18:57 -070049namespace converter {
50using namespace ::android::hardware::audiocontrol::internal;
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070051}
52
Oscar Azucena2bf55fb2024-09-27 23:18:57 -070053namespace api = aidl::android::hardware::automotive::audiocontrol;
54namespace xsd = ::android::audio::policy::configuration::V7_0;
55
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070056namespace {
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070057bool checkCallerHasWritePermissions(int fd) {
58 // Double check that's only called by root - it should be be blocked at debug() level,
59 // but it doesn't hurt to make sure...
60 if (AIBinder_getCallingUid() != AID_ROOT) {
61 dprintf(fd, "Must be root\n");
62 return false;
63 }
64 return true;
65}
66
67bool isValidValue(float value) {
Oscar Azucena2bf55fb2024-09-27 23:18:57 -070068 return (value >= -1.0f) && (value <= 1.0f);
Hayden Gomesaeeb9b02020-10-27 13:08:34 -070069}
70
71bool safelyParseInt(string s, int* out) {
72 if (!ParseInt(s, out)) {
73 return false;
74 }
75 return true;
76}
Oscar Azucena2bf55fb2024-09-27 23:18:57 -070077
78std::string formatDump(const std::string& input) {
79 const char kSpacer = ' ';
80 std::string output;
81 int indentLevel = 0;
82 bool newLine = false;
83
84 for (char c : input) {
85 switch (c) {
86 case '{':
87 if (!newLine) {
88 output += '\n';
89 }
90 newLine = true;
91 indentLevel++;
92 for (int i = 0; i < indentLevel; ++i) {
93 output += kSpacer;
94 }
95 break;
96 case '}':
97 if (!newLine) {
98 output += '\n';
99 }
100 newLine = true;
101 indentLevel--;
102 for (int i = 0; i < indentLevel; ++i) {
103 output += kSpacer;
104 }
105 break;
106 case ',':
107 if (!newLine) {
108 output += '\n';
109 }
110 newLine = true;
111 for (int i = 0; i < indentLevel; ++i) {
112 output += kSpacer;
113 }
114 break;
115 default:
116 newLine = false;
117 output += c;
118 }
119 }
120
121 return output;
122}
123
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700124} // namespace
125
Raj Goparaju27a02e12023-01-30 16:17:14 -0800126namespace {
127using ::aidl::android::media::audio::common::AudioChannelLayout;
128using ::aidl::android::media::audio::common::AudioDeviceDescription;
129using ::aidl::android::media::audio::common::AudioDeviceType;
130using ::aidl::android::media::audio::common::AudioFormatDescription;
131using ::aidl::android::media::audio::common::AudioFormatType;
132using ::aidl::android::media::audio::common::AudioGain;
133using ::aidl::android::media::audio::common::AudioGainMode;
134using ::aidl::android::media::audio::common::AudioIoFlags;
135using ::aidl::android::media::audio::common::AudioOutputFlags;
136using ::aidl::android::media::audio::common::AudioPort;
137using ::aidl::android::media::audio::common::AudioPortDeviceExt;
138using ::aidl::android::media::audio::common::AudioPortExt;
139using ::aidl::android::media::audio::common::AudioPortMixExt;
140using ::aidl::android::media::audio::common::AudioProfile;
141using ::aidl::android::media::audio::common::PcmType;
142
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700143const static std::string kAudioConfigFile = "/vendor/etc/car_audio_configuration.xml";
144const static std::string kFadeConfigFile = "/vendor/etc/car_audio_fade_configuration.xml";
145
Raj Goparaju27a02e12023-01-30 16:17:14 -0800146// reuse common code artifacts
147void fillProfile(const std::vector<int32_t>& channelLayouts,
148 const std::vector<int32_t>& sampleRates, AudioProfile* profile) {
149 for (auto layout : channelLayouts) {
150 profile->channelMasks.push_back(
151 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(layout));
152 }
153 profile->sampleRates.insert(profile->sampleRates.end(), sampleRates.begin(), sampleRates.end());
154}
155
156AudioProfile createProfile(PcmType pcmType, const std::vector<int32_t>& channelLayouts,
157 const std::vector<int32_t>& sampleRates) {
158 AudioProfile profile;
159 profile.format.type = AudioFormatType::PCM;
160 profile.format.pcm = pcmType;
161 fillProfile(channelLayouts, sampleRates, &profile);
162 return profile;
163}
164
165AudioProfile createProfile(const std::string& encodingType,
166 const std::vector<int32_t>& channelLayouts,
167 const std::vector<int32_t>& sampleRates) {
168 AudioProfile profile;
169 profile.format.encoding = encodingType;
170 fillProfile(channelLayouts, sampleRates, &profile);
171 return profile;
172}
173
174AudioPortExt createDeviceExt(AudioDeviceType devType, int32_t flags,
175 const std::string& connection = "", const std::string& address = "") {
176 AudioPortDeviceExt deviceExt;
177 deviceExt.device.type.type = devType;
178 if (devType == AudioDeviceType::IN_MICROPHONE && connection.empty()) {
179 deviceExt.device.address = "bottom";
180 } else if (devType == AudioDeviceType::IN_MICROPHONE_BACK && connection.empty()) {
181 deviceExt.device.address = "back";
182 } else {
183 deviceExt.device.address = std::move(address);
184 }
185 deviceExt.device.type.connection = std::move(connection);
186 deviceExt.flags = flags;
187 return AudioPortExt::make<AudioPortExt::Tag::device>(deviceExt);
188}
189
190AudioPort createPort(int32_t id, const std::string& name, int32_t flags, bool isInput,
191 const AudioPortExt& ext) {
192 AudioPort port;
193 port.id = id;
194 port.name = name;
195 port.flags = isInput ? AudioIoFlags::make<AudioIoFlags::Tag::input>(flags)
196 : AudioIoFlags::make<AudioIoFlags::Tag::output>(flags);
197 port.ext = ext;
198 return port;
199}
200
201AudioGain createGain(int32_t mode, AudioChannelLayout channelMask, int32_t minValue,
202 int32_t maxValue, int32_t defaultValue, int32_t stepValue,
203 int32_t minRampMs = 100, int32_t maxRampMs = 100, bool useForVolume = true) {
204 AudioGain gain;
205 gain.mode = mode;
206 gain.channelMask = channelMask;
207 gain.minValue = minValue;
208 gain.maxValue = maxValue;
209 gain.defaultValue = defaultValue;
210 gain.stepValue = stepValue;
211 gain.minRampMs = minRampMs;
212 gain.maxRampMs = maxRampMs;
213 gain.useForVolume = useForVolume;
214 return gain;
215}
216} // namespace
217
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700218AudioControl::AudioControl() : AudioControl(kAudioConfigFile, kFadeConfigFile) {}
219
220AudioControl::AudioControl(const std::string& carAudioConfig, const std::string& audioFadeConfig)
221 : mCarAudioConfigurationConverter(std::make_shared<CarAudioConfigurationXmlConverter>(
222 carAudioConfig, audioFadeConfig)) {}
223
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700224ndk::ScopedAStatus AudioControl::registerFocusListener(
225 const shared_ptr<IFocusListener>& in_listener) {
226 LOG(DEBUG) << "registering focus listener";
227
Hayden Gomes6333eed2021-03-25 11:27:59 -0700228 if (in_listener) {
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700229 std::atomic_store(&mFocusListener, in_listener);
230 } else {
231 LOG(ERROR) << "Unexpected nullptr for listener resulting in no-op.";
232 }
233 return ndk::ScopedAStatus::ok();
234}
235
236ndk::ScopedAStatus AudioControl::setBalanceTowardRight(float value) {
237 if (isValidValue(value)) {
238 // Just log in this default mock implementation
239 LOG(INFO) << "Balance set to " << value;
Hayden Gomes6333eed2021-03-25 11:27:59 -0700240 return ndk::ScopedAStatus::ok();
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700241 }
Hayden Gomes6333eed2021-03-25 11:27:59 -0700242
243 LOG(ERROR) << "Balance value out of range -1 to 1 at " << value;
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700244 return ndk::ScopedAStatus::ok();
245}
246
247ndk::ScopedAStatus AudioControl::setFadeTowardFront(float value) {
248 if (isValidValue(value)) {
249 // Just log in this default mock implementation
250 LOG(INFO) << "Fader set to " << value;
Hayden Gomes6333eed2021-03-25 11:27:59 -0700251 return ndk::ScopedAStatus::ok();
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700252 }
Hayden Gomes6333eed2021-03-25 11:27:59 -0700253
254 LOG(ERROR) << "Fader value out of range -1 to 1 at " << value;
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700255 return ndk::ScopedAStatus::ok();
256}
257
258ndk::ScopedAStatus AudioControl::onAudioFocusChange(const string& in_usage, int32_t in_zoneId,
259 AudioFocusChange in_focusChange) {
260 LOG(INFO) << "Focus changed: " << toString(in_focusChange).c_str() << " for usage "
261 << in_usage.c_str() << " in zone " << in_zoneId;
262 return ndk::ScopedAStatus::ok();
263}
264
Hayden Gomesad816702020-12-14 15:29:29 -0800265ndk::ScopedAStatus AudioControl::onDevicesToDuckChange(
266 const std::vector<DuckingInfo>& in_duckingInfos) {
267 LOG(INFO) << "AudioControl::onDevicesToDuckChange";
268 for (const DuckingInfo& duckingInfo : in_duckingInfos) {
269 LOG(INFO) << "zone: " << duckingInfo.zoneId;
270 LOG(INFO) << "Devices to duck:";
Oscar Azucenab8e5cd02020-12-16 13:53:14 -0800271 for (const auto& addressToDuck : duckingInfo.deviceAddressesToDuck) {
Hayden Gomesad816702020-12-14 15:29:29 -0800272 LOG(INFO) << addressToDuck;
273 }
274 LOG(INFO) << "Devices to unduck:";
Oscar Azucenab8e5cd02020-12-16 13:53:14 -0800275 for (const auto& addressToUnduck : duckingInfo.deviceAddressesToUnduck) {
Hayden Gomesad816702020-12-14 15:29:29 -0800276 LOG(INFO) << addressToUnduck;
277 }
278 LOG(INFO) << "Usages holding focus:";
Oscar Azucenab8e5cd02020-12-16 13:53:14 -0800279 for (const auto& usage : duckingInfo.usagesHoldingFocus) {
Hayden Gomesad816702020-12-14 15:29:29 -0800280 LOG(INFO) << usage;
281 }
282 }
283 return ndk::ScopedAStatus::ok();
284}
285
Oscar Azucenab8e5cd02020-12-16 13:53:14 -0800286ndk::ScopedAStatus AudioControl::onDevicesToMuteChange(
287 const std::vector<MutingInfo>& in_mutingInfos) {
288 LOG(INFO) << "AudioControl::onDevicesToMuteChange";
289 for (const MutingInfo& mutingInfo : in_mutingInfos) {
290 LOG(INFO) << "zone: " << mutingInfo.zoneId;
291 LOG(INFO) << "Devices to mute:";
292 for (const auto& addressToMute : mutingInfo.deviceAddressesToMute) {
293 LOG(INFO) << addressToMute;
294 }
295 LOG(INFO) << "Devices to unmute:";
296 for (const auto& addressToUnmute : mutingInfo.deviceAddressesToUnmute) {
297 LOG(INFO) << addressToUnmute;
298 }
299 }
300 return ndk::ScopedAStatus::ok();
301}
302
Francois Gaffie625a8982022-02-08 17:40:14 +0100303template <typename aidl_type>
304static inline std::string toString(const std::vector<aidl_type>& in_values) {
305 return std::accumulate(std::begin(in_values), std::end(in_values), std::string{},
Tomasz Wasilczyk53706702024-01-09 12:15:20 -0800306 [](const std::string& ls, const aidl_type& rs) {
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700307 return ls + (ls.empty() ? "" : ", ") + rs.toString();
Francois Gaffie625a8982022-02-08 17:40:14 +0100308 });
309}
310template <typename aidl_enum_type>
311static inline std::string toEnumString(const std::vector<aidl_enum_type>& in_values) {
312 return std::accumulate(std::begin(in_values), std::end(in_values), std::string{},
Tomasz Wasilczyk53706702024-01-09 12:15:20 -0800313 [](const std::string& ls, const aidl_enum_type& rs) {
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700314 return ls + (ls.empty() ? "" : ", ") + toString(rs);
Francois Gaffie625a8982022-02-08 17:40:14 +0100315 });
316}
317
Oscar Azucena2744dea2024-09-20 19:35:03 -0700318template <typename aidl_type>
319static inline std::string toString(const std::vector<std::optional<aidl_type>>& in_values) {
320 return std::accumulate(std::begin(in_values), std::end(in_values), std::string{},
321 [](const std::string& ls, const std::optional<aidl_type>& rs) {
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700322 return ls + (ls.empty() ? "" : ", ") +
Oscar Azucena2744dea2024-09-20 19:35:03 -0700323 (rs.has_value() ? rs.value().toString() : "empty");
324 });
325}
326
Francois Gaffie625a8982022-02-08 17:40:14 +0100327ndk::ScopedAStatus AudioControl::onAudioFocusChangeWithMetaData(
328 const audiohalcommon::PlaybackTrackMetadata& in_playbackMetaData, int32_t in_zoneId,
329 AudioFocusChange in_focusChange) {
330 LOG(INFO) << "Focus changed: " << toString(in_focusChange).c_str() << " for metadata "
331 << in_playbackMetaData.toString().c_str() << " in zone " << in_zoneId;
332 return ndk::ScopedAStatus::ok();
333}
334
335ndk::ScopedAStatus AudioControl::setAudioDeviceGainsChanged(
336 const std::vector<Reasons>& in_reasons, const std::vector<AudioGainConfigInfo>& in_gains) {
337 LOG(INFO) << "Audio Device Gains changed: resons:" << toEnumString(in_reasons).c_str()
338 << " for devices: " << toString(in_gains).c_str();
339 return ndk::ScopedAStatus::ok();
340}
341
342ndk::ScopedAStatus AudioControl::registerGainCallback(
343 const std::shared_ptr<IAudioGainCallback>& in_callback) {
344 LOG(DEBUG) << ": " << __func__;
345 if (in_callback) {
346 std::atomic_store(&mAudioGainCallback, in_callback);
347 } else {
348 LOG(ERROR) << "Unexpected nullptr for audio gain callback resulting in no-op.";
349 }
350 return ndk::ScopedAStatus::ok();
351}
352
Raj Goparaju27a02e12023-01-30 16:17:14 -0800353ndk::ScopedAStatus AudioControl::setModuleChangeCallback(
354 const std::shared_ptr<IModuleChangeCallback>& in_callback) {
355 LOG(DEBUG) << ": " << __func__;
356
357 if (in_callback.get() == nullptr) {
358 LOG(ERROR) << __func__ << ": Callback is nullptr";
359 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
360 }
361 if (mModuleChangeCallback != nullptr) {
362 LOG(ERROR) << __func__ << ": Module change callback was already registered";
363 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
364 }
365 std::atomic_store(&mModuleChangeCallback, in_callback);
366 return ndk::ScopedAStatus::ok();
367}
368
369ndk::ScopedAStatus AudioControl::clearModuleChangeCallback() {
370 if (mModuleChangeCallback != nullptr) {
371 shared_ptr<IModuleChangeCallback> nullCallback = nullptr;
372 std::atomic_store(&mModuleChangeCallback, nullCallback);
373 LOG(DEBUG) << ":" << __func__ << ": Unregistered successfully";
374 } else {
375 LOG(DEBUG) << ":" << __func__ << ": No callback registered, no-op";
376 }
377 return ndk::ScopedAStatus::ok();
378}
379
Oscar Azucena2744dea2024-09-20 19:35:03 -0700380ndk::ScopedAStatus AudioControl::getAudioDeviceConfiguration(
381 AudioDeviceConfiguration* audioDeviceConfig) {
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700382 if (!audioDeviceConfig) {
383 LOG(ERROR) << __func__ << "Audio device configuration must not be null";
384 return ndk::ScopedAStatus::fromStatus(STATUS_UNEXPECTED_NULL);
385 }
386 if (!mCarAudioConfigurationConverter) {
387 return ndk::ScopedAStatus::ok();
388 }
389 const auto& innerDeviceConfig = mCarAudioConfigurationConverter->getAudioDeviceConfiguration();
390 audioDeviceConfig->routingConfig = innerDeviceConfig.routingConfig;
391 audioDeviceConfig->useCoreAudioVolume = innerDeviceConfig.useCoreAudioVolume;
392 audioDeviceConfig->useCarVolumeGroupMuting = innerDeviceConfig.useCarVolumeGroupMuting;
393 audioDeviceConfig->useHalDuckingSignals = innerDeviceConfig.useHalDuckingSignals;
Oscar Azucena2744dea2024-09-20 19:35:03 -0700394 return ndk::ScopedAStatus::ok();
395}
396
397ndk::ScopedAStatus AudioControl::getOutputMirroringDevices(
398 std::vector<AudioPort>* mirroringDevices) {
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700399 if (!mirroringDevices) {
400 LOG(ERROR) << __func__ << "Mirroring devices must not be null";
401 return ndk::ScopedAStatus::fromStatus(STATUS_UNEXPECTED_NULL);
Oscar Azucena2744dea2024-09-20 19:35:03 -0700402 }
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700403 if (!mCarAudioConfigurationConverter || !mCarAudioConfigurationConverter->getErrors().empty()) {
404 return ndk::ScopedAStatus::ok();
405 }
406 const auto& innerDevice = mCarAudioConfigurationConverter->getOutputMirroringDevices();
407 mirroringDevices->insert(mirroringDevices->end(), innerDevice.begin(), innerDevice.end());
Oscar Azucena2744dea2024-09-20 19:35:03 -0700408 return ndk::ScopedAStatus::ok();
409}
410
411ndk::ScopedAStatus AudioControl::getCarAudioZones(std::vector<AudioZone>* audioZones) {
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700412 if (!audioZones) {
413 LOG(ERROR) << __func__ << "Audio zones must not be null";
414 return ndk::ScopedAStatus::fromStatus(STATUS_UNEXPECTED_NULL);
Oscar Azucena2744dea2024-09-20 19:35:03 -0700415 }
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700416 if (!mCarAudioConfigurationConverter || !mCarAudioConfigurationConverter->getErrors().empty()) {
417 return ndk::ScopedAStatus::ok();
418 }
419 const auto& innerZones = mCarAudioConfigurationConverter->getAudioZones();
420 audioZones->insert(audioZones->end(), innerZones.begin(), innerZones.end());
Oscar Azucena2744dea2024-09-20 19:35:03 -0700421 return ndk::ScopedAStatus::ok();
422}
423
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700424binder_status_t AudioControl::dump(int fd, const char** args, uint32_t numArgs) {
425 if (numArgs == 0) {
426 return dumpsys(fd);
427 }
428
429 string option = string(args[0]);
430 if (EqualsIgnoreCase(option, "--help")) {
431 return cmdHelp(fd);
432 } else if (EqualsIgnoreCase(option, "--request")) {
433 return cmdRequestFocus(fd, args, numArgs);
434 } else if (EqualsIgnoreCase(option, "--abandon")) {
435 return cmdAbandonFocus(fd, args, numArgs);
Francois Gaffie625a8982022-02-08 17:40:14 +0100436 } else if (EqualsIgnoreCase(option, "--requestFocusWithMetaData")) {
437 return cmdRequestFocusWithMetaData(fd, args, numArgs);
438 } else if (EqualsIgnoreCase(option, "--abandonFocusWithMetaData")) {
439 return cmdAbandonFocusWithMetaData(fd, args, numArgs);
440 } else if (EqualsIgnoreCase(option, "--audioGainCallback")) {
441 return cmdOnAudioDeviceGainsChanged(fd, args, numArgs);
Raj Goparaju27a02e12023-01-30 16:17:14 -0800442 } else if (EqualsIgnoreCase(option, "--audioPortsChangedCallback")) {
443 return cmdOnAudioPortsChanged(fd, args, numArgs);
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700444 } else {
445 dprintf(fd, "Invalid option: %s\n", option.c_str());
446 return STATUS_BAD_VALUE;
447 }
448}
449
450binder_status_t AudioControl::dumpsys(int fd) {
451 if (mFocusListener == nullptr) {
452 dprintf(fd, "No focus listener registered\n");
453 } else {
454 dprintf(fd, "Focus listener registered\n");
455 }
Francois Gaffie625a8982022-02-08 17:40:14 +0100456 dprintf(fd, "AudioGainCallback %sregistered\n", (mAudioGainCallback == nullptr ? "NOT " : ""));
Oscar Azucena2bf55fb2024-09-27 23:18:57 -0700457
458 AudioDeviceConfiguration configuration;
459 if (getAudioDeviceConfiguration(&configuration).isOk()) {
460 dprintf(fd, "AudioDeviceConfiguration: %s\n", configuration.toString().c_str());
461 }
462 std::vector<AudioZone> audioZones;
463 if (getCarAudioZones(&audioZones).isOk()) {
464 dprintf(fd, "Audio zones count: %zu\n", audioZones.size());
465 for (const auto& zone : audioZones) {
466 dprintf(fd, "AudioZone: %s\n", formatDump(zone.toString()).c_str());
467 }
468 }
469 std::vector<AudioPort> mirroringDevices;
470 if (getOutputMirroringDevices(&mirroringDevices).isOk()) {
471 dprintf(fd, "Mirroring devices count: %zu\n", mirroringDevices.size());
472 for (const auto& device : mirroringDevices) {
473 dprintf(fd, "Mirroring device: %s\n", formatDump(device.toString()).c_str());
474 }
475 }
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700476 return STATUS_OK;
477}
478
479binder_status_t AudioControl::cmdHelp(int fd) const {
480 dprintf(fd, "Usage: \n\n");
Francois Gaffie625a8982022-02-08 17:40:14 +0100481 dprintf(fd, "[no args]: dumps focus listener / gain callback registered status\n");
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700482 dprintf(fd, "--help: shows this help\n");
483 dprintf(fd,
484 "--request <USAGE> <ZONE_ID> <FOCUS_GAIN>: requests audio focus for specified "
Francois Gaffie625a8982022-02-08 17:40:14 +0100485 "usage (string), audio zone ID (int), and focus gain type (int)\n"
486 "Deprecated, use MetaData instead\n");
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700487 dprintf(fd,
488 "--abandon <USAGE> <ZONE_ID>: abandons audio focus for specified usage (string) and "
Francois Gaffie625a8982022-02-08 17:40:14 +0100489 "audio zone ID (int)\n"
490 "Deprecated, use MetaData instead\n");
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700491 dprintf(fd, "See audio_policy_configuration.xsd for valid AudioUsage values.\n");
Francois Gaffie625a8982022-02-08 17:40:14 +0100492
493 dprintf(fd,
494 "--requestFocusWithMetaData <METADATA> <ZONE_ID> <FOCUS_GAIN>: "
495 "requests audio focus for specified metadata, audio zone ID (int), "
496 "and focus gain type (int)\n");
497 dprintf(fd,
498 "--abandonFocusWithMetaData <METADATA> <ZONE_ID>: "
499 "abandons audio focus for specified metadata and audio zone ID (int)\n");
500 dprintf(fd,
501 "--audioGainCallback <ZONE_ID> <REASON_1>[,<REASON_N> ...]"
502 "<DEVICE_ADDRESS_1> <GAIN_INDEX_1> [<DEVICE_ADDRESS_N> <GAIN_INDEX_N> ...]: fire audio "
503 "gain callback for audio zone ID (int), the given reasons (csv int) for given pairs "
504 "of device address (string) and gain index (int) \n");
505
506 dprintf(fd,
507 "Note on <METADATA>: <USAGE,CONTENT_TYPE[,TAGS]> specified as where (int)usage, "
508 "(int)content type and tags (string)string)\n");
509 dprintf(fd,
510 "See android/media/audio/common/AudioUsageType.aidl for valid AudioUsage values.\n");
511 dprintf(fd,
512 "See android/media/audio/common/AudioContentType.aidl for valid AudioContentType "
513 "values.\n");
514 dprintf(fd,
515 "Tags are optional. If provided, it must follow the <key>=<value> pattern, where the "
516 "value is namespaced (for example com.google.strategy=VR).\n");
Raj Goparaju27a02e12023-01-30 16:17:14 -0800517 dprintf(fd,
518 "--audioPortsChangedCallback <ID_1> <NAME_1> <BUS_ADDRESS_1> <CONNECTION_TYPE_1> "
519 "<AUDIO_GAINS_1> [<ID_N> <NAME_N> <BUS_ADDRESS_N> <CONNECTION_TYPE_N> "
520 "<AUDIO_GAINS_N>]: fires audio ports changed callback. Carries list of modified "
521 "AudioPorts. "
522 "For simplicity, this command accepts limited information for each AudioPort: "
523 "id(int), name(string), port address(string), connection type (string), "
524 "audio gain (csv int)\n");
525 dprintf(fd, "Notes: \n");
526 dprintf(fd,
527 "1. AudioGain csv should match definition at "
528 "android/media/audio/common/AudioPort.aidl\n");
529 dprintf(fd,
530 "2. See android/media/audio/common/AudioDeviceDescription.aidl for valid "
531 "<CONNECTION_TYPE> values.\n");
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700532 return STATUS_OK;
533}
534
535binder_status_t AudioControl::cmdRequestFocus(int fd, const char** args, uint32_t numArgs) {
536 if (!checkCallerHasWritePermissions(fd)) {
537 return STATUS_PERMISSION_DENIED;
538 }
539 if (numArgs != 4) {
540 dprintf(fd,
541 "Invalid number of arguments: please provide --request <USAGE> <ZONE_ID> "
542 "<FOCUS_GAIN>\n");
543 return STATUS_BAD_VALUE;
544 }
545
546 string usage = string(args[1]);
Hayden Gomes6333eed2021-03-25 11:27:59 -0700547 if (xsd::isUnknownAudioUsage(usage)) {
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700548 dprintf(fd,
549 "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 "
550 "for supported values\n",
551 usage.c_str());
552 return STATUS_BAD_VALUE;
553 }
554
555 int zoneId;
556 if (!safelyParseInt(string(args[2]), &zoneId)) {
557 dprintf(fd, "Non-integer zoneId provided with request: %s\n", string(args[2]).c_str());
558 return STATUS_BAD_VALUE;
559 }
560
561 int focusGainValue;
562 if (!safelyParseInt(string(args[3]), &focusGainValue)) {
563 dprintf(fd, "Non-integer focusGain provided with request: %s\n", string(args[3]).c_str());
564 return STATUS_BAD_VALUE;
565 }
566 AudioFocusChange focusGain = AudioFocusChange(focusGainValue);
567
568 if (mFocusListener == nullptr) {
569 dprintf(fd, "Unable to request focus - no focus listener registered\n");
570 return STATUS_BAD_VALUE;
571 }
572
573 mFocusListener->requestAudioFocus(usage, zoneId, focusGain);
574 dprintf(fd, "Requested focus for usage %s, zoneId %d, and focusGain %d\n", usage.c_str(),
575 zoneId, focusGain);
576 return STATUS_OK;
577}
578
579binder_status_t AudioControl::cmdAbandonFocus(int fd, const char** args, uint32_t numArgs) {
580 if (!checkCallerHasWritePermissions(fd)) {
581 return STATUS_PERMISSION_DENIED;
582 }
583 if (numArgs != 3) {
584 dprintf(fd, "Invalid number of arguments: please provide --abandon <USAGE> <ZONE_ID>\n");
585 return STATUS_BAD_VALUE;
586 }
587
588 string usage = string(args[1]);
Hayden Gomes6333eed2021-03-25 11:27:59 -0700589 if (xsd::isUnknownAudioUsage(usage)) {
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700590 dprintf(fd,
591 "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 "
592 "for supported values\n",
593 usage.c_str());
594 return STATUS_BAD_VALUE;
595 }
596
597 int zoneId;
598 if (!safelyParseInt(string(args[2]), &zoneId)) {
599 dprintf(fd, "Non-integer zoneId provided with abandon: %s\n", string(args[2]).c_str());
600 return STATUS_BAD_VALUE;
601 }
602
603 if (mFocusListener == nullptr) {
604 dprintf(fd, "Unable to abandon focus - no focus listener registered\n");
605 return STATUS_BAD_VALUE;
606 }
607
608 mFocusListener->abandonAudioFocus(usage, zoneId);
609 dprintf(fd, "Abandoned focus for usage %s and zoneId %d\n", usage.c_str(), zoneId);
610 return STATUS_OK;
611}
612
Francois Gaffie625a8982022-02-08 17:40:14 +0100613binder_status_t AudioControl::parseMetaData(int fd, const std::string& metadataLiteral,
614 audiohalcommon::PlaybackTrackMetadata& trackMetadata) {
615 std::stringstream csvMetaData(metadataLiteral);
616 std::vector<std::string> splitMetaData;
617 std::string attribute;
618 while (getline(csvMetaData, attribute, ',')) {
619 splitMetaData.push_back(attribute);
620 }
621 if (splitMetaData.size() != 2 && splitMetaData.size() != 3) {
622 dprintf(fd,
623 "Invalid metadata: %s, please provide <METADATA> as <USAGE,CONTENT_TYPE[,TAGS]> "
624 "where (int)usage, (int)content type and tags (string)string)\n",
625 metadataLiteral.c_str());
626 return STATUS_BAD_VALUE;
627 }
628 int usage;
629 if (!safelyParseInt(splitMetaData[0], &usage)) {
630 dprintf(fd, "Non-integer usage provided with request: %s\n", splitMetaData[0].c_str());
631 return STATUS_BAD_VALUE;
632 }
633 int contentType;
634 if (!safelyParseInt(splitMetaData[1], &contentType)) {
635 dprintf(fd, "Non-integer content type provided with request: %s\n",
636 splitMetaData[1].c_str());
637 return STATUS_BAD_VALUE;
638 }
639 const std::string tags = (splitMetaData.size() == 3 ? splitMetaData[2] : "");
640
641 trackMetadata = {.usage = static_cast<audiomediacommon::AudioUsage>(usage),
642 .contentType = static_cast<audiomediacommon::AudioContentType>(contentType),
643 .tags = {tags}};
644 return STATUS_OK;
645}
646
647binder_status_t AudioControl::cmdRequestFocusWithMetaData(int fd, const char** args,
648 uint32_t numArgs) {
649 if (!checkCallerHasWritePermissions(fd)) {
650 return STATUS_PERMISSION_DENIED;
651 }
652 if (numArgs != 4) {
653 dprintf(fd,
654 "Invalid number of arguments: please provide:\n"
655 "--requestFocusWithMetaData <METADATA> <ZONE_ID> <FOCUS_GAIN>: "
656 "requests audio focus for specified metadata, audio zone ID (int), "
657 "and focus gain type (int)\n");
658 return STATUS_BAD_VALUE;
659 }
660 std::string metadataLiteral = std::string(args[1]);
661 audiohalcommon::PlaybackTrackMetadata trackMetadata{};
662 auto status = parseMetaData(fd, metadataLiteral, trackMetadata);
663 if (status != STATUS_OK) {
664 return status;
665 }
666
667 int zoneId;
668 if (!safelyParseInt(std::string(args[2]), &zoneId)) {
669 dprintf(fd, "Non-integer zoneId provided with request: %s\n", std::string(args[2]).c_str());
670 return STATUS_BAD_VALUE;
671 }
672
673 int focusGainValue;
674 if (!safelyParseInt(std::string(args[3]), &focusGainValue)) {
675 dprintf(fd, "Non-integer focusGain provided with request: %s\n",
676 std::string(args[3]).c_str());
677 return STATUS_BAD_VALUE;
678 }
679 AudioFocusChange focusGain = AudioFocusChange(focusGainValue);
680
681 if (mFocusListener == nullptr) {
682 dprintf(fd, "Unable to request focus - no focus listener registered\n");
683 return STATUS_BAD_VALUE;
684 }
685 mFocusListener->requestAudioFocusWithMetaData(trackMetadata, zoneId, focusGain);
686 dprintf(fd, "Requested focus for metadata %s, zoneId %d, and focusGain %d\n",
687 trackMetadata.toString().c_str(), zoneId, focusGain);
688 return STATUS_OK;
689}
690
691binder_status_t AudioControl::cmdAbandonFocusWithMetaData(int fd, const char** args,
692 uint32_t numArgs) {
693 if (!checkCallerHasWritePermissions(fd)) {
694 return STATUS_PERMISSION_DENIED;
695 }
696 if (numArgs != 3) {
697 dprintf(fd,
698 "Invalid number of arguments: please provide:\n"
699 "--abandonFocusWithMetaData <METADATA> <ZONE_ID>: "
700 "abandons audio focus for specified metadata and audio zone ID (int)\n");
701 return STATUS_BAD_VALUE;
702 }
703 std::string metadataLiteral = std::string(args[1]);
704 audiohalcommon::PlaybackTrackMetadata trackMetadata{};
705 auto status = parseMetaData(fd, metadataLiteral, trackMetadata);
706 if (status != STATUS_OK) {
707 return status;
708 }
709 int zoneId;
710 if (!safelyParseInt(std::string(args[2]), &zoneId)) {
711 dprintf(fd, "Non-integer zoneId provided with request: %s\n", std::string(args[2]).c_str());
712 return STATUS_BAD_VALUE;
713 }
714 if (mFocusListener == nullptr) {
715 dprintf(fd, "Unable to abandon focus - no focus listener registered\n");
716 return STATUS_BAD_VALUE;
717 }
718
719 mFocusListener->abandonAudioFocusWithMetaData(trackMetadata, zoneId);
720 dprintf(fd, "Abandoned focus for metadata %s and zoneId %d\n", trackMetadata.toString().c_str(),
721 zoneId);
722 return STATUS_OK;
723}
724
725binder_status_t AudioControl::cmdOnAudioDeviceGainsChanged(int fd, const char** args,
726 uint32_t numArgs) {
727 if (!checkCallerHasWritePermissions(fd)) {
728 return STATUS_PERMISSION_DENIED;
729 }
730 if ((numArgs + 1) % 2 != 0) {
731 dprintf(fd,
732 "Invalid number of arguments: please provide\n"
733 "--audioGainCallback <ZONE_ID> <REASON_1>[,<REASON_N> ...]"
734 "<DEVICE_ADDRESS_1> <GAIN_INDEX_1> [<DEVICE_ADDRESS_N> <GAIN_INDEX_N> ...]: "
735 "fire audio gain callback for audio zone ID (int), "
736 "with the given reasons (csv int) for given pairs of device address (string) "
737 "and gain index (int) \n");
738 return STATUS_BAD_VALUE;
739 }
740 int zoneId;
741 if (!safelyParseInt(string(args[1]), &zoneId)) {
742 dprintf(fd, "Non-integer zoneId provided with request: %s\n", std::string(args[1]).c_str());
743 return STATUS_BAD_VALUE;
744 }
745 std::string reasonsLiteral = std::string(args[2]);
746 std::stringstream csvReasonsLiteral(reasonsLiteral);
747 std::vector<Reasons> reasons;
748 std::string reasonLiteral;
749 while (getline(csvReasonsLiteral, reasonLiteral, ',')) {
750 int reason;
751 if (!safelyParseInt(reasonLiteral, &reason)) {
752 dprintf(fd, "Invalid Reason(s) provided %s\n", reasonLiteral.c_str());
753 return STATUS_BAD_VALUE;
754 }
755 reasons.push_back(static_cast<Reasons>(reason));
756 }
757
758 std::vector<AudioGainConfigInfo> agcis{};
759 for (uint32_t i = 3; i < numArgs; i += 2) {
760 std::string deviceAddress = std::string(args[i]);
761 int32_t index;
762 if (!safelyParseInt(std::string(args[i + 1]), &index)) {
763 dprintf(fd, "Non-integer index provided with request: %s\n",
764 std::string(args[i + 1]).c_str());
765 return STATUS_BAD_VALUE;
766 }
767 AudioGainConfigInfo agci{zoneId, deviceAddress, index};
768 agcis.push_back(agci);
769 }
770 if (mAudioGainCallback == nullptr) {
771 dprintf(fd,
772 "Unable to trig audio gain callback for reasons=%s and gains=%s\n"
773 " - no audio gain callback registered\n",
774 toEnumString(reasons).c_str(), toString(agcis).c_str());
775 return STATUS_BAD_VALUE;
776 }
777
778 mAudioGainCallback->onAudioDeviceGainsChanged(reasons, agcis);
779 dprintf(fd, "Fired audio gain callback for reasons=%s and gains=%s\n",
780 toEnumString(reasons).c_str(), toString(agcis).c_str());
781 return STATUS_OK;
782}
Raj Goparaju27a02e12023-01-30 16:17:14 -0800783
784binder_status_t AudioControl::parseAudioGains(int fd, const std::string& stringGain,
785 std::vector<AudioGain>& gains) {
786 const int kAudioGainSize = 9;
787 std::stringstream csvGain(stringGain);
788 std::vector<std::string> vecGain;
789 std::string value;
790 while (getline(csvGain, value, ',')) {
791 vecGain.push_back(value);
792 }
793
794 if ((vecGain.size() == 0) || ((vecGain.size() % kAudioGainSize) != 0)) {
795 dprintf(fd, "Erroneous input to generate AudioGain: %s\n", stringGain.c_str());
796 return STATUS_BAD_VALUE;
797 }
798
799 // iterate over injected AudioGains
800 for (int index = 0; index < vecGain.size(); index += kAudioGainSize) {
801 int32_t mode;
802 if (!safelyParseInt(vecGain[index], &mode)) {
803 dprintf(fd, "Non-integer index provided with request: %s\n", vecGain[index].c_str());
804 return STATUS_BAD_VALUE;
805 }
806
807 // car audio framework only supports JOINT mode.
808 // skip injected AudioGains that are not compliant with this.
809 if (mode != static_cast<int>(AudioGainMode::JOINT)) {
810 LOG(WARNING) << ":" << __func__ << ": skipping gain since it is not JOINT mode!";
811 continue;
812 }
813
814 int32_t layout;
815 if (!safelyParseInt(vecGain[index + 1], &layout)) {
816 dprintf(fd, "Non-integer index provided with request: %s\n",
817 vecGain[index + 1].c_str());
818 return STATUS_BAD_VALUE;
819 }
820 AudioChannelLayout channelMask =
821 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(layout);
822
823 int32_t minValue;
824 if (!safelyParseInt(vecGain[index + 2], &minValue)) {
825 dprintf(fd, "Non-integer index provided with request: %s\n",
826 vecGain[index + 2].c_str());
827 return STATUS_BAD_VALUE;
828 }
829
830 int32_t maxValue;
831 if (!safelyParseInt(vecGain[index + 3], &maxValue)) {
832 dprintf(fd, "Non-integer index provided with request: %s\n",
833 vecGain[index + 3].c_str());
834 return STATUS_BAD_VALUE;
835 }
836
837 int32_t defaultValue;
838 if (!safelyParseInt(vecGain[index + 4], &defaultValue)) {
839 dprintf(fd, "Non-integer index provided with request: %s\n",
840 vecGain[index + 4].c_str());
841 return STATUS_BAD_VALUE;
842 }
843
844 int32_t stepValue;
845 if (!safelyParseInt(vecGain[index + 5], &stepValue)) {
846 dprintf(fd, "Non-integer index provided with request: %s\n",
847 vecGain[index + 5].c_str());
848 return STATUS_BAD_VALUE;
849 }
850
851 int32_t minRampMs;
852 if (!safelyParseInt(vecGain[index + 6], &minRampMs)) {
853 dprintf(fd, "Non-integer index provided with request: %s\n",
854 vecGain[index + 6].c_str());
855 return STATUS_BAD_VALUE;
856 }
857
858 int32_t maxRampMs;
859 if (!safelyParseInt(vecGain[index + 7], &maxRampMs)) {
860 dprintf(fd, "Non-integer index provided with request: %s\n",
861 vecGain[index + 7].c_str());
862 return STATUS_BAD_VALUE;
863 }
864
865 ParseBoolResult useForVolume = ParseBool(vecGain[index + 8]);
866 if (useForVolume == ParseBoolResult::kError) {
867 dprintf(fd, "Non-boolean index provided with request: %s\n",
868 vecGain[index + 8].c_str());
869 return STATUS_BAD_VALUE;
870 } else if (useForVolume == ParseBoolResult::kFalse) {
871 // at this level we only care about gain stages that are relevant
872 // for volume control. skip the gain stage if its flagged as false.
873 LOG(WARNING) << ":" << __func__
874 << ": skipping gain since it is not for volume control!";
875 continue;
876 }
877
878 AudioGain gain = createGain(mode, channelMask, minValue, maxValue, defaultValue, stepValue,
879 minRampMs, maxRampMs, true /*useForVolume*/);
880 gains.push_back(gain);
881 }
882 return STATUS_OK;
883}
884
885binder_status_t AudioControl::cmdOnAudioPortsChanged(int fd, const char** args, uint32_t numArgs) {
886 if (!checkCallerHasWritePermissions(fd)) {
887 return STATUS_PERMISSION_DENIED;
888 }
889
890 if ((numArgs < 6) || ((numArgs - 1) % 5 != 0)) {
891 dprintf(fd,
892 "Invalid number of arguments: please provide\n"
893 "--audioPortsChangedCallback <ID_1> <NAME_1> <BUS_ADDRESS_1> <CONNECTION_TYPE_1> "
894 "<AUDIO_GAINS_1> [<ID_N> <NAME_N> <BUS_ADDRESS_N> <CONNECTION_TYPE_N> "
895 "<AUDIO_GAINS_N>]: triggers audio ports changed callback. Carries list of "
896 "modified AudioPorts. "
897 "For simplicity, this command accepts limited information for each AudioPort: "
898 "id(int), name(string), port address(string), connection type (string), "
899 "audio gain (csv int)\n");
900 return STATUS_BAD_VALUE;
901 }
902
903 std::vector<AudioPort> ports;
904 for (uint32_t i = 1; i < numArgs; i += 5) {
905 binder_status_t status;
906 int32_t id;
907 if (!safelyParseInt(std::string(args[i]), &id)) {
908 dprintf(fd, "Non-integer index provided with request: %s\n",
909 std::string(args[i]).c_str());
910 return STATUS_BAD_VALUE;
911 }
912
913 std::string name = std::string(args[i + 1]);
914 std::string address = std::string(args[i + 2]);
915 std::string connection = std::string(args[i + 3]);
916
917 std::string stringGains = std::string(args[i + 4]);
918 std::vector<AudioGain> gains;
919 status = parseAudioGains(fd, stringGains, gains);
920 if (status != STATUS_OK) {
921 return status;
922 }
923
924 AudioPort port = createPort(
925 id, name, 0 /*flags*/, false /*isInput*/,
926 createDeviceExt(AudioDeviceType::OUT_DEVICE, 0 /*flags*/, connection, address));
927 port.gains.insert(port.gains.begin(), gains.begin(), gains.end());
928
929 ports.push_back(port);
930 }
931
932 if (mModuleChangeCallback == nullptr) {
933 dprintf(fd,
934 "Unable to trigger audio port callback for ports: %s \n"
935 " - no module change callback registered\n",
936 toString(ports).c_str());
937 return STATUS_BAD_VALUE;
938 }
939
940 // TODO (b/269139706) fix atomic read issue
941 mModuleChangeCallback->onAudioPortsChanged(ports);
942 dprintf(fd, "SUCCESS audio port callback for ports: %s \n", toString(ports).c_str());
943 return STATUS_OK;
944}
Hayden Gomesaeeb9b02020-10-27 13:08:34 -0700945} // namespace aidl::android::hardware::automotive::audiocontrol