Skip to contentSkip to conversation

doc/org.bluez.MediaTransport: Add Mute property

PATCH New Simon Mikuda 1 participant 4 messages Nov 25 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 doc/org.bluez.MediaTransport: Add Mute property

Add a Mute property for LE Audio (BAP) transports with a Volume Control Service session, next to Volume.

Assisted-by Claude:claude-opus-5-5

---
Changes in v2:
- shared/vcp: client mute waits for the request in flight, like volume
- client/player: transport.mute without a value exits with success

 doc/org.bluez.MediaTransport.rst | 10 ++++++++++
 1 file changed, 10 insertions(+)
doc/org.bluez.MediaTransport.rst +10 -0
index e50ec3f20..35a227237 100644
@@ -160,6 +160,16 @@ Examples:
 
 :bluetoothctl: > transport.volume <transport> [value]
 
+boolean Mute [readwrite, optional]
+``````````````````````````````````
+
+Indicates mute state of the transport, only present for LE Audio (BAP)
+transports backed by VCS.
+
+Examples:
+
+:bluetoothctl: > transport.mute <transport> [on/off]
+
 object Endpoint [readonly, optional, experimental]
 ``````````````````````````````````````````````````
 

base-commit: 1608f836188610aabeff5a3164169ebe0360e443

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 2/4 shared/vcp: Add bt_vcp_get_mute and bt_vcp_set_mute

Keep the Mute field of Volume State next to the volume. As a client, setting it writes Mute or Unmute to the remote Volume Control Point. As a server, it updates the local Volume State and notifies it.

The volume callback now also runs when only the mute state changes, so its user sees a remote client muting or unmuting the server.

Assisted-by Claude:claude-opus-5-5
---
 src/shared/vcp.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/vcp.h |   3 ++
 2 files changed, 116 insertions(+)
src/shared/vcp.c +113 -0
index c7f74956e..1dae20d46 100644
@@ -181,6 +181,8 @@ struct bt_vcp_notify {
 struct bt_vcp_client_op {
 	uint8_t volume;
 	bool resend;
+	uint8_t mute;
+	bool resend_mute;
 	bool wait_reply;
 	bool wait_notify;
 	unsigned int timeout_id;
@@ -216,6 +218,7 @@ struct bt_vcp {
 
 	uint8_t volume;
 	uint8_t volume_counter;
+	uint8_t mute;
 
 	struct bt_vcp_client_op pending_op;
 
@@ -977,6 +980,18 @@ struct vocs_op_handler {
 	{}
 };
 
+static void vcp_mute_changed(struct bt_vcp *vcp, uint8_t mute)
+{
+	if (vcp->mute == mute)
+		return;
+
+	vcp->mute = mute;
+
+	/* The volume callback reports any Volume State change */
+	if (vcp->volume_changed)
+		vcp->volume_changed(vcp, vcp->volume);
+}
+
 static void vcs_cp_write(struct gatt_db_attribute *attrib,
 				unsigned int id, uint16_t offset,
 				const uint8_t *value, size_t len,
@@ -991,6 +1006,7 @@ static void vcs_cp_write(struct gatt_db_attribute *attrib,
 	};
 	uint8_t	*vcp_op;
 	struct vcs_op_handler *handler;
+	struct vol_state *vstate;
 	uint8_t ret = BT_ATT_ERROR_REQUEST_NOT_SUPPORTED;
 
 	DBG(vcp, "VCP Control Point Write");
@@ -1032,6 +1048,10 @@ static void vcs_cp_write(struct gatt_db_attribute *attrib,
 		DBG(vcp, "%s", handler->str);
 
 		ret = handler->func(vcs, vcp, &iov);
+
+		vstate = vdb_get_vstate(vcs->vdb);
+		if (!ret && vstate)
+			vcp_mute_changed(vcp, vstate->mute);
 	} else {
 		DBG(vcp, "Unknown opcode 0x%02x", *vcp_op);
 		ret = BT_ATT_ERROR_OPCODE_NOT_SUPPORTED;
@@ -2045,6 +2065,8 @@ static void vcp_set_volume_complete(struct bt_vcp *vcp)
 {
 	bool resend = vcp->pending_op.resend;
 	uint8_t volume = vcp->pending_op.volume;
+	bool resend_mute = vcp->pending_op.resend_mute;
+	uint8_t mute = vcp->pending_op.mute;
 
 	vcp_client_op_clear(&vcp->pending_op);
 
@@ -2055,6 +2077,12 @@ static void vcp_set_volume_complete(struct bt_vcp *vcp)
 		DBG(vcp, "set pending volume 0x%x", volume);
 		bt_vcp_set_volume(vcp, volume);
 	}
+
+	/* If the volume request above is in flight, this one waits for it */
+	if (resend_mute) {
+		DBG(vcp, "set pending mute 0x%x", mute);
+		bt_vcp_set_mute(vcp, mute);
+	}
 }
 
 static void vcp_vstate_notify(struct bt_vcp *vcp, uint16_t value_handle,
@@ -2076,6 +2104,7 @@ static void vcp_vstate_notify(struct bt_vcp *vcp, uint16_t value_handle,
 
 	vcp->volume = vstate->vol_set;
 	vcp->volume_counter = vstate->counter;
+	vcp->mute = vstate->mute;
 
 	if (vcp->volume_changed)
 		vcp->volume_changed(vcp, vcp->volume);
@@ -2207,6 +2236,89 @@ uint8_t bt_vcp_get_volume(struct bt_vcp *vcp)
 	return vcp->volume;
 }
 
+static bool vcp_set_mute_client(struct bt_vcp *vcp, uint8_t mute)
+{
+	struct bt_vcs_param req;
+	uint16_t value_handle;
+	struct bt_vcs *vcs = vcp_get_vcs(vcp);
+
+	if (!vcs || !vcs->vol_cp) {
+		DBG(vcp, "error: vol_cp characteristic not available");
+		return false;
+	}
+
+	if (!gatt_db_attribute_get_char_data(vcs->vol_cp, NULL, &value_handle,
+							NULL, NULL, NULL)) {
+		DBG(vcp, "error: vol_cp characteristic not available");
+		return false;
+	}
+
+	/* Like volume, wait for the op in flight: it changes the counter */
+	if (vcp->pending_op.timeout_id) {
+		vcp->pending_op.mute = mute;
+		vcp->pending_op.resend_mute = true;
+		return true;
+	} else if (vcp->mute == mute) {
+		return true;
+	}
+
+	req.op = mute ? BT_VCS_MUTE : BT_VCS_UNMUTE;
+	req.change_counter = vcp->volume_counter;
+
+	if (!bt_gatt_client_write_value(vcp->client, value_handle, (void *)&req,
+					sizeof(req), vcp_volume_cp_sent, vcp,
+					NULL)) {
+		DBG(vcp, "error writing mute");
+		return false;
+	}
+
+	vcp->pending_op.timeout_id = timeout_add(VCP_CLIENT_OP_TIMEOUT,
+					vcp_set_volume_timeout, vcp, NULL);
+	vcp->pending_op.wait_notify = true;
+	vcp->pending_op.wait_reply = true;
+	return true;
+}
+
+static bool vcp_set_mute_server(struct bt_vcp *vcp, uint8_t mute)
+{
+	struct bt_vcp_db *vdb = vcp_get_vdb(vcp);
+	struct vol_state *vstate;
+
+	if (!vdb) {
+		DBG(vcp, "error: VDB not available");
+		return false;
+	}
+
+	vstate = vdb_get_vstate(vdb);
+	if (!vstate) {
+		DBG(vcp, "error: VSTATE not available");
+		return false;
+	}
+
+	if (vstate->mute == mute)
+		return true;
+
+	vstate->mute = mute;
+	vstate->counter = -~vstate->counter; /*Increment Change Counter*/
+	gatt_db_attribute_notify(vdb->vcs->vs, (void *) vstate,
+			sizeof(struct vol_state), bt_vcp_get_att(vcp));
+	vcp_mute_changed(vcp, mute);
+	return true;
+}
+
+bool bt_vcp_set_mute(struct bt_vcp *vcp, uint8_t mute)
+{
+	if (vcp->client)
+		return vcp_set_mute_client(vcp, mute);
+	else
+		return vcp_set_mute_server(vcp, mute);
+}
+
+uint8_t bt_vcp_get_mute(struct bt_vcp *vcp)
+{
+	return vcp->mute;
+}
+
 static void vcp_voffset_state_notify(struct bt_vcp *vcp, uint16_t value_handle,
 				const uint8_t *value, uint16_t length,
 				void *user_data)
@@ -2320,6 +2432,7 @@ static void read_vol_state(struct bt_vcp *vcp, bool success, uint8_t att_ecode,
 
 	vcp->volume = vs->vol_set;
 	vcp->volume_counter = vs->counter;
+	vcp->mute = vs->mute;
 }
 
 static void read_vol_offset_state(struct bt_vcp *vcp, bool success,
src/shared/vcp.h +3 -0
index e031beafd..807d2bdbe 100644
@@ -42,6 +42,9 @@ void bt_vcp_detach(struct bt_vcp *vcp);
 uint8_t bt_vcp_get_volume(struct bt_vcp *vcp);
 bool bt_vcp_set_volume(struct bt_vcp *vcp, uint8_t volume);
 
+uint8_t bt_vcp_get_mute(struct bt_vcp *vcp);
+bool bt_vcp_set_mute(struct bt_vcp *vcp, uint8_t mute);
+
 bool bt_vcp_set_debug(struct bt_vcp *vcp, bt_vcp_debug_func_t cb,
 			void *user_data, bt_vcp_destroy_func_t destroy);
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 3/4 audio/transport: Add Mute property

In reply to Simon Mikuda

Add Mute to BAP unicast transports that have a VCP session, read and written through VCP like Volume. A2DP transports don't get it: AVRCP has no mute.

Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/transport.c | 93 ++++++++++++++++++++++++++++++++++++++
 profiles/audio/vcp.c       | 20 ++++++++
 profiles/audio/vcp.h       | 12 +++++
 3 files changed, 125 insertions(+)
profiles/audio/transport.c +93 -0
index ad2fc2c1c..ca5bda560 100644
@@ -1267,6 +1267,64 @@ static void set_volume(const GDBusPropertyTable *property,
 	g_dbus_pending_property_success(id);
 }
 
+static int transport_bap_get_mute(struct media_transport *transport);
+static int transport_bap_set_mute(struct media_transport *transport,
+								bool mute);
+
+static gboolean mute_exists(const GDBusPropertyTable *property, void *data)
+{
+	struct media_transport *transport = data;
+
+	return transport_bap_get_mute(transport) >= 0;
+}
+
+static gboolean get_mute(const GDBusPropertyTable *property,
+					DBusMessageIter *iter, void *data)
+{
+	struct media_transport *transport = data;
+	dbus_bool_t mute;
+	int ret;
+
+	ret = transport_bap_get_mute(transport);
+	if (ret < 0)
+		return FALSE;
+
+	mute = ret;
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &mute);
+
+	return TRUE;
+}
+
+static void set_mute(const GDBusPropertyTable *property,
+			DBusMessageIter *iter, GDBusPendingPropertySet id,
+			void *data)
+{
+	struct media_transport *transport = data;
+	dbus_bool_t mute;
+	int err;
+
+	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_BOOLEAN) {
+		g_dbus_pending_property_error(id,
+				ERROR_INTERFACE ".InvalidArguments",
+				"Expected BOOLEAN");
+		return;
+	}
+
+	dbus_message_iter_get_basic(iter, &mute);
+
+	err = transport_bap_set_mute(transport, mute);
+	if (err) {
+		error("Unable to set mute: %s (%d)", strerror(-err), err);
+		g_dbus_pending_property_error(id, ERROR_INTERFACE ".Failed",
+						"Internal error %s (%d)",
+						strerror(-err), err);
+		return;
+	}
+
+	g_dbus_pending_property_success(id);
+}
+
 static gboolean endpoint_exists(const GDBusPropertyTable *property, void *data)
 {
 	struct media_transport *transport = data;
@@ -1620,6 +1678,7 @@ static const GDBusPropertyTable transport_bap_uc_properties[] = {
 	{ "Metadata", "ay", get_metadata, set_metadata },
 	{ "Links", "ao", get_links, NULL, links_exists },
 	{ "Volume", "q", get_volume, set_volume, volume_exists },
+	{ "Mute", "b", get_mute, set_mute, mute_exists },
 	{ }
 };
 
@@ -1635,6 +1694,7 @@ static const GDBusPropertyTable transport_bap_uc_test_properties[] = {
 	{ "Metadata", "ay", get_metadata, set_metadata },
 	{ "Links", "ao", get_links, NULL, links_exists },
 	{ "Volume", "q", get_volume, set_volume, volume_exists },
+	{ "Mute", "b", get_mute, set_mute, mute_exists },
 	{ "Desynchronized", "b", get_desync, set_desync, links_exists},
 	{ }
 };
@@ -2620,6 +2680,22 @@ static int transport_bap_set_volume(struct media_transport *transport,
 		return -ENOTSUP; /* TODO: MICP */
 }
 
+static int transport_bap_get_mute(struct media_transport *transport)
+{
+	if (transport_bap_is_playback(transport))
+		return bt_audio_vcp_get_mute(transport->device);
+	else
+		return -ENOTSUP; /* TODO: MICP */
+}
+
+static int transport_bap_set_mute(struct media_transport *transport, bool mute)
+{
+	if (transport_bap_is_playback(transport))
+		return bt_audio_vcp_set_mute(transport->device, mute);
+	else
+		return -ENOTSUP; /* TODO: MICP */
+}
+
 static void transport_bap_destroy(void *data)
 {
 	struct bap_transport *bap = data;
@@ -3198,6 +3274,22 @@ static void media_transport_emit_volume(struct media_transport *transport)
 					MEDIA_TRANSPORT_INTERFACE, "Volume");
 }
 
+static void media_transport_emit_mute(struct media_transport *transport)
+{
+	const char *uuid = media_endpoint_get_uuid(transport->endpoint);
+
+	if (strcasecmp(uuid, PAC_SINK_UUID) &&
+				strcasecmp(uuid, PAC_SOURCE_UUID))
+		return;
+
+	if (!mute_exists(NULL, transport))
+		return;
+
+	g_dbus_emit_property_changed(btd_get_dbus_connection(),
+					transport->path,
+					MEDIA_TRANSPORT_INTERFACE, "Mute");
+}
+
 int media_transport_get_a2dp_volume(struct btd_device *dev)
 {
 	GSList *l;
@@ -3272,6 +3364,7 @@ void media_transport_volume_changed(struct btd_device *dev)
 			continue;
 
 		media_transport_emit_volume(transport);
+		media_transport_emit_mute(transport);
 	}
 }
 
profiles/audio/vcp.c +20 -0
index 00ee2b64b..7adac4dd5 100644
@@ -185,6 +185,26 @@ int bt_audio_vcp_set_volume(struct btd_device *device, uint8_t volume)
 	return -ENODEV;
 }
 
+int bt_audio_vcp_get_mute(struct btd_device *device)
+{
+	struct vcp_data *data = queue_find(sessions, match_device, device);
+
+	if (data)
+		return bt_vcp_get_mute(data->vcp);
+
+	return -ENODEV;
+}
+
+int bt_audio_vcp_set_mute(struct btd_device *device, bool mute)
+{
+	struct vcp_data *data = queue_find(sessions, match_device, device);
+
+	if (data)
+		return bt_vcp_set_mute(data->vcp, mute) ? 0 : -EIO;
+
+	return -ENODEV;
+}
+
 static void vcp_remote_client_detached(struct bt_vcp *vcp, void *user_data)
 {
 	struct vcp_data *data;
profiles/audio/vcp.h +12 -0
index b538cebf0..35ffc09f8 100644
@@ -12,6 +12,8 @@
 
 int bt_audio_vcp_get_volume(struct btd_device *device);
 int bt_audio_vcp_set_volume(struct btd_device *device, uint8_t volume);
+int bt_audio_vcp_get_mute(struct btd_device *device);
+int bt_audio_vcp_set_mute(struct btd_device *device, bool mute);
 
 #else
 
@@ -26,4 +28,14 @@ static inline int bt_audio_vcp_set_volume(struct btd_device *device,
 	return -ENODEV;
 }
 
+static inline int bt_audio_vcp_get_mute(struct btd_device *device)
+{
+	return -ENODEV;
+}
+
+static inline int bt_audio_vcp_set_mute(struct btd_device *device, bool mute)
+{
+	return -ENODEV;
+}
+
 #endif
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 4/4 client/player: Add transport.mute command

In reply to Simon Mikuda

Get or set the Mute property of a transport, like transport.volume.

Assisted-by Claude:claude-opus-5-5
---
 client/player.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
client/player.c +51 -0
index 932338ef5..71d2ee20b 100644
@@ -6160,6 +6160,54 @@ static void cmd_volume_transport(int argc, char *argv[])
 	}
 }
 
+static void mute_callback(const DBusError *error, void *user_data)
+{
+	if (dbus_error_is_set(error)) {
+		bt_shell_printf("Failed to set Mute: %s\n", error->name);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	bt_shell_printf("Changing Mute succeeded\n");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
+static void cmd_mute_transport(int argc, char *argv[])
+{
+	GDBusProxy *proxy;
+	dbus_bool_t mute;
+
+	proxy = g_dbus_proxy_lookup(transports, NULL, argv[1],
+					BLUEZ_MEDIA_TRANSPORT_INTERFACE);
+	if (!proxy) {
+		bt_shell_printf("Transport %s not found\n", argv[1]);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	if (argc == 2) {
+		print_property(proxy, "Mute");
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+	}
+
+	if (!strcasecmp(argv[2], "on") || !strcasecmp(argv[2], "yes") ||
+				!strcmp(argv[2], "1"))
+		mute = TRUE;
+	else if (!strcasecmp(argv[2], "off") || !strcasecmp(argv[2], "no") ||
+				!strcmp(argv[2], "0"))
+		mute = FALSE;
+	else {
+		bt_shell_printf("Invalid argument: %s\n", argv[2]);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	if (!g_dbus_proxy_set_property_basic(proxy, "Mute", DBUS_TYPE_BOOLEAN,
+						&mute, mute_callback,
+						NULL, NULL)) {
+		bt_shell_printf("Failed to set mute\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+}
+
 static void set_metadata_cb(const DBusError *error, void *user_data)
 {
 	if (dbus_error_is_set(error)) {
@@ -6278,6 +6326,9 @@ static const struct bt_shell_menu transport_menu = {
 	{ "volume",      "<transport> [value]",	cmd_volume_transport,
 						"Get/Set transport volume",
 						transport_generator },
+	{ "mute",        "<transport> [on/off]", cmd_mute_transport,
+						"Get/Set transport mute",
+						transport_generator },
 	{ "select",      "<transport> [transport1...]", cmd_select_transport,
 						"Select Transport",
 						transport_generator },
Signature · 1 line
2.43.0