]> sjero.net Git - linphone/blobdiff - coreapi/linphonecore.c
AAC-EL: fix SDP/fmtp content according to new recommendation from Fraunhofer
[linphone] / coreapi / linphonecore.c
index 3fddf65feac2af674acdf71babb747d21cf6cf13..12c8880b65a4068547ffacd35a07c984b4ab8304 100644 (file)
@@ -17,11 +17,14 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
+#define _GNU_SOURCE
+
 #include "linphonecore.h"
 #include "sipsetup.h"
 #include "lpconfig.h"
 #include "private.h"
 
+#include <math.h>
 #include <ortp/telephonyevents.h>
 #include <ortp/zrtp.h>
 #include "mediastreamer2/mediastream.h"
@@ -38,20 +41,32 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
+#include "liblinphone_gitversion.h"
+#else
+#ifndef LIBLINPHONE_GIT_VERSION
+#define LIBLINPHONE_GIT_VERSION "unknown"
 #endif
+#endif
+
 
 /*#define UNSTANDART_GSM_11K 1*/
 
 #define ROOT_CA_FILE PACKAGE_DATA_DIR "/linphone/rootca.pem"
 
-static const char *liblinphone_version=LIBLINPHONE_VERSION;
+static const char *liblinphone_version=
+#ifdef LIBLINPHONE_GIT_VERSION
+       LIBLINPHONE_GIT_VERSION
+#else
+       LIBLINPHONE_VERSION
+#endif
+;
 static void set_network_reachable(LinphoneCore* lc,bool_t isReachable, time_t curtime);
 static void linphone_core_run_hooks(LinphoneCore *lc);
 static void linphone_core_free_hooks(LinphoneCore *lc);
 
 #include "enum.h"
+
 const char *linphone_core_get_nat_address_resolved(LinphoneCore *lc);
-void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result);
 static void toggle_video_preview(LinphoneCore *lc, bool_t val);
 
 /* relative path where is stored local ring*/
@@ -77,34 +92,31 @@ int lc_callback_obj_invoke(LCCallbackObj *obj, LinphoneCore *lc){
 
 /*prevent a gcc bug with %c*/
 static size_t my_strftime(char *s, size_t max, const char  *fmt,  const struct tm *tm){
-#if !defined(_WIN32_WCE)
        return strftime(s, max, fmt, tm);
-#else
-       return 0;
-       /*FIXME*/
-#endif /*_WIN32_WCE*/
 }
 
-static void set_call_log_date(LinphoneCallLog *cl, const struct tm *loctime){
-       my_strftime(cl->start_date,sizeof(cl->start_date),"%c",loctime);
-}
-
-LinphoneCallLog * linphone_call_log_new(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
-       LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
+static void set_call_log_date(LinphoneCallLog *cl, time_t start_time){
        struct tm loctime;
-       cl->dir=call->dir;
 #ifdef WIN32
 #if !defined(_WIN32_WCE)
-       loctime=*localtime(&call->start_time);
+       loctime=*localtime(&start_time);
        /*FIXME*/
 #endif /*_WIN32_WCE*/
 #else
-       localtime_r(&call->start_time,&loctime);
+       localtime_r(&start_time,&loctime);
 #endif
-       set_call_log_date(cl,&loctime);
+       my_strftime(cl->start_date,sizeof(cl->start_date),"%c",&loctime);
+}
+
+LinphoneCallLog * linphone_call_log_new(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
+       LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
+       cl->dir=call->dir;
+       cl->start_date_time=call->start_time;
+       set_call_log_date(cl,cl->start_date_time);
        cl->from=from;
        cl->to=to;
-    cl->status=LinphoneCallAborted; /*default status*/
+       cl->status=LinphoneCallAborted; /*default status*/
+       cl->quality=-1;
        return cl;
 }
 
@@ -120,6 +132,7 @@ void call_logs_write_to_config_file(LinphoneCore *lc){
        for(i=0,elem=lc->call_logs;elem!=NULL;elem=elem->next,++i){
                LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
                snprintf(logsection,sizeof(logsection),"call_log_%i",i);
+               lp_config_clean_section(cfg,logsection);
                lp_config_set_int(cfg,logsection,"dir",cl->dir);
                lp_config_set_int(cfg,logsection,"status",cl->status);
                tmp=linphone_address_as_string(cl->from);
@@ -128,10 +141,14 @@ void call_logs_write_to_config_file(LinphoneCore *lc){
                tmp=linphone_address_as_string(cl->to);
                lp_config_set_string(cfg,logsection,"to",tmp);
                ms_free(tmp);
-               lp_config_set_string(cfg,logsection,"start_date",cl->start_date);
+               if (cl->start_date_time)
+                       lp_config_set_int64(cfg,logsection,"start_date_time",(int64_t)cl->start_date_time);
+               else lp_config_set_string(cfg,logsection,"start_date",cl->start_date);
                lp_config_set_int(cfg,logsection,"duration",cl->duration);
                if (cl->refkey) lp_config_set_string(cfg,logsection,"refkey",cl->refkey);
                lp_config_set_float(cfg,logsection,"quality",cl->quality);
+               lp_config_set_int(cfg,logsection,"video_enabled", cl->video_enabled);
+               lp_config_set_string(cfg,logsection,"call_id",cl->call_id);
        }
        for(;i<lc->max_call_logs;++i){
                snprintf(logsection,sizeof(logsection),"call_log_%i",i);
@@ -139,10 +156,21 @@ void call_logs_write_to_config_file(LinphoneCore *lc){
        }
 }
 
+static time_t string_to_time(const char *date){
+#ifndef WIN32
+       struct tm tmtime={0};
+       strptime(date,"%c",&tmtime);
+       return mktime(&tmtime);
+#else
+       return 0;
+#endif
+}
+
 static void call_logs_read_from_config_file(LinphoneCore *lc){
        char logsection[32];
        int i;
        const char *tmp;
+       uint64_t sec;
        LpConfig *cfg=lc->config;
        for(i=0;;++i){
                snprintf(logsection,sizeof(logsection),"call_log_%i",i);
@@ -154,12 +182,25 @@ static void call_logs_read_from_config_file(LinphoneCore *lc){
                        if (tmp) cl->from=linphone_address_new(tmp);
                        tmp=lp_config_get_string(cfg,logsection,"to",NULL);
                        if (tmp) cl->to=linphone_address_new(tmp);
-                       tmp=lp_config_get_string(cfg,logsection,"start_date",NULL);
-                       if (tmp) strncpy(cl->start_date,tmp,sizeof(cl->start_date));
+                       sec=lp_config_get_int64(cfg,logsection,"start_date_time",0);
+                       if (sec) {
+                               /*new call log format with date expressed in seconds */
+                               cl->start_date_time=(time_t)sec;
+                               set_call_log_date(cl,cl->start_date_time);
+                       }else{
+                               tmp=lp_config_get_string(cfg,logsection,"start_date",NULL);
+                               if (tmp) {
+                                       strncpy(cl->start_date,tmp,sizeof(cl->start_date));
+                                       cl->start_date_time=string_to_time(cl->start_date);
+                               }
+                       }
                        cl->duration=lp_config_get_int(cfg,logsection,"duration",0);
                        tmp=lp_config_get_string(cfg,logsection,"refkey",NULL);
                        if (tmp) cl->refkey=ms_strdup(tmp);
                        cl->quality=lp_config_get_float(cfg,logsection,"quality",-1);
+                       cl->video_enabled=lp_config_get_int(cfg,logsection,"video_enabled",0);
+                       tmp=lp_config_get_string(cfg,logsection,"call_id",NULL);
+                       if (tmp) cl->call_id=ms_strdup(tmp);
                        lc->call_logs=ms_list_append(lc->call_logs,cl);
                }else break;
        }
@@ -225,10 +266,20 @@ const rtp_stats_t *linphone_call_log_get_remote_stats(const LinphoneCallLog *cl)
        return &cl->remote_stats;
 }
 
+const char *linphone_call_log_get_call_id(const LinphoneCallLog *cl){
+       return cl->call_id;
+}
+
+/**
+ * Assign a user pointer to the call log.
+**/
 void linphone_call_log_set_user_pointer(LinphoneCallLog *cl, void *up){
        cl->user_pointer=up;
 }
 
+/**
+ * Returns the user pointer associated with the call log.
+**/
 void *linphone_call_log_get_user_pointer(const LinphoneCallLog *cl){
        return cl->user_pointer;
 }
@@ -261,12 +312,74 @@ const char *linphone_call_log_get_ref_key(const LinphoneCallLog *cl){
        return cl->refkey;
 }
 
+/**
+ * Returns origin (ie from) address of the call.
+**/
+LinphoneAddress *linphone_call_log_get_from(LinphoneCallLog *cl){
+       return cl->from;
+}
+
+/**
+ * Returns destination address (ie to) of the call.
+**/
+LinphoneAddress *linphone_call_log_get_to(LinphoneCallLog *cl){
+       return cl->to;
+}
+
+/**
+ * Returns remote address (that is from or to depending on call direction).
+**/
+LinphoneAddress *linphone_call_log_get_remote_address(LinphoneCallLog *cl){
+       return (cl->dir == LinphoneCallIncoming) ? cl->from : cl->to;
+}
+
+/**
+ * Returns the direction of the call.
+**/
+LinphoneCallDir linphone_call_log_get_dir(LinphoneCallLog *cl){
+       return cl->dir;
+}
+
+/**
+ * Returns the status of the call.
+**/
+LinphoneCallStatus linphone_call_log_get_status(LinphoneCallLog *cl){
+       return cl->status;
+}
+
+/**
+ * Returns the start date of the call, expressed as a POSIX time_t.
+**/
+time_t linphone_call_log_get_start_date(LinphoneCallLog *cl){
+       return cl->start_date_time;
+}
+
+/**
+ * Returns duration of the call.
+**/
+int linphone_call_log_get_duration(LinphoneCallLog *cl){
+       return cl->duration;
+}
+
+/**
+ * Returns overall quality indication of the call.
+**/
+float linphone_call_log_get_quality(LinphoneCallLog *cl){
+       return cl->quality;
+}
+/**
+ * return true if video was enabled at the end of the call
+ */
+LinphoneCallStatus linphone_call_log_video_enabled(LinphoneCallLog *cl) {
+       return cl->video_enabled;
+}
 /** @} */
 
 void linphone_call_log_destroy(LinphoneCallLog *cl){
        if (cl->from!=NULL) linphone_address_destroy(cl->from);
        if (cl->to!=NULL) linphone_address_destroy(cl->to);
        if (cl->refkey!=NULL) ms_free(cl->refkey);
+       if (cl->call_id) ms_free(cl->call_id);
        ms_free(cl);
 }
 
@@ -294,10 +407,24 @@ const LinphoneAddress *linphone_core_get_current_call_remote_address(struct _Lin
        return linphone_call_get_remote_address(call);
 }
 
+void linphone_core_set_log_handler(OrtpLogFunc logfunc) {
+       ortp_set_log_handler(logfunc);
+}
+
+void linphone_core_set_log_file(FILE *file) {
+       if (file == NULL) file = stdout;
+       ortp_set_log_file(file);
+}
+
+void linphone_core_set_log_level(OrtpLogLevel loglevel) {
+       ortp_set_log_level_mask(loglevel);
+}
+
 /**
  * Enable logs in supplied FILE*.
  *
  * @ingroup misc
+ * @deprecated Use #linphone_core_set_log_file and #linphone_core_set_log_level instead.
  *
  * @param file a C FILE* where to fprintf logs. If null stdout is used.
  *
@@ -312,6 +439,7 @@ void linphone_core_enable_logs(FILE *file){
  * Enable logs through the user's supplied log callback.
  *
  * @ingroup misc
+ * @deprecated Use #linphone_core_set_log_handler and #linphone_core_set_log_level instead.
  *
  * @param logfunc The address of a OrtpLogFunc callback whose protoype is
  *               typedef void (*OrtpLogFunc)(OrtpLogLevel lev, const char *fmt, va_list args);
@@ -326,6 +454,7 @@ void linphone_core_enable_logs_with_cb(OrtpLogFunc logfunc){
  * Entirely disable logging.
  *
  * @ingroup misc
+ * @deprecated Use #linphone_core_set_log_level instead.
 **/
 void linphone_core_disable_logs(){
        ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
@@ -351,7 +480,7 @@ static void net_config_read (LinphoneCore *lc)
        linphone_core_set_firewall_policy(lc,tmp);
        tmp=lp_config_get_int(lc->config,"net","nat_sdp_only",0);
        lc->net_conf.nat_sdp_only=tmp;
-       tmp=lp_config_get_int(lc->config,"net","mtu",0);
+       tmp=lp_config_get_int(lc->config,"net","mtu",1300);
        linphone_core_set_mtu(lc,tmp);
        tmp=lp_config_get_int(lc->config,"net","download_ptime",0);
        linphone_core_set_download_ptime(lc,tmp);
@@ -380,7 +509,6 @@ static void sound_config_read(LinphoneCore *lc)
        int tmp;
        const char *tmpbuf;
        const char *devid;
-       float gain=0;
 #ifdef __linux
        /*alsadev let the user use custom alsa device within linphone*/
        devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
@@ -441,21 +569,24 @@ static void sound_config_read(LinphoneCore *lc)
        check_sound_device(lc);
        lc->sound_conf.latency=0;
 #ifndef __ios 
-    tmp=TRUE;
+       tmp=TRUE;
 #else
-    tmp=FALSE; /* on iOS we have builtin echo cancellation.*/
+       tmp=FALSE; /* on iOS we have builtin echo cancellation.*/
 #endif
-    tmp=lp_config_get_int(lc->config,"sound","echocancellation",tmp);
+       tmp=lp_config_get_int(lc->config,"sound","echocancellation",tmp);
        linphone_core_enable_echo_cancellation(lc,tmp);
        linphone_core_enable_echo_limiter(lc,
                lp_config_get_int(lc->config,"sound","echolimiter",0));
        linphone_core_enable_agc(lc,
                lp_config_get_int(lc->config,"sound","agc",0));
 
-       gain=lp_config_get_float(lc->config,"sound","playback_gain_db",0);
-       linphone_core_set_playback_gain_db (lc,gain);
+       linphone_core_set_playback_gain_db (lc,lp_config_get_float(lc->config,"sound","playback_gain_db",0));
+       linphone_core_set_mic_gain_db (lc,lp_config_get_float(lc->config,"sound","mic_gain_db",0));
 
        linphone_core_set_remote_ringback_tone (lc,lp_config_get_string(lc->config,"sound","ringback_tone",NULL));
+
+       /*just parse requested stream feature once at start to print out eventual errors*/
+       linphone_core_get_audio_features(lc);
 }
 
 static void sip_config_read(LinphoneCore *lc)
@@ -465,9 +596,7 @@ static void sip_config_read(LinphoneCore *lc)
        LCSipTransports tr;
        int i,tmp;
        int ipv6;
-
-       tmp=lp_config_get_int(lc->config,"sip","use_info",0);
-       linphone_core_set_use_info_for_dtmf(lc,tmp);
+       int random_port;
 
        if (lp_config_get_int(lc->config,"sip","use_session_timers",0)==1){
                sal_use_session_timers(lc->sal,200);
@@ -476,9 +605,7 @@ static void sip_config_read(LinphoneCore *lc)
        sal_use_rport(lc->sal,lp_config_get_int(lc->config,"sip","use_rport",1));
        sal_use_101(lc->sal,lp_config_get_int(lc->config,"sip","use_101",1));
        sal_reuse_authorization(lc->sal, lp_config_get_int(lc->config,"sip","reuse_authorization",0));
-
-       tmp=lp_config_get_int(lc->config,"sip","use_rfc2833",0);
-       linphone_core_set_use_rfc2833_for_dtmf(lc,tmp);
+       sal_expire_old_registration_contacts(lc->sal,lp_config_get_int(lc->config,"sip","expire_old_registration_contacts",0));
 
        ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
        if (ipv6==-1){
@@ -486,21 +613,29 @@ static void sip_config_read(LinphoneCore *lc)
        }
        linphone_core_enable_ipv6(lc,ipv6);
        memset(&tr,0,sizeof(tr));
-       if (lp_config_get_int(lc->config,"sip","sip_random_port",0)) {
-               tr.udp_port=(0xDFF&+random())+1024;
-       } else {
-               tr.udp_port=lp_config_get_int(lc->config,"sip","sip_port",5060);
-       }
-       if (lp_config_get_int(lc->config,"sip","sip_tcp_random_port",0)) {
-               tr.tcp_port=(0xDFF&+random())+1024;
-       } else {
-               tr.tcp_port=lp_config_get_int(lc->config,"sip","sip_tcp_port",0);
-       }
-       if (lp_config_get_int(lc->config,"sip","sip_tls_random_port",0)) {
-               tr.tls_port=(0xDFF&+random())+1024;
-       } else {
-               tr.tls_port=lp_config_get_int(lc->config,"sip","sip_tls_port",0);
-       }
+       
+       tr.udp_port=lp_config_get_int(lc->config,"sip","sip_port",0);
+       tr.tcp_port=lp_config_get_int(lc->config,"sip","sip_tcp_port",0);
+       tr.tls_port=lp_config_get_int(lc->config,"sip","sip_tls_port",0);
+       
+       if (lp_config_get_int(lc->config,"sip","sip_random_port",0)==1)
+               random_port=(0xDFFF&random())+1024;
+       else random_port=0;
+       
+       if (tr.udp_port==0 && tr.tcp_port==0 && tr.tls_port==0){
+               tr.udp_port=5060;
+       }       
+       
+       if (tr.udp_port>0 && random_port){
+               tr.udp_port=random_port;
+               tr.tls_port=tr.tcp_port=0; /*make sure only one transport is active at a time*/
+       }else if (tr.tcp_port>0 && random_port){
+               tr.tcp_port=random_port;
+               tr.tls_port=tr.udp_port=0; /*make sure only one transport is active at a time*/
+       }else if (tr.tls_port>0 && random_port){
+               tr.tls_port=random_port;
+               tr.udp_port=tr.tcp_port=0; /*make sure only one transport is active at a time*/
+       } 
 
 #ifdef __linux
        sal_set_root_ca(lc->sal, lp_config_get_string(lc->config,"sip","root_ca", "/etc/ssl/certs"));
@@ -508,6 +643,9 @@ static void sip_config_read(LinphoneCore *lc)
        sal_set_root_ca(lc->sal, lp_config_get_string(lc->config,"sip","root_ca", ROOT_CA_FILE));
 #endif
        linphone_core_verify_server_certificates(lc,lp_config_get_int(lc->config,"sip","verify_server_certs",TRUE));
+       linphone_core_verify_server_cn(lc,lp_config_get_int(lc->config,"sip","verify_server_cn",TRUE));
+       /*setting the dscp must be done before starting the transports, otherwise it is not taken into effect*/
+       sal_set_dscp(lc->sal,linphone_core_get_sip_dscp(lc));
        /*start listening on ports*/
        linphone_core_set_sip_transports(lc,&tr);
 
@@ -534,9 +672,15 @@ static void sip_config_read(LinphoneCore *lc)
        linphone_core_set_guess_hostname(lc,tmp);
 
 
-       tmp=lp_config_get_int(lc->config,"sip","inc_timeout",15);
+       tmp=lp_config_get_int(lc->config,"sip","inc_timeout",30);
        linphone_core_set_inc_timeout(lc,tmp);
 
+       tmp=lp_config_get_int(lc->config,"sip","in_call_timeout",0);
+       linphone_core_set_in_call_timeout(lc,tmp);
+       
+       tmp=lp_config_get_int(lc->config,"sip","delayed_timeout",4);
+       linphone_core_set_delayed_timeout(lc,tmp);
+
        /* get proxies config */
        for(i=0;; i++){
                LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc->config,i);
@@ -567,47 +711,69 @@ static void sip_config_read(LinphoneCore *lc)
        lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
        lc->sip_conf.register_only_when_network_is_up=
                lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
+       lc->sip_conf.register_only_when_upnp_is_ok=
+               lp_config_get_int(lc->config,"sip","register_only_when_upnp_is_ok",1);
        lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",1);
        lc->sip_conf.auto_net_state_mon=lp_config_get_int(lc->config,"sip","auto_net_state_mon",1);
        lc->sip_conf.keepalive_period=lp_config_get_int(lc->config,"sip","keepalive_period",10000);
-       sal_set_keepalive_period(lc->sal,lc->sip_conf.keepalive_period);
+       lc->sip_conf.tcp_tls_keepalive=lp_config_get_int(lc->config,"sip","tcp_tls_keepalive",0);
+       linphone_core_enable_keep_alive(lc, (lc->sip_conf.keepalive_period > 0));
        sal_use_one_matching_codec_policy(lc->sal,lp_config_get_int(lc->config,"sip","only_one_codec",0));
        sal_use_double_registrations(lc->sal,lp_config_get_int(lc->config,"sip","use_double_registrations",1));
+       sal_use_dates(lc->sal,lp_config_get_int(lc->config,"sip","put_date",0));
 }
 
 static void rtp_config_read(LinphoneCore *lc)
 {
-       int port;
+       int min_port, max_port;
        int jitt_comp;
        int nortp_timeout;
        bool_t rtp_no_xmit_on_audio_mute;
+       bool_t adaptive_jitt_comp_enabled;
 
-       port=lp_config_get_int(lc->config,"rtp","audio_rtp_port",7078);
-       linphone_core_set_audio_port(lc,port);
+       if (lp_config_get_range(lc->config, "rtp", "audio_rtp_port", &min_port, &max_port, 7078, 7078) == TRUE) {
+               if (min_port <= 0) min_port = 1;
+               if (max_port > 65535) max_port = 65535;
+               linphone_core_set_audio_port_range(lc, min_port, max_port);
+       } else {
+               min_port = lp_config_get_int(lc->config, "rtp", "audio_rtp_port", 7078);
+               linphone_core_set_audio_port(lc, min_port);
+       }
 
-       port=lp_config_get_int(lc->config,"rtp","video_rtp_port",9078);
-       if (port==0) port=9078;
-       linphone_core_set_video_port(lc,port);
+       if (lp_config_get_range(lc->config, "rtp", "video_rtp_port", &min_port, &max_port, 9078, 9078) == TRUE) {
+               if (min_port <= 0) min_port = 1;
+               if (max_port > 65535) max_port = 65535;
+               linphone_core_set_video_port_range(lc, min_port, max_port);
+       } else {
+               min_port = lp_config_get_int(lc->config, "rtp", "video_rtp_port", 9078);
+               linphone_core_set_video_port(lc, min_port);
+       }
 
        jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
        linphone_core_set_audio_jittcomp(lc,jitt_comp);
        jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
        if (jitt_comp==0) jitt_comp=60;
-       lc->rtp_conf.video_jitt_comp=jitt_comp;
+       linphone_core_set_video_jittcomp(lc,jitt_comp);
        nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
        linphone_core_set_nortp_timeout(lc,nortp_timeout);
        rtp_no_xmit_on_audio_mute=lp_config_get_int(lc->config,"rtp","rtp_no_xmit_on_audio_mute",FALSE);
        linphone_core_set_rtp_no_xmit_on_audio_mute(lc,rtp_no_xmit_on_audio_mute);
+       adaptive_jitt_comp_enabled = lp_config_get_int(lc->config, "rtp", "audio_adaptive_jitt_comp_enabled", TRUE);
+       linphone_core_enable_audio_adaptive_jittcomp(lc, adaptive_jitt_comp_enabled);
+       adaptive_jitt_comp_enabled = lp_config_get_int(lc->config, "rtp", "video_adaptive_jitt_comp_enabled", TRUE);
+       linphone_core_enable_video_adaptive_jittcomp(lc, adaptive_jitt_comp_enabled);
+       lc->rtp_conf.disable_upnp = lp_config_get_int(lc->config, "rtp", "disable_upnp", FALSE);
 }
 
-static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, const char *recv_fmtp){
+static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, int channels, const char *recv_fmtp){
        PayloadType *candidate=NULL;
        int i;
        PayloadType *it;
-       for(i=0;i<127;++i){
+       for(i=0;i<RTP_PROFILE_MAX_PAYLOADS;++i){
                it=rtp_profile_get_payload(prof,i);
                if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
-                       && (clock_rate==it->clock_rate || clock_rate<=0) ){
+                       && (clock_rate==it->clock_rate || clock_rate<=0)
+                       && (channels==it->channels || channels<=0) ){
                        if ( (recv_fmtp && it->recv_fmtp && strstr(recv_fmtp,it->recv_fmtp)!=NULL) ||
                                (recv_fmtp==NULL && it->recv_fmtp==NULL) ){
                                /*exact match*/
@@ -626,11 +792,12 @@ static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int c
        return candidate;
 }
 
-static bool_t get_codec(LpConfig *config, const char* type, int index, PayloadType **ret){
+static bool_t get_codec(LinphoneCore *lc, const char* type, int index, PayloadType **ret){
        char codeckey[50];
        const char *mime,*fmtp;
-       int rate,enabled;
+       int rate,channels,enabled;
        PayloadType *pt;
+       LpConfig *config=lc->config;
 
        *ret=NULL;
        snprintf(codeckey,50,"%s_%i",type,index);
@@ -639,8 +806,9 @@ static bool_t get_codec(LpConfig *config, const char* type, int index, PayloadTy
 
        rate=lp_config_get_int(config,codeckey,"rate",8000);
        fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
+       channels=lp_config_get_int(config,codeckey,"channels",0);
        enabled=lp_config_get_int(config,codeckey,"enabled",1);
-       pt=find_payload(&av_profile,mime,rate,fmtp);
+       pt=find_payload(lc->default_profile,mime,rate,channels,fmtp);
        if (pt && enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
        //ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
        if (pt==NULL) ms_warning("Ignoring codec config %s/%i with fmtp=%s because unsupported",
@@ -650,24 +818,28 @@ static bool_t get_codec(LpConfig *config, const char* type, int index, PayloadTy
 }
 
 #define RANK_END 10000
-static const char *codec_pref_order[]={
-       "speex",
-       "iLBC",
-       "amr",
-       "gsm",
-       "pcmu",
-       "pcma",
-       "VP8",
-       "H264",
-       "MP4V-ES",
-       "H263-1998",
-       NULL,
+
+typedef struct codec_desc{
+       const char *name;
+       int rate;
+}codec_desc_t;
+
+static codec_desc_t codec_pref_order[]={
+       {"SILK", 16000},
+       {"speex", 16000},
+       {"speex", 8000},
+       {"pcmu",8000},
+       {"pcma",8000},
+       {"VP8",90000},
+       {"H264",90000},
+       {"MP4V-ES",90000},
+       {NULL,0}
 };
 
-static int find_codec_rank(const char *mime){
+static int find_codec_rank(const char *mime, int clock_rate){
        int i;
-       for(i=0;codec_pref_order[i]!=NULL;++i){
-               if (strcasecmp(codec_pref_order[i],mime)==0)
+       for(i=0;codec_pref_order[i].name!=NULL;++i){
+               if (strcasecmp(codec_pref_order[i].name,mime)==0 && clock_rate==codec_pref_order[i].rate)
                        return i;
        }
        return RANK_END;
@@ -675,17 +847,17 @@ static int find_codec_rank(const char *mime){
 
 static int codec_compare(const PayloadType *a, const PayloadType *b){
        int ra,rb;
-       ra=find_codec_rank(a->mime_type);
-       rb=find_codec_rank(b->mime_type);
+       ra=find_codec_rank(a->mime_type,a->clock_rate);
+       rb=find_codec_rank(b->mime_type,b->clock_rate);
        if (ra>rb) return 1;
        if (ra<rb) return -1;
        return 0;
 }
 
-static MSList *add_missing_codecs(SalStreamType mtype, MSList *l){
+static MSList *add_missing_codecs(LinphoneCore *lc, SalStreamType mtype, MSList *l){
        int i;
-       for(i=0;i<127;++i){
-               PayloadType *pt=rtp_profile_get_payload(&av_profile,i);
+       for(i=0;i<RTP_PROFILE_MAX_PAYLOADS;++i){
+               PayloadType *pt=rtp_profile_get_payload(lc->default_profile,i);
                if (pt){
                        if (mtype==SalVideo && pt->type!=PAYLOAD_VIDEO)
                                pt=NULL;
@@ -696,7 +868,7 @@ static MSList *add_missing_codecs(SalStreamType mtype, MSList *l){
                        if (pt && ms_filter_codec_supported(pt->mime_type)){
                                if (ms_list_find(l,pt)==NULL){
                                        /*unranked codecs are disabled by default*/
-                                       if (find_codec_rank(pt->mime_type)!=RANK_END){
+                                       if (find_codec_rank(pt->mime_type, pt->clock_rate)!=RANK_END){
                                                payload_type_set_flag(pt,PAYLOAD_TYPE_ENABLED);
                                        }
                                        ms_message("Adding new codec %s/%i with fmtp %s",
@@ -726,37 +898,34 @@ static void codecs_config_read(LinphoneCore *lc)
        PayloadType *pt;
        MSList *audio_codecs=NULL;
        MSList *video_codecs=NULL;
-       for (i=0;get_codec(lc->config,"audio_codec",i,&pt);i++){
+       for (i=0;get_codec(lc,"audio_codec",i,&pt);i++){
                if (pt){
                        if (!ms_filter_codec_supported(pt->mime_type)){
                                ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
                        }else audio_codecs=codec_append_if_new(audio_codecs,pt);
                }
        }
-       audio_codecs=add_missing_codecs(SalAudio,audio_codecs);
-       for (i=0;get_codec(lc->config,"video_codec",i,&pt);i++){
+       audio_codecs=add_missing_codecs(lc,SalAudio,audio_codecs);
+       for (i=0;get_codec(lc,"video_codec",i,&pt);i++){
                if (pt){
                        if (!ms_filter_codec_supported(pt->mime_type)){
                                ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
                        }else video_codecs=codec_append_if_new(video_codecs,(void *)pt);
                }
        }
-       video_codecs=add_missing_codecs(SalVideo,video_codecs);
+       video_codecs=add_missing_codecs(lc,SalVideo,video_codecs);
        linphone_core_set_audio_codecs(lc,audio_codecs);
        linphone_core_set_video_codecs(lc,video_codecs);
        linphone_core_update_allocated_audio_bandwidth(lc);
 }
 
-static void video_config_read(LinphoneCore *lc){
-#ifdef VIDEO_ENABLED
-       int capture, display, self_view;
-#endif
-       const char *str;
-       int ndev;
-       const char **devices;
+static void build_video_devices_table(LinphoneCore *lc){
        const MSList *elem;
        int i;
-
+       int ndev;
+       const char **devices;
+       if (lc->video_conf.cams)
+               ms_free(lc->video_conf.cams);
        /* retrieve all video devices */
        elem=ms_web_cam_manager_get_list(ms_web_cam_manager_get());
        ndev=ms_list_size(elem);
@@ -766,6 +935,18 @@ static void video_config_read(LinphoneCore *lc){
        }
        devices[ndev]=NULL;
        lc->video_conf.cams=devices;
+}
+
+static void video_config_read(LinphoneCore *lc){
+#ifdef VIDEO_ENABLED
+       int capture, display, self_view;
+#endif
+       const char *str;        
+#ifdef VIDEO_ENABLED   
+       LinphoneVideoPolicy vpol;
+       memset(&vpol, 0, sizeof(LinphoneVideoPolicy));
+#endif
+       build_video_devices_table(lc);
 
        str=lp_config_get_string(lc->config,"video","device",NULL);
        if (str && str[0]==0) str=NULL;
@@ -778,12 +959,16 @@ static void video_config_read(LinphoneCore *lc){
        capture=lp_config_get_int(lc->config,"video","capture",1);
        display=lp_config_get_int(lc->config,"video","display",1);
        self_view=lp_config_get_int(lc->config,"video","self_view",1);
+       vpol.automatically_initiate=lp_config_get_int(lc->config,"video","automatically_initiate",1);
+       vpol.automatically_accept=lp_config_get_int(lc->config,"video","automatically_accept",1);
        lc->video_conf.displaytype=lp_config_get_string(lc->config,"video","displaytype",NULL);
        if(lc->video_conf.displaytype)
                ms_message("we are using a specific display:%s\n",lc->video_conf.displaytype);
 
        linphone_core_enable_video(lc,capture,display);
+       linphone_core_enable_video_preview(lc,lp_config_get_int(lc->config,"video","show_local",0));
        linphone_core_enable_self_view(lc,self_view);
+       linphone_core_set_video_policy(lc,&vpol);
 #endif
 }
 
@@ -819,11 +1004,14 @@ bool_t linphone_core_tunnel_available(void){
 }
 
 /**
- * Enable adaptive rate control (experimental feature, audio-only).
+ * Enable adaptive rate control.
+ * 
+ * @ingroup media_parameters
  *
  * Adaptive rate control consists in using RTCP feedback provided information to dynamically
- * control the output bitrate of the encoders, so that we can adapt to the network conditions and
- * available bandwidth.
+ * control the output bitrate of the audio and video encoders, so that we can adapt to the network conditions and
+ * available bandwidth. Control of the audio encoder is done in case of audio-only call, and control of the video encoder is done for audio & video calls.
+ * Adaptive rate control feature is enabled by default.
 **/
 void linphone_core_enable_adaptive_rate_control(LinphoneCore *lc, bool_t enabled){
        lp_config_set_int(lc->config,"net","adaptive_rate_control",(int)enabled);
@@ -831,6 +1019,8 @@ void linphone_core_enable_adaptive_rate_control(LinphoneCore *lc, bool_t enabled
 
 /**
  * Returns whether adaptive rate control is enabled.
+ * 
+ * @ingroup media_parameters
  *
  * See linphone_core_enable_adaptive_rate_control().
 **/
@@ -903,23 +1093,28 @@ int linphone_core_get_upload_bandwidth(const LinphoneCore *lc){
        return lc->net_conf.upload_bw;
 }
 /**
- * Set audio packetization time linphone expects to receive from peer
+ * Set audio packetization time linphone expects to receive from peer.
+ * A value of zero means that ptime is not specified.
+ * @ingroup media_parameters
  */
 void linphone_core_set_download_ptime(LinphoneCore *lc, int ptime) {
-       lc->net_conf.down_ptime=ptime;
+       lp_config_set_int(lc->config,"rtp","download_ptime",ptime);
 }
 
 /**
- * Get audio packetization time linphone expects to receive from peer
+ * Get audio packetization time linphone expects to receive from peer.
+ * A value of zero means that ptime is not specified.
+ * @ingroup media_parameters
  */
 int linphone_core_get_download_ptime(LinphoneCore *lc) {
-       return lc->net_conf.down_ptime;
+       return lp_config_get_int(lc->config,"rtp","download_ptime",0);
 }
 
 /**
  * Set audio packetization time linphone will send (in absence of requirement from peer)
  * A value of 0 stands for the current codec default packetization time.
  *
+ * @ingroup media_parameters
 **/
 void linphone_core_set_upload_ptime(LinphoneCore *lc, int ptime){
        lp_config_set_int(lc->config,"rtp","upload_ptime",ptime);
@@ -929,6 +1124,8 @@ void linphone_core_set_upload_ptime(LinphoneCore *lc, int ptime){
  * Set audio packetization time linphone will send (in absence of requirement from peer)
  * A value of 0 stands for the current codec default packetization time.
  *
+ * 
+ * @ingroup media_parameters
 **/
 int linphone_core_get_upload_ptime(LinphoneCore *lc){
        return lp_config_get_int(lc->config,"rtp","upload_ptime",0);
@@ -953,7 +1150,7 @@ static void linphone_core_assign_payload_type(LinphoneCore *lc, PayloadType *con
                /*look for a free number */
                MSList *elem;
                int i;
-               for(i=lc->dyn_pt;i<=127;++i){
+               for(i=lc->dyn_pt;i<RTP_PROFILE_MAX_PAYLOADS;++i){
                        bool_t already_assigned=FALSE;
                        for(elem=lc->payload_types;elem!=NULL;elem=elem->next){
                                PayloadType *it=(PayloadType*)elem->data;
@@ -975,11 +1172,26 @@ static void linphone_core_assign_payload_type(LinphoneCore *lc, PayloadType *con
        ms_message("assigning %s/%i payload type number %i",pt->mime_type,pt->clock_rate,number);
        payload_type_set_number(pt,number);
        if (recv_fmtp!=NULL) payload_type_set_recv_fmtp(pt,recv_fmtp);
-       rtp_profile_set_payload(&av_profile,number,pt);
+       rtp_profile_set_payload(lc->default_profile,number,pt);
        lc->payload_types=ms_list_append(lc->payload_types,pt);
 }
 
+static void linphone_core_handle_static_payloads(LinphoneCore *lc){
+       RtpProfile *prof=&av_profile;
+       int i;
+       for(i=0;i<RTP_PROFILE_MAX_PAYLOADS;++i){
+               PayloadType *pt=rtp_profile_get_payload(prof,i);
+               if (pt){
+                       if (payload_type_get_number(pt)!=i){
+                               linphone_core_assign_payload_type(lc,pt,i,NULL);
+                       }
+               }
+       }
+}
+
 static void linphone_core_free_payload_types(LinphoneCore *lc){
+       rtp_profile_clear_all(lc->default_profile);
+       rtp_profile_destroy(lc->default_profile);
        ms_list_for_each(lc->payload_types,(void (*)(void*))payload_type_destroy);
        ms_list_free(lc->payload_types);
        lc->payload_types=NULL;
@@ -993,17 +1205,18 @@ void linphone_core_set_state(LinphoneCore *lc, LinphoneGlobalState gstate, const
 }
 static void misc_config_read (LinphoneCore *lc) {
        LpConfig *config=lc->config;
-    lc->max_call_logs=lp_config_get_int(config,"misc","history_max_size",15);
-    lc->max_calls=lp_config_get_int(config,"misc","max_calls",NB_MAX_CALLS);
+       lc->max_call_logs=lp_config_get_int(config,"misc","history_max_size",15);
+       lc->max_calls=lp_config_get_int(config,"misc","max_calls",NB_MAX_CALLS);
 }
 
 
 
 
-static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path,
-    const char *factory_config_path, void * userdata)
+static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, LpConfig *config, void * userdata)
 {
+       ms_message("Initializing LinphoneCore %s", linphone_core_get_version());
        memset (lc, 0, sizeof (LinphoneCore));
+       lc->config=config;
        lc->data=userdata;
        lc->ringstream_autorelease=TRUE;
 
@@ -1012,6 +1225,7 @@ static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vta
        linphone_core_set_state(lc,LinphoneGlobalStartup,"Starting up");
        ortp_init();
        lc->dyn_pt=96;
+       lc->default_profile=rtp_profile_new("default profile");
        linphone_core_assign_payload_type(lc,&payload_type_pcmu8000,0,NULL);
        linphone_core_assign_payload_type(lc,&payload_type_gsm,3,NULL);
        linphone_core_assign_payload_type(lc,&payload_type_pcma8000,8,NULL);
@@ -1056,7 +1270,7 @@ static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vta
        /*add all payload type for which we don't care about the number */
        linphone_core_assign_payload_type(lc,&payload_type_ilbc,-1,"mode=30");
        linphone_core_assign_payload_type(lc,&payload_type_amr,-1,"octet-align=1");
-        linphone_core_assign_payload_type(lc,&payload_type_amrwb,-1,"octet-align=1");
+       linphone_core_assign_payload_type(lc,&payload_type_amrwb,-1,"octet-align=1");
        linphone_core_assign_payload_type(lc,&payload_type_lpc1015,-1,NULL);
        linphone_core_assign_payload_type(lc,&payload_type_g726_16,-1,NULL);
        linphone_core_assign_payload_type(lc,&payload_type_g726_24,-1,NULL);
@@ -1071,6 +1285,9 @@ static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vta
        linphone_core_assign_payload_type(lc,&payload_type_silk_wb,-1,NULL);
        linphone_core_assign_payload_type(lc,&payload_type_silk_swb,-1,NULL);
        linphone_core_assign_payload_type(lc,&payload_type_g729,18,"annexb=no");
+       linphone_core_assign_payload_type(lc,&payload_type_aaceld_22k,-1,"config=F8EE2000; constantDuration=512;  indexDeltaLength=3; indexLength=3; mode=AAC-hbr; profile-level-id=76; sizeLength=13; streamType=5");
+       linphone_core_assign_payload_type(lc,&payload_type_aaceld_44k,-1,"config=F8E82000; constantDuration=512;  indexDeltaLength=3; indexLength=3; mode=AAC-hbr; profile-level-id=76; sizeLength=13; streamType=5");
+       linphone_core_handle_static_payloads(lc);
        
        ms_init();
        /* create a mediastreamer2 event queue and set it as global */
@@ -1078,14 +1295,13 @@ static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vta
        lc->msevq=ms_event_queue_new();
        ms_set_global_event_queue(lc->msevq);
 
-       lc->config=lp_config_new(config_path);
-       if (factory_config_path)
-               lp_config_read_file(lc->config,factory_config_path);
-
        lc->sal=sal_init();
        sal_set_user_pointer(lc->sal,lc);
        sal_set_callbacks(lc->sal,&linphone_sal_callbacks);
 
+        lc->network_last_check = 0;
+        lc->network_last_status = FALSE;
+
        sip_setup_register_all();
        sound_config_read(lc);
        net_config_read(lc);
@@ -1124,13 +1340,19 @@ static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vta
  *        It is OPTIONAL, use NULL if unneeded.
  * @param userdata an opaque user pointer that can be retrieved at any time (for example in
  *        callbacks) using linphone_core_get_user_data().
- *
+ * @see linphone_core_new_with_config
 **/
 LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
                                                const char *config_path, const char *factory_config_path, void * userdata)
 {
-       LinphoneCore *core=ms_new(LinphoneCore,1);
-       linphone_core_init(core,vtable,config_path, factory_config_path, userdata);
+       LpConfig *config = lp_config_new_with_factory(config_path, factory_config_path);
+       return linphone_core_new_with_config(vtable, config, userdata);
+}
+
+LinphoneCore *linphone_core_new_with_config(const LinphoneCoreVTable *vtable, struct _LpConfig *config, void *userdata)
+{
+       LinphoneCore *core = ms_new(LinphoneCore, 1);
+       linphone_core_init(core, vtable, config, userdata);
        return core;
 }
 
@@ -1141,6 +1363,7 @@ LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
  * structure holding the codec information.
  * It is possible to make copy of the list with ms_list_copy() in order to modify it
  * (such as the order of codecs).
+ * @ingroup media_parameters
 **/
 const MSList *linphone_core_get_audio_codecs(const LinphoneCore *lc)
 {
@@ -1154,6 +1377,7 @@ const MSList *linphone_core_get_audio_codecs(const LinphoneCore *lc)
  * structure holding the codec information.
  * It is possible to make copy of the list with ms_list_copy() in order to modify it
  * (such as the order of codecs).
+ * @ingroup media_parameters
 **/
 const MSList *linphone_core_get_video_codecs(const LinphoneCore *lc)
 {
@@ -1194,6 +1418,14 @@ void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result
                strncpy(result,ip,LINPHONE_IPADDR_SIZE);
                return;
        }
+#ifdef BUILD_UPNP
+       else if (lc->upnp != NULL && linphone_core_get_firewall_policy(lc)==LinphonePolicyUseUpnp &&
+                       linphone_upnp_context_get_state(lc->upnp) == LinphoneUpnpStateOk) {
+               ip = linphone_upnp_context_get_external_ipaddress(lc->upnp);
+               strncpy(result,ip,LINPHONE_IPADDR_SIZE);
+               return;
+       }
+#endif //BUILD_UPNP
        if (linphone_core_get_local_ip_for(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,dest,result)==0)
                return;
        /*else fallback to SAL routine that will attempt to find the most realistic interface */
@@ -1284,6 +1516,7 @@ int linphone_core_set_audio_codecs(LinphoneCore *lc, MSList *codecs)
 {
        if (lc->codecs_conf.audio_codecs!=NULL) ms_list_free(lc->codecs_conf.audio_codecs);
        lc->codecs_conf.audio_codecs=codecs;
+       _linphone_core_codec_config_write(lc);
        return 0;
 }
 
@@ -1298,6 +1531,7 @@ int linphone_core_set_video_codecs(LinphoneCore *lc, MSList *codecs)
 {
        if (lc->codecs_conf.video_codecs!=NULL) ms_list_free(lc->codecs_conf.video_codecs);
        lc->codecs_conf.video_codecs=codecs;
+       _linphone_core_codec_config_write(lc);
        return 0;
 }
 
@@ -1306,8 +1540,18 @@ const MSList * linphone_core_get_friend_list(const LinphoneCore *lc)
        return lc->friends;
 }
 
+void linphone_core_enable_audio_adaptive_jittcomp(LinphoneCore* lc, bool_t val)
+{
+       lc->rtp_conf.audio_adaptive_jitt_comp_enabled = val;
+}
+
+bool_t linphone_core_audio_adaptive_jittcomp_enabled(LinphoneCore* lc)
+{
+       return lc->rtp_conf.audio_adaptive_jitt_comp_enabled;
+}
+
 /**
- * Returns the nominal jitter buffer size in milliseconds.
+ * Returns the nominal audio jitter buffer size in milliseconds.
  *
  * @ingroup media_parameters
 **/
@@ -1316,6 +1560,26 @@ int linphone_core_get_audio_jittcomp(LinphoneCore *lc)
        return lc->rtp_conf.audio_jitt_comp;
 }
 
+void linphone_core_enable_video_adaptive_jittcomp(LinphoneCore* lc, bool_t val)
+{
+       lc->rtp_conf.video_adaptive_jitt_comp_enabled = val;
+}
+
+bool_t linphone_core_video_adaptive_jittcomp_enabled(LinphoneCore* lc)
+{
+       return lc->rtp_conf.video_adaptive_jitt_comp_enabled;
+}
+
+/**
+ * Returns the nominal video jitter buffer size in milliseconds.
+ *
+ * @ingroup media_parameters
+**/
+int linphone_core_get_video_jittcomp(LinphoneCore *lc)
+{
+       return lc->rtp_conf.video_jitt_comp;
+}
+
 /**
  * Returns the UDP port used for audio streaming.
  *
@@ -1323,7 +1587,18 @@ int linphone_core_get_audio_jittcomp(LinphoneCore *lc)
 **/
 int linphone_core_get_audio_port(const LinphoneCore *lc)
 {
-       return lc->rtp_conf.audio_rtp_port;
+       return lc->rtp_conf.audio_rtp_min_port;
+}
+
+/**
+ * Get the audio port range from which is randomly chosen the UDP port used for audio streaming.
+ *
+ * @ingroup network_parameters
+ */
+void linphone_core_get_audio_port_range(const LinphoneCore *lc, int *min_port, int *max_port)
+{
+       *min_port = lc->rtp_conf.audio_rtp_min_port;
+       *max_port = lc->rtp_conf.audio_rtp_max_port;
 }
 
 /**
@@ -1332,7 +1607,18 @@ int linphone_core_get_audio_port(const LinphoneCore *lc)
  * @ingroup network_parameters
 **/
 int linphone_core_get_video_port(const LinphoneCore *lc){
-       return lc->rtp_conf.video_rtp_port;
+       return lc->rtp_conf.video_rtp_min_port;
+}
+
+/**
+ * Get the video port range from which is randomly chosen the UDP port used for video streaming.
+ *
+ * @ingroup network_parameters
+ */
+void linphone_core_get_video_port_range(const LinphoneCore *lc, int *min_port, int *max_port)
+{
+       *min_port = lc->rtp_conf.video_rtp_min_port;
+       *max_port = lc->rtp_conf.video_rtp_max_port;
 }
 
 
@@ -1363,6 +1649,16 @@ void linphone_core_set_audio_jittcomp(LinphoneCore *lc, int value)
        lc->rtp_conf.audio_jitt_comp=value;
 }
 
+/**
+ * Sets the nominal video jitter buffer size in milliseconds.
+ *
+ * @ingroup media_parameters
+**/
+void linphone_core_set_video_jittcomp(LinphoneCore *lc, int value)
+{
+       lc->rtp_conf.video_jitt_comp=value;
+}
+
 void linphone_core_set_rtp_no_xmit_on_audio_mute(LinphoneCore *lc,bool_t rtp_no_xmit_on_audio_mute){
        lc->rtp_conf.rtp_no_xmit_on_audio_mute=rtp_no_xmit_on_audio_mute;
 }
@@ -1374,7 +1670,17 @@ void linphone_core_set_rtp_no_xmit_on_audio_mute(LinphoneCore *lc,bool_t rtp_no_
 **/
 void linphone_core_set_audio_port(LinphoneCore *lc, int port)
 {
-       lc->rtp_conf.audio_rtp_port=port;
+       lc->rtp_conf.audio_rtp_min_port=lc->rtp_conf.audio_rtp_max_port=port;
+}
+
+/**
+ * Sets the UDP port range from which to randomly select the port used for audio streaming.
+ * @ingroup media_parameters
+ */
+void linphone_core_set_audio_port_range(LinphoneCore *lc, int min_port, int max_port)
+{
+       lc->rtp_conf.audio_rtp_min_port=min_port;
+       lc->rtp_conf.audio_rtp_max_port=max_port;
 }
 
 /**
@@ -1383,7 +1689,17 @@ void linphone_core_set_audio_port(LinphoneCore *lc, int port)
  * @ingroup network_parameters
 **/
 void linphone_core_set_video_port(LinphoneCore *lc, int port){
-       lc->rtp_conf.video_rtp_port=port;
+       lc->rtp_conf.video_rtp_min_port=lc->rtp_conf.video_rtp_max_port=port;
+}
+
+/**
+ * Sets the UDP port range from which to randomly select the port used for video streaming.
+ * @ingroup media_parameters
+ */
+void linphone_core_set_video_port_range(LinphoneCore *lc, int min_port, int max_port)
+{
+       lc->rtp_conf.video_rtp_min_port=min_port;
+       lc->rtp_conf.video_rtp_max_port=max_port;
 }
 
 /**
@@ -1403,7 +1719,7 @@ void linphone_core_set_nortp_timeout(LinphoneCore *lc, int nortp_timeout){
 **/
 bool_t linphone_core_get_use_info_for_dtmf(LinphoneCore *lc)
 {
-       return lc->sip_conf.use_info;
+       return lp_config_get_int(lc->config, "sip", "use_info", 0);
 }
 
 /**
@@ -1413,7 +1729,9 @@ bool_t linphone_core_get_use_info_for_dtmf(LinphoneCore *lc)
 **/
 void linphone_core_set_use_info_for_dtmf(LinphoneCore *lc,bool_t use_info)
 {
-       lc->sip_conf.use_info=use_info;
+       if (linphone_core_ready(lc)) {
+               lp_config_set_int(lc->config, "sip", "use_info", use_info);
+       }
 }
 
 /**
@@ -1423,7 +1741,7 @@ void linphone_core_set_use_info_for_dtmf(LinphoneCore *lc,bool_t use_info)
 **/
 bool_t linphone_core_get_use_rfc2833_for_dtmf(LinphoneCore *lc)
 {
-       return lc->sip_conf.use_rfc2833;
+       return lp_config_get_int(lc->config, "sip", "use_rfc2833", 1);
 }
 
 /**
@@ -1433,7 +1751,9 @@ bool_t linphone_core_get_use_rfc2833_for_dtmf(LinphoneCore *lc)
 **/
 void linphone_core_set_use_rfc2833_for_dtmf(LinphoneCore *lc,bool_t use_rfc2833)
 {
-       lc->sip_conf.use_rfc2833=use_rfc2833;
+       if (linphone_core_ready(lc)) {
+               lp_config_set_int(lc->config, "sip", "use_rfc2833", use_rfc2833);
+       }
 }
 
 /**
@@ -1472,9 +1792,18 @@ static void apply_user_agent(LinphoneCore *lc){
  *
  * @ingroup misc
 **/
-void linphone_core_set_user_agent(const char *name, const char *ver){
+void linphone_core_set_user_agent(LinphoneCore *lc, const char *name, const char *ver){
        strncpy(_ua_name,name,sizeof(_ua_name)-1);
        strncpy(_ua_version,ver,sizeof(_ua_version));
+       apply_user_agent(lc);
+}
+
+const char *linphone_core_get_user_agent_name(void){
+       return _ua_name;
+}
+
+const char *linphone_core_get_user_agent_version(void){
+       return _ua_version;
 }
 
 static void transport_error(LinphoneCore *lc, const char* transport, int port){
@@ -1607,24 +1936,22 @@ void linphone_core_enable_ipv6(LinphoneCore *lc, bool_t val){
 
 
 static void monitor_network_state(LinphoneCore *lc, time_t curtime){
-       static time_t last_check=0;
-       static bool_t last_status=FALSE;
        char result[LINPHONE_IPADDR_SIZE];
-       bool_t new_status=last_status;
+       bool_t new_status=lc->network_last_status;
 
        /* only do the network up checking every five seconds */
-       if (last_check==0 || (curtime-last_check)>=5){
+       if (lc->network_last_check==0 || (curtime-lc->network_last_check)>=5){
                linphone_core_get_local_ip_for(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,NULL,result);
                if (strcmp(result,"::1")!=0 && strcmp(result,"127.0.0.1")!=0){
                        new_status=TRUE;
                }else new_status=FALSE;
-               last_check=curtime;
-               if (new_status!=last_status) {
+               lc->network_last_check=curtime;
+               if (new_status!=lc->network_last_status) {
                        if (new_status){
                                ms_message("New local ip address is %s",result);
                        }
                        set_network_reachable(lc,new_status, curtime);
-                       last_status=new_status;
+                       lc->network_last_status=new_status;
                }
        }
 }
@@ -1747,7 +2074,13 @@ void linphone_core_iterate(LinphoneCore *lc){
                                lc->ecc->cb(lc,ecs,lc->ecc->delay,lc->ecc->cb_data);
                        if (ecs==LinphoneEcCalibratorDone){
                                int len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
-                               lp_config_set_int(lc->config, "sound", "ec_delay",MAX(lc->ecc->delay-(len/2),0));
+                               int margin=len/2;
+                               
+                               lp_config_set_int(lc->config, "sound", "ec_delay",MAX(lc->ecc->delay-margin,0));
+                       } else if (ecs == LinphoneEcCalibratorFailed) {
+                               lp_config_set_int(lc->config, "sound", "ec_delay", -1);/*use default value from soundcard*/
+                       } else if (ecs == LinphoneEcCalibratorDoneNoEcho) {
+                               linphone_core_enable_echo_cancellation(lc, FALSE);
                        }
                        ec_calibrator_destroy(lc->ecc);
                        lc->ecc=NULL;
@@ -1778,23 +2111,43 @@ void linphone_core_iterate(LinphoneCore *lc){
        calls= lc->calls;
        while(calls!= NULL){
                call = (LinphoneCall *)calls->data;
+               elapsed = curtime-call->start_time;
                 /* get immediately a reference to next one in case the one
                 we are going to examine is destroy and removed during
                 linphone_core_start_invite() */
                calls=calls->next;
                linphone_call_background_tasks(call,one_second_elapsed);
-               if (call->state==LinphoneCallOutgoingInit && (curtime-call->start_time>=2)){
+               if (call->state==LinphoneCallOutgoingInit && (elapsed>=lc->sip_conf.delayed_timeout)){
                        /*start the call even if the OPTIONS reply did not arrive*/
-                       linphone_core_start_invite(lc,call,NULL);
+                       if (call->ice_session != NULL) {
+                               ms_warning("ICE candidates gathering from [%s] has not finished yet, proceed with the call without ICE anyway."
+                                               ,linphone_core_get_stun_server(lc));
+                               linphone_call_delete_ice_session(call);
+                               linphone_call_stop_media_streams_for_ice_gathering(call);
+                       }
+#ifdef BUILD_UPNP
+                       if (call->upnp_session != NULL) {
+                               ms_warning("uPnP mapping has not finished yet, proceeded with the call without uPnP anyway.");
+                               linphone_call_delete_upnp_session(call);
+                       }
+#endif //BUILD_UPNP
+                       linphone_core_start_invite(lc,call);
                }
-               if (call->dir==LinphoneCallIncoming && call->state==LinphoneCallOutgoingRinging){
-                       elapsed=curtime-call->start_time;
+               if (call->state==LinphoneCallIncomingReceived){
                        ms_message("incoming call ringing for %i seconds",elapsed);
                        if (elapsed>lc->sip_conf.inc_timeout){
+                               LinphoneReason decline_reason;
+                               ms_message("incoming call timeout (%i)",lc->sip_conf.inc_timeout);
+                               decline_reason=lc->current_call ? LinphoneReasonBusy : LinphoneReasonDeclined;
                                call->log->status=LinphoneCallMissed;
-                               linphone_core_terminate_call(lc,call);
+                               call->reason=LinphoneReasonNotAnswered;
+                               linphone_core_decline_call(lc,call,decline_reason);
                        }
                }
+               if (lc->sip_conf.in_call_timeout > 0 && elapsed>lc->sip_conf.in_call_timeout) {
+                       ms_message("in call timeout (%i)",lc->sip_conf.in_call_timeout);
+                       linphone_core_terminate_call(lc,call);
+               }
        }
                
        if (linphone_core_video_preview_enabled(lc)){
@@ -1817,14 +2170,18 @@ void linphone_core_iterate(LinphoneCore *lc){
                lc->initial_subscribes_sent=TRUE;
        }
 
-       if (one_second_elapsed && lp_config_needs_commit(lc->config)){
-               lp_config_sync(lc->config);
+       if (one_second_elapsed) {
+               if (lp_config_needs_commit(lc->config)) {
+                       lp_config_sync(lc->config);
+               }
        }
 }
 
 /**
  * Interpret a call destination as supplied by the user, and returns a fully qualified
  * LinphoneAddress.
+ * 
+ * @ingroup call_control
  *
  * A sip address should look like DisplayName <sip:username@domain:port> .
  * Basically this function performs the following tasks
@@ -1930,11 +2287,20 @@ const char * linphone_core_get_route(LinphoneCore *lc){
 void linphone_core_start_refered_call(LinphoneCore *lc, LinphoneCall *call){
        if (call->refer_pending){
                LinphoneCallParams *cp=linphone_core_create_default_call_parameters(lc);
+               LinphoneCall *newcall;
+               cp->has_video &= !!lc->video_policy.automatically_initiate;
                cp->referer=call;
                ms_message("Starting new call to refered address %s",call->refer_to);
                call->refer_pending=FALSE;
-               linphone_core_invite_with_params(lc,call->refer_to,cp);
+               newcall=linphone_core_invite_with_params(lc,call->refer_to,cp);
                linphone_call_params_destroy(cp);
+               if (newcall) linphone_core_notify_refer_state(lc,call,newcall);
+       }
+}
+
+void linphone_core_notify_refer_state(LinphoneCore *lc, LinphoneCall *referer, LinphoneCall *newcall){
+       if (referer->op!=NULL){
+               sal_call_notify_refer_state(referer->op,newcall ? newcall->op : NULL);
        }
 }
 
@@ -2022,11 +2388,43 @@ static char *get_fixed_contact(LinphoneCore *lc, LinphoneCall *call , LinphonePr
        return NULL;
 }
 
-int linphone_core_start_invite(LinphoneCore *lc, LinphoneCall *call, LinphoneProxyConfig *dest_proxy){
+int linphone_core_proceed_with_invite_if_ready(LinphoneCore *lc, LinphoneCall *call, LinphoneProxyConfig *dest_proxy){
+       bool_t ice_ready = FALSE;
+       bool_t upnp_ready = FALSE;
+       bool_t ping_ready = FALSE;
+
+       if (call->ice_session != NULL) {
+               if (ice_session_candidates_gathered(call->ice_session)) ice_ready = TRUE;
+       } else {
+               ice_ready = TRUE;
+       }
+#ifdef BUILD_UPNP
+       if (call->upnp_session != NULL) {
+               if (linphone_upnp_session_get_state(call->upnp_session) == LinphoneUpnpStateOk) upnp_ready = TRUE;
+       } else {
+               upnp_ready = TRUE;
+       }
+#else
+       upnp_ready=TRUE;
+#endif //BUILD_UPNP
+       if (call->ping_op != NULL) {
+               if (call->ping_replied == TRUE) ping_ready = TRUE;
+       } else {
+               ping_ready = TRUE;
+       }
+
+       if ((ice_ready == TRUE) && (upnp_ready == TRUE) && (ping_ready == TRUE)) {
+               return linphone_core_start_invite(lc, call);
+       }
+       return 0;
+}
+
+int linphone_core_start_invite(LinphoneCore *lc, LinphoneCall *call){
        int err;
        char *contact;
        char *real_url,*barmsg;
        char *from;
+       LinphoneProxyConfig *dest_proxy=call->dest_proxy;
 
        /*try to be best-effort in giving real local or routable contact address */
        contact=get_fixed_contact(lc,call,dest_proxy);
@@ -2034,9 +2432,11 @@ int linphone_core_start_invite(LinphoneCore *lc, LinphoneCall *call, LinphonePro
                sal_op_set_contact(call->op, contact);
                ms_free(contact);
        }
-
-       //TODO : should probably not be done here
+       linphone_core_stop_dtmf_stream(lc);
        linphone_call_init_media_streams(call);
+       if (lc->ringstream==NULL)
+               audio_stream_prepare_sound(call->audiostream,lc->sound_conf.play_sndcard,lc->sound_conf.capt_sndcard);
+       linphone_call_make_local_media_description(lc,call);
        if (!lc->sip_conf.sdp_200_ack){
                call->media_pending=TRUE;
                sal_call_set_local_media_description(call->op,call->localdesc);
@@ -2044,6 +2444,7 @@ int linphone_core_start_invite(LinphoneCore *lc, LinphoneCall *call, LinphonePro
        real_url=linphone_address_as_string(call->log->to);
        from=linphone_address_as_string(call->log->from);
        err=sal_call(call->op,from,real_url);
+       call->log->call_id=ms_strdup(sal_op_get_call_id(call->op)); /*must be known at that time*/
 
        if (lc->sip_conf.sdp_200_ack){
                call->media_pending=TRUE;
@@ -2082,6 +2483,7 @@ int linphone_core_start_invite(LinphoneCore *lc, LinphoneCall *call, LinphonePro
 LinphoneCall * linphone_core_invite(LinphoneCore *lc, const char *url){
        LinphoneCall *call;
        LinphoneCallParams *p=linphone_core_create_default_call_parameters (lc);
+       p->has_video &= !!lc->video_policy.automatically_initiate;
        call=linphone_core_invite_with_params(lc,url,p);
        linphone_call_params_destroy(p);
        return call;
@@ -2128,7 +2530,8 @@ LinphoneCall * linphone_core_invite_with_params(LinphoneCore *lc, const char *ur
 **/
 LinphoneCall * linphone_core_invite_address(LinphoneCore *lc, const LinphoneAddress *addr){
        LinphoneCall *call;
-       LinphoneCallParams *p=linphone_core_create_default_call_parameters (lc);
+       LinphoneCallParams *p=linphone_core_create_default_call_parameters(lc);
+       p->has_video &= !!lc->video_policy.automatically_initiate;
        call=linphone_core_invite_address_with_params (lc,addr,p);
        linphone_call_params_destroy(p);
        return call;
@@ -2154,11 +2557,11 @@ LinphoneCall * linphone_core_invite_address_with_params(LinphoneCore *lc, const
 {
        const char *route=NULL;
        const char *from=NULL;
-       LinphoneProxyConfig *proxy=NULL;
+       LinphoneProxyConfig *proxy=NULL,*dest_proxy=NULL;
        LinphoneAddress *parsed_url2=NULL;
        char *real_url=NULL;
-       LinphoneProxyConfig *dest_proxy=NULL;
        LinphoneCall *call;
+       bool_t defer = FALSE;
 
        linphone_core_preempt_sound_resources(lc);
        
@@ -2189,6 +2592,7 @@ LinphoneCall * linphone_core_invite_address_with_params(LinphoneCore *lc, const
        parsed_url2=linphone_address_new(from);
 
        call=linphone_call_new_outgoing(lc,parsed_url2,linphone_address_clone(addr),params);
+       call->dest_proxy=dest_proxy;
        sal_op_set_route(call->op,route);
 
        if(linphone_core_add_call(lc,call)!= 0)
@@ -2200,15 +2604,50 @@ LinphoneCall * linphone_core_invite_address_with_params(LinphoneCore *lc, const
        /* this call becomes now the current one*/
        lc->current_call=call;
        linphone_call_set_state (call,LinphoneCallOutgoingInit,"Starting outgoing call");
-       if (dest_proxy!=NULL || lc->sip_conf.ping_with_options==FALSE){
-               linphone_core_start_invite(lc,call,dest_proxy);
-       }else{
-               /*defer the start of the call after the OPTIONS ping*/
-               call->ping_op=sal_op_new(lc->sal);
-               sal_ping(call->ping_op,from,real_url);
-               sal_op_set_user_pointer(call->ping_op,call);
+       if (linphone_core_get_firewall_policy(call->core) == LinphonePolicyUseIce) {
+               /* Defer the start of the call after the ICE gathering process. */
+               linphone_call_init_media_streams(call);
+               linphone_call_start_media_streams_for_ice_gathering(call);
+               call->start_time=time(NULL);
+               if (linphone_core_gather_ice_candidates(lc,call)<0) {
+                       /* Ice candidates gathering failed, proceed with the call anyway. */
+                       linphone_call_delete_ice_session(call);
+                       linphone_call_stop_media_streams_for_ice_gathering(call);
+               } else {
+                       defer = TRUE;
+               }
+       }
+       else if (linphone_core_get_firewall_policy(call->core) == LinphonePolicyUseUpnp) {
+#ifdef BUILD_UPNP
+               linphone_call_init_media_streams(call);
                call->start_time=time(NULL);
+               if (linphone_core_update_upnp(lc,call)<0) {
+                       /* uPnP port mappings failed, proceed with the call anyway. */
+                       linphone_call_delete_upnp_session(call);
+               } else {
+                       defer = TRUE;
+               }
+#endif //BUILD_UPNP
+       }
+
+       if (call->dest_proxy==NULL && lc->sip_conf.ping_with_options==TRUE){
+#ifdef BUILD_UPNP
+               if (lc->upnp != NULL && linphone_core_get_firewall_policy(lc)==LinphonePolicyUseUpnp &&
+                       linphone_upnp_context_get_state(lc->upnp) == LinphoneUpnpStateOk) {
+#else //BUILD_UPNP
+               {
+#endif //BUILD_UPNP
+                       /*defer the start of the call after the OPTIONS ping*/
+                       call->ping_replied=FALSE;
+                       call->ping_op=sal_op_new(lc->sal);
+                       sal_ping(call->ping_op,from,real_url);
+                       sal_op_set_user_pointer(call->ping_op,call);
+                       call->start_time=time(NULL);
+                       defer = TRUE;
+               }
        }
+       
+       if (defer==FALSE) linphone_core_start_invite(lc,call);
 
        if (real_url!=NULL) ms_free(real_url);
        return call;
@@ -2217,6 +2656,7 @@ LinphoneCall * linphone_core_invite_address_with_params(LinphoneCore *lc, const
 /**
  * Performs a simple call transfer to the specified destination.
  *
+ * @ingroup call_control
  * The remote endpoint is expected to issue a new call to the specified destination.
  * The current call remains active and thus can be later paused or terminated.
 **/
@@ -2238,6 +2678,7 @@ int linphone_core_transfer_call(LinphoneCore *lc, LinphoneCall *call, const char
        sal_call_refer(call->op,real_url);
        ms_free(real_url);
        linphone_address_destroy(real_parsed_url);
+       linphone_call_set_transfer_state(call, LinphoneCallOutgoingInit);
        return 0;
 }
 
@@ -2246,6 +2687,8 @@ int linphone_core_transfer_call(LinphoneCore *lc, LinphoneCall *call, const char
  * @param lc linphone core object
  * @param call a running call you want to transfer
  * @param dest a running call whose remote person will receive the transfer
+ * 
+ * @ingroup call_control
  *
  * The transfered call is supposed to be in paused state, so that it is able to accept the transfer immediately.
  * The destination call is a call previously established to introduce the transfered person.
@@ -2254,7 +2697,9 @@ int linphone_core_transfer_call(LinphoneCore *lc, LinphoneCall *call, const char
  * close the call with us (the 'dest' call).
 **/
 int linphone_core_transfer_call_to_another(LinphoneCore *lc, LinphoneCall *call, LinphoneCall *dest){
-       return sal_call_refer_with_replaces (call->op,dest->op);
+       int result = sal_call_refer_with_replaces (call->op,dest->op);
+       linphone_call_set_transfer_state(call, LinphoneCallOutgoingInit);
+       return result;
 }
 
 bool_t linphone_core_inc_invite_pending(LinphoneCore*lc){
@@ -2268,35 +2713,185 @@ bool_t linphone_core_inc_invite_pending(LinphoneCore*lc){
        return FALSE;
 }
 
-/**
- * @ingroup call_control
- * Updates a running call according to supplied call parameters or parameters changed in the LinphoneCore.
- *
- * In this version this is limited to the following use cases:
- * - setting up/down the video stream according to the video parameter of the LinphoneCallParams (see linphone_call_params_enable_video() ).
- * - changing the size of the transmitted video after calling linphone_core_set_preferred_video_size()
- *
- * In case no changes are requested through the LinphoneCallParams argument, then this argument can be omitted and set to NULL.
- *
- * @return 0 if successful, -1 otherwise.
-**/
-int linphone_core_update_call(LinphoneCore *lc, LinphoneCall *call, const LinphoneCallParams *params){
-       int err=0;
+bool_t linphone_core_incompatible_security(LinphoneCore *lc, SalMediaDescription *md){
+       if (linphone_core_is_media_encryption_mandatory(lc) && linphone_core_get_media_encryption(lc)==LinphoneMediaEncryptionSRTP){
+               int i;
+               for(i=0;i<md->n_active_streams;i++){
+                       SalStreamDescription *sd=&md->streams[i];
+                       if (sd->proto!=SalProtoRtpSavp){
+                               return TRUE;
+                       }
+               }
+       }
+       return FALSE;
+}
+
+void linphone_core_notify_incoming_call(LinphoneCore *lc, LinphoneCall *call){
+       char *barmesg;
+       char *tmp;
+       LinphoneAddress *from_parsed;
+       SalMediaDescription *md;
+       bool_t propose_early_media=lp_config_get_int(lc->config,"sip","incoming_calls_early_media",FALSE);
+       const char *ringback_tone=linphone_core_get_remote_ringback_tone (lc);
+
+       linphone_call_make_local_media_description(lc,call);
+       sal_call_set_local_media_description(call->op,call->localdesc);
+       md=sal_call_get_final_media_description(call->op);
+       if (md){
+               if (sal_media_description_empty(md) || linphone_core_incompatible_security(lc,md)){
+                       sal_call_decline(call->op,SalReasonMedia,NULL);
+                       linphone_call_unref(call);
+                       return;
+               }
+       }
+
+       from_parsed=linphone_address_new(sal_op_get_from(call->op));
+       linphone_address_clean(from_parsed);
+       tmp=linphone_address_as_string(from_parsed);
+       linphone_address_destroy(from_parsed);
+       barmesg=ortp_strdup_printf("%s %s%s",tmp,_("is contacting you"),
+           (sal_call_autoanswer_asked(call->op)) ?_(" and asked autoanswer."):_("."));
+       if (lc->vtable.show) lc->vtable.show(lc);
+       if (lc->vtable.display_status)
+           lc->vtable.display_status(lc,barmesg);
+
+       /* play the ring if this is the only call*/
+       if (ms_list_size(lc->calls)==1){
+               lc->current_call=call;
+               if (lc->ringstream && lc->dmfs_playing_start_time!=0){
+                       ring_stop(lc->ringstream);
+                       lc->ringstream=NULL;
+                       lc->dmfs_playing_start_time=0;
+               }
+               if (lc->sound_conf.ring_sndcard!=NULL){
+                       if(lc->ringstream==NULL && lc->sound_conf.local_ring){
+                               MSSndCard *ringcard=lc->sound_conf.lsd_card ?lc->sound_conf.lsd_card : lc->sound_conf.ring_sndcard;
+                               ms_message("Starting local ring...");
+                               lc->ringstream=ring_start(lc->sound_conf.local_ring,2000,ringcard);
+                       }
+                       else
+                       {
+                               ms_message("the local ring is already started");
+                       }
+               }
+       }else{
+               /* else play a tone within the context of the current call */
+               call->ringing_beep=TRUE;
+               linphone_core_play_named_tone(lc,LinphoneToneCallWaiting);
+       }
+
+       linphone_call_set_state(call,LinphoneCallIncomingReceived,"Incoming call");
+
+       if (call->state==LinphoneCallIncomingReceived){
+               sal_call_notify_ringing(call->op,propose_early_media || ringback_tone!=NULL);
+
+               if (propose_early_media || ringback_tone!=NULL){
+                       linphone_call_set_state(call,LinphoneCallIncomingEarlyMedia,"Incoming call early media");
+                       md=sal_call_get_final_media_description(call->op);
+                       linphone_core_update_streams(lc,call,md);
+               }
+               if (sal_call_get_replaces(call->op)!=NULL && lp_config_get_int(lc->config,"sip","auto_answer_replacing_calls",1)){
+                       linphone_core_accept_call(lc,call);
+               }
+       }
+       linphone_call_unref(call);
+
+       ms_free(barmesg);
+       ms_free(tmp);
+}
+
+int linphone_core_start_update_call(LinphoneCore *lc, LinphoneCall *call){
+       const char *subject;
+       call->camera_active=call->params.has_video;
+       if (call->ice_session != NULL) {
+               linphone_core_update_local_media_description_from_ice(call->localdesc, call->ice_session);
+       }
+#ifdef BUILD_UPNP
+       if(call->upnp_session != NULL) {
+               linphone_core_update_local_media_description_from_upnp(call->localdesc, call->upnp_session);
+       }
+#endif //BUILD_UPNP
+       if (call->params.in_conference){
+               subject="Conference";
+       }else{
+               subject="Media change";
+       }
+       if (lc->vtable.display_status)
+               lc->vtable.display_status(lc,_("Modifying call parameters..."));
+       sal_call_set_local_media_description (call->op,call->localdesc);
+       return sal_call_update(call->op,subject);
+}
+
+/**
+ * @ingroup call_control
+ * Updates a running call according to supplied call parameters or parameters changed in the LinphoneCore.
+ *
+ * In this version this is limited to the following use cases:
+ * - setting up/down the video stream according to the video parameter of the LinphoneCallParams (see linphone_call_params_enable_video() ).
+ * - changing the size of the transmitted video after calling linphone_core_set_preferred_video_size()
+ *
+ * In case no changes are requested through the LinphoneCallParams argument, then this argument can be omitted and set to NULL.
+ * @param lc the core
+ * @param call the call to be updated
+ * @param params the new call parameters to use. (may be NULL)
+ * @return 0 if successful, -1 otherwise.
+**/
+int linphone_core_update_call(LinphoneCore *lc, LinphoneCall *call, const LinphoneCallParams *params){
+       int err=0;
        if (params!=NULL){
-               const char *subject;
-               call->params=*params;
-               update_local_media_description(lc,call,&call->localdesc);
-               call->camera_active=params->has_video;
+               linphone_call_set_state(call,LinphoneCallUpdating,"Updating call");
+#ifdef VIDEO_ENABLED
+               bool_t has_video = call->params.has_video;
 
-               if (params->in_conference){
-                       subject="Conference";
-               }else{
-                       subject="Media change";
+               // Video removing
+               if((call->videostream != NULL) && !params->has_video) {
+                       if (call->ice_session != NULL) {
+                               ice_session_remove_check_list(call->ice_session, call->videostream->ms.ice_check_list);
+                               call->videostream->ms.ice_check_list = NULL;
+                       }
+#ifdef BUILD_UPNP
+                       if(call->upnp_session != NULL) {
+                               if (linphone_core_update_upnp(lc, call)<0) {
+                                       /* uPnP port mappings failed, proceed with the call anyway. */
+                                       linphone_call_delete_upnp_session(call);
+                               }
+                       }
+#endif //BUILD_UPNP
+               }
+               
+               _linphone_call_params_copy(&call->params,params);
+               linphone_call_make_local_media_description(lc, call);
+
+               // Video adding
+               if (!has_video && call->params.has_video) {
+                       if (call->ice_session != NULL) {
+                               /* Defer call update until the ICE candidates gathering process has finished. */
+                               ms_message("Defer call update to gather ICE candidates");
+                               linphone_call_init_video_stream(call);
+                               video_stream_prepare_video(call->videostream);
+                               if (linphone_core_gather_ice_candidates(lc,call)<0) {
+                                       /* Ice candidates gathering failed, proceed with the call anyway. */
+                                       linphone_call_delete_ice_session(call);
+                               } else {
+                                       return err;
+                               }
+                       }
+#ifdef BUILD_UPNP
+                       if(call->upnp_session != NULL) {
+                               ms_message("Defer call update to add uPnP port mappings");
+                               linphone_call_init_video_stream(call);
+                               video_stream_prepare_video(call->videostream);
+                               if (linphone_core_update_upnp(lc, call)<0) {
+                                       /* uPnP port mappings failed, proceed with the call anyway. */
+                                       linphone_call_delete_upnp_session(call);
+                               } else {
+                                       return err;
+                               }
+                       }
+#endif //BUILD_UPNP
                }
-               if (lc->vtable.display_status)
-                       lc->vtable.display_status(lc,_("Modifying call parameters..."));
-               sal_call_set_local_media_description (call->op,call->localdesc);
-               err=sal_call_update(call->op,subject);
+#endif
+               err = linphone_core_start_update_call(lc, call);
        }else{
 #ifdef VIDEO_ENABLED
                if (call->videostream!=NULL){
@@ -2311,6 +2906,143 @@ int linphone_core_update_call(LinphoneCore *lc, LinphoneCall *call, const Linpho
        return err;
 }
 
+/**
+ * @ingroup call_control
+ * When receiving a #LinphoneCallUpdatedByRemote state notification, prevent LinphoneCore from performing an automatic answer.
+ * 
+ * When receiving a #LinphoneCallUpdatedByRemote state notification (ie an incoming reINVITE), the default behaviour of
+ * LinphoneCore is to automatically answer the reINIVTE with call parameters unchanged.
+ * However when for example when the remote party updated the call to propose a video stream, it can be useful
+ * to prompt the user before answering. This can be achieved by calling linphone_core_defer_call_update() during 
+ * the call state notifiacation, to deactivate the automatic answer that would just confirm the audio but reject the video.
+ * Then, when the user responds to dialog prompt, it becomes possible to call linphone_core_accept_call_update() to answer
+ * the reINVITE, with eventually video enabled in the LinphoneCallParams argument.
+ * 
+ * @return 0 if successful, -1 if the linphone_core_defer_call_update() was done outside a #LinphoneCallUpdatedByRemote notification, which is illegal.
+**/
+int linphone_core_defer_call_update(LinphoneCore *lc, LinphoneCall *call){
+       if (call->state==LinphoneCallUpdatedByRemote){
+               call->defer_update=TRUE;
+               return 0;
+       }
+       ms_error("linphone_core_defer_call_update() not done in state LinphoneCallUpdatedByRemote");
+       return -1;
+}
+
+int linphone_core_start_accept_call_update(LinphoneCore *lc, LinphoneCall *call){
+       SalMediaDescription *md;
+       if (call->ice_session != NULL) {
+               if (ice_session_nb_losing_pairs(call->ice_session) > 0) {
+                       /* Defer the sending of the answer until there are no losing pairs left. */
+                       return 0;
+               }
+               linphone_core_update_local_media_description_from_ice(call->localdesc, call->ice_session);
+       }
+#ifdef BUILD_UPNP
+       if(call->upnp_session != NULL) {
+               linphone_core_update_local_media_description_from_upnp(call->localdesc, call->upnp_session);
+       }
+#endif //BUILD_UPNP
+       linphone_call_update_remote_session_id_and_ver(call);
+       sal_call_set_local_media_description(call->op,call->localdesc);
+       sal_call_accept(call->op);
+       md=sal_call_get_final_media_description(call->op);
+       if (md && !sal_media_description_empty(md))
+               linphone_core_update_streams (lc,call,md);
+       linphone_call_set_state(call,LinphoneCallStreamsRunning,"Connected (streams running)");
+       return 0;
+}
+
+/**
+ * @ingroup call_control
+ * Accept call modifications initiated by other end.
+ * 
+ * This call may be performed in response to a #LinphoneCallUpdatedByRemote state notification.
+ * When such notification arrives, the application can decide to call linphone_core_defer_update_call() so that it can
+ * have the time to prompt the user. linphone_call_get_remote_params() can be used to get information about the call parameters
+ * requested by the other party, such as whether a video stream is requested.
+ * 
+ * When the user accepts or refuse the change, linphone_core_accept_call_update() can be done to answer to the other party.
+ * If params is NULL, then the same call parameters established before the update request will continue to be used (no change).
+ * If params is not NULL, then the update will be accepted according to the parameters passed.
+ * Typical example is when a user accepts to start video, then params should indicate that video stream should be used 
+ * (see linphone_call_params_enable_video()).
+ * @param lc the linphone core object.
+ * @param call the LinphoneCall object
+ * @param params a LinphoneCallParams object describing the call parameters to accept.
+ * @return 0 if sucessful, -1 otherwise (actually when this function call is performed outside ot #LinphoneCallUpdatedByRemote state).
+**/
+int linphone_core_accept_call_update(LinphoneCore *lc, LinphoneCall *call, const LinphoneCallParams *params){
+       SalMediaDescription *remote_desc;
+       bool_t keep_sdp_version;
+#ifdef VIDEO_ENABLED
+       bool_t old_has_video = call->params.has_video;
+#endif
+       if (call->state!=LinphoneCallUpdatedByRemote){
+               ms_error("linphone_core_accept_update(): invalid state %s to call this function.",
+                        linphone_call_state_to_string(call->state));
+               return -1;
+       }
+       remote_desc = sal_call_get_remote_media_description(call->op);
+       keep_sdp_version = lp_config_get_int(lc->config, "sip", "keep_sdp_version", 0);
+       if (keep_sdp_version &&(remote_desc->session_id == call->remote_session_id) && (remote_desc->session_ver == call->remote_session_ver)) {
+               /* Remote has sent an INVITE with the same SDP as before, so send a 200 OK with the same SDP as before. */
+               ms_warning("SDP version has not changed, send same SDP as before.");
+               sal_call_accept(call->op);
+               linphone_call_set_state(call,LinphoneCallStreamsRunning,"Connected (streams running)");
+               return 0;
+       }
+       if (params==NULL){
+               call->params.has_video=lc->video_policy.automatically_accept || call->current_params.has_video;
+       }else
+               call->params=*params;
+
+       if (call->params.has_video && !linphone_core_video_enabled(lc)){
+               ms_warning("linphone_core_accept_call_update(): requested video but video support is globally disabled. Refusing video.");
+               call->params.has_video=FALSE;
+       }
+       if (call->current_params.in_conference) {
+               ms_warning("Video isn't supported in conference");
+               call->params.has_video = FALSE;
+       }
+       call->params.has_video &= linphone_core_media_description_contains_video_stream(remote_desc);
+       call->camera_active=call->params.has_video;
+       linphone_call_make_local_media_description(lc,call);
+       if (call->ice_session != NULL) {
+               linphone_core_update_ice_from_remote_media_description(call, remote_desc);
+#ifdef VIDEO_ENABLED
+               if ((call->ice_session != NULL) &&!ice_session_candidates_gathered(call->ice_session)) {
+                       if ((call->params.has_video) && (call->params.has_video != old_has_video)) {
+                               linphone_call_init_video_stream(call);
+                               video_stream_prepare_video(call->videostream);
+                               if (linphone_core_gather_ice_candidates(lc,call)<0) {
+                                       /* Ice candidates gathering failed, proceed with the call anyway. */
+                                       linphone_call_delete_ice_session(call);
+                               } else return 0;
+                       }
+               }
+#endif //VIDEO_ENABLED
+       }
+
+#ifdef BUILD_UPNP
+       if(call->upnp_session != NULL) {
+               linphone_core_update_upnp_from_remote_media_description(call, sal_call_get_remote_media_description(call->op));
+#ifdef VIDEO_ENABLED
+               if ((call->params.has_video) && (call->params.has_video != old_has_video)) {
+                       linphone_call_init_video_stream(call);
+                       video_stream_prepare_video(call->videostream);
+                       if (linphone_core_update_upnp(lc, call)<0) {
+                               /* uPnP update failed, proceed with the call anyway. */
+                               linphone_call_delete_upnp_session(call);
+                       } else return 0;
+               }
+#endif //VIDEO_ENABLED
+       }
+#endif //BUILD_UPNP
+
+       linphone_core_start_accept_call_update(lc, call);
+       return 0;
+}
 
 /**
  * Accept an incoming call.
@@ -2319,18 +3051,36 @@ int linphone_core_update_call(LinphoneCore *lc, LinphoneCall *call, const Linpho
  * Basically the application is notified of incoming calls within the
  * call_state_changed callback of the #LinphoneCoreVTable structure, where it will receive
  * a LinphoneCallIncoming event with the associated LinphoneCall object.
+ * The application can later accept the call using this method.
+ * @param lc the LinphoneCore object
+ * @param call the LinphoneCall object representing the call to be answered.
+ *
+**/
+int linphone_core_accept_call(LinphoneCore *lc, LinphoneCall *call){
+       return linphone_core_accept_call_with_params(lc,call,NULL);
+}
+
+/**
+ * Accept an incoming call, with parameters.
+ *
+ * @ingroup call_control
+ * Basically the application is notified of incoming calls within the
+ * call_state_changed callback of the #LinphoneCoreVTable structure, where it will receive
+ * a LinphoneCallIncoming event with the associated LinphoneCall object.
  * The application can later accept the call using
  * this method.
  * @param lc the LinphoneCore object
  * @param call the LinphoneCall object representing the call to be answered.
+ * @param params the specific parameters for this call, for example whether video is accepted or not. Use NULL to use default parameters.
  *
 **/
-int linphone_core_accept_call(LinphoneCore *lc, LinphoneCall *call)
+int linphone_core_accept_call_with_params(LinphoneCore *lc, LinphoneCall *call, const LinphoneCallParams *params)
 {
-       LinphoneProxyConfig *cfg=NULL,*dest_proxy=NULL;
+       LinphoneProxyConfig *cfg=NULL;
        const char *contact=NULL;
        SalOp *replaced;
        SalMediaDescription *new_md;
+       bool_t was_ringing=FALSE;
 
        if (call==NULL){
                //if just one call is present answer the only one ...
@@ -2366,6 +3116,7 @@ int linphone_core_accept_call(LinphoneCore *lc, LinphoneCall *call)
                ring_stop(lc->ringstream);
                ms_message("ring stopped");
                lc->ringstream=NULL;
+               was_ringing=TRUE;
        }
        if (call->ringing_beep){
                linphone_core_stop_dtmf(lc);
@@ -2373,21 +3124,36 @@ int linphone_core_accept_call(LinphoneCore *lc, LinphoneCall *call)
        }
 
        linphone_core_get_default_proxy(lc,&cfg);
-       dest_proxy=cfg;
-       dest_proxy=linphone_core_lookup_known_proxy(lc,call->log->to);
+       call->dest_proxy=cfg;
+       call->dest_proxy=linphone_core_lookup_known_proxy(lc,call->log->to);
 
-       if (cfg!=dest_proxy && dest_proxy!=NULL) {
+       if (cfg!=call->dest_proxy && call->dest_proxy!=NULL) {
                ms_message("Overriding default proxy setting for this call:");
-               ms_message("The used identity will be %s",linphone_proxy_config_get_identity(dest_proxy));
+               ms_message("The used identity will be %s",linphone_proxy_config_get_identity(call->dest_proxy));
        }
        /*try to be best-effort in giving real local or routable contact address*/
-       contact=get_fixed_contact(lc,call,dest_proxy);
+       contact=get_fixed_contact(lc,call,call->dest_proxy);
        if (contact)
                sal_op_set_contact(call->op,contact);
 
+       if (params){
+               const SalMediaDescription *md = sal_call_get_remote_media_description(call->op);
+               _linphone_call_params_copy(&call->params,params);
+               // There might not be a md if the INVITE was lacking an SDP
+               // In this case we use the parameters as is.
+               if (md) call->params.has_video &= linphone_core_media_description_contains_video_stream(md);
+               call->camera_active=call->params.has_video;
+               linphone_call_make_local_media_description(lc,call);
+               sal_call_set_local_media_description(call->op,call->localdesc);
+       }
+       
        if (call->audiostream==NULL)
                linphone_call_init_media_streams(call);
+       if (!was_ringing && call->audiostream->ms.ticker==NULL){
+               audio_stream_prepare_sound(call->audiostream,lc->sound_conf.play_sndcard,lc->sound_conf.capt_sndcard);
+       }
 
+       linphone_call_update_remote_session_id_and_ver(call);
        sal_call_accept(call->op);
        if (lc->vtable.display_status!=NULL)
                lc->vtable.display_status(lc,_("Connected."));
@@ -2412,6 +3178,11 @@ int linphone_core_abort_call(LinphoneCore *lc, LinphoneCall *call, const char *e
                lc->ringstream=NULL;
        }
        linphone_call_stop_media_streams(call);
+
+#ifdef BUILD_UPNP
+       linphone_call_delete_upnp_session(call);
+#endif //BUILD_UPNP
+
        if (lc->vtable.display_status!=NULL)
                lc->vtable.display_status(lc,_("Call aborted") );
        linphone_call_set_state(call,LinphoneCallError,error);
@@ -2420,7 +3191,8 @@ int linphone_core_abort_call(LinphoneCore *lc, LinphoneCall *call, const char *e
 
 static void terminate_call(LinphoneCore *lc, LinphoneCall *call){
        if (call->state==LinphoneCallIncomingReceived){
-               call->reason=LinphoneReasonDeclined;
+               if (call->reason!=LinphoneReasonNotAnswered)
+                       call->reason=LinphoneReasonDeclined;
        }
        /*stop ringing*/
        if (lc->ringstream!=NULL) {
@@ -2429,8 +3201,14 @@ static void terminate_call(LinphoneCore *lc, LinphoneCall *call){
        }
 
        linphone_call_stop_media_streams(call);
+
+#ifdef BUILD_UPNP
+       linphone_call_delete_upnp_session(call);
+#endif //BUILD_UPNP
+
        if (lc->vtable.display_status!=NULL)
                lc->vtable.display_status(lc,_("Call ended") );
+       linphone_call_set_state(call,LinphoneCallEnd,"Call terminated");
 }
 
 int linphone_core_redirect_call(LinphoneCore *lc, LinphoneCall *call, const char *redirect_uri){
@@ -2438,7 +3216,6 @@ int linphone_core_redirect_call(LinphoneCore *lc, LinphoneCall *call, const char
                sal_call_decline(call->op,SalReasonRedirect,redirect_uri);
                call->reason=LinphoneReasonDeclined;
                terminate_call(lc,call);
-               linphone_call_set_state(call,LinphoneCallEnd,"Call terminated");
        }else{
                ms_error("Bad state for call redirection.");
                return -1;
@@ -2472,8 +3249,38 @@ int linphone_core_terminate_call(LinphoneCore *lc, LinphoneCall *the_call)
        }
        sal_call_terminate(call->op);
        terminate_call(lc,call);
+       return 0;
+}
 
-       linphone_call_set_state(call,LinphoneCallEnd,"Call terminated");
+/**
+ * Decline a pending incoming call, with a reason.
+ * 
+ * @ingroup call_control
+ * 
+ * @param lc the linphone core
+ * @param call the LinphoneCall, must be in the IncomingReceived state.
+ * @param reason the reason for rejecting the call: LinphoneReasonDeclined or LinphoneReasonBusy
+**/
+int linphone_core_decline_call(LinphoneCore *lc, LinphoneCall * call, LinphoneReason reason){
+       SalReason sal_reason=SalReasonUnknown;
+       if (call->state!=LinphoneCallIncomingReceived && call->state!=LinphoneCallIncomingEarlyMedia){
+               ms_error("linphone_core_decline_call(): Cannot decline a call that is in state %s",linphone_call_state_to_string(call->state));
+               return -1;
+       }
+       switch(reason){
+               case LinphoneReasonDeclined:
+                       sal_reason=SalReasonDeclined;
+               break;
+               case LinphoneReasonBusy:
+                       sal_reason=SalReasonBusy;
+               break;
+               default:
+                       ms_error("linphone_core_decline_call(): unsupported reason %s",linphone_reason_to_string(reason));
+                       return -1;
+               break;
+       }
+       sal_call_decline(call->op,sal_reason,NULL);
+       terminate_call(lc,call);
        return 0;
 }
 
@@ -2521,8 +3328,7 @@ bool_t linphone_core_in_call(const LinphoneCore *lc){
  *
  * @ingroup call_control
 **/
-LinphoneCall *linphone_core_get_current_call(const LinphoneCore *lc)
-{
+LinphoneCall *linphone_core_get_current_call(const LinphoneCore *lc){
        return lc->current_call;
 }
 
@@ -2532,7 +3338,14 @@ LinphoneCall *linphone_core_get_current_call(const LinphoneCore *lc)
  *
  * @ingroup call_control
 **/
-int linphone_core_pause_call(LinphoneCore *lc, LinphoneCall *call)
+int linphone_core_pause_call(LinphoneCore *lc, LinphoneCall *call){
+       int err=_linphone_core_pause_call(lc,call);
+       if (err==0)  call->paused_by_app=TRUE;
+       return err;
+}
+
+/* Internal version that does not play tone indication*/
+int _linphone_core_pause_call(LinphoneCore *lc, LinphoneCall *call)
 {
        const char *subject=NULL;
 
@@ -2540,7 +3353,15 @@ int linphone_core_pause_call(LinphoneCore *lc, LinphoneCall *call)
                ms_warning("Cannot pause this call, it is not active.");
                return -1;
        }
-       update_local_media_description(lc,call,&call->localdesc);
+       linphone_call_make_local_media_description(lc,call);
+       if (call->ice_session != NULL) {
+               linphone_core_update_local_media_description_from_ice(call->localdesc, call->ice_session);
+       }
+#ifdef BUILD_UPNP
+       if(call->upnp_session != NULL) {
+               linphone_core_update_local_media_description_from_upnp(call->localdesc, call->upnp_session);
+       }
+#endif //BUILD_UPNP
        if (sal_media_description_has_dir(call->resultdesc,SalStreamSendRecv)){
                sal_media_description_set_dir(call->localdesc,SalStreamSendOnly);
                subject="Call on hold";
@@ -2562,11 +3383,13 @@ int linphone_core_pause_call(LinphoneCore *lc, LinphoneCall *call)
                lc->vtable.display_status(lc,_("Pausing the current call..."));
        if (call->audiostream || call->videostream)
                linphone_call_stop_media_streams (call);
+       call->paused_by_app=FALSE;
        return 0;
 }
 
 /**
  * Pause all currently running calls.
+ * @ingroup call_control
 **/
 int linphone_core_pause_all_calls(LinphoneCore *lc){
        const MSList *elem;
@@ -2574,7 +3397,7 @@ int linphone_core_pause_all_calls(LinphoneCore *lc){
                LinphoneCall *call=(LinphoneCall *)elem->data;
                LinphoneCallState cs=linphone_call_get_state(call);
                if (cs==LinphoneCallStreamsRunning || cs==LinphoneCallPausedByRemote){
-                       linphone_core_pause_call(lc,call);
+                       _linphone_core_pause_call(lc,call);
                }
        }
        return 0;
@@ -2589,7 +3412,11 @@ void linphone_core_preempt_sound_resources(LinphoneCore *lc){
        current_call=linphone_core_get_current_call(lc);
        if(current_call != NULL){
                ms_message("Pausing automatically the current call.");
-               linphone_core_pause_call(lc,current_call);
+               _linphone_core_pause_call(lc,current_call);
+       }
+       if (lc->ringstream){
+               ring_stop(lc->ringstream);
+               lc->ringstream=NULL;
        }
 }
 
@@ -2613,11 +3440,19 @@ int linphone_core_resume_call(LinphoneCore *lc, LinphoneCall *the_call)
                ms_message("Resuming call %p",call);
        }
 
-       // Stop playing music immediately. If remote side is a conference it
-       // prevents the participants to hear it while the 200OK comes back.
-       audio_stream_play(call->audiostream, NULL);
+       /* Stop playing music immediately. If remote side is a conference it
+        prevents the participants to hear it while the 200OK comes back.*/
+       if (call->audiostream) audio_stream_play(call->audiostream, NULL);
 
-       update_local_media_description(lc,the_call,&call->localdesc);
+       linphone_call_make_local_media_description(lc,the_call);
+       if (call->ice_session != NULL) {
+               linphone_core_update_local_media_description_from_ice(call->localdesc, call->ice_session);
+       }
+#ifdef BUILD_UPNP
+       if(call->upnp_session != NULL) {
+               linphone_core_update_local_media_description_from_upnp(call->localdesc, call->upnp_session);
+       }
+#endif //BUILD_UPNP
        sal_call_set_local_media_description(call->op,call->localdesc);
        sal_media_description_set_dir(call->localdesc,SalStreamSendRecv);
        if (call->params.in_conference && !call->current_params.in_conference) subject="Conference";
@@ -2641,6 +3476,8 @@ static int remote_address_compare(LinphoneCall *call, const LinphoneAddress *rad
  * @param lc
  * @param remote_address
  * @return the LinphoneCall of the call if found
+ * 
+ * @ingroup call_control
  */
 LinphoneCall *linphone_core_get_call_by_remote_address(LinphoneCore *lc, const char *remote_address){
        LinphoneAddress *raddr=linphone_address_new(remote_address);
@@ -2669,6 +3506,9 @@ int linphone_core_send_publish(LinphoneCore *lc,
 **/
 void linphone_core_set_inc_timeout(LinphoneCore *lc, int seconds){
        lc->sip_conf.inc_timeout=seconds;
+       if (linphone_core_ready(lc)){
+               lp_config_set_int(lc->config,"sip","inc_timeout",seconds);
+       }
 }
 
 /**
@@ -2681,6 +3521,46 @@ int linphone_core_get_inc_timeout(LinphoneCore *lc){
        return lc->sip_conf.inc_timeout;
 }
 
+/**
+ * Set the in call timeout in seconds.
+ *
+ * @ingroup call_control
+ * After this timeout period, the call is automatically hangup.
+**/
+void linphone_core_set_in_call_timeout(LinphoneCore *lc, int seconds){
+       lc->sip_conf.in_call_timeout=seconds;
+}
+
+/**
+ * Returns the in call timeout
+ *
+ * @ingroup call_control
+ * See linphone_core_set_in_call_timeout() for details.
+**/
+int linphone_core_get_in_call_timeout(LinphoneCore *lc){
+       return lc->sip_conf.in_call_timeout;
+}
+
+/**
+ * Returns the delayed timeout
+ *
+ * @ingroup call_control
+ * See linphone_core_set_delayed_timeout() for details.
+**/
+int linphone_core_get_delayed_timeout(LinphoneCore *lc){
+       return lc->sip_conf.delayed_timeout;
+}
+
+/**
+ * Set the in delayed timeout in seconds.
+ *
+ * @ingroup call_control
+ * After this timeout period, a delayed call (internal call initialisation or resolution) is resumed.
+**/
+void linphone_core_set_delayed_timeout(LinphoneCore *lc, int seconds){
+       lc->sip_conf.delayed_timeout=seconds;
+}
+
 void linphone_core_set_presence_info(LinphoneCore *lc,int minutes_away,
                                                                                                        const char *contact,
                                                                                                        LinphoneOnlineStatus presence_mode)
@@ -2749,6 +3629,40 @@ void linphone_core_set_ring_level(LinphoneCore *lc, int level){
        if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
 }
 
+/**
+ * Allow to control microphone level:  gain in db
+ *
+ * @ingroup media_parameters
+**/
+void linphone_core_set_mic_gain_db (LinphoneCore *lc, float gaindb){
+       float gain=gaindb;
+       LinphoneCall *call=linphone_core_get_current_call (lc);
+       AudioStream *st;
+
+       lc->sound_conf.soft_mic_lev=gaindb;
+
+       if (linphone_core_ready(lc)){
+               lp_config_set_float(lc->config,"sound","mic_gain_db",lc->sound_conf.soft_mic_lev);
+       }
+
+       if (call==NULL || (st=call->audiostream)==NULL){
+               ms_message("linphone_core_set_mic_gain_db(): no active call.");
+               return;
+       }
+       if (st->volrecv){
+               ms_filter_call_method(st->volsend,MS_VOLUME_SET_DB_GAIN,&gain);
+       }else ms_warning("Could not apply gain: gain control wasn't activated.");
+}
+
+/**
+ * Get microphone gain in db.
+ *
+ * @ingroup media_parameters
+**/
+float linphone_core_get_mic_gain_db(LinphoneCore *lc) {
+       return lc->sound_conf.soft_mic_lev;
+}
+
 /**
  * Allow to control play level before entering sound card:  gain in db
  *
@@ -2760,6 +3674,9 @@ void linphone_core_set_playback_gain_db (LinphoneCore *lc, float gaindb){
        AudioStream *st;
 
        lc->sound_conf.soft_play_lev=gaindb;
+       if (linphone_core_ready(lc)){
+               lp_config_set_float(lc->config,"sound","playback_gain_db",lc->sound_conf.soft_play_lev);
+       }
 
        if (call==NULL || (st=call->audiostream)==NULL){
                ms_message("linphone_core_set_playback_gain_db(): no active call.");
@@ -2952,7 +3869,6 @@ const char * linphone_core_get_capture_device(LinphoneCore *lc)
  * @param lc The LinphoneCore object
 **/
 const char**  linphone_core_get_sound_devices(LinphoneCore *lc){
-       build_sound_devices_table(lc);
        return lc->sound_conf.cards;
 }
 
@@ -2966,6 +3882,38 @@ const char**  linphone_core_get_video_devices(const LinphoneCore *lc){
        return lc->video_conf.cams;
 }
 
+/**
+ * Update detection of sound devices.
+ * 
+ * Use this function when the application is notified of USB plug events, so that
+ * list of available hardwares for sound playback and capture is updated.
+ **/
+void linphone_core_reload_sound_devices(LinphoneCore *lc){
+       const char *ringer,*playback,*capture;
+       ringer=linphone_core_get_ringer_device(lc);
+       playback=linphone_core_get_playback_device(lc);
+       capture=linphone_core_get_capture_device(lc);
+       ms_snd_card_manager_reload(ms_snd_card_manager_get());
+       build_sound_devices_table(lc);
+       linphone_core_set_ringer_device(lc,ringer);
+       linphone_core_set_playback_device(lc,playback);
+       linphone_core_set_capture_device(lc,capture);
+}
+
+/**
+ * Update detection of camera devices.
+ * 
+ * Use this function when the application is notified of USB plug events, so that
+ * list of available hardwares for video capture is updated.
+ **/
+void linphone_core_reload_video_devices(LinphoneCore *lc){
+       const char *devid;
+       devid=linphone_core_get_video_device(lc);
+       ms_web_cam_manager_reload(ms_web_cam_manager_get());
+       build_video_devices_table(lc);
+       linphone_core_set_video_device(lc,devid);
+}
+
 char linphone_core_get_sound_source(LinphoneCore *lc)
 {
        return lc->sound_conf.source;
@@ -3023,19 +3971,40 @@ const char *linphone_core_get_ring(const LinphoneCore *lc){
  * @param path
  * @param lc The LinphoneCore object
  *
- * @ingroup media_parameters
+ * @ingroup initializing
 **/
 void linphone_core_set_root_ca(LinphoneCore *lc,const char *path){
        sal_set_root_ca(lc->sal, path);
 }
 
+/**
+ * Gets the path to a file or folder containing the trusted root CAs (PEM format)
+ *
+ * @param lc The LinphoneCore object
+ *
+ * @ingroup initializing
+**/
+const char *linphone_core_get_root_ca(LinphoneCore *lc){
+       return sal_get_root_ca(lc->sal);
+}
+
 /**
  * Specify whether the tls server certificate must be verified when connecting to a SIP/TLS server.
+ * 
+ * @ingroup initializing
 **/
 void linphone_core_verify_server_certificates(LinphoneCore *lc, bool_t yesno){
        sal_verify_server_certificates(lc->sal,yesno);
 }
 
+/**
+ * Specify whether the tls server certificate common name must be verified when connecting to a SIP/TLS server.
+ * @ingroup initializing
+**/
+void linphone_core_verify_server_cn(LinphoneCore *lc, bool_t yesno){
+       sal_verify_server_cn(lc->sal,yesno);
+}
+
 static void notify_end_of_ring(void *ud, MSFilter *f, unsigned int event, void *arg){
        LinphoneCore *lc=(LinphoneCore*)ud;
        lc->preview_finished=1;
@@ -3129,7 +4098,7 @@ void linphone_core_mute_mic(LinphoneCore *lc, bool_t val){
        }
        if (st!=NULL){
                audio_stream_set_mic_gain(st,
-                       (val==TRUE) ? 0 : lp_config_get_float(lc->config,"sound","mic_gain",1));
+                       (val==TRUE) ? 0 : pow(10,lc->sound_conf.soft_mic_lev/10));
                if ( linphone_core_get_rtp_no_xmit_on_audio_mute(lc) ){
                        audio_stream_mute_rtp(st,val);
                }
@@ -3213,12 +4182,39 @@ void linphone_core_set_stun_server(LinphoneCore *lc, const char *server){
        if (server)
                lc->net_conf.stun_server=ms_strdup(server);
        else lc->net_conf.stun_server=NULL;
+       if (linphone_core_ready(lc))
+               lp_config_set_string(lc->config,"net","stun_server",lc->net_conf.stun_server);
 }
 
 const char * linphone_core_get_stun_server(const LinphoneCore *lc){
        return lc->net_conf.stun_server;
 }
 
+bool_t linphone_core_upnp_available(const LinphoneCore *lc){
+#ifdef BUILD_UPNP
+       return TRUE;
+#else
+       return FALSE;
+#endif //BUILD_UPNP
+}
+
+LinphoneUpnpState linphone_core_get_upnp_state(const LinphoneCore *lc){
+#ifdef BUILD_UPNP
+       return linphone_upnp_context_get_state(lc->upnp);
+#else
+       return LinphoneUpnpStateNotAvailable;
+#endif //BUILD_UPNP
+}
+
+const char * linphone_core_get_upnp_external_ipaddress(const LinphoneCore *lc){
+#ifdef BUILD_UPNP
+       return linphone_upnp_context_get_external_ipaddress(lc->upnp);
+#else
+       return NULL;
+#endif //BUILD_UPNP
+}
+
+
 const char * linphone_core_get_relay_addr(const LinphoneCore *lc){
        return lc->net_conf.relay;
 }
@@ -3275,8 +4271,29 @@ const char *linphone_core_get_nat_address_resolved(LinphoneCore *lc)
 }
 
 void linphone_core_set_firewall_policy(LinphoneCore *lc, LinphoneFirewallPolicy pol){
+#ifndef BUILD_UPNP
+       if(pol == LinphonePolicyUseUpnp) {
+               ms_warning("UPNP is not available, reset firewall policy to no firewall");
+               pol = LinphonePolicyNoFirewall;
+       }
+#endif //BUILD_UPNP
        lc->net_conf.firewall_policy=pol;
+#ifdef BUILD_UPNP
+       if(pol == LinphonePolicyUseUpnp) {
+               if(lc->upnp == NULL) {
+                       lc->upnp = linphone_upnp_context_new(lc);
+               }
+       } else {
+               if(lc->upnp != NULL) {
+                       linphone_upnp_context_destroy(lc->upnp);
+                       lc->upnp = NULL;
+               }
+       }
+       linphone_core_enable_keep_alive(lc, (lc->sip_conf.keepalive_period > 0));
+#endif //BUILD_UPNP
        if (lc->sip_conf.contact) update_primary_contact(lc);
+       if (linphone_core_ready(lc))
+               lp_config_set_int(lc->config,"net","firewall_policy",pol);
 }
 
 LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc){
@@ -3289,7 +4306,6 @@ LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc)
  * @ingroup call_logs
 **/
 const MSList * linphone_core_get_call_logs(LinphoneCore *lc){
-       lc->missed_calls=0;
        return lc->call_logs;
 }
 
@@ -3305,6 +4321,33 @@ void linphone_core_clear_call_logs(LinphoneCore *lc){
        call_logs_write_to_config_file(lc);
 }
 
+/**
+ * Returns number of missed calls.
+ * Once checked, this counter can be reset with linphone_core_reset_missed_calls_count().
+**/
+int linphone_core_get_missed_calls_count(LinphoneCore *lc) {
+       return lc->missed_calls;
+}
+
+/**
+ * Resets the counter of missed calls.
+**/
+void linphone_core_reset_missed_calls_count(LinphoneCore *lc) {
+       lc->missed_calls=0;
+}
+
+/**
+ * Remove a specific call log from call history list.
+ * This function destroys the call log object. It must not be accessed anymore by the application after calling this function.
+ * @param lc the linphone core object
+ * @param a LinphoneCallLog object.
+**/
+void linphone_core_remove_call_log(LinphoneCore *lc, LinphoneCallLog *cl){
+       lc->call_logs = ms_list_remove(lc->call_logs, cl);
+       call_logs_write_to_config_file(lc);
+       linphone_call_log_destroy(cl);
+}
+
 static void toggle_video_preview(LinphoneCore *lc, bool_t val){
 #ifdef VIDEO_ENABLED
        if (val){
@@ -3346,7 +4389,10 @@ void linphone_core_enable_video(LinphoneCore *lc, bool_t vcap_enabled, bool_t di
 #endif
        lc->video_conf.capture=vcap_enabled;
        lc->video_conf.display=display_enabled;
-
+       if (linphone_core_ready(lc)){
+               lp_config_set_int(lc->config,"video","display",lc->video_conf.display);
+               lp_config_set_int(lc->config,"video","capture",lc->video_conf.capture);
+       }
        /* need to re-apply network bandwidth settings*/
        linphone_core_set_download_bandwidth(lc,
                linphone_core_get_download_bandwidth(lc));
@@ -3354,6 +4400,14 @@ void linphone_core_enable_video(LinphoneCore *lc, bool_t vcap_enabled, bool_t di
                linphone_core_get_upload_bandwidth(lc));
 }
 
+bool_t linphone_core_video_supported(LinphoneCore *lc){
+#ifdef VIDEO_ENABLED
+       return TRUE;
+#else
+       return FALSE;
+#endif
+}
+
 /**
  * Returns TRUE if video is enabled, FALSE otherwise.
  * @ingroup media_parameters
@@ -3362,6 +4416,30 @@ bool_t linphone_core_video_enabled(LinphoneCore *lc){
        return (lc->video_conf.display || lc->video_conf.capture);
 }
 
+/**
+ * Sets the default policy for video.
+ * This policy defines whether:
+ * - video shall be initiated by default for outgoing calls
+ * - video shall be accepter by default for incoming calls
+ * @ingroup media_parameters
+**/
+void linphone_core_set_video_policy(LinphoneCore *lc, const LinphoneVideoPolicy *policy){
+       lc->video_policy=*policy;
+       if (linphone_core_ready(lc)){
+               lp_config_set_int(lc->config,"video","automatically_initiate",policy->automatically_initiate);
+               lp_config_set_int(lc->config,"video","automatically_accept",policy->automatically_accept);
+       }
+}
+
+/**
+ * Get the default policy for video.
+ * See linphone_core_set_video_policy() for more details.
+ * @ingroup media_parameters
+**/
+const LinphoneVideoPolicy *linphone_core_get_video_policy(LinphoneCore *lc){
+       return &lc->video_policy;
+}
+
 /**
  * Controls video preview enablement.
  *
@@ -3371,6 +4449,8 @@ bool_t linphone_core_video_enabled(LinphoneCore *lc){
 **/
 void linphone_core_enable_video_preview(LinphoneCore *lc, bool_t val){
        lc->video_conf.show_local=val;
+       if (linphone_core_ready(lc))
+               lp_config_set_int(lc->config,"video","show_local",val);
 }
 
 /**
@@ -3396,6 +4476,9 @@ void linphone_core_enable_self_view(LinphoneCore *lc, bool_t val){
        if (call && call->videostream){
                video_stream_enable_self_view(call->videostream,val);
        }
+       if (linphone_core_ready(lc)){
+               lp_config_set_int(lc->config,"video","self_view",val);
+       }
 #endif
 }
 
@@ -3423,7 +4506,7 @@ int linphone_core_set_video_device(LinphoneCore *lc, const char *id){
        if (id!=NULL){
                lc->video_conf.device=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),id);
                if (lc->video_conf.device==NULL){
-                       ms_warning("Could not found video device %s",id);
+                       ms_warning("Could not find video device %s",id);
                }
        }
        if (lc->video_conf.device==NULL)
@@ -3489,6 +4572,16 @@ int linphone_core_set_static_picture(LinphoneCore *lc, const char *path) {
        return 0;
 }
 
+const char *linphone_core_get_static_picture(LinphoneCore *lc) {
+       const char *path=NULL;
+#ifdef VIDEO_ENABLED
+       path=ms_static_image_get_default_image();       
+#else
+       ms_warning("Video support not compiled.");
+#endif
+       return path;
+}
+
 int linphone_core_set_static_picture_fps(LinphoneCore *lc, float fps) {
 #ifdef VIDEO_ENABLED
        VideoStream *vs = NULL;
@@ -3589,6 +4682,8 @@ void linphone_core_set_native_preview_window_id(LinphoneCore *lc, unsigned long
        LinphoneCall *call=linphone_core_get_current_call(lc);
        if (call!=NULL && call->videostream){
                video_stream_set_native_preview_window_id(call->videostream,id);
+       }else if (lc->previewstream){
+               video_preview_set_native_window_id(lc->previewstream,id);
        }
 #endif
 }
@@ -3632,7 +4727,7 @@ int linphone_core_get_device_rotation(LinphoneCore *lc ) {
  *
 **/
 void linphone_core_set_device_rotation(LinphoneCore *lc, int rotation) {
-ms_message("%s : rotation=%d\n", __FUNCTION__, rotation);
+       ms_message("%s : rotation=%d\n", __FUNCTION__, rotation);
        lc->device_rotation = rotation;
 #ifdef VIDEO_ENABLED
        LinphoneCall *call=linphone_core_get_current_call(lc);
@@ -3759,7 +4854,7 @@ void linphone_core_set_play_file(LinphoneCore *lc, const char *file){
        }
        if (file!=NULL) {
                lc->play_file=ms_strdup(file);
-               if (call && call->audiostream && call->audiostream->ticker)
+               if (call && call->audiostream && call->audiostream->ms.ticker)
                        audio_stream_play(call->audiostream,file);
        }
 }
@@ -3769,7 +4864,8 @@ void linphone_core_set_play_file(LinphoneCore *lc, const char *file){
  * Sets a wav file where incoming stream is to be recorded,
  * when files are used instead of soundcards (see linphone_core_use_files()).
  *
- * The file must be a 16 bit linear wav file.
+ * This feature is different from call recording (linphone_call_params_set_record_file())
+ * The file will be a 16 bit linear wav file.
 **/
 void linphone_core_set_record_file(LinphoneCore *lc, const char *file){
        LinphoneCall *call=linphone_core_get_current_call(lc);
@@ -3797,7 +4893,7 @@ static MSFilter *get_dtmf_gen(LinphoneCore *lc){
                return stream->dtmfgen;
        }
        if (lc->ringstream==NULL){
-               float amp=0.1;
+               float amp=lp_config_get_float(lc->config,"sound","dtmf_player_amp",0.1);
                MSSndCard *ringcard=lc->sound_conf.lsd_card ?lc->sound_conf.lsd_card : lc->sound_conf.ring_sndcard;
                if (ringcard == NULL)
                        return NULL;
@@ -3831,23 +4927,44 @@ void linphone_core_play_dtmf(LinphoneCore *lc, char dtmf, int duration_ms){
        else ms_filter_call_method(f, MS_DTMF_GEN_START, &dtmf);
 }
 
-/**
- * @ingroup media_parameters
- * Plays a repeated tone to the local user until next further call to #linphone_core_stop_dtmf()
- * @param lc #LinphoneCore
-**/
-void linphone_core_play_tone(LinphoneCore *lc){
-       MSFilter *f=get_dtmf_gen(lc);
-       MSDtmfGenCustomTone def;
-       if (f==NULL){
-               ms_error("No dtmf generator at this time !");
-               return;
+void linphone_core_play_named_tone(LinphoneCore *lc, LinphoneToneID toneid){
+       if (linphone_core_tone_indications_enabled(lc)){
+               MSFilter *f=get_dtmf_gen(lc);
+               MSDtmfGenCustomTone def;
+               if (f==NULL){
+                       ms_error("No dtmf generator at this time !");
+                       return;
+               }
+               memset(&def,0,sizeof(def));
+               def.amplitude=1;
+               /*these are french tones, excepted the failed one, which is USA congestion tone (does not exist in France)*/
+               switch(toneid){
+                       case LinphoneToneCallOnHold:
+                       case LinphoneToneCallWaiting:
+                               def.duration=300;
+                               def.frequencies[0]=440;
+                               def.interval=2000;
+                       break;
+                       case LinphoneToneBusy:
+                               def.duration=500;
+                               def.frequencies[0]=440;
+                               def.interval=500;
+                               def.repeat_count=3;
+                       break;
+                       case LinphoneToneCallFailed:
+                               def.duration=250;
+                               def.frequencies[0]=480;
+                               def.frequencies[0]=620;
+                               def.interval=250;
+                               def.repeat_count=3;
+                               
+                       break;
+                       default:
+                               ms_warning("Unhandled tone id.");
+               }
+               if (def.duration>0)
+                       ms_filter_call_method(f, MS_DTMF_GEN_PLAY_CUSTOM,&def);
        }
-       def.duration=300;
-       def.frequency=500;
-       def.amplitude=1;
-       def.interval=2000;
-       ms_filter_call_method(f, MS_DTMF_GEN_PLAY_CUSTOM,&def);
 }
 
 /**
@@ -3872,10 +4989,27 @@ void *linphone_core_get_user_data(LinphoneCore *lc){
        return lc->data;
 }
 
+
+/**
+ * Associate a user pointer to the linphone core.
+ *
+ * @ingroup initializing
+**/
+void linphone_core_set_user_data(LinphoneCore *lc, void *userdata){
+       lc->data=userdata;
+}
+
 int linphone_core_get_mtu(const LinphoneCore *lc){
        return lc->net_conf.mtu;
 }
 
+/**
+ * Sets the maximum transmission unit size in bytes.
+ * This information is useful for sending RTP packets.
+ * Default value is 1500.
+ * 
+ * @ingroup media_parameters
+**/
 void linphone_core_set_mtu(LinphoneCore *lc, int mtu){
        lc->net_conf.mtu=mtu;
        if (mtu>0){
@@ -3948,7 +5082,6 @@ void net_config_uninit(LinphoneCore *lc)
        net_config_t *config=&lc->net_conf;
 
        if (config->stun_server!=NULL){
-               lp_config_set_string(lc->config,"net","stun_server",config->stun_server);
                ms_free(lc->net_conf.stun_server);
        }
        if (config->nat_address!=NULL){
@@ -3958,7 +5091,6 @@ void net_config_uninit(LinphoneCore *lc)
        if (lc->net_conf.nat_address_ip !=NULL){
                ms_free(lc->net_conf.nat_address_ip);
        }
-       lp_config_set_int(lc->config,"net","firewall_policy",config->firewall_policy);
        lp_config_set_int(lc->config,"net","mtu",config->mtu);
 }
 
@@ -3972,11 +5104,11 @@ void sip_config_uninit(LinphoneCore *lc)
        lp_config_set_int(lc->config,"sip","guess_hostname",config->guess_hostname);
        lp_config_set_string(lc->config,"sip","contact",config->contact);
        lp_config_set_int(lc->config,"sip","inc_timeout",config->inc_timeout);
-       lp_config_set_int(lc->config,"sip","use_info",config->use_info);
-       lp_config_set_int(lc->config,"sip","use_rfc2833",config->use_rfc2833);
+       lp_config_set_int(lc->config,"sip","in_call_timeout",config->in_call_timeout);
+       lp_config_set_int(lc->config,"sip","delayed_timeout",config->delayed_timeout);
        lp_config_set_int(lc->config,"sip","use_ipv6",config->ipv6_enabled);
        lp_config_set_int(lc->config,"sip","register_only_when_network_is_up",config->register_only_when_network_is_up);
-
+       lp_config_set_int(lc->config,"sip","register_only_when_upnp_is_ok",config->register_only_when_upnp_is_ok);
 
        
 
@@ -4017,68 +5149,84 @@ void sip_config_uninit(LinphoneCore *lc)
 void rtp_config_uninit(LinphoneCore *lc)
 {
        rtp_config_t *config=&lc->rtp_conf;
-       lp_config_set_int(lc->config,"rtp","audio_rtp_port",config->audio_rtp_port);
-       lp_config_set_int(lc->config,"rtp","video_rtp_port",config->video_rtp_port);
+       if (config->audio_rtp_min_port == config->audio_rtp_max_port) {
+               lp_config_set_int(lc->config, "rtp", "audio_rtp_port", config->audio_rtp_min_port);
+       } else {
+               lp_config_set_range(lc->config, "rtp", "audio_rtp_port", config->audio_rtp_min_port, config->audio_rtp_max_port);
+       }
+       if (config->video_rtp_min_port == config->video_rtp_max_port) {
+               lp_config_set_int(lc->config, "rtp", "video_rtp_port", config->video_rtp_min_port);
+       } else {
+               lp_config_set_range(lc->config, "rtp", "video_rtp_port", config->video_rtp_min_port, config->video_rtp_max_port);
+       }
        lp_config_set_int(lc->config,"rtp","audio_jitt_comp",config->audio_jitt_comp);
        lp_config_set_int(lc->config,"rtp","video_jitt_comp",config->video_jitt_comp);
        lp_config_set_int(lc->config,"rtp","nortp_timeout",config->nortp_timeout);
+       lp_config_set_int(lc->config,"rtp","audio_adaptive_jitt_comp_enabled",config->audio_adaptive_jitt_comp_enabled);
+       lp_config_set_int(lc->config,"rtp","video_adaptive_jitt_comp_enabled",config->video_adaptive_jitt_comp_enabled);
 }
 
-void sound_config_uninit(LinphoneCore *lc)
+static void sound_config_uninit(LinphoneCore *lc)
 {
        sound_config_t *config=&lc->sound_conf;
        ms_free(config->cards);
 
        lp_config_set_string(lc->config,"sound","remote_ring",config->remote_ring);
+       lp_config_set_float(lc->config,"sound","playback_gain_db",config->soft_play_lev);
+       lp_config_set_float(lc->config,"sound","mic_gain_db",config->soft_mic_lev);
 
        if (config->local_ring) ms_free(config->local_ring);
        if (config->remote_ring) ms_free(config->remote_ring);
        ms_snd_card_manager_destroy();
 }
 
-void video_config_uninit(LinphoneCore *lc)
+static void video_config_uninit(LinphoneCore *lc)
 {
        lp_config_set_string(lc->config,"video","size",video_size_get_name(linphone_core_get_preferred_video_size(lc)));
        lp_config_set_int(lc->config,"video","display",lc->video_conf.display);
        lp_config_set_int(lc->config,"video","capture",lc->video_conf.capture);
-       lp_config_set_int(lc->config,"video","show_local",linphone_core_video_preview_enabled(lc));
-       lp_config_set_int(lc->config,"video","self_view",linphone_core_self_view_enabled(lc));
        if (lc->video_conf.cams)
                ms_free(lc->video_conf.cams);
 }
 
-void codecs_config_uninit(LinphoneCore *lc)
-{
-       PayloadType *pt;
-       codecs_config_t *config=&lc->codecs_conf;
-       MSList *node;
-       char key[50];
-       int index;
-       index=0;
-       for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
-               pt=(PayloadType*)(node->data);
+void _linphone_core_codec_config_write(LinphoneCore *lc){
+       if (linphone_core_ready(lc)){
+               PayloadType *pt;
+               codecs_config_t *config=&lc->codecs_conf;
+               MSList *node;
+               char key[50];
+               int index;
+               index=0;
+               for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
+                       pt=(PayloadType*)(node->data);
+                       sprintf(key,"audio_codec_%i",index);
+                       lp_config_set_string(lc->config,key,"mime",pt->mime_type);
+                       lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
+                       lp_config_set_int(lc->config,key,"channels",pt->channels);
+                       lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
+                       index++;
+               }
                sprintf(key,"audio_codec_%i",index);
-               lp_config_set_string(lc->config,key,"mime",pt->mime_type);
-               lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
-               lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
-               index++;
-       }
-       sprintf(key,"audio_codec_%i",index);
-       lp_config_clean_section (lc->config,key);
-
-       index=0;
-       for(node=config->video_codecs;node!=NULL;node=ms_list_next(node)){
-               pt=(PayloadType*)(node->data);
+               lp_config_clean_section (lc->config,key);
+
+               index=0;
+               for(node=config->video_codecs;node!=NULL;node=ms_list_next(node)){
+                       pt=(PayloadType*)(node->data);
+                       sprintf(key,"video_codec_%i",index);
+                       lp_config_set_string(lc->config,key,"mime",pt->mime_type);
+                       lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
+                       lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
+                       lp_config_set_string(lc->config,key,"recv_fmtp",pt->recv_fmtp);
+                       index++;
+               }
                sprintf(key,"video_codec_%i",index);
-               lp_config_set_string(lc->config,key,"mime",pt->mime_type);
-               lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
-               lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
-               lp_config_set_string(lc->config,key,"recv_fmtp",pt->recv_fmtp);
-               index++;
+               lp_config_clean_section (lc->config,key);
        }
-       sprintf(key,"video_codec_%i",index);
-       lp_config_clean_section (lc->config,key);
+}
 
+static void codecs_config_uninit(LinphoneCore *lc)
+{
+       _linphone_core_codec_config_write(lc);
        ms_list_free(lc->codecs_conf.audio_codecs);
        ms_list_free(lc->codecs_conf.video_codecs);
 }
@@ -4118,6 +5266,13 @@ static void linphone_core_uninit(LinphoneCore *lc)
                usleep(50000);
 #endif
        }
+#ifdef BUILD_UPNP
+       if(lc->upnp != NULL) {
+               linphone_upnp_context_destroy(lc->upnp);
+               lc->upnp = NULL;
+       }
+#endif //BUILD_UPNP
+
        if (lc->friends)
                ms_list_for_each(lc->friends,(void (*)(void *))linphone_friend_close_subscriptions);
        linphone_core_set_state(lc,LinphoneGlobalShutdown,"Shutting down");
@@ -4145,8 +5300,24 @@ static void linphone_core_uninit(LinphoneCore *lc)
 
        ms_list_for_each(lc->call_logs,(void (*)(void*))linphone_call_log_destroy);
        lc->call_logs=ms_list_free(lc->call_logs);
+       
+       ms_list_for_each(lc->last_recv_msg_ids,ms_free);
+       lc->last_recv_msg_ids=ms_list_free(lc->last_recv_msg_ids);
+       
+       // Free struct variable
+       if(lc->zrtp_secrets_cache != NULL) {
+               ms_free(lc->zrtp_secrets_cache);
+       }
+       if(lc->play_file!=NULL){
+               ms_free(lc->play_file);
+       }
+       if(lc->rec_file!=NULL){
+               ms_free(lc->rec_file);
+       }
 
        linphone_core_free_payload_types(lc);
+       
+       linphone_core_message_storage_close(lc);
        ortp_exit();
        linphone_core_set_state(lc,LinphoneGlobalOff,"Off");
 #ifdef TUNNEL_ENABLED
@@ -4170,12 +5341,22 @@ static void set_network_reachable(LinphoneCore* lc,bool_t isReachable, time_t cu
        }
        lc->netup_time=curtime;
        lc->network_reachable=isReachable;
+       
        if(!isReachable) {
-               sal_unlisten_ports (lc->sal);
+               sal_reset_transports(lc->sal);
+       }
+#ifdef BUILD_UPNP
+       if(lc->upnp == NULL) {
+               if(isReachable && lc->net_conf.firewall_policy == LinphonePolicyUseUpnp) {
+                       lc->upnp = linphone_upnp_context_new(lc);       
+               }
        } else {
-               apply_transports(lc);
+               if(!isReachable && lc->net_conf.firewall_policy == LinphonePolicyUseUpnp) {
+                       linphone_upnp_context_destroy(lc->upnp);
+                       lc->upnp = NULL;
+               }
        }
-
+#endif 
 }
 
 void linphone_core_refresh_registers(LinphoneCore* lc) {
@@ -4187,7 +5368,7 @@ void linphone_core_refresh_registers(LinphoneCore* lc) {
        elem=linphone_core_get_proxy_config_list(lc);
        for(;elem!=NULL;elem=elem->next){
                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
-               if (linphone_proxy_config_register_enabled(cfg) ) {
+               if (linphone_proxy_config_register_enabled(cfg) && linphone_proxy_config_get_expires(cfg)>0) {
                        linphone_proxy_config_refresh_register(cfg);
                }
        }
@@ -4197,7 +5378,7 @@ void __linphone_core_invalidate_registers(LinphoneCore* lc){
        const MSList *elem=linphone_core_get_proxy_config_list(lc);
        for(;elem!=NULL;elem=elem->next){
                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
-               if (linphone_proxy_config_register_enabled(cfg) ) {
+               if (linphone_proxy_config_register_enabled(cfg)) {
                        linphone_proxy_config_edit(cfg);
                        linphone_proxy_config_done(cfg);
                }
@@ -4213,7 +5394,7 @@ void linphone_core_set_network_reachable(LinphoneCore* lc,bool_t isReachable) {
        set_network_reachable(lc,isReachable, ms_time(NULL));
 }
 
-bool_t linphone_core_is_network_reachabled(LinphoneCore* lc) {
+bool_t linphone_core_is_network_reachable(LinphoneCore* lc) {
        return lc->network_reachable;
 }
 ortp_socket_t linphone_core_get_sip_socket(LinphoneCore *lc){
@@ -4299,29 +5480,26 @@ const char *linphone_core_get_remote_ringback_tone(const LinphoneCore *lc){
        return lc->sound_conf.ringback_tone;
 }
 
-static PayloadType* find_payload_type_from_list(const char* type, int rate,const MSList* from) {
+static PayloadType* find_payload_type_from_list(const char* type, int rate, int channels, const MSList* from) {
        const MSList *elem;
        for(elem=from;elem!=NULL;elem=elem->next){
                PayloadType *pt=(PayloadType*)elem->data;
-               if ((strcmp((char*)type, payload_type_get_mime(pt)) == 0) && (rate == -1 || rate==pt->clock_rate)) {
+               if ((strcasecmp((char*)type, payload_type_get_mime(pt)) == 0)
+                       && (rate == LINPHONE_FIND_PAYLOAD_IGNORE_RATE || rate==pt->clock_rate)
+                       && (channels == LINPHONE_FIND_PAYLOAD_IGNORE_CHANNELS || channels==pt->channels)) {
                        return pt;
                }
        }
        return NULL;
 }
 
-/**
- * Get payload type  from mime type and clock rate
- * @ingroup media_parameters
- * This function searches in audio and video codecs for the given payload type name and clockrate.
- * Returns NULL if not found.
- */
-PayloadType* linphone_core_find_payload_type(LinphoneCore* lc, const char* type, int rate) {
-       PayloadType* result = find_payload_type_from_list(type, rate, linphone_core_get_audio_codecs(lc));
+
+PayloadType* linphone_core_find_payload_type(LinphoneCore* lc, const char* type, int rate, int channels) {
+       PayloadType* result = find_payload_type_from_list(type, rate, channels, linphone_core_get_audio_codecs(lc));
        if (result)  {
                return result;
        } else {
-               result = find_payload_type_from_list(type, rate, linphone_core_get_video_codecs(lc));
+               result = find_payload_type_from_list(type, rate, 0, linphone_core_get_video_codecs(lc));
                if (result) {
                        return result;
                }
@@ -4370,6 +5548,10 @@ const char *linphone_reason_to_string(LinphoneReason err){
                        return "Call declined";
                case LinphoneReasonNotFound:
                        return "User not found";
+               case LinphoneReasonNotAnswered:
+                       return "Not answered";
+               case LinphoneReasonBusy:
+                       return "Busy";
        }
        return "unknown error";
 }
@@ -4381,7 +5563,13 @@ const char *linphone_error_to_string(LinphoneReason err){
  * Enables signaling keep alive
  */
 void linphone_core_enable_keep_alive(LinphoneCore* lc,bool_t enable) {
+#ifdef BUILD_UPNP
+       if (linphone_core_get_firewall_policy(lc)==LinphonePolicyUseUpnp) {
+               enable = FALSE;
+       }
+#endif //BUILD_UPNP
        if (enable > 0) {
+               sal_use_tcp_tls_keepalive(lc->sal,lc->sip_conf.tcp_tls_keepalive);
                sal_set_keepalive_period(lc->sal,lc->sip_conf.keepalive_period);
        } else {
                sal_set_keepalive_period(lc->sal,0);
@@ -4400,8 +5588,10 @@ void linphone_core_start_dtmf_stream(LinphoneCore* lc) {
 }
 
 void linphone_core_stop_dtmf_stream(LinphoneCore* lc) {
-       if (lc->ringstream) ring_stop(lc->ringstream);
-       lc->ringstream=NULL;
+       if (lc->ringstream && lc->dmfs_playing_start_time!=0) {
+               ring_stop(lc->ringstream);
+               lc->ringstream=NULL;
+       }
 }
 
 int linphone_core_get_max_calls(LinphoneCore *lc) {
@@ -4446,7 +5636,7 @@ void linphone_core_remove_iterate_hook(LinphoneCore *lc, LinphoneCoreIterateHook
        for(elem=lc->hooks;elem!=NULL;elem=elem->next){
                Hook *h=(Hook*)elem->data;
                if (h->fun==hook && h->data==hook_data){
-                       ms_list_remove_link(lc->hooks,elem);
+                       lc->hooks = ms_list_remove_link(lc->hooks,elem);
                        ms_free(h);
                        return;
                }
@@ -4461,6 +5651,10 @@ void linphone_core_set_zrtp_secrets_file(LinphoneCore *lc, const char* file){
        lc->zrtp_secrets_cache=file ? ms_strdup(file) : NULL;
 }
 
+const char *linphone_core_get_zrtp_secrets_file(LinphoneCore *lc){
+       return lc->zrtp_secrets_cache;
+}
+
 const LinphoneCall* linphone_core_find_call_from_uri(LinphoneCore *lc, const char *uri) {
        if (uri == NULL) return NULL;
        MSList *calls=lc->calls;
@@ -4499,7 +5693,7 @@ bool_t linphone_core_sound_resources_locked(LinphoneCore *lc){
                        case LinphoneCallConnected:
                        case LinphoneCallRefered:
                        case LinphoneCallIncomingEarlyMedia:
-                       case LinphoneCallUpdated:
+                       case LinphoneCallUpdating:
                                return TRUE;
                        default:
                                break;
@@ -4569,8 +5763,109 @@ void linphone_core_set_media_encryption_mandatory(LinphoneCore *lc, bool_t m) {
 }
 
 void linphone_core_init_default_params(LinphoneCore*lc, LinphoneCallParams *params) {
-       params->has_video=linphone_core_video_enabled(lc);
-       params->media_encryption=linphone_core_get_media_encryption(lc);        
+       params->has_video=linphone_core_video_enabled(lc) && lc->video_policy.automatically_initiate;
+       params->media_encryption=linphone_core_get_media_encryption(lc);
        params->in_conference=FALSE;
 }
 
+void linphone_core_set_device_identifier(LinphoneCore *lc,const char* device_id) {
+       if (lc->device_id) ms_free(lc->device_id);
+       lc->device_id=ms_strdup(device_id);
+}
+const char*  linphone_core_get_device_identifier(const LinphoneCore *lc) {
+       return lc->device_id;
+}
+
+/**
+ * Set the DSCP field for SIP signaling channel.
+ * 
+ * @ingroup network_parameters
+ * * The DSCP defines the quality of service in IP packets.
+ * 
+**/
+void linphone_core_set_sip_dscp(LinphoneCore *lc, int dscp){
+       sal_set_dscp(lc->sal,dscp);
+       if (linphone_core_ready(lc)){
+               lp_config_set_int_hex(lc->config,"sip","dscp",dscp);
+               apply_transports(lc);
+       }
+}
+
+/**
+ * Get the DSCP field for SIP signaling channel.
+ * 
+ * @ingroup network_parameters
+ * * The DSCP defines the quality of service in IP packets.
+ * 
+**/
+int linphone_core_get_sip_dscp(const LinphoneCore *lc){
+       return lp_config_get_int(lc->config,"sip","dscp",0x1a);
+}
+
+/**
+ * Set the DSCP field for outgoing audio streams.
+ *
+ * @ingroup network_parameters
+ * The DSCP defines the quality of service in IP packets.
+ * 
+**/
+void linphone_core_set_audio_dscp(LinphoneCore *lc, int dscp){
+       if (linphone_core_ready(lc))
+               lp_config_set_int_hex(lc->config,"rtp","audio_dscp",dscp);
+}
+
+/**
+ * Get the DSCP field for outgoing audio streams.
+ *
+ * @ingroup network_parameters
+ * The DSCP defines the quality of service in IP packets.
+ * 
+**/
+int linphone_core_get_audio_dscp(const LinphoneCore *lc){
+       return lp_config_get_int(lc->config,"rtp","audio_dscp",0x2e);
+}
+
+/**
+ * Set the DSCP field for outgoing video streams.
+ *
+ * @ingroup network_parameters
+ * The DSCP defines the quality of service in IP packets.
+ * 
+**/
+void linphone_core_set_video_dscp(LinphoneCore *lc, int dscp){
+       if (linphone_core_ready(lc))
+               lp_config_set_int_hex(lc->config,"rtp","video_dscp",dscp);
+       
+}
+
+/**
+ * Get the DSCP field for outgoing video streams.
+ *
+ * @ingroup network_parameters
+ * The DSCP defines the quality of service in IP packets.
+ * 
+**/
+int linphone_core_get_video_dscp(const LinphoneCore *lc){
+       return lp_config_get_int(lc->config,"rtp","video_dscp",0x2e);
+}
+
+
+/**
+ * Sets the database filename where chat messages will be stored.
+ * If the file does not exist, it will be created.
+ * @ingroup initializing
+ * @param lc the linphone core
+ * @param path filesystem path
+**/
+void linphone_core_set_chat_database_path(LinphoneCore *lc, const char *path){
+       if (lc->chat_db_file){
+               ms_free(lc->chat_db_file);
+               lc->chat_db_file=NULL;
+       }
+       if (path) {
+               lc->chat_db_file=ms_strdup(path);
+               linphone_core_message_storage_init(lc);
+       }
+}
+
+