Linux bluetooth development
 help / color / mirror / Atom feed
From: Simon Mikuda <simon.mikuda@streamunlimited.com>
To: linux-bluetooth@vger.kernel.org
Cc: Simon Mikuda <simon.mikuda@streamunlimited.com>
Subject: [PATCH BlueZ v2 8/8] player: Fix NumberOfItems of Now Playing when its content changes
Date: Mon, 23 Nov 2026 17:59:32 +0100	[thread overview]
Message-ID: <20261123165932.470612-9-simon.mikuda@streamunlimited.com> (raw)
In-Reply-To: <20261123165932.470612-1-simon.mikuda@streamunlimited.com>

NOW_PLAYING_CONTENT_CHANGED clears the Now Playing items but keeps the
number of items, which is requested again only when the scope changes,
and ListItems does not list past it. So a controller that stays in Now
Playing lists a grown list cut at its old length.

To reproduce, list the 2 items of Now Playing, let the target add 3
items, and list Now Playing again:

  btmon: EventID: 0x09 (EVENT_NOW_PLAYING_CONTENT_CHANGED)
  avrcp_now_playing_changed() NowPlaying changed
  bluetoothd: profiles/audio/player.c:get_items() 2
  btmon: AVRCP: GetFolderItems: len 0x0022
  btmon: Scope: 0x03 (Now Playing)
  btmon: StartItem: 0x00000000 (0)
  btmon: EndItem: 0x00000001 (1)
  avrcp_list_items_rsp() start 0 end 1 items 2 total 2

Only the 2 items listed before are listed again.

Reset the number of items, so that ListItems leaves the end of the list
to the target until it is known again, and request it again when Now
Playing is the current folder.

That request can still be in flight when the client changes folder or
player, and its reply would then set the number of items of the folder
current by then, of whichever player is the current one. Give each
reply the player and folder it was requested for, and drop it when that
folder is no longer the player's current one.

Fixes: 671f370a8e34 ("audio/player: Clear playlist on Now Playing Changed AVRCP event")
Assisted-by: Claude:claude-opus-5-5
---
 profiles/audio/avrcp.c   | 86 ++++++++++++++++++++++++++++++++--------
 profiles/audio/player.c  | 25 +++++++++++-
 profiles/audio/player.h  |  1 +
 unit/test-media-player.c |  8 +++-
 4 files changed, 101 insertions(+), 19 deletions(-)

diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c
index 128d7fe39..9486cb99e 100644
--- a/profiles/audio/avrcp.c
+++ b/profiles/audio/avrcp.c
@@ -252,6 +252,13 @@ struct avrcp {
 	bool pos_changed_supported;
 	uint8_t transaction;
 	struct pending_pdu *pending_pdu;
+	GSList *total_items_reqs;
+};
+
+struct total_items_req {
+	struct avrcp *session;
+	struct avrcp_player *player;
+	char *name;
 };
 
 struct passthrough_handler {
@@ -3554,50 +3561,71 @@ static int ct_add_to_nowplaying(struct media_player *mp, const char *name,
 	return 0;
 }
 
+static void total_items_req_free(void *data)
+{
+	struct total_items_req *req = data;
+
+	g_free(req->name);
+	g_free(req);
+}
+
 static gboolean avrcp_get_total_numberofitems_rsp(struct avctp *conn,
 					uint8_t *operands, size_t operand_count,
 					void *user_data)
 {
 	struct iovec iov = { operands, operand_count };
-	struct avrcp *session = user_data;
-	struct avrcp_player *player = session->controller->player;
-	struct media_player *mp = player->user_data;
+	struct total_items_req *req = user_data;
+	struct avrcp *session = req->session;
+	struct avrcp_player *player = req->player;
 	uint32_t num_of_items = 0;
 	uint16_t uid_counter;
 	uint8_t status;
 
+	session->total_items_reqs = g_slist_remove(session->total_items_reqs,
+									req);
+
+	/* The player may be gone */
+	if (!g_slist_find(session->controller->players, player))
+		goto free;
+
 	if (operands == NULL)
-		return FALSE;
+		goto free;
 
 	if (!avrcp_pull_browsing_header(&iov) ||
 			!util_iov_pull_u8(&iov, &status))
-		return FALSE;
+		goto free;
 
 	if (status == AVRCP_STATUS_OUT_OF_BOUNDS)
 		goto done;
 
 	if (status != AVRCP_STATUS_SUCCESS)
-		return FALSE;
+		goto free;
 
 	if (!util_iov_pull_be16(&iov, &uid_counter) ||
 			!util_iov_pull_be32(&iov, &num_of_items))
-		return FALSE;
+		goto free;
 
 	player->uid_counter = uid_counter;
 
 	if (!num_of_items)
-		return FALSE;
+		goto free;
 
 done:
-	media_player_total_items_complete(mp, num_of_items);
+	media_player_total_items_complete(player->user_data, req->name,
+							num_of_items);
+free:
+	total_items_req_free(req);
 	return FALSE;
 }
 
-static void avrcp_get_total_numberofitems(struct avrcp *session)
+static int avrcp_get_total_numberofitems(struct avrcp *session,
+							const char *name)
 {
 	uint8_t buf[AVRCP_BROWSING_HEADER_LENGTH + 1];
 	struct avrcp_player *player = session->controller->player;
 	struct avrcp_browsing_header *pdu = (void *) buf;
+	struct total_items_req *req;
+	int err;
 
 	memset(buf, 0, sizeof(buf));
 
@@ -3606,8 +3634,26 @@ static void avrcp_get_total_numberofitems(struct avrcp *session)
 
 	pdu->params[0] = player->scope;
 
-	avctp_send_browsing_req(session->conn, buf, sizeof(buf),
-				avrcp_get_total_numberofitems_rsp, session);
+	/*
+	 * The reply is matched to the folder it was asked for, since the
+	 * player or its folder may change while the request is in flight.
+	 */
+	req = g_new0(struct total_items_req, 1);
+	req->session = session;
+	req->player = player;
+	req->name = g_strdup(name);
+
+	err = avctp_send_browsing_req(session->conn, buf, sizeof(buf),
+				avrcp_get_total_numberofitems_rsp, req);
+	if (err < 0) {
+		total_items_req_free(req);
+		return err;
+	}
+
+	session->total_items_reqs = g_slist_prepend(session->total_items_reqs,
+									req);
+
+	return 0;
 }
 
 static int ct_get_total_numberofitems(struct media_player *mp, const char *name,
@@ -3631,9 +3677,7 @@ static int ct_get_total_numberofitems(struct media_player *mp, const char *name,
 	else
 		player->scope = 0x01;
 
-	avrcp_get_total_numberofitems(session);
-
-	return 0;
+	return avrcp_get_total_numberofitems(session, name);
 }
 
 static const struct media_player_callback ct_cbs = {
@@ -4556,6 +4600,8 @@ static void session_destroy(struct avrcp *session, int err)
 	if (session->position_timer > 0)
 		timeout_remove(session->position_timer);
 
+	g_slist_free_full(session->total_items_reqs, total_items_req_free);
+
 	if (session->controller != NULL)
 		controller_destroy(session);
 
@@ -4618,7 +4664,15 @@ static void state_changed(struct btd_device *device, avctp_state_t old_state,
 
 		break;
 	case AVCTP_STATE_CONNECTED:
-		if (session == NULL || session->control_id > 0)
+		if (session == NULL)
+			break;
+
+		/* The requests of a lost browsing channel get no reply */
+		g_slist_free_full(session->total_items_reqs,
+						total_items_req_free);
+		session->total_items_reqs = NULL;
+
+		if (session->control_id > 0)
 			break;
 
 		session_init_control(session);
diff --git a/profiles/audio/player.c b/profiles/audio/player.c
index 5f29be749..166279f91 100644
--- a/profiles/audio/player.c
+++ b/profiles/audio/player.c
@@ -758,11 +758,13 @@ done:
 }
 
 void media_player_total_items_complete(struct media_player *mp,
-							uint32_t num_of_items)
+						const char *name,
+						uint32_t num_of_items)
 {
 	struct media_folder *folder = mp->scope;
 
-	if (folder == NULL)
+	/* The scope may have changed while the request was in flight */
+	if (folder == NULL || g_strcmp0(folder->item->name, name) != 0)
 		return;
 
 	if (folder->number_of_items != num_of_items) {
@@ -2073,9 +2075,28 @@ struct media_item *media_player_set_playlist_item(struct media_player *mp,
 
 void media_player_clear_playlist(struct media_player *mp)
 {
+	struct player_callback *cb = mp->cb;
+
 	if (mp->playlist) {
 		g_slist_free_full(mp->playlist->items, media_item_destroy);
 		mp->playlist->items = NULL;
+
+		/*
+		 * The number of items may have changed: leave the end of the
+		 * list to the target until it is known again.
+		 */
+		mp->playlist->number_of_items = 0;
+	}
+
+	if (mp->playlist && mp->scope == mp->playlist) {
+		g_dbus_emit_property_changed(btd_get_dbus_connection(),
+						mp->path,
+						MEDIA_FOLDER_INTERFACE,
+						"NumberOfItems");
+
+		if (cb->cbs->total_items)
+			cb->cbs->total_items(mp, mp->scope->item->name,
+							cb->user_data);
 	}
 
 	g_dbus_emit_property_changed(btd_get_dbus_connection(), mp->path,
diff --git a/profiles/audio/player.h b/profiles/audio/player.h
index dcd7df286..5d2e0a962 100644
--- a/profiles/audio/player.h
+++ b/profiles/audio/player.h
@@ -108,6 +108,7 @@ void media_player_change_folder_complete(struct media_player *player,
 						int ret);
 void media_player_search_complete(struct media_player *mp, int ret);
 void media_player_total_items_complete(struct media_player *mp,
+						const char *name,
 						uint32_t num_of_items);
 
 void media_player_set_callbacks(struct media_player *mp,
diff --git a/unit/test-media-player.c b/unit/test-media-player.c
index ef2d5dff3..7dfee7681 100644
--- a/unit/test-media-player.c
+++ b/unit/test-media-player.c
@@ -53,6 +53,7 @@ struct context {
 	bool destroy_on_play;
 	bool check_total_items;
 	unsigned int total_items_calls;
+	char *total_items_name;
 };
 
 static struct context *context;
@@ -145,7 +146,8 @@ static gboolean complete_total_items(gpointer user_data)
 	if (context == NULL)
 		return FALSE;
 
-	media_player_total_items_complete(context->mp, TOTAL_ITEMS);
+	media_player_total_items_complete(context->mp,
+					context->total_items_name, TOTAL_ITEMS);
 
 	call_get_number_of_items(&context->pending2, number_of_items_reply);
 
@@ -165,6 +167,9 @@ static int test_total_items(struct media_player *mp, const char *name,
 
 	ctx->total_items_calls++;
 
+	g_free(ctx->total_items_name);
+	ctx->total_items_name = g_strdup(name);
+
 	if (ctx->check_total_items)
 		g_idle_add(complete_total_items, NULL);
 
@@ -240,6 +245,7 @@ static void destroy_context(void)
 	dbus_connection_unref(context->dbus_conn);
 
 	g_free(context->play_item_name);
+	g_free(context->total_items_name);
 	g_free(context);
 	context = NULL;
 }
-- 
2.43.0


  parent reply	other threads:[~2026-11-23 16:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 16:59 [PATCH BlueZ v2 0/8] AVRCP browsing fixes, and browsing and cover art in the target role Simon Mikuda
2026-11-23 16:59 ` [PATCH BlueZ v2 1/8] avrcp: Fix not checking browsing PDU parameter length Simon Mikuda
2026-11-24  2:07   ` AVRCP browsing fixes, and browsing and cover art in the target role bluez.test.bot
2026-11-23 16:59 ` [PATCH BlueZ v2 2/8] avrcp: Fix listing media players up to item 0xffffffff Simon Mikuda
2026-11-23 16:59 ` [PATCH BlueZ v2 3/8] avrcp: Fix byte order of ADDRESSED_PLAYER_CHANGED notification Simon Mikuda
2026-11-23 16:59 ` [PATCH BlueZ v2 4/8] avrcp: Fix byte order of TRACK_CHANGED identifier Simon Mikuda
2026-11-23 16:59 ` [PATCH BlueZ v2 5/8] avrcp: Fix not browsing a player addressed before it was listed Simon Mikuda
2026-11-23 16:59 ` [PATCH BlueZ v2 6/8] player: Fix crash on Search in a folder with a space in its name Simon Mikuda
2026-11-23 16:59 ` [PATCH BlueZ v2 7/8] player: Fix missing Name of listed Now Playing items Simon Mikuda
2026-11-23 16:59 ` Simon Mikuda [this message]
2026-11-23 17:42 ` [PATCH BlueZ v2 0/8] AVRCP browsing fixes, and browsing and cover art in the target role Luiz Augusto von Dentz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123165932.470612-9-simon.mikuda@streamunlimited.com \
    --to=simon.mikuda@streamunlimited.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox