[vlc-devel] [PATCH] pulse: use pa_stream_begin_write()

Thomas Guillem thomas at gllm.fr
Mon Feb 11 16:34:49 CET 2019


Instead of using a free callback with a hackish way to get the block pointer.
Even if pa_stream_begin_write() is used to optimize the number of memcpy, this
commit won't change the total number of memcpy. The memcpy is just moved to the
Play() function.

pa_stream_begin_write() is available since 0.9.16. I guess that's why it was
not used since the beginning.
---
 modules/audio_output/pulse.c | 48 ++++++++++++------------------------
 1 file changed, 16 insertions(+), 32 deletions(-)

diff --git a/modules/audio_output/pulse.c b/modules/audio_output/pulse.c
index dcae4e6d4a..8c024006af 100644
--- a/modules/audio_output/pulse.c
+++ b/modules/audio_output/pulse.c
@@ -458,30 +458,6 @@ static int TimeGet(audio_output_t *aout, vlc_tick_t *restrict delay)
     return ret;
 }
 
-/* Memory free callback. The block_t address is in front of the data. */
-static void data_free(void *data)
-{
-    block_t **pp = data, *block;
-
-    memcpy(&block, pp - 1, sizeof (block));
-    block_Release(block);
-}
-
-static void *data_convert(block_t **pp)
-{
-    block_t *block = *pp;
-    /* In most cases, there is enough head room, and this is really cheap: */
-    block = block_Realloc(block, sizeof (block), block->i_buffer);
-    *pp = block;
-    if (unlikely(block == NULL))
-        return NULL;
-
-    memcpy(block->p_buffer, &block, sizeof (block));
-    block->p_buffer += sizeof (block);
-    block->i_buffer -= sizeof (block);
-    return block->p_buffer;
-}
-
 /**
  * Queue one audio frame to the playback stream
  */
@@ -490,11 +466,6 @@ static void Play(audio_output_t *aout, block_t *block, vlc_tick_t date)
     aout_sys_t *sys = aout->sys;
     pa_stream *s = sys->stream;
 
-    const void *ptr = data_convert(&block);
-    if (unlikely(ptr == NULL))
-        return;
-
-    size_t len = block->i_buffer;
 
     /* Note: The core already holds the output FIFO lock at this point.
      * Therefore we must not under any circumstances (try to) acquire the
@@ -515,11 +486,24 @@ static void Play(audio_output_t *aout, block_t *block, vlc_tick_t date)
         pa_operation_unref(pa_stream_flush(s, NULL, NULL));
     }
 #endif
+    do
+    {
+        void *ptr;
+        size_t len = block->i_buffer;
+
+        if (pa_stream_begin_write(s, &ptr, &len))
+            vlc_pa_error(aout, "cannot begin write", sys->context);
 
-    if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0) {
-        vlc_pa_error(aout, "cannot write", sys->context);
-        block_Release(block);
+        memcpy(ptr, block->p_buffer, len);
+        block->p_buffer += len;
+        block->i_buffer -= len;
+
+        if (pa_stream_write(s, ptr, len, NULL, 0, PA_SEEK_RELATIVE) < 0)
+            vlc_pa_error(aout, "cannot write", sys->context);
     }
+    while (unlikely(block->i_buffer > 0));
+
+    block_Release(block);
 
     pa_threaded_mainloop_unlock(sys->mainloop);
 }
-- 
2.20.1



More information about the vlc-devel mailing list