Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 87e0e580 authored by Vlad Popa's avatar Vlad Popa
Browse files

AbsVol: boost attenuation of the non driving abs vol streams

In order to avoid the double attenuation subtract when playing on
absolute volume devices the attenuation which is already applied by the
absolute volume controller.

Flag: com.android.media.audio.abs_volume_index_fix
Test: logs manual - adb shell setprop log.tag.APM_AudioPolicyManager V
Bug: 340693050
Change-Id: I0c38981565e8d14bf13ac3cd7e26b0edd5e111f6
parent 6c800f95
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -1289,6 +1289,21 @@ void AudioSystem::releaseInput(audio_port_handle_t portId) {
    (void) status;
}

status_t AudioSystem::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
                                                     const char *address,
                                                     bool enabled,
                                                     audio_stream_type_t streamToDriveAbs) {
    const sp<IAudioPolicyService> aps = get_audio_policy_service();
    if (aps == nullptr) return PERMISSION_DENIED;

    AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_device_AudioDevice(deviceType, address));
    AudioStreamType streamToDriveAbsAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_stream_type_t_AudioStreamType(streamToDriveAbs));
    return statusTFromBinderStatus(
            aps->setDeviceAbsoluteVolumeEnabled(deviceAidl, enabled, streamToDriveAbsAidl));
}

status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
                                       int indexMin,
                                       int indexMax) {
+4 −0
Original line number Diff line number Diff line
@@ -114,6 +114,10 @@ interface IAudioPolicyService {

    void releaseInput(int /* audio_port_handle_t */ portId);

    oneway void setDeviceAbsoluteVolumeEnabled(in AudioDevice device,
                                               boolean enabled,
                                               AudioStreamType streamToDriveAbs);

    void initStreamVolume(AudioStreamType stream,
                          int indexMin,
                          int indexMax);
+6 −2
Original line number Diff line number Diff line
@@ -374,6 +374,10 @@ public:
    static status_t startInput(audio_port_handle_t portId);
    static status_t stopInput(audio_port_handle_t portId);
    static void releaseInput(audio_port_handle_t portId);
    static status_t setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
                                                   const char *address,
                                                   bool enabled,
                                                   audio_stream_type_t streamToDriveAbs);
    static status_t initStreamVolume(audio_stream_type_t stream,
                                     int indexMin,
                                     int indexMax);
+8 −2
Original line number Diff line number Diff line
@@ -180,6 +180,12 @@ public:
    // volume control functions
    //

    // notifies the audio policy manager that the absolute volume mode is enabled/disabled on
    // the passed device. Also specifies the stream that is controlling the absolute volume.
    virtual status_t setDeviceAbsoluteVolumeEnabled(audio_devices_t device,
                                                    const char *address,
                                                    bool enabled,
                                                    audio_stream_type_t streamToDriveAbs) = 0;
    // initialises stream volume conversion parameters by specifying volume index range.
    virtual void initStreamVolume(audio_stream_type_t stream,
                                  int indexMin,
+73 −2
Original line number Diff line number Diff line
@@ -3374,6 +3374,27 @@ void AudioPolicyManager::checkCloseInputs() {
    }
}

status_t AudioPolicyManager::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
                                                            const char *address __unused,
                                                            bool enabled,
                                                            audio_stream_type_t streamToDriveAbs)
{
    audio_attributes_t attributesToDriveAbs = mEngine->getAttributesForStreamType(streamToDriveAbs);
    if (attributesToDriveAbs == AUDIO_ATTRIBUTES_INITIALIZER) {
        ALOGW("%s: no attributes for stream %s, bailing out", __func__,
              toString(streamToDriveAbs).c_str());
        return BAD_VALUE;
    }

    if (enabled) {
        mAbsoluteVolumeDrivingStreams[deviceType] = attributesToDriveAbs;
    } else {
        mAbsoluteVolumeDrivingStreams.erase(deviceType);
    }

    return NO_ERROR;
}

void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax)
{
    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
@@ -4487,6 +4508,13 @@ void AudioPolicyManager::dump(String8 *dst) const

    dst->appendFormat("\nPolicy Engine dump:\n");
    mEngine->dump(dst);

    dst->appendFormat("\nAbsolute volume devices with driving streams:\n");
    for (const auto it : mAbsoluteVolumeDrivingStreams) {
        dst->appendFormat("   - device type: %s, driving stream %d\n",
                          dumpDeviceTypes({it.first}).c_str(),
                          mEngine->getVolumeGroupForAttributes(it.second));
    }
}

status_t AudioPolicyManager::dump(int fd)
@@ -7986,14 +8014,57 @@ sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &de
    return nullptr;
}

float AudioPolicyManager::adjustDeviceAttenuationForAbsVolume(IVolumeCurves &curves,
                                                              VolumeSource volumeSource,
                                                              int index,
                                                              const DeviceTypeSet &deviceTypes)
{
    audio_devices_t volumeDevice = Volume::getDeviceForVolume(deviceTypes);
    device_category deviceCategory = Volume::getDeviceCategory({volumeDevice});
    float volumeDb = curves.volIndexToDb(deviceCategory, index);

    if (com_android_media_audio_abs_volume_index_fix()) {
        if (mAbsoluteVolumeDrivingStreams.find(volumeDevice) !=
            mAbsoluteVolumeDrivingStreams.end()) {
            audio_attributes_t attributesToDriveAbs = mAbsoluteVolumeDrivingStreams[volumeDevice];
            auto groupToDriveAbs = mEngine->getVolumeGroupForAttributes(attributesToDriveAbs);
            if (groupToDriveAbs == VOLUME_GROUP_NONE) {
                ALOGD("%s: no group matching with %s", __FUNCTION__,
                      toString(attributesToDriveAbs).c_str());
                return volumeDb;
            }

            float volumeDbMax = curves.volIndexToDb(deviceCategory, curves.getVolumeIndexMax());
            VolumeSource vsToDriveAbs = toVolumeSource(groupToDriveAbs);
            if (vsToDriveAbs == volumeSource) {
                // attenuation is applied by the abs volume controller
                return volumeDbMax;
            } else {
                IVolumeCurves &curvesAbs = getVolumeCurves(vsToDriveAbs);
                int indexAbs = curvesAbs.getVolumeIndex({volumeDevice});
                float volumeDbAbs = curvesAbs.volIndexToDb(deviceCategory, indexAbs);
                float volumeDbAbsMax = curvesAbs.volIndexToDb(deviceCategory,
                                                              curvesAbs.getVolumeIndexMax());
                float newVolumeDb = fminf(volumeDb + volumeDbAbsMax - volumeDbAbs, volumeDbMax);
                ALOGV("%s: abs vol stream %d with attenuation %f is adjusting stream %d from "
                      "attenuation %f to attenuation %f %f", __func__, vsToDriveAbs, volumeDbAbs,
                      volumeSource, volumeDb, newVolumeDb, volumeDbMax);
                return newVolumeDb;
            }
        }
        return volumeDb;
    } else {
        return volumeDb;
    }
}

float AudioPolicyManager::computeVolume(IVolumeCurves &curves,
                                        VolumeSource volumeSource,
                                        int index,
                                        const DeviceTypeSet& deviceTypes,
                                        bool computeInternalInteraction)
{
    float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index);

    float volumeDb = adjustDeviceAttenuationForAbsVolume(curves, volumeSource, index, deviceTypes);
    ALOGV("%s volume source %d, index %d,  devices %s, compute internal %b ", __func__,
          volumeSource, index, dumpDeviceTypes(deviceTypes).c_str(), computeInternalInteraction);

Loading