Skip to content

Commit

Permalink
Add delay-adjustment command to bluealsa-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
borine committed Jul 21, 2023
1 parent cee535d commit f3badce
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 3 deletions.
17 changes: 15 additions & 2 deletions doc/bluealsa-cli.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ bluealsa-cli
a simple command line interface for the BlueALSA D-Bus API
----------------------------------------------------------

:Date: January 2023
:Date: July 2023
:Manual section: 1
:Manual group: General Commands Manual
:Version: $VERSION$
Expand Down Expand Up @@ -61,7 +61,7 @@ status
::

Service: org.bluealsa
Version: v3.1.0
Version: v4.1.1
Adapters: hci0 hci1
Profiles:
A2DP-source : SBC AAC
Expand Down Expand Up @@ -148,6 +148,19 @@ soft-volume *PCM_PATH* [*STATE*]
for soft-volume on, or **off**, **no**, **false**, **n** or **0** for
soft-volume off.

delay-adjustment *PCM_PATH* [*ADJUSTMENT*]
Get or set the DelayAdjustment property of the given PCM for the current
codec.

If the *ADJUSTMENT* argument is given, set the DelayAdjustment property for
the current codec in the given PCM. This property may be used by clients to
adjust the reported audio delay and may be useful with PCM devices that do
not report an accurate Delay property.

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

monitor [-p[PROPS] | --properties[=PROPS]]
Listen for D-Bus signals indicating adding/removing BlueALSA interfaces.
Also detect service running and service stopped events, and optionally
Expand Down
2 changes: 1 addition & 1 deletion misc/bash-completion/bluealsa
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ _bluealsa_cli() {

# the command names supported by this version of bluealsa-cli
local simple_commands="list-pcms list-services monitor status"
local path_commands="codec info mute open soft-volume volume"
local path_commands="codec delay-adjustment info mute open soft-volume volume"

# options that may appear before or after the command
local global_shortopts="-h"
Expand Down
1 change: 1 addition & 0 deletions utils/cli/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bluealsa_cli_SOURCES = \
../../src/shared/hex.c \
../../src/shared/log.c \
cmd-codec.c \
cmd-delay-adjustment.c \
cmd-info.c \
cmd-list-pcms.c \
cmd-list-services.c \
Expand Down
3 changes: 3 additions & 0 deletions utils/cli/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ void cli_print_pcm_properties(const struct ba_pcm *pcm, DBusError *err) {
cli_print_pcm_available_codecs(pcm, err);
cli_print_pcm_selected_codec(pcm);
printf("Delay: %#.1f ms\n", (double)pcm->delay / 10);
printf("DelayAdjustment: %#.1f ms\n", (double)pcm->delay_adjustment / 10);
cli_print_pcm_soft_volume(pcm);
cli_print_pcm_volume(pcm);
cli_print_pcm_mute(pcm);
Expand All @@ -306,6 +307,7 @@ extern const struct cli_command cmd_list_pcms;
extern const struct cli_command cmd_status;
extern const struct cli_command cmd_info;
extern const struct cli_command cmd_codec;
extern const struct cli_command cmd_delay_adjustment;
extern const struct cli_command cmd_monitor;
extern const struct cli_command cmd_mute;
extern const struct cli_command cmd_open;
Expand All @@ -318,6 +320,7 @@ static const struct cli_command *commands[] = {
&cmd_status,
&cmd_info,
&cmd_codec,
&cmd_delay_adjustment,
&cmd_volume,
&cmd_mute,
&cmd_softvol,
Expand Down
99 changes: 99 additions & 0 deletions utils/cli/cmd-delay-adjustment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* BlueALSA - cmd-delay-adjustment.c
* Copyright (c) 2016-2023 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
* This project is licensed under the terms of the MIT license.
*
*/

#include <errno.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <dbus/dbus.h>

#include "cli.h"
#include "shared/dbus-client.h"

static void usage(const char *command) {
printf("Get or set the DelayAdjustment property of the given PCM.\n\n");
cli_print_usage("%s [OPTION]... PCM-PATH [STATE]", command);
printf("\nOptions:\n"
" -h, --help\t\tShow this message and exit\n"
"\nPositional arguments:\n"
" PCM-PATH\tBlueALSA PCM D-Bus object path\n"
" ADJUSTMENT\t\tAdjustment value (+/-), in milliseconds)\n"
);
}

static int cmd_delay_adjustment_func(int argc, char *argv[]) {

int opt;
const char *opts = "+h";
const struct option longopts[] = {
{ "help", no_argument, NULL, 'h' },
{ 0 },
};

opterr = 0;
while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1)
if (opt == 'h') { /* --help */
usage(argv[0]);
return EXIT_SUCCESS;
}

if (argc - optind < 1) {
cmd_print_error("Missing BlueALSA PCM path argument");
return EXIT_FAILURE;
}
if (argc - optind > 2) {
cmd_print_error("Invalid number of arguments");
return EXIT_FAILURE;
}

DBusError err = DBUS_ERROR_INIT;
const char *path = argv[optind];

struct ba_pcm pcm;
if (!cli_get_ba_pcm(path, &pcm, &err)) {
cmd_print_error("Couldn't get BlueALSA PCM: %s", err.message);
return EXIT_FAILURE;
}

if (argc == 2) {
printf("DelayAdjustment: %#.1f ms\n", (double)pcm.delay_adjustment/ 10);
return EXIT_SUCCESS;
}

const char *value = argv[optind + 1];
errno = 0;
char *endptr = NULL;
double dbl = strtod(value, &endptr);
if (endptr == value || errno == ERANGE) {
cmd_print_error("Invalid argument: %s", value);
return EXIT_FAILURE;
}

int adjustment = (int) (dbl * 10.0);
if (adjustment < INT16_MIN || adjustment > INT16_MAX) {
cmd_print_error("Invalid argument: %s", value);
return EXIT_FAILURE;
}

if (!bluealsa_dbus_pcm_set_delay_adjustment(&config.dbus, pcm.pcm_path, pcm.codec.name, adjustment, &err)) {
cmd_print_error("DelayAdjustment update failed: %s", err.message);
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

const struct cli_command cmd_delay_adjustment = {
"delay-adjustment",
"Get or set PCM DelayAdjustment property",
cmd_delay_adjustment_func,
};

0 comments on commit f3badce

Please sign in to comment.