Skip to content

Commit f40fe1c

Browse files
committed
Read-write client delay instead of adjustments
1 parent d19da25 commit f40fe1c

22 files changed

+161
-284
lines changed

doc/bluealsa-plugins.7.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ CTL Parameters
429429

430430
EXT
431431
Causes the plugin to include extra controls. These are the controls for
432-
Bluetooth codec selection, volume mode selection, delay adjustment (sync)
432+
Bluetooth codec selection, volume mode selection, client delay (sync)
433433
and/or battery level indicator.
434434
If the value is **yes** then all of these additional controls are included;
435435
if the value is **no** then none of them are included. The default is
@@ -452,13 +452,13 @@ CTL Parameters
452452
See the `Volume control` section in the ``bluealsad(8)`` for more
453453
information on the software volume setting.
454454

455-
The delay adjustment controls are called "Sync". They can be used to apply
455+
The client delay controls are called "Sync". They can be used to apply
456456
a fixed adjustment to the delay reported by the associated PCM to the
457457
application, and may be useful with applications that need to synchronize
458458
the bluetooth audio stream with some some other stream, such as a video.
459459
The values are in milliseconds from ``-3275 ms`` to ``+3275 ms`` in steps
460460
of ``25 ms``. The playback control has index 0 and the capture control has
461-
index 1. Each codec supported by a PCM has its own delay adjustment value.
461+
index 1. Each codec supported by a PCM has its own client delay value.
462462
Note that this control changes only the delay value reported to the
463463
application by ALSA, it does not affect the actual delay (latency) of the
464464
PCM stream. Values set by this control type are saved in the BlueALSA

doc/bluealsactl.1.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,15 @@ soft-volume *PCM_PATH* [*STATE*]
195195
for soft-volume on, or **off**, **no**, **false**, **n** or **0** for
196196
soft-volume off.
197197

198-
delay-adjustment *PCM_PATH* [*ADJUSTMENT*]
199-
Get or set the DelayAdjustment property of the given PCM for the current
200-
codec.
198+
client-delay *PCM_PATH* [[-]\ *DELAY*]
199+
Get or set the ClientDelay property of the given PCM.
201200

202-
If the *ADJUSTMENT* argument is given, set the DelayAdjustment property for
203-
the current codec in the given PCM. This property may be used by clients to
201+
If the *DELAY* argument is given, set the ClientDelay property for the
202+
given PCM. This property may be used by clients to
204203
adjust the reported audio delay and may be useful with PCM devices that do
205204
not report an accurate Delay property.
206205

207-
The *ADJUSTMENT* value is in milliseconds and must be a decimal number with
206+
The *DELAY* value is in milliseconds and must be a decimal number with
208207
optional sign prefix (e.g. **250**, **-500**, **+360.4**). The permitted
209208
range is [-3276.8, 3276.7].
210209

doc/org.bluealsa.PCM1.7.rst

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,6 @@ void SelectCodec(string codec, dict props)
8080
dbus.Error.NotSupported
8181
dbus.Error.Failed
8282

83-
void SetDelayAdjustment(string codec, int16 adjustment)
84-
Set an arbitrary adjustment (+/-) to the reported Delay in 1/10 of
85-
millisecond for a specific codec. This adjustment is applied to the Delay
86-
property when that codec is selected, and can be used to compensate for
87-
devices that do not report accurate Delay values.
88-
89-
Possible Errors:
90-
::
91-
92-
dbus.Error.InvalidArguments
93-
94-
array{string, int16} GetDelayAdjustments()
95-
Return the array of currently set delay adjustments. Each entry of the
96-
array gives the name of a codec and the adjustment that the PCM will apply
97-
to the Delay property when that codec is selected.
98-
9983
Properties
10084
----------
10185

@@ -165,10 +149,14 @@ array{byte} CodecConfiguration [readonly]
165149
uint16 Delay [readonly]
166150
Approximate PCM delay in 1/10 of millisecond.
167151

168-
int16 DelayAdjustment [readonly]
169-
An adjustment (+/-) included within the reported Delay in 1/10 of
170-
millisecond to compensate for devices that do not report accurate delay
171-
values.
152+
int16 ClientDelay [readwrite]
153+
Positive (or negative) client side delay in 1/10 of millisecond.
154+
155+
This property shall be set by the client in order to account for the client
156+
side delay. In case of PCM source it shall be set to a value reported by a
157+
playback subsystem to account for playback delay. In case of PCM sink it
158+
can be used to adjust the Delay property to compensate for devices that do
159+
not report accurate delay values.
172160

173161
boolean SoftVolume [readwrite]
174162
This property determines whether BlueALSA will make volume control

misc/bash-completion/bluealsad

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ _bluealsactl() {
291291

292292
# the command names supported by this version of bluealsactl
293293
local simple_commands="list-pcms list-services monitor status"
294-
local path_commands="codec delay-adjustment info mute open soft-volume volume"
294+
local path_commands="codec client-delay info mute open soft-volume volume"
295295

296296
# options that may appear before or after the command
297297
local global_shortopts="-h"

src/asound/bluealsa-ctl.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ struct bluealsa_ctl {
131131
bool show_codec;
132132
/* if true, show volume mode control */
133133
bool show_vol_mode;
134-
/* if true, show delay adjustment sync control */
134+
/* if true, show client delay sync control */
135135
bool show_delay_sync;
136136
/* if true, show battery level indicator */
137137
bool show_battery;
@@ -707,7 +707,7 @@ static size_t bluealsa_elem_list_add_pcm_elems(struct bluealsa_ctl *ctl,
707707
n++;
708708
}
709709

710-
/* add special delay adjustment "sync" element */
710+
/* add special client delay "sync" element */
711711
if (ctl->show_delay_sync) {
712712
elem_list[n].type = CTL_ELEM_TYPE_DELAY_SYNC;
713713
elem_list[n].dev = dev;
@@ -1243,7 +1243,7 @@ static int bluealsa_read_enumerated(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
12431243
items[0] = pcm->soft_volume ? 1 : 0;
12441244
break;
12451245
case CTL_ELEM_TYPE_DELAY_SYNC:
1246-
items[0] = DIV_ROUND(pcm->delay_adjustment - INT16_MIN, DELAY_SYNC_STEP);
1246+
items[0] = DIV_ROUND(pcm->client_delay - INT16_MIN, DELAY_SYNC_STEP);
12471247
break;
12481248
case CTL_ELEM_TYPE_BATTERY:
12491249
case CTL_ELEM_TYPE_SWITCH:
@@ -1296,16 +1296,16 @@ static int bluealsa_write_enumerated(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
12961296
return 0;
12971297
pcm->soft_volume = soft_volume;
12981298
if (!ba_dbus_pcm_update(&ctl->dbus_ctx, pcm, BLUEALSA_PCM_SOFT_VOLUME, NULL))
1299-
return -ENOMEM;
1299+
return -EIO;
13001300
break;
13011301
case CTL_ELEM_TYPE_DELAY_SYNC:
13021302
if (items[0] >= DELAY_SYNC_NUM_VALUES)
13031303
return -EINVAL;
1304-
const int16_t delay_adjustment = items[0] * DELAY_SYNC_STEP + DELAY_SYNC_MIN_VALUE;
1305-
if (pcm->delay_adjustment == delay_adjustment)
1304+
const int16_t delay = items[0] * DELAY_SYNC_STEP + DELAY_SYNC_MIN_VALUE;
1305+
if (pcm->client_delay == delay)
13061306
return 0;
1307-
if (!ba_dbus_pcm_set_delay_adjustment(&ctl->dbus_ctx, pcm->pcm_path,
1308-
pcm->codec.name, delay_adjustment, NULL))
1307+
pcm->client_delay = delay;
1308+
if (!ba_dbus_pcm_update(&ctl->dbus_ctx, pcm, BLUEALSA_PCM_CLIENT_DELAY, NULL))
13091309
return -EIO;
13101310
process_events(&ctl->ext);
13111311
break;

src/asound/bluealsa-pcm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,8 @@ static snd_pcm_sframes_t bluealsa_calculate_delay(snd_pcm_ioplug_t *io) {
934934

935935
/* data transfer (communication) and encoding/decoding */
936936
delay += (io->rate / 100) * pcm->ba_pcm.delay / 100;
937+
/* additional delay specified by the client */
938+
delay += (io->rate / 100) * pcm->ba_pcm.client_delay / 100;
937939

938940
delay += pcm->delay_ex;
939941

src/ba-transport-pcm.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,12 @@ int transport_pcm_init(
8585

8686
pthread_mutex_init(&pcm->mutex, NULL);
8787
pthread_mutex_init(&pcm->state_mtx, NULL);
88-
pthread_mutex_init(&pcm->delay_adjustments_mtx, NULL);
8988
pthread_mutex_init(&pcm->client_mtx, NULL);
9089
pthread_cond_init(&pcm->cond, NULL);
9190

9291
if (pipe(pcm->pipe) == -1)
9392
return -1;
9493

95-
pcm->delay_adjustments = g_hash_table_new(NULL, NULL);
96-
9794
pcm->ba_dbus_path = g_strdup_printf("%s/%s/%s",
9895
t->d->ba_dbus_path, transport_get_dbus_path_type(t->profile),
9996
mode == BA_TRANSPORT_PCM_MODE_SOURCE ? "source" : "sink");
@@ -110,7 +107,6 @@ void transport_pcm_free(
110107

111108
pthread_mutex_destroy(&pcm->mutex);
112109
pthread_mutex_destroy(&pcm->state_mtx);
113-
pthread_mutex_destroy(&pcm->delay_adjustments_mtx);
114110
pthread_mutex_destroy(&pcm->client_mtx);
115111
pthread_cond_destroy(&pcm->cond);
116112

@@ -119,7 +115,6 @@ void transport_pcm_free(
119115
if (pcm->pipe[1] != -1)
120116
close(pcm->pipe[1]);
121117

122-
g_hash_table_unref(pcm->delay_adjustments);
123118
g_free(pcm->ba_dbus_path);
124119

125120
}
@@ -726,7 +721,6 @@ int ba_transport_pcm_get_delay(const struct ba_transport_pcm *pcm) {
726721
const struct ba_transport *t = pcm->t;
727722

728723
int delay = pcm->codec_delay_dms + pcm->processing_delay_dms;
729-
delay += ba_transport_pcm_delay_adjustment_get(pcm);
730724

731725
if (t->profile & BA_TRANSPORT_PROFILE_MASK_A2DP)
732726
delay += t->a2dp.delay;
@@ -736,33 +730,6 @@ int ba_transport_pcm_get_delay(const struct ba_transport_pcm *pcm) {
736730
return delay;
737731
}
738732

739-
int16_t ba_transport_pcm_delay_adjustment_get(
740-
const struct ba_transport_pcm *pcm) {
741-
742-
struct ba_transport *t = pcm->t;
743-
uint32_t codec_id = ba_transport_get_codec(t);
744-
int16_t adjustment = 0;
745-
746-
pthread_mutex_lock(MUTABLE(&pcm->delay_adjustments_mtx));
747-
void *val = g_hash_table_lookup(pcm->delay_adjustments, GINT_TO_POINTER(codec_id));
748-
pthread_mutex_unlock(MUTABLE(&pcm->delay_adjustments_mtx));
749-
750-
if (val != NULL)
751-
adjustment = GPOINTER_TO_INT(val);
752-
753-
return adjustment;
754-
}
755-
756-
void ba_transport_pcm_delay_adjustment_set(
757-
struct ba_transport_pcm *pcm,
758-
uint32_t codec_id,
759-
int16_t adjustment) {
760-
pthread_mutex_lock(&pcm->delay_adjustments_mtx);
761-
g_hash_table_insert(pcm->delay_adjustments,
762-
GINT_TO_POINTER(codec_id), GINT_TO_POINTER(adjustment));
763-
pthread_mutex_unlock(&pcm->delay_adjustments_mtx);
764-
}
765-
766733
const char *ba_transport_pcm_channel_to_string(
767734
enum ba_transport_pcm_channel channel) {
768735
switch (channel) {

src/ba-transport-pcm.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,8 @@ struct ba_transport_pcm {
129129
* the host computational power. It is used to compensate for the time
130130
* required to encode or decode audio. */
131131
unsigned int processing_delay_dms;
132-
133-
/* guard delay adjustments access */
134-
pthread_mutex_t delay_adjustments_mtx;
135-
/* PCM delay adjustments in 1/10 of millisecond, set by client API to allow
136-
* user correction of delay reporting inaccuracy. */
137-
GHashTable *delay_adjustments;
132+
/* Positive (or negative) delay reported by the client. */
133+
int client_delay_dms;
138134

139135
/* indicates whether FIFO buffer was synchronized */
140136
bool synced;
@@ -260,13 +256,6 @@ int ba_transport_pcm_get_hardware_volume(
260256
int ba_transport_pcm_get_delay(
261257
const struct ba_transport_pcm *pcm);
262258

263-
int16_t ba_transport_pcm_delay_adjustment_get(
264-
const struct ba_transport_pcm *pcm);
265-
void ba_transport_pcm_delay_adjustment_set(
266-
struct ba_transport_pcm *pcm,
267-
uint32_t codec_id,
268-
int16_t adjustment);
269-
270259
const char *ba_transport_pcm_channel_to_string(
271260
enum ba_transport_pcm_channel channel);
272261

src/ba-transport.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,12 @@ void ba_transport_unref(struct ba_transport *t) {
816816
if (ref_count > 0)
817817
return;
818818

819+
debug("Freeing transport: %s", ba_transport_debug_name(t));
820+
g_assert_cmpint(ref_count, ==, 0);
821+
822+
if (t->bt_fd != -1)
823+
close(t->bt_fd);
824+
819825
if (t->profile & BA_TRANSPORT_PROFILE_MASK_A2DP) {
820826
storage_pcm_data_update(&t->a2dp.pcm);
821827
storage_pcm_data_update(&t->a2dp.pcm_bc);
@@ -825,12 +831,6 @@ void ba_transport_unref(struct ba_transport *t) {
825831
storage_pcm_data_update(&t->sco.pcm_mic);
826832
}
827833

828-
debug("Freeing transport: %s", ba_transport_debug_name(t));
829-
g_assert_cmpint(ref_count, ==, 0);
830-
831-
if (t->bt_fd != -1)
832-
close(t->bt_fd);
833-
834834
ba_device_unref(d);
835835

836836
#if DEBUG

0 commit comments

Comments
 (0)