]> sjero.net Git - linphone/blobdiff - coreapi/linphonecall.c
fix bug when choosing SDP connection address
[linphone] / coreapi / linphonecall.c
index 410358e94e59b72cbbb8bf76eda864d3082c1264..db3190bbb3e851709690144472689ea3cc3e6ad5 100644 (file)
@@ -26,7 +26,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "lpconfig.h"
 #include "private.h"
 #include <ortp/event.h>
-
+#include <ortp/b64.h>
+#include <math.h>
 
 #include "mediastreamer2/mediastream.h"
 #include "mediastreamer2/msvolume.h"
@@ -34,6 +35,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "mediastreamer2/msfileplayer.h"
 #include "mediastreamer2/msjpegwriter.h"
 #include "mediastreamer2/mseventqueue.h"
+#include "mediastreamer2/mssndcard.h"
 
 #ifdef VIDEO_ENABLED
 static MSWebCam *get_nowebcam_device(){
@@ -41,20 +43,29 @@ static MSWebCam *get_nowebcam_device(){
 }
 #endif
 
-LinphoneCore *linphone_call_get_core(const LinphoneCall *call){
-       return call->core;
+static bool_t generate_b64_crypto_key(int key_length, char* key_out) {
+       int b64_size;
+       uint8_t* tmp = (uint8_t*) malloc(key_length);                   
+       if (ortp_crypto_get_random(tmp, key_length)!=0) {
+               ms_error("Failed to generate random key");
+               free(tmp);
+               return FALSE;
+       }
+       
+       b64_size = b64_encode((const char*)tmp, key_length, NULL, 0);
+       if (b64_size == 0) {
+               ms_error("Failed to b64 encode key");
+               free(tmp);
+               return FALSE;
+       }
+       key_out[b64_size] = '\0';
+       b64_encode((const char*)tmp, key_length, key_out, 40);
+       free(tmp);
+       return TRUE;
 }
 
-static const char* get_hexa_zrtp_identifier(LinphoneCore *lc){
-       const char *confZid=lp_config_get_string(lc->config,"rtp","zid",NULL);
-       if (confZid != NULL) {
-               return confZid;
-       } else {
-        char zidstr[128];
-        snprintf(zidstr,sizeof(zidstr),"%x-%x-%x",rand(),rand(),rand());
-               lp_config_set_string(lc->config,"rtp","zid",zidstr);
-               return lp_config_get_string(lc->config,"rtp","zid",NULL);
-       }
+LinphoneCore *linphone_call_get_core(const LinphoneCall *call){
+       return call->core;
 }
 
 const char* linphone_call_get_authentication_token(LinphoneCall *call){
@@ -64,7 +75,8 @@ const char* linphone_call_get_authentication_token(LinphoneCall *call){
 bool_t linphone_call_get_authentication_token_verified(LinphoneCall *call){
        return call->auth_token_verified;
 }
-bool_t linphone_call_are_all_streams_encrypted(LinphoneCall *call) {
+
+static bool_t linphone_call_are_all_streams_encrypted(LinphoneCall *call) {
        // Check ZRTP encryption in audiostream
        if (!call->audiostream_encrypted) {
                return FALSE;
@@ -82,14 +94,17 @@ bool_t linphone_call_are_all_streams_encrypted(LinphoneCall *call) {
 }
 
 void propagate_encryption_changed(LinphoneCall *call){
-       if (call->core->vtable.call_encryption_changed == NULL) return;
-
+       LinphoneCore *lc=call->core;
        if (!linphone_call_are_all_streams_encrypted(call)) {
                ms_message("Some streams are not encrypted");
-               call->core->vtable.call_encryption_changed(call->core, call, FALSE, call->auth_token);
+               call->current_params.media_encryption=LinphoneMediaEncryptionNone;
+               if (lc->vtable.call_encryption_changed)
+                       lc->vtable.call_encryption_changed(call->core, call, FALSE, call->auth_token);
        } else {
                ms_message("All streams are encrypted");
-               call->core->vtable.call_encryption_changed(call->core, call, TRUE, call->auth_token);
+               call->current_params.media_encryption=LinphoneMediaEncryptionZRTP;
+               if (lc->vtable.call_encryption_changed)
+                       lc->vtable.call_encryption_changed(call->core, call, TRUE, call->auth_token);
        }
 }
 
@@ -109,7 +124,7 @@ static void linphone_call_audiostream_encryption_changed(void *data, bool_t encr
 
        LinphoneCall *call = (LinphoneCall *)data;
        call->audiostream_encrypted=encrypted;
-
+       
        if (encrypted && call->core->vtable.display_status != NULL) {
                snprintf(status,sizeof(status)-1,_("Authentication token is %s"),call->auth_token);
                 call->core->vtable.display_status(call->core, status);
@@ -124,7 +139,6 @@ static void linphone_call_audiostream_encryption_changed(void *data, bool_t encr
        if (params->has_video) {
                ms_message("Trying to enable encryption on video stream");
                OrtpZrtpParams params;
-               params.zid=get_hexa_zrtp_identifier(call->core);
                params.zid_file=NULL; //unused
                video_stream_enable_zrtp(call->videostream,call->audiostream,&params);
        }
@@ -143,10 +157,27 @@ static void linphone_call_audiostream_auth_token_ready(void *data, const char* a
        ms_message("Authentication token is %s (%s)", auth_token, verified?"verified":"unverified");
 }
 
+void linphone_call_set_authentication_token_verified(LinphoneCall *call, bool_t verified){
+       if (call->audiostream==NULL){
+               ms_error("linphone_call_set_authentication_token_verified(): No audio stream");
+       }
+       if (call->audiostream->ms.zrtp_context==NULL){
+               ms_error("linphone_call_set_authentication_token_verified(): No zrtp context.");
+       }
+       if (!call->auth_token_verified && verified){
+               ortp_zrtp_sas_verified(call->audiostream->ms.zrtp_context);
+       }else if (call->auth_token_verified && !verified){
+               ortp_zrtp_sas_reset_verified(call->audiostream->ms.zrtp_context);
+       }
+       call->auth_token_verified=verified;
+       propagate_encryption_changed(call);
+}
 
-static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, int bandwidth_limit){
+static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, int bandwidth_limit,int* max_sample_rate, int nb_codecs_limit){
        MSList *l=NULL;
        const MSList *it;
+       int nb = 0;
+       if (max_sample_rate) *max_sample_rate=0;
        for(it=codecs;it!=NULL;it=it->next){
                PayloadType *pt=(PayloadType*)it->data;
                if (pt->flags & PAYLOAD_TYPE_ENABLED){
@@ -156,78 +187,161 @@ static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, int bandw
                        }
                        if (linphone_core_check_payload_type_usability(lc,pt)){
                                l=ms_list_append(l,payload_type_clone(pt));
+                               nb++;
+                               if (max_sample_rate && payload_type_get_rate(pt)>*max_sample_rate) *max_sample_rate=payload_type_get_rate(pt);
                        }
                }
+               if ((nb_codecs_limit > 0) && (nb >= nb_codecs_limit)) break;
        }
        return l;
 }
 
-static SalMediaDescription *_create_local_media_description(LinphoneCore *lc, LinphoneCall *call, unsigned int session_id, unsigned int session_ver){
+static void update_media_description_from_stun(SalMediaDescription *md, const StunCandidate *ac, const StunCandidate *vc){
+       int i;
+       for (i = 0; i < md->n_active_streams; i++) {
+               if ((md->streams[i].type == SalAudio) && (ac->port != 0)) {
+                       strcpy(md->streams[0].rtp_addr,ac->addr);
+                       md->streams[0].rtp_port=ac->port;
+                       if ((ac->addr[0]!='\0' && vc->addr[0]!='\0' && strcmp(ac->addr,vc->addr)==0) || md->n_active_streams==1){
+                               strcpy(md->addr,ac->addr);
+                       }
+               }
+               if ((md->streams[i].type == SalVideo) && (vc->port != 0)) {
+                       strcpy(md->streams[1].rtp_addr,vc->addr);
+                       md->streams[1].rtp_port=vc->port;
+               }
+       }
+}
+
+void linphone_call_make_local_media_description(LinphoneCore *lc, LinphoneCall *call){
        MSList *l;
        PayloadType *pt;
+       SalMediaDescription *old_md=call->localdesc;
+       int i;
        const char *me=linphone_core_get_identity(lc);
        LinphoneAddress *addr=linphone_address_new(me);
        const char *username=linphone_address_get_username (addr);
        SalMediaDescription *md=sal_media_description_new();
+       bool_t keep_srtp_keys=lp_config_get_int(lc->config,"sip","keep_srtp_keys",0);
+
+       linphone_core_adapt_to_network(lc,call->ping_time,&call->params);
 
-       md->session_id=session_id;
-       md->session_ver=session_ver;
-       md->nstreams=1;
+       md->session_id=(old_md ? old_md->session_id : (rand() & 0xfff));
+       md->session_ver=(old_md ? (old_md->session_ver+1) : (rand() & 0xfff));
+       md->n_total_streams=(old_md ? old_md->n_total_streams : 1);
+       md->n_active_streams=1;
        strncpy(md->addr,call->localip,sizeof(md->addr));
        strncpy(md->username,username,sizeof(md->username));
-       md->bandwidth=linphone_core_get_download_bandwidth(lc);
+       
+       if (call->params.down_bw)
+               md->bandwidth=call->params.down_bw;
+       else md->bandwidth=linphone_core_get_download_bandwidth(lc);
 
        /*set audio capabilities */
-       strncpy(md->streams[0].addr,call->localip,sizeof(md->streams[0].addr));
-       md->streams[0].port=call->audio_port;
-       md->streams[0].proto=SalProtoRtpAvp;
+       strncpy(md->streams[0].rtp_addr,call->localip,sizeof(md->streams[0].rtp_addr));
+       strncpy(md->streams[0].rtcp_addr,call->localip,sizeof(md->streams[0].rtcp_addr));
+       md->streams[0].rtp_port=call->audio_port;
+       md->streams[0].rtcp_port=call->audio_port+1;
+       md->streams[0].proto=(call->params.media_encryption == LinphoneMediaEncryptionSRTP) ? 
+               SalProtoRtpSavp : SalProtoRtpAvp;
        md->streams[0].type=SalAudio;
-       md->streams[0].ptime=lc->net_conf.down_ptime;
-       l=make_codec_list(lc,lc->codecs_conf.audio_codecs,call->params.audio_bw);
-       pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
+       if (call->params.down_ptime)
+               md->streams[0].ptime=call->params.down_ptime;
+       else
+               md->streams[0].ptime=linphone_core_get_download_ptime(lc);
+       l=make_codec_list(lc,lc->codecs_conf.audio_codecs,call->params.audio_bw,&md->streams[0].max_rate,-1);
+       pt=payload_type_clone(rtp_profile_get_payload_from_mime(lc->default_profile,"telephone-event"));
        l=ms_list_append(l,pt);
        md->streams[0].payloads=l;
 
-
        if (call->params.has_video){
-               md->nstreams++;
-               md->streams[1].port=call->video_port;
-               md->streams[1].proto=SalProtoRtpAvp;
+               md->n_active_streams++;
+               md->streams[1].rtp_port=call->video_port;
+               md->streams[1].rtcp_port=call->video_port+1;
+               md->streams[1].proto=md->streams[0].proto;
                md->streams[1].type=SalVideo;
-               l=make_codec_list(lc,lc->codecs_conf.video_codecs,0);
+               l=make_codec_list(lc,lc->codecs_conf.video_codecs,0,NULL,-1);
                md->streams[1].payloads=l;
        }
-       linphone_address_destroy(addr);
-       return md;
-}
-
-void update_local_media_description(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription **md){
-       if (*md == NULL) {
-               *md = _create_local_media_description(lc,call,0,0);
-       } else {
-               unsigned int id = (*md)->session_id;
-               unsigned int ver = (*md)->session_ver+1;
-               sal_media_description_unref(*md);
-               *md = _create_local_media_description(lc,call,id,ver);
+       if (md->n_total_streams < md->n_active_streams)
+               md->n_total_streams = md->n_active_streams;
+
+       /* Deactivate inactive streams. */
+       for (i = md->n_active_streams; i < md->n_total_streams; i++) {
+               md->streams[i].rtp_port = 0;
+               md->streams[i].rtcp_port = 0;
+               md->streams[i].proto = SalProtoRtpAvp;
+               md->streams[i].type = old_md->streams[i].type;
+               md->streams[i].dir = SalStreamInactive;
+               l = make_codec_list(lc, lc->codecs_conf.video_codecs, 0, NULL, 1);
+               md->streams[i].payloads = l;
+       }
+
+       for(i=0; i<md->n_active_streams; i++) {
+               if (md->streams[i].proto == SalProtoRtpSavp) {
+                       if (keep_srtp_keys && old_md && old_md->streams[i].proto==SalProtoRtpSavp){
+                               int j;
+                               for(j=0;j<SAL_CRYPTO_ALGO_MAX;++j){
+                                       memcpy(&md->streams[i].crypto[j],&old_md->streams[i].crypto[j],sizeof(SalSrtpCryptoAlgo));
+                               }
+                       }else{
+                               md->streams[i].crypto[0].tag = 1;
+                               md->streams[i].crypto[0].algo = AES_128_SHA1_80;
+                               if (!generate_b64_crypto_key(30, md->streams[i].crypto[0].master_key))
+                                       md->streams[i].crypto[0].algo = 0;
+                               md->streams[i].crypto[1].tag = 2;
+                               md->streams[i].crypto[1].algo = AES_128_SHA1_32;
+                               if (!generate_b64_crypto_key(30, md->streams[i].crypto[1].master_key))
+                                       md->streams[i].crypto[1].algo = 0;
+                               md->streams[i].crypto[2].algo = 0;
+                       }
+               }
        }
+       update_media_description_from_stun(md,&call->ac,&call->vc);
+       if (call->ice_session != NULL) {
+               linphone_core_update_local_media_description_from_ice(md, call->ice_session);
+               linphone_core_update_ice_state_in_call_stats(call);
+       }
+#ifdef BUILD_UPNP
+       if(call->upnp_session != NULL) {
+               linphone_core_update_local_media_description_from_upnp(md, call->upnp_session);
+               linphone_core_update_upnp_state_in_call_stats(call);
+       }
+#endif  //BUILD_UPNP
+       linphone_address_destroy(addr);
+       call->localdesc=md;
+       if (old_md) sal_media_description_unref(old_md);
 }
 
-SalMediaDescription *create_local_media_description(LinphoneCore *lc, LinphoneCall *call){
-       unsigned int id=rand() & 0xfff;
-       return _create_local_media_description(lc,call,id,id);
-}
-
-static int find_port_offset(LinphoneCore *lc){
+static int find_port_offset(LinphoneCore *lc, SalStreamType type){
        int offset;
        MSList *elem;
-       int audio_port;
+       int tried_port;
+       int existing_port;
        bool_t already_used=FALSE;
        for(offset=0;offset<100;offset+=2){
-               audio_port=linphone_core_get_audio_port (lc)+offset;
+               switch (type) {
+                       default:
+                       case SalAudio:
+                               tried_port=linphone_core_get_audio_port (lc)+offset;
+                               break;
+                       case SalVideo:
+                               tried_port=linphone_core_get_video_port (lc)+offset;
+                               break;
+               }
                already_used=FALSE;
                for(elem=lc->calls;elem!=NULL;elem=elem->next){
                        LinphoneCall *call=(LinphoneCall*)elem->data;
-                       if (call->audio_port==audio_port) {
+                       switch (type) {
+                               default:
+                               case SalAudio:
+                                       existing_port = call->audio_port;
+                                       break;
+                               case SalVideo:
+                                       existing_port = call->video_port;
+                                       break;
+                       }
+                       if (existing_port==tried_port) {
                                already_used=TRUE;
                                break;
                        }
@@ -241,22 +355,100 @@ static int find_port_offset(LinphoneCore *lc){
        return offset;
 }
 
+static int select_random_port(LinphoneCore *lc, SalStreamType type) {
+       MSList *elem;
+       int nb_tries;
+       int tried_port = 0;
+       int existing_port = 0;
+       int min_port = 0, max_port = 0;
+       bool_t already_used = FALSE;
+
+       switch (type) {
+               default:
+               case SalAudio:
+                       linphone_core_get_audio_port_range(lc, &min_port, &max_port);
+                       break;
+               case SalVideo:
+                       linphone_core_get_video_port_range(lc, &min_port, &max_port);
+                       break;
+       }
+       tried_port = (rand() % (max_port - min_port) + min_port) & ~0x1;
+       if (tried_port < min_port) tried_port = min_port + 2;
+       for (nb_tries = 0; nb_tries < 100; nb_tries++) {
+               for (elem = lc->calls; elem != NULL; elem = elem->next) {
+                       LinphoneCall *call = (LinphoneCall *)elem->data;
+                       switch (type) {
+                               default:
+                               case SalAudio:
+                                       existing_port = call->audio_port;
+                                       break;
+                               case SalVideo:
+                                       existing_port = call->video_port;
+                                       break;
+                       }
+                       if (existing_port == tried_port) {
+                               already_used = TRUE;
+                               break;
+                       }
+               }
+               if (!already_used) break;
+       }
+       if (nb_tries == 100) {
+               ms_error("Could not find any free port!");
+               return -1;
+       }
+       return tried_port;
+}
+
 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
        int port_offset;
+       int min_port, max_port;
+       call->magic=linphone_call_magic;
        call->refcnt=1;
        call->state=LinphoneCallIdle;
+       call->transfer_state = LinphoneCallIdle;
        call->start_time=time(NULL);
        call->media_start_time=0;
        call->log=linphone_call_log_new(call, from, to);
        call->owns_call_log=TRUE;
        linphone_core_notify_all_friends(call->core,LinphoneStatusOnThePhone);
-       port_offset=find_port_offset (call->core);
-       if (port_offset==-1) return;
-       call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
-       call->video_port=linphone_core_get_video_port(call->core)+port_offset;
+       linphone_core_get_audio_port_range(call->core, &min_port, &max_port);
+       if (min_port == max_port) {
+               /* Used fixed RTP audio port. */
+               port_offset=find_port_offset (call->core, SalAudio);
+               if (port_offset==-1) return;
+               call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
+       } else {
+               /* Select random RTP audio port in the specified range. */
+               call->audio_port = select_random_port(call->core, SalAudio);
+       }
+       linphone_core_get_video_port_range(call->core, &min_port, &max_port);
+       if (min_port == max_port) {
+               /* Used fixed RTP video port. */
+               port_offset=find_port_offset (call->core, SalVideo);
+               if (port_offset==-1) return;
+               call->video_port=linphone_core_get_video_port(call->core)+port_offset;
+       } else {
+               /* Select random RTP video port in the specified range. */
+               call->video_port = select_random_port(call->core, SalVideo);
+       }
+       linphone_call_init_stats(&call->stats[LINPHONE_CALL_STATS_AUDIO], LINPHONE_CALL_STATS_AUDIO);
+       linphone_call_init_stats(&call->stats[LINPHONE_CALL_STATS_VIDEO], LINPHONE_CALL_STATS_VIDEO);
+}
 
+void linphone_call_init_stats(LinphoneCallStats *stats, int type) {
+       stats->type = type;
+       stats->received_rtcp = NULL;
+       stats->sent_rtcp = NULL;
+       stats->ice_state = LinphoneIceStateNotActivated;
+#ifdef BUILD_UPNP
+       stats->upnp_state = LinphoneUpnpStateIdle;
+#else
+       stats->upnp_state = LinphoneUpnpStateNotAvailable;
+#endif //BUILD_UPNP
 }
 
+
 static void discover_mtu(LinphoneCore *lc, const char *remote){
        int mtu;
        if (lc->net_conf.mtu==0 ){
@@ -277,16 +469,32 @@ LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddr
        call->op=sal_op_new(lc->sal);
        sal_op_set_user_pointer(call->op,call);
        call->core=lc;
-       linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
+       linphone_core_get_local_ip(lc,NULL,call->localip);
        linphone_call_init_common(call,from,to);
-       call->params=*params;
-       call->localdesc=create_local_media_description (lc,call);
+       _linphone_call_params_copy(&call->params,params);
+       sal_op_set_custom_header(call->op,call->params.custom_headers);
+       call->params.custom_headers=NULL;
+       
+       if (linphone_core_get_firewall_policy(call->core) == LinphonePolicyUseIce) {
+               call->ice_session = ice_session_new();
+               ice_session_set_role(call->ice_session, IR_Controlling);
+       }
+       if (linphone_core_get_firewall_policy(call->core) == LinphonePolicyUseStun) {
+               call->ping_time=linphone_core_run_stun_tests(call->core,call);
+       }
+#ifdef BUILD_UPNP
+       if (linphone_core_get_firewall_policy(call->core) == LinphonePolicyUseUpnp) {
+               if(!lc->rtp_conf.disable_upnp) {
+                       call->upnp_session = linphone_upnp_session_new(call);
+               }
+       }
+#endif //BUILD_UPNP
        call->camera_active=params->has_video;
-       if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
-               linphone_core_run_stun_tests(call->core,call);
+       
        discover_mtu(lc,linphone_address_get_domain (to));
        if (params->referer){
-               sal_call_set_referer (call->op,params->referer->op);
+               sal_call_set_referer(call->op,params->referer->op);
+               call->referer=linphone_call_ref(params->referer);
        }
        return call;
 }
@@ -294,6 +502,7 @@ LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddr
 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
        LinphoneCall *call=ms_new0(LinphoneCall,1);
        char *from_str;
+       const SalMediaDescription *md;
 
        call->dir=LinphoneCallIncoming;
        sal_op_set_user_pointer(op,call);
@@ -301,24 +510,73 @@ LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *fro
        call->core=lc;
 
        if (lc->sip_conf.ping_with_options){
-               /*the following sends an option request back to the caller so that
-                we get a chance to discover our nat'd address before answering.*/
-               call->ping_op=sal_op_new(lc->sal);
-               from_str=linphone_address_as_string(from);
-               sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
-               sal_op_set_user_pointer(call->ping_op,call);
-               sal_ping(call->ping_op,linphone_core_find_best_identity(lc,from,NULL),from_str);
-               ms_free(from_str);
+#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
+                       /*the following sends an option request back to the caller so that
+                        we get a chance to discover our nat'd address before answering.*/
+                       call->ping_op=sal_op_new(lc->sal);
+                       from_str=linphone_address_as_string_uri_only(from);
+                       sal_op_set_route(call->ping_op,sal_op_get_network_origin(op));
+                       sal_op_set_user_pointer(call->ping_op,call);
+                       sal_ping(call->ping_op,linphone_core_find_best_identity(lc,from,NULL),from_str);
+                       ms_free(from_str);
+               }
        }
 
        linphone_address_clean(from);
-       linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
+       linphone_core_get_local_ip(lc,NULL,call->localip);
        linphone_call_init_common(call, from, to);
-       call->params.has_video=linphone_core_video_enabled(lc);
-       call->localdesc=create_local_media_description (lc,call);
+       call->log->call_id=ms_strdup(sal_op_get_call_id(op)); /*must be known at that time*/
+       linphone_core_init_default_params(lc, &call->params);
+       md=sal_call_get_remote_media_description(op);
+       call->params.has_video &= !!lc->video_policy.automatically_accept;
+       if (md) {
+               // It is licit to receive an INVITE without SDP
+               // In this case WE chose the media parameters according to policy.
+               call->params.has_video &= linphone_core_media_description_contains_video_stream(md);
+       }
+       switch (linphone_core_get_firewall_policy(call->core)) {
+               case LinphonePolicyUseIce:
+                       call->ice_session = ice_session_new();
+                       ice_session_set_role(call->ice_session, IR_Controlled);
+                       linphone_core_update_ice_from_remote_media_description(call, sal_call_get_remote_media_description(op));
+                       if (call->ice_session != NULL) {
+                               linphone_call_init_media_streams(call);
+                               linphone_call_start_media_streams_for_ice_gathering(call);
+                               if (linphone_core_gather_ice_candidates(call->core,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);
+                               }
+                       }
+                       break;
+               case LinphonePolicyUseStun:
+                       call->ping_time=linphone_core_run_stun_tests(call->core,call);
+                       /* No break to also destroy ice session in this case. */
+                       break;
+               case LinphonePolicyUseUpnp:
+#ifdef BUILD_UPNP
+                       if(!lc->rtp_conf.disable_upnp) {
+                               call->upnp_session = linphone_upnp_session_new(call);
+                               if (call->upnp_session != NULL) {
+                                       linphone_call_init_media_streams(call);
+                                       if (linphone_core_update_upnp_from_remote_media_description(call, sal_call_get_remote_media_description(op))<0) {
+                                               /* uPnP port mappings failed, proceed with the call anyway. */
+                                               linphone_call_delete_upnp_session(call);
+                                       }
+                               }
+                       }
+#endif //BUILD_UPNP
+                       break;
+               default:
+                       break;
+       }
        call->camera_active=call->params.has_video;
-       if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
-               linphone_core_run_stun_tests(call->core,call);
+       
        discover_mtu(lc,linphone_address_get_domain(from));
        return call;
 }
@@ -350,11 +608,20 @@ static void linphone_call_set_terminated(LinphoneCall *call){
        if (ms_list_size(lc->calls)==0)
                linphone_core_notify_all_friends(lc,lc->presence_mode);
 
-       linphone_core_conference_check_uninit(&lc->conf_ctx);
+       linphone_core_conference_check_uninit(lc);
        if (call->ringing_beep){
                linphone_core_stop_dtmf(lc);
                call->ringing_beep=FALSE;
        }
+       if (call->referer){
+               linphone_call_unref(call->referer);
+               call->referer=NULL;
+       }
+}
+
+void linphone_call_fix_call_parameters(LinphoneCall *call){
+       call->params.has_video=call->current_params.has_video;
+       call->params.media_encryption=call->current_params.media_encryption;
 }
 
 const char *linphone_call_state_to_string(LinphoneCallState cs){
@@ -393,8 +660,8 @@ const char *linphone_call_state_to_string(LinphoneCallState cs){
                        return "LinphoneCallUpdatedByRemote";
                case LinphoneCallIncomingEarlyMedia:
                        return "LinphoneCallIncomingEarlyMedia";
-               case LinphoneCallUpdated:
-                       return "LinphoneCallUpdated";
+               case LinphoneCallUpdating:
+                       return "LinphoneCallUpdating";
                case LinphoneCallReleased:
                        return "LinphoneCallReleased";
        }
@@ -408,7 +675,7 @@ void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const
                if (call->state==LinphoneCallEnd || call->state==LinphoneCallError){
                        if (cstate!=LinphoneCallReleased){
                                ms_warning("Spurious call state change from %s to %s, ignored.",linphone_call_state_to_string(call->state),
-                          linphone_call_state_to_string(cstate));
+                                  linphone_call_state_to_string(cstate));
                                return;
                        }
                }
@@ -420,14 +687,22 @@ void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const
                        call->state=cstate;
                }
                if (cstate==LinphoneCallEnd || cstate==LinphoneCallError){
-             if (call->reason==LinphoneReasonDeclined){
-                 call->log->status=LinphoneCallDeclined;
-             }
-            linphone_call_set_terminated (call);
+                       switch(call->reason){
+                               case LinphoneReasonDeclined:
+                                       call->log->status=LinphoneCallDeclined;
+                                       break;
+                               case LinphoneReasonNotAnswered:
+                                       call->log->status=LinphoneCallMissed;
+                               break;
+                               default:
+                               break;
+                       }
+                       linphone_call_set_terminated (call);
+               }
+               if (cstate == LinphoneCallConnected) {
+                       call->log->status=LinphoneCallSuccess;
+                       call->media_start_time=time(NULL);
                }
-        if (cstate == LinphoneCallConnected) {
-            call->log->status=LinphoneCallSuccess;
-        }
 
                if (lc->vtable.call_state_changed)
                        lc->vtable.call_state_changed(lc,call,cstate,message);
@@ -445,6 +720,10 @@ void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const
 
 static void linphone_call_destroy(LinphoneCall *obj)
 {
+#ifdef BUILD_UPNP
+       linphone_call_delete_upnp_session(obj);
+#endif //BUILD_UPNP
+       linphone_call_delete_ice_session(obj);
        if (obj->op!=NULL) {
                sal_op_release(obj->op);
                obj->op=NULL;
@@ -468,7 +747,7 @@ static void linphone_call_destroy(LinphoneCall *obj)
        if (obj->auth_token) {
                ms_free(obj->auth_token);
        }
-
+       linphone_call_params_uninit(&obj->params);
        ms_free(obj);
 }
 
@@ -503,10 +782,53 @@ void linphone_call_unref(LinphoneCall *obj){
 /**
  * Returns current parameters associated to the call.
 **/
-const LinphoneCallParams * linphone_call_get_current_params(const LinphoneCall *call){
+const LinphoneCallParams * linphone_call_get_current_params(LinphoneCall *call){
+       if (call->params.record_file)
+               call->current_params.record_file=call->params.record_file;
        return &call->current_params;
 }
 
+static bool_t is_video_active(const SalStreamDescription *sd){
+       return sd->rtp_port!=0 && sd->dir!=SalStreamInactive;
+}
+
+/**
+ * Returns call parameters proposed by remote.
+ * 
+ * This is useful when receiving an incoming call, to know whether the remote party
+ * supports video, encryption or whatever.
+**/
+const LinphoneCallParams * linphone_call_get_remote_params(LinphoneCall *call){
+       LinphoneCallParams *cp=&call->remote_params;
+       memset(cp,0,sizeof(*cp));
+       if (call->op){
+               SalMediaDescription *md=sal_call_get_remote_media_description(call->op);
+               if (md){
+                       SalStreamDescription *asd,*vsd,*secure_asd,*secure_vsd;
+
+                       asd=sal_media_description_find_stream(md,SalProtoRtpAvp,SalAudio);
+                       vsd=sal_media_description_find_stream(md,SalProtoRtpAvp,SalVideo);
+                       secure_asd=sal_media_description_find_stream(md,SalProtoRtpSavp,SalAudio);
+                       secure_vsd=sal_media_description_find_stream(md,SalProtoRtpSavp,SalVideo);
+                       if (secure_vsd){
+                               cp->has_video=is_video_active(secure_vsd);
+                               if (secure_asd || asd==NULL)
+                                       cp->media_encryption=LinphoneMediaEncryptionSRTP;
+                       }else if (vsd){
+                               cp->has_video=is_video_active(vsd);
+                       }
+                       if (!cp->has_video){
+                               if (md->bandwidth>0 && md->bandwidth<=linphone_core_get_edge_bw(call->core)){
+                                       cp->low_bandwidth=TRUE;
+                               }
+                       }
+                       cp->custom_headers=(SalCustomHeader*)sal_op_get_custom_header(call->op);
+                       return cp;
+               }
+       }
+       return NULL;
+}
+
 /**
  * Returns the remote address associated to this call
  *
@@ -593,6 +915,16 @@ const char *linphone_call_get_remote_user_agent(LinphoneCall *call){
        return NULL;
 }
 
+/**
+ * Returns the far end's sip contact as a string, if available.
+**/
+const char *linphone_call_get_remote_contact(LinphoneCall *call){
+       if (call->op){
+               return sal_op_get_remote_contact(call->op);
+       }
+       return NULL;
+}
+
 /**
  * Returns true if this calls has received a transfer that has not been
  * executed yet.
@@ -632,7 +964,7 @@ LinphoneCall *linphone_call_get_replaced_call(LinphoneCall *call){
 **/
 void linphone_call_enable_camera (LinphoneCall *call, bool_t enable){
 #ifdef VIDEO_ENABLED
-       if (call->videostream!=NULL && call->videostream->ticker!=NULL){
+       if (call->videostream!=NULL && call->videostream->ms.ticker!=NULL){
                LinphoneCore *lc=call->core;
                MSWebCam *nowebcam=get_nowebcam_device();
                if (call->camera_active!=enable && lc->video_conf.device!=nowebcam){
@@ -644,6 +976,18 @@ void linphone_call_enable_camera (LinphoneCall *call, bool_t enable){
 #endif
 }
 
+#ifdef VIDEO_ENABLED
+/**
+ * Request remote side to send us a Video Fast Update.
+**/
+void linphone_call_send_vfu_request(LinphoneCall *call)
+{
+       if (LinphoneCallStreamsRunning == linphone_call_get_state(call))
+               sal_call_send_vfu_request(call->op);
+}
+#endif
+
+
 /**
  * Take a photo of currently received video and write it into a jpeg file.
 **/
@@ -672,6 +1016,46 @@ void linphone_call_params_enable_video(LinphoneCallParams *cp, bool_t enabled){
        cp->has_video=enabled;
 }
 
+/**
+ * Returns the audio codec used in the call, described as a PayloadType structure.
+**/
+const PayloadType* linphone_call_params_get_used_audio_codec(const LinphoneCallParams *cp) {
+       return cp->audio_codec;
+}
+
+
+/**
+ * Returns the video codec used in the call, described as a PayloadType structure.
+**/
+const PayloadType* linphone_call_params_get_used_video_codec(const LinphoneCallParams *cp) {
+       return cp->video_codec;
+}
+
+/**
+ * @ingroup call_control
+ * Use to know if this call has been configured in low bandwidth mode.
+ * This mode can be automatically discovered thanks to a stun server when activate_edge_workarounds=1 in section [net] of configuration file.
+ * An application that would have reliable way to know network capacity may not use activate_edge_workarounds=1 but instead manually configure
+ * low bandwidth mode with linphone_call_params_enable_low_bandwidth().
+ * <br> When enabled, this param may transform a call request with video in audio only mode.
+ * @return TRUE if low bandwidth has been configured/detected
+ */
+bool_t linphone_call_params_low_bandwidth_enabled(const LinphoneCallParams *cp) {
+       return cp->low_bandwidth;
+}
+
+/**
+ * @ingroup call_control
+ * Indicate low bandwith mode. 
+ * Configuring a call to low bandwidth mode will result in the core to activate several settings for the call in order to ensure that bitrate usage
+ * is lowered to the minimum possible. Typically, ptime (packetization time) will be increased, audio codec's output bitrate will be targetted to 20kbit/s provided
+ * that it is achievable by the codec selected after SDP handshake. Video is automatically disabled.
+ * 
+**/
+void linphone_call_params_enable_low_bandwidth(LinphoneCallParams *cp, bool_t enabled){
+       cp->low_bandwidth=enabled;
+}
+
 /**
  * Returns whether video is enabled.
 **/
@@ -679,6 +1063,21 @@ bool_t linphone_call_params_video_enabled(const LinphoneCallParams *cp){
        return cp->has_video;
 }
 
+/**
+ * Returns kind of media encryption selected for the call.
+**/
+enum LinphoneMediaEncryption linphone_call_params_get_media_encryption(const LinphoneCallParams *cp) {
+       return cp->media_encryption;
+}
+
+/**
+ * Set requested media encryption for a call.
+**/
+void linphone_call_params_set_media_encryption(LinphoneCallParams *cp, enum LinphoneMediaEncryption e) {
+       cp->media_encryption = e;
+}
+
+
 /**
  * Enable sending of real early media (during outgoing calls).
 **/
@@ -686,6 +1085,9 @@ void linphone_call_params_enable_early_media_sending(LinphoneCallParams *cp, boo
        cp->real_early_media=enabled;
 }
 
+/**
+ * Indicates whether sending of early media was enabled.
+**/
 bool_t linphone_call_params_early_media_sending_enabled(const LinphoneCallParams *cp){
        return cp->real_early_media;
 }
@@ -705,33 +1107,46 @@ void linphone_call_params_set_audio_bandwidth_limit(LinphoneCallParams *cp, int
        cp->audio_bw=bandwidth;
 }
 
-#ifdef VIDEO_ENABLED
-/**
- * Request remote side to send us a Video Fast Update.
-**/
-void linphone_call_send_vfu_request(LinphoneCall *call)
-{
-       if (LinphoneCallStreamsRunning == linphone_call_get_state(call))
-               sal_call_send_vfu_request(call->op);
+void linphone_call_params_add_custom_header(LinphoneCallParams *params, const char *header_name, const char *header_value){
+       params->custom_headers=sal_custom_header_append(params->custom_headers,header_name,header_value);
+}
+
+const char *linphone_call_params_get_custom_header(const LinphoneCallParams *params, const char *header_name){
+       return sal_custom_header_find(params->custom_headers,header_name);
+}
+
+void _linphone_call_params_copy(LinphoneCallParams *ncp, const LinphoneCallParams *cp){
+       memcpy(ncp,cp,sizeof(LinphoneCallParams));
+       if (cp->record_file) ncp->record_file=ms_strdup(cp->record_file);
+       /*
+        * The management of the custom headers is not optimal. We copy everything while ref counting would be more efficient.
+        */
+       if (cp->custom_headers) ncp->custom_headers=sal_custom_header_clone(cp->custom_headers);
 }
-#endif
 
 /**
- *
+ * Copy existing LinphoneCallParams to a new LinphoneCallParams object.
 **/
 LinphoneCallParams * linphone_call_params_copy(const LinphoneCallParams *cp){
        LinphoneCallParams *ncp=ms_new0(LinphoneCallParams,1);
-       memcpy(ncp,cp,sizeof(LinphoneCallParams));
+       _linphone_call_params_copy(ncp,cp);
        return ncp;
 }
 
+void linphone_call_params_uninit(LinphoneCallParams *p){
+       if (p->record_file) ms_free(p->record_file);
+       if (p->custom_headers) sal_custom_header_free(p->custom_headers);
+}
+
 /**
- *
+ * Destroy LinphoneCallParams.
 **/
 void linphone_call_params_destroy(LinphoneCallParams *p){
+       linphone_call_params_uninit(p);
        ms_free(p);
 }
 
+
 /**
  * @}
 **/
@@ -746,11 +1161,17 @@ static void rendercb(void *data, const MSPicture *local, const MSPicture *remote
 
 #ifdef VIDEO_ENABLED
 static void video_stream_event_cb(void *user_pointer, const MSFilter *f, const unsigned int event_id, const void *args){
+    LinphoneCall* call = (LinphoneCall*) user_pointer;
        ms_warning("In linphonecall.c: video_stream_event_cb");
        switch (event_id) {
                case MS_VIDEO_DECODER_DECODING_ERRORS:
                        ms_warning("Case is MS_VIDEO_DECODER_DECODING_ERRORS");
-                       linphone_call_send_vfu_request((LinphoneCall*) user_pointer);
+                       linphone_call_send_vfu_request(call);
+                       break;
+               case MS_VIDEO_DECODER_FIRST_IMAGE_DECODED:
+                       ms_message("First video frame decoded successfully");
+                       if (call->nextVideoFrameDecoded._func != NULL)
+                       call->nextVideoFrameDecoded._func(call, call->nextVideoFrameDecoded._user_data);
                        break;
                default:
                        ms_warning("Unhandled event %i", event_id);
@@ -759,12 +1180,24 @@ static void video_stream_event_cb(void *user_pointer, const MSFilter *f, const u
 }
 #endif
 
-void linphone_call_init_media_streams(LinphoneCall *call){
+void linphone_call_set_next_video_frame_decoded_callback(LinphoneCall *call, LinphoneCallCbFunc cb, void* user_data) {
+       call->nextVideoFrameDecoded._func = cb;
+       call->nextVideoFrameDecoded._user_data = user_data;
+#ifdef VIDEO_ENABLED
+       ms_filter_call_method_noarg(call->videostream->ms.decoder, MS_VIDEO_DECODER_RESET_FIRST_IMAGE_NOTIFICATION);
+#endif
+}
+
+void linphone_call_init_audio_stream(LinphoneCall *call){
        LinphoneCore *lc=call->core;
-       SalMediaDescription *md=call->localdesc;
        AudioStream *audiostream;
+       int dscp;
 
-       call->audiostream=audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
+       if (call->audiostream != NULL) return;
+       call->audiostream=audiostream=audio_stream_new(call->audio_port,call->audio_port+1,linphone_core_ipv6_enabled(lc));
+       dscp=linphone_core_get_audio_dscp(lc);
+       if (dscp!=-1)
+               audio_stream_set_dscp(audiostream,dscp);
        if (linphone_core_echo_limiter_enabled(lc)){
                const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
                if (strcasecmp(type,"mic")==0)
@@ -790,23 +1223,65 @@ void linphone_call_init_media_streams(LinphoneCall *call){
                audio_stream_enable_noise_gate(audiostream,enabled);
        }
 
-       if (lc->a_rtp)
-               rtp_session_set_transports(audiostream->session,lc->a_rtp,lc->a_rtcp);
+       audio_stream_set_features(audiostream,linphone_core_get_audio_features(lc));
+
+       if (lc->rtptf){
+               RtpTransport *artp=lc->rtptf->audio_rtp_func(lc->rtptf->audio_rtp_func_data, call->audio_port);
+               RtpTransport *artcp=lc->rtptf->audio_rtcp_func(lc->rtptf->audio_rtcp_func_data, call->audio_port+1);
+               rtp_session_set_transports(audiostream->ms.session,artp,artcp);
+       }
+       if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseIce) && (call->ice_session != NULL)){
+               rtp_session_set_pktinfo(audiostream->ms.session, TRUE);
+               rtp_session_set_symmetric_rtp(audiostream->ms.session, FALSE);
+               if (ice_session_check_list(call->ice_session, 0) == NULL) {
+                       ice_session_add_check_list(call->ice_session, ice_check_list_new());
+               }
+               audiostream->ms.ice_check_list = ice_session_check_list(call->ice_session, 0);
+               ice_check_list_set_rtp_session(audiostream->ms.ice_check_list, audiostream->ms.session);
+       }
 
        call->audiostream_app_evq = ortp_ev_queue_new();
-       rtp_session_register_event_queue(audiostream->session,call->audiostream_app_evq);
+       rtp_session_register_event_queue(audiostream->ms.session,call->audiostream_app_evq);
+}
 
+void linphone_call_init_video_stream(LinphoneCall *call){
 #ifdef VIDEO_ENABLED
+       LinphoneCore *lc=call->core;
 
-       if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0){
-               call->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
-       if( lc->video_conf.displaytype != NULL)
-               video_stream_set_display_filter_name(call->videostream,lc->video_conf.displaytype);
-       video_stream_set_event_callback(call->videostream,video_stream_event_cb, call);
-       if (lc->v_rtp)
-               rtp_session_set_transports(call->videostream->session,lc->v_rtp,lc->v_rtcp);
-       call->videostream_app_evq = ortp_ev_queue_new();
-       rtp_session_register_event_queue(call->videostream->session,call->videostream_app_evq);
+       if (!call->params.has_video) {
+               linphone_call_stop_video_stream(call);
+               return;
+       }
+       if (call->videostream != NULL) return;
+       if ((lc->video_conf.display || lc->video_conf.capture) && call->params.has_video){
+               int video_recv_buf_size=lp_config_get_int(lc->config,"video","recv_buf_size",0);
+               int dscp=linphone_core_get_video_dscp(lc);
+               
+               call->videostream=video_stream_new(call->video_port,call->video_port+1,linphone_core_ipv6_enabled(lc));
+               if (dscp!=-1)
+                       video_stream_set_dscp(call->videostream,dscp);
+               video_stream_enable_display_filter_auto_rotate(call->videostream, lp_config_get_int(lc->config,"video","display_filter_auto_rotate",0));
+               if (video_recv_buf_size>0) rtp_session_set_recv_buf_size(call->videostream->ms.session,video_recv_buf_size);
+
+               if( lc->video_conf.displaytype != NULL)
+                       video_stream_set_display_filter_name(call->videostream,lc->video_conf.displaytype);
+               video_stream_set_event_callback(call->videostream,video_stream_event_cb, call);
+               if (lc->rtptf){
+                       RtpTransport *vrtp=lc->rtptf->video_rtp_func(lc->rtptf->video_rtp_func_data, call->video_port);
+                       RtpTransport *vrtcp=lc->rtptf->video_rtcp_func(lc->rtptf->video_rtcp_func_data, call->video_port+1);
+                       rtp_session_set_transports(call->videostream->ms.session,vrtp,vrtcp);
+               }
+               if ((linphone_core_get_firewall_policy(lc) == LinphonePolicyUseIce) && (call->ice_session != NULL)){
+                       rtp_session_set_pktinfo(call->videostream->ms.session, TRUE);
+                       rtp_session_set_symmetric_rtp(call->videostream->ms.session, FALSE);
+                       if (ice_session_check_list(call->ice_session, 1) == NULL) {
+                               ice_session_add_check_list(call->ice_session, ice_check_list_new());
+                       }
+                       call->videostream->ms.ice_check_list = ice_session_check_list(call->ice_session, 1);
+                       ice_check_list_set_rtp_session(call->videostream->ms.ice_check_list, call->videostream->ms.session);
+               }
+               call->videostream_app_evq = ortp_ev_queue_new();
+               rtp_session_register_event_queue(call->videostream->ms.session,call->videostream_app_evq);
 #ifdef TEST_EXT_RENDERER
                video_stream_set_render_callback(call->videostream,rendercb,NULL);
 #endif
@@ -816,11 +1291,15 @@ void linphone_call_init_media_streams(LinphoneCall *call){
 #endif
 }
 
+void linphone_call_init_media_streams(LinphoneCall *call){
+       linphone_call_init_audio_stream(call);
+       linphone_call_init_video_stream(call);
+}
+
 
 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
 
-static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
-       LinphoneCore* lc = (LinphoneCore*)user_data;
+static void linphone_core_dtmf_received(LinphoneCore *lc, int dtmf){
        if (dtmf<0 || dtmf>15){
                ms_warning("Bad dtmf value %i",dtmf);
                return;
@@ -852,8 +1331,7 @@ static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
 }
 
 void _post_configure_audio_stream(AudioStream *st, LinphoneCore *lc, bool_t muted){
-       float mic_gain=lp_config_get_float(lc->config,"sound","mic_gain",1);
-       float spk_gain=lp_config_get_float(lc->config,"sound","speaker_gain",1);
+       float mic_gain=lc->sound_conf.soft_mic_lev;
        float thres = 0;
        float recv_gain;
        float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
@@ -861,7 +1339,7 @@ void _post_configure_audio_stream(AudioStream *st, LinphoneCore *lc, bool_t mute
        int dc_removal=lp_config_get_int(lc->config,"sound","dc_removal",0);
 
        if (!muted)
-               audio_stream_set_mic_gain(st,mic_gain);
+               linphone_core_set_mic_gain_db (lc, mic_gain);
        else
                audio_stream_set_mic_gain(st,0);
 
@@ -895,10 +1373,9 @@ void _post_configure_audio_stream(AudioStream *st, LinphoneCore *lc, bool_t mute
        }
        if (st->volrecv){
                /* parameters for a limited noise-gate effect, using echo limiter threshold */
-               float floorgain = 1/mic_gain;
+               float floorgain = 1/pow(10,(mic_gain)/10);
                int spk_agc=lp_config_get_int(lc->config,"sound","speaker_agc_enabled",0);
                ms_filter_call_method(st->volrecv, MS_VOLUME_ENABLE_AGC, &spk_agc);
-               ms_filter_call_method(st->volrecv, MS_VOLUME_SET_GAIN, &spk_gain);
                ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
                ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&floorgain);
        }
@@ -910,10 +1387,10 @@ static void post_configure_audio_streams(LinphoneCall*call){
        LinphoneCore *lc=call->core;
        _post_configure_audio_stream(st,lc,call->audio_muted);
        if (lc->vtable.dtmf_received!=NULL){
-               /* replace by our default action*/
                audio_stream_play_received_dtmfs(call->audiostream,FALSE);
-               rtp_session_signal_connect(call->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
        }
+       if (call->record_active) 
+               linphone_call_start_recording(call);
 }
 
 static RtpProfile *make_profile(LinphoneCall *call, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
@@ -924,6 +1401,7 @@ static RtpProfile *make_profile(LinphoneCall *call, const SalMediaDescription *m
        int remote_bw=0;
        LinphoneCore *lc=call->core;
        int up_ptime=0;
+       const LinphoneCallParams *params=&call->params;
        *used_pt=-1;
 
        for(elem=desc->payloads;elem!=NULL;elem=elem->next){
@@ -933,7 +1411,9 @@ static RtpProfile *make_profile(LinphoneCall *call, const SalMediaDescription *m
                if ((pt->flags & PAYLOAD_TYPE_FLAG_CAN_SEND) && first) {
                        if (desc->type==SalAudio){
                                linphone_core_update_allocated_audio_bandwidth_in_call(call,pt);
-                               up_ptime=linphone_core_get_upload_ptime(lc);
+                               if (params->up_ptime)
+                                       up_ptime=params->up_ptime;
+                               else up_ptime=linphone_core_get_upload_ptime(lc);
                        }
                        *used_pt=payload_type_get_number(pt);
                        first=FALSE;
@@ -948,7 +1428,12 @@ static RtpProfile *make_profile(LinphoneCall *call, const SalMediaDescription *m
                }
 
                if (desc->type==SalAudio){
-                               bw=get_min_bandwidth(call->audio_bw,remote_bw);
+                       int audio_bw=call->audio_bw;
+                       if (params->up_bw){
+                               if (params->up_bw< audio_bw)
+                                       audio_bw=params->up_bw;
+                       }
+                       bw=get_min_bandwidth(audio_bw,remote_bw);
                }else bw=get_min_bandwidth(get_video_bandwidth(linphone_core_get_upload_bandwidth (lc),call->audio_bw),remote_bw);
                if (bw>0) pt->normal_bitrate=bw*1000;
                else if (desc->type==SalAudio){
@@ -978,23 +1463,35 @@ static void setup_ring_player(LinphoneCore *lc, LinphoneCall *call){
        ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
 }
 
-#define LINPHONE_RTCP_SDES_TOOL "Linphone-" LINPHONE_VERSION
-
 static bool_t linphone_call_sound_resources_available(LinphoneCall *call){
        LinphoneCore *lc=call->core;
        LinphoneCall *current=linphone_core_get_current_call(lc);
        return !linphone_core_is_in_conference(lc) && 
                (current==NULL || current==call);
 }
-
+static int find_crypto_index_from_tag(const SalSrtpCryptoAlgo crypto[],unsigned char tag) {
+    int i;
+    for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
+        if (crypto[i].tag == tag) {
+            return i;
+        }
+    }
+    return -1;
+}
 static void linphone_call_start_audio_stream(LinphoneCall *call, const char *cname, bool_t muted, bool_t send_ringbacktone, bool_t use_arc){
        LinphoneCore *lc=call->core;
-       int jitt_comp=lc->rtp_conf.audio_jitt_comp;
        int used_pt=-1;
+       char rtcp_tool[128]={0};
+       snprintf(rtcp_tool,sizeof(rtcp_tool)-1,"%s-%s",linphone_core_get_user_agent_name(),linphone_core_get_user_agent_version());
+       /* look for savp stream first */
        const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
+                                               SalProtoRtpSavp,SalAudio);
+       /* no savp audio stream, use avp */
+       if (!stream)
+               stream=sal_media_description_find_stream(call->resultdesc,
                                                SalProtoRtpAvp,SalAudio);
 
-       if (stream && stream->dir!=SalStreamInactive && stream->port!=0){
+       if (stream && stream->dir!=SalStreamInactive && stream->rtp_port!=0){
                MSSndCard *playcard=lc->sound_conf.lsd_card ?
                        lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
                MSSndCard *captcard=lc->sound_conf.capt_sndcard;
@@ -1004,6 +1501,7 @@ static void linphone_call_start_audio_stream(LinphoneCall *call, const char *cna
                bool_t use_ec;
 
                if (used_pt!=-1){
+                       call->current_params.audio_codec = rtp_profile_get_payload(call->audio_profile, used_pt);
                        if (playcard==NULL) {
                                ms_warning("No card defined for playback !");
                        }
@@ -1012,7 +1510,7 @@ static void linphone_call_start_audio_stream(LinphoneCall *call, const char *cna
                        }
                        /*Replace soundcard filters by inactive file players or recorders
                         when placed in recvonly or sendonly mode*/
-                       if (stream->port==0 || stream->dir==SalStreamRecvOnly){
+                       if (stream->rtp_port==0 || stream->dir==SalStreamRecvOnly){
                                captcard=NULL;
                                playfile=NULL;
                        }else if (stream->dir==SalStreamSendOnly){
@@ -1040,16 +1538,21 @@ static void linphone_call_start_audio_stream(LinphoneCall *call, const char *cna
                                captcard=playcard=NULL;
                        }
                        use_ec=captcard==NULL ? FALSE : linphone_core_echo_cancellation_enabled(lc);
-
+                       if (playcard &&  stream->max_rate>0) ms_snd_card_set_preferred_sample_rate(playcard, stream->max_rate);
+                       if (captcard &&  stream->max_rate>0) ms_snd_card_set_preferred_sample_rate(captcard, stream->max_rate);
                        audio_stream_enable_adaptive_bitrate_control(call->audiostream,use_arc);
+                       audio_stream_enable_adaptive_jittcomp(call->audiostream, linphone_core_audio_adaptive_jittcomp_enabled(lc));
+                       if (!call->params.in_conference && call->params.record_file)
+                               audio_stream_mixed_record_open(call->audiostream,call->params.record_file);
                        audio_stream_start_full(
                                call->audiostream,
                                call->audio_profile,
-                               stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
-                               stream->port,
-                               linphone_core_rtcp_enabled(lc) ? (stream->port+1) : 0,
+                               stream->rtp_addr[0]!='\0' ? stream->rtp_addr : call->resultdesc->addr,
+                               stream->rtp_port,
+                               stream->rtcp_addr[0]!='\0' ? stream->rtcp_addr : call->resultdesc->addr,
+                               linphone_core_rtcp_enabled(lc) ? (stream->rtcp_port) : 0,
                                used_pt,
-                               jitt_comp,
+                               linphone_core_get_audio_jittcomp(lc),
                                playfile,
                                recfile,
                                playcard,
@@ -1067,11 +1570,33 @@ static void linphone_call_start_audio_stream(LinphoneCall *call, const char *cna
                        if (send_ringbacktone){
                                setup_ring_player(lc,call);
                        }
-                       audio_stream_set_rtcp_information(call->audiostream, cname, LINPHONE_RTCP_SDES_TOOL);
+                       audio_stream_set_rtcp_information(call->audiostream, cname, rtcp_tool);
+                       
+                       /* valid local tags are > 0 */
+                       if (stream->proto == SalProtoRtpSavp) {
+                       const SalStreamDescription *local_st_desc=sal_media_description_find_stream(call->localdesc,
+                                                                                                       SalProtoRtpSavp,SalAudio);
+                       int crypto_idx = find_crypto_index_from_tag(local_st_desc->crypto, stream->crypto_local_tag);
+
+                       if (crypto_idx >= 0) {
+                               audio_stream_enable_srtp(
+                                                       call->audiostream, 
+                                                       stream->crypto[0].algo,
+                                                       local_st_desc->crypto[crypto_idx].master_key,
+                                                       stream->crypto[0].master_key);
+                               call->audiostream_encrypted=TRUE;
+                       } else {
+                               ms_warning("Failed to find local crypto algo with tag: %d", stream->crypto_local_tag);
+                               call->audiostream_encrypted=FALSE;
+                       }
+                       }else call->audiostream_encrypted=FALSE;
                        if (call->params.in_conference){
                                /*transform the graph to connect it to the conference filter */
-                               linphone_call_add_to_conf(call);
+                               bool_t mute=stream->dir==SalStreamRecvOnly;
+                               linphone_call_add_to_conf(call, mute);
                        }
+                       call->current_params.in_conference=call->params.in_conference;
+                       call->current_params.low_bandwidth=call->params.low_bandwidth;
                }else ms_warning("No audio stream accepted ?");
        }
 }
@@ -1080,18 +1605,29 @@ static void linphone_call_start_video_stream(LinphoneCall *call, const char *cna
 #ifdef VIDEO_ENABLED
        LinphoneCore *lc=call->core;
        int used_pt=-1;
+       /* look for savp stream first */
        const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
-                                                       SalProtoRtpAvp,SalVideo);
+                                               SalProtoRtpSavp,SalVideo);
+       char rtcp_tool[128]={0};
+       snprintf(rtcp_tool,sizeof(rtcp_tool)-1,"%s-%s",linphone_core_get_user_agent_name(),linphone_core_get_user_agent_version());
+       
+       /* no savp audio stream, use avp */
+       if (!vstream)
+               vstream=sal_media_description_find_stream(call->resultdesc,
+                                               SalProtoRtpAvp,SalVideo);
+                                               
        /* shutdown preview */
        if (lc->previewstream!=NULL) {
                video_preview_stop(lc->previewstream);
                lc->previewstream=NULL;
        }
-       call->current_params.has_video=FALSE;
-       if (vstream!=NULL && vstream->dir!=SalStreamInactive && vstream->port!=0) {
-               const char *addr=vstream->addr[0]!='\0' ? vstream->addr : call->resultdesc->addr;
+       
+       if (vstream!=NULL && vstream->dir!=SalStreamInactive && vstream->rtp_port!=0) {
+               const char *rtp_addr=vstream->rtp_addr[0]!='\0' ? vstream->rtp_addr : call->resultdesc->addr;
+               const char *rtcp_addr=vstream->rtcp_addr[0]!='\0' ? vstream->rtcp_addr : call->resultdesc->addr;
                call->video_profile=make_profile(call,call->resultdesc,vstream,&used_pt);
                if (used_pt!=-1){
+                       call->current_params.video_codec = rtp_profile_get_payload(call->video_profile, used_pt);
                        VideoStreamDir dir=VideoStreamSendRecv;
                        MSWebCam *cam=lc->video_conf.device;
                        bool_t is_inactive=FALSE;
@@ -1100,6 +1636,7 @@ static void linphone_call_start_video_stream(LinphoneCall *call, const char *cna
 
                        video_stream_enable_adaptive_bitrate_control(call->videostream,
                                                                  linphone_core_adaptive_rate_control_enabled(lc));
+                       video_stream_enable_adaptive_jittcomp(call->videostream, linphone_core_video_adaptive_jittcomp_enabled(lc));
                        video_stream_set_sent_video_size(call->videostream,linphone_core_get_preferred_video_size(lc));
                        video_stream_enable_self_view(call->videostream,lc->video_conf.selfview);
                        if (lc->video_window_id!=0)
@@ -1129,14 +1666,30 @@ static void linphone_call_start_video_stream(LinphoneCall *call, const char *cna
                                cam=get_nowebcam_device();
                        }
                        if (!is_inactive){
+                               call->log->video_enabled = TRUE;
                                video_stream_set_direction (call->videostream, dir);
                                ms_message("%s lc rotation:%d\n", __FUNCTION__, lc->device_rotation);
                                video_stream_set_device_rotation(call->videostream, lc->device_rotation);
                                video_stream_start(call->videostream,
-                                       call->video_profile, addr, vstream->port,
-                                       linphone_core_rtcp_enabled(lc) ? (vstream->port+1) : 0,
-                                       used_pt, lc->rtp_conf.audio_jitt_comp, cam);
-                               video_stream_set_rtcp_information(call->videostream, cname,LINPHONE_RTCP_SDES_TOOL);
+                                       call->video_profile, rtp_addr, vstream->rtp_port,
+                                       rtcp_addr, linphone_core_rtcp_enabled(lc) ? (vstream->rtcp_port) : 0,
+                                       used_pt, linphone_core_get_video_jittcomp(lc), cam);
+                               video_stream_set_rtcp_information(call->videostream, cname,rtcp_tool);
+                       }
+                       
+                       if (vstream->proto == SalProtoRtpSavp) {
+                               const SalStreamDescription *local_st_desc=sal_media_description_find_stream(call->localdesc,
+                                               SalProtoRtpSavp,SalVideo);
+                                               
+                               video_stream_enable_strp(
+                                       call->videostream, 
+                                       vstream->crypto[0].algo,
+                                       local_st_desc->crypto[0].master_key, 
+                                       vstream->crypto[0].master_key
+                                       );
+                               call->videostream_encrypted=TRUE;
+                       }else{
+                               call->videostream_encrypted=FALSE;
                        }
                }else ms_warning("No video stream accepted.");
        }else{
@@ -1147,21 +1700,22 @@ static void linphone_call_start_video_stream(LinphoneCall *call, const char *cna
 
 void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_muted, bool_t send_ringbacktone){
        LinphoneCore *lc=call->core;
+
+       call->current_params.audio_codec = NULL;
+       call->current_params.video_codec = NULL;
+
        LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
        char *cname;
-       bool_t use_arc;
+       bool_t use_arc=linphone_core_adaptive_rate_control_enabled(lc);
 #ifdef VIDEO_ENABLED
        const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
                                                        SalProtoRtpAvp,SalVideo);
 #endif
 
-       if(call->audiostream == NULL)
-       {
+       if ((call->audiostream == NULL) && (call->videostream == NULL)) {
                ms_fatal("start_media_stream() called without prior init !");
                return;
        }
-       call->current_params = call->params;
-       if (call->media_start_time==0) call->media_start_time=time(NULL);
        cname=linphone_address_as_string_uri_only(me);
 
 #if defined(VIDEO_ENABLED)
@@ -1170,7 +1724,10 @@ void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_mut
                use_arc=FALSE;
        }
 #endif
-       linphone_call_start_audio_stream(call,cname,all_inputs_muted,send_ringbacktone,use_arc);
+       if (call->audiostream!=NULL) {
+               linphone_call_start_audio_stream(call,cname,all_inputs_muted,send_ringbacktone,use_arc);
+       }
+       call->current_params.has_video=FALSE;
        if (call->videostream!=NULL) {
                linphone_call_start_video_stream(call,cname,all_inputs_muted);
        }
@@ -1179,11 +1736,23 @@ void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_mut
        call->playing_ringbacktone=send_ringbacktone;
        call->up_bw=linphone_core_get_upload_bandwidth(lc);
 
-       if (ortp_zrtp_available()) {
+       if (call->params.media_encryption==LinphoneMediaEncryptionZRTP) {
                OrtpZrtpParams params;
-               params.zid=get_hexa_zrtp_identifier(lc);
+               /*will be set later when zrtp is activated*/
+               call->current_params.media_encryption=LinphoneMediaEncryptionNone;
+               
                params.zid_file=lc->zrtp_secrets_cache;
                audio_stream_enable_zrtp(call->audiostream,&params);
+       }else if (call->params.media_encryption==LinphoneMediaEncryptionSRTP){
+               call->current_params.media_encryption=linphone_call_are_all_streams_encrypted(call) ?
+                       LinphoneMediaEncryptionSRTP : LinphoneMediaEncryptionNone;
+       }
+
+       /*also reflect the change if the "wished" params, in order to avoid to propose SAVP or video again
+        * further in the call, for example during pause,resume, conferencing reINVITEs*/
+       linphone_call_fix_call_parameters(call);
+       if ((call->ice_session != NULL) && (ice_session_state(call->ice_session) != IS_Completed)) {
+               ice_session_start_connectivity_checks(call->ice_session);
        }
 
        goto end;
@@ -1192,43 +1761,153 @@ void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_mut
                linphone_address_destroy(me);
 }
 
-static void linphone_call_log_fill_stats(LinphoneCallLog *log, AudioStream *st){
-       audio_stream_get_local_rtp_stats (st,&log->local_stats);
-       log->quality=audio_stream_get_average_quality_rating(st);
+void linphone_call_start_media_streams_for_ice_gathering(LinphoneCall *call){
+       audio_stream_prepare_sound(call->audiostream, NULL, NULL);
+#ifdef VIDEO_ENABLED
+       if (call->videostream) {
+               video_stream_prepare_video(call->videostream);
+       }
+#endif
 }
 
-void linphone_call_stop_media_streams(LinphoneCall *call){
+void linphone_call_stop_media_streams_for_ice_gathering(LinphoneCall *call){
+       audio_stream_unprepare_sound(call->audiostream);
+#ifdef VIDEO_ENABLED
+       if (call->videostream) {
+               video_stream_unprepare_video(call->videostream);
+       }
+#endif
+}
+
+void linphone_call_update_crypto_parameters(LinphoneCall *call, SalMediaDescription *old_md, SalMediaDescription *new_md) {
+       SalStreamDescription *old_stream;
+       SalStreamDescription *new_stream;
+       int i;
+
+       old_stream = sal_media_description_find_stream(old_md, SalProtoRtpSavp, SalAudio);
+       new_stream = sal_media_description_find_stream(new_md, SalProtoRtpSavp, SalAudio);
+       if (old_stream && new_stream) {
+               const SalStreamDescription *local_st_desc = sal_media_description_find_stream(call->localdesc, SalProtoRtpSavp, SalAudio);
+               if (local_st_desc) {
+                       int crypto_idx = find_crypto_index_from_tag(local_st_desc->crypto, new_stream->crypto_local_tag);
+                       if (crypto_idx >= 0) {
+                               audio_stream_enable_srtp(call->audiostream, new_stream->crypto[0].algo, local_st_desc->crypto[crypto_idx].master_key, new_stream->crypto[0].master_key);
+                               call->audiostream_encrypted = TRUE;
+                       } else {
+                               ms_warning("Failed to find local crypto algo with tag: %d", new_stream->crypto_local_tag);
+                               call->audiostream_encrypted = FALSE;
+                       }
+                       for (i = 0; i < SAL_CRYPTO_ALGO_MAX; i++) {
+                               old_stream->crypto[i].tag = new_stream->crypto[i].tag;
+                               old_stream->crypto[i].algo = new_stream->crypto[i].algo;
+                               strncpy(old_stream->crypto[i].master_key, new_stream->crypto[i].master_key, sizeof(old_stream->crypto[i].master_key) - 1);
+                       }
+               }
+       }
+
+#ifdef VIDEO_ENABLED
+       old_stream = sal_media_description_find_stream(old_md, SalProtoRtpSavp, SalVideo);
+       new_stream = sal_media_description_find_stream(new_md, SalProtoRtpSavp, SalVideo);
+       if (old_stream && new_stream) {
+               const SalStreamDescription *local_st_desc = sal_media_description_find_stream(call->localdesc, SalProtoRtpSavp, SalVideo);
+               if (local_st_desc) {
+                       int crypto_idx = find_crypto_index_from_tag(local_st_desc->crypto, new_stream->crypto_local_tag);
+                       if (crypto_idx >= 0) {
+                               video_stream_enable_strp(call->videostream, new_stream->crypto[0].algo, local_st_desc->crypto[crypto_idx].master_key, new_stream->crypto[0].master_key);
+                               call->videostream_encrypted = TRUE;
+                       } else {
+                               ms_warning("Failed to find local crypto algo with tag: %d", new_stream->crypto_local_tag);
+                               call->videostream_encrypted = FALSE;
+                       }
+                       for (i = 0; i < SAL_CRYPTO_ALGO_MAX; i++) {
+                               old_stream->crypto[i].tag = new_stream->crypto[i].tag;
+                               old_stream->crypto[i].algo = new_stream->crypto[i].algo;
+                               strncpy(old_stream->crypto[i].master_key, new_stream->crypto[i].master_key, sizeof(old_stream->crypto[i].master_key) - 1);
+                       }
+               }
+       }
+#endif
+}
+
+void linphone_call_update_remote_session_id_and_ver(LinphoneCall *call) {
+       SalMediaDescription *remote_desc = sal_call_get_remote_media_description(call->op);
+       if (remote_desc) {
+               call->remote_session_id = remote_desc->session_id;
+               call->remote_session_ver = remote_desc->session_ver;
+       }
+}
+
+void linphone_call_delete_ice_session(LinphoneCall *call){
+       if (call->ice_session != NULL) {
+               ice_session_destroy(call->ice_session);
+               call->ice_session = NULL;
+               if (call->audiostream != NULL) call->audiostream->ms.ice_check_list = NULL;
+               if (call->videostream != NULL) call->videostream->ms.ice_check_list = NULL;
+               call->stats[LINPHONE_CALL_STATS_AUDIO].ice_state = LinphoneIceStateNotActivated;
+               call->stats[LINPHONE_CALL_STATS_VIDEO].ice_state = LinphoneIceStateNotActivated;
+       }
+}
+
+#ifdef BUILD_UPNP
+void linphone_call_delete_upnp_session(LinphoneCall *call){
+       if(call->upnp_session!=NULL) {
+               linphone_upnp_session_destroy(call->upnp_session);
+               call->upnp_session=NULL;
+       }
+}
+#endif //BUILD_UPNP
+
+static void linphone_call_log_fill_stats(LinphoneCallLog *log, MediaStream *st){
+       float quality=media_stream_get_average_quality_rating(st);
+       if (quality>=0){
+               if (log->quality!=-1){
+                       log->quality*=quality/5.0;
+               }else log->quality=quality;
+       }
+}
+
+void linphone_call_stop_audio_stream(LinphoneCall *call) {
        if (call->audiostream!=NULL) {
-               rtp_session_unregister_event_queue(call->audiostream->session,call->audiostream_app_evq);
+               rtp_session_unregister_event_queue(call->audiostream->ms.session,call->audiostream_app_evq);
                ortp_ev_queue_flush(call->audiostream_app_evq);
                ortp_ev_queue_destroy(call->audiostream_app_evq);
+               call->audiostream_app_evq=NULL;
 
                if (call->audiostream->ec){
                        const char *state_str=NULL;
                        ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_STATE_STRING,&state_str);
                        if (state_str){
-                               ms_message("Writing echo canceller state, %i bytes",(int)strlen(state_str));
+                               ms_message("Writing echo canceler state, %i bytes",(int)strlen(state_str));
                                lp_config_set_string(call->core->config,"sound","ec_state",state_str);
                        }
                }
-               linphone_call_log_fill_stats (call->log,call->audiostream);
+               audio_stream_get_local_rtp_stats(call->audiostream,&call->log->local_stats);
+               linphone_call_log_fill_stats (call->log,(MediaStream*)call->audiostream);
                if (call->endpoint){
                        linphone_call_remove_from_conf(call);
                }
                audio_stream_stop(call->audiostream);
                call->audiostream=NULL;
        }
+}
 
-
+void linphone_call_stop_video_stream(LinphoneCall *call) {
 #ifdef VIDEO_ENABLED
        if (call->videostream!=NULL){
-               rtp_session_unregister_event_queue(call->videostream->session,call->videostream_app_evq);
+               rtp_session_unregister_event_queue(call->videostream->ms.session,call->videostream_app_evq);
                ortp_ev_queue_flush(call->videostream_app_evq);
                ortp_ev_queue_destroy(call->videostream_app_evq);
+               call->videostream_app_evq=NULL;
+               linphone_call_log_fill_stats(call->log,(MediaStream*)call->videostream);
                video_stream_stop(call->videostream);
                call->videostream=NULL;
        }
 #endif
+}
+
+void linphone_call_stop_media_streams(LinphoneCall *call){
+       linphone_call_stop_audio_stream(call);
+       linphone_call_stop_video_stream(call);
        ms_event_queue_skip(call->core->msevq);
        
        if (call->audio_profile){
@@ -1289,7 +1968,7 @@ bool_t linphone_call_echo_limiter_enabled(const LinphoneCall *call){
 **/
 
 /**
- * Returns the measured sound volume played locally (received from remote)
+ * Returns the measured sound volume played locally (received from remote).
  * It is expressed in dbm0.
 **/
 float linphone_call_get_play_volume(LinphoneCall *call){
@@ -1304,7 +1983,7 @@ float linphone_call_get_play_volume(LinphoneCall *call){
 }
 
 /**
- * Returns the measured sound volume recorded locally (sent to remote)
+ * Returns the measured sound volume recorded locally (sent to remote).
  * It is expressed in dbm0.
 **/
 float linphone_call_get_record_volume(LinphoneCall *call){
@@ -1336,10 +2015,20 @@ float linphone_call_get_record_volume(LinphoneCall *call){
  * active audio stream exist. Otherwise it returns the quality rating.
 **/
 float linphone_call_get_current_quality(LinphoneCall *call){
+       float audio_rating=-1;
+       float video_rating=-1;
+       float result;
        if (call->audiostream){
-               return audio_stream_get_quality_rating(call->audiostream);
+               audio_rating=media_stream_get_quality_rating((MediaStream*)call->audiostream)/5.0;
        }
-       return -1;
+       if (call->videostream){
+               video_rating=media_stream_get_quality_rating((MediaStream*)call->videostream)/5.0;
+       }
+       if (audio_rating<0 && video_rating<0) result=-1;
+       else if (audio_rating<0) result=video_rating*5.0;
+       else if (video_rating<0) result=audio_rating*5.0;
+       else result=audio_rating*video_rating*5.0;
+       return result;
 }
 
 /**
@@ -1354,16 +2043,99 @@ float linphone_call_get_average_quality(LinphoneCall *call){
        return -1;
 }
 
+static void update_local_stats(LinphoneCallStats *stats, MediaStream *stream){
+       const MSQualityIndicator *qi=media_stream_get_quality_indicator(stream);
+       if (qi) {
+               stats->local_late_rate=ms_quality_indicator_get_local_late_rate(qi);
+               stats->local_loss_rate=ms_quality_indicator_get_local_loss_rate(qi);
+       }
+}
+
+/**
+ * Access last known statistics for audio stream, for a given call.
+**/
+const LinphoneCallStats *linphone_call_get_audio_stats(LinphoneCall *call) {
+       LinphoneCallStats *stats=&call->stats[LINPHONE_CALL_STATS_AUDIO];
+       if (call->audiostream){
+               update_local_stats(stats,(MediaStream*)call->audiostream);
+       }
+       return stats;
+}
+
+/**
+ * Access last known statistics for video stream, for a given call.
+**/
+const LinphoneCallStats *linphone_call_get_video_stats(LinphoneCall *call) {
+       LinphoneCallStats *stats=&call->stats[LINPHONE_CALL_STATS_VIDEO];
+       if (call->videostream){
+               update_local_stats(stats,(MediaStream*)call->videostream);
+       }
+       return stats;
+}
+
+/**
+ * Enable recording of the call (voice-only).
+ * This function must be used before the call parameters are assigned to the call.
+ * The call recording can be started and paused after the call is established with
+ * linphone_call_start_recording() and linphone_call_pause_recording().
+ * @param cp the call parameters
+ * @param path path and filename of the file where audio is written.
+**/
+void linphone_call_params_set_record_file(LinphoneCallParams *cp, const char *path){
+       if (cp->record_file){
+               ms_free(cp->record_file);
+               cp->record_file=NULL;
+       }
+       if (path) cp->record_file=ms_strdup(path);
+}
+
+/**
+ * Retrieves the path for the audio recoding of the call.
+**/
+const char *linphone_call_params_get_record_file(const LinphoneCallParams *cp){
+       return cp->record_file;
+}
+
+/**
+ * Start call recording.
+ * The output file where audio is recorded must be previously specified with linphone_call_params_set_record_file().
+**/
+void linphone_call_start_recording(LinphoneCall *call){
+       if (!call->params.record_file){
+               ms_error("linphone_call_start_recording(): no output file specified. Use linphone_call_params_set_record_file().");
+               return;
+       }
+       if (call->audiostream && !call->params.in_conference){
+               audio_stream_mixed_record_start(call->audiostream);
+       }
+       call->record_active=TRUE;
+}
+
+/**
+ * Stop call recording.
+**/
+void linphone_call_stop_recording(LinphoneCall *call){
+       if (call->audiostream && !call->params.in_conference){
+               audio_stream_mixed_record_stop(call->audiostream);
+       }
+       call->record_active=FALSE;
+}
+
 /**
  * @}
 **/
 
-static void display_bandwidth(RtpSession *as, RtpSession *vs){
+static void report_bandwidth(LinphoneCall *call, RtpSession *as, RtpSession *vs){
+       call->stats[LINPHONE_CALL_STATS_AUDIO].download_bandwidth=(as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0;
+       call->stats[LINPHONE_CALL_STATS_AUDIO].upload_bandwidth=(as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0;
+       call->stats[LINPHONE_CALL_STATS_VIDEO].download_bandwidth=(vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0;
+       call->stats[LINPHONE_CALL_STATS_VIDEO].upload_bandwidth=(vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0;
        ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
-       (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
-       (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
-       (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
-       (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
+               call->stats[LINPHONE_CALL_STATS_AUDIO].download_bandwidth,
+               call->stats[LINPHONE_CALL_STATS_AUDIO].upload_bandwidth ,
+               call->stats[LINPHONE_CALL_STATS_VIDEO].download_bandwidth,
+               call->stats[LINPHONE_CALL_STATS_VIDEO].upload_bandwidth
+       );
 }
 
 static void linphone_core_disconnected(LinphoneCore *lc, LinphoneCall *call){
@@ -1374,7 +2146,7 @@ static void linphone_core_disconnected(LinphoneCore *lc, LinphoneCall *call){
        if (from)
        {
                snprintf(temp,sizeof(temp),"Remote end %s seems to have disconnected, the call is going to be closed.",from);
-               free(from);
+               ms_free(from);
        }
        else
        {
@@ -1383,9 +2155,79 @@ static void linphone_core_disconnected(LinphoneCore *lc, LinphoneCall *call){
        if (lc->vtable.display_warning!=NULL)
                lc->vtable.display_warning(lc,temp);
        linphone_core_terminate_call(lc,call);
+       linphone_core_play_named_tone(lc,LinphoneToneCallFailed);
+}
+
+static void handle_ice_events(LinphoneCall *call, OrtpEvent *ev){
+       OrtpEventType evt=ortp_event_get_type(ev);
+       OrtpEventData *evd=ortp_event_get_data(ev);
+       int ping_time;
+
+       if (evt == ORTP_EVENT_ICE_SESSION_PROCESSING_FINISHED) {
+               switch (ice_session_state(call->ice_session)) {
+                       case IS_Completed:
+                               ice_session_select_candidates(call->ice_session);
+                               if (ice_session_role(call->ice_session) == IR_Controlling) {
+                                       linphone_core_update_call(call->core, call, &call->current_params);
+                               }
+                               break;
+                       case IS_Failed:
+                               if (ice_session_has_completed_check_list(call->ice_session) == TRUE) {
+                                       ice_session_select_candidates(call->ice_session);
+                                       if (ice_session_role(call->ice_session) == IR_Controlling) {
+                                               /* At least one ICE session has succeeded, so perform a call update. */
+                                               linphone_core_update_call(call->core, call, &call->current_params);
+                                       }
+                               }
+                               break;
+                       default:
+                               break;
+               }
+               linphone_core_update_ice_state_in_call_stats(call);
+       } else if (evt == ORTP_EVENT_ICE_GATHERING_FINISHED) {
+
+               if (evd->info.ice_processing_successful==TRUE) {
+                       ice_session_compute_candidates_foundations(call->ice_session);
+                       ice_session_eliminate_redundant_candidates(call->ice_session);
+                       ice_session_choose_default_candidates(call->ice_session);
+                       ping_time = ice_session_average_gathering_round_trip_time(call->ice_session);
+                       if (ping_time >=0) {
+                               call->ping_time=ping_time;
+                       }
+               } else {
+                       ms_warning("No STUN answer from [%s], disabling ICE",linphone_core_get_stun_server(call->core));
+                       linphone_call_delete_ice_session(call);
+               }
+               switch (call->state) {
+                       case LinphoneCallUpdating:
+                               linphone_core_start_update_call(call->core, call);
+                               break;
+                       case LinphoneCallUpdatedByRemote:
+                               linphone_core_start_accept_call_update(call->core, call);
+                               break;
+                       case LinphoneCallOutgoingInit:
+                               linphone_call_stop_media_streams_for_ice_gathering(call);
+                               linphone_core_proceed_with_invite_if_ready(call->core, call, NULL);
+                               break;
+                       case LinphoneCallIdle:
+                               linphone_call_stop_media_streams_for_ice_gathering(call);
+                               linphone_core_notify_incoming_call(call->core, call);
+                               break;
+                       default:
+                               break;
+               }
+       } else if (evt == ORTP_EVENT_ICE_LOSING_PAIRS_COMPLETED) {
+               linphone_core_start_accept_call_update(call->core, call);
+               linphone_core_update_ice_state_in_call_stats(call);
+       } else if (evt == ORTP_EVENT_ICE_RESTART_NEEDED) {
+               ice_session_restart(call->ice_session);
+               ice_session_set_role(call->ice_session, IR_Controlling);
+               linphone_core_update_call(call->core, call, &call->current_params);
+       }
 }
 
 void linphone_call_background_tasks(LinphoneCall *call, bool_t one_second_elapsed){
+       LinphoneCore* lc = call->core;
        int disconnect_timeout = linphone_core_get_nortp_timeout(call->core);
        bool_t disconnected=FALSE;
 
@@ -1393,55 +2235,107 @@ void linphone_call_background_tasks(LinphoneCall *call, bool_t one_second_elapse
                RtpSession *as=NULL,*vs=NULL;
                float audio_load=0, video_load=0;
                if (call->audiostream!=NULL){
-                       as=call->audiostream->session;
-                       if (call->audiostream->ticker)
-                               audio_load=ms_ticker_get_average_load(call->audiostream->ticker);
+                       as=call->audiostream->ms.session;
+                       if (call->audiostream->ms.ticker)
+                               audio_load=ms_ticker_get_average_load(call->audiostream->ms.ticker);
                }
                if (call->videostream!=NULL){
-                       if (call->videostream->ticker)
-                               video_load=ms_ticker_get_average_load(call->videostream->ticker);
-                       vs=call->videostream->session;
+                       if (call->videostream->ms.ticker)
+                               video_load=ms_ticker_get_average_load(call->videostream->ms.ticker);
+                       vs=call->videostream->ms.session;
                }
-               display_bandwidth(as,vs);
+               report_bandwidth(call,as,vs);
                ms_message("Thread processing load: audio=%f\tvideo=%f",audio_load,video_load);
        }
+
+#ifdef BUILD_UPNP
+       linphone_upnp_call_process(call);
+#endif //BUILD_UPNP
+
 #ifdef VIDEO_ENABLED
        if (call->videostream!=NULL) {
+               OrtpEvent *ev;
+
+               /* Ensure there is no dangling ICE check list. */
+               if (call->ice_session == NULL) call->videostream->ms.ice_check_list = NULL;
+
                // Beware that the application queue should not depend on treatments fron the
                // mediastreamer queue.
                video_stream_iterate(call->videostream);
 
-               if (call->videostream_app_evq){
-                       OrtpEvent *ev;
-                       while (NULL != (ev=ortp_ev_queue_get(call->videostream_app_evq))){
-                               OrtpEventType evt=ortp_event_get_type(ev);
-                               if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
-                                       OrtpEventData *evd=ortp_event_get_data(ev);
-                                       linphone_call_videostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
-                               }
-                               ortp_event_destroy(ev);
+               while (call->videostream_app_evq && (NULL != (ev=ortp_ev_queue_get(call->videostream_app_evq)))){
+                       OrtpEventType evt=ortp_event_get_type(ev);
+                       OrtpEventData *evd=ortp_event_get_data(ev);
+                       if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
+                               linphone_call_videostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
+                       } else if (evt == ORTP_EVENT_RTCP_PACKET_RECEIVED) {
+                               call->stats[LINPHONE_CALL_STATS_VIDEO].round_trip_delay = rtp_session_get_round_trip_propagation(call->videostream->ms.session);
+                               if(call->stats[LINPHONE_CALL_STATS_VIDEO].received_rtcp != NULL)
+                                       freemsg(call->stats[LINPHONE_CALL_STATS_VIDEO].received_rtcp);
+                               call->stats[LINPHONE_CALL_STATS_VIDEO].received_rtcp = evd->packet;
+                               evd->packet = NULL;
+                               update_local_stats(&call->stats[LINPHONE_CALL_STATS_VIDEO],(MediaStream*)call->videostream);
+                               if (lc->vtable.call_stats_updated)
+                                       lc->vtable.call_stats_updated(lc, call, &call->stats[LINPHONE_CALL_STATS_VIDEO]);
+                       } else if (evt == ORTP_EVENT_RTCP_PACKET_EMITTED) {
+                               memcpy(&call->stats[LINPHONE_CALL_STATS_VIDEO].jitter_stats, rtp_session_get_jitter_stats(call->videostream->ms.session), sizeof(jitter_stats_t));
+                               if(call->stats[LINPHONE_CALL_STATS_VIDEO].sent_rtcp != NULL)
+                                       freemsg(call->stats[LINPHONE_CALL_STATS_VIDEO].sent_rtcp);
+                               call->stats[LINPHONE_CALL_STATS_VIDEO].sent_rtcp = evd->packet;
+                               evd->packet = NULL;
+                               update_local_stats(&call->stats[LINPHONE_CALL_STATS_VIDEO],(MediaStream*)call->videostream);
+                               if (lc->vtable.call_stats_updated)
+                                       lc->vtable.call_stats_updated(lc, call, &call->stats[LINPHONE_CALL_STATS_VIDEO]);
+                       } else if ((evt == ORTP_EVENT_ICE_SESSION_PROCESSING_FINISHED) || (evt == ORTP_EVENT_ICE_GATHERING_FINISHED)
+                               || (evt == ORTP_EVENT_ICE_LOSING_PAIRS_COMPLETED) || (evt == ORTP_EVENT_ICE_RESTART_NEEDED)) {
+                               handle_ice_events(call, ev);
                        }
+                       ortp_event_destroy(ev);
                }
        }
 #endif
        if (call->audiostream!=NULL) {
+               OrtpEvent *ev;
+
+               /* Ensure there is no dangling ICE check list. */
+               if (call->ice_session == NULL) call->audiostream->ms.ice_check_list = NULL;
+
                // Beware that the application queue should not depend on treatments fron the
                // mediastreamer queue.
                audio_stream_iterate(call->audiostream);
 
-               if (call->audiostream->evq){
-                       OrtpEvent *ev;
-                       while (NULL != (ev=ortp_ev_queue_get(call->audiostream_app_evq))){
-                               OrtpEventType evt=ortp_event_get_type(ev);
-                               if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
-                                       OrtpEventData *evd=ortp_event_get_data(ev);
-                                       linphone_call_audiostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
-                               } else if (evt == ORTP_EVENT_ZRTP_SAS_READY) {
-                                       OrtpEventData *evd=ortp_event_get_data(ev);
-                                       linphone_call_audiostream_auth_token_ready(call, evd->info.zrtp_sas.sas, evd->info.zrtp_sas.verified);
-                               }
-                               ortp_event_destroy(ev);
+               while (call->audiostream_app_evq && (NULL != (ev=ortp_ev_queue_get(call->audiostream_app_evq)))){
+                       OrtpEventType evt=ortp_event_get_type(ev);
+                       OrtpEventData *evd=ortp_event_get_data(ev);
+                       if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
+                               linphone_call_audiostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
+                       } else if (evt == ORTP_EVENT_ZRTP_SAS_READY) {
+                               linphone_call_audiostream_auth_token_ready(call, evd->info.zrtp_sas.sas, evd->info.zrtp_sas.verified);
+                       } else if (evt == ORTP_EVENT_RTCP_PACKET_RECEIVED) {
+                               call->stats[LINPHONE_CALL_STATS_AUDIO].round_trip_delay = rtp_session_get_round_trip_propagation(call->audiostream->ms.session);
+                               if(call->stats[LINPHONE_CALL_STATS_AUDIO].received_rtcp != NULL)
+                                       freemsg(call->stats[LINPHONE_CALL_STATS_AUDIO].received_rtcp);
+                               call->stats[LINPHONE_CALL_STATS_AUDIO].received_rtcp = evd->packet;
+                               evd->packet = NULL;
+                               update_local_stats(&call->stats[LINPHONE_CALL_STATS_AUDIO],(MediaStream*)call->audiostream);
+                               if (lc->vtable.call_stats_updated)
+                                       lc->vtable.call_stats_updated(lc, call, &call->stats[LINPHONE_CALL_STATS_AUDIO]);
+                       } else if (evt == ORTP_EVENT_RTCP_PACKET_EMITTED) {
+                               memcpy(&call->stats[LINPHONE_CALL_STATS_AUDIO].jitter_stats, rtp_session_get_jitter_stats(call->audiostream->ms.session), sizeof(jitter_stats_t));
+                               if(call->stats[LINPHONE_CALL_STATS_AUDIO].sent_rtcp != NULL)
+                                       freemsg(call->stats[LINPHONE_CALL_STATS_AUDIO].sent_rtcp);
+                               call->stats[LINPHONE_CALL_STATS_AUDIO].sent_rtcp = evd->packet;
+                               evd->packet = NULL;
+                               update_local_stats(&call->stats[LINPHONE_CALL_STATS_AUDIO],(MediaStream*)call->audiostream);
+                               if (lc->vtable.call_stats_updated)
+                                       lc->vtable.call_stats_updated(lc, call, &call->stats[LINPHONE_CALL_STATS_AUDIO]);
+                       } else if ((evt == ORTP_EVENT_ICE_SESSION_PROCESSING_FINISHED) || (evt == ORTP_EVENT_ICE_GATHERING_FINISHED)
+                               || (evt == ORTP_EVENT_ICE_LOSING_PAIRS_COMPLETED) || (evt == ORTP_EVENT_ICE_RESTART_NEEDED)) {
+                               handle_ice_events(call, ev);
+                       } else if (evt==ORTP_EVENT_TELEPHONE_EVENT){
+                               linphone_core_dtmf_received(lc,evd->info.telephone_event);
                        }
+                       ortp_event_destroy(ev);
                }
        }
        if (call->state==LinphoneCallStreamsRunning && one_second_elapsed && call->audiostream!=NULL && disconnect_timeout>0 )
@@ -1482,4 +2376,59 @@ void linphone_call_log_completed(LinphoneCall *call){
        call_logs_write_to_config_file(lc);
 }
 
+LinphoneCallState linphone_call_get_transfer_state(LinphoneCall *call) {
+       return call->transfer_state;
+}
+
+void linphone_call_set_transfer_state(LinphoneCall* call, LinphoneCallState state) {
+       if (state != call->transfer_state) {
+               LinphoneCore* lc = call->core;
+               call->transfer_state = state;
+               if (lc->vtable.transfer_state_changed)
+                       lc->vtable.transfer_state_changed(lc, call, state);
+       }
+}
+
+/**
+ * Returns true if the call is part of the conference.
+ * @ingroup conferencing
+**/
+bool_t linphone_call_is_in_conference(const LinphoneCall *call) {
+       return call->params.in_conference;
+}
+
+
+/**
+ * Perform a zoom of the video displayed during a call.
+ * @param call the call.
+ * @param zoom_factor a floating point number describing the zoom factor. A value 1.0 corresponds to no zoom applied.
+ * @param cx a floating point number pointing the horizontal center of the zoom to be applied. This value should be between 0.0 and 1.0.
+ * @param cy a floating point number pointing the vertical center of the zoom to be applied. This value should be between 0.0 and 1.0.
+ * 
+ * cx and cy are updated in return in case their coordinates were to excentrated for the requested zoom factor. The zoom ensures that all the screen is fullfilled with the video.
+**/
+void linphone_call_zoom_video(LinphoneCall* call, float zoom_factor, float* cx, float* cy) {
+       VideoStream* vstream = call->videostream;
+       if (vstream && vstream->output) {
+               float zoom[3];
+               
+               if (zoom_factor < 1)
+                       zoom_factor = 1;
+               float halfsize = 0.5 * 1.0 / zoom_factor;
+
+               if ((*cx - halfsize) < 0)
+                       *cx = 0 + halfsize;
+               if ((*cx + halfsize) > 1)
+                       *cx = 1 - halfsize;
+               if ((*cy - halfsize) < 0)
+                       *cy = 0 + halfsize;
+               if ((*cy + halfsize) > 1)
+                       *cy = 1 - halfsize;
+       
+               zoom[0] = zoom_factor;
+               zoom[1] = *cx;
+               zoom[2] = *cy;
+               ms_filter_call_method(vstream->output, MS_VIDEO_DISPLAY_ZOOM, &zoom);
+       }else ms_warning("Could not apply zoom: video output wasn't activated.");
+}