[04/10] bluetooth: Allow bluetooth to use alternative smoother code

Submitted by Georg Chini on April 9, 2018, 4:57 p.m.

Details

Message ID 20180409165748.17459-5-georg@chini.tk
State New
Series "pulsecore: Add new time smoother"
Headers show

Commit Message

Georg Chini April 9, 2018, 4:57 p.m.
---
 src/modules/bluetooth/module-bluez5-device.c | 57 ++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 4 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/modules/bluetooth/module-bluez5-device.c b/src/modules/bluetooth/module-bluez5-device.c
index d40bbb0c..935ee2ce 100644
--- a/src/modules/bluetooth/module-bluez5-device.c
+++ b/src/modules/bluetooth/module-bluez5-device.c
@@ -43,7 +43,12 @@ 
 #include <pulsecore/socket-util.h>
 #include <pulsecore/thread.h>
 #include <pulsecore/thread-mq.h>
+
+#ifdef USE_SMOOTHER_2
+#include <pulsecore/time-smoother_2.h>
+#else
 #include <pulsecore/time-smoother.h>
+#endif
 
 #include "a2dp-codecs.h"
 #include "bluez5-util.h"
@@ -147,7 +152,13 @@  struct userdata {
     uint64_t read_index;
     uint64_t write_index;
     pa_usec_t started_at;
+
+#ifdef USE_SMOOTHER_2
+    pa_smoother_2 *read_smoother;
+#else
     pa_smoother *read_smoother;
+#endif
+
     pa_memchunk write_memchunk;
     pa_sample_spec sample_spec;
     struct sbc_info sbc_info;
@@ -408,8 +419,13 @@  static int sco_process_push(struct userdata *u) {
     u->source_buffered_blocks++;
 
     if (u->source_buffered_blocks >= max_blocks) {
+#ifdef USE_SMOOTHER_2
+        pa_smoother_2_resume(u->read_smoother, tstamp);
+        pa_smoother_2_put(u->read_smoother, tstamp, u->read_index);
+#else
         pa_smoother_put(u->read_smoother, tstamp, pa_bytes_to_usec(u->read_index, &u->sample_spec));
         pa_smoother_resume(u->read_smoother, tstamp, true);
+#endif
 
         u->source_buffer.length = u->source_buffer.index;
         u->source_buffer.index = 0;
@@ -663,8 +679,14 @@  static int a2dp_process_push(struct userdata *u) {
         }
 
         u->read_index += (uint64_t) total_written;
+
+#ifdef USE_SMOOTHER_2
+        pa_smoother_2_resume(u->read_smoother, tstamp);
+        pa_smoother_2_put(u->read_smoother, tstamp, u->read_index);
+#else
         pa_smoother_put(u->read_smoother, tstamp, pa_bytes_to_usec(u->read_index, &u->sample_spec));
         pa_smoother_resume(u->read_smoother, tstamp, true);
+#endif
 
         memchunk.length -= to_write;
 
@@ -791,7 +813,11 @@  static void teardown_stream(struct userdata *u) {
     }
 
     if (u->read_smoother) {
+#ifdef USE_SMOOTHER_2
+        pa_smoother_2_free(u->read_smoother);
+#else
         pa_smoother_free(u->read_smoother);
+#endif
         u->read_smoother = NULL;
     }
 
@@ -935,7 +961,11 @@  static void setup_stream(struct userdata *u) {
     u->stream_setup_done = true;
 
     if (u->source)
+#ifdef USE_SMOOTHER_2
+        u->read_smoother = pa_smoother_2_new(5*PA_USEC_PER_SEC, pa_rtclock_now(), pa_frame_size(&u->sample_spec), u->sample_spec.rate);
+#else
         u->read_smoother = pa_smoother_new(PA_USEC_PER_SEC, 2*PA_USEC_PER_SEC, true, true, 10, pa_rtclock_now(), true);
+#endif
 }
 
 /* Called from I/O thread, returns true if the transport was acquired or
@@ -962,13 +992,19 @@  static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
     switch (code) {
 
         case PA_SOURCE_MESSAGE_GET_LATENCY: {
-            int64_t wi, ri;
+#ifndef USE_SMOOTHER_2
+             int64_t wi, ri;
+#endif
 
             if (u->read_smoother) {
+#ifdef USE_SMOOTHER_2
+                *((int64_t*) data) = u->source->thread_info.fixed_latency - pa_smoother_2_get_delay(u->read_smoother, pa_rtclock_now(), u->read_index);
+#else
                 wi = pa_smoother_get(u->read_smoother, pa_rtclock_now());
                 ri = pa_bytes_to_usec(u->read_index, &u->sample_spec);
 
                 *((int64_t*) data) = u->source->thread_info.fixed_latency + wi - ri;
+#endif
                 *((int64_t*) data) += u->source_buffered_blocks * pa_bytes_to_usec(u->read_block_size, &u->source->sample_spec);
             } else
                 *((int64_t*) data) = 0;
@@ -1004,8 +1040,11 @@  static int source_set_state_in_io_thread_cb(pa_source *s, pa_source_state_t new_
                 transport_release(u);
 
             if (u->read_smoother)
+#ifdef USE_SMOOTHER_2
+                pa_smoother_2_pause(u->read_smoother, pa_rtclock_now());
+#else
                 pa_smoother_pause(u->read_smoother, pa_rtclock_now());
-
+#endif
             break;
 
         case PA_SOURCE_IDLE:
@@ -1139,17 +1178,23 @@  static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
     switch (code) {
 
         case PA_SINK_MESSAGE_GET_LATENCY: {
-            int64_t wi = 0, ri = 0;
+            int64_t wi, ri, delay = 0;
 
             if (u->read_smoother) {
+#ifdef USE_SMOOTHER_2
+                delay = pa_smoother_2_get_delay(u->read_smoother, pa_rtclock_now(), u->write_index + u->write_block_size);
+#else
                 ri = pa_smoother_get(u->read_smoother, pa_rtclock_now());
                 wi = pa_bytes_to_usec(u->write_index + u->write_block_size, &u->sample_spec);
+                delay = wi - ri;
+#endif
             } else if (u->started_at) {
                 ri = pa_rtclock_now() - u->started_at;
                 wi = pa_bytes_to_usec(u->write_index, &u->sample_spec);
+                delay = wi - ri;
             }
 
-            *((int64_t*) data) = u->sink->thread_info.fixed_latency + wi - ri;
+            *((int64_t*) data) = u->sink->thread_info.fixed_latency + delay;
 
             return 0;
         }
@@ -1829,7 +1874,11 @@  static void stop_thread(struct userdata *u) {
     }
 
     if (u->read_smoother) {
+#ifdef USE_SMOOTHER_2
+        pa_smoother_2_free(u->read_smoother);
+#else
         pa_smoother_free(u->read_smoother);
+#endif
         u->read_smoother = NULL;
     }
 }