Reuse a decoding buffer for ALSA rather than constantly allocating new ones

This commit is contained in:
Thomas Goyne 2014-05-23 09:19:19 -07:00
parent 1bf6197869
commit e36ecbde49

View file

@ -81,6 +81,8 @@ class AlsaPlayer final : public AudioPlayer {
int64_t last_position = 0; int64_t last_position = 0;
timespec last_position_time = {0, 0}; timespec last_position_time = {0, 0};
std::vector<char> decode_buffer;
std::thread thread; std::thread thread;
void PlaybackThread(); void PlaybackThread();
@ -162,12 +164,12 @@ do_setup:
// Initial buffer-fill // Initial buffer-fill
{ {
auto avail = std::min(snd_pcm_avail(pcm), (snd_pcm_sframes_t)(end_position-position)); auto avail = std::min(snd_pcm_avail(pcm), (snd_pcm_sframes_t)(end_position-position));
std::unique_ptr<char[]> buf{new char[avail*framesize]}; decode_buffer.resize(avail * framesize);
provider->GetAudioWithVolume(buf.get(), position, avail, volume); provider->GetAudioWithVolume(decode_buffer.data(), position, avail, volume);
snd_pcm_sframes_t written = 0; snd_pcm_sframes_t written = 0;
while (written <= 0) while (written <= 0)
{ {
written = snd_pcm_writei(pcm, buf.get(), avail); written = snd_pcm_writei(pcm, decode_buffer.data(), avail);
if (written == -ESTRPIPE) if (written == -ESTRPIPE)
snd_pcm_recover(pcm, written, 0); snd_pcm_recover(pcm, written, 0);
else if (written <= 0) else if (written <= 0)
@ -228,12 +230,12 @@ do_setup:
continue; continue;
{ {
std::unique_ptr<char[]> buf{new char[avail*framesize]}; decode_buffer.resize(avail * framesize);
provider->GetAudioWithVolume(buf.get(), position, avail, volume); provider->GetAudioWithVolume(decode_buffer.data(), position, avail, volume);
snd_pcm_sframes_t written = 0; snd_pcm_sframes_t written = 0;
while (written <= 0) while (written <= 0)
{ {
written = snd_pcm_writei(pcm, buf.get(), avail); written = snd_pcm_writei(pcm, decode_buffer.data(), avail);
if (written == -ESTRPIPE || written == -EPIPE) if (written == -ESTRPIPE || written == -EPIPE)
snd_pcm_recover(pcm, written, 0); snd_pcm_recover(pcm, written, 0);
else if (written == 0) else if (written == 0)