Add default implementation for dynamic volume config feature
- new dumpsys cmd "--audioPortChangeCallback" to trigger AudioPort
callback. For now, limited to AudioGain stage updates only.
- AudioPort callback will carry the new gain stages information to
the framework.
Bug: 261647905
Test: VTS
Change-Id: I404bde3671bf1ad39d1cb091ebf63d1049edec34
Signed-off-by: Raj Goparaju <rajgoparaju@google.com>
diff --git a/automotive/audiocontrol/aidl/default/AudioControl.cpp b/automotive/audiocontrol/aidl/default/AudioControl.cpp
index a121f8b..cf7307d 100644
--- a/automotive/audiocontrol/aidl/default/AudioControl.cpp
+++ b/automotive/audiocontrol/aidl/default/AudioControl.cpp
@@ -24,6 +24,7 @@
#include <aidl/android/hardware/automotive/audiocontrol/IFocusListener.h>
#include <android-base/logging.h>
+#include <android-base/parsebool.h>
#include <android-base/parseint.h>
#include <android-base/strings.h>
@@ -37,6 +38,8 @@
namespace aidl::android::hardware::automotive::audiocontrol {
using ::android::base::EqualsIgnoreCase;
+using ::android::base::ParseBool;
+using ::android::base::ParseBoolResult;
using ::android::base::ParseInt;
using ::std::shared_ptr;
using ::std::string;
@@ -70,6 +73,95 @@
}
} // namespace
+namespace {
+using ::aidl::android::media::audio::common::AudioChannelLayout;
+using ::aidl::android::media::audio::common::AudioDeviceDescription;
+using ::aidl::android::media::audio::common::AudioDeviceType;
+using ::aidl::android::media::audio::common::AudioFormatDescription;
+using ::aidl::android::media::audio::common::AudioFormatType;
+using ::aidl::android::media::audio::common::AudioGain;
+using ::aidl::android::media::audio::common::AudioGainMode;
+using ::aidl::android::media::audio::common::AudioIoFlags;
+using ::aidl::android::media::audio::common::AudioOutputFlags;
+using ::aidl::android::media::audio::common::AudioPort;
+using ::aidl::android::media::audio::common::AudioPortDeviceExt;
+using ::aidl::android::media::audio::common::AudioPortExt;
+using ::aidl::android::media::audio::common::AudioPortMixExt;
+using ::aidl::android::media::audio::common::AudioProfile;
+using ::aidl::android::media::audio::common::PcmType;
+
+// reuse common code artifacts
+void fillProfile(const std::vector<int32_t>& channelLayouts,
+ const std::vector<int32_t>& sampleRates, AudioProfile* profile) {
+ for (auto layout : channelLayouts) {
+ profile->channelMasks.push_back(
+ AudioChannelLayout::make<AudioChannelLayout::layoutMask>(layout));
+ }
+ profile->sampleRates.insert(profile->sampleRates.end(), sampleRates.begin(), sampleRates.end());
+}
+
+AudioProfile createProfile(PcmType pcmType, const std::vector<int32_t>& channelLayouts,
+ const std::vector<int32_t>& sampleRates) {
+ AudioProfile profile;
+ profile.format.type = AudioFormatType::PCM;
+ profile.format.pcm = pcmType;
+ fillProfile(channelLayouts, sampleRates, &profile);
+ return profile;
+}
+
+AudioProfile createProfile(const std::string& encodingType,
+ const std::vector<int32_t>& channelLayouts,
+ const std::vector<int32_t>& sampleRates) {
+ AudioProfile profile;
+ profile.format.encoding = encodingType;
+ fillProfile(channelLayouts, sampleRates, &profile);
+ return profile;
+}
+
+AudioPortExt createDeviceExt(AudioDeviceType devType, int32_t flags,
+ const std::string& connection = "", const std::string& address = "") {
+ AudioPortDeviceExt deviceExt;
+ deviceExt.device.type.type = devType;
+ if (devType == AudioDeviceType::IN_MICROPHONE && connection.empty()) {
+ deviceExt.device.address = "bottom";
+ } else if (devType == AudioDeviceType::IN_MICROPHONE_BACK && connection.empty()) {
+ deviceExt.device.address = "back";
+ } else {
+ deviceExt.device.address = std::move(address);
+ }
+ deviceExt.device.type.connection = std::move(connection);
+ deviceExt.flags = flags;
+ return AudioPortExt::make<AudioPortExt::Tag::device>(deviceExt);
+}
+
+AudioPort createPort(int32_t id, const std::string& name, int32_t flags, bool isInput,
+ const AudioPortExt& ext) {
+ AudioPort port;
+ port.id = id;
+ port.name = name;
+ port.flags = isInput ? AudioIoFlags::make<AudioIoFlags::Tag::input>(flags)
+ : AudioIoFlags::make<AudioIoFlags::Tag::output>(flags);
+ port.ext = ext;
+ return port;
+}
+
+AudioGain createGain(int32_t mode, AudioChannelLayout channelMask, int32_t minValue,
+ int32_t maxValue, int32_t defaultValue, int32_t stepValue,
+ int32_t minRampMs = 100, int32_t maxRampMs = 100, bool useForVolume = true) {
+ AudioGain gain;
+ gain.mode = mode;
+ gain.channelMask = channelMask;
+ gain.minValue = minValue;
+ gain.maxValue = maxValue;
+ gain.defaultValue = defaultValue;
+ gain.stepValue = stepValue;
+ gain.minRampMs = minRampMs;
+ gain.maxRampMs = maxRampMs;
+ gain.useForVolume = useForVolume;
+ return gain;
+}
+} // namespace
+
ndk::ScopedAStatus AudioControl::registerFocusListener(
const shared_ptr<IFocusListener>& in_listener) {
LOG(DEBUG) << "registering focus listener";
@@ -190,6 +282,33 @@
return ndk::ScopedAStatus::ok();
}
+ndk::ScopedAStatus AudioControl::setModuleChangeCallback(
+ const std::shared_ptr<IModuleChangeCallback>& in_callback) {
+ LOG(DEBUG) << ": " << __func__;
+
+ if (in_callback.get() == nullptr) {
+ LOG(ERROR) << __func__ << ": Callback is nullptr";
+ return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
+ }
+ if (mModuleChangeCallback != nullptr) {
+ LOG(ERROR) << __func__ << ": Module change callback was already registered";
+ return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
+ }
+ std::atomic_store(&mModuleChangeCallback, in_callback);
+ return ndk::ScopedAStatus::ok();
+}
+
+ndk::ScopedAStatus AudioControl::clearModuleChangeCallback() {
+ if (mModuleChangeCallback != nullptr) {
+ shared_ptr<IModuleChangeCallback> nullCallback = nullptr;
+ std::atomic_store(&mModuleChangeCallback, nullCallback);
+ LOG(DEBUG) << ":" << __func__ << ": Unregistered successfully";
+ } else {
+ LOG(DEBUG) << ":" << __func__ << ": No callback registered, no-op";
+ }
+ return ndk::ScopedAStatus::ok();
+}
+
binder_status_t AudioControl::dump(int fd, const char** args, uint32_t numArgs) {
if (numArgs == 0) {
return dumpsys(fd);
@@ -208,6 +327,8 @@
return cmdAbandonFocusWithMetaData(fd, args, numArgs);
} else if (EqualsIgnoreCase(option, "--audioGainCallback")) {
return cmdOnAudioDeviceGainsChanged(fd, args, numArgs);
+ } else if (EqualsIgnoreCase(option, "--audioPortsChangedCallback")) {
+ return cmdOnAudioPortsChanged(fd, args, numArgs);
} else {
dprintf(fd, "Invalid option: %s\n", option.c_str());
return STATUS_BAD_VALUE;
@@ -262,7 +383,21 @@
dprintf(fd,
"Tags are optional. If provided, it must follow the <key>=<value> pattern, where the "
"value is namespaced (for example com.google.strategy=VR).\n");
-
+ dprintf(fd,
+ "--audioPortsChangedCallback <ID_1> <NAME_1> <BUS_ADDRESS_1> <CONNECTION_TYPE_1> "
+ "<AUDIO_GAINS_1> [<ID_N> <NAME_N> <BUS_ADDRESS_N> <CONNECTION_TYPE_N> "
+ "<AUDIO_GAINS_N>]: fires audio ports changed callback. Carries list of modified "
+ "AudioPorts. "
+ "For simplicity, this command accepts limited information for each AudioPort: "
+ "id(int), name(string), port address(string), connection type (string), "
+ "audio gain (csv int)\n");
+ dprintf(fd, "Notes: \n");
+ dprintf(fd,
+ "1. AudioGain csv should match definition at "
+ "android/media/audio/common/AudioPort.aidl\n");
+ dprintf(fd,
+ "2. See android/media/audio/common/AudioDeviceDescription.aidl for valid "
+ "<CONNECTION_TYPE> values.\n");
return STATUS_OK;
}
@@ -514,4 +649,166 @@
toEnumString(reasons).c_str(), toString(agcis).c_str());
return STATUS_OK;
}
+
+binder_status_t AudioControl::parseAudioGains(int fd, const std::string& stringGain,
+ std::vector<AudioGain>& gains) {
+ const int kAudioGainSize = 9;
+ std::stringstream csvGain(stringGain);
+ std::vector<std::string> vecGain;
+ std::string value;
+ while (getline(csvGain, value, ',')) {
+ vecGain.push_back(value);
+ }
+
+ if ((vecGain.size() == 0) || ((vecGain.size() % kAudioGainSize) != 0)) {
+ dprintf(fd, "Erroneous input to generate AudioGain: %s\n", stringGain.c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ // iterate over injected AudioGains
+ for (int index = 0; index < vecGain.size(); index += kAudioGainSize) {
+ int32_t mode;
+ if (!safelyParseInt(vecGain[index], &mode)) {
+ dprintf(fd, "Non-integer index provided with request: %s\n", vecGain[index].c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ // car audio framework only supports JOINT mode.
+ // skip injected AudioGains that are not compliant with this.
+ if (mode != static_cast<int>(AudioGainMode::JOINT)) {
+ LOG(WARNING) << ":" << __func__ << ": skipping gain since it is not JOINT mode!";
+ continue;
+ }
+
+ int32_t layout;
+ if (!safelyParseInt(vecGain[index + 1], &layout)) {
+ dprintf(fd, "Non-integer index provided with request: %s\n",
+ vecGain[index + 1].c_str());
+ return STATUS_BAD_VALUE;
+ }
+ AudioChannelLayout channelMask =
+ AudioChannelLayout::make<AudioChannelLayout::layoutMask>(layout);
+
+ int32_t minValue;
+ if (!safelyParseInt(vecGain[index + 2], &minValue)) {
+ dprintf(fd, "Non-integer index provided with request: %s\n",
+ vecGain[index + 2].c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ int32_t maxValue;
+ if (!safelyParseInt(vecGain[index + 3], &maxValue)) {
+ dprintf(fd, "Non-integer index provided with request: %s\n",
+ vecGain[index + 3].c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ int32_t defaultValue;
+ if (!safelyParseInt(vecGain[index + 4], &defaultValue)) {
+ dprintf(fd, "Non-integer index provided with request: %s\n",
+ vecGain[index + 4].c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ int32_t stepValue;
+ if (!safelyParseInt(vecGain[index + 5], &stepValue)) {
+ dprintf(fd, "Non-integer index provided with request: %s\n",
+ vecGain[index + 5].c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ int32_t minRampMs;
+ if (!safelyParseInt(vecGain[index + 6], &minRampMs)) {
+ dprintf(fd, "Non-integer index provided with request: %s\n",
+ vecGain[index + 6].c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ int32_t maxRampMs;
+ if (!safelyParseInt(vecGain[index + 7], &maxRampMs)) {
+ dprintf(fd, "Non-integer index provided with request: %s\n",
+ vecGain[index + 7].c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ ParseBoolResult useForVolume = ParseBool(vecGain[index + 8]);
+ if (useForVolume == ParseBoolResult::kError) {
+ dprintf(fd, "Non-boolean index provided with request: %s\n",
+ vecGain[index + 8].c_str());
+ return STATUS_BAD_VALUE;
+ } else if (useForVolume == ParseBoolResult::kFalse) {
+ // at this level we only care about gain stages that are relevant
+ // for volume control. skip the gain stage if its flagged as false.
+ LOG(WARNING) << ":" << __func__
+ << ": skipping gain since it is not for volume control!";
+ continue;
+ }
+
+ AudioGain gain = createGain(mode, channelMask, minValue, maxValue, defaultValue, stepValue,
+ minRampMs, maxRampMs, true /*useForVolume*/);
+ gains.push_back(gain);
+ }
+ return STATUS_OK;
+}
+
+binder_status_t AudioControl::cmdOnAudioPortsChanged(int fd, const char** args, uint32_t numArgs) {
+ if (!checkCallerHasWritePermissions(fd)) {
+ return STATUS_PERMISSION_DENIED;
+ }
+
+ if ((numArgs < 6) || ((numArgs - 1) % 5 != 0)) {
+ dprintf(fd,
+ "Invalid number of arguments: please provide\n"
+ "--audioPortsChangedCallback <ID_1> <NAME_1> <BUS_ADDRESS_1> <CONNECTION_TYPE_1> "
+ "<AUDIO_GAINS_1> [<ID_N> <NAME_N> <BUS_ADDRESS_N> <CONNECTION_TYPE_N> "
+ "<AUDIO_GAINS_N>]: triggers audio ports changed callback. Carries list of "
+ "modified AudioPorts. "
+ "For simplicity, this command accepts limited information for each AudioPort: "
+ "id(int), name(string), port address(string), connection type (string), "
+ "audio gain (csv int)\n");
+ return STATUS_BAD_VALUE;
+ }
+
+ std::vector<AudioPort> ports;
+ for (uint32_t i = 1; i < numArgs; i += 5) {
+ binder_status_t status;
+ int32_t id;
+ if (!safelyParseInt(std::string(args[i]), &id)) {
+ dprintf(fd, "Non-integer index provided with request: %s\n",
+ std::string(args[i]).c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ std::string name = std::string(args[i + 1]);
+ std::string address = std::string(args[i + 2]);
+ std::string connection = std::string(args[i + 3]);
+
+ std::string stringGains = std::string(args[i + 4]);
+ std::vector<AudioGain> gains;
+ status = parseAudioGains(fd, stringGains, gains);
+ if (status != STATUS_OK) {
+ return status;
+ }
+
+ AudioPort port = createPort(
+ id, name, 0 /*flags*/, false /*isInput*/,
+ createDeviceExt(AudioDeviceType::OUT_DEVICE, 0 /*flags*/, connection, address));
+ port.gains.insert(port.gains.begin(), gains.begin(), gains.end());
+
+ ports.push_back(port);
+ }
+
+ if (mModuleChangeCallback == nullptr) {
+ dprintf(fd,
+ "Unable to trigger audio port callback for ports: %s \n"
+ " - no module change callback registered\n",
+ toString(ports).c_str());
+ return STATUS_BAD_VALUE;
+ }
+
+ // TODO (b/269139706) fix atomic read issue
+ mModuleChangeCallback->onAudioPortsChanged(ports);
+ dprintf(fd, "SUCCESS audio port callback for ports: %s \n", toString(ports).c_str());
+ return STATUS_OK;
+}
} // namespace aidl::android::hardware::automotive::audiocontrol