2021-08-19 01:22:28 +03:00
/*
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
* Copyright ( c ) 2021 , kleines Filmröllchen < malu . bertsch @ gmail . com >
2021-09-17 21:33:21 +03:00
* Copyright ( c ) 2021 , David Isaksson < davidisaksson93 @ gmail . com >
2021-08-19 01:22:28 +03:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include <AK/Variant.h>
# include <AK/Vector.h>
# include <LibAudio/Buffer.h>
# include <LibAudio/ClientConnection.h>
# include <LibCore/ArgsParser.h>
# include <LibCore/EventLoop.h>
# include <LibCore/File.h>
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 19:00:19 +03:00
# include <LibCore/System.h>
2021-11-26 00:29:12 +03:00
# include <LibMain/Main.h>
2021-09-17 21:33:21 +03:00
# include <math.h>
2021-08-19 01:22:28 +03:00
# include <stdio.h>
# include <sys/ioctl.h>
enum AudioVariable : u32 {
Volume ,
Mute ,
SampleRate
} ;
// asctl: audio server control utility
2021-11-26 00:29:12 +03:00
ErrorOr < int > serenity_main ( Main : : Arguments arguments )
2021-08-19 01:22:28 +03:00
{
Core : : EventLoop loop ;
auto audio_client = Audio : : ClientConnection : : construct ( ) ;
String command = String : : empty ( ) ;
2021-11-26 00:29:12 +03:00
Vector < StringView > command_arguments ;
2021-08-19 01:22:28 +03:00
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 \t The get command accepts a list of variables to print. \n \t They are printed in the given order. \n \t If no value is specified, all are printed. \n \n \t The set command accepts a any number of variables \n \t followed by the value they should be set to. \n \n \t Possible variables are (v)olume, (m)ute, sample(r)ate. \n " , " command " ) ;
2021-11-26 00:29:12 +03:00
args_parser . add_positional_argument ( command_arguments , " Arguments for the command " , " args " , Core : : ArgsParser : : Required : : No ) ;
args_parser . parse ( arguments ) ;
2021-08-19 01:22:28 +03:00
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 19:00:19 +03:00
TRY ( Core : : System : : unveil ( nullptr , nullptr ) ) ;
TRY ( Core : : System : : pledge ( " stdio rpath wpath recvfd " , nullptr ) ) ;
2021-08-19 01:22:28 +03:00
if ( command . equals_ignoring_case ( " get " ) | | command = = " g " ) {
// Get variables
Vector < AudioVariable > values_to_print ;
2021-11-26 00:29:12 +03:00
if ( command_arguments . is_empty ( ) ) {
2021-08-19 01:22:28 +03:00
values_to_print . append ( AudioVariable : : Volume ) ;
values_to_print . append ( AudioVariable : : Mute ) ;
values_to_print . append ( AudioVariable : : SampleRate ) ;
} else {
2021-11-26 00:29:12 +03:00
for ( auto & variable : command_arguments ) {
2021-08-19 01:22:28 +03:00
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 : {
2021-09-17 21:33:21 +03:00
auto volume = static_cast < int > ( round ( audio_client - > get_main_mix_volume ( ) * 100 ) ) ;
2021-08-19 01:22:28 +03:00
if ( human_mode )
2021-09-17 21:33:21 +03:00
outln ( " Volume: {}% " , volume ) ;
2021-08-19 01:22:28 +03:00
else
out ( " {} " , volume ) ;
break ;
}
case AudioVariable : : Mute : {
2021-11-02 03:52:22 +03:00
bool muted = audio_client - > is_main_mix_muted ( ) ;
2021-08-19 01:22:28 +03:00
if ( human_mode )
outln ( " Muted: {} " , muted ? " Yes " : " No " ) ;
else
out ( " {} " , muted ? 1 : 0 ) ;
break ;
}
case AudioVariable : : SampleRate : {
2021-11-24 14:20:20 +03:00
u32 sample_rate = audio_client - > get_sample_rate ( ) ;
2021-08-19 01:22:28 +03:00
if ( human_mode )
outln ( " Sample rate: {:5d} Hz " , sample_rate ) ;
else
out ( " {} " , sample_rate ) ;
break ;
}
}
}
if ( ! human_mode )
outln ( ) ;
} else if ( command . equals_ignoring_case ( " set " ) | | command = = " s " ) {
// Set variables
HashMap < AudioVariable , Variant < int , bool > > values_to_set ;
2021-11-26 00:29:12 +03:00
for ( size_t i = 0 ; i < command_arguments . size ( ) ; + + i ) {
if ( i = = command_arguments . size ( ) - 1 ) {
2021-08-19 01:22:28 +03:00
warnln ( " Error: value missing for last variable " ) ;
return 1 ;
}
2021-11-26 00:29:12 +03:00
auto & variable = command_arguments [ i ] ;
2021-08-19 01:22:28 +03:00
if ( variable . is_one_of ( " v " sv , " volume " sv ) ) {
2021-11-26 00:29:12 +03:00
auto volume = command_arguments [ + + i ] . to_int ( ) ;
2021-08-19 01:22:28 +03:00
if ( ! volume . has_value ( ) ) {
2021-11-26 00:29:12 +03:00
warnln ( " Error: {} is not an integer volume " , command_arguments [ i - 1 ] ) ;
2021-08-19 01:22:28 +03:00
return 1 ;
}
2021-09-17 21:33:21 +03:00
if ( volume . value ( ) < 0 | | volume . value ( ) > 100 ) {
2021-11-26 00:29:12 +03:00
warnln ( " Error: {} is not between 0 and 100 " , command_arguments [ i - 1 ] ) ;
2021-09-17 21:33:21 +03:00
return 1 ;
}
2021-08-19 01:22:28 +03:00
values_to_set . set ( AudioVariable : : Volume , volume . value ( ) ) ;
} else if ( variable . is_one_of ( " m " sv , " mute " sv ) ) {
2021-11-26 00:29:12 +03:00
auto & mute_text = command_arguments [ + + i ] ;
2021-08-19 01:22:28 +03:00
bool mute ;
if ( mute_text . equals_ignoring_case ( " true " ) | | mute_text = = " 1 " ) {
mute = true ;
} else if ( mute_text . equals_ignoring_case ( " false " ) | | 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-26 00:29:12 +03:00
auto sample_rate = command_arguments [ + + i ] . to_int ( ) ;
2021-08-19 01:22:28 +03:00
if ( ! sample_rate . has_value ( ) ) {
2021-11-26 00:29:12 +03:00
warnln ( " Error: {} is not an integer sample rate " , command_arguments [ i - 1 ] ) ;
2021-08-19 01:22:28 +03:00
return 1 ;
}
values_to_set . set ( AudioVariable : : SampleRate , sample_rate . value ( ) ) ;
} else {
2021-11-26 00:29:12 +03:00
warnln ( " Error: Unrecognized variable {} " , command_arguments [ i ] ) ;
2021-08-19 01:22:28 +03:00
return 1 ;
}
}
for ( auto to_set : values_to_set ) {
switch ( to_set . key ) {
case AudioVariable : : Volume : {
int & volume = to_set . value . get < int > ( ) ;
2021-09-17 21:33:21 +03:00
audio_client - > set_main_mix_volume ( static_cast < double > ( volume ) / 100 ) ;
2021-08-19 01:22:28 +03:00
break ;
}
case AudioVariable : : Mute : {
bool & mute = to_set . value . get < bool > ( ) ;
2021-11-02 03:52:22 +03:00
audio_client - > set_main_mix_muted ( mute ) ;
2021-08-19 01:22:28 +03:00
break ;
}
case AudioVariable : : SampleRate : {
int & sample_rate = to_set . value . get < int > ( ) ;
audio_client - > set_sample_rate ( sample_rate ) ;
break ;
}
}
}
}
return 0 ;
}