From 3302d8c1eae9dd4c33a108a4fa9f85f69ad91037 Mon Sep 17 00:00:00 2001
From: Simon Morlat <simon.morlat@linphone.org>
Date: Mon, 24 Jan 2011 15:58:24 +0100
Subject: [PATCH] echo calibrator ready

---
 coreapi/Makefile.am          | 13 +++++++++++--
 coreapi/linphonecore.c       | 13 +++++++++++++
 coreapi/linphonecore_utils.h | 17 +++++++++++++++++
 coreapi/private.h            | 23 +++++++++++++++++++++++
 mediastreamer2               |  2 +-
 oRTP                         |  2 +-
 6 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/coreapi/Makefile.am b/coreapi/Makefile.am
index 4835dc74cc..8f88045f56 100644
--- a/coreapi/Makefile.am
+++ b/coreapi/Makefile.am
@@ -35,7 +35,8 @@ liblinphone_la_SOURCES=\
 	linphonecall.c \
 	sipsetup.c sipsetup.h \
 	siplogin.c \
-	lsd.c linphonecore_utils.h
+	lsd.c linphonecore_utils.h \
+	ec-calibrator.c
 
 
 liblinphone_la_LDFLAGS= -version-info $(LIBLINPHONE_SO_VERSION) -no-undefined
@@ -49,7 +50,7 @@ if BUILD_WIN32
 liblinphone_la_LIBADD+=$(top_builddir)/oRTP/src/libortp.la
 endif
 
-noinst_PROGRAMS=test_lsd
+noinst_PROGRAMS=test_lsd test_ecc
 
 test_lsd_SOURCES=test_lsd.c
 
@@ -57,6 +58,14 @@ test_lsd_LDADD=liblinphone.la \
 							$(MEDIASTREAMER_LIBS) \
 							$(ORTP_LIBS)
 
+test_ecc_SOURCES=test_ecc.c
+
+test_ecc_LDADD=liblinphone.la \
+							$(MEDIASTREAMER_LIBS) \
+							$(ORTP_LIBS)
+
+
+
 AM_CFLAGS=$(STRICT_OPTIONS)  -DIN_LINPHONE \
 	$(ORTP_CFLAGS) \
 	$(OSIP_CFLAGS) \
diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c
index ef145c2fd3..f10214178f 100644
--- a/coreapi/linphonecore.c
+++ b/coreapi/linphonecore.c
@@ -1635,6 +1635,19 @@ void linphone_core_iterate(LinphoneCore *lc){
 		one_second_elapsed=TRUE;
 	}
 
+	if (lc->ecc!=NULL){
+		LinphoneEcCalibratorStatus ecs=ec_calibrator_get_status(lc->ecc);
+		if (ecs!=LinphoneEcCalibratorInProgress){
+			if (lc->ecc->cb)
+				lc->ecc->cb(lc,ecs,lc->ecc->delay,lc->ecc->cb_data);
+			if (ecs==LinphoneEcCalibratorDone){
+				lp_config_set_int(lc->config, "sound", "ec_delay",lc->ecc->delay);
+			}
+			ec_calibrator_destroy(lc->ecc);
+			lc->ecc=NULL;
+		}
+	}
+
 	if (lc->preview_finished){
 		lc->preview_finished=0;
 		ring_stop(lc->ringstream);
diff --git a/coreapi/linphonecore_utils.h b/coreapi/linphonecore_utils.h
index 8c29ef1afe..b2ede96496 100644
--- a/coreapi/linphonecore_utils.h
+++ b/coreapi/linphonecore_utils.h
@@ -49,4 +49,21 @@ void linphone_sound_daemon_release_all_players(LinphoneSoundDaemon *obj);
 void linphone_core_use_sound_daemon(LinphoneCore *lc, LinphoneSoundDaemon *lsd);
 void linphone_sound_daemon_destroy(LinphoneSoundDaemon *obj);
 
+/**
+ * Enum describing the result of the echo canceller calibration process.
+**/
+typedef enum {
+	LinphoneEcCalibratorInProgress,
+	LinphoneEcCalibratorDone,
+	LinphoneEcCalibratorFailed
+}LinphoneEcCalibratorStatus;
+
+
+typedef void (*LinphoneEcCalibrationCallback)(LinphoneCore *lc, LinphoneEcCalibratorStatus status, int delay_ms, void *data);
+
+/**
+ * Start an echo calibration of the sound devices, in order to find adequate settings for the echo canceller automatically.
+**/
+int linphone_core_start_echo_calibration(LinphoneCore *lc, LinphoneEcCalibrationCallback cb, void *cb_data);
+
 #endif
diff --git a/coreapi/private.h b/coreapi/private.h
index 5759bbc19a..a960acbd81 100644
--- a/coreapi/private.h
+++ b/coreapi/private.h
@@ -26,6 +26,7 @@
 #define _PRIVATE_H
 
 #include "linphonecore.h"
+#include "linphonecore_utils.h"
 #include "sal.h"
 
 #ifdef HAVE_CONFIG_H
@@ -424,6 +425,7 @@ struct _LinphoneCore
 	unsigned long video_window_id;
 	unsigned long preview_window_id;
 	time_t netup_time; /*time when network went reachable */
+	struct _EcCalibrator *ecc;
 	bool_t use_files;
 	bool_t apply_nat_settings;
 	bool_t initial_subscribes_sent;
@@ -451,6 +453,27 @@ bool_t linphone_core_is_payload_type_usable_for_bandwidth(LinphoneCore *lc, Payl
 #define linphone_core_ready(lc) ((lc)->state!=LinphoneGlobalStartup)
 void _linphone_core_configure_resolver();
 
+struct _EcCalibrator{
+	ms_thread_t thread;
+	MSSndCard *play_card,*capt_card;
+	MSFilter *sndread,*det,*rec;
+	MSFilter *play, *gen, *sndwrite;
+	MSTicker *ticker;
+	LinphoneEcCalibrationCallback cb;
+	void *cb_data;
+	int recv_count;
+	int sent_count;
+	int64_t acc;
+	int delay;
+	LinphoneEcCalibratorStatus status;
+};
+
+typedef struct _EcCalibrator EcCalibrator;
+
+LinphoneEcCalibratorStatus ec_calibrator_get_status(EcCalibrator *ecc);
+
+void ec_calibrator_destroy(EcCalibrator *ecc);
+
 #define HOLD_OFF	(0)
 #define HOLD_ON		(1)
 
diff --git a/mediastreamer2 b/mediastreamer2
index dce5ac7d4d..c5959fa452 160000
--- a/mediastreamer2
+++ b/mediastreamer2
@@ -1 +1 @@
-Subproject commit dce5ac7d4dc8e298cdedd5dcef55c60d7485206e
+Subproject commit c5959fa4520005013cd14d2f773116f24f2eb7b7
diff --git a/oRTP b/oRTP
index c8b487f32f..37c60a638f 160000
--- a/oRTP
+++ b/oRTP
@@ -1 +1 @@
-Subproject commit c8b487f32fe225f8b1961754db9140eb282a0d28
+Subproject commit 37c60a638fd108404ca437e2bbef78f227178450
-- 
GitLab