1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:57:35 +00:00

AudioServer: Persist audio settings with a config file

AudioServer loads its settings, currently volume and mute state, from a
user config file "Audio.ini". Additionally, the current settings are
stored every ten seconds, if necessary. This allows for persistent audio
settings in between boots.
This commit is contained in:
kleines Filmröllchen 2021-07-25 20:54:09 +02:00 committed by Andreas Kling
parent 47bc72bcf6
commit d1b0143ba5
3 changed files with 58 additions and 4 deletions

View file

@ -1,21 +1,25 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Mixer.h"
#include <AK/Array.h>
#include <AK/MemoryStream.h>
#include <AK/NumericLimits.h>
#include <AudioServer/ClientConnection.h>
#include <AudioServer/Mixer.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/Timer.h>
#include <pthread.h>
namespace AudioServer {
u8 Mixer::m_zero_filled_buffer[4096];
Mixer::Mixer()
Mixer::Mixer(NonnullRefPtr<Core::ConfigFile> config)
: m_device(Core::File::construct("/dev/audio", this))
, m_sound_thread(Threading::Thread::construct(
[this] {
@ -23,6 +27,7 @@ Mixer::Mixer()
return 0;
},
"AudioServer[mixer]"))
, m_config(move(config))
{
if (!m_device->open(Core::OpenMode::WriteOnly)) {
dbgln("Can't open audio device: {}", m_device->error_string());
@ -32,6 +37,9 @@ Mixer::Mixer()
pthread_mutex_init(&m_pending_mutex, nullptr);
pthread_cond_init(&m_pending_cond, nullptr);
m_muted = m_config->read_bool_entry("Master", "Mute", false);
m_main_volume = m_config->read_num_entry("Master", "Volume", 100);
m_sound_thread->start();
}
@ -119,6 +127,10 @@ void Mixer::set_main_volume(int volume)
m_main_volume = 200;
else
m_main_volume = volume;
m_config->write_num_entry("Master", "Volume", volume);
request_setting_sync();
ClientConnection::for_each([&](ClientConnection& client) {
client.did_change_main_mix_volume({}, m_main_volume);
});
@ -129,11 +141,28 @@ void Mixer::set_muted(bool muted)
if (m_muted == muted)
return;
m_muted = muted;
m_config->write_bool_entry("Master", "Mute", m_muted);
request_setting_sync();
ClientConnection::for_each([muted](ClientConnection& client) {
client.did_change_muted_state({}, muted);
});
}
void Mixer::request_setting_sync()
{
if (m_config_write_timer.is_null() || !m_config_write_timer->is_active()) {
m_config_write_timer = Core::Timer::create_single_shot(
AUDIO_CONFIG_WRITE_INTERVAL,
[this] {
m_config->sync();
},
this);
m_config_write_timer->start();
}
}
BufferQueue::BufferQueue(ClientConnection& client)
: m_client(client)
{

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -16,6 +17,7 @@
#include <AK/WeakPtr.h>
#include <LibAudio/Buffer.h>
#include <LibCore/File.h>
#include <LibCore/Timer.h>
#include <LibThreading/Mutex.h>
#include <LibThreading/Thread.h>
@ -93,7 +95,7 @@ private:
class Mixer : public Core::Object {
C_OBJECT(Mixer)
public:
Mixer();
Mixer(NonnullRefPtr<Core::ConfigFile> config);
virtual ~Mixer() override;
NonnullRefPtr<BufferQueue> create_queue(ClientConnection&);
@ -105,6 +107,8 @@ public:
void set_muted(bool);
private:
void request_setting_sync();
Vector<NonnullRefPtr<BufferQueue>> m_pending_mixing;
Atomic<bool> m_added_queue { false };
pthread_mutex_t m_pending_mutex;
@ -117,8 +121,15 @@ private:
bool m_muted { false };
int m_main_volume { 100 };
NonnullRefPtr<Core::ConfigFile> m_config;
RefPtr<Core::Timer> m_config_write_timer;
static u8 m_zero_filled_buffer[4096];
void mix();
};
// Interval in ms when the server tries to save its configuration to disk.
constexpr unsigned AUDIO_CONFIG_WRITE_INTERVAL = 2000;
}

View file

@ -1,10 +1,13 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Mixer.h"
#include <LibCore/ConfigFile.h>
#include <LibCore/File.h>
#include <LibCore/LocalServer.h>
int main(int, char**)
@ -14,8 +17,19 @@ int main(int, char**)
return 1;
}
auto config = Core::ConfigFile::get_for_app("Audio");
if (unveil(config->filename().characters(), "rwc") < 0) {
perror("unveil");
return 1;
}
if (unveil("/dev/audio", "wc") < 0) {
perror("unveil");
return 1;
}
unveil(nullptr, nullptr);
Core::EventLoop event_loop;
AudioServer::Mixer mixer;
AudioServer::Mixer mixer { config };
auto server = Core::LocalServer::construct();
bool ok = server->take_over_from_system_server();
@ -31,7 +45,7 @@ int main(int, char**)
IPC::new_client_connection<AudioServer::ClientConnection>(client_socket.release_nonnull(), client_id, mixer);
};
if (pledge("stdio recvfd thread accept", nullptr) < 0) {
if (pledge("stdio recvfd thread accept cpath rpath wpath", nullptr) < 0) {
perror("pledge");
return 1;
}