player: Fix Track being emitted with empty metadata
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Tue, 15 Feb 2022 20:46:47 +0000 (12:46 -0800)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:53 +0000 (14:55 +0530)
This sometimes causes the Track to be schedule while some metadata are
still pending, also don't remove the Duration from track when updating
its metadata since Duration is typically updated by player status rather
than metadata.

Fixes: https://github.com/bluez/bluez/issues/291
Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
profiles/audio/avrcp.c
profiles/audio/player.c
profiles/audio/player.h

index 8a26377..e41fe76 100644 (file)
@@ -2596,6 +2596,8 @@ static void avrcp_parse_attribute_list(struct avrcp_player *player,
        struct media_item *item;
        int i;
 
+       media_player_clear_metadata(mp);
+
        item = media_player_set_playlist_item(mp, player->uid);
 
        for (i = 0; count > 0; count--) {
@@ -2622,6 +2624,8 @@ static void avrcp_parse_attribute_list(struct avrcp_player *player,
 
                i += len;
        }
+
+       media_player_metadata_changed(mp);
 }
 
 static gboolean avrcp_get_element_attributes_rsp(struct avctp *conn,
index 9e5eb59..0357d1e 100755 (executable)
@@ -84,7 +84,6 @@ struct media_player {
        char                    *status;
        uint32_t                position;
        GTimer                  *progress;
-       guint                   process_id;
        struct player_callback  *cb;
        GSList                  *pending;
        GSList                  *folders;
@@ -1357,9 +1356,6 @@ void media_player_destroy(struct media_player *mp)
        if (mp->settings)
                g_hash_table_unref(mp->settings);
 
-       if (mp->process_id > 0)
-               g_source_remove(mp->process_id);
-
        if (mp->scope)
                g_dbus_unregister_interface(btd_get_dbus_connection(),
                                                mp->path,
@@ -1432,9 +1428,10 @@ void media_player_set_duration(struct media_player *mp, uint32_t duration)
 
        g_hash_table_replace(mp->track, g_strdup("Duration"), value);
 
-       g_dbus_emit_property_changed(btd_get_dbus_connection(),
+       g_dbus_emit_property_changed_full(btd_get_dbus_connection(),
                                        mp->path, MEDIA_PLAYER_INTERFACE,
-                                       "Track");
+                                       "Track",
+                                       G_DBUS_PROPERTY_CHANGED_FLAG_FLUSH);
 }
 
 void media_player_set_position(struct media_player *mp, uint32_t position)
@@ -1519,26 +1516,20 @@ void media_player_set_status(struct media_player *mp, const char *status)
        g_timer_start(mp->progress);
 }
 
-static gboolean process_metadata_changed(void *user_data)
+static gboolean remove_metadata(void *key, void *value, void *user_data)
 {
-       struct media_player *mp = user_data;
-       const char *item;
-
-       mp->process_id = 0;
-
-       g_dbus_emit_property_changed(btd_get_dbus_connection(),
-                                       mp->path, MEDIA_PLAYER_INTERFACE,
-                                       "Track");
-
-       item = g_hash_table_lookup(mp->track, "Item");
-       if (item == NULL)
+       if (!strcmp(key, "Duration"))
                return FALSE;
 
-       g_dbus_emit_property_changed(btd_get_dbus_connection(),
-                                       item, MEDIA_ITEM_INTERFACE,
-                                       "Metadata");
+       return strcmp(key, "Item") ? TRUE : FALSE;
+}
 
-       return FALSE;
+void media_player_clear_metadata(struct media_player *mp)
+{
+       if (!mp)
+               return;
+
+       g_hash_table_foreach_remove(mp->track, remove_metadata, NULL);
 }
 
 void media_player_set_metadata(struct media_player *mp,
@@ -1576,14 +1567,32 @@ void media_player_set_metadata(struct media_player *mp,
                return;
        }
 #endif
-       if (mp->process_id == 0) {
-               g_hash_table_remove_all(mp->track);
-               mp->process_id = g_idle_add(process_metadata_changed, mp);
-       }
 
        g_hash_table_replace(mp->track, g_strdup(key), value);
 }
 
+void media_player_metadata_changed(struct media_player *mp)
+{
+       char *item;
+
+       if (!mp)
+               return;
+
+       g_dbus_emit_property_changed_full(btd_get_dbus_connection(),
+                                       mp->path, MEDIA_PLAYER_INTERFACE,
+                                       "Track",
+                                       G_DBUS_PROPERTY_CHANGED_FLAG_FLUSH);
+
+       item = g_hash_table_lookup(mp->track, "Item");
+       if (item == NULL)
+               return;
+
+       g_dbus_emit_property_changed_full(btd_get_dbus_connection(),
+                                       item, MEDIA_ITEM_INTERFACE,
+                                       "Metadata",
+                                       G_DBUS_PROPERTY_CHANGED_FLAG_FLUSH);
+}
+
 void media_player_set_type(struct media_player *mp, const char *type)
 {
        if (g_strcmp0(mp->type, type) == 0)
@@ -2123,6 +2132,7 @@ struct media_item *media_player_set_playlist_item(struct media_player *mp,
 {
        struct media_folder *folder = mp->playlist;
        struct media_item *item;
+       char *path;
 
        DBG("%" PRIu64 "", uid);
 
@@ -2141,16 +2151,11 @@ struct media_item *media_player_set_playlist_item(struct media_player *mp,
                mp->track = g_hash_table_ref(item->metadata);
        }
 
-       if (item == g_hash_table_lookup(mp->track, "Item"))
+       path = g_hash_table_lookup(mp->track, "Item");
+       if (path && !strcmp(path, item->path))
                return item;
 
-       if (mp->process_id == 0) {
-               g_hash_table_remove_all(mp->track);
-               mp->process_id = g_idle_add(process_metadata_changed, mp);
-       }
-
-       g_hash_table_replace(mp->track, g_strdup("Item"),
-                                               g_strdup(item->path));
+       g_hash_table_replace(mp->track, g_strdup("Item"), g_strdup(item->path));
 
        return item;
 }
index eab57a5..af92d54 100755 (executable)
@@ -79,9 +79,11 @@ void media_player_set_setting(struct media_player *mp, const char *key,
                                                        const char *value);
 const char *media_player_get_status(struct media_player *mp);
 void media_player_set_status(struct media_player *mp, const char *status);
+void media_player_clear_metadata(struct media_player *mp);
 void media_player_set_metadata(struct media_player *mp,
                                struct media_item *item, const char *key,
                                void *data, size_t len);
+void media_player_metadata_changed(struct media_player *mp);
 void media_player_set_type(struct media_player *mp, const char *type);
 void media_player_set_subtype(struct media_player *mp, const char *subtype);
 void media_player_set_name(struct media_player *mp, const char *name);