From 43da4c0ecaf23ebb7b282bdfe3b5c6bfc3ab6c41 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Tue, 23 Dec 2014 20:34:45 +0100
Subject: [PATCH] input/mms: limit the mmsx_read() size

---
 NEWS                                 | 2 ++
 src/input/plugins/MmsInputPlugin.cxx | 7 +++++++
 2 files changed, 9 insertions(+)

diff --git a/NEWS b/NEWS
index 54e04ab9a..f09d77217 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,6 @@
 ver 0.19.8 (not yet released)
+* input
+  - mms: reduce delay at the beginning of playback
 * decoder
   - dsdiff, dsf: allow ID3 tags larger than 4 kB
 
diff --git a/src/input/plugins/MmsInputPlugin.cxx b/src/input/plugins/MmsInputPlugin.cxx
index 1aed9c662..df291bc84 100644
--- a/src/input/plugins/MmsInputPlugin.cxx
+++ b/src/input/plugins/MmsInputPlugin.cxx
@@ -92,6 +92,13 @@ input_mms_open(const char *url,
 size_t
 MmsInputStream::ThreadRead(void *ptr, size_t read_size, Error &error)
 {
+	/* unfortunately, mmsx_read() blocks until the whole buffer
+	   has been filled; to avoid big latencies, limit the size of
+	   each chunk we read to a reasonable size */
+	constexpr size_t MAX_CHUNK = 16384;
+	if (read_size > MAX_CHUNK)
+		read_size = MAX_CHUNK;
+
 	int nbytes = mmsx_read(nullptr, mms, (char *)ptr, read_size);
 	if (nbytes <= 0) {
 		if (nbytes < 0)