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

Commit 0a3da508 authored by Etienne Ruffieux's avatar Etienne Ruffieux
Browse files

Unify volume conversion method and add force vol update

Upon connection, the remote device sends its volume to the
phone, and the phone will then send its own if it's not the
same. In come cases, the remote device doesn't send its
volume and we end up not sending the local either because
it's the same as before disconnection.

This change resets the volume on disconnection and replaces
the floor/ceil used for conversion from system to AVRCP vol
by rounds, to have a better approximation.

Bug: 302322731
Tag: #feature
Test: atest net_test_avrcp
Change-Id: I56cd10b04669a9daba389d612b978402dcb0a80e
parent 3fc63129
Loading
Loading
Loading
Loading
+5 −8
Original line number Original line Diff line number Diff line
@@ -63,12 +63,12 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
    boolean mAbsoluteVolumeSupported = false;
    boolean mAbsoluteVolumeSupported = false;


    static int avrcpToSystemVolume(int avrcpVolume) {
    static int avrcpToSystemVolume(int avrcpVolume) {
        return (int) Math.floor((double) avrcpVolume * sDeviceMaxVolume / AVRCP_MAX_VOL);
        return (int) Math.round((double) avrcpVolume * sDeviceMaxVolume / AVRCP_MAX_VOL);
    }
    }


    static int systemToAvrcpVolume(int deviceVolume) {
    static int systemToAvrcpVolume(int deviceVolume) {
        int avrcpVolume = (int) Math.ceil((double) deviceVolume
        int avrcpVolume =
                * AVRCP_MAX_VOL / sDeviceMaxVolume);
                (int) Math.round((double) deviceVolume * AVRCP_MAX_VOL / sDeviceMaxVolume);
        if (avrcpVolume > 127) avrcpVolume = 127;
        if (avrcpVolume > 127) avrcpVolume = 127;
        return avrcpVolume;
        return avrcpVolume;
    }
    }
@@ -178,8 +178,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
    }
    }


    void setVolume(@NonNull BluetoothDevice device, int avrcpVolume) {
    void setVolume(@NonNull BluetoothDevice device, int avrcpVolume) {
        int deviceVolume =
        int deviceVolume = avrcpToSystemVolume(avrcpVolume);
                (int) Math.round((double) avrcpVolume * sDeviceMaxVolume / AVRCP_MAX_VOL);
        mVolumeEventLogger.logd(DEBUG, TAG, "setVolume:"
        mVolumeEventLogger.logd(DEBUG, TAG, "setVolume:"
                        + " device=" + device
                        + " device=" + device
                        + " avrcpVolume=" + avrcpVolume
                        + " avrcpVolume=" + avrcpVolume
@@ -196,9 +195,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
            d("sendVolumeChanged: Skipping update volume to same as current.");
            d("sendVolumeChanged: Skipping update volume to same as current.");
            return;
            return;
        }
        }
        int avrcpVolume =
        int avrcpVolume = systemToAvrcpVolume(deviceVolume);
                (int) Math.round((double) deviceVolume * AVRCP_MAX_VOL / sDeviceMaxVolume);
        if (avrcpVolume > 127) avrcpVolume = 127;
        mVolumeEventLogger.logd(DEBUG, TAG, "sendVolumeChanged:"
        mVolumeEventLogger.logd(DEBUG, TAG, "sendVolumeChanged:"
                        + " device=" + device
                        + " device=" + device
                        + " avrcpVolume=" + avrcpVolume
                        + " avrcpVolume=" + avrcpVolume
+5 −1
Original line number Original line Diff line number Diff line
@@ -75,10 +75,14 @@ public class AvrcpVolumeManagerTest {
    }
    }


    @Test
    @Test
    public void avrcpToSystemVolume() {
    public void avrcpVolumeConversion() {
        assertThat(AvrcpVolumeManager.avrcpToSystemVolume(0)).isEqualTo(0);
        assertThat(AvrcpVolumeManager.avrcpToSystemVolume(0)).isEqualTo(0);
        assertThat(AvrcpVolumeManager.avrcpToSystemVolume(AVRCP_MAX_VOL))
        assertThat(AvrcpVolumeManager.avrcpToSystemVolume(AVRCP_MAX_VOL))
                .isEqualTo(TEST_DEVICE_MAX_VOUME);
                .isEqualTo(TEST_DEVICE_MAX_VOUME);

        assertThat(AvrcpVolumeManager.systemToAvrcpVolume(0)).isEqualTo(0);
        assertThat(AvrcpVolumeManager.systemToAvrcpVolume(TEST_DEVICE_MAX_VOUME))
                .isEqualTo(AVRCP_MAX_VOL);
    }
    }


    @Test
    @Test
+5 −0
Original line number Original line Diff line number Diff line
@@ -1862,6 +1862,11 @@ void Device::DeviceDisconnected() {
  // remove these conditionals.
  // remove these conditionals.
  if (volume_interface_ != nullptr)
  if (volume_interface_ != nullptr)
    volume_interface_->DeviceDisconnected(GetAddress());
    volume_interface_->DeviceDisconnected(GetAddress());
  // The volume at connection is set by the remote device when indicating
  // that it supports absolute volume, in case it's not, we need
  // to reset the local volume var to be sure we send the correct value
  // to the remote device on the next connection.
  volume_ = VOL_NOT_SUPPORTED;
}
}


static std::string volumeToStr(int8_t volume) {
static std::string volumeToStr(int8_t volume) {
+15 −0
Original line number Original line Diff line number Diff line
@@ -1469,6 +1469,21 @@ TEST_F(AvrcpDeviceTest, setVolumeOnceTest) {
  test_device->SetVolume(vol);
  test_device->SetVolume(vol);
}
}


TEST_F(AvrcpDeviceTest, setVolumeAfterReconnectionTest) {
  int vol = 0x48;

  auto set_abs_vol = SetAbsoluteVolumeRequestBuilder::MakeBuilder(vol);

  // Ensure that SetVolume is called twice as DeviceDisconnected will
  // reset the previous stored volume.
  EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(set_abs_vol))))
      .Times(2);

  test_device->SetVolume(vol);
  test_device->DeviceDisconnected();
  test_device->SetVolume(vol);
}

TEST_F(AvrcpDeviceTest, playPushedActiveDeviceTest) {
TEST_F(AvrcpDeviceTest, playPushedActiveDeviceTest) {
  MockMediaInterface interface;
  MockMediaInterface interface;
  NiceMock<MockA2dpInterface> a2dp_interface;
  NiceMock<MockA2dpInterface> a2dp_interface;