mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 14:48:14 +00:00
SoundPlayer: Don't enqueue samples depending on the GUI loop
Previously, SoundPlayer would read and enqueue samples in the GUI loop (through a Timer). Apart from general problems with doing audio on the GUI thread, this is particularly bad as the audio would lag or drop out when the GUI lags (e.g. window resizes and moves, changing the visualizer). As Piano does, now SoundPlayer enqueues more audio once the audio server signals that a buffer has finished playing. The GUI- dependent decoding is still kept as a "backup" and to start the entire cycle, but it's not solely depended on. A queue of buffer IDs is used to keep track of playing buffers and how many there are. The buffer overhead, i.e. how many buffers "too many" currently exist, is currently set to its absolute minimum of 2.
This commit is contained in:
parent
b48badc3b6
commit
c748c0726a
2 changed files with 16 additions and 3 deletions
|
@ -12,8 +12,16 @@ PlaybackManager::PlaybackManager(NonnullRefPtr<Audio::ClientConnection> connecti
|
|||
m_timer = Core::Timer::construct(PlaybackManager::update_rate_ms, [&]() {
|
||||
if (!m_loader)
|
||||
return;
|
||||
next_buffer();
|
||||
// Make sure that we have some buffers queued up at all times: an audio dropout is the last thing we want.
|
||||
if (m_enqueued_buffers.size() < always_enqueued_buffer_count)
|
||||
next_buffer();
|
||||
});
|
||||
m_connection->on_finish_playing_buffer = [this](auto finished_buffer) {
|
||||
auto last_buffer_in_queue = m_enqueued_buffers.dequeue();
|
||||
// A fail here would mean that the server skipped one of our buffers, which is BAD.
|
||||
VERIFY(last_buffer_in_queue == finished_buffer);
|
||||
next_buffer();
|
||||
};
|
||||
m_timer->stop();
|
||||
m_device_sample_rate = connection->get_sample_rate();
|
||||
}
|
||||
|
@ -117,7 +125,7 @@ void PlaybackManager::next_buffer()
|
|||
return;
|
||||
}
|
||||
|
||||
if (audio_server_remaining_samples < m_device_samples_per_buffer) {
|
||||
if (audio_server_remaining_samples < m_device_samples_per_buffer * always_enqueued_buffer_count) {
|
||||
auto maybe_buffer = m_loader->get_more_samples(m_source_buffer_size_bytes);
|
||||
if (!maybe_buffer.is_error()) {
|
||||
m_current_buffer = maybe_buffer.release_value();
|
||||
|
@ -126,6 +134,7 @@ void PlaybackManager::next_buffer()
|
|||
// FIXME: Handle OOM better.
|
||||
m_current_buffer = MUST(Audio::resample_buffer(m_resampler.value(), *m_current_buffer));
|
||||
m_connection->enqueue(*m_current_buffer);
|
||||
m_enqueued_buffers.enqueue(m_current_buffer->id());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue