test/run_output: use class StaticFifoBuffer

This commit is contained in:
Max Kellermann 2021-03-08 16:58:12 +01:00
parent 2bebc79363
commit eff50b263a

View File

@ -31,6 +31,7 @@
#include "util/StringBuffer.hxx" #include "util/StringBuffer.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include "util/ScopeExit.hxx" #include "util/ScopeExit.hxx"
#include "util/StaticFifoBuffer.hxx"
#include "util/PrintException.hxx" #include "util/PrintException.hxx"
#include "LogBackend.hxx" #include "LogBackend.hxx"
@ -127,32 +128,37 @@ RunOutput(AudioOutput &ao, AudioFormat audio_format,
fprintf(stderr, "audio_format=%s\n", fprintf(stderr, "audio_format=%s\n",
ToString(audio_format).c_str()); ToString(audio_format).c_str());
size_t frame_size = audio_format.GetFrameSize(); const size_t in_frame_size = audio_format.GetFrameSize();
/* play */ /* play */
size_t length = 0; StaticFifoBuffer<std::byte, 4096> buffer;
char buffer[4096];
while (true) { while (true) {
if (length < sizeof(buffer)) { {
ssize_t nbytes = in_fd.Read(buffer + length, const auto dest = buffer.Write();
sizeof(buffer) - length); assert(!dest.empty());
ssize_t nbytes = in_fd.Read(dest.data, dest.size);
if (nbytes <= 0) if (nbytes <= 0)
break; break;
length += (size_t)nbytes; buffer.Append(nbytes);
} }
size_t play_length = (length / frame_size) * frame_size; auto src = buffer.Read();
if (play_length > 0) { assert(!src.empty());
size_t consumed = ao.Play(buffer, play_length);
assert(consumed <= length); src.size -= src.size % in_frame_size;
assert(consumed % frame_size == 0); if (src.empty())
continue;
length -= consumed; size_t consumed = ao.Play(src.data, src.size);
memmove(buffer, buffer + consumed, length);
} assert(consumed <= src.size);
assert(consumed % in_frame_size == 0);
buffer.Consume(consumed);
} }
} }