1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:38:11 +00:00

AudioServer: Add a buffer queue so we can buffer some sound.

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.
This commit is contained in:
Andreas Kling 2019-07-28 18:27:32 +02:00
parent 7f82e86fb8
commit 7cabe6433e
6 changed files with 61 additions and 1 deletions

View file

@ -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));
@ -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));
}