#endif
#include <stdio.h>
+#include <math.h>
#include "echo-cancel.h"
/* NOTE: Make sure the enum and ec_table are maintained in the correct order */
typedef enum {
PA_ECHO_CANCELLER_INVALID = -1,
- PA_ECHO_CANCELLER_SPEEX = 0,
+#ifdef HAVE_SPEEX
+ PA_ECHO_CANCELLER_SPEEX,
+#endif
PA_ECHO_CANCELLER_ADRIAN,
+#ifdef HAVE_WEBRTC
+ PA_ECHO_CANCELLER_WEBRTC,
+#endif
} pa_echo_canceller_method_t;
+#ifdef HAVE_WEBRTC
+#define DEFAULT_ECHO_CANCELLER "webrtc"
+#else
#define DEFAULT_ECHO_CANCELLER "speex"
+#endif
static const pa_echo_canceller ec_table[] = {
+#ifdef HAVE_SPEEX
{
/* Speex */
.init = pa_speex_ec_init,
.run = pa_speex_ec_run,
.done = pa_speex_ec_done,
},
+#endif
{
/* Adrian Andre's NLMS implementation */
.init = pa_adrian_ec_init,
.run = pa_adrian_ec_run,
.done = pa_adrian_ec_done,
},
+#ifdef HAVE_WEBRTC
+ {
+ /* WebRTC's audio processing engine */
+ .init = pa_webrtc_ec_init,
+ .play = pa_webrtc_ec_play,
+ .record = pa_webrtc_ec_record,
+ .set_drift = pa_webrtc_ec_set_drift,
+ .run = pa_webrtc_ec_run,
+ .done = pa_webrtc_ec_done,
+ },
+#endif
};
#define DEFAULT_RATE 32000
* don't give enough accuracy to be able to do that right now.
*/
+struct userdata;
+
+struct pa_echo_canceller_msg {
+ pa_msgobject parent;
+ struct userdata *userdata;
+};
+
+PA_DEFINE_PRIVATE_CLASS(pa_echo_canceller_msg, pa_msgobject);
+#define PA_ECHO_CANCELLER_MSG(o) (pa_echo_canceller_msg_cast(o))
+
struct snapshot {
pa_usec_t sink_now;
pa_usec_t sink_latency;
int64_t recv_counter;
size_t sink_skip;
+ /* Bytes left over from previous iteration */
+ size_t sink_rem;
+ size_t source_rem;
+
pa_atomic_t request_resync;
pa_time_event *time_event;
FILE *captured_file;
FILE *played_file;
FILE *canceled_file;
+ FILE *drift_file;
+
+ pa_bool_t use_volume_sharing;
+
+ struct {
+ pa_cvolume current_volume;
+ } thread_info;
};
static void source_output_snapshot_within_thread(struct userdata *u, struct snapshot *snapshot);
SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
};
+enum {
+ ECHO_CANCELLER_MESSAGE_SET_VOLUME,
+};
+
static int64_t calc_diff(struct userdata *u, struct snapshot *snapshot) {
int64_t buffer, diff_time, buffer_latency;
return 0;
+ case PA_SOURCE_MESSAGE_SET_VOLUME_SYNCED:
+ u->thread_info.current_volume = u->source->reference_volume;
+ break;
}
return pa_source_process_msg(o, code, data, offset, chunk);
apply_diff_time(u, diff_time);
}
-/* 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, to_skip;
- pa_memchunk rchunk, pchunk;
+/* 1. Calculate drift at this point, pass to canceller
+ * 2. Push out playback samples in blocksize chunks
+ * 3. Push out capture samples in blocksize chunks
+ * 4. ???
+ * 5. Profit
+ */
+static void do_push_drift_comp(struct userdata *u) {
+ size_t rlen, plen;
+ pa_memchunk rchunk, pchunk, cchunk;
+ uint8_t *rdata, *pdata, *cdata;
+ float drift;
+ int unused PA_GCC_UNUSED;
- pa_source_output_assert_ref(o);
- pa_source_output_assert_io_context(o);
- pa_assert_se(u = o->userdata);
+ rlen = pa_memblockq_get_length(u->source_memblockq);
+ plen = pa_memblockq_get_length(u->sink_memblockq);
- if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output))) {
- pa_log("push when no link?");
- return;
- }
+ /* Estimate snapshot drift as follows:
+ * pd: amount of data consumed since last time
+ * rd: amount of data consumed since last time
+ *
+ * drift = (pd - rd) / rd;
+ *
+ * We calculate pd and rd as the memblockq length less the number of
+ * samples left from the last iteration (to avoid double counting
+ * those remainder samples.
+ */
+ drift = ((float)(plen - u->sink_rem) - (rlen - u->source_rem)) / ((float)(rlen - u->source_rem));
+ u->sink_rem = plen % u->blocksize;
+ u->source_rem = rlen % u->blocksize;
+
+ /* Now let the canceller work its drift compensation magic */
+ u->ec->set_drift(u->ec, drift);
- 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;
+ if (u->save_aec) {
+ if (u->drift_file)
+ fprintf(u->drift_file, "d %a\n", drift);
}
- /* handle queued messages, do any message sending of our own */
- while (pa_asyncmsgq_process_one(u->asyncmsgq) > 0)
- ;
+ /* Send in the playback samples first */
+ while (plen >= u->blocksize) {
+ pa_memblockq_peek_fixed_size(u->sink_memblockq, u->blocksize, &pchunk);
+ pdata = pa_memblock_acquire(pchunk.memblock);
+ pdata += pchunk.index;
- pa_memblockq_push_align(u->source_memblockq, chunk);
+ u->ec->play(u->ec, pdata);
- rlen = pa_memblockq_get_length(u->source_memblockq);
- plen = pa_memblockq_get_length(u->sink_memblockq);
+ if (u->save_aec) {
+ if (u->drift_file)
+ fprintf(u->drift_file, "p %d\n", u->blocksize);
+ if (u->played_file)
+ unused = fwrite(pdata, 1, u->blocksize, u->played_file);
+ }
- /* Let's not do anything else till we have enough data to process */
- if (rlen < u->blocksize)
- return;
+ pa_memblock_release(pchunk.memblock);
+ pa_memblockq_drop(u->sink_memblockq, u->blocksize);
+ pa_memblock_unref(pchunk.memblock);
- /* See if we need to drop samples in order to sync */
- if (pa_atomic_cmpxchg (&u->request_resync, 1, 0)) {
- do_resync(u);
+ plen -= u->blocksize;
}
- /* 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;
+ /* And now the capture samples */
+ while (rlen >= u->blocksize) {
+ pa_memblockq_peek_fixed_size(u->source_memblockq, u->blocksize, &rchunk);
- if (to_skip) {
- pa_memblockq_peek_fixed_size(u->source_memblockq, to_skip, &rchunk);
- pa_source_post(u->source, &rchunk);
+ rdata = pa_memblock_acquire(rchunk.memblock);
+ rdata += rchunk.index;
- pa_memblock_unref(rchunk.memblock);
- pa_memblockq_drop(u->source_memblockq, u->blocksize);
+ cchunk.index = 0;
+ cchunk.length = u->blocksize;
+ cchunk.memblock = pa_memblock_new(u->source->core->mempool, cchunk.length);
+ cdata = pa_memblock_acquire(cchunk.memblock);
- rlen -= to_skip;
- u->source_skip -= to_skip;
- }
+ u->ec->record(u->ec, rdata, cdata);
- 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);
+ if (u->save_aec) {
+ if (u->drift_file)
+ fprintf(u->drift_file, "c %d\n", u->blocksize);
+ if (u->captured_file)
+ unused = fwrite(rdata, 1, u->blocksize, u->captured_file);
+ if (u->canceled_file)
+ unused = fwrite(cdata, 1, u->blocksize, u->canceled_file);
}
- }
- /* 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_memblock_release(cchunk.memblock);
+ pa_memblock_release(rchunk.memblock);
- pa_memblockq_drop(u->sink_memblockq, to_skip);
+ pa_memblock_unref(rchunk.memblock);
- plen -= to_skip;
- u->sink_skip -= to_skip;
+ pa_source_post(u->source, &cchunk);
+ pa_memblock_unref(cchunk.memblock);
+
+ pa_memblockq_drop(u->source_memblockq, u->blocksize);
+ rlen -= u->blocksize;
}
+}
+
+/* This one's simpler than the drift compensation case -- we just iterate over
+ * the capture buffer, and pass the canceller blocksize bytes of playback and
+ * capture data. */
+static void do_push(struct userdata *u) {
+ size_t rlen, plen;
+ pa_memchunk rchunk, pchunk, cchunk;
+ uint8_t *rdata, *pdata, *cdata;
+ int unused PA_GCC_UNUSED;
+
+ rlen = pa_memblockq_get_length(u->source_memblockq);
+ plen = pa_memblockq_get_length(u->sink_memblockq);
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) {
- uint8_t *rdata, *pdata, *cdata;
- pa_memchunk cchunk;
- int unused;
-
if (plen > u->blocksize) {
/* take fixed block from played samples */
pa_memblockq_peek_fixed_size(u->sink_memblockq, u->blocksize, &pchunk);
}
}
+/* 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, to_skip;
+ pa_memchunk rchunk;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output))) {
+ pa_log("push when no link?");
+ return;
+ }
+
+ 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)
+ ;
+
+ 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);
+ }
+
+ /* 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;
+ }
+
+ 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);
+ }
+ }
+
+ /* 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;
+ }
+
+ /* process and push out samples */
+ if (u->ec->params.drift_compensation)
+ do_push_drift_comp(u);
+ else
+ do_push(u);
+}
+
/* Called from I/O thread context */
static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
struct userdata *u;
pa_assert_ctl_context();
pa_assert_se(u = o->userdata);
- if (u->dead)
+ if (u->dead || u->autoloaded)
return FALSE;
return (u->source != dest) && (u->sink != dest->monitor_of);
pa_sink_input_assert_ref(i);
pa_assert_se(u = i->userdata);
- if (u->dead)
+ if (u->dead || u->autoloaded)
return FALSE;
return u->sink != dest;
pa_source_set_asyncmsgq(u->source, NULL);
if (u->source_auto_desc && dest) {
- const char *z;
+ const char *y, *z;
pa_proplist *pl;
pl = pa_proplist_new();
+ y = pa_proplist_gets(u->sink_input->sink->proplist, PA_PROP_DEVICE_DESCRIPTION);
z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
- pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Source %s on %s",
- pa_proplist_gets(u->source->proplist, "device.echo-cancel.name"), z ? z : dest->name);
+ pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "%s (echo cancelled with %s)", z ? z : dest->name,
+ y ? y : u->sink_input->sink->name);
pa_source_update_proplist(u->source, PA_UPDATE_REPLACE, pl);
pa_proplist_free(pl);
pa_sink_set_asyncmsgq(u->sink, NULL);
if (u->sink_auto_desc && dest) {
- const char *z;
+ const char *y, *z;
pa_proplist *pl;
pl = pa_proplist_new();
+ y = pa_proplist_gets(u->source_output->source->proplist, PA_PROP_DEVICE_DESCRIPTION);
z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
- pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Sink %s on %s",
- pa_proplist_gets(u->sink->proplist, "device.echo-cancel.name"), z ? z : dest->name);
+ pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "%s (echo cancelled with %s)", z ? z : dest->name,
+ y ? y : u->source_output->source->name);
pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
pa_proplist_free(pl);
pa_sink_mute_changed(u->sink, i->muted);
}
+/* Called from main context */
+static int canceller_process_msg_cb(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
+ struct pa_echo_canceller_msg *msg;
+ struct userdata *u;
+
+ pa_assert(o);
+
+ msg = PA_ECHO_CANCELLER_MSG(o);
+ u = msg->userdata;
+
+ switch (code) {
+ case ECHO_CANCELLER_MESSAGE_SET_VOLUME: {
+ pa_cvolume *v = (pa_cvolume *) userdata;
+
+ if (u->use_volume_sharing)
+ pa_source_set_volume(u->source, v, TRUE, FALSE);
+ else
+ pa_source_output_set_volume(u->source_output, v, FALSE, TRUE);
+
+ break;
+ }
+
+ default:
+ pa_assert_not_reached();
+ break;
+ }
+
+ return 0;
+}
+
+/* Called by the canceller, so thread context */
+void pa_echo_canceller_get_capture_volume(pa_echo_canceller *ec, pa_cvolume *v) {
+ *v = ec->msg->userdata->thread_info.current_volume;
+}
+
+/* Called by the canceller, so thread context */
+void pa_echo_canceller_set_capture_volume(pa_echo_canceller *ec, pa_cvolume *v) {
+ if (!pa_cvolume_equal(&ec->msg->userdata->thread_info.current_volume, v)) {
+ pa_cvolume *vol = pa_xnewdup(pa_cvolume, v, 1);
+
+ pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(ec->msg), ECHO_CANCELLER_MESSAGE_SET_VOLUME, vol, 0, NULL,
+ pa_xfree);
+ }
+}
+
static pa_echo_canceller_method_t get_ec_method_from_string(const char *method) {
+#ifdef HAVE_SPEEX
if (pa_streq(method, "speex"))
return PA_ECHO_CANCELLER_SPEEX;
- else if (pa_streq(method, "adrian"))
+#endif
+ if (pa_streq(method, "adrian"))
return PA_ECHO_CANCELLER_ADRIAN;
- else
- return PA_ECHO_CANCELLER_INVALID;
+#ifdef HAVE_WEBRTC
+ if (pa_streq(method, "webrtc"))
+ return PA_ECHO_CANCELLER_WEBRTC;
+#endif
+ return PA_ECHO_CANCELLER_INVALID;
}
/* Common initialisation bits between module-echo-cancel and the standalone test program */
}
u->ec->init = ec_table[ec_method].init;
+ u->ec->play = ec_table[ec_method].play;
+ u->ec->record = ec_table[ec_method].record;
+ u->ec->set_drift = ec_table[ec_method].set_drift;
u->ec->run = ec_table[ec_method].run;
u->ec->done = ec_table[ec_method].done;
pa_sink_new_data sink_data;
pa_memchunk silence;
uint32_t temp;
- pa_bool_t use_volume_sharing = TRUE;
pa_assert(m);
sink_ss = sink_master->sample_spec;
sink_map = sink_master->channel_map;
- if (pa_modargs_get_value_boolean(ma, "use_volume_sharing", &use_volume_sharing) < 0) {
- pa_log("use_volume_sharing= expects a boolean argument");
- goto fail;
- }
-
u = pa_xnew0(struct userdata, 1);
if (!u) {
pa_log("Failed to alloc userdata");
m->userdata = u;
u->dead = FALSE;
+ u->use_volume_sharing = TRUE;
+ if (pa_modargs_get_value_boolean(ma, "use_volume_sharing", &u->use_volume_sharing) < 0) {
+ pa_log("use_volume_sharing= expects a boolean argument");
+ goto fail;
+ }
+
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");
}
}
+ if (u->ec->params.drift_compensation)
+ pa_assert(u->ec->set_drift);
+
/* Create source */
pa_source_new_data_init(&source_data);
source_data.driver = __FILE__;
pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
if (!u->autoloaded)
pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
- pa_proplist_sets(source_data.proplist, "device.echo-cancel.name", source_data.name);
if (pa_modargs_get_proplist(ma, "source_properties", source_data.proplist, PA_UPDATE_REPLACE) < 0) {
pa_log("Invalid properties");
}
if ((u->source_auto_desc = !pa_proplist_contains(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
- const char *z;
+ const char *y, *z;
+ y = pa_proplist_gets(sink_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
z = pa_proplist_gets(source_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
- pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Source %s on %s", source_data.name, z ? z : source_master->name);
+ pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "%s (echo cancelled with %s)",
+ z ? z : source_master->name, y ? y : sink_master->name);
}
u->source = pa_source_new(m->core, &source_data, (source_master->flags & (PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY))
- | (use_volume_sharing ? PA_SOURCE_SHARE_VOLUME_WITH_MASTER : 0));
+ | (u->use_volume_sharing ? PA_SOURCE_SHARE_VOLUME_WITH_MASTER : 0));
pa_source_new_data_done(&source_data);
if (!u->source) {
u->source->update_requested_latency = source_update_requested_latency_cb;
pa_source_set_get_mute_callback(u->source, source_get_mute_cb);
pa_source_set_set_mute_callback(u->source, source_set_mute_cb);
- if (!use_volume_sharing) {
+ if (!u->use_volume_sharing) {
pa_source_set_get_volume_callback(u->source, source_get_volume_cb);
pa_source_set_set_volume_callback(u->source, source_set_volume_cb);
pa_source_enable_decibel_volume(u->source, TRUE);
pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
if (!u->autoloaded)
pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
- pa_proplist_sets(sink_data.proplist, "device.echo-cancel.name", sink_data.name);
if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
pa_log("Invalid properties");
}
if ((u->sink_auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
- const char *z;
+ const char *y, *z;
+ y = pa_proplist_gets(source_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
z = pa_proplist_gets(sink_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
- pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Sink %s on %s", sink_data.name, z ? z : sink_master->name);
+ pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "%s (echo cancelled with %s)",
+ z ? z : sink_master->name, y ? y : source_master->name);
}
u->sink = pa_sink_new(m->core, &sink_data, (sink_master->flags & (PA_SINK_LATENCY | PA_SINK_DYNAMIC_LATENCY))
- | (use_volume_sharing ? PA_SINK_SHARE_VOLUME_WITH_MASTER : 0));
+ | (u->use_volume_sharing ? PA_SINK_SHARE_VOLUME_WITH_MASTER : 0));
pa_sink_new_data_done(&sink_data);
if (!u->sink) {
u->sink->update_requested_latency = sink_update_requested_latency_cb;
u->sink->request_rewind = sink_request_rewind_cb;
pa_sink_set_set_mute_callback(u->sink, sink_set_mute_cb);
- if (!use_volume_sharing) {
+ if (!u->use_volume_sharing) {
pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
pa_sink_enable_decibel_volume(u->sink, TRUE);
}
u->sink_input->state_change = sink_input_state_change_cb;
u->sink_input->may_move_to = sink_input_may_move_to_cb;
u->sink_input->moving = sink_input_moving_cb;
- if (!use_volume_sharing)
+ if (!u->use_volume_sharing)
u->sink_input->volume_changed = sink_input_volume_changed_cb;
u->sink_input->mute_changed = sink_input_mute_changed_cb;
u->sink_input->userdata = u;
goto fail;
}
- if (u->adjust_time > 0)
+ if (u->adjust_time > 0 && !u->ec->params.drift_compensation)
u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
+ else if (u->ec->params.drift_compensation) {
+ pa_log_info("Canceller does drift compensation -- built-in compensation will be disabled");
+ u->adjust_time = 0;
+ /* Perform resync just once to give the canceller a leg up */
+ pa_atomic_store(&u->request_resync, 1);
+ }
if (u->save_aec) {
pa_log("Creating AEC files in /tmp");
u->canceled_file = fopen("/tmp/aec_out.sw", "wb");
if (u->canceled_file == NULL)
perror ("fopen failed");
+ if (u->ec->params.drift_compensation) {
+ u->drift_file = fopen("/tmp/aec_drift.txt", "w");
+ if (u->drift_file == NULL)
+ perror ("fopen failed");
+ }
}
+ u->ec->msg = pa_msgobject_new(pa_echo_canceller_msg);
+ u->ec->msg->parent.process_msg = canceller_process_msg_cb;
+ u->ec->msg->userdata = u;
+
+ u->thread_info.current_volume = u->source->reference_volume;
+
pa_sink_put(u->sink);
pa_source_put(u->source);
pa_sink_input_put(u->sink_input);
pa_source_output_put(u->source_output);
-
pa_modargs_free(ma);
return 0;
fclose(u->captured_file);
if (u->canceled_file)
fclose(u->canceled_file);
+ if (u->drift_file)
+ fclose(u->drift_file);
}
pa_xfree(u);
pa_channel_map source_map, sink_map;
pa_modargs *ma = NULL;
uint8_t *rdata = NULL, *pdata = NULL, *cdata = NULL;
- int ret = 0, unused;
+ int unused PA_GCC_UNUSED;
+ int ret = 0, i;
+ char c;
+ float drift;
pa_memzero(&u, sizeof(u));
- if (argc < 4 || argc > 6) {
+ if (argc < 4 || argc > 7) {
goto usage;
}
- u.ec = pa_xnew0(pa_echo_canceller, 1);
- if (!u.ec) {
- pa_log("Failed to alloc echo canceller");
- goto fail;
- }
-
u.captured_file = fopen(argv[2], "r");
if (u.captured_file == NULL) {
perror ("fopen failed");
goto fail;
}
+ if (u.ec->params.drift_compensation) {
+ if (argc < 7) {
+ pa_log("Drift compensation enabled but drift file not specified");
+ goto fail;
+ }
+
+ u.drift_file = fopen(argv[6], "r");
+
+ if (u.drift_file == NULL) {
+ perror ("fopen failed");
+ goto fail;
+ }
+ }
+
rdata = pa_xmalloc(u.blocksize);
pdata = pa_xmalloc(u.blocksize);
cdata = pa_xmalloc(u.blocksize);
- while (fread(rdata, u.blocksize, 1, u.captured_file) > 0) {
- if (fread(pdata, u.blocksize, 1, u.played_file) == 0) {
- perror("played file ended before captured file");
- break;
+ if (!u.ec->params.drift_compensation) {
+ while (fread(rdata, u.blocksize, 1, u.captured_file) > 0) {
+ if (fread(pdata, u.blocksize, 1, u.played_file) == 0) {
+ perror("Played file ended before captured file");
+ goto fail;
+ }
+
+ u.ec->run(u.ec, rdata, pdata, cdata);
+
+ unused = fwrite(cdata, u.blocksize, 1, u.canceled_file);
}
+ } else {
+ while (fscanf(u.drift_file, "%c", &c) > 0) {
+ switch (c) {
+ case 'd':
+ if (!fscanf(u.drift_file, "%a", &drift)) {
+ perror("Drift file incomplete");
+ goto fail;
+ }
+
+ u.ec->set_drift(u.ec, drift);
+
+ break;
+
+ case 'c':
+ if (!fscanf(u.drift_file, "%d", &i)) {
+ perror("Drift file incomplete");
+ goto fail;
+ }
+
+ if (fread(rdata, i, 1, u.captured_file) <= 0) {
+ perror("Captured file ended prematurely");
+ goto fail;
+ }
+
+ u.ec->record(u.ec, rdata, cdata);
- u.ec->run(u.ec, rdata, pdata, cdata);
+ unused = fwrite(cdata, i, 1, u.canceled_file);
+
+ break;
+
+ case 'p':
+ if (!fscanf(u.drift_file, "%d", &i)) {
+ perror("Drift file incomplete");
+ goto fail;
+ }
+
+ if (fread(pdata, i, 1, u.played_file) <= 0) {
+ perror("Played file ended prematurely");
+ goto fail;
+ }
+
+ u.ec->play(u.ec, pdata);
+
+ break;
+ }
+ }
- unused = fwrite(cdata, u.blocksize, 1, u.canceled_file);
+ if (fread(rdata, i, 1, u.captured_file) > 0)
+ pa_log("All capture data was not consumed");
+ if (fread(pdata, i, 1, u.played_file) > 0)
+ pa_log("All playback data was not consumed");
}
u.ec->done(u.ec);
fclose(u.captured_file);
fclose(u.played_file);
fclose(u.canceled_file);
+ if (u.drift_file)
+ fclose(u.drift_file);
out:
pa_xfree(rdata);
return ret;
usage:
- pa_log("Usage: %s play_file rec_file out_file [module args] [aec_args]",argv[0]);
+ pa_log("Usage: %s play_file rec_file out_file [module args] [aec_args] [drift_file]", argv[0]);
fail:
ret = -1;