AudioFlinger: Control volume using Port ID
This CL migrates the volume management within AudioFlinger
from stream type to port ID.
It gives full power to AudioPolicy to compute the list of port
(so MmapThreads/Tracks) on which volume control is required.
It prevents from overwritting MUSIC stream type which is
the default for volume groups without associated stream type.
Bug: 317212590
Test: build & play audio
Test: atest audiopolicy_tests
Flag: com.android.media.audioserver.portid_volume_management
Change-Id: I4c9e8bb45660c9ceffcc0f4029b0617f9795ab3c
diff --git a/services/audiopolicy/service/AudioPolicyClientImpl.cpp b/services/audiopolicy/service/AudioPolicyClientImpl.cpp
index f70dc52..a598a52 100644
--- a/services/audiopolicy/service/AudioPolicyClientImpl.cpp
+++ b/services/audiopolicy/service/AudioPolicyClientImpl.cpp
@@ -188,6 +188,13 @@
delay_ms);
}
+status_t AudioPolicyService::AudioPolicyClient::setPortsVolume(
+ const std::vector<audio_port_handle_t> &ports, float volume, audio_io_handle_t output,
+ int delayMs)
+{
+ return mAudioPolicyService->setPortsVolume(ports, volume, output, delayMs);
+}
+
void AudioPolicyService::AudioPolicyClient::setParameters(audio_io_handle_t io_handle,
const String8& keyValuePairs,
int delay_ms)
diff --git a/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp b/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp
index f414862..6194002 100644
--- a/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp
+++ b/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp
@@ -423,6 +423,7 @@
AudioPolicyInterface::output_type_t outputType;
bool isSpatialized = false;
bool isBitPerfect = false;
+ float volume;
status_t result = mAudioPolicyManager->getOutputForAttr(&attr, &output, session,
&stream,
attributionSource,
@@ -431,7 +432,8 @@
&secondaryOutputs,
&outputType,
&isSpatialized,
- &isBitPerfect);
+ &isBitPerfect,
+ &volume);
// FIXME: Introduce a way to check for the the telephony device before opening the output
if (result == NO_ERROR) {
@@ -495,6 +497,7 @@
_aidl_return->isBitPerfect = isBitPerfect;
_aidl_return->attr = VALUE_OR_RETURN_BINDER_STATUS(
legacy2aidl_audio_attributes_t_AudioAttributes(attr));
+ _aidl_return->volume = volume;
} else {
_aidl_return->configBase.format = VALUE_OR_RETURN_BINDER_STATUS(
legacy2aidl_audio_format_t_AudioFormatDescription(config.format));
diff --git a/services/audiopolicy/service/AudioPolicyService.cpp b/services/audiopolicy/service/AudioPolicyService.cpp
index cc67481..8c3faeb 100644
--- a/services/audiopolicy/service/AudioPolicyService.cpp
+++ b/services/audiopolicy/service/AudioPolicyService.cpp
@@ -1815,6 +1815,16 @@
data->mIO);
ul.lock();
}break;
+ case SET_PORTS_VOLUME: {
+ VolumePortsData *data = (VolumePortsData *)command->mParam.get();
+ ALOGV("AudioCommandThread() processing set volume Ports %s volume %f, \
+ output %d", data->dumpPorts().c_str(), data->mVolume, data->mIO);
+ ul.unlock();
+ command->mStatus = AudioSystem::setPortsVolume(data->mPorts,
+ data->mVolume,
+ data->mIO);
+ ul.lock();
+ }break;
case SET_PARAMETERS: {
ParametersData *data = (ParametersData *)command->mParam.get();
ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
@@ -2127,6 +2137,23 @@
return sendCommand(command, delayMs);
}
+status_t AudioPolicyService::AudioCommandThread::volumePortsCommand(
+ const std::vector<audio_port_handle_t> &ports, float volume, audio_io_handle_t output,
+ int delayMs)
+{
+ sp<AudioCommand> command = new AudioCommand();
+ command->mCommand = SET_PORTS_VOLUME;
+ sp<VolumePortsData> data = new VolumePortsData();
+ data->mPorts = ports;
+ data->mVolume = volume;
+ data->mIO = output;
+ command->mParam = data;
+ command->mWaitStatus = true;
+ ALOGV("AudioCommandThread() adding set volume ports %s, volume %f, output %d",
+ data->dumpPorts().c_str(), volume, output);
+ return sendCommand(command, delayMs);
+}
+
status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
const char *keyValuePairs,
int delayMs)
@@ -2457,6 +2484,31 @@
delayMs = 1;
} break;
+ case SET_PORTS_VOLUME: {
+ VolumePortsData *data = (VolumePortsData *)command->mParam.get();
+ VolumePortsData *data2 = (VolumePortsData *)command2->mParam.get();
+ if (data->mIO != data2->mIO) break;
+ // Can remove command only if port ids list is the same, otherwise, remove from
+ // command 2 all port whose volume will be replaced with command 1 volume.
+ std::vector<audio_port_handle_t> portsOnlyInCommand2{};
+ std::copy_if(data2->mPorts.begin(), data2->mPorts.end(),
+ std::back_inserter(portsOnlyInCommand2), [&](const auto &portId) {
+ return std::find(data->mPorts.begin(), data->mPorts.end(), portId) ==
+ data->mPorts.end();
+ });
+ if (!portsOnlyInCommand2.empty()) {
+ data2->mPorts = portsOnlyInCommand2;
+ break;
+ }
+ ALOGV("Filtering out volume command on output %d for ports %s",
+ data->mIO, data->dumpPorts().c_str());
+ removedCommands.add(command2);
+ command->mTime = command2->mTime;
+ // force delayMs to non 0 so that code below does not request to wait for
+ // command status as the command is now delayed
+ delayMs = 1;
+ } break;
+
case SET_VOICE_VOLUME: {
VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
@@ -2603,6 +2655,12 @@
output, delayMs);
}
+int AudioPolicyService::setPortsVolume(const std::vector<audio_port_handle_t> &ports, float volume,
+ audio_io_handle_t output, int delayMs)
+{
+ return (int)mAudioCommandThread->volumePortsCommand(ports, volume, output, delayMs);
+}
+
int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
{
return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
diff --git a/services/audiopolicy/service/AudioPolicyService.h b/services/audiopolicy/service/AudioPolicyService.h
index 720ba84..0492cd3 100644
--- a/services/audiopolicy/service/AudioPolicyService.h
+++ b/services/audiopolicy/service/AudioPolicyService.h
@@ -47,6 +47,7 @@
#include <android/hardware/BnSensorPrivacyListener.h>
#include <android/content/AttributionSourceState.h>
+#include <numeric>
#include <unordered_map>
namespace android {
@@ -354,6 +355,21 @@
float volume,
audio_io_handle_t output,
int delayMs = 0);
+
+ /**
+ * Set a volume on AudioTrack port id(s) for a particular output.
+ * For the same user setting, a volume group (and associated given port of the
+ * client's track) can have different volumes for each output destination device
+ * it is attached to.
+ *
+ * @param ports to consider
+ * @param volume to set
+ * @param output to consider
+ * @param delayMs to use
+ * @return NO_ERROR if successful
+ */
+ virtual status_t setPortsVolume(const std::vector<audio_port_handle_t> &ports, float volume,
+ audio_io_handle_t output, int delayMs = 0);
virtual status_t setVoiceVolume(float volume, int delayMs = 0);
void doOnNewAudioModulesAvailable();
@@ -577,6 +593,7 @@
// commands for tone AudioCommand
enum {
SET_VOLUME,
+ SET_PORTS_VOLUME,
SET_PARAMETERS,
SET_VOICE_VOLUME,
STOP_OUTPUT,
@@ -610,6 +627,8 @@
void exit();
status_t volumeCommand(audio_stream_type_t stream, float volume,
audio_io_handle_t output, int delayMs = 0);
+ status_t volumePortsCommand(const std::vector<audio_port_handle_t> &ports,
+ float volume, audio_io_handle_t output, int delayMs = 0);
status_t parametersCommand(audio_io_handle_t ioHandle,
const char *keyValuePairs, int delayMs = 0);
status_t voiceVolumeCommand(float volume, int delayMs = 0);
@@ -684,6 +703,20 @@
audio_io_handle_t mIO;
};
+ class VolumePortsData : public AudioCommandData {
+ public:
+ std::vector<audio_port_handle_t> mPorts;
+ float mVolume;
+ audio_io_handle_t mIO;
+ std::string dumpPorts() {
+ return std::string("volume ") + std::to_string(mVolume) + " on IO " +
+ std::to_string(mIO) + " and ports " +
+ std::accumulate(std::begin(mPorts), std::end(mPorts), std::string{},
+ [] (const std::string& ls, int rs) {
+ return ls + std::to_string(rs) + " "; });
+ }
+ };
+
class ParametersData : public AudioCommandData {
public:
audio_io_handle_t mIO;
@@ -823,6 +856,19 @@
// set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
// for each output (destination device) it is attached to.
virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0);
+ /**
+ * Set a volume on port(s) for a particular output. For the same user setting, a volume
+ * group (and associated given port of the client's track) can have different volumes for
+ * each output (destination device) it is attached to.
+ *
+ * @param ports to consider
+ * @param volume to set
+ * @param output to consider
+ * @param delayMs to use
+ * @return NO_ERROR if successful
+ */
+ status_t setPortsVolume(const std::vector<audio_port_handle_t> &ports, float volume,
+ audio_io_handle_t output, int delayMs = 0) override;
// function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);