From f3badce08cf0a444724bd3ae3104b480cb83bad0 Mon Sep 17 00:00:00 2001 From: borine <32966433+borine@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:19:59 +0100 Subject: [PATCH] Add delay-adjustment command to bluealsa-cli --- doc/bluealsa-cli.1.rst | 17 +++++- misc/bash-completion/bluealsa | 2 +- utils/cli/Makefile.am | 1 + utils/cli/cli.c | 3 + utils/cli/cmd-delay-adjustment.c | 99 ++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 utils/cli/cmd-delay-adjustment.c diff --git a/doc/bluealsa-cli.1.rst b/doc/bluealsa-cli.1.rst index e2abaff74..53a2fe283 100644 --- a/doc/bluealsa-cli.1.rst +++ b/doc/bluealsa-cli.1.rst @@ -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$ @@ -61,7 +61,7 @@ status :: Service: org.bluealsa - Version: v3.1.0 + Version: v4.1.1 Adapters: hci0 hci1 Profiles: A2DP-source : SBC AAC @@ -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 diff --git a/misc/bash-completion/bluealsa b/misc/bash-completion/bluealsa index 8e49ee67c..9d2a12469 100644 --- a/misc/bash-completion/bluealsa +++ b/misc/bash-completion/bluealsa @@ -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" diff --git a/utils/cli/Makefile.am b/utils/cli/Makefile.am index e63148794..0c3fc451f 100644 --- a/utils/cli/Makefile.am +++ b/utils/cli/Makefile.am @@ -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 \ diff --git a/utils/cli/cli.c b/utils/cli/cli.c index d480ef113..f868caf14 100644 --- a/utils/cli/cli.c +++ b/utils/cli/cli.c @@ -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); @@ -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; @@ -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, diff --git a/utils/cli/cmd-delay-adjustment.c b/utils/cli/cmd-delay-adjustment.c new file mode 100644 index 000000000..36a4ae9ae --- /dev/null +++ b/utils/cli/cmd-delay-adjustment.c @@ -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 +#include +#include +#include +#include + +#include + +#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, +};