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

Commit 109bde3b authored by Vlad Popa's avatar Vlad Popa
Browse files

CSD: Avoid higher volume when resetting to RS1

Also add some more logging for printing out the abs volume attenuation
used for the sound dose computation.

Flag: EXEMPT bugfix
Test: manual
Bug: 348579505
Change-Id: I5c7fea5d1f2933e617400b6ffc62199a12f884dd
parent d9227e90
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -11698,7 +11698,7 @@ public class AudioService extends IAudioService.Stub
    static final int LOG_NB_EVENTS_VOLUME = 100;
    static final int LOG_NB_EVENTS_DYN_POLICY = 10;
    static final int LOG_NB_EVENTS_SPATIAL = 30;
    static final int LOG_NB_EVENTS_SOUND_DOSE = 30;
    static final int LOG_NB_EVENTS_SOUND_DOSE = 50;
    static final int LOG_NB_EVENTS_LOUDNESS_CODEC = 30;
+9 −0
Original line number Diff line number Diff line
@@ -577,6 +577,7 @@ public class AudioServiceEvents {
        static final int DOSE_REPEAT_5X = 2;
        static final int DOSE_ACCUMULATION_START = 3;
        static final int LOWER_VOLUME_TO_RS1 = 4;
        static final int UPDATE_ABS_VOLUME_ATTENUATION = 5;

        final int mEventType;
        final float mFloatValue;
@@ -608,6 +609,10 @@ public class AudioServiceEvents {
            return new SoundDoseEvent(LOWER_VOLUME_TO_RS1, 0 /*ignored*/, 0 /*ignored*/);
        }

        static SoundDoseEvent getAbsVolumeAttenuationEvent(float attenuation, int device) {
            return new SoundDoseEvent(UPDATE_ABS_VOLUME_ATTENUATION, attenuation, device);
        }

        @Override
        public String eventToString() {
            switch (mEventType) {
@@ -623,6 +628,10 @@ public class AudioServiceEvents {
                    return "CSD accumulating: RS2 entered";
                case LOWER_VOLUME_TO_RS1:
                    return "CSD lowering volume to RS1";
                case UPDATE_ABS_VOLUME_ATTENUATION:
                    return String.format(java.util.Locale.US,
                            "Updating CSD absolute volume attenuation on device %d with %.2f dB ",
                            mLongValue, mFloatValue);
            }
            return new StringBuilder("FIXME invalid event type:").append(mEventType).toString();
        }
+29 −17
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.media.AudioSystem;
import android.media.ISoundDose;
import android.media.ISoundDoseCallback;
import android.media.SoundDoseRecord;
import android.media.VolumeInfo;
import android.os.Binder;
import android.os.Message;
import android.os.RemoteException;
@@ -895,6 +896,8 @@ public class SoundDoseHelper {

        try {
            if (!isAbsoluteVolume) {
                mLogger.enqueue(
                        SoundDoseEvent.getAbsVolumeAttenuationEvent(/*attenuation=*/0.f, device));
                // remove any possible previous attenuation
                soundDose.updateAttenuation(/* attenuationDB= */0.f, device);

@@ -903,10 +906,11 @@ public class SoundDoseHelper {

            if (AudioService.mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC
                    && safeDevicesContains(device)) {
                soundDose.updateAttenuation(
                        -AudioSystem.getStreamVolumeDB(AudioSystem.STREAM_MUSIC,
                                (newIndex + 5) / 10,
                                device), device);
                float attenuationDb = -AudioSystem.getStreamVolumeDB(AudioSystem.STREAM_MUSIC,
                        (newIndex + 5) / 10, device);
                mLogger.enqueue(
                        SoundDoseEvent.getAbsVolumeAttenuationEvent(attenuationDb, device));
                soundDose.updateAttenuation(attenuationDb, device);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Could not apply the attenuation for MEL calculation with volume index "
@@ -1313,22 +1317,30 @@ public class SoundDoseHelper {

    /** Called when handling MSG_LOWER_VOLUME_TO_RS1 */
    private void onLowerVolumeToRs1() {
        mLogger.enqueue(SoundDoseEvent.getLowerVolumeToRs1Event());
        final ArrayList<AudioDeviceAttributes> devices = mAudioService.getDevicesForAttributesInt(
                new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(), true);
        final int nativeDeviceType;
        final AudioDeviceAttributes ada;
        if (!devices.isEmpty()) {
            ada = devices.get(0);
            nativeDeviceType = ada.getInternalType();
        } else {
            nativeDeviceType = AudioSystem.DEVICE_OUT_USB_HEADSET;
            ada = new AudioDeviceAttributes(AudioSystem.DEVICE_OUT_USB_HEADSET, "");
                new AudioAttributes.Builder().setUsage(
                        AudioAttributes.USAGE_MEDIA).build(), /*forVolume=*/true);
        if (devices.isEmpty()) {
            Log.e(TAG, "Cannot lower the volume to RS1, no devices registered for USAGE_MEDIA");
            return;
        }
        final int index = safeMediaVolumeIndex(nativeDeviceType);
        mAudioService.setStreamVolumeWithAttributionInt(STREAM_MUSIC, index / 10, /*flags*/ 0, ada,
        final AudioDeviceAttributes ada = devices.get(0);
        final int nativeDeviceType = ada.getInternalType();
        final int index = safeMediaVolumeIndex(nativeDeviceType) / 10;
        final VolumeInfo curVolume = mAudioService.getDeviceVolume(
                new VolumeInfo.Builder(STREAM_MUSIC).build(), ada,
                /*callingPackage=*/"sounddosehelper");

        if (index < curVolume.getVolumeIndex()) {
            mLogger.enqueue(SoundDoseEvent.getLowerVolumeToRs1Event());
            mAudioService.setStreamVolumeWithAttributionInt(STREAM_MUSIC, index, /*flags*/ 0, ada,
                    mContext.getOpPackageName(), /*attributionTag=*/null,
                true /*canChangeMuteAndUpdateController*/);
                    /*canChangeMuteAndUpdateController=*/true);
        } else {
            Log.i(TAG, "The current volume " + curVolume.getVolumeIndex()
                    + " for device type " + nativeDeviceType
                    + " is already smaller or equal to the safe index volume " + index);
        }
    }

    // StreamVolumeCommand contains the information needed to defer the process of