summaryrefslogtreecommitdiff
path: root/Userland/Services/AudioServer
diff options
context:
space:
mode:
authorkleines Filmröllchen <filmroellchen@serenityos.org>2022-02-20 13:01:22 +0100
committerLinus Groh <mail@linusgroh.de>2022-04-21 13:55:00 +0200
commit49b087f3cd49261164bd4556cd6e9e0d95a3afc1 (patch)
tree6e247f1fe819504cfa1ae4a1b9c9b97adaa40be7 /Userland/Services/AudioServer
parentcb0e95c928e152d39dc60198ab714f437a2347ce (diff)
downloadserenity-49b087f3cd49261164bd4556cd6e9e0d95a3afc1.zip
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:
Diffstat (limited to 'Userland/Services/AudioServer')
-rw-r--r--Userland/Services/AudioServer/AudioClient.ipc1
-rw-r--r--Userland/Services/AudioServer/AudioServer.ipc13
-rw-r--r--Userland/Services/AudioServer/ConnectionFromClient.cpp57
-rw-r--r--Userland/Services/AudioServer/ConnectionFromClient.h17
-rw-r--r--Userland/Services/AudioServer/Mixer.cpp7
-rw-r--r--Userland/Services/AudioServer/Mixer.h68
6 files changed, 54 insertions, 109 deletions
diff --git a/Userland/Services/AudioServer/AudioClient.ipc b/Userland/Services/AudioServer/AudioClient.ipc
index 0a6d139467..df97c95709 100644
--- a/Userland/Services/AudioServer/AudioClient.ipc
+++ b/Userland/Services/AudioServer/AudioClient.ipc
@@ -2,7 +2,6 @@
endpoint AudioClient
{
- finished_playing_buffer(i32 buffer_id) =|
main_mix_muted_state_changed(bool muted) =|
main_mix_volume_changed(double volume) =|
client_volume_changed(double volume) =|
diff --git a/Userland/Services/AudioServer/AudioServer.ipc b/Userland/Services/AudioServer/AudioServer.ipc
index 0c7d24ddfc..890642d23e 100644
--- a/Userland/Services/AudioServer/AudioServer.ipc
+++ b/Userland/Services/AudioServer/AudioServer.ipc
@@ -1,4 +1,5 @@
#include <LibCore/AnonymousBuffer.h>
+#include <LibAudio/Buffer.h>
endpoint AudioServer
{
@@ -17,12 +18,8 @@ endpoint AudioServer
get_sample_rate() => (u32 sample_rate)
// Buffer playback
- enqueue_buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count) => (bool success)
- set_paused(bool paused) => ()
- clear_buffer(bool paused) => ()
-
- //Buffer information
- get_remaining_samples() => (int remaining_samples)
- get_played_samples() => (int played_samples)
- get_playing_buffer() => (i32 buffer_id)
+ set_buffer(Audio::AudioQueue buffer) => ()
+ clear_buffer() =|
+ start_playback() =|
+ pause_playback() =|
}
diff --git a/Userland/Services/AudioServer/ConnectionFromClient.cpp b/Userland/Services/AudioServer/ConnectionFromClient.cpp
index 0f4b457561..4c3d6862c4 100644
--- a/Userland/Services/AudioServer/ConnectionFromClient.cpp
+++ b/Userland/Services/AudioServer/ConnectionFromClient.cpp
@@ -34,9 +34,17 @@ void ConnectionFromClient::die()
s_connections.remove(client_id());
}
-void ConnectionFromClient::did_finish_playing_buffer(Badge<ClientAudioStream>, int buffer_id)
+void ConnectionFromClient::set_buffer(Audio::AudioQueue const& buffer)
{
- async_finished_playing_buffer(buffer_id);
+ if (!buffer.is_valid()) {
+ did_misbehave("Received an invalid buffer");
+ return;
+ }
+ if (!m_queue)
+ m_queue = m_mixer.create_queue(*this);
+
+ // This is ugly but we know nobody uses the buffer afterwards anyways.
+ m_queue->set_buffer(make<Audio::AudioQueue>(move(const_cast<Audio::AudioQueue&>(buffer))));
}
void ConnectionFromClient::did_change_main_mix_muted_state(Badge<Mixer>, bool muted)
@@ -85,55 +93,22 @@ void ConnectionFromClient::set_self_volume(double volume)
m_queue->set_volume(volume);
}
-Messages::AudioServer::EnqueueBufferResponse ConnectionFromClient::enqueue_buffer(Core::AnonymousBuffer const& buffer, i32 buffer_id, int sample_count)
-{
- if (!m_queue)
- m_queue = m_mixer.create_queue(*this);
-
- if (m_queue->is_full())
- return false;
-
- // There's not a big allocation to worry about here.
- m_queue->enqueue(MUST(Audio::LegacyBuffer::create_with_anonymous_buffer(buffer, buffer_id, sample_count)));
- return true;
-}
-
-Messages::AudioServer::GetRemainingSamplesResponse ConnectionFromClient::get_remaining_samples()
-{
- int remaining = 0;
- if (m_queue)
- remaining = m_queue->get_remaining_samples();
-
- return remaining;
-}
-
-Messages::AudioServer::GetPlayedSamplesResponse ConnectionFromClient::get_played_samples()
-{
- int played = 0;
- if (m_queue)
- played = m_queue->get_played_samples();
-
- return played;
-}
-
-void ConnectionFromClient::set_paused(bool paused)
+void ConnectionFromClient::start_playback()
{
if (m_queue)
- m_queue->set_paused(paused);
+ m_queue->set_paused(false);
}
-void ConnectionFromClient::clear_buffer(bool paused)
+void ConnectionFromClient::pause_playback()
{
if (m_queue)
- m_queue->clear(paused);
+ m_queue->set_paused(true);
}
-Messages::AudioServer::GetPlayingBufferResponse ConnectionFromClient::get_playing_buffer()
+void ConnectionFromClient::clear_buffer()
{
- int id = -1;
if (m_queue)
- id = m_queue->get_playing_buffer();
- return id;
+ m_queue->clear();
}
Messages::AudioServer::IsMainMixMutedResponse ConnectionFromClient::is_main_mix_muted()
diff --git a/Userland/Services/AudioServer/ConnectionFromClient.h b/Userland/Services/AudioServer/ConnectionFromClient.h
index e744e75c3d..2962ecbf37 100644
--- a/Userland/Services/AudioServer/ConnectionFromClient.h
+++ b/Userland/Services/AudioServer/ConnectionFromClient.h
@@ -9,12 +9,10 @@
#include <AK/HashMap.h>
#include <AudioServer/AudioClientEndpoint.h>
#include <AudioServer/AudioServerEndpoint.h>
+#include <LibAudio/Buffer.h>
+#include <LibCore/EventLoop.h>
#include <LibIPC/ConnectionFromClient.h>
-namespace Audio {
-class LegacyBuffer;
-}
-
namespace AudioServer {
class ClientAudioStream;
@@ -25,7 +23,6 @@ class ConnectionFromClient final : public IPC::ConnectionFromClient<AudioClientE
public:
~ConnectionFromClient() override = default;
- void did_finish_playing_buffer(Badge<ClientAudioStream>, int buffer_id);
void did_change_client_volume(Badge<ClientAudioStream>, double volume);
void did_change_main_mix_muted_state(Badge<Mixer>, bool muted);
void did_change_main_mix_volume(Badge<Mixer>, double volume);
@@ -41,12 +38,10 @@ private:
virtual void set_main_mix_volume(double) override;
virtual Messages::AudioServer::GetSelfVolumeResponse get_self_volume() override;
virtual void set_self_volume(double) override;
- virtual Messages::AudioServer::EnqueueBufferResponse enqueue_buffer(Core::AnonymousBuffer const&, i32, int) override;
- virtual Messages::AudioServer::GetRemainingSamplesResponse get_remaining_samples() override;
- virtual Messages::AudioServer::GetPlayedSamplesResponse get_played_samples() override;
- virtual void set_paused(bool) override;
- virtual void clear_buffer(bool) override;
- virtual Messages::AudioServer::GetPlayingBufferResponse get_playing_buffer() override;
+ virtual void set_buffer(Audio::AudioQueue const&) override;
+ virtual void clear_buffer() override;
+ virtual void start_playback() override;
+ virtual void pause_playback() override;
virtual Messages::AudioServer::IsMainMixMutedResponse is_main_mix_muted() override;
virtual void set_main_mix_muted(bool) override;
virtual Messages::AudioServer::IsSelfMutedResponse is_self_muted() override;
diff --git a/Userland/Services/AudioServer/Mixer.cpp b/Userland/Services/AudioServer/Mixer.cpp
index cd86c4ee0e..fc49e36204 100644
--- a/Userland/Services/AudioServer/Mixer.cpp
+++ b/Userland/Services/AudioServer/Mixer.cpp
@@ -6,8 +6,8 @@
*/
#include "Mixer.h"
-#include "AK/Format.h"
#include <AK/Array.h>
+#include <AK/Format.h>
#include <AK/MemoryStream.h>
#include <AK/NumericLimits.h>
#include <AudioServer/ConnectionFromClient.h>
@@ -200,9 +200,4 @@ ClientAudioStream::ClientAudioStream(ConnectionFromClient& client)
{
}
-void ClientAudioStream::enqueue(NonnullRefPtr<Audio::LegacyBuffer>&& buffer)
-{
- m_remaining_samples += buffer->sample_count();
- m_queue.enqueue(move(buffer));
-}
}
diff --git a/Userland/Services/AudioServer/Mixer.h b/Userland/Services/AudioServer/Mixer.h
index ca24a77050..77b93b09e1 100644
--- a/Userland/Services/AudioServer/Mixer.h
+++ b/Userland/Services/AudioServer/Mixer.h
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
+ * Copyright (c) 2021-2022, kleines Filmröllchen <filmroellchen@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -37,58 +37,43 @@ public:
explicit ClientAudioStream(ConnectionFromClient&);
~ClientAudioStream() = default;
- bool is_full() const { return m_queue.size() >= 3; }
- void enqueue(NonnullRefPtr<Audio::LegacyBuffer>&&);
-
bool get_next_sample(Audio::Sample& sample)
{
if (m_paused)
return false;
- while (!m_current && !m_queue.is_empty())
- m_current = m_queue.dequeue();
-
- if (!m_current)
- return false;
+ if (m_in_chunk_location >= m_current_audio_chunk.size()) {
+ // FIXME: We should send a did_misbehave to the client if the queue is empty,
+ // but the lifetimes involved mean that we segfault if we try to do that.
+ auto result = m_buffer->try_dequeue();
+ if (result.is_error()) {
+ if (result.error() == Audio::AudioQueue::QueueStatus::Empty)
+ dbgln("Audio client can't keep up!");
+
+ return false;
+ }
+ m_current_audio_chunk = result.release_value();
+ m_in_chunk_location = 0;
+ }
- sample = m_current->samples()[m_position++];
- if (m_remaining_samples > 0)
- --m_remaining_samples;
- ++m_played_samples;
+ sample = m_current_audio_chunk[m_in_chunk_location++];
- if (m_position >= m_current->sample_count()) {
- m_client->did_finish_playing_buffer({}, m_current->id());
- m_current = nullptr;
- m_position = 0;
- }
return true;
}
ConnectionFromClient* client() { return m_client.ptr(); }
- void clear(bool paused = false)
- {
- m_queue.clear();
- m_position = 0;
- m_remaining_samples = 0;
- m_played_samples = 0;
- m_current = nullptr;
- m_paused = paused;
- }
+ void set_buffer(OwnPtr<Audio::AudioQueue> buffer) { m_buffer = move(buffer); }
- void set_paused(bool paused)
+ void clear()
{
- m_paused = paused;
+ ErrorOr<Array<Audio::Sample, Audio::AUDIO_BUFFER_SIZE>, Audio::AudioQueue::QueueStatus> result = Audio::AudioQueue::QueueStatus::Invalid;
+ do {
+ result = m_buffer->try_dequeue();
+ } while (result.is_error() && result.error() != Audio::AudioQueue::QueueStatus::Empty);
}
- int get_remaining_samples() const { return m_remaining_samples; }
- int get_played_samples() const { return m_played_samples; }
- int get_playing_buffer() const
- {
- if (m_current)
- return m_current->id();
- return -1;
- }
+ void set_paused(bool paused) { m_paused = paused; }
FadingProperty<double>& volume() { return m_volume; }
double volume() const { return m_volume; }
@@ -97,11 +82,10 @@ public:
void set_muted(bool muted) { m_muted = muted; }
private:
- RefPtr<Audio::LegacyBuffer> m_current;
- Queue<NonnullRefPtr<Audio::LegacyBuffer>> m_queue;
- int m_position { 0 };
- int m_remaining_samples { 0 };
- int m_played_samples { 0 };
+ OwnPtr<Audio::AudioQueue> m_buffer;
+ Array<Audio::Sample, Audio::AUDIO_BUFFER_SIZE> m_current_audio_chunk;
+ size_t m_in_chunk_location;
+
bool m_paused { false };
bool m_muted { false };