AudioServer+LibAudio: Pass audio buffers as Core::AnonymousBuffer
This was the last remaining user of shbufs! :^)
This commit is contained in:
parent
cc8b3c92ba
commit
2cd16778b5
Notes:
sideshowbarker
2024-07-18 23:08:48 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/2cd16778b5a
9 changed files with 36 additions and 34 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -24,10 +24,17 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Atomic.h>
|
||||
#include <LibAudio/Buffer.h>
|
||||
|
||||
namespace Audio {
|
||||
|
||||
i32 Buffer::allocate_id()
|
||||
{
|
||||
static Atomic<i32> next_id;
|
||||
return next_id++;
|
||||
}
|
||||
|
||||
template<typename SampleReader>
|
||||
static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector<Sample>& samples, ResampleHelper& resampler, int num_channels)
|
||||
{
|
||||
|
|
|
@ -28,9 +28,9 @@
|
|||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/MemoryStream.h>
|
||||
#include <AK/SharedBuffer.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/AnonymousBuffer.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace Audio {
|
||||
|
@ -115,33 +115,38 @@ public:
|
|||
{
|
||||
return adopt(*new Buffer(move(samples)));
|
||||
}
|
||||
static NonnullRefPtr<Buffer> create_with_shared_buffer(NonnullRefPtr<SharedBuffer>&& buffer, int sample_count)
|
||||
static NonnullRefPtr<Buffer> 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<void>(); }
|
||||
const void* data() const { return m_buffer.data<void>(); }
|
||||
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<Sample>&& samples)
|
||||
: m_buffer(*SharedBuffer::create_with_size(samples.size() * sizeof(Sample)))
|
||||
explicit Buffer(const Vector<Sample> 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<void>(), samples.data(), samples.size() * sizeof(Sample));
|
||||
memcpy(m_buffer.data<void>(), samples.data(), samples.size() * sizeof(Sample));
|
||||
}
|
||||
|
||||
explicit Buffer(NonnullRefPtr<SharedBuffer>&& 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<SharedBuffer> m_buffer;
|
||||
static i32 allocate_id();
|
||||
|
||||
Core::AnonymousBuffer m_buffer;
|
||||
const i32 m_id;
|
||||
const int m_sample_count;
|
||||
};
|
||||
|
||||
|
|
|
@ -43,8 +43,7 @@ void ClientConnection::handshake()
|
|||
void ClientConnection::enqueue(const Buffer& buffer)
|
||||
{
|
||||
for (;;) {
|
||||
const_cast<Buffer&>(buffer).shared_buffer().share_with(server_pid());
|
||||
auto response = send_sync<Messages::AudioServer::EnqueueBuffer>(buffer.shbuf_id(), buffer.sample_count());
|
||||
auto response = send_sync<Messages::AudioServer::EnqueueBuffer>(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&>(buffer).shared_buffer().share_with(server_pid());
|
||||
auto response = send_sync<Messages::AudioServer::EnqueueBuffer>(buffer.shbuf_id(), buffer.sample_count());
|
||||
auto response = send_sync<Messages::AudioServer::EnqueueBuffer>(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count());
|
||||
return response->success();
|
||||
}
|
||||
|
||||
|
|
|
@ -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) => ()
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
|
||||
#include "ClientConnection.h"
|
||||
#include "Mixer.h"
|
||||
#include <AK/SharedBuffer.h>
|
||||
#include <AudioServer/AudioClientEndpoint.h>
|
||||
#include <LibAudio/Buffer.h>
|
||||
#include <errno.h>
|
||||
|
@ -98,20 +97,13 @@ OwnPtr<Messages::AudioServer::SetMainMixVolumeResponse> ClientConnection::handle
|
|||
|
||||
OwnPtr<Messages::AudioServer::EnqueueBufferResponse> 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<Messages::AudioServer::EnqueueBufferResponse>(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<Messages::AudioServer::EnqueueBufferResponse>(true);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<AudioServer::ClientConnection>(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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue