transport: Fix not being able to initialize volume properly
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Wed, 27 Apr 2022 20:14:19 +0000 (13:14 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:54 +0000 (14:55 +0530)
In case AVRCP is connected first and
media_transport_update_device_volume is called without any media_player
being available the volume setting would be lost and Transport.Volume
won't be available, so this introduces btd_device_{set,get}_volume
helpers which is used to store the volume temporarely so
media_player_get_device_volume is able to restore it when the transport
is created.

Fixes: https://github.com/bluez/bluez/issues/335
Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
profiles/audio/media.c
profiles/audio/transport.c
src/device.c
src/device.h

index 16759b5..cbd7ffc 100644 (file)
@@ -647,11 +647,11 @@ int8_t media_player_get_device_volume(struct btd_device *device)
 
        target_player = avrcp_get_target_player_by_device(device);
        if (!target_player)
-               return -1;
+               goto done;
 
        adapter = find_adapter(device);
        if (!adapter)
-               return -1;
+               goto done;
 
        for (l = adapter->players; l; l = l->next) {
                struct media_player *mp = l->data;
@@ -660,7 +660,9 @@ int8_t media_player_get_device_volume(struct btd_device *device)
                        return mp->volume;
        }
 
-       return -1;
+done:
+       /* If media_player doesn't exists use device_volume */
+       return btd_device_get_volume(device);
 }
 
 static gboolean set_configuration(struct media_endpoint *endpoint,
index 2b05236..4f1e703 100644 (file)
@@ -1089,6 +1089,7 @@ int8_t media_transport_get_device_volume(struct btd_device *dev)
        if (dev == NULL)
                return -1;
 
+       /* Attempt to locate the transport to get its volume */
        for (l = transports; l; l = l->next) {
                struct media_transport *transport = l->data;
                if (transport->device != dev)
@@ -1099,7 +1100,8 @@ int8_t media_transport_get_device_volume(struct btd_device *dev)
                        return media_transport_get_volume(transport);
        }
 
-       return 0;
+       /* If transport volume doesn't exists use device_volume */
+       return btd_device_get_volume(dev);
 }
 
 void media_transport_update_device_volume(struct btd_device *dev,
@@ -1110,15 +1112,21 @@ void media_transport_update_device_volume(struct btd_device *dev,
        if (dev == NULL || volume < 0)
                return;
 
+       /* Attempt to locate the transport to set its volume */
        for (l = transports; l; l = l->next) {
                struct media_transport *transport = l->data;
                if (transport->device != dev)
                        continue;
 
                /* Volume is A2DP only */
-               if (media_endpoint_get_sep(transport->endpoint))
+               if (media_endpoint_get_sep(transport->endpoint)) {
                        media_transport_update_volume(transport, volume);
+                       return;
+               }
        }
+
+       /* If transport volume doesn't exists add to device_volume */
+       btd_device_set_volume(dev, volume);
 }
 
 #if defined(TIZEN_FEATURE_BLUEZ_MODIFY) && defined(TIZEN_FEATURE_BLUEZ_A2DP_MULTISTREAM)
index fcd19bd..60ff675 100644 (file)
@@ -321,6 +321,8 @@ struct btd_device {
        guint           store_id;
 
        time_t          name_resolve_failed_time;
+
+       int8_t          volume;
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
        bool    legacy_pairing;
        char            *manufacturer_data;
@@ -6896,6 +6898,7 @@ static struct btd_device *device_new(struct btd_adapter *adapter,
                return NULL;
 
        device->tx_power = 127;
+       device->volume = -1;
 
        device->db = gatt_db_new();
        if (!device->db) {
@@ -10774,6 +10777,16 @@ void btd_device_cleanup(void)
        btd_service_remove_state_cb(service_state_cb_id);
 }
 
+void btd_device_set_volume(struct btd_device *device, int8_t volume)
+{
+       device->volume = volume;
+}
+
+int8_t btd_device_get_volume(struct btd_device *device)
+{
+       return device->volume;
+}
+
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
 void btd_device_set_legacy_pairing(struct btd_device *dev, bool legacy_pairing)
 {
index 7f97ac4..f8074da 100644 (file)
@@ -260,3 +260,6 @@ bool btd_device_all_services_allowed(struct btd_device *dev);
 void btd_device_update_allowed_services(struct btd_device *dev);
 void btd_device_init(void);
 void btd_device_cleanup(void);
+
+void btd_device_set_volume(struct btd_device *dev, int8_t volume);
+int8_t btd_device_get_volume(struct btd_device *dev);