Adjust buffer for youtube music (#412)
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Wed, 13 Jul 2022 23:57:36 +0000 (01:57 +0200)
committerGitHub <noreply@github.com>
Wed, 13 Jul 2022 23:57:36 +0000 (01:57 +0200)
* Decrease the buffer a bit to better support Youtube Music

which throttles the music stream so we can't buffer too far ahead

* allow more buffer for next tracks

music_assistant/controllers/streams.py

index 98073956b1dae6f0f019c40dcc629d72176a6a51..77fd25838314e29684ebfa556498930c410732db 100644 (file)
@@ -629,7 +629,7 @@ class QueueStream:
             crossfade_duration = self.queue.settings.crossfade_duration
             crossfade_size = sample_size_per_second * crossfade_duration
             # buffer_duration has some overhead to account for padded silence
-            buffer_duration = (crossfade_duration or 2) * 2
+            buffer_duration = (crossfade_duration or 2) * 2 if track_count > 1 else 1
             # predict total size to expect for this track from duration
             stream_duration = (queue_track.duration or 0) - seek_position
 
@@ -672,7 +672,13 @@ class QueueStream:
                 if (
                     streamdetails.media_type == MediaType.ANNOUNCEMENT
                     or not stream_duration
+                    or stream_duration < buffer_duration
                 ):
+                    # handle edge case where we have a previous chunk in buffer
+                    # and the next track is too short
+                    if last_fadeout_part:
+                        yield last_fadeout_part
+                        last_fadeout_part = b""
                     yield chunk
                     bytes_written += len(chunk)
                     continue