Skip to contentSkip to conversation

AVRCP browsing fixes

PATCH In review Simon Mikuda 2 participants 12 messages Nov 24 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

v4 cover AVRCP browsing fixes

These fix AVRCP browsing, mostly in the controller role: the browsing PDU length is not checked, the player list stops short of item 0xffffffff, two notifications go out in the wrong byte order, a player addressed before it was listed is never browsed, Search in a folder whose name has a space crashes bluetoothd, and listed Now Playing items lose their Name and NumberOfItems. Each patch has its steps to reproduce.

Changes in v4:
- Patches 6 and 9: shorter subjects, their titles were over CI's gitlint limit.

Simon Mikuda (10):
avrcp: Fix not checking browsing PDU parameter length
avrcp: Fix listing media players up to item 0xffffffff
avrcp: Fix byte order of ADDRESSED_PLAYER_CHANGED notification
avrcp: Fix byte order of TRACK_CHANGED identifier
avrcp: Fix not browsing a player addressed before it was listed
player: Fix crash on Search in a folder whose name has a space
player: Fix missing Name of listed Now Playing items
avrcp: Fix NumberOfItems set from a reply for another folder unit/test-media-player: Add test for stale NumberOfItems reply
player: Fix Now Playing NumberOfItems when its content changes

 profiles/audio/avrcp.c   | 110 +++++++++++++++++++++++++++++----------
 profiles/audio/player.c  |  65 ++++++++++++++++++++---
 profiles/audio/player.h  |   1 +
 unit/test-media-player.c |  40 ++++++++++++--
 4 files changed, 179 insertions(+), 37 deletions(-)

base-commit: 1608f836188610aabeff5a3164169ebe0360e443

Signature · 1 line
2.43.0
#1 lore raw

PATCH v4 1/10 avrcp: Fix not checking browsing PDU parameter length

The parameter length of a browsing PDU is not checked against the number of bytes received, so a handler takes the parameters missing from a short PDU from whatever the buffer held before.

To reproduce, send to the target a GetFolderItems with a parameter length of 10 but no parameters. It is answered from the zeroed buffer as a request for the media players 0 to 0:

btmon: AVRCP: GetFolderItems: len 0x000a handle_browsing_pdu() AVRCP Browsing PDU 0x71, len 0x000A avrcp_handle_get_folder_items() scope 0x00 start_item 0x00000000

      end_item 0x00000000

btmon: AVRCP: GetFolderItems: len 0x002c
btmon: Status: 0x04 (Success)
btmon: NumOfItems: 0x0001 (1)
btmon: Name: Player A

Reject a PDU whose parameter length does not match, as the control channel does since commit e2b0f0d8d63e ("avrcp: Fix not checking if params_len match number of received bytes").

Fixes 731aec9cae63 ("AVRCP: Add handler for browsing PDUs")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/avrcp.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
profiles/audio/avrcp.c +11 -0
index 2f214e982..95b1d8c47 100644
@@ -2164,9 +2164,20 @@ static size_t handle_browsing_pdu(struct avctp *conn,
 	const struct browsing_pdu_handler *handler;
 	struct avrcp_browsing_header *pdu = (void *) operands;
 
+	if (operand_count < AVRCP_BROWSING_HEADER_LENGTH)
+		return avrcp_browsing_general_reject(operands);
+
 	DBG("AVRCP Browsing PDU 0x%02X, len 0x%04X", pdu->pdu_id,
 						be16_to_cpu(pdu->param_len));
 
+	if (be16_to_cpu(pdu->param_len) !=
+			operand_count - AVRCP_BROWSING_HEADER_LENGTH) {
+		DBG("AVRCP Browsing PDU parameters length don't match");
+		pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
+		pdu->param_len = cpu_to_be16(1);
+		return AVRCP_BROWSING_HEADER_LENGTH + 1;
+	}
+
 	for (handler = browsing_handlers; handler->pdu_id; handler++) {
 		if (handler->pdu_id == pdu->pdu_id)
 			goto done;
Signature · 1 line
2.43.0
#2 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1220797/

---Test result---

Test Summary:

CheckPatch                    PASS      5.04 seconds
GitLint                       PASS      3.41 seconds
BuildEll                      PASS      20.69 seconds
BluezMake                     PASS      315.69 seconds
MakeCheck                     PASS      14.55 seconds
MakeDistcheck                 PASS      147.44 seconds
CheckValgrind                 PASS      351.41 seconds
CheckSmatch                   PASS      317.95 seconds
bluezmakeextell               PASS      100.89 seconds
TestFunctional                PASS      1228.97 seconds
IncrementalBuild              PASS      349.10 seconds
ScanBuild                     PASS      1196.19 seconds

https://github.com/bluez/bluez/pull/2899

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH v4 2/10 avrcp: Fix listing media players up to item 0xffffffff

In reply to Simon Mikuda

GetFolderItems for the media player list stops once it has end_item - start_item + 1 items, which overflows to 0 for a request from 0 to 0xffffffff, so the list stops before the first player and is answered with Range Out of Bounds. That is the request the BlueZ controller sends, so it never learns the target's players.

To reproduce, register a media player on the target and connect AVRCP from a BlueZ controller, whose player never gets a Name:

btmon: AVRCP: GetFolderItems: len 0x000a
btmon: Scope: 0x00 (Media Player List)
btmon: StartItem: 0x00000000 (0)
btmon: EndItem: 0xffffffff (4294967295) avrcp_handle_get_folder_items() scope 0x00 start_item 0x00000000

      end_item 0xffffffff

btmon: AVRCP: GetFolderItems: len 0x0001
btmon: Status: 0x0b (Range Out of Bounds)

Fixes d6bb0111b569 ("audio/avrcp: Add GetFolderItems support")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/avrcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
profiles/audio/avrcp.c +1 -1
index 95b1d8c47..3ab7b6d7c 100644
@@ -1997,7 +1997,7 @@ static void avrcp_handle_media_player_list(struct avrcp *session,
 		struct player_item *item;
 		uint16_t namelen;
 
-		if (rsp->num_items == (end_item - start_item) + 1)
+		if (rsp->num_items > end_item - start_item)
 			break;
 
 		folder = (void *)&pdu->params[pdu->param_len];
Signature · 1 line
2.43.0
#4 lore raw

PATCH v4 3/10 avrcp: Fix byte order of ADDRESSED_PLAYER_CHANGED notification

In reply to Simon Mikuda

The Player Id and UID Counter of the CHANGED response were copied in host byte order, so on a little endian host player 1 is announced as player 256. The interim response already sends them in big endian.

To reproduce, register for EVENT_ADDRESSED_PLAYER_CHANGED and send SetAddressedPlayer for player 1:

AV/C: Interim AVRCP: RegisterNotification EventID: 0x0b
(EVENT_ADDRESSED_PLAYER_CHANGED)
btmon: PlayerID: 0x0001 (1)
btmon: AV/C: Control AVRCP: SetAddressedPlayer pt Single len 0x0002
btmon: PlayerID: 0x0001 (1)
btmon: AV/C: Changed AVRCP: RegisterNotification pt Single len 0x0005
btmon: EventID: 0x0b (EVENT_ADDRESSED_PLAYER_CHANGED)
btmon: PlayerID: 0x0100 (256)

Fixes 4827d6b61fd4 ("audio/avrcp: Add Set Addressed Player support")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/avrcp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
profiles/audio/avrcp.c +2 -2
index 3ab7b6d7c..aff1c381a 100644
@@ -803,8 +803,8 @@ void avrcp_player_event(struct avrcp_player *player, uint8_t id,
 		break;
 	case AVRCP_EVENT_ADDRESSED_PLAYER_CHANGED:
 		size = 5;
-		memcpy(&pdu->params[1], &player->id, sizeof(uint16_t));
-		memcpy(&pdu->params[3], &player->uid_counter, sizeof(uint16_t));
+		put_be16(player->id, &pdu->params[1]);
+		put_be16(player->uid_counter, &pdu->params[3]);
 		break;
 	case AVRCP_EVENT_AVAILABLE_PLAYERS_CHANGED:
 		size = 1;
Signature · 1 line
2.43.0
#5 lore raw

PATCH v4 4/10 avrcp: Fix byte order of TRACK_CHANGED identifier

In reply to Simon Mikuda

The Identifier of TRACK_CHANGED, in the interim and the changed response, was copied in host byte order. The identifiers sent so far, 0 and UINT64_MAX, read the same in either byte order, but any other one, such as the UID of an item of a browsable player, is swapped on a little endian host, and the controller then asks for an item that does not exist.

To reproduce, with a target that reports the UID of the current track, as "avrcp: Add browsing support in the target role" later in this series does, change the track to the item of UID 2:

btmon: EventID: 0x02 (EVENT_TRACK_CHANGED)
btmon: Identifier: 0x 200000000000000 (144115188075855872)
btmon: AVRCP: GetItemAttributes: len 0x000c
btmon: UID: 0x0200000000000000 (144115188075855872)
btmon: AVRCP: GetItemAttributes: len 0x0001
btmon: Status: 0x09 (Does Not Exist)

Fixes 9ab125a8ec20 ("AVRCP: Use track's UID in event notification")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/avrcp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
profiles/audio/avrcp.c +2 -2
index aff1c381a..fbd212511 100644
@@ -791,7 +791,7 @@ void avrcp_player_event(struct avrcp_player *player, uint8_t id,
 		break;
 	case AVRCP_EVENT_TRACK_CHANGED:
 		size = 9;
-		memcpy(&pdu->params[1], data, sizeof(uint64_t));
+		put_be64(*(const uint64_t *) data, &pdu->params[1]);
 
 		break;
 	case AVRCP_EVENT_TRACK_REACHED_END:
@@ -1629,7 +1629,7 @@ static uint8_t avrcp_handle_register_notification(struct avrcp *session,
 	case AVRCP_EVENT_TRACK_CHANGED:
 		len = 9;
 		uid = player_get_uid(player);
-		memcpy(&pdu->params[1], &uid, sizeof(uint64_t));
+		put_be64(uid, &pdu->params[1]);
 
 		break;
 	case AVRCP_EVENT_TRACK_REACHED_END:
Signature · 1 line
2.43.0
#6 lore raw

PATCH v4 5/10 avrcp: Fix not browsing a player addressed before it was listed

In reply to Simon Mikuda

SetBrowsedPlayer is sent for a browsable player from the player list only if the player is addressed. But ADDRESSED_PLAYER_CHANGED does not mark the player addressed when its id is already the id of the current
player: the player 0 of a target that has no player at connection, or a player whose id was taken from the player list. Neither that notification nor the SetAddressedPlayer response sends SetBrowsedPlayer, so the player never gets the MediaFolder1 interface.

To reproduce, connect AVRCP to a target without a player, then register a browsable player on the target. The controller gets the player from the list as browsable, but never browses it:

btmon: EventID: 0x0a (EVENT_AVAILABLE_PLAYERS_CHANGED)
btmon: AVRCP: GetFolderItems: len 0x000a
btmon: Scope: 0x00 (Media Player List)
btmon: AVRCP: GetFolderItems: len 0x003a
btmon: PlayerID: 0x0001 (1)
btmon: Browsing
btmon: Name: BlueZ Browsable Player
bluetoothd: profiles/audio/player.c:media_player_set_browsable() true media_player_set_name() BlueZ Browsable Player

and no SetBrowsedPlayer follows.

Mark the player addressed also in that case, and send SetBrowsedPlayer once the player is addressed. As SetBrowsedPlayer may then be sent again while the one sent with SetAddressedPlayer is in flight, mark the player browsed when the request is sent, and clear it if it fails.

Fixes 39a62aaa29b8 ("audio/avrcp: Don't send SetBrowsedPlayer if player is not browsable")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/avrcp.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
profiles/audio/avrcp.c +15 -9
index fbd212511..f65d394e8 100644
@@ -344,6 +344,8 @@ static const uint32_t company_ids[] = {
 static void avrcp_register_notification(struct avrcp *session, uint8_t event);
 static GList *player_list_settings(struct avrcp_player *player);
 static void avrcp_get_play_status(struct avrcp *session);
+static void set_browsed_player(struct avrcp *session,
+					struct avrcp_player *player);
 
 static void avrcp_browsing_record(sdp_record_t *record, sdp_data_t *version)
 {
@@ -2883,20 +2885,18 @@ static gboolean avrcp_set_browsed_player_rsp(struct avctp *conn,
 	uint16_t uid_counter, charset;
 	uint8_t status, depth, count;
 
-	if (operands == NULL)
-		return FALSE;
-
-	if (!avrcp_pull_browsing_header(&iov) ||
+	if (!operands || !avrcp_pull_browsing_header(&iov) ||
 			!util_iov_pull_u8(&iov, &status) ||
 			status != AVRCP_STATUS_SUCCESS ||
 			!util_iov_pull_be16(&iov, &uid_counter) ||
 			!util_iov_pull_be32(&iov, &items) ||
 			!util_iov_pull_be16(&iov, &charset) ||
-			!util_iov_pull_u8(&iov, &depth))
+			!util_iov_pull_u8(&iov, &depth)) {
+		player->browsed = false;
 		return FALSE;
+	}
 
 	player->uid_counter = uid_counter;
-	player->browsed = true;
 
 	folders = g_new0(char *, depth + 2);
 	folders[0] = g_strdup("/Filesystem");
@@ -2942,8 +2942,10 @@ static void avrcp_set_browsed_player(struct avrcp *session,
 	memcpy(pdu->params, &id, 2);
 	pdu->param_len = cpu_to_be16(2);
 
-	avctp_send_browsing_req(session->conn, buf, sizeof(buf),
-				avrcp_set_browsed_player_rsp, session);
+	/* Set while the request is in flight, so that it is sent once */
+	if (!avctp_send_browsing_req(session->conn, buf, sizeof(buf),
+				avrcp_set_browsed_player_rsp, session))
+		player->browsed = true;
 }
 
 static gboolean avrcp_get_item_attributes_rsp(struct avctp *conn,
@@ -3067,6 +3069,7 @@ static gboolean avrcp_set_addressed_player_rsp(struct avctp *conn, uint8_t code,
 		return FALSE;
 
 	player->addressed = true;
+	set_browsed_player(session, player);
 
 	return FALSE;
 }
@@ -3990,8 +3993,11 @@ static void avrcp_addressed_player_changed(struct avrcp *session,
 	struct avrcp_player *player = session->controller->player;
 	uint16_t id = get_be16(&pdu->params[1]);
 
-	if (player != NULL && player->id == id)
+	if (player != NULL && player->id == id) {
+		player->addressed = true;
+		set_browsed_player(session, player);
 		return;
+	}
 
 	player = find_ct_player(session, id);
 	if (player == NULL) {
Signature · 1 line
2.43.0
#7 lore raw

PATCH v4 6/10 player: Fix crash on Search in a folder whose name has a space

In reply to Simon Mikuda

The search folder object path is made from the name of the folder searched, e.g. /Filesystem/My Music/search. A name with characters an object path cannot have, such as a space, fails to register, and media_player_search_complete() dereferences the NULL item.

To reproduce, browse a target with a folder named "My Music", change to it and call Search:

media_player_create_subfolder() /Filesystem/My Music/search media_folder_create_item() D-Bus failed to register

      org.bluez.MediaItem1 on
      /org/bluez/hci0/dev_00_AA_01_01_00_42/avrcp/player0/Filesystem/My
      Music/search path
  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  dbus_message_iter_append_basic () from libdbus-1.so.3
  #1  dbus_message_append_args_valist () from libdbus-1.so.3
  #2  g_dbus_create_reply_valist (...) at gdbus/object.c:1572
  #3  g_dbus_create_reply (...) at gdbus/object.c:1587
  #4  media_player_search_complete (mp=..., ret=2) at
      profiles/audio/player.c:741

#5 avrcp_search_rsp (...) at profiles/audio/avrcp.c:4460 #6 browsing_response (...) at profiles/audio/avctp.c:964

Replace those characters with '_' in the object path of a folder named by its path, as the search folder and the folders of the browsed player are, and fail the search if the folder still cannot be created.

Fixes 0a232a434d4b ("audio/player: Add implementation of MediaFolder.Search")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/player.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
profiles/audio/player.c +17 -3
index 4c94dcab4..bfc51ac4b 100644
@@ -730,8 +730,17 @@ void media_player_search_complete(struct media_player *mp, int ret)
 	}
 
 	if (search == NULL) {
+		struct media_item *item;
+
+		item = media_player_create_subfolder(mp, "search", 0);
+		if (item == NULL) {
+			reply = btd_error_failed(mp->msg,
+					"Unable to create search folder");
+			goto done;
+		}
+
 		search = g_new0(struct media_folder, 1);
-		search->item = media_player_create_subfolder(mp, "search", 0);
+		search->item = item;
 		mp->search = search;
 		mp->folders = g_slist_prepend(mp->folders, search);
 	}
@@ -1890,11 +1899,16 @@ static struct media_item *media_folder_create_item(struct media_player *mp,
 	item->player = mp;
 	item->uid = uid;
 
-	if (!uid && name[0] == '/')
+	if (!uid && name[0] == '/') {
 		item->path = g_strdup_printf("%s%s", mp->path, name);
-	else
+
+		/* Replace what a D-Bus object path cannot have with '_' */
+		g_strcanon(item->path + strlen(mp->path), G_CSET_A_2_Z
+					G_CSET_a_2_z G_CSET_DIGITS "_/", '_');
+	} else {
 		item->path = g_strdup_printf("%s/item%" PRIu64 "",
 						folder->item->path, uid);
+	}
 
 	item->name = g_strdup(name);
 	item->type = type;
Signature · 1 line
2.43.0
#8 lore raw

PATCH v4 7/10 player: Fix missing Name of listed Now Playing items

In reply to Simon Mikuda

A track change creates the Now Playing item of the new track by its UID alone, without a name. When the folder is listed afterwards, media_folder_create_item() finds that item by its UID and returns it as it is, so the current track, and any track played before, are listed without a Name.

To reproduce, browse a target with a Now Playing list, change the track to one of its items, and list Now Playing:

btmon: AVRCP: GetItemAttributes: len 0x000c
btmon: Scope: 0x03 (Now Playing)
btmon: UID: 0x0000000000000002 (2)
bluetoothd: profiles/audio/player.c:media_player_set_playlist_item() 2 media_folder_create_item() (null) type audio uid 2
...
btmon: AVRCP: GetFolderItems: len 0x00e5
btmon: Name: Blue Morning
btmon: Name: Green Afternoon
media_folder_create_item() Blue Morning type audio uid 1 avrcp_list_items_rsp() start 0 end 1 items 2 total 2

Item 2, "Green Afternoon", the current track, is listed without a Name.

Set the name of an existing item when it is listed, and report its Metadata as changed too when it has none, since the name is then its Title.

Fixes b18723a18e8f ("audio/player: Create item on media_player_set_playlist_item")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/player.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
profiles/audio/player.c +22 -1
index bfc51ac4b..07f4d4f78 100644
@@ -1876,6 +1876,24 @@ void media_item_set_playable(struct media_item *item, bool value)
 					MEDIA_ITEM_INTERFACE, "Playable");
 }
 
+static void media_item_set_name(struct media_item *item, const char *name)
+{
+	if (name == NULL || g_strcmp0(item->name, name) == 0)
+		return;
+
+	g_free(item->name);
+	item->name = g_strdup(name);
+
+	g_dbus_emit_property_changed(btd_get_dbus_connection(), item->path,
+					MEDIA_ITEM_INTERFACE, "Name");
+
+	/* Without metadata, get_metadata() reports the name as Title */
+	if (item->metadata && g_hash_table_size(item->metadata) == 0)
+		g_dbus_emit_property_changed(btd_get_dbus_connection(),
+					item->path, MEDIA_ITEM_INTERFACE,
+					"Metadata");
+}
+
 static struct media_item *media_folder_create_item(struct media_player *mp,
 						struct media_folder *folder,
 						const char *name,
@@ -1886,8 +1904,11 @@ static struct media_item *media_folder_create_item(struct media_player *mp,
 	const char *strtype;
 
 	item = media_folder_find_item(folder, uid);
-	if (item != NULL)
+	if (item != NULL) {
+		/* media_player_set_playlist_item() creates it unnamed */
+		media_item_set_name(item, name);
 		return item;
+	}
 
 	strtype = type_to_string(type);
 	if (strtype == NULL)
Signature · 1 line
2.43.0
#9 lore raw

PATCH v4 8/10 avrcp: Fix NumberOfItems set from a reply for another folder

In reply to Simon Mikuda

The GetTotalNumberOfItems reply does not say which folder it counts, and the client may change folder or player before it arrives. The reply then sets NumberOfItems of whichever folder is current by then.

To reproduce, change folder to /Filesystem, to another folder and back to /Filesystem before the replies arrive: the reply for the other folder sets NumberOfItems of /Filesystem.

Keep the player and directory path each request was sent for, and drop the reply when that directory is no longer the player's current folder.

Fixes f17d3a2c3b0d ("audio/avrcp: Add support for GetTotalNumberOfItems")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/avrcp.c   | 65 +++++++++++++++++++++++++++++++---------
 profiles/audio/player.c  |  6 ++--
 profiles/audio/player.h  |  1 +
 unit/test-media-player.c |  9 ++++--
 4 files changed, 62 insertions(+), 19 deletions(-)
profiles/audio/avrcp.c +51 -14
index f65d394e8..796ab917e 100644
@@ -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 *dir_path;
 };
 
 struct passthrough_handler {
@@ -3554,50 +3561,66 @@ 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->dir_path);
+	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;
 
-	if (operands == NULL)
-		return FALSE;
+	session->total_items_reqs = g_slist_remove(session->total_items_reqs,
+									req);
+
+	if (!operands || !g_slist_find(session->controller->players, player))
+		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->dir_path,
+							num_of_items);
+free:
+	total_items_req_free(req);
 	return FALSE;
 }
 
-static void avrcp_get_total_numberofitems(struct avrcp *session)
+static void avrcp_get_total_numberofitems(struct avrcp *session,
+							const char *dir_path)
 {
 	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;
 
 	memset(buf, 0, sizeof(buf));
 
@@ -3606,8 +3629,20 @@ 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 player and its folder may change before the reply */
+	req = g_new0(struct total_items_req, 1);
+	req->session = session;
+	req->player = player;
+	req->dir_path = g_strdup(dir_path);
+
+	if (avctp_send_browsing_req(session->conn, buf, sizeof(buf),
+				avrcp_get_total_numberofitems_rsp, req) < 0) {
+		total_items_req_free(req);
+		return;
+	}
+
+	session->total_items_reqs = g_slist_prepend(session->total_items_reqs,
+									req);
 }
 
 static int ct_get_total_numberofitems(struct media_player *mp, const char *name,
@@ -3631,7 +3666,7 @@ static int ct_get_total_numberofitems(struct media_player *mp, const char *name,
 	else
 		player->scope = 0x01;
 
-	avrcp_get_total_numberofitems(session);
+	avrcp_get_total_numberofitems(session, name);
 
 	return 0;
 }
@@ -4552,6 +4587,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);
 
profiles/audio/player.c +4 -2
index 07f4d4f78..9802e52bb 100644
@@ -758,11 +758,13 @@ done:
 }
 
 void media_player_total_items_complete(struct media_player *mp,
-							uint32_t num_of_items)
+						const char *dir_path,
+						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, dir_path) != 0)
 		return;
 
 	if (folder->number_of_items != num_of_items) {
profiles/audio/player.h +1 -0
index dcd7df286..ee8c40a67 100644
@@ -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 *dir_path,
 						uint32_t num_of_items);
 
 void media_player_set_callbacks(struct media_player *mp,
unit/test-media-player.c +6 -3
index ef2d5dff3..ba10e5175 100644
@@ -142,10 +142,12 @@ static void number_of_items_reply(DBusPendingCall *call, void *user_data);
 
 static gboolean complete_total_items(gpointer user_data)
 {
+	const char *dir_path = user_data;
+
 	if (context == NULL)
 		return FALSE;
 
-	media_player_total_items_complete(context->mp, TOTAL_ITEMS);
+	media_player_total_items_complete(context->mp, dir_path, TOTAL_ITEMS);
 
 	call_get_number_of_items(&context->pending2, number_of_items_reply);
 
@@ -158,7 +160,7 @@ static int test_search(struct media_player *mp, const char *string,
 	return 0;
 }
 
-static int test_total_items(struct media_player *mp, const char *name,
+static int test_total_items(struct media_player *mp, const char *dir_path,
 							void *user_data)
 {
 	struct context *ctx = user_data;
@@ -166,7 +168,8 @@ static int test_total_items(struct media_player *mp, const char *name,
 	ctx->total_items_calls++;
 
 	if (ctx->check_total_items)
-		g_idle_add(complete_total_items, NULL);
+		g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, complete_total_items,
+						g_strdup(dir_path), g_free);
 
 	return 0;
 }
Signature · 1 line
2.43.0
#10 lore raw

PATCH v4 9/10 unit/test-media-player: Add test for stale NumberOfItems reply

In reply to Simon Mikuda

The number of items of a folder may be reported after the client has moved to another folder. Check that it does not replace the number of items of the current folder, while the current folder's own is still taken.

Assisted-by Claude:claude-opus-5-5
---
 unit/test-media-player.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
unit/test-media-player.c +31 -0
index ba10e5175..53dc6f69b 100644
@@ -770,6 +770,33 @@ static void test_total_items_scope_change(const void *data)
 	g_assert(context->total_items_calls == 1);
 }
 
+/*
+ * The count of a folder that is no longer the scope must not replace the
+ * count of the current one.
+ */
+static void test_total_items_stale(const void *data)
+{
+	context = create_context();
+	if (context == NULL)
+		return;
+
+	g_assert(media_player_create_folder(context->mp, "/Filesystem",
+					PLAYER_FOLDER_TYPE_MIXED, 0) != NULL);
+	g_assert(media_player_create_folder(context->mp, "/Other",
+					PLAYER_FOLDER_TYPE_MIXED, 0) != NULL);
+
+	media_player_set_folder(context->mp, "/Filesystem", 0);
+	media_player_set_folder(context->mp, "/Other", 0);
+	media_player_set_folder(context->mp, "/Filesystem", 0);
+
+	media_player_total_items_complete(context->mp, "/Filesystem",
+								TOTAL_ITEMS);
+	media_player_total_items_complete(context->mp, "/Other",
+							TOTAL_ITEMS + 1);
+
+	call_get_number_of_items(&context->pending, number_of_items_reply);
+}
+
 /*
  * Search() shares the pending request slot with the other browsing
  * requests, so a busy Search() must report EBUSY like they do.
@@ -834,6 +861,10 @@ int main(int argc, char *argv[])
 					test_total_items_scope_change,
 					test_teardown);
 
+	tester_add("/media_player/total_items_stale", NULL, NULL,
+					test_total_items_stale,
+					test_teardown);
+
 	tester_add("/media_player/search_busy", NULL, NULL,
 					test_search_busy, test_teardown);
 
Signature · 1 line
2.43.0
#11 lore raw

PATCH v4 10/10 player: Fix Now Playing NumberOfItems when its content changes

In reply to Simon Mikuda

When the target reports that Now Playing changed (NOW_PLAYING_CONTENT_CHANGED), bluetoothd drops the listed items but keeps the old NumberOfItems, and ListItems asks the target for only that many items. So if the list grows while the client stays in Now Playing, ListItems still returns only as many items as before.

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.

When Now Playing changes, reset NumberOfItems to 0 (unknown), so that ListItems asks for all items until the target reports the new number, and request the number again if Now Playing is the current folder.

Fixes 671f370a8e34 ("audio/player: Clear playlist on Now Playing Changed AVRCP event")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/player.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
profiles/audio/player.c +16 -0
index 9802e52bb..e1458dfb8 100644
@@ -2065,9 +2065,25 @@ 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;
+
+		/* Unknown until the target reports it 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,
Signature · 1 line
2.43.0