SDL: audio: PlayDevice now passes the buffer, too, for convenience.

From 258bc9efed5a75d8a51c6d60c43383ace1e67515 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 3 Jul 2023 20:34:15 -0400
Subject: [PATCH] audio: PlayDevice now passes the buffer, too, for
 convenience.

---
 src/audio/SDL_audio.c                 |  4 ++--
 src/audio/SDL_sysaudio.h              |  2 +-
 src/audio/disk/SDL_diskaudio.c        |  6 ++----
 src/audio/pipewire/SDL_pipewire.c     |  2 +-
 src/audio/pulseaudio/SDL_pulseaudio.c | 12 +++++-------
 5 files changed, 11 insertions(+), 15 deletions(-)

diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index aa1d66d4bc35..a031686abdf7 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -415,7 +415,7 @@ void SDL_AudioDeviceDisconnected(SDL_AudioDevice *device)
 static void SDL_AudioThreadInit_Default(SDL_AudioDevice *device) { /* no-op. */ }
 static void SDL_AudioThreadDeinit_Default(SDL_AudioDevice *device) { /* no-op. */ }
 static void SDL_AudioWaitDevice_Default(SDL_AudioDevice *device) { /* no-op. */ }
-static void SDL_AudioPlayDevice_Default(SDL_AudioDevice *device, int buffer_size) { /* no-op. */ }
+static void SDL_AudioPlayDevice_Default(SDL_AudioDevice *device, const Uint8 *buffer, int buffer_size) { /* no-op. */ }
 static void SDL_AudioWaitCaptureDevice_Default(SDL_AudioDevice *device) { /* no-op. */ }
 static void SDL_AudioFlushCapture_Default(SDL_AudioDevice *device) { /* no-op. */ }
 static void SDL_AudioCloseDevice_Default(SDL_AudioDevice *device) { /* no-op. */ }
@@ -738,7 +738,7 @@ SDL_bool SDL_OutputAudioThreadIterate(SDL_AudioDevice *device)
         }
 
         // !!! FIXME: have PlayDevice return a value and do disconnects in here with it.
-        current_audio.impl.PlayDevice(device, buffer_size);  // this SHOULD NOT BLOCK, as we are holding a lock right now. Block in WaitDevice!
+        current_audio.impl.PlayDevice(device, mix_buffer, buffer_size);  // this SHOULD NOT BLOCK, as we are holding a lock right now. Block in WaitDevice!
     }
 
     SDL_UnlockMutex(device->lock);
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index ff3d1755c8b8..ecf2fcb0ac38 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -108,7 +108,7 @@ typedef struct SDL_AudioDriverImpl
     void (*ThreadInit)(SDL_AudioDevice *device);   /* Called by audio thread at start */
     void (*ThreadDeinit)(SDL_AudioDevice *device); /* Called by audio thread at end */
     void (*WaitDevice)(SDL_AudioDevice *device);
-    void (*PlayDevice)(SDL_AudioDevice *device, int buffer_size);
+    void (*PlayDevice)(SDL_AudioDevice *device, const Uint8 *buffer, int buflen);  // buffer and buflen are always from GetDeviceBuf, passed here for convenience.
     Uint8 *(*GetDeviceBuf)(SDL_AudioDevice *device, int *buffer_size);
     void (*WaitCaptureDevice)(SDL_AudioDevice *device);
     int (*CaptureFromDevice)(SDL_AudioDevice *device, void *buffer, int buflen);
diff --git a/src/audio/disk/SDL_diskaudio.c b/src/audio/disk/SDL_diskaudio.c
index e96014ce957c..0cfa3b7a278c 100644
--- a/src/audio/disk/SDL_diskaudio.c
+++ b/src/audio/disk/SDL_diskaudio.c
@@ -41,11 +41,9 @@ static void DISKAUDIO_WaitDevice(SDL_AudioDevice *device)
     SDL_Delay(device->hidden->io_delay);
 }
 
-static void DISKAUDIO_PlayDevice(SDL_AudioDevice *device, int buffer_size)
+static void DISKAUDIO_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buffer_size)
 {
-    const Sint64 written = SDL_RWwrite(device->hidden->io,
-                                       device->hidden->mixbuf,
-                                       buffer_size);
+    const Sint64 written = SDL_RWwrite(device->hidden->io, buffer, buffer_size);
 
     /* If we couldn't write, assume fatal error for now */
     if (written != buffer_size) {
diff --git a/src/audio/pipewire/SDL_pipewire.c b/src/audio/pipewire/SDL_pipewire.c
index fc8efc539ae6..eac531bf19f2 100644
--- a/src/audio/pipewire/SDL_pipewire.c
+++ b/src/audio/pipewire/SDL_pipewire.c
@@ -948,7 +948,7 @@ static Uint8 *PIPEWIRE_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
     return (Uint8 *) spa_buf->datas[0].data;
 }
 
-static void PIPEWIRE_PlayDevice(SDL_AudioDevice *device, int buffer_size)
+static void PIPEWIRE_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buffer_size)
 {
     struct pw_stream *stream = device->hidden->stream;
     struct pw_buffer *pw_buf = device->hidden->pw_buf;
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.c b/src/audio/pulseaudio/SDL_pulseaudio.c
index 0852b2cc4c3b..c0462f786a13 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.c
+++ b/src/audio/pulseaudio/SDL_pulseaudio.c
@@ -388,18 +388,16 @@ static void PULSEAUDIO_WaitDevice(SDL_AudioDevice *device)
     PULSEAUDIO_pa_threaded_mainloop_unlock(pulseaudio_threaded_mainloop);
 }
 
-static void PULSEAUDIO_PlayDevice(SDL_AudioDevice *device, int buffer_size)
+static void PULSEAUDIO_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buffer_size)
 {
     struct SDL_PrivateAudioData *h = device->hidden;
-    const int available = buffer_size;
-    int rc;
 
     /*printf("PULSEAUDIO PLAYDEVICE START! mixlen=%d\n", available);*/
 
-    SDL_assert(h->bytes_requested >= available);
+    SDL_assert(h->bytes_requested >= buffer_size);
 
     PULSEAUDIO_pa_threaded_mainloop_lock(pulseaudio_threaded_mainloop);
-    rc = PULSEAUDIO_pa_stream_write(h->stream, h->mixbuf, available, NULL, 0LL, PA_SEEK_RELATIVE);
+    const int rc = PULSEAUDIO_pa_stream_write(h->stream, buffer, buffer_size, NULL, 0LL, PA_SEEK_RELATIVE);
     PULSEAUDIO_pa_threaded_mainloop_unlock(pulseaudio_threaded_mainloop);
 
     if (rc < 0) {
@@ -407,8 +405,8 @@ static void PULSEAUDIO_PlayDevice(SDL_AudioDevice *device, int buffer_size)
         return;
     }
 
-    /*printf("PULSEAUDIO FEED! nbytes=%u\n", (unsigned int) available);*/
-    h->bytes_requested -= available;
+    /*printf("PULSEAUDIO FEED! nbytes=%d\n", buffer_size);*/
+    h->bytes_requested -= buffer_size;
 
     /*printf("PULSEAUDIO PLAYDEVICE END! written=%d\n", written);*/
 }