diff mbox series

[BlueZ,2/2] transport: Allow to set A2DP transport delay property

Message ID 20241026191434.556716-2-arkadiusz.bokowy@gmail.com (mailing list archive)
State New
Headers show
Series [BlueZ,1/2] transport: Expose DelayReporting on MediaTransport interface | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/CheckPatch warning WARNING:LONG_LINE: line length of 82 exceeds 80 columns #185: FILE: profiles/audio/transport.c:946: + DBusMessageIter *iter, GDBusPendingPropertySet id, WARNING:LONG_LINE: line length of 85 exceeds 80 columns #222: FILE: profiles/audio/transport.c:1111: + { "Delay", "q", get_delay_report, set_delay_report, delay_reporting_exists }, WARNING:LONG_LINE: line length of 86 exceeds 80 columns #231: FILE: profiles/audio/transport.c:2271: + _get_volume, _set_volume, _set_delay, _update_links, _destroy) \ WARNING:LONG_LINE: line length of 84 exceeds 80 columns #260: FILE: profiles/audio/transport.c:2303: + transport_bap_get_stream, NULL, NULL, NULL, _update_links, \ /github/workspace/src/src/13852327.patch total: 0 errors, 4 warnings, 158 lines checked NOTE: For some of the reported defects, checkpatch may be able to mechanically convert to the typical style using --fix or --fix-inplace. /github/workspace/src/src/13852327.patch has style problems, please review. NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO NOTE: If any of the errors are false positives, please report them to the maintainer, see CHECKPATCH in MAINTAINERS.
tedd_an/GitLint success Gitlint PASS

Commit Message

Arkadiusz Bokowy Oct. 26, 2024, 7:14 p.m. UTC
In order to properly synchronize audio/video playback it is required to
report audio delay to the A2DP source. This commit allows connected
media application to update the Delay property of the A2DP transport
which will inform remote source about the playback delay.

The functionality was tested by streaming audio between two hosts
running BlueZ Bluetooth stack.
---
 doc/org.bluez.MediaTransport.rst |  2 +-
 profiles/audio/transport.c       | 87 +++++++++++++++++++++++++++++---
 2 files changed, 82 insertions(+), 7 deletions(-)

Comments

Luiz Augusto von Dentz Oct. 28, 2024, 2:24 p.m. UTC | #1
Hi Arkadiusz,

On Sat, Oct 26, 2024 at 3:15 PM Arkadiusz Bokowy
<arkadiusz.bokowy@gmail.com> wrote:
>
> In order to properly synchronize audio/video playback it is required to
> report audio delay to the A2DP source. This commit allows connected
> media application to update the Delay property of the A2DP transport
> which will inform remote source about the playback delay.
>
> The functionality was tested by streaming audio between two hosts
> running BlueZ Bluetooth stack.
> ---
>  doc/org.bluez.MediaTransport.rst |  2 +-
>  profiles/audio/transport.c       | 87 +++++++++++++++++++++++++++++---
>  2 files changed, 82 insertions(+), 7 deletions(-)
>
> diff --git a/doc/org.bluez.MediaTransport.rst b/doc/org.bluez.MediaTransport.rst
> index 5da13b3b5..310a69c6f 100644
> --- a/doc/org.bluez.MediaTransport.rst
> +++ b/doc/org.bluez.MediaTransport.rst
> @@ -125,7 +125,7 @@ uint16 Delay [readwrite, optional]
>
>         Transport delay in 1/10 of millisecond.
>         This property is available only if the DelayReporting is true and is
> -       writeable only when the transport was acquired by the sender.
> +       writeable only when the transport corresponds to a sink endpoint.

I don't think we should allow changes to the Delay by any application
since this might introduce problems to the streaming, so Id say this
policy should stay, if you are trying to control this via bluetoothctl
then also acquire the stream via bluetoothctl.

>  uint16 Volume [readwrite, optional]
>  ```````````````````````````````````
> diff --git a/profiles/audio/transport.c b/profiles/audio/transport.c
> index dd6878427..be34cc899 100644
> --- a/profiles/audio/transport.c
> +++ b/profiles/audio/transport.c
> @@ -116,6 +116,7 @@ struct media_transport_ops {
>         void *(*get_stream)(struct media_transport *transport);
>         int8_t (*get_volume)(struct media_transport *transport);
>         int (*set_volume)(struct media_transport *transport, int8_t level);
> +       int (*set_delay)(struct media_transport *transport, uint16_t delay);
>         void (*update_links)(const struct media_transport *transport);
>         GDestroyNotify destroy;
>  };
> @@ -551,6 +552,36 @@ static int transport_a2dp_snk_set_volume(struct media_transport *transport,
>         return avrcp_set_volume(transport->device, level, notify);
>  }
>
> +static int transport_a2dp_snk_set_delay(struct media_transport *transport,
> +                                       uint16_t delay)
> +{
> +       struct a2dp_transport *a2dp = transport->data;
> +       struct avdtp_stream *stream;
> +
> +       if (a2dp->delay == delay)
> +               return 0;
> +
> +       if (a2dp->session == NULL) {
> +               a2dp->session = a2dp_avdtp_get(transport->device);
> +               if (a2dp->session == NULL)
> +                       return -EIO;
> +       }
> +
> +       stream = media_transport_get_stream(transport);
> +       if (stream == NULL)
> +               return -EIO;
> +
> +       if (a2dp->watch) {
> +               a2dp->delay = delay;
> +               g_dbus_emit_property_changed(btd_get_dbus_connection(),
> +                                               transport->path,
> +                                               MEDIA_TRANSPORT_INTERFACE,
> +                                               "Delay");
> +       }
> +
> +       return avdtp_delay_report(a2dp->session, stream, delay);
> +}
> +
>  static void media_owner_exit(DBusConnection *connection, void *user_data)
>  {
>         struct media_owner *owner = user_data;
> @@ -900,6 +931,47 @@ static gboolean get_delay_report(const GDBusPropertyTable *property,
>         return TRUE;
>  }
>
> +static int media_transport_set_delay(struct media_transport *transport,
> +                                       uint16_t delay)
> +{
> +       DBG("Transport %s delay %d", transport->path, delay);
> +
> +       if (transport->ops && transport->ops->set_delay)
> +               return transport->ops->set_delay(transport, delay);
> +
> +       return 0;
> +}
> +
> +static void set_delay_report(const GDBusPropertyTable *property,
> +                               DBusMessageIter *iter, GDBusPendingPropertySet id,
> +                               void *data)
> +{
> +       struct media_transport *transport = data;
> +       uint16_t arg;
> +       int err;

This really needs to check that sender is the owner of the transport.

> +       if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT16) {
> +               g_dbus_pending_property_error(id,
> +                               ERROR_INTERFACE ".InvalidArguments",
> +                               "Expected UINT16");
> +               return;
> +       }
> +
> +       dbus_message_iter_get_basic(iter, &arg);
> +
> +       err = media_transport_set_delay(transport, arg);
> +       if (err) {
> +               error("Unable to set delay: %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 volume_exists(const GDBusPropertyTable *property, void *data)
>  {
>         struct media_transport *transport = data;
> @@ -1036,7 +1108,7 @@ static const GDBusPropertyTable transport_a2dp_properties[] = {
>         { "Configuration", "ay", get_configuration },
>         { "State", "s", get_state },
>         { "DelayReporting", "b", get_delay_reporting },
> -       { "Delay", "q", get_delay_report, NULL, delay_reporting_exists },
> +       { "Delay", "q", get_delay_report, set_delay_report, delay_reporting_exists },
>         { "Volume", "q", get_volume, set_volume, volume_exists },
>         { "Endpoint", "o", get_endpoint, NULL, endpoint_exists,
>                                 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
> @@ -2196,7 +2268,7 @@ static void *transport_asha_init(struct media_transport *transport, void *data)
>
>  #define TRANSPORT_OPS(_uuid, _props, _set_owner, _remove_owner, _init, \
>                       _resume, _suspend, _cancel, _set_state, _get_stream, \
> -                     _get_volume, _set_volume, _update_links, _destroy) \
> +                     _get_volume, _set_volume, _set_delay, _update_links, _destroy) \
>  { \
>         .uuid = _uuid, \
>         .properties = _props, \
> @@ -2210,16 +2282,17 @@ static void *transport_asha_init(struct media_transport *transport, void *data)
>         .get_stream = _get_stream, \
>         .get_volume = _get_volume, \
>         .set_volume = _set_volume, \
> +       .set_delay = _set_delay, \
>         .update_links = _update_links, \
>         .destroy = _destroy \
>  }
>
> -#define A2DP_OPS(_uuid, _init, _set_volume, _destroy) \
> +#define A2DP_OPS(_uuid, _init, _set_volume, _set_delay, _destroy) \
>         TRANSPORT_OPS(_uuid, transport_a2dp_properties, NULL, NULL, _init, \
>                         transport_a2dp_resume, transport_a2dp_suspend, \
>                         transport_a2dp_cancel, NULL, \
>                         transport_a2dp_get_stream, transport_a2dp_get_volume, \
> -                       _set_volume, NULL, _destroy)
> +                       _set_volume, _set_delay, NULL, _destroy)
>
>  #define BAP_OPS(_uuid, _props, _set_owner, _remove_owner, _update_links, \
>                 _set_state) \
> @@ -2227,7 +2300,7 @@ static void *transport_asha_init(struct media_transport *transport, void *data)
>                         transport_bap_init, \
>                         transport_bap_resume, transport_bap_suspend, \
>                         transport_bap_cancel, _set_state, \
> -                       transport_bap_get_stream, NULL, NULL, _update_links, \
> +                       transport_bap_get_stream, NULL, NULL, NULL, _update_links, \
>                         transport_bap_destroy)
>
>  #define BAP_UC_OPS(_uuid) \
> @@ -2245,14 +2318,16 @@ static void *transport_asha_init(struct media_transport *transport, void *data)
>                         transport_asha_resume, transport_asha_suspend, \
>                         transport_asha_cancel, NULL, NULL, \
>                         transport_asha_get_volume, transport_asha_set_volume, \
> -                       NULL, NULL)
> +                       NULL, NULL, NULL)
>
>  static const struct media_transport_ops transport_ops[] = {
>         A2DP_OPS(A2DP_SOURCE_UUID, transport_a2dp_src_init,
>                         transport_a2dp_src_set_volume,
> +                       NULL,
>                         transport_a2dp_src_destroy),
>         A2DP_OPS(A2DP_SINK_UUID, transport_a2dp_snk_init,
>                         transport_a2dp_snk_set_volume,
> +                       transport_a2dp_snk_set_delay,
>                         transport_a2dp_snk_destroy),
>         BAP_UC_OPS(PAC_SOURCE_UUID),
>         BAP_UC_OPS(PAC_SINK_UUID),
> --
> 2.39.5
>
>
Arkadiusz Bokowy Oct. 28, 2024, 8:32 p.m. UTC | #2
> > +static void set_delay_report(const GDBusPropertyTable *property,
> > +                               DBusMessageIter *iter, GDBusPendingPropertySet id,
> > +                               void *data)
> > +{
> > +       struct media_transport *transport = data;
> > +       uint16_t arg;
> > +       int err;
>
> This really needs to check that sender is the owner of the transport.

The problem is that currently there is no mechanism which will allow
to get the sender name in the property get/set callback (at least I
couldn't find any). The statement "this property is only writable when
the transport was acquired by the sender" is also for the Volume
property, but as for Delay (which currently lacks the setter) it's not
implemented. So, now the question is whether you would like to fix
that as well (or amend the doc)? Some users might say that allowing
others to control volume might be a feature. For the Delay I'm not
sure... Probably you are right that the Delay is more intrinsic to the
transport, and external manipulation would not be desired.

As for the design of this authorization check I can see two possibilities:

1. Pass `message` to the `property->set()` in the
"dbus/objects.c:properties_set()". But this will require updates in
all setters (and maybe for symmetry the getter should also receive the
original message?).
2. Add a dedicated callback for setter/getter authorization, in a
similar way the `exists()` works.

In either way I think that this should be a separate patch, applied
either before or after the Delay work.

Regards,
Arek
Luiz Augusto von Dentz Oct. 28, 2024, 8:52 p.m. UTC | #3
Hi Arkadiusz,

On Mon, Oct 28, 2024 at 4:33 PM Arkadiusz Bokowy
<arkadiusz.bokowy@gmail.com> wrote:
>
> > > +static void set_delay_report(const GDBusPropertyTable *property,
> > > +                               DBusMessageIter *iter, GDBusPendingPropertySet id,
> > > +                               void *data)
> > > +{
> > > +       struct media_transport *transport = data;
> > > +       uint16_t arg;
> > > +       int err;
> >
> > This really needs to check that sender is the owner of the transport.
>
> The problem is that currently there is no mechanism which will allow
> to get the sender name in the property get/set callback (at least I
> couldn't find any). The statement "this property is only writable when
> the transport was acquired by the sender" is also for the Volume
> property, but as for Delay (which currently lacks the setter) it's not
> implemented. So, now the question is whether you would like to fix
> that as well (or amend the doc)? Some users might say that allowing
> others to control volume might be a feature. For the Delay I'm not
> sure... Probably you are right that the Delay is more intrinsic to the
> transport, and external manipulation would not be desired.
>
> As for the design of this authorization check I can see two possibilities:
>
> 1. Pass `message` to the `property->set()` in the
> "dbus/objects.c:properties_set()". But this will require updates in
> all setters (and maybe for symmetry the getter should also receive the
> original message?).
> 2. Add a dedicated callback for setter/getter authorization, in a
> similar way the `exists()` works.
>
> In either way I think that this should be a separate patch, applied
> either before or after the Delay work.

Right, or we could just implement something like get_sender_by_id then
the callback can just call it to check who is the sender, that way we
don't need to modify existing code if it doesn't care about it.

> Regards,
> Arek
Arkadiusz Bokowy Oct. 28, 2024, 9:05 p.m. UTC | #4
> > > > +static void set_delay_report(const GDBusPropertyTable *property,
> > > > +                               DBusMessageIter *iter, GDBusPendingPropertySet id,
> > > > +                               void *data)
> > > > +{
> > > > +       struct media_transport *transport = data;
> > > > +       uint16_t arg;
> > > > +       int err;
> > >
> > > This really needs to check that sender is the owner of the transport.
> >
> > The problem is that currently there is no mechanism which will allow
> > to get the sender name in the property get/set callback (at least I
> > couldn't find any). The statement "this property is only writable when
> > the transport was acquired by the sender" is also for the Volume
> > property, but as for Delay (which currently lacks the setter) it's not
> > implemented. So, now the question is whether you would like to fix
> > that as well (or amend the doc)? Some users might say that allowing
> > others to control volume might be a feature. For the Delay I'm not
> > sure... Probably you are right that the Delay is more intrinsic to the
> > transport, and external manipulation would not be desired.
> >
> > As for the design of this authorization check I can see two possibilities:
> >
> > 1. Pass `message` to the `property->set()` in the
> > "dbus/objects.c:properties_set()". But this will require updates in
> > all setters (and maybe for symmetry the getter should also receive the
> > original message?).
> > 2. Add a dedicated callback for setter/getter authorization, in a
> > similar way the `exists()` works.
> >
> > In either way I think that this should be a separate patch, applied
> > either before or after the Delay work.
>
> Right, or we could just implement something like get_sender_by_id then
> the callback can just call it to check who is the sender, that way we
> don't need to modify existing code if it doesn't care about it.

Yeap, that was my 3rd idea, but I thought that it would be a bit
hackish since the `pending_property_set` seems to be kinda private.
But OK, I can make it that way. It would be definitely the easiest
way.
diff mbox series

Patch

diff --git a/doc/org.bluez.MediaTransport.rst b/doc/org.bluez.MediaTransport.rst
index 5da13b3b5..310a69c6f 100644
--- a/doc/org.bluez.MediaTransport.rst
+++ b/doc/org.bluez.MediaTransport.rst
@@ -125,7 +125,7 @@  uint16 Delay [readwrite, optional]
 
 	Transport delay in 1/10 of millisecond.
 	This property is available only if the DelayReporting is true and is
-	writeable only when the transport was acquired by the sender.
+	writeable only when the transport corresponds to a sink endpoint.
 
 uint16 Volume [readwrite, optional]
 ```````````````````````````````````
diff --git a/profiles/audio/transport.c b/profiles/audio/transport.c
index dd6878427..be34cc899 100644
--- a/profiles/audio/transport.c
+++ b/profiles/audio/transport.c
@@ -116,6 +116,7 @@  struct media_transport_ops {
 	void *(*get_stream)(struct media_transport *transport);
 	int8_t (*get_volume)(struct media_transport *transport);
 	int (*set_volume)(struct media_transport *transport, int8_t level);
+	int (*set_delay)(struct media_transport *transport, uint16_t delay);
 	void (*update_links)(const struct media_transport *transport);
 	GDestroyNotify destroy;
 };
@@ -551,6 +552,36 @@  static int transport_a2dp_snk_set_volume(struct media_transport *transport,
 	return avrcp_set_volume(transport->device, level, notify);
 }
 
+static int transport_a2dp_snk_set_delay(struct media_transport *transport,
+					uint16_t delay)
+{
+	struct a2dp_transport *a2dp = transport->data;
+	struct avdtp_stream *stream;
+
+	if (a2dp->delay == delay)
+		return 0;
+
+	if (a2dp->session == NULL) {
+		a2dp->session = a2dp_avdtp_get(transport->device);
+		if (a2dp->session == NULL)
+			return -EIO;
+	}
+
+	stream = media_transport_get_stream(transport);
+	if (stream == NULL)
+		return -EIO;
+
+	if (a2dp->watch) {
+		a2dp->delay = delay;
+		g_dbus_emit_property_changed(btd_get_dbus_connection(),
+						transport->path,
+						MEDIA_TRANSPORT_INTERFACE,
+						"Delay");
+	}
+
+	return avdtp_delay_report(a2dp->session, stream, delay);
+}
+
 static void media_owner_exit(DBusConnection *connection, void *user_data)
 {
 	struct media_owner *owner = user_data;
@@ -900,6 +931,47 @@  static gboolean get_delay_report(const GDBusPropertyTable *property,
 	return TRUE;
 }
 
+static int media_transport_set_delay(struct media_transport *transport,
+					uint16_t delay)
+{
+	DBG("Transport %s delay %d", transport->path, delay);
+
+	if (transport->ops && transport->ops->set_delay)
+		return transport->ops->set_delay(transport, delay);
+
+	return 0;
+}
+
+static void set_delay_report(const GDBusPropertyTable *property,
+				DBusMessageIter *iter, GDBusPendingPropertySet id,
+				void *data)
+{
+	struct media_transport *transport = data;
+	uint16_t arg;
+	int err;
+
+	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT16) {
+		g_dbus_pending_property_error(id,
+				ERROR_INTERFACE ".InvalidArguments",
+				"Expected UINT16");
+		return;
+	}
+
+	dbus_message_iter_get_basic(iter, &arg);
+
+	err = media_transport_set_delay(transport, arg);
+	if (err) {
+		error("Unable to set delay: %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 volume_exists(const GDBusPropertyTable *property, void *data)
 {
 	struct media_transport *transport = data;
@@ -1036,7 +1108,7 @@  static const GDBusPropertyTable transport_a2dp_properties[] = {
 	{ "Configuration", "ay", get_configuration },
 	{ "State", "s", get_state },
 	{ "DelayReporting", "b", get_delay_reporting },
-	{ "Delay", "q", get_delay_report, NULL, delay_reporting_exists },
+	{ "Delay", "q", get_delay_report, set_delay_report, delay_reporting_exists },
 	{ "Volume", "q", get_volume, set_volume, volume_exists },
 	{ "Endpoint", "o", get_endpoint, NULL, endpoint_exists,
 				G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
@@ -2196,7 +2268,7 @@  static void *transport_asha_init(struct media_transport *transport, void *data)
 
 #define TRANSPORT_OPS(_uuid, _props, _set_owner, _remove_owner, _init, \
 		      _resume, _suspend, _cancel, _set_state, _get_stream, \
-		      _get_volume, _set_volume, _update_links, _destroy) \
+		      _get_volume, _set_volume, _set_delay, _update_links, _destroy) \
 { \
 	.uuid = _uuid, \
 	.properties = _props, \
@@ -2210,16 +2282,17 @@  static void *transport_asha_init(struct media_transport *transport, void *data)
 	.get_stream = _get_stream, \
 	.get_volume = _get_volume, \
 	.set_volume = _set_volume, \
+	.set_delay = _set_delay, \
 	.update_links = _update_links, \
 	.destroy = _destroy \
 }
 
-#define A2DP_OPS(_uuid, _init, _set_volume, _destroy) \
+#define A2DP_OPS(_uuid, _init, _set_volume, _set_delay, _destroy) \
 	TRANSPORT_OPS(_uuid, transport_a2dp_properties, NULL, NULL, _init, \
 			transport_a2dp_resume, transport_a2dp_suspend, \
 			transport_a2dp_cancel, NULL, \
 			transport_a2dp_get_stream, transport_a2dp_get_volume, \
-			_set_volume, NULL, _destroy)
+			_set_volume, _set_delay, NULL, _destroy)
 
 #define BAP_OPS(_uuid, _props, _set_owner, _remove_owner, _update_links, \
 		_set_state) \
@@ -2227,7 +2300,7 @@  static void *transport_asha_init(struct media_transport *transport, void *data)
 			transport_bap_init, \
 			transport_bap_resume, transport_bap_suspend, \
 			transport_bap_cancel, _set_state, \
-			transport_bap_get_stream, NULL, NULL, _update_links, \
+			transport_bap_get_stream, NULL, NULL, NULL, _update_links, \
 			transport_bap_destroy)
 
 #define BAP_UC_OPS(_uuid) \
@@ -2245,14 +2318,16 @@  static void *transport_asha_init(struct media_transport *transport, void *data)
 			transport_asha_resume, transport_asha_suspend, \
 			transport_asha_cancel, NULL, NULL, \
 			transport_asha_get_volume, transport_asha_set_volume, \
-			NULL, NULL)
+			NULL, NULL, NULL)
 
 static const struct media_transport_ops transport_ops[] = {
 	A2DP_OPS(A2DP_SOURCE_UUID, transport_a2dp_src_init,
 			transport_a2dp_src_set_volume,
+			NULL,
 			transport_a2dp_src_destroy),
 	A2DP_OPS(A2DP_SINK_UUID, transport_a2dp_snk_init,
 			transport_a2dp_snk_set_volume,
+			transport_a2dp_snk_set_delay,
 			transport_a2dp_snk_destroy),
 	BAP_UC_OPS(PAC_SOURCE_UUID),
 	BAP_UC_OPS(PAC_SINK_UUID),