Skip to content

Commit

Permalink
AudioServer: Add a buffer queue so we can buffer some sound.
Browse files Browse the repository at this point in the history
The idea here is to keep a small number of sample buffers queued in the
AudioServer so we don't get caught without something to play.
  • Loading branch information
awesomekling committed Jul 28, 2019
1 parent 7f82e86 commit 7cabe64
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Libraries/LibAudio/AClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ void AClientConnection::play(const ABuffer& buffer, bool block)
request.play_buffer.buffer_id = buffer.shared_buffer_id();
sync_request(request, block ? ASAPI_ServerMessage::Type::FinishedPlayingBuffer : ASAPI_ServerMessage::Type::PlayingBuffer);
}

void AClientConnection::enqueue(const ABuffer& buffer)
{
for (;;) {
const_cast<ABuffer&>(buffer).shared_buffer().share_with(server_pid());
ASAPI_ClientMessage request;
request.type = ASAPI_ClientMessage::Type::EnqueueBuffer;
request.play_buffer.buffer_id = buffer.shared_buffer_id();
auto response = sync_request(request, ASAPI_ServerMessage::Type::EnqueueBufferResponse);
if (response.success)
break;
dbg() << "EnqueueBuffer failed, retrying...";
sleep(1);
}
}
1 change: 1 addition & 0 deletions Libraries/LibAudio/AClientConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ class AClientConnection : public IPC::Client::Connection<ASAPI_ServerMessage, AS

virtual void handshake() override;
void play(const ABuffer&, bool block);
void enqueue(const ABuffer&);
};
3 changes: 3 additions & 0 deletions Libraries/LibAudio/ASAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ struct ASAPI_ServerMessage {
Greeting,
PlayingBuffer,
FinishedPlayingBuffer,
EnqueueBufferResponse,
};

Type type { Type::Invalid };
unsigned extra_size { 0 };
bool success { true };

union {
struct {
Expand All @@ -27,6 +29,7 @@ struct ASAPI_ClientMessage {
Invalid,
Greeting,
PlayBuffer,
EnqueueBuffer,
};

Type type { Type::Invalid };
Expand Down
35 changes: 35 additions & 0 deletions Servers/AudioServer/ASClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ bool ASClientConnection::handle_message(const ASAPI_ClientMessage& message, cons
m_mixer.queue(*this, ABuffer::create_with_shared_buffer(*shared_buffer));
break;
}
case ASAPI_ClientMessage::Type::EnqueueBuffer: {
auto shared_buffer = SharedBuffer::create_from_shared_buffer_id(message.play_buffer.buffer_id);
if (!shared_buffer) {
did_misbehave();
return false;
}

static const int max_in_queue = 2;

ASAPI_ServerMessage reply;
reply.type = ASAPI_ServerMessage::Type::EnqueueBufferResponse;
reply.playing_buffer.buffer_id = message.play_buffer.buffer_id;
if (m_buffer_queue.size() >= max_in_queue) {
reply.success = false;
} else {
m_buffer_queue.enqueue(ABuffer::create_with_shared_buffer(*shared_buffer));
}
post_message(reply);

if (m_playing_queued_buffer_id == -1)
play_next_in_queue();

break;
}
case ASAPI_ClientMessage::Type::Invalid:
default:
dbgprintf("ASClientConnection: Unexpected message ID %d\n", int(message.type));
Expand All @@ -65,8 +89,19 @@ bool ASClientConnection::handle_message(const ASAPI_ClientMessage& message, cons

void ASClientConnection::did_finish_playing_buffer(Badge<ASMixer>, int buffer_id)
{
if (m_playing_queued_buffer_id == buffer_id)
play_next_in_queue();

ASAPI_ServerMessage reply;
reply.type = ASAPI_ServerMessage::Type::FinishedPlayingBuffer;
reply.playing_buffer.buffer_id = buffer_id;
post_message(reply);
}

void ASClientConnection::play_next_in_queue()
{
dbg() << "Playing next in queue (" << m_buffer_queue.size() << " queued)";
auto buffer = m_buffer_queue.dequeue();
m_playing_queued_buffer_id = buffer->shared_buffer_id();
m_mixer.queue(*this, move(buffer));
}
6 changes: 6 additions & 0 deletions Servers/AudioServer/ASClientConnection.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <AK/Queue.h>
#include <LibAudio/ASAPI.h>
#include <LibCore/CoreIPCServer.h>

class ABuffer;
class ASMixer;

class ASClientConnection final : public IPC::Server::Connection<ASAPI_ServerMessage, ASAPI_ClientMessage> {
Expand All @@ -16,5 +18,9 @@ class ASClientConnection final : public IPC::Server::Connection<ASAPI_ServerMess
void did_finish_playing_buffer(Badge<ASMixer>, int buffer_id);

private:
void play_next_in_queue();

ASMixer& m_mixer;
Queue<NonnullRefPtr<ABuffer>> m_buffer_queue;
int m_playing_queued_buffer_id { -1 };
};
2 changes: 1 addition & 1 deletion Userland/aplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(int argc, char **argv)
break;
}
printf("Playing %d sample(s)\n", samples->sample_count());
a_conn.play(*samples, true);
a_conn.enqueue(*samples);
}

printf("Exiting! :)\n");
Expand Down

0 comments on commit 7cabe64

Please sign in to comment.