serenity/Userland/Utilities/asctl.cpp

166 lines
6.7 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
* Copyright (c) 2021, David Isaksson <davidisaksson93@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Variant.h>
#include <AK/Vector.h>
2023-06-24 10:56:51 +00:00
#include <LibAudio/ConnectionToManagerServer.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
2021-11-27 16:00:19 +00:00
#include <LibCore/System.h>
2021-11-25 21:29:12 +00:00
#include <LibMain/Main.h>
#include <math.h>
#include <stdio.h>
#include <sys/ioctl.h>
enum AudioVariable : u32 {
Volume,
Mute,
SampleRate
};
// asctl: audio server control utility
2021-11-25 21:29:12 +00:00
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
Core::EventLoop loop;
2023-06-24 10:56:51 +00:00
auto audio_client = TRY(Audio::ConnectionToManagerServer::try_create());
2023-03-10 14:52:02 +00:00
StringView command;
2021-11-25 21:29:12 +00:00
Vector<StringView> command_arguments;
bool human_mode = false;
Core::ArgsParser args_parser;
args_parser.set_general_help("Send control signals to the audio server and hardware.");
args_parser.add_option(human_mode, "Print human-readable output", "human-readable", 'h');
args_parser.add_positional_argument(command, "Command, either (g)et or (s)et\n\n\tThe get command accepts a list of variables to print.\n\tThey are printed in the given order.\n\tIf no value is specified, all are printed.\n\n\tThe set command accepts a any number of variables\n\tfollowed by the value they should be set to.\n\n\tPossible variables are (v)olume, (m)ute, sample(r)ate.\n", "command");
2021-11-25 21:29:12 +00:00
args_parser.add_positional_argument(command_arguments, "Arguments for the command", "args", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
2021-11-27 16:00:19 +00:00
TRY(Core::System::unveil(nullptr, nullptr));
LibAudio+Userland: Use new audio queue in client-server communication Previously, we were sending Buffers to the server whenever we had new audio data for it. This meant that for every audio enqueue action, we needed to create a new shared memory anonymous buffer, send that buffer's file descriptor over IPC (+recfd on the other side) and then map the buffer into the audio server's memory to be able to play it. This was fine for sending large chunks of audio data, like when playing existing audio files. However, in the future we want to move to real-time audio in some applications like Piano. This means that the size of buffers that are sent need to be very small, as just the size of a buffer itself is part of the audio latency. If we were to try real-time audio with the existing system, we would run into problems really quickly. Dealing with a continuous stream of new anonymous files like the current audio system is rather expensive, as we need Kernel help in multiple places. Additionally, every enqueue incurs an IPC call, which are not optimized for >1000 calls/second (which would be needed for real-time audio with buffer sizes of ~40 samples). So a fundamental change in how we handle audio sending in userspace is necessary. This commit moves the audio sending system onto a shared single producer circular queue (SSPCQ) (introduced with one of the previous commits). This queue is intended to live in shared memory and be accessed by multiple processes at the same time. It was specifically written to support the audio sending case, so e.g. it only supports a single producer (the audio client). Now, audio sending follows these general steps: - The audio client connects to the audio server. - The audio client creates a SSPCQ in shared memory. - The audio client sends the SSPCQ's file descriptor to the audio server with the set_buffer() IPC call. - The audio server receives the SSPCQ and maps it. - The audio client signals start of playback with start_playback(). - At the same time: - The audio client writes its audio data into the shared-memory queue. - The audio server reads audio data from the shared-memory queue(s). Both sides have additional before-queue/after-queue buffers, depending on the exact application. - Pausing playback is just an IPC call, nothing happens to the buffer except that the server stops reading from it until playback is resumed. - Muting has nothing to do with whether audio data is read or not. - When the connection closes, the queues are unmapped on both sides. This should already improve audio playback performance in a bunch of places. Implementation & commit notes: - Audio loaders don't create LegacyBuffers anymore. LegacyBuffer is kept for WavLoader, see previous commit message. - Most intra-process audio data passing is done with FixedArray<Sample> or Vector<Sample>. - Improvements to most audio-enqueuing applications. (If necessary I can try to extract some of the aplay improvements.) - New APIs on LibAudio/ClientConnection which allows non-realtime applications to enqueue audio in big chunks like before. - Removal of status APIs from the audio server connection for information that can be directly obtained from the shared queue. - Split the pause playback API into two APIs with more intuitive names. I know this is a large commit, and you can kinda tell from the commit message. It's basically impossible to break this up without hacks, so please forgive me. These are some of the best changes to the audio subsystem and I hope that that makes up for this :yaktangle: commit. :yakring:
2022-02-20 12:01:22 +00:00
TRY(Core::System::pledge("stdio rpath wpath recvfd thread"));
2021-11-27 16:00:19 +00:00
if (command.equals_ignoring_ascii_case("get"sv) || command == "g") {
// Get variables
Vector<AudioVariable> values_to_print;
2021-11-25 21:29:12 +00:00
if (command_arguments.is_empty()) {
values_to_print.append(AudioVariable::Volume);
values_to_print.append(AudioVariable::Mute);
values_to_print.append(AudioVariable::SampleRate);
} else {
2021-11-25 21:29:12 +00:00
for (auto& variable : command_arguments) {
if (variable.is_one_of("v"sv, "volume"sv))
values_to_print.append(AudioVariable::Volume);
else if (variable.is_one_of("m"sv, "mute"sv))
values_to_print.append(AudioVariable::Mute);
else if (variable.is_one_of("r"sv, "samplerate"sv))
values_to_print.append(AudioVariable::SampleRate);
else {
warnln("Error: Unrecognized variable {}", variable);
return 1;
}
}
}
for (auto to_print : values_to_print) {
switch (to_print) {
case AudioVariable::Volume: {
auto volume = lround(audio_client->get_main_mix_volume() * 100);
if (human_mode)
outln("Volume: {}%", volume);
else
out("{} ", volume);
break;
}
case AudioVariable::Mute: {
bool muted = audio_client->is_main_mix_muted();
if (human_mode)
outln("Muted: {}", muted ? "Yes" : "No");
else
out("{} ", muted ? 1 : 0);
break;
}
case AudioVariable::SampleRate: {
2023-06-24 10:56:51 +00:00
u32 sample_rate = audio_client->get_device_sample_rate();
if (human_mode)
outln("Sample rate: {:5d} Hz", sample_rate);
else
out("{} ", sample_rate);
break;
}
}
}
if (!human_mode)
outln();
} else if (command.equals_ignoring_ascii_case("set"sv) || command == "s") {
// Set variables
HashMap<AudioVariable, Variant<int, bool>> values_to_set;
2021-11-25 21:29:12 +00:00
for (size_t i = 0; i < command_arguments.size(); ++i) {
if (i == command_arguments.size() - 1) {
warnln("Error: value missing for last variable");
return 1;
}
2021-11-25 21:29:12 +00:00
auto& variable = command_arguments[i];
if (variable.is_one_of("v"sv, "volume"sv)) {
2021-11-25 21:29:12 +00:00
auto volume = command_arguments[++i].to_int();
if (!volume.has_value()) {
2021-11-25 21:29:12 +00:00
warnln("Error: {} is not an integer volume", command_arguments[i - 1]);
return 1;
}
if (volume.value() < 0 || volume.value() > 100) {
2021-11-25 21:29:12 +00:00
warnln("Error: {} is not between 0 and 100", command_arguments[i - 1]);
return 1;
}
values_to_set.set(AudioVariable::Volume, volume.value());
} else if (variable.is_one_of("m"sv, "mute"sv)) {
2021-11-25 21:29:12 +00:00
auto& mute_text = command_arguments[++i];
bool mute;
if (mute_text.equals_ignoring_ascii_case("true"sv) || mute_text == "1") {
mute = true;
} else if (mute_text.equals_ignoring_ascii_case("false"sv) || mute_text == "0") {
mute = false;
} else {
warnln("Error: {} is not one of {{0, 1, true, false}}", mute_text);
return 1;
}
values_to_set.set(AudioVariable::Mute, mute);
} else if (variable.is_one_of("r"sv, "samplerate"sv)) {
2021-11-25 21:29:12 +00:00
auto sample_rate = command_arguments[++i].to_int();
if (!sample_rate.has_value()) {
2021-11-25 21:29:12 +00:00
warnln("Error: {} is not an integer sample rate", command_arguments[i - 1]);
return 1;
}
values_to_set.set(AudioVariable::SampleRate, sample_rate.value());
} else {
2021-11-25 21:29:12 +00:00
warnln("Error: Unrecognized variable {}", command_arguments[i]);
return 1;
}
}
for (auto to_set : values_to_set) {
switch (to_set.key) {
case AudioVariable::Volume: {
int& volume = to_set.value.get<int>();
audio_client->set_main_mix_volume(static_cast<double>(volume) / 100);
break;
}
case AudioVariable::Mute: {
bool& mute = to_set.value.get<bool>();
audio_client->set_main_mix_muted(mute);
break;
}
case AudioVariable::SampleRate: {
int& sample_rate = to_set.value.get<int>();
2023-06-24 10:56:51 +00:00
audio_client->set_device_sample_rate(sample_rate);
break;
}
}
}
}
return 0;
}