diff --git a/Userland/Applications/SoundPlayer/PlaybackManager.cpp b/Userland/Applications/SoundPlayer/PlaybackManager.cpp index 1421e75215..f301df0acb 100644 --- a/Userland/Applications/SoundPlayer/PlaybackManager.cpp +++ b/Userland/Applications/SoundPlayer/PlaybackManager.cpp @@ -108,14 +108,14 @@ void PlaybackManager::remove_dead_buffers() int id = m_connection->get_playing_buffer(); int current_id = -1; if (m_current_buffer) - current_id = m_current_buffer->shbuf_id(); + current_id = m_current_buffer->id(); if (id >= 0 && id != current_id) { while (!m_buffers.is_empty()) { --m_next_ptr; auto buffer = m_buffers.take_first(); - if (buffer->shbuf_id() == id) { + if (buffer->id() == id) { m_current_buffer = buffer; break; } diff --git a/Userland/Applications/SoundPlayer/main.cpp b/Userland/Applications/SoundPlayer/main.cpp index 3958365abe..45752c089b 100644 --- a/Userland/Applications/SoundPlayer/main.cpp +++ b/Userland/Applications/SoundPlayer/main.cpp @@ -37,14 +37,14 @@ int main(int argc, char** argv) { - if (pledge("stdio recvfd sendfd accept rpath thread unix cpath fattr shared_buffer", nullptr) < 0) { + if (pledge("stdio recvfd sendfd accept rpath thread unix cpath fattr", nullptr) < 0) { perror("pledge"); return 1; } auto app = GUI::Application::construct(argc, argv); - if (pledge("stdio recvfd sendfd accept rpath thread unix shared_buffer", nullptr) < 0) { + if (pledge("stdio recvfd sendfd accept rpath thread unix", nullptr) < 0) { perror("pledge"); return 1; } @@ -52,7 +52,7 @@ int main(int argc, char** argv) auto audio_client = Audio::ClientConnection::construct(); audio_client->handshake(); - if (pledge("stdio recvfd sendfd accept rpath thread shared_buffer", nullptr) < 0) { + if (pledge("stdio recvfd sendfd accept rpath thread", nullptr) < 0) { perror("pledge"); return 1; } diff --git a/Userland/Libraries/LibAudio/Buffer.cpp b/Userland/Libraries/LibAudio/Buffer.cpp index c23576482b..7ee2c88343 100644 --- a/Userland/Libraries/LibAudio/Buffer.cpp +++ b/Userland/Libraries/LibAudio/Buffer.cpp @@ -24,10 +24,17 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include namespace Audio { +i32 Buffer::allocate_id() +{ + static Atomic next_id; + return next_id++; +} + template static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector& samples, ResampleHelper& resampler, int num_channels) { diff --git a/Userland/Libraries/LibAudio/Buffer.h b/Userland/Libraries/LibAudio/Buffer.h index 5be0c2443a..6efdeb2baf 100644 --- a/Userland/Libraries/LibAudio/Buffer.h +++ b/Userland/Libraries/LibAudio/Buffer.h @@ -28,9 +28,9 @@ #include #include -#include #include #include +#include #include namespace Audio { @@ -115,33 +115,38 @@ public: { return adopt(*new Buffer(move(samples))); } - static NonnullRefPtr create_with_shared_buffer(NonnullRefPtr&& buffer, int sample_count) + static NonnullRefPtr create_with_anonymous_buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count) { - return adopt(*new Buffer(move(buffer), sample_count)); + return adopt(*new Buffer(move(buffer), buffer_id, sample_count)); } const Sample* samples() const { return (const Sample*)data(); } int sample_count() const { return m_sample_count; } - const void* data() const { return m_buffer->data(); } + const void* data() const { return m_buffer.data(); } int size_in_bytes() const { return m_sample_count * (int)sizeof(Sample); } - int shbuf_id() const { return m_buffer->shbuf_id(); } - SharedBuffer& shared_buffer() { return *m_buffer; } + int id() const { return m_id; } + const Core::AnonymousBuffer& anonymous_buffer() const { return m_buffer; } private: - explicit Buffer(Vector&& samples) - : m_buffer(*SharedBuffer::create_with_size(samples.size() * sizeof(Sample))) + explicit Buffer(const Vector samples) + : m_buffer(Core::AnonymousBuffer::create_with_size(samples.size() * sizeof(Sample))) + , m_id(allocate_id()) , m_sample_count(samples.size()) { - memcpy(m_buffer->data(), samples.data(), samples.size() * sizeof(Sample)); + memcpy(m_buffer.data(), samples.data(), samples.size() * sizeof(Sample)); } - explicit Buffer(NonnullRefPtr&& buffer, int sample_count) + explicit Buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count) : m_buffer(move(buffer)) + , m_id(buffer_id) , m_sample_count(sample_count) { } - NonnullRefPtr m_buffer; + static i32 allocate_id(); + + Core::AnonymousBuffer m_buffer; + const i32 m_id; const int m_sample_count; }; diff --git a/Userland/Libraries/LibAudio/ClientConnection.cpp b/Userland/Libraries/LibAudio/ClientConnection.cpp index 11fdeaf516..3cb3bd4627 100644 --- a/Userland/Libraries/LibAudio/ClientConnection.cpp +++ b/Userland/Libraries/LibAudio/ClientConnection.cpp @@ -43,8 +43,7 @@ void ClientConnection::handshake() void ClientConnection::enqueue(const Buffer& buffer) { for (;;) { - const_cast(buffer).shared_buffer().share_with(server_pid()); - auto response = send_sync(buffer.shbuf_id(), buffer.sample_count()); + auto response = send_sync(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count()); if (response->success()) break; sleep(1); @@ -53,8 +52,7 @@ void ClientConnection::enqueue(const Buffer& buffer) bool ClientConnection::try_enqueue(const Buffer& buffer) { - const_cast(buffer).shared_buffer().share_with(server_pid()); - auto response = send_sync(buffer.shbuf_id(), buffer.sample_count()); + auto response = send_sync(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count()); return response->success(); } diff --git a/Userland/Services/AudioServer/AudioServer.ipc b/Userland/Services/AudioServer/AudioServer.ipc index b48dcd7e46..1ae8ed8d9a 100644 --- a/Userland/Services/AudioServer/AudioServer.ipc +++ b/Userland/Services/AudioServer/AudioServer.ipc @@ -10,7 +10,7 @@ endpoint AudioServer = 85 SetMainMixVolume(i32 volume) => () // Buffer playback - EnqueueBuffer(i32 buffer_id, int sample_count) => (bool success) + EnqueueBuffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count) => (bool success) SetPaused(bool paused) => () ClearBuffer(bool paused) => () diff --git a/Userland/Services/AudioServer/ClientConnection.cpp b/Userland/Services/AudioServer/ClientConnection.cpp index c034dc5199..773aa4ca86 100644 --- a/Userland/Services/AudioServer/ClientConnection.cpp +++ b/Userland/Services/AudioServer/ClientConnection.cpp @@ -26,7 +26,6 @@ #include "ClientConnection.h" #include "Mixer.h" -#include #include #include #include @@ -98,20 +97,13 @@ OwnPtr ClientConnection::handle OwnPtr ClientConnection::handle(const Messages::AudioServer::EnqueueBuffer& message) { - auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.buffer_id()); - if (!shared_buffer) { - // FIXME: The shared buffer should have been retrieved for us already. - // We don't want to do IPC error checking at this layer. - ASSERT_NOT_REACHED(); - } - if (!m_queue) m_queue = m_mixer.create_queue(*this); if (m_queue->is_full()) return make(false); - m_queue->enqueue(Audio::Buffer::create_with_shared_buffer(*shared_buffer, message.sample_count())); + m_queue->enqueue(Audio::Buffer::create_with_anonymous_buffer(message.buffer(), message.buffer_id(), message.sample_count())); return make(true); } diff --git a/Userland/Services/AudioServer/Mixer.h b/Userland/Services/AudioServer/Mixer.h index 74fbba6ede..b1593acfe8 100644 --- a/Userland/Services/AudioServer/Mixer.h +++ b/Userland/Services/AudioServer/Mixer.h @@ -67,7 +67,7 @@ public: ++m_played_samples; if (m_position >= m_current->sample_count()) { - m_client->did_finish_playing_buffer({}, m_current->shbuf_id()); + m_client->did_finish_playing_buffer({}, m_current->id()); m_current = nullptr; m_position = 0; } @@ -96,7 +96,7 @@ public: int get_playing_buffer() const { if (m_current) - return m_current->shbuf_id(); + return m_current->id(); return -1; } diff --git a/Userland/Services/AudioServer/main.cpp b/Userland/Services/AudioServer/main.cpp index 9eca20bfd3..ee7c8ca8fd 100644 --- a/Userland/Services/AudioServer/main.cpp +++ b/Userland/Services/AudioServer/main.cpp @@ -30,7 +30,7 @@ int main(int, char**) { - if (pledge("stdio thread shared_buffer accept rpath wpath cpath unix fattr", nullptr) < 0) { + if (pledge("stdio recvfd thread accept rpath wpath cpath unix fattr", nullptr) < 0) { perror("pledge"); return 1; } @@ -52,7 +52,7 @@ int main(int, char**) IPC::new_client_connection(client_socket.release_nonnull(), client_id, mixer); }; - if (pledge("stdio thread shared_buffer accept", nullptr) < 0) { + if (pledge("stdio recvfd thread accept", nullptr) < 0) { perror("pledge"); return 1; }