From f5d104e7af6dd14a3c0c85c2d0d3319eac9826f2 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Tue, 12 Jul 2022 12:39:08 +0200
Subject: [PATCH] output/ao: simplify write_size checks

---
 src/output/plugins/AoOutputPlugin.cxx | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/src/output/plugins/AoOutputPlugin.cxx b/src/output/plugins/AoOutputPlugin.cxx
index ce01f7e29..e4c938c2d 100644
--- a/src/output/plugins/AoOutputPlugin.cxx
+++ b/src/output/plugins/AoOutputPlugin.cxx
@@ -58,6 +58,8 @@ class AoOutput final : AudioOutput, SafeSingleton<AoInit> {
 
 	size_t frame_size;
 
+	std::size_t max_size;
+
 	explicit AoOutput(const ConfigBlock &block);
 	~AoOutput() override;
 
@@ -173,6 +175,11 @@ AoOutput::Open(AudioFormat &audio_format)
 
 	frame_size = audio_format.GetFrameSize();
 
+	/* round down to a multiple of the frame size */
+	/* no matter how small "write_size" was configured, we must
+	   pass at least one frame to libao */
+	max_size = std::max(write_size / frame_size, std::size_t{1}) * frame_size;
+
 	format.rate = audio_format.sample_rate;
 	format.byte_format = AO_FMT_NATIVE;
 	format.channels = audio_format.channels;
@@ -193,16 +200,8 @@ AoOutput::Play(const void *chunk, size_t size)
 {
 	assert(size % frame_size == 0);
 
-	if (size > write_size) {
-		/* round down to a multiple of the frame size */
-		size = (write_size / frame_size) * frame_size;
-
-		if (size < frame_size)
-			/* no matter how small "write_size" was
-			   configured, we must pass at least one frame
-			   to libao */
-			size = frame_size;
-	}
+	if (size > max_size)
+		size = max_size;
 
 	/* For whatever reason, libao wants a non-const pointer.
 	   Let's hope it does not write to the buffer, and use the