]> code.delx.au - pulseaudio/blobdiff - src/modules/echo-cancel/module-echo-cancel.c
echo-cancel: Add the WebRTC echo canceller
[pulseaudio] / src / modules / echo-cancel / module-echo-cancel.c
index 10f411887844f965ddcc2553b68ee94153414669..7360b270d7609f51181c6d0092b77dfe1f7844fe 100644 (file)
@@ -66,6 +66,7 @@ PA_MODULE_USAGE(
           "sink_properties=<properties for the sink> "
           "sink_master=<name of sink to filter> "
           "adjust_time=<how often to readjust rates in s> "
+          "adjust_threshold=<how much drift to readjust after in ms> "
           "format=<sample format> "
           "rate=<sample rate> "
           "channels=<number of channels> "
@@ -82,6 +83,9 @@ typedef enum {
     PA_ECHO_CANCELLER_INVALID = -1,
     PA_ECHO_CANCELLER_SPEEX = 0,
     PA_ECHO_CANCELLER_ADRIAN,
+#ifdef HAVE_WEBRTC
+    PA_ECHO_CANCELLER_WEBRTC,
+#endif
 } pa_echo_canceller_method_t;
 
 #define DEFAULT_ECHO_CANCELLER "speex"
@@ -99,16 +103,29 @@ static const pa_echo_canceller ec_table[] = {
         .run                    = pa_adrian_ec_run,
         .done                   = pa_adrian_ec_done,
     },
+#ifdef HAVE_WEBRTC
+    {
+        /* WebRTC's audio processing engine */
+        .init                   = pa_webrtc_ec_init,
+        .run                    = pa_webrtc_ec_run,
+        .done                   = pa_webrtc_ec_done,
+    },
+#endif
 };
 
 #define DEFAULT_RATE 32000
 #define DEFAULT_CHANNELS 1
 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
+#define DEFAULT_ADJUST_TOLERANCE (5*PA_USEC_PER_MSEC)
 #define DEFAULT_SAVE_AEC FALSE
 #define DEFAULT_AUTOLOADED FALSE
 
 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
 
+/* Can only be used in main context */
+#define IS_ACTIVE(u) ((pa_source_get_state((u)->source) == PA_SOURCE_RUNNING) && \
+                      (pa_sink_get_state((u)->sink) == PA_SINK_RUNNING))
+
 /* This module creates a new (virtual) source and sink.
  *
  * The data sent to the new sink is kept in a memblockq before being
@@ -166,7 +183,6 @@ struct userdata {
     pa_bool_t need_realign;
 
     /* to wakeup the source I/O thread */
-    pa_bool_t in_push;
     pa_asyncmsgq *asyncmsgq;
     pa_rtpoll_item *rtpoll_item_read, *rtpoll_item_write;
 
@@ -186,9 +202,9 @@ struct userdata {
 
     pa_atomic_t request_resync;
 
-    int active_mask;
     pa_time_event *time_event;
     pa_usec_t adjust_time;
+    int adjust_threshold;
 
     FILE *captured_file;
     FILE *played_file;
@@ -205,6 +221,7 @@ static const char* const valid_modargs[] = {
     "sink_properties",
     "sink_master",
     "adjust_time",
+    "adjust_threshold",
     "format",
     "rate",
     "channels",
@@ -275,7 +292,7 @@ static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct tim
     pa_assert(u->time_event == e);
     pa_assert_ctl_context();
 
-    if (u->active_mask != 3)
+    if (!IS_ACTIVE(u))
         return;
 
     /* update our snapshots */
@@ -298,7 +315,7 @@ static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct tim
         new_rate = base_rate;
     }
     else {
-        if (diff_time > 1000) {
+        if (diff_time > u->adjust_threshold) {
             /* diff too big, quickly adjust */
             pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME,
                 NULL, diff_time, NULL, NULL);
@@ -400,20 +417,17 @@ static int source_set_state_cb(pa_source *s, pa_source_state_t state) {
         !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
         return 0;
 
-    pa_log_debug("Source state %d %d", state, u->active_mask);
-
     if (state == PA_SOURCE_RUNNING) {
         /* restart timer when both sink and source are active */
-        u->active_mask |= 1;
-        if (u->active_mask == 3)
+        if (IS_ACTIVE(u) && u->adjust_time)
             pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
 
         pa_atomic_store(&u->request_resync, 1);
         pa_source_output_cork(u->source_output, FALSE);
     } else if (state == PA_SOURCE_SUSPENDED) {
-        u->active_mask &= ~1;
         pa_source_output_cork(u->source_output, TRUE);
     }
+
     return 0;
 }
 
@@ -428,20 +442,17 @@ static int sink_set_state_cb(pa_sink *s, pa_sink_state_t state) {
         !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
         return 0;
 
-    pa_log_debug("Sink state %d %d", state, u->active_mask);
-
     if (state == PA_SINK_RUNNING) {
         /* restart timer when both sink and source are active */
-        u->active_mask |= 2;
-        if (u->active_mask == 3)
+        if (IS_ACTIVE(u) && u->adjust_time)
             pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
 
         pa_atomic_store(&u->request_resync, 1);
         pa_sink_input_cork(u->sink_input, FALSE);
     } else if (state == PA_SINK_SUSPENDED) {
-        u->active_mask &= ~2;
         pa_sink_input_cork(u->sink_input, TRUE);
     }
+
     return 0;
 }
 
@@ -642,7 +653,8 @@ static void do_resync(struct userdata *u) {
 /* Called from input thread context */
 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
     struct userdata *u;
-    size_t rlen, plen;
+    size_t rlen, plen, to_skip;
+    pa_memchunk rchunk, pchunk;
 
     pa_source_output_assert_ref(o);
     pa_source_output_assert_io_context(o);
@@ -653,47 +665,78 @@ static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk)
         return;
     }
 
-    /* handle queued messages */
-    u->in_push = TRUE;
+    if (PA_UNLIKELY(u->source->thread_info.state != PA_SOURCE_RUNNING ||
+                    u->sink->thread_info.state != PA_SINK_RUNNING)) {
+        pa_source_post(u->source, chunk);
+        return;
+    }
+
+    /* handle queued messages, do any message sending of our own */
     while (pa_asyncmsgq_process_one(u->asyncmsgq) > 0)
         ;
-    u->in_push = FALSE;
 
+    pa_memblockq_push_align(u->source_memblockq, chunk);
+
+    rlen = pa_memblockq_get_length(u->source_memblockq);
+    plen = pa_memblockq_get_length(u->sink_memblockq);
+
+    /* Let's not do anything else till we have enough data to process */
+    if (rlen < u->blocksize)
+        return;
+
+    /* See if we need to drop samples in order to sync */
     if (pa_atomic_cmpxchg (&u->request_resync, 1, 0)) {
         do_resync(u);
     }
 
-    pa_memblockq_push_align(u->source_memblockq, chunk);
+    /* Okay, skip cancellation for skipped source samples if needed. */
+    if (PA_UNLIKELY(u->source_skip)) {
+        /* The slightly tricky bit here is that we drop all but modulo
+         * blocksize bytes and then adjust for that last bit on the sink side.
+         * We do this because the source data is coming at a fixed rate, which
+         * means the only way to try to catch up is drop sink samples and let
+         * the canceller cope up with this. */
+        to_skip = rlen >= u->source_skip ? u->source_skip : rlen;
+        to_skip -= to_skip % u->blocksize;
+
+        if (to_skip) {
+            pa_memblockq_peek_fixed_size(u->source_memblockq, to_skip, &rchunk);
+            pa_source_post(u->source, &rchunk);
+
+            pa_memblock_unref(rchunk.memblock);
+            pa_memblockq_drop(u->source_memblockq, u->blocksize);
+
+            rlen -= to_skip;
+            u->source_skip -= to_skip;
+        }
 
-    rlen = pa_memblockq_get_length(u->source_memblockq);
-    plen = pa_memblockq_get_length(u->sink_memblockq);
+        if (rlen && u->source_skip % u->blocksize) {
+            u->sink_skip += u->blocksize - (u->source_skip % u->blocksize);
+            u->source_skip -= (u->source_skip % u->blocksize);
+        }
+    }
 
-    while (rlen >= u->blocksize) {
-        pa_memchunk rchunk, pchunk;
+    /* And for the sink, these samples have been played back already, so we can
+     * just drop them and get on with it. */
+    if (PA_UNLIKELY(u->sink_skip)) {
+        to_skip = plen >= u->sink_skip ? u->sink_skip : plen;
+
+        pa_memblockq_drop(u->sink_memblockq, to_skip);
+
+        plen -= to_skip;
+        u->sink_skip -= to_skip;
+    }
 
+    while (rlen >= u->blocksize) {
         /* take fixed block from recorded samples */
         pa_memblockq_peek_fixed_size(u->source_memblockq, u->blocksize, &rchunk);
 
-        if (plen > u->blocksize && u->source_skip == 0) {
+        if (plen > u->blocksize) {
             uint8_t *rdata, *pdata, *cdata;
             pa_memchunk cchunk;
             int unused;
 
-            if (u->sink_skip) {
-                size_t to_skip;
-
-                if (u->sink_skip > plen)
-                    to_skip = plen;
-                else
-                    to_skip = u->sink_skip;
-
-                pa_memblockq_drop(u->sink_memblockq, to_skip);
-                plen -= to_skip;
-
-                u->sink_skip -= to_skip;
-            }
-
-            if (plen > u->blocksize && u->sink_skip == 0) {
+            if (plen > u->blocksize) {
                 /* take fixed block from played samples */
                 pa_memblockq_peek_fixed_size(u->sink_memblockq, u->blocksize, &pchunk);
 
@@ -745,16 +788,6 @@ static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk)
 
         pa_memblockq_drop(u->source_memblockq, u->blocksize);
         rlen -= u->blocksize;
-
-        if (u->source_skip) {
-            if (u->source_skip > u->blocksize) {
-                u->source_skip -= u->blocksize;
-            }
-            else {
-                u->sink_skip += (u->blocksize - u->source_skip);
-                u->source_skip = 0;
-            }
-        }
     }
 }
 
@@ -851,7 +884,7 @@ static int source_output_process_msg_cb(pa_msgobject *obj, int code, void *data,
 
             pa_source_output_assert_io_context(u->source_output);
 
-            if (PA_SOURCE_IS_OPENED(u->source_output->source->thread_info.state))
+            if (u->source_output->source->thread_info.state == PA_SOURCE_RUNNING)
                 pa_memblockq_push_align(u->sink_memblockq, chunk);
             else
                 pa_memblockq_flush_write(u->sink_memblockq, TRUE);
@@ -1318,6 +1351,10 @@ static pa_echo_canceller_method_t get_ec_method_from_string(const char *method)
         return PA_ECHO_CANCELLER_SPEEX;
     else if (pa_streq(method, "adrian"))
         return PA_ECHO_CANCELLER_ADRIAN;
+#ifdef HAVE_WEBRTC
+    else if (pa_streq(method, "webrtc"))
+        return PA_ECHO_CANCELLER_WEBRTC;
+#endif
     else
         return PA_ECHO_CANCELLER_INVALID;
 }
@@ -1365,7 +1402,7 @@ int pa__init(pa_module*m) {
     pa_source_new_data source_data;
     pa_sink_new_data sink_data;
     pa_memchunk silence;
-    uint32_t adjust_time_sec;
+    uint32_t temp;
     pa_bool_t use_volume_sharing = TRUE;
 
     pa_assert(m);
@@ -1415,17 +1452,28 @@ int pa__init(pa_module*m) {
     m->userdata = u;
     u->dead = FALSE;
 
-    adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
-    if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
+    temp = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
+    if (pa_modargs_get_value_u32(ma, "adjust_time", &temp) < 0) {
         pa_log("Failed to parse adjust_time value");
         goto fail;
     }
 
-    if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
-        u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
+    if (temp != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
+        u->adjust_time = temp * PA_USEC_PER_SEC;
     else
         u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
 
+    temp = DEFAULT_ADJUST_TOLERANCE / PA_USEC_PER_MSEC;
+    if (pa_modargs_get_value_u32(ma, "adjust_threshold", &temp) < 0) {
+        pa_log("Failed to parse adjust_threshold value");
+        goto fail;
+    }
+
+    if (temp != DEFAULT_ADJUST_TOLERANCE / PA_USEC_PER_MSEC)
+        u->adjust_threshold = temp * PA_USEC_PER_MSEC;
+    else
+        u->adjust_threshold = DEFAULT_ADJUST_TOLERANCE;
+
     u->save_aec = DEFAULT_SAVE_AEC;
     if (pa_modargs_get_value_boolean(ma, "save_aec", &u->save_aec) < 0) {
         pa_log("Failed to parse save_aec value");
@@ -1640,9 +1688,6 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    /* our source and sink are not suspended when we create them */
-    u->active_mask = 3;
-
     if (u->adjust_time > 0)
         u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
 
@@ -1738,6 +1783,15 @@ void pa__done(pa_module*m) {
     if (u->asyncmsgq)
         pa_asyncmsgq_unref(u->asyncmsgq);
 
+    if (u->save_aec) {
+        if (u->played_file)
+            fclose(u->played_file);
+        if (u->captured_file)
+            fclose(u->captured_file);
+        if (u->canceled_file)
+            fclose(u->canceled_file);
+    }
+
     pa_xfree(u);
 }