summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2021-03-08 16:58:12 +0100
committerMax Kellermann <max@musicpd.org>2021-03-08 17:01:48 +0100
commiteff50b263a9b0a738e0f26d08b2a5ce782761b7c (patch)
tree2d742c81665d0548e879e594107c36d34ee9572f
parent2bebc793639f54edd23183355cfeab64f36aab88 (diff)
test/run_output: use class StaticFifoBuffer
-rw-r--r--test/run_output.cxx36
1 files changed, 21 insertions, 15 deletions
diff --git a/test/run_output.cxx b/test/run_output.cxx
index f7d86a825..4fd3ff2ea 100644
--- a/test/run_output.cxx
+++ b/test/run_output.cxx
@@ -31,6 +31,7 @@
#include "util/StringBuffer.hxx"
#include "util/RuntimeError.hxx"
#include "util/ScopeExit.hxx"
+#include "util/StaticFifoBuffer.hxx"
#include "util/PrintException.hxx"
#include "LogBackend.hxx"
@@ -127,32 +128,37 @@ RunOutput(AudioOutput &ao, AudioFormat audio_format,
fprintf(stderr, "audio_format=%s\n",
ToString(audio_format).c_str());
- size_t frame_size = audio_format.GetFrameSize();
+ const size_t in_frame_size = audio_format.GetFrameSize();
/* play */
- size_t length = 0;
- char buffer[4096];
+ StaticFifoBuffer<std::byte, 4096> buffer;
+
while (true) {
- if (length < sizeof(buffer)) {
- ssize_t nbytes = in_fd.Read(buffer + length,
- sizeof(buffer) - length);
+ {
+ const auto dest = buffer.Write();
+ assert(!dest.empty());
+
+ ssize_t nbytes = in_fd.Read(dest.data, dest.size);
if (nbytes <= 0)
break;
- length += (size_t)nbytes;
+ buffer.Append(nbytes);
}
- size_t play_length = (length / frame_size) * frame_size;
- if (play_length > 0) {
- size_t consumed = ao.Play(buffer, play_length);
+ auto src = buffer.Read();
+ assert(!src.empty());
- assert(consumed <= length);
- assert(consumed % frame_size == 0);
+ src.size -= src.size % in_frame_size;
+ if (src.empty())
+ continue;
- length -= consumed;
- memmove(buffer, buffer + consumed, length);
- }
+ size_t consumed = ao.Play(src.data, src.size);
+
+ assert(consumed <= src.size);
+ assert(consumed % in_frame_size == 0);
+
+ buffer.Consume(consumed);
}
}