2021-10-10 17:20:49

by Pauli Virtanen

[permalink] [raw]
Subject: [PATCH BlueZ] avrcp: keep track of last volume, and use as transport init_volume

Some devices may send AVRCP VolumeChanged notification before AVDTP
SetConfiguration occurs, and not send another until a hardware button on
the device is pressed. If a media_player is registered to BlueZ, the
volume from the event is stored on the player, and used as init_volume
for new transports. However, if no media_player is registered,
transports are created with volume missing.

If that occurs, the DBus "Volume" attribute on transports will be
missing until a hardware button is pressed. Consequently, applications
cannot get or set volume, even though it is actually possible.

Address this by keeping track of the last device volume set in AVRCP
session. If no media_player is registered, use that as the init_volume
for new transports. This has a similar effect as if a dummy media
player was registered.

This fixes AVRCP absolute volume not being available on some headphones
on Pipewire & Pulseaudio until HW button press.
---
profiles/audio/avrcp.c | 23 +++++++++++++++++++++++
profiles/audio/avrcp.h | 1 +
profiles/audio/media.c | 3 +++
3 files changed, 27 insertions(+)

diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c
index 7c280203c..0df416d2c 100644
--- a/profiles/audio/avrcp.c
+++ b/profiles/audio/avrcp.c
@@ -276,6 +276,8 @@ struct avrcp {
uint8_t transaction;
uint8_t transaction_events[AVRCP_EVENT_LAST + 1];
struct pending_pdu *pending_pdu;
+
+ int8_t last_device_volume;
};

struct passthrough_handler {
@@ -1759,6 +1761,7 @@ static uint8_t avrcp_handle_set_absolute_volume(struct avrcp *session,
volume = pdu->params[0] & 0x7F;

media_transport_update_device_volume(session->dev, volume);
+ session->last_device_volume = volume;

return AVC_CTYPE_ACCEPTED;

@@ -3731,6 +3734,7 @@ static void avrcp_volume_changed(struct avrcp *session,

/* Always attempt to update the transport volume */
media_transport_update_device_volume(session->dev, volume);
+ session->last_device_volume = volume;

if (player)
player->cb->set_volume(volume, session->dev, player->user_data);
@@ -4145,6 +4149,7 @@ static void target_init(struct avrcp *session)

init_volume = media_player_get_device_volume(session->dev);
media_transport_update_device_volume(session->dev, init_volume);
+ session->last_device_volume = init_volume;
}

session->supported_events |= (1 << AVRCP_EVENT_STATUS_CHANGED) |
@@ -4308,6 +4313,7 @@ static struct avrcp *session_create(struct avrcp_server *server,
session->server = server;
session->conn = avctp_connect(device);
session->dev = device;
+ session->last_device_volume = -1;

server->sessions = g_slist_append(server->sessions, session);

@@ -4497,6 +4503,7 @@ static gboolean avrcp_handle_set_volume(struct avctp *conn, uint8_t code,

/* Always attempt to update the transport volume */
media_transport_update_device_volume(session->dev, volume);
+ session->last_device_volume = volume;

if (player != NULL)
player->cb->set_volume(volume, session->dev, player->user_data);
@@ -4598,6 +4605,22 @@ int avrcp_set_volume(struct btd_device *dev, int8_t volume, bool notify)
avrcp_handle_set_volume, session);
}

+int8_t avrcp_get_last_volume(struct btd_device *dev)
+{
+ struct avrcp_server *server;
+ struct avrcp *session;
+
+ server = find_server(servers, device_get_adapter(dev));
+ if (server == NULL)
+ return -1;
+
+ session = find_session(server->sessions, dev);
+ if (session == NULL)
+ return -1;
+
+ return session->last_device_volume;
+}
+
struct avrcp_player *avrcp_get_target_player_by_device(struct btd_device *dev)
{
struct avrcp_server *server;
diff --git a/profiles/audio/avrcp.h b/profiles/audio/avrcp.h
index dcc580e37..952f0eea9 100644
--- a/profiles/audio/avrcp.h
+++ b/profiles/audio/avrcp.h
@@ -91,6 +91,7 @@ struct avrcp_player_cb {
};

int avrcp_set_volume(struct btd_device *dev, int8_t volume, bool notify);
+int8_t avrcp_get_last_volume(struct btd_device *dev);

struct avrcp_player *avrcp_register_player(struct btd_adapter *adapter,
struct avrcp_player_cb *cb,
diff --git a/profiles/audio/media.c b/profiles/audio/media.c
index 521902ed8..a37378393 100644
--- a/profiles/audio/media.c
+++ b/profiles/audio/media.c
@@ -494,6 +494,9 @@ static gboolean set_configuration(struct media_endpoint *endpoint,
return FALSE;

init_volume = media_player_get_device_volume(device);
+ if (init_volume < 0)
+ init_volume = avrcp_get_last_volume(device);
+
media_transport_update_volume(transport, init_volume);

msg = dbus_message_new_method_call(endpoint->sender, endpoint->path,
--
2.31.1


2021-10-11 04:50:40

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH BlueZ] avrcp: keep track of last volume, and use as transport init_volume

Hi Pauli,

On Sun, Oct 10, 2021 at 10:20 AM Pauli Virtanen <[email protected]> wrote:
>
> Some devices may send AVRCP VolumeChanged notification before AVDTP
> SetConfiguration occurs, and not send another until a hardware button on
> the device is pressed. If a media_player is registered to BlueZ, the
> volume from the event is stored on the player, and used as init_volume
> for new transports. However, if no media_player is registered,
> transports are created with volume missing.
>
> If that occurs, the DBus "Volume" attribute on transports will be
> missing until a hardware button is pressed. Consequently, applications
> cannot get or set volume, even though it is actually possible.
>
> Address this by keeping track of the last device volume set in AVRCP
> session. If no media_player is registered, use that as the init_volume
> for new transports. This has a similar effect as if a dummy media
> player was registered.
>
> This fixes AVRCP absolute volume not being available on some headphones
> on Pipewire & Pulseaudio until HW button press.
> ---
> profiles/audio/avrcp.c | 23 +++++++++++++++++++++++
> profiles/audio/avrcp.h | 1 +
> profiles/audio/media.c | 3 +++
> 3 files changed, 27 insertions(+)
>
> diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c
> index 7c280203c..0df416d2c 100644
> --- a/profiles/audio/avrcp.c
> +++ b/profiles/audio/avrcp.c
> @@ -276,6 +276,8 @@ struct avrcp {
> uint8_t transaction;
> uint8_t transaction_events[AVRCP_EVENT_LAST + 1];
> struct pending_pdu *pending_pdu;
> +
> + int8_t last_device_volume;

We can probably keep this short and just call it volume.

> };
>
> struct passthrough_handler {
> @@ -1759,6 +1761,7 @@ static uint8_t avrcp_handle_set_absolute_volume(struct avrcp *session,
> volume = pdu->params[0] & 0x7F;
>
> media_transport_update_device_volume(session->dev, volume);
> + session->last_device_volume = volume;
>
> return AVC_CTYPE_ACCEPTED;
>
> @@ -3731,6 +3734,7 @@ static void avrcp_volume_changed(struct avrcp *session,
>
> /* Always attempt to update the transport volume */
> media_transport_update_device_volume(session->dev, volume);
> + session->last_device_volume = volume;
>
> if (player)
> player->cb->set_volume(volume, session->dev, player->user_data);
> @@ -4145,6 +4149,7 @@ static void target_init(struct avrcp *session)
>
> init_volume = media_player_get_device_volume(session->dev);
> media_transport_update_device_volume(session->dev, init_volume);
> + session->last_device_volume = init_volume;
> }
>
> session->supported_events |= (1 << AVRCP_EVENT_STATUS_CHANGED) |
> @@ -4308,6 +4313,7 @@ static struct avrcp *session_create(struct avrcp_server *server,
> session->server = server;
> session->conn = avctp_connect(device);
> session->dev = device;
> + session->last_device_volume = -1;
>
> server->sessions = g_slist_append(server->sessions, session);
>
> @@ -4497,6 +4503,7 @@ static gboolean avrcp_handle_set_volume(struct avctp *conn, uint8_t code,
>
> /* Always attempt to update the transport volume */
> media_transport_update_device_volume(session->dev, volume);
> + session->last_device_volume = volume;

So if I understand this right we are going to cache the volume here
since media_transport_update_device_volume may not have a transport
yet? If that is the case we probably need to be checking if there is a
transport or not beforehand instead of doing this blindly.

>
> if (player != NULL)
> player->cb->set_volume(volume, session->dev, player->user_data);
> @@ -4598,6 +4605,22 @@ int avrcp_set_volume(struct btd_device *dev, int8_t volume, bool notify)
> avrcp_handle_set_volume, session);
> }
>
> +int8_t avrcp_get_last_volume(struct btd_device *dev)
> +{
> + struct avrcp_server *server;
> + struct avrcp *session;
> +
> + server = find_server(servers, device_get_adapter(dev));
> + if (server == NULL)
> + return -1;
> +
> + session = find_session(server->sessions, dev);
> + if (session == NULL)
> + return -1;
> +
> + return session->last_device_volume;
> +}
> +
> struct avrcp_player *avrcp_get_target_player_by_device(struct btd_device *dev)
> {
> struct avrcp_server *server;
> diff --git a/profiles/audio/avrcp.h b/profiles/audio/avrcp.h
> index dcc580e37..952f0eea9 100644
> --- a/profiles/audio/avrcp.h
> +++ b/profiles/audio/avrcp.h
> @@ -91,6 +91,7 @@ struct avrcp_player_cb {
> };
>
> int avrcp_set_volume(struct btd_device *dev, int8_t volume, bool notify);
> +int8_t avrcp_get_last_volume(struct btd_device *dev);

Let's have it as avrcp_get_volume so it is symmetric to avrcp_set_volume.

> struct avrcp_player *avrcp_register_player(struct btd_adapter *adapter,
> struct avrcp_player_cb *cb,
> diff --git a/profiles/audio/media.c b/profiles/audio/media.c
> index 521902ed8..a37378393 100644
> --- a/profiles/audio/media.c
> +++ b/profiles/audio/media.c
> @@ -494,6 +494,9 @@ static gboolean set_configuration(struct media_endpoint *endpoint,
> return FALSE;
>
> init_volume = media_player_get_device_volume(device);
> + if (init_volume < 0)
> + init_volume = avrcp_get_last_volume(device);

I wonder if we shouldn't be better to move the call to
avrcp_get_volume inside media_player_get_device_volume so it does the
fallback automatically if there is no volume set.

> media_transport_update_volume(transport, init_volume);
>
> msg = dbus_message_new_method_call(endpoint->sender, endpoint->path,
> --
> 2.31.1
>


--
Luiz Augusto von Dentz

2021-10-12 20:45:12

by Pauli Virtanen

[permalink] [raw]
Subject: Re: [PATCH BlueZ] avrcp: keep track of last volume, and use as transport init_volume

Hi Luiz,

Thanks,

su, 2021-10-10 kello 21:26 -0700, Luiz Augusto von Dentz kirjoitti:
> Hi Pauli,
>
> On Sun, Oct 10, 2021 at 10:20 AM Pauli Virtanen <[email protected]> wrote:
> >
> > Some devices may send AVRCP VolumeChanged notification before AVDTP
> > SetConfiguration occurs, and not send another until a hardware button on
> > the device is pressed. If a media_player is registered to BlueZ, the
> > volume from the event is stored on the player, and used as init_volume
> > for new transports. However, if no media_player is registered,
> > transports are created with volume missing.
> >
> > If that occurs, the DBus "Volume" attribute on transports will be
> > missing until a hardware button is pressed. Consequently, applications
> > cannot get or set volume, even though it is actually possible.
> >
> > Address this by keeping track of the last device volume set in AVRCP
> > session. If no media_player is registered, use that as the init_volume
> > for new transports. This has a similar effect as if a dummy media
> > player was registered.
> >
> > This fixes AVRCP absolute volume not being available on some headphones
> > on Pipewire & Pulseaudio until HW button press.
> > ---
> > profiles/audio/avrcp.c | 23 +++++++++++++++++++++++
> > profiles/audio/avrcp.h | 1 +
> > profiles/audio/media.c | 3 +++
> > 3 files changed, 27 insertions(+)
> >
> > diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c
> > index 7c280203c..0df416d2c 100644
> > --- a/profiles/audio/avrcp.c
> > +++ b/profiles/audio/avrcp.c
> > @@ -276,6 +276,8 @@ struct avrcp {
> > uint8_t transaction;
> > uint8_t transaction_events[AVRCP_EVENT_LAST + 1];
> > struct pending_pdu *pending_pdu;
> > +
> > + int8_t last_device_volume;
>
> We can probably keep this short and just call it volume.

Ok.

> >  };
> >
> >  struct passthrough_handler {
> > @@ -1759,6 +1761,7 @@ static uint8_t avrcp_handle_set_absolute_volume(struct avrcp *session,
> >         volume = pdu->params[0] & 0x7F;
> >
> >         media_transport_update_device_volume(session->dev, volume);
> > + session->last_device_volume = volume;
> >
> >         return AVC_CTYPE_ACCEPTED;
> >
> > @@ -3731,6 +3734,7 @@ static void avrcp_volume_changed(struct avrcp *session,
> >
> >         /* Always attempt to update the transport volume */
> >         media_transport_update_device_volume(session->dev, volume);
> > + session->last_device_volume = volume;
> >
> >         if (player)
> >                 player->cb->set_volume(volume, session->dev, player->user_data);
> > @@ -4145,6 +4149,7 @@ static void target_init(struct avrcp *session)
> >
> >                 init_volume = media_player_get_device_volume(session->dev);
> >                 media_transport_update_device_volume(session->dev, init_volume);
> > + session->last_device_volume = init_volume;
> >         }
> >
> >         session->supported_events |= (1 << AVRCP_EVENT_STATUS_CHANGED) |
> > @@ -4308,6 +4313,7 @@ static struct avrcp *session_create(struct avrcp_server *server,
> >         session->server = server;
> >         session->conn = avctp_connect(device);
> >         session->dev = device;
> > + session->last_device_volume = -1;
> >
> >         server->sessions = g_slist_append(server->sessions, session);
> >
> > @@ -4497,6 +4503,7 @@ static gboolean avrcp_handle_set_volume(struct avctp *conn, uint8_t code,
> >
> >         /* Always attempt to update the transport volume */
> >         media_transport_update_device_volume(session->dev, volume);
> > + session->last_device_volume = volume;
>
> So if I understand this right we are going to cache the volume here
> since media_transport_update_device_volume may not have a transport
> yet? If that is the case we probably need to be checking if there is a
> transport or not beforehand instead of doing this blindly.

Yes, so that avrcp volume received previously is exhibited in
transports created next. I now see that this is the same issue as in
4b6153b0501c ("audio/transport: supply volume on transport init"), but
in the case when there are no local players registered, and the fix
there does not work.

Currently, if there is a local player registered, the current volume is
cached in the player volume whether there are transports or not.
Caching always in struct avrcp would then make the zero-players case
the same. I don't immediately see something incorrect.

Albeit, this probably should be only done for BlueZ as CT, so shouldn't
cache in avrcp_handle_set_absolute_volume. That could be a minimal v2?

***

There's also something confusing in the code in current master
branch: why are avrcp_handle_set_volume and avrcp_volume_changed
setting the volume on local target->player, which I thought is a BlueZ
as TG thing?

I see that df7d3fa50023 ("audio/avrcp: Always update transport volume
regardless of player") moved most of what player->set_volume does to
the callbacks, and the player volumes seem to be currently used only to
provide initial volume for transports. The volume probably should be
stored elsewhere than in local players?

In fact, it causes problems because there's just a single player that
has long lifetime: BlueZ sink/TG use as the initial volume, the volume
of a previously connected different remote device sink/TG. Also, having
multiple remote device TG/sink makes the initial volumes conflict. So
there seems to be something to fix (separate patch?).

Caching in struct avrcp session doesn't have these problems, but
df7d3fa50023 mentions there's some issue with avrcp session appearing
late. I'll need to think a bit how to fix this.

> >
> > if (player != NULL)
> > player->cb->set_volume(volume, session->dev, player->user_data);
> > @@ -4598,6 +4605,22 @@ int avrcp_set_volume(struct btd_device *dev, int8_t volume, bool notify)
> > avrcp_handle_set_volume, session);
> > }
> >
> > +int8_t avrcp_get_last_volume(struct btd_device *dev)
> > +{
> > + struct avrcp_server *server;
> > + struct avrcp *session;
> > +
> > + server = find_server(servers, device_get_adapter(dev));
> > + if (server == NULL)
> > + return -1;
> > +
> > + session = find_session(server->sessions, dev);
> > + if (session == NULL)
> > + return -1;
> > +
> > + return session->last_device_volume;
> > +}
> > +
> > struct avrcp_player *avrcp_get_target_player_by_device(struct btd_device *dev)
> > {
> > struct avrcp_server *server;
> > diff --git a/profiles/audio/avrcp.h b/profiles/audio/avrcp.h
> > index dcc580e37..952f0eea9 100644
> > --- a/profiles/audio/avrcp.h
> > +++ b/profiles/audio/avrcp.h
> > @@ -91,6 +91,7 @@ struct avrcp_player_cb {
> > };
> >
> > int avrcp_set_volume(struct btd_device *dev, int8_t volume, bool notify);
> > +int8_t avrcp_get_last_volume(struct btd_device *dev);
>
> Let's have it as avrcp_get_volume so it is symmetric to avrcp_set_volume.

Ok.

> >  struct avrcp_player *avrcp_register_player(struct btd_adapter
> > *adapter,
> >                                                 struct
> > avrcp_player_cb *cb,
> > diff --git a/profiles/audio/media.c b/profiles/audio/media.c
> > index 521902ed8..a37378393 100644
> > --- a/profiles/audio/media.c
> > +++ b/profiles/audio/media.c
> > @@ -494,6 +494,9 @@ static gboolean set_configuration(struct
> > media_endpoint *endpoint,
> >                 return FALSE;
> >
> >         init_volume = media_player_get_device_volume(device);
> > + if (init_volume < 0)
> > + init_volume = avrcp_get_last_volume(device);
>
> I wonder if we shouldn't be better to move the call to
> avrcp_get_volume inside media_player_get_device_volume so it does the
> fallback automatically if there is no volume set.

Ok.

> >         media_transport_update_volume(transport, init_volume);
> >
> >         msg = dbus_message_new_method_call(endpoint->sender,
> > endpoint->path,
> > --
> > 2.31.1
> >
>
>





2021-10-12 21:53:28

by Marijn Suijten

[permalink] [raw]
Subject: Re: [PATCH BlueZ] avrcp: keep track of last volume, and use as transport init_volume

Hi Pauli,

On 2021-10-12 20:35:17, Pauli Virtanen wrote:
> [..]
> There's also something confusing in the code in current master
> branch:?why are avrcp_handle_set_volume and avrcp_volume_changed
> setting the volume on local?target->player, which I thought is a BlueZ
> as TG thing?

If I remember correctly you can also have a player with BlueZ as CT
which is basically a passthrough representing the "actual player" on the
other end of the connection?

> I see that df7d3fa50023 ("audio/avrcp: Always update transport volume
> regardless of player") moved most of what player->set_volume does to
> the callbacks, and the player volumes seem to be currently used only to
> provide initial volume for transports. The volume probably should be
> stored elsewhere than in local players?

Going off of memory without looking at the codebase, it probably makes
sense to move volume out of the players and into the device entirely as
a single source-of-truth representing the currently known volume value
of the peer. That might solve these issues (ie. no player available in
the linked commit) and get rid of a bunch of edgecases? That's probably
overlooking some major dependencies though, but it'd be great if you
could look into that direction.

In addition I don't know if AVRCP volume is "local" to an entire device
or just a single transport "within" that device.

> [..]
> Caching in struct avrcp session doesn't have these problems, but
> df7d3fa50023 mentions there's some issue with avrcp session appearing
> late. I'll need to think a bit how to fix this.

I don't remember there being no avrcp session yet, but you're probably
referring to 4b6153b0501c ("audio/transport: supply volume on transport
init") instead?

- Marijn

2021-10-13 15:29:51

by Pauli Virtanen

[permalink] [raw]
Subject: Re: [PATCH BlueZ] avrcp: keep track of last volume, and use as transport init_volume

Hi Marijn,

ti, 2021-10-12 kello 23:51 +0200, Marijn Suijten kirjoitti:
>
> On 2021-10-12 20:35:17, Pauli Virtanen wrote:
> > [..]
> > There's also something confusing in the code in current master
> > branch: why are avrcp_handle_set_volume and avrcp_volume_changed
> > setting the volume on local target->player, which I thought is a BlueZ
> > as TG thing?
>
> If I remember correctly you can also have a player with BlueZ as CT
> which is basically a passthrough representing the "actual player" on the
> other end of the connection?

In these routines (which are called on RegisterNotification response,
and SetAbsoluteVolume response, so BlueZ as CT), the player is
from target_get_player, so it's one of the RegisterPlayer() players for
Bluez as TG.

There's indeed a separate set of "proxy" players in 
session->controller->players, but their avrcp_player_cb is NULL and
don't have set_volume defined or appear as TG players.

> > I see that df7d3fa50023 ("audio/avrcp: Always update transport volume
> > regardless of player") moved most of what player->set_volume does to
> > the callbacks, and the player volumes seem to be currently used only to
> > provide initial volume for transports. The volume probably should be
> > stored elsewhere than in local players?
>
> Going off of memory without looking at the codebase, it probably makes
> sense to move volume out of the players and into the device entirely as
> a single source-of-truth representing the currently known volume value
> of the peer.  That might solve these issues (ie. no player available in
> the linked commit) and get rid of a bunch of edgecases?  That's probably
> overlooking some major dependencies though, but it'd be great if you
> could look into that direction.
>
> In addition I don't know if AVRCP volume is "local" to an entire device
> or just a single transport "within" that device.

From my current reading of the spec, there's no correspondence to audio
transports. One might understand it so that volume is specific to a
certain player on TG, but as TG is supposedly sink, it's sounds like it
intended to be a single device volume.

> > [..]
> > Caching in struct avrcp session doesn't have these problems, but
> > df7d3fa50023 mentions there's some issue with avrcp session appearing
> > late. I'll need to think a bit how to fix this.
>
> I don't remember there being no avrcp session yet, but you're probably
> referring to 4b6153b0501c ("audio/transport: supply volume on transport
> init") instead?

Sorry, yes, I meant 4b6153b0501c.

Best,
Pauli