]> sjero.net Git - linphone/blob - coreapi/linphonecall.c
encryption support in progress
[linphone] / coreapi / linphonecall.c
1
2 /*
3 linphone
4 Copyright (C) 2010  Belledonne Communications SARL
5  (simon.morlat@linphone.org)
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 #ifdef WIN32
22 #include <time.h>
23 #endif
24 #include "linphonecore.h"
25 #include "sipsetup.h"
26 #include "lpconfig.h"
27 #include "private.h"
28 #include <ortp/event.h>
29 #include <ortp/b64.h>
30
31
32 #include "mediastreamer2/mediastream.h"
33 #include "mediastreamer2/msvolume.h"
34 #include "mediastreamer2/msequalizer.h"
35 #include "mediastreamer2/msfileplayer.h"
36 #include "mediastreamer2/msjpegwriter.h"
37 #include "mediastreamer2/mseventqueue.h"
38
39 #ifdef VIDEO_ENABLED
40 static MSWebCam *get_nowebcam_device(){
41         return ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),"StaticImage: Static picture");
42 }
43 #endif
44
45 static bool_t generate_b64_crypto_key(int key_length, char* key_out) {
46         int b64_size;
47         uint8_t* tmp = (uint8_t*) malloc(key_length);                   
48         if (ortp_crypto_get_random(tmp, key_length)!=0) {
49                 ms_error("Failed to generate random key");
50                 free(tmp);
51                 return FALSE;
52         }
53         
54         b64_size = b64_encode((const char*)tmp, key_length, NULL, 0);
55         if (b64_size == 0) {
56                 ms_error("Failed to b64 encode key");
57                 free(tmp);
58                 return FALSE;
59         }
60         key_out[b64_size] = '\0';
61         b64_encode((const char*)tmp, key_length, key_out, 40);
62         free(tmp);
63         return TRUE;
64 }
65
66 LinphoneCore *linphone_call_get_core(const LinphoneCall *call){
67         return call->core;
68 }
69
70 static const char* get_hexa_zrtp_identifier(LinphoneCore *lc){
71         const char *confZid=lp_config_get_string(lc->config,"rtp","zid",NULL);
72         if (confZid != NULL) {
73                 return confZid;
74         } else {
75         char zidstr[128];
76         snprintf(zidstr,sizeof(zidstr),"%x-%x-%x",rand(),rand(),rand());
77                 lp_config_set_string(lc->config,"rtp","zid",zidstr);
78                 return lp_config_get_string(lc->config,"rtp","zid",NULL);
79         }
80 }
81
82 const char* linphone_call_get_authentication_token(LinphoneCall *call){
83         return call->auth_token;
84 }
85
86 bool_t linphone_call_get_authentication_token_verified(LinphoneCall *call){
87         return call->auth_token_verified;
88 }
89
90 static bool_t linphone_call_are_all_streams_encrypted(LinphoneCall *call) {
91         // Check ZRTP encryption in audiostream
92         if (!call->audiostream_encrypted) {
93                 return FALSE;
94         }
95
96 #ifdef VIDEO_ENABLED
97         // If video enabled, check ZRTP encryption in videostream
98         const LinphoneCallParams *params=linphone_call_get_current_params(call);
99         if (params->has_video && !call->videostream_encrypted) {
100                 return FALSE;
101         }
102 #endif
103
104         return TRUE;
105 }
106
107 void propagate_encryption_changed(LinphoneCall *call){
108         LinphoneCore *lc=call->core;
109         if (!linphone_call_are_all_streams_encrypted(call)) {
110                 ms_message("Some streams are not encrypted");
111                 call->current_params.media_encryption=LinphoneMediaEncryptionNone;
112                 if (lc->vtable.call_encryption_changed)
113                         lc->vtable.call_encryption_changed(call->core, call, FALSE, call->auth_token);
114         } else {
115                 ms_message("All streams are encrypted");
116                 call->current_params.media_encryption=LinphoneMediaEncryptionZRTP;
117                 if (lc->vtable.call_encryption_changed)
118                         lc->vtable.call_encryption_changed(call->core, call, TRUE, call->auth_token);
119         }
120 }
121
122 #ifdef VIDEO_ENABLED
123 static void linphone_call_videostream_encryption_changed(void *data, bool_t encrypted){
124         ms_message("Video stream is %s", encrypted ? "encrypted" : "not encrypted");
125
126         LinphoneCall *call = (LinphoneCall *)data;
127         call->videostream_encrypted=encrypted;
128         propagate_encryption_changed(call);
129 }
130 #endif
131
132 static void linphone_call_audiostream_encryption_changed(void *data, bool_t encrypted) {
133         char status[255]={0};
134         ms_message("Audio stream is %s ", encrypted ? "encrypted" : "not encrypted");
135
136         LinphoneCall *call = (LinphoneCall *)data;
137         call->audiostream_encrypted=encrypted;
138         
139         if (encrypted && call->core->vtable.display_status != NULL) {
140                 snprintf(status,sizeof(status)-1,_("Authentication token is %s"),call->auth_token);
141                  call->core->vtable.display_status(call->core, status);
142         }
143
144         propagate_encryption_changed(call);
145
146
147 #ifdef VIDEO_ENABLED
148         // Enable video encryption
149         const LinphoneCallParams *params=linphone_call_get_current_params(call);
150         if (params->has_video) {
151                 ms_message("Trying to enable encryption on video stream");
152                 OrtpZrtpParams params;
153                 params.zid=get_hexa_zrtp_identifier(call->core);
154                 params.zid_file=NULL; //unused
155                 video_stream_enable_zrtp(call->videostream,call->audiostream,&params);
156         }
157 #endif
158 }
159
160
161 static void linphone_call_audiostream_auth_token_ready(void *data, const char* auth_token, bool_t verified) {
162         LinphoneCall *call=(LinphoneCall *)data;
163         if (call->auth_token != NULL)
164                 ms_free(call->auth_token);
165
166         call->auth_token=ms_strdup(auth_token);
167         call->auth_token_verified=verified;
168
169         ms_message("Authentication token is %s (%s)", auth_token, verified?"verified":"unverified");
170 }
171
172 void linphone_call_set_authentication_token_verified(LinphoneCall *call, bool_t verified){
173         if (call->audiostream==NULL){
174                 ms_error("linphone_call_set_authentication_token_verified(): No audio stream");
175         }
176         if (call->audiostream->ortpZrtpContext==NULL){
177                 ms_error("linphone_call_set_authentication_token_verified(): No zrtp context.");
178         }
179         if (!call->auth_token_verified && verified){
180                 ortp_zrtp_sas_verified(call->audiostream->ortpZrtpContext);
181         }else if (call->auth_token_verified && !verified){
182                 ortp_zrtp_sas_reset_verified(call->audiostream->ortpZrtpContext);
183         }
184         call->auth_token_verified=verified;
185         propagate_encryption_changed(call);
186 }
187
188 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, int bandwidth_limit){
189         MSList *l=NULL;
190         const MSList *it;
191         for(it=codecs;it!=NULL;it=it->next){
192                 PayloadType *pt=(PayloadType*)it->data;
193                 if (pt->flags & PAYLOAD_TYPE_ENABLED){
194                         if (bandwidth_limit>0 && !linphone_core_is_payload_type_usable_for_bandwidth(lc,pt,bandwidth_limit)){
195                                 ms_message("Codec %s/%i eliminated because of audio bandwidth constraint.",pt->mime_type,pt->clock_rate);
196                                 continue;
197                         }
198                         if (linphone_core_check_payload_type_usability(lc,pt)){
199                                 l=ms_list_append(l,payload_type_clone(pt));
200                         }
201                 }
202         }
203         return l;
204 }
205
206 static SalMediaDescription *_create_local_media_description(LinphoneCore *lc, LinphoneCall *call, unsigned int session_id, unsigned int session_ver){
207         MSList *l;
208         PayloadType *pt;
209         int i;
210         const char *me=linphone_core_get_identity(lc);
211         LinphoneAddress *addr=linphone_address_new(me);
212         const char *username=linphone_address_get_username (addr);
213         SalMediaDescription *md=sal_media_description_new();
214
215         md->session_id=session_id;
216         md->session_ver=session_ver;
217         md->nstreams=1;
218         strncpy(md->addr,call->localip,sizeof(md->addr));
219         strncpy(md->username,username,sizeof(md->username));
220         md->bandwidth=linphone_core_get_download_bandwidth(lc);
221
222         /*set audio capabilities */
223         strncpy(md->streams[0].addr,call->localip,sizeof(md->streams[0].addr));
224         md->streams[0].port=call->audio_port;
225         md->streams[0].proto=(call->params.media_encryption == LinphoneMediaEncryptionSRTP) ? 
226                 SalProtoRtpSavp : SalProtoRtpAvp;
227         md->streams[0].type=SalAudio;
228         md->streams[0].ptime=lc->net_conf.down_ptime;
229         l=make_codec_list(lc,lc->codecs_conf.audio_codecs,call->params.audio_bw);
230         pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
231         l=ms_list_append(l,pt);
232         md->streams[0].payloads=l;
233
234
235         if (call->params.has_video){
236                 md->nstreams++;
237                 md->streams[1].port=call->video_port;
238                 md->streams[1].proto=md->streams[0].proto;
239                 md->streams[1].type=SalVideo;
240                 l=make_codec_list(lc,lc->codecs_conf.video_codecs,0);
241                 md->streams[1].payloads=l;
242         }
243         
244         for(i=0; i<md->nstreams; i++) {
245                 if (md->streams[i].proto == SalProtoRtpSavp) {
246                         md->streams[i].crypto[0].tag = 1;
247                         md->streams[i].crypto[0].algo = AES_128_SHA1_80;
248                         if (!generate_b64_crypto_key(30, md->streams[i].crypto[0].master_key))
249                                 md->streams[i].crypto[0].algo = 0;
250                         md->streams[i].crypto[1].tag = 2;
251                         md->streams[i].crypto[1].algo = AES_128_SHA1_32;
252                         if (!generate_b64_crypto_key(30, md->streams[i].crypto[1].master_key))
253                                 md->streams[i].crypto[1].algo = 0;
254                         md->streams[i].crypto[2].algo = 0;
255                 }
256         }
257         
258         linphone_address_destroy(addr);
259         return md;
260 }
261
262 void update_local_media_description(LinphoneCore *lc, LinphoneCall *call, SalMediaDescription **md){
263         if (*md == NULL) {
264                 *md = _create_local_media_description(lc,call,0,0);
265         } else {
266                 unsigned int id = (*md)->session_id;
267                 unsigned int ver = (*md)->session_ver+1;
268                 sal_media_description_unref(*md);
269                 *md = _create_local_media_description(lc,call,id,ver);
270         }
271 }
272
273 SalMediaDescription *create_local_media_description(LinphoneCore *lc, LinphoneCall *call){
274         unsigned int id=rand() & 0xfff;
275         return _create_local_media_description(lc,call,id,id);
276 }
277
278 static int find_port_offset(LinphoneCore *lc){
279         int offset;
280         MSList *elem;
281         int audio_port;
282         bool_t already_used=FALSE;
283         for(offset=0;offset<100;offset+=2){
284                 audio_port=linphone_core_get_audio_port (lc)+offset;
285                 already_used=FALSE;
286                 for(elem=lc->calls;elem!=NULL;elem=elem->next){
287                         LinphoneCall *call=(LinphoneCall*)elem->data;
288                         if (call->audio_port==audio_port) {
289                                 already_used=TRUE;
290                                 break;
291                         }
292                 }
293                 if (!already_used) break;
294         }
295         if (offset==100){
296                 ms_error("Could not find any free port !");
297                 return -1;
298         }
299         return offset;
300 }
301
302 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
303         int port_offset;
304         call->refcnt=1;
305         call->state=LinphoneCallIdle;
306         call->start_time=time(NULL);
307         call->media_start_time=0;
308         call->log=linphone_call_log_new(call, from, to);
309         call->owns_call_log=TRUE;
310         linphone_core_notify_all_friends(call->core,LinphoneStatusOnThePhone);
311         port_offset=find_port_offset (call->core);
312         if (port_offset==-1) return;
313         call->audio_port=linphone_core_get_audio_port(call->core)+port_offset;
314         call->video_port=linphone_core_get_video_port(call->core)+port_offset;
315
316 }
317
318 static void discover_mtu(LinphoneCore *lc, const char *remote){
319         int mtu;
320         if (lc->net_conf.mtu==0 ){
321                 /*attempt to discover mtu*/
322                 mtu=ms_discover_mtu(remote);
323                 if (mtu>0){
324                         ms_set_mtu(mtu);
325                         ms_message("Discovered mtu is %i, RTP payload max size is %i",
326                                 mtu, ms_get_payload_max_size());
327                 }
328         }
329 }
330
331 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, const LinphoneCallParams *params)
332 {
333         LinphoneCall *call=ms_new0(LinphoneCall,1);
334         call->dir=LinphoneCallOutgoing;
335         call->op=sal_op_new(lc->sal);
336         sal_op_set_user_pointer(call->op,call);
337         call->core=lc;
338         linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
339         linphone_call_init_common(call,from,to);
340         call->params=*params;
341         call->localdesc=create_local_media_description (lc,call);
342         call->camera_active=params->has_video;
343         if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
344                 linphone_core_run_stun_tests(call->core,call);
345         discover_mtu(lc,linphone_address_get_domain (to));
346         if (params->referer){
347                 sal_call_set_referer(call->op,params->referer->op);
348         }
349         return call;
350 }
351
352 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
353         LinphoneCall *call=ms_new0(LinphoneCall,1);
354         char *from_str;
355
356         call->dir=LinphoneCallIncoming;
357         sal_op_set_user_pointer(op,call);
358         call->op=op;
359         call->core=lc;
360
361         if (lc->sip_conf.ping_with_options){
362                 /*the following sends an option request back to the caller so that
363                  we get a chance to discover our nat'd address before answering.*/
364                 call->ping_op=sal_op_new(lc->sal);
365                 from_str=linphone_address_as_string(from);
366                 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
367                 sal_op_set_user_pointer(call->ping_op,call);
368                 sal_ping(call->ping_op,linphone_core_find_best_identity(lc,from,NULL),from_str);
369                 ms_free(from_str);
370         }
371
372         linphone_address_clean(from);
373         linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
374         linphone_call_init_common(call, from, to);
375         linphone_core_init_default_params(lc, &call->params);
376         call->localdesc=create_local_media_description (lc,call);
377         call->camera_active=call->params.has_video;
378         if (linphone_core_get_firewall_policy(call->core)==LinphonePolicyUseStun)
379                 linphone_core_run_stun_tests(call->core,call);
380         discover_mtu(lc,linphone_address_get_domain(from));
381         return call;
382 }
383
384 /* this function is called internally to get rid of a call.
385  It performs the following tasks:
386  - remove the call from the internal list of calls
387  - update the call logs accordingly
388 */
389
390 static void linphone_call_set_terminated(LinphoneCall *call){
391         LinphoneCore *lc=call->core;
392
393         linphone_core_update_allocated_audio_bandwidth(lc);
394
395         call->owns_call_log=FALSE;
396         linphone_call_log_completed(call);
397
398
399         if (call == lc->current_call){
400                 ms_message("Resetting the current call");
401                 lc->current_call=NULL;
402         }
403
404         if (linphone_core_del_call(lc,call) != 0){
405                 ms_error("Could not remove the call from the list !!!");
406         }
407
408         if (ms_list_size(lc->calls)==0)
409                 linphone_core_notify_all_friends(lc,lc->presence_mode);
410
411         linphone_core_conference_check_uninit(&lc->conf_ctx);
412         if (call->ringing_beep){
413                 linphone_core_stop_dtmf(lc);
414                 call->ringing_beep=FALSE;
415         }
416 }
417
418 const char *linphone_call_state_to_string(LinphoneCallState cs){
419         switch (cs){
420                 case LinphoneCallIdle:
421                         return "LinphoneCallIdle";
422                 case LinphoneCallIncomingReceived:
423                         return "LinphoneCallIncomingReceived";
424                 case LinphoneCallOutgoingInit:
425                         return "LinphoneCallOutgoingInit";
426                 case LinphoneCallOutgoingProgress:
427                         return "LinphoneCallOutgoingProgress";
428                 case LinphoneCallOutgoingRinging:
429                         return "LinphoneCallOutgoingRinging";
430                 case LinphoneCallOutgoingEarlyMedia:
431                         return "LinphoneCallOutgoingEarlyMedia";
432                 case LinphoneCallConnected:
433                         return "LinphoneCallConnected";
434                 case LinphoneCallStreamsRunning:
435                         return "LinphoneCallStreamsRunning";
436                 case LinphoneCallPausing:
437                         return "LinphoneCallPausing";
438                 case LinphoneCallPaused:
439                         return "LinphoneCallPaused";
440                 case LinphoneCallResuming:
441                         return "LinphoneCallResuming";
442                 case LinphoneCallRefered:
443                         return "LinphoneCallRefered";
444                 case LinphoneCallError:
445                         return "LinphoneCallError";
446                 case LinphoneCallEnd:
447                         return "LinphoneCallEnd";
448                 case LinphoneCallPausedByRemote:
449                         return "LinphoneCallPausedByRemote";
450                 case LinphoneCallUpdatedByRemote:
451                         return "LinphoneCallUpdatedByRemote";
452                 case LinphoneCallIncomingEarlyMedia:
453                         return "LinphoneCallIncomingEarlyMedia";
454                 case LinphoneCallUpdated:
455                         return "LinphoneCallUpdated";
456                 case LinphoneCallReleased:
457                         return "LinphoneCallReleased";
458         }
459         return "undefined state";
460 }
461
462 void linphone_call_set_state(LinphoneCall *call, LinphoneCallState cstate, const char *message){
463         LinphoneCore *lc=call->core;
464
465         if (call->state!=cstate){
466                 if (call->state==LinphoneCallEnd || call->state==LinphoneCallError){
467                         if (cstate!=LinphoneCallReleased){
468                                 ms_warning("Spurious call state change from %s to %s, ignored.",linphone_call_state_to_string(call->state),
469                                    linphone_call_state_to_string(cstate));
470                                 return;
471                         }
472                 }
473                 ms_message("Call %p: moving from state %s to %s",call,linphone_call_state_to_string(call->state),
474                            linphone_call_state_to_string(cstate));
475                 if (cstate!=LinphoneCallRefered){
476                         /*LinphoneCallRefered is rather an event, not a state.
477                          Indeed it does not change the state of the call (still paused or running)*/
478                         call->state=cstate;
479                 }
480                 if (cstate==LinphoneCallEnd || cstate==LinphoneCallError){
481              if (call->reason==LinphoneReasonDeclined){
482                                 call->log->status=LinphoneCallDeclined;
483                         }
484                         linphone_call_set_terminated (call);
485                 }
486                 if (cstate == LinphoneCallConnected) {
487                         call->log->status=LinphoneCallSuccess;
488                         call->media_start_time=time(NULL);
489                 }
490
491                 if (lc->vtable.call_state_changed)
492                         lc->vtable.call_state_changed(lc,call,cstate,message);
493                 if (cstate==LinphoneCallReleased){
494                         if (call->op!=NULL) {
495                                 /* so that we cannot have anymore upcalls for SAL
496                                  concerning this call*/
497                                 sal_op_release(call->op);
498                                 call->op=NULL;
499                         }
500                         linphone_call_unref(call);
501                 }
502         }
503 }
504
505 static void linphone_call_destroy(LinphoneCall *obj)
506 {
507         if (obj->op!=NULL) {
508                 sal_op_release(obj->op);
509                 obj->op=NULL;
510         }
511         if (obj->resultdesc!=NULL) {
512                 sal_media_description_unref(obj->resultdesc);
513                 obj->resultdesc=NULL;
514         }
515         if (obj->localdesc!=NULL) {
516                 sal_media_description_unref(obj->localdesc);
517                 obj->localdesc=NULL;
518         }
519         if (obj->ping_op) {
520                 sal_op_release(obj->ping_op);
521         }
522         if (obj->refer_to){
523                 ms_free(obj->refer_to);
524         }
525         if (obj->owns_call_log)
526                 linphone_call_log_destroy(obj->log);
527         if (obj->auth_token) {
528                 ms_free(obj->auth_token);
529         }
530
531         ms_free(obj);
532 }
533
534 /**
535  * @addtogroup call_control
536  * @{
537 **/
538
539 /**
540  * Increments the call 's reference count.
541  * An application that wishes to retain a pointer to call object
542  * must use this function to unsure the pointer remains
543  * valid. Once the application no more needs this pointer,
544  * it must call linphone_call_unref().
545 **/
546 LinphoneCall * linphone_call_ref(LinphoneCall *obj){
547         obj->refcnt++;
548         return obj;
549 }
550
551 /**
552  * Decrements the call object reference count.
553  * See linphone_call_ref().
554 **/
555 void linphone_call_unref(LinphoneCall *obj){
556         obj->refcnt--;
557         if (obj->refcnt==0){
558                 linphone_call_destroy(obj);
559         }
560 }
561
562 /**
563  * Returns current parameters associated to the call.
564 **/
565 const LinphoneCallParams * linphone_call_get_current_params(const LinphoneCall *call){
566         return &call->current_params;
567 }
568
569 /**
570  * Returns the remote address associated to this call
571  *
572 **/
573 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
574         return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
575 }
576
577 /**
578  * Returns the remote address associated to this call as a string.
579  *
580  * The result string must be freed by user using ms_free().
581 **/
582 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
583         return linphone_address_as_string(linphone_call_get_remote_address(call));
584 }
585
586 /**
587  * Retrieves the call's current state.
588 **/
589 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
590         return call->state;
591 }
592
593 /**
594  * Returns the reason for a call termination (either error or normal termination)
595 **/
596 LinphoneReason linphone_call_get_reason(const LinphoneCall *call){
597         return call->reason;
598 }
599
600 /**
601  * Get the user_pointer in the LinphoneCall
602  *
603  * @ingroup call_control
604  *
605  * return user_pointer an opaque user pointer that can be retrieved at any time
606 **/
607 void *linphone_call_get_user_pointer(LinphoneCall *call)
608 {
609         return call->user_pointer;
610 }
611
612 /**
613  * Set the user_pointer in the LinphoneCall
614  *
615  * @ingroup call_control
616  *
617  * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
618 **/
619 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
620 {
621         call->user_pointer = user_pointer;
622 }
623
624 /**
625  * Returns the call log associated to this call.
626 **/
627 LinphoneCallLog *linphone_call_get_call_log(const LinphoneCall *call){
628         return call->log;
629 }
630
631 /**
632  * Returns the refer-to uri (if the call was transfered).
633 **/
634 const char *linphone_call_get_refer_to(const LinphoneCall *call){
635         return call->refer_to;
636 }
637
638 /**
639  * Returns direction of the call (incoming or outgoing).
640 **/
641 LinphoneCallDir linphone_call_get_dir(const LinphoneCall *call){
642         return call->log->dir;
643 }
644
645 /**
646  * Returns the far end's user agent description string, if available.
647 **/
648 const char *linphone_call_get_remote_user_agent(LinphoneCall *call){
649         if (call->op){
650                 return sal_op_get_remote_ua (call->op);
651         }
652         return NULL;
653 }
654
655 /**
656  * Returns true if this calls has received a transfer that has not been
657  * executed yet.
658  * Pending transfers are executed when this call is being paused or closed,
659  * locally or by remote endpoint.
660  * If the call is already paused while receiving the transfer request, the
661  * transfer immediately occurs.
662 **/
663 bool_t linphone_call_has_transfer_pending(const LinphoneCall *call){
664         return call->refer_pending;
665 }
666
667 /**
668  * Returns call's duration in seconds.
669 **/
670 int linphone_call_get_duration(const LinphoneCall *call){
671         if (call->media_start_time==0) return 0;
672         return time(NULL)-call->media_start_time;
673 }
674
675 /**
676  * Returns the call object this call is replacing, if any.
677  * Call replacement can occur during call transfers.
678  * By default, the core automatically terminates the replaced call and accept the new one.
679  * This function allows the application to know whether a new incoming call is a one that replaces another one.
680 **/
681 LinphoneCall *linphone_call_get_replaced_call(LinphoneCall *call){
682         SalOp *op=sal_call_get_replaces(call->op);
683         if (op){
684                 return (LinphoneCall*)sal_op_get_user_pointer(op);
685         }
686         return NULL;
687 }
688
689 /**
690  * Indicate whether camera input should be sent to remote end.
691 **/
692 void linphone_call_enable_camera (LinphoneCall *call, bool_t enable){
693 #ifdef VIDEO_ENABLED
694         if (call->videostream!=NULL && call->videostream->ticker!=NULL){
695                 LinphoneCore *lc=call->core;
696                 MSWebCam *nowebcam=get_nowebcam_device();
697                 if (call->camera_active!=enable && lc->video_conf.device!=nowebcam){
698                         video_stream_change_camera(call->videostream,
699                                      enable ? lc->video_conf.device : nowebcam);
700                 }
701         }
702         call->camera_active=enable;
703 #endif
704 }
705
706 /**
707  * Take a photo of currently received video and write it into a jpeg file.
708 **/
709 int linphone_call_take_video_snapshot(LinphoneCall *call, const char *file){
710 #ifdef VIDEO_ENABLED
711         if (call->videostream!=NULL && call->videostream->jpegwriter!=NULL){
712                 return ms_filter_call_method(call->videostream->jpegwriter,MS_JPEG_WRITER_TAKE_SNAPSHOT,(void*)file);
713         }
714         ms_warning("Cannot take snapshot: no currently running video stream on this call.");
715         return -1;
716 #endif
717         return -1;
718 }
719
720 /**
721  * Returns TRUE if camera pictures are sent to the remote party.
722 **/
723 bool_t linphone_call_camera_enabled (const LinphoneCall *call){
724         return call->camera_active;
725 }
726
727 /**
728  * Enable video stream.
729 **/
730 void linphone_call_params_enable_video(LinphoneCallParams *cp, bool_t enabled){
731         cp->has_video=enabled;
732 }
733
734 /**
735  * Returns whether video is enabled.
736 **/
737 bool_t linphone_call_params_video_enabled(const LinphoneCallParams *cp){
738         return cp->has_video;
739 }
740
741 enum LinphoneMediaEncryption linphone_call_params_get_media_encryption(const LinphoneCallParams *cp) {
742         return cp->media_encryption;
743 }
744
745 void linphone_call_params_set_media_encryption(LinphoneCallParams *cp, enum LinphoneMediaEncryption e) {
746         cp->media_encryption = e;
747 }
748
749
750 /**
751  * Enable sending of real early media (during outgoing calls).
752 **/
753 void linphone_call_params_enable_early_media_sending(LinphoneCallParams *cp, bool_t enabled){
754         cp->real_early_media=enabled;
755 }
756
757 bool_t linphone_call_params_early_media_sending_enabled(const LinphoneCallParams *cp){
758         return cp->real_early_media;
759 }
760
761 /**
762  * Returns true if the call is part of the locally managed conference.
763 **/
764 bool_t linphone_call_params_local_conference_mode(const LinphoneCallParams *cp){
765         return cp->in_conference;
766 }
767
768 /**
769  * Refine bandwidth settings for this call by setting a bandwidth limit for audio streams.
770  * As a consequence, codecs whose bitrates are not compatible with this limit won't be used.
771 **/
772 void linphone_call_params_set_audio_bandwidth_limit(LinphoneCallParams *cp, int bandwidth){
773         cp->audio_bw=bandwidth;
774 }
775
776 #ifdef VIDEO_ENABLED
777 /**
778  * Request remote side to send us a Video Fast Update.
779 **/
780 void linphone_call_send_vfu_request(LinphoneCall *call)
781 {
782         if (LinphoneCallStreamsRunning == linphone_call_get_state(call))
783                 sal_call_send_vfu_request(call->op);
784 }
785 #endif
786
787 /**
788  *
789 **/
790 LinphoneCallParams * linphone_call_params_copy(const LinphoneCallParams *cp){
791         LinphoneCallParams *ncp=ms_new0(LinphoneCallParams,1);
792         memcpy(ncp,cp,sizeof(LinphoneCallParams));
793         return ncp;
794 }
795
796 /**
797  *
798 **/
799 void linphone_call_params_destroy(LinphoneCallParams *p){
800         ms_free(p);
801 }
802
803 /**
804  * @}
805 **/
806
807
808 #ifdef TEST_EXT_RENDERER
809 static void rendercb(void *data, const MSPicture *local, const MSPicture *remote){
810         ms_message("rendercb, local buffer=%p, remote buffer=%p",
811                    local ? local->planes[0] : NULL, remote? remote->planes[0] : NULL);
812 }
813 #endif
814
815 #ifdef VIDEO_ENABLED
816 static void video_stream_event_cb(void *user_pointer, const MSFilter *f, const unsigned int event_id, const void *args){
817         ms_warning("In linphonecall.c: video_stream_event_cb");
818         switch (event_id) {
819                 case MS_VIDEO_DECODER_DECODING_ERRORS:
820                         ms_warning("Case is MS_VIDEO_DECODER_DECODING_ERRORS");
821                         linphone_call_send_vfu_request((LinphoneCall*) user_pointer);
822                         break;
823                 default:
824                         ms_warning("Unhandled event %i", event_id);
825                         break;
826         }
827 }
828 #endif
829
830 void linphone_call_init_media_streams(LinphoneCall *call){
831         LinphoneCore *lc=call->core;
832         SalMediaDescription *md=call->localdesc;
833         AudioStream *audiostream;
834
835         call->audiostream=audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
836         if (linphone_core_echo_limiter_enabled(lc)){
837                 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
838                 if (strcasecmp(type,"mic")==0)
839                         audio_stream_enable_echo_limiter(audiostream,ELControlMic);
840                 else if (strcasecmp(type,"full")==0)
841                         audio_stream_enable_echo_limiter(audiostream,ELControlFull);
842         }
843         audio_stream_enable_gain_control(audiostream,TRUE);
844         if (linphone_core_echo_cancellation_enabled(lc)){
845                 int len,delay,framesize;
846                 const char *statestr=lp_config_get_string(lc->config,"sound","ec_state",NULL);
847                 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
848                 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
849                 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
850                 audio_stream_set_echo_canceller_params(audiostream,len,delay,framesize);
851                 if (statestr && audiostream->ec){
852                         ms_filter_call_method(audiostream->ec,MS_ECHO_CANCELLER_SET_STATE_STRING,(void*)statestr);
853                 }
854         }
855         audio_stream_enable_automatic_gain_control(audiostream,linphone_core_agc_enabled(lc));
856         {
857                 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
858                 audio_stream_enable_noise_gate(audiostream,enabled);
859         }
860
861         if (lc->a_rtp)
862                 rtp_session_set_transports(audiostream->session,lc->a_rtp,lc->a_rtcp);
863
864         call->audiostream_app_evq = ortp_ev_queue_new();
865         rtp_session_register_event_queue(audiostream->session,call->audiostream_app_evq);
866
867 #ifdef VIDEO_ENABLED
868
869         if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0){
870                 call->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
871         if( lc->video_conf.displaytype != NULL)
872                 video_stream_set_display_filter_name(call->videostream,lc->video_conf.displaytype);
873         video_stream_set_event_callback(call->videostream,video_stream_event_cb, call);
874         if (lc->v_rtp)
875                 rtp_session_set_transports(call->videostream->session,lc->v_rtp,lc->v_rtcp);
876         call->videostream_app_evq = ortp_ev_queue_new();
877         rtp_session_register_event_queue(call->videostream->session,call->videostream_app_evq);
878 #ifdef TEST_EXT_RENDERER
879                 video_stream_set_render_callback(call->videostream,rendercb,NULL);
880 #endif
881         }
882 #else
883         call->videostream=NULL;
884 #endif
885 }
886
887
888 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
889
890 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
891         LinphoneCore* lc = (LinphoneCore*)user_data;
892         if (dtmf<0 || dtmf>15){
893                 ms_warning("Bad dtmf value %i",dtmf);
894                 return;
895         }
896         if (lc->vtable.dtmf_received != NULL)
897                 lc->vtable.dtmf_received(lc, linphone_core_get_current_call(lc), dtmf_tab[dtmf]);
898 }
899
900 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
901         if (st->equalizer){
902                 MSFilter *f=st->equalizer;
903                 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
904                 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
905                 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
906                 if (enabled){
907                         if (gains){
908                                 do{
909                                         int bytes;
910                                         MSEqualizerGain g;
911                                         if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
912                                                 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
913                                                 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
914                                                 gains+=bytes;
915                                         }else break;
916                                 }while(1);
917                         }
918                 }
919         }
920 }
921
922 void _post_configure_audio_stream(AudioStream *st, LinphoneCore *lc, bool_t muted){
923         float mic_gain=lp_config_get_float(lc->config,"sound","mic_gain",1);
924         float thres = 0;
925         float recv_gain;
926         float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
927         float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
928         int dc_removal=lp_config_get_int(lc->config,"sound","dc_removal",0);
929
930         if (!muted)
931                 audio_stream_set_mic_gain(st,mic_gain);
932         else
933                 audio_stream_set_mic_gain(st,0);
934
935         recv_gain = lc->sound_conf.soft_play_lev;
936         if (recv_gain != 0) {
937                 linphone_core_set_playback_gain_db (lc,recv_gain);
938         }
939         
940         if (st->volsend){
941                 ms_filter_call_method(st->volsend,MS_VOLUME_REMOVE_DC,&dc_removal);
942                 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
943                 thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
944                 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
945                 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
946                 float transmit_thres=lp_config_get_float(lc->config,"sound","el_transmit_thres",-1);
947                 MSFilter *f=NULL;
948                 f=st->volsend;
949                 if (speed==-1) speed=0.03;
950                 if (force==-1) force=25;
951                 ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
952                 ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
953                 if (thres!=-1)
954                         ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
955                 if (sustain!=-1)
956                         ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
957                 if (transmit_thres!=-1)
958                                 ms_filter_call_method(f,MS_VOLUME_SET_EA_TRANSMIT_THRESHOLD,&transmit_thres);
959
960                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
961                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
962         }
963         if (st->volrecv){
964                 /* parameters for a limited noise-gate effect, using echo limiter threshold */
965                 float floorgain = 1/mic_gain;
966                 int spk_agc=lp_config_get_int(lc->config,"sound","speaker_agc_enabled",0);
967                 ms_filter_call_method(st->volrecv, MS_VOLUME_ENABLE_AGC, &spk_agc);
968                 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
969                 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&floorgain);
970         }
971         parametrize_equalizer(lc,st);
972 }
973
974 static void post_configure_audio_streams(LinphoneCall*call){
975         AudioStream *st=call->audiostream;
976         LinphoneCore *lc=call->core;
977         _post_configure_audio_stream(st,lc,call->audio_muted);
978         if (lc->vtable.dtmf_received!=NULL){
979                 /* replace by our default action*/
980                 audio_stream_play_received_dtmfs(call->audiostream,FALSE);
981                 rtp_session_signal_connect(call->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
982         }
983 }
984
985 static RtpProfile *make_profile(LinphoneCall *call, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
986         int bw;
987         const MSList *elem;
988         RtpProfile *prof=rtp_profile_new("Call profile");
989         bool_t first=TRUE;
990         int remote_bw=0;
991         LinphoneCore *lc=call->core;
992         int up_ptime=0;
993         *used_pt=-1;
994
995         for(elem=desc->payloads;elem!=NULL;elem=elem->next){
996                 PayloadType *pt=(PayloadType*)elem->data;
997                 int number;
998
999                 if ((pt->flags & PAYLOAD_TYPE_FLAG_CAN_SEND) && first) {
1000                         if (desc->type==SalAudio){
1001                                 linphone_core_update_allocated_audio_bandwidth_in_call(call,pt);
1002                                 up_ptime=linphone_core_get_upload_ptime(lc);
1003                         }
1004                         *used_pt=payload_type_get_number(pt);
1005                         first=FALSE;
1006                 }
1007                 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
1008                 else if (md->bandwidth>0) {
1009                         /*case where b=AS is given globally, not per stream*/
1010                         remote_bw=md->bandwidth;
1011                         if (desc->type==SalVideo){
1012                                 remote_bw=get_video_bandwidth(remote_bw,call->audio_bw);
1013                         }
1014                 }
1015
1016                 if (desc->type==SalAudio){
1017                                 bw=get_min_bandwidth(call->audio_bw,remote_bw);
1018                 }else bw=get_min_bandwidth(get_video_bandwidth(linphone_core_get_upload_bandwidth (lc),call->audio_bw),remote_bw);
1019                 if (bw>0) pt->normal_bitrate=bw*1000;
1020                 else if (desc->type==SalAudio){
1021                         pt->normal_bitrate=-1;
1022                 }
1023                 if (desc->ptime>0){
1024                         up_ptime=desc->ptime;
1025                 }
1026                 if (up_ptime>0){
1027                         char tmp[40];
1028                         snprintf(tmp,sizeof(tmp),"ptime=%i",up_ptime);
1029                         payload_type_append_send_fmtp(pt,tmp);
1030                 }
1031                 number=payload_type_get_number(pt);
1032                 if (rtp_profile_get_payload(prof,number)!=NULL){
1033                         ms_warning("A payload type with number %i already exists in profile !",number);
1034                 }else
1035                         rtp_profile_set_payload(prof,number,pt);
1036         }
1037         return prof;
1038 }
1039
1040
1041 static void setup_ring_player(LinphoneCore *lc, LinphoneCall *call){
1042         int pause_time=3000;
1043         audio_stream_play(call->audiostream,lc->sound_conf.ringback_tone);
1044         ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
1045 }
1046
1047 #define LINPHONE_RTCP_SDES_TOOL "Linphone-" LINPHONE_VERSION
1048
1049 static bool_t linphone_call_sound_resources_available(LinphoneCall *call){
1050         LinphoneCore *lc=call->core;
1051         LinphoneCall *current=linphone_core_get_current_call(lc);
1052         return !linphone_core_is_in_conference(lc) && 
1053                 (current==NULL || current==call);
1054 }
1055
1056 static void linphone_call_start_audio_stream(LinphoneCall *call, const char *cname, bool_t muted, bool_t send_ringbacktone, bool_t use_arc){
1057         LinphoneCore *lc=call->core;
1058         int jitt_comp=lc->rtp_conf.audio_jitt_comp;
1059         int used_pt=-1;
1060         /* look for savp stream first */
1061         const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
1062                                                 SalProtoRtpSavp,SalAudio);
1063         /* no savp audio stream, use avp */
1064         if (!stream)
1065                 stream=sal_media_description_find_stream(call->resultdesc,
1066                                                 SalProtoRtpAvp,SalAudio);
1067
1068         if (stream && stream->dir!=SalStreamInactive && stream->port!=0){
1069                 MSSndCard *playcard=lc->sound_conf.lsd_card ?
1070                         lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
1071                 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
1072                 const char *playfile=lc->play_file;
1073                 const char *recfile=lc->rec_file;
1074                 call->audio_profile=make_profile(call,call->resultdesc,stream,&used_pt);
1075                 bool_t use_ec;
1076
1077                 if (used_pt!=-1){
1078                         if (playcard==NULL) {
1079                                 ms_warning("No card defined for playback !");
1080                         }
1081                         if (captcard==NULL) {
1082                                 ms_warning("No card defined for capture !");
1083                         }
1084                         /*Replace soundcard filters by inactive file players or recorders
1085                          when placed in recvonly or sendonly mode*/
1086                         if (stream->port==0 || stream->dir==SalStreamRecvOnly){
1087                                 captcard=NULL;
1088                                 playfile=NULL;
1089                         }else if (stream->dir==SalStreamSendOnly){
1090                                 playcard=NULL;
1091                                 captcard=NULL;
1092                                 recfile=NULL;
1093                                 /*And we will eventually play "playfile" if set by the user*/
1094                                 /*playfile=NULL;*/
1095                         }
1096                         if (send_ringbacktone){
1097                                 captcard=NULL;
1098                                 playfile=NULL;/* it is setup later*/
1099                         }
1100                         /*if playfile are supplied don't use soundcards*/
1101                         if (lc->use_files) {
1102                                 captcard=NULL;
1103                                 playcard=NULL;
1104                         }
1105                         if (call->params.in_conference){
1106                                 /* first create the graph without soundcard resources*/
1107                                 captcard=playcard=NULL;
1108                         }
1109                         if (!linphone_call_sound_resources_available(call)){
1110                                 ms_message("Sound resources are used by another call, not using soundcard.");
1111                                 captcard=playcard=NULL;
1112                         }
1113                         use_ec=captcard==NULL ? FALSE : linphone_core_echo_cancellation_enabled(lc);
1114
1115                         audio_stream_enable_adaptive_bitrate_control(call->audiostream,use_arc);
1116                         audio_stream_start_full(
1117                                 call->audiostream,
1118                                 call->audio_profile,
1119                                 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
1120                                 stream->port,
1121                                 linphone_core_rtcp_enabled(lc) ? (stream->port+1) : 0,
1122                                 used_pt,
1123                                 jitt_comp,
1124                                 playfile,
1125                                 recfile,
1126                                 playcard,
1127                                 captcard,
1128                                 use_ec
1129                                 );
1130                         post_configure_audio_streams(call);
1131                         if (muted && !send_ringbacktone){
1132                                 audio_stream_set_mic_gain(call->audiostream,0);
1133                         }
1134                         if (stream->dir==SalStreamSendOnly && playfile!=NULL){
1135                                 int pause_time=500;
1136                                 ms_filter_call_method(call->audiostream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);
1137                         }
1138                         if (send_ringbacktone){
1139                                 setup_ring_player(lc,call);
1140                         }
1141                         audio_stream_set_rtcp_information(call->audiostream, cname, LINPHONE_RTCP_SDES_TOOL);
1142                         if (call->params.in_conference){
1143                                 /*transform the graph to connect it to the conference filter */
1144                                 linphone_call_add_to_conf(call);
1145                         }
1146                         
1147                         if (stream->proto == SalProtoRtpSavp) {
1148                                 const SalStreamDescription *local_st_desc=sal_media_description_find_stream(call->localdesc,
1149                                                 SalProtoRtpSavp,SalAudio);
1150                                                 
1151                                 audio_stream_enable_strp(
1152                                         call->audiostream, 
1153                                         stream->crypto[0].algo,
1154                                         local_st_desc->crypto[0].master_key,
1155                                         stream->crypto[0].master_key);                          
1156                         }
1157                 }else ms_warning("No audio stream accepted ?");
1158         }
1159 }
1160
1161 static void linphone_call_start_video_stream(LinphoneCall *call, const char *cname,bool_t all_inputs_muted){
1162 #ifdef VIDEO_ENABLED
1163         LinphoneCore *lc=call->core;
1164         int used_pt=-1;
1165         /* look for savp stream first */
1166         const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
1167                                                 SalProtoRtpSavp,SalVideo);
1168         /* no savp audio stream, use avp */
1169         if (!vstream)
1170                 vstream=sal_media_description_find_stream(call->resultdesc,
1171                                                 SalProtoRtpAvp,SalVideo);
1172                                                 
1173         /* shutdown preview */
1174         if (lc->previewstream!=NULL) {
1175                 video_preview_stop(lc->previewstream);
1176                 lc->previewstream=NULL;
1177         }
1178         call->current_params.has_video=FALSE;
1179         if (vstream!=NULL && vstream->dir!=SalStreamInactive && vstream->port!=0) {
1180                 const char *addr=vstream->addr[0]!='\0' ? vstream->addr : call->resultdesc->addr;
1181                 call->video_profile=make_profile(call,call->resultdesc,vstream,&used_pt);
1182                 if (used_pt!=-1){
1183                         VideoStreamDir dir=VideoStreamSendRecv;
1184                         MSWebCam *cam=lc->video_conf.device;
1185                         bool_t is_inactive=FALSE;
1186
1187                         call->current_params.has_video=TRUE;
1188
1189                         video_stream_enable_adaptive_bitrate_control(call->videostream,
1190                                                                   linphone_core_adaptive_rate_control_enabled(lc));
1191                         video_stream_set_sent_video_size(call->videostream,linphone_core_get_preferred_video_size(lc));
1192                         video_stream_enable_self_view(call->videostream,lc->video_conf.selfview);
1193                         if (lc->video_window_id!=0)
1194                                 video_stream_set_native_window_id(call->videostream,lc->video_window_id);
1195                         if (lc->preview_window_id!=0)
1196                                 video_stream_set_native_preview_window_id (call->videostream,lc->preview_window_id);
1197                         video_stream_use_preview_video_window (call->videostream,lc->use_preview_window);
1198                         
1199                         if (vstream->dir==SalStreamSendOnly && lc->video_conf.capture ){
1200                                 cam=get_nowebcam_device();
1201                                 dir=VideoStreamSendOnly;
1202                         }else if (vstream->dir==SalStreamRecvOnly && lc->video_conf.display ){
1203                                 dir=VideoStreamRecvOnly;
1204                         }else if (vstream->dir==SalStreamSendRecv){
1205                                 if (lc->video_conf.display && lc->video_conf.capture)
1206                                         dir=VideoStreamSendRecv;
1207                                 else if (lc->video_conf.display)
1208                                         dir=VideoStreamRecvOnly;
1209                                 else
1210                                         dir=VideoStreamSendOnly;
1211                         }else{
1212                                 ms_warning("video stream is inactive.");
1213                                 /*either inactive or incompatible with local capabilities*/
1214                                 is_inactive=TRUE;
1215                         }
1216                         if (call->camera_active==FALSE || all_inputs_muted){
1217                                 cam=get_nowebcam_device();
1218                         }
1219                         if (!is_inactive){
1220                                 video_stream_set_direction (call->videostream, dir);
1221                                 ms_message("%s lc rotation:%d\n", __FUNCTION__, lc->device_rotation);
1222                                 video_stream_set_device_rotation(call->videostream, lc->device_rotation);
1223                                 video_stream_start(call->videostream,
1224                                         call->video_profile, addr, vstream->port,
1225                                         linphone_core_rtcp_enabled(lc) ? (vstream->port+1) : 0,
1226                                         used_pt, lc->rtp_conf.audio_jitt_comp, cam);
1227                                 video_stream_set_rtcp_information(call->videostream, cname,LINPHONE_RTCP_SDES_TOOL);
1228                         }
1229                         
1230                         if (vstream->proto == SalProtoRtpSavp) {
1231                                 const SalStreamDescription *local_st_desc=sal_media_description_find_stream(call->localdesc,
1232                                                 SalProtoRtpSavp,SalVideo);
1233                                                 
1234                                 video_stream_enable_strp(
1235                                         call->videostream, 
1236                                         vstream->crypto[0].algo,
1237                                         local_st_desc->crypto[0].master_key, 
1238                                         vstream->crypto[0].master_key
1239                                         );                              
1240                         }
1241                 }else ms_warning("No video stream accepted.");
1242         }else{
1243                 ms_warning("No valid video stream defined.");
1244         }
1245 #endif
1246 }
1247
1248 void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_muted, bool_t send_ringbacktone){
1249         LinphoneCore *lc=call->core;
1250         LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
1251         char *cname;
1252         bool_t use_arc;
1253 #ifdef VIDEO_ENABLED
1254         const SalStreamDescription *vstream=sal_media_description_find_stream(call->resultdesc,
1255                                                         SalProtoRtpAvp,SalVideo);
1256 #endif
1257
1258         if(call->audiostream == NULL)
1259         {
1260                 ms_fatal("start_media_stream() called without prior init !");
1261                 return;
1262         }
1263         call->current_params = call->params;
1264         cname=linphone_address_as_string_uri_only(me);
1265
1266 #if defined(VIDEO_ENABLED)
1267         if (vstream!=NULL && vstream->dir!=SalStreamInactive && vstream->payloads!=NULL){
1268                 /*when video is used, do not make adaptive rate control on audio, it is stupid.*/
1269                 use_arc=FALSE;
1270         }
1271 #endif
1272         linphone_call_start_audio_stream(call,cname,all_inputs_muted,send_ringbacktone,use_arc);
1273         if (call->videostream!=NULL) {
1274                 linphone_call_start_video_stream(call,cname,all_inputs_muted);
1275         }
1276
1277         call->all_muted=all_inputs_muted;
1278         call->playing_ringbacktone=send_ringbacktone;
1279         call->up_bw=linphone_core_get_upload_bandwidth(lc);
1280
1281         if (call->params.media_encryption==LinphoneMediaEncryptionZRTP) {
1282                 OrtpZrtpParams params;
1283                 /*will be set later when zrtp is activated*/
1284                 call->current_params.media_encryption=LinphoneMediaEncryptionNone;
1285                 
1286                 params.zid=get_hexa_zrtp_identifier(lc);
1287                 params.zid_file=lc->zrtp_secrets_cache;
1288                 audio_stream_enable_zrtp(call->audiostream,&params);
1289         }
1290
1291         goto end;
1292         end:
1293                 ms_free(cname);
1294                 linphone_address_destroy(me);
1295 }
1296
1297 static void linphone_call_log_fill_stats(LinphoneCallLog *log, AudioStream *st){
1298         audio_stream_get_local_rtp_stats (st,&log->local_stats);
1299         log->quality=audio_stream_get_average_quality_rating(st);
1300 }
1301
1302 void linphone_call_stop_media_streams(LinphoneCall *call){
1303         if (call->audiostream!=NULL) {
1304                 rtp_session_unregister_event_queue(call->audiostream->session,call->audiostream_app_evq);
1305                 ortp_ev_queue_flush(call->audiostream_app_evq);
1306                 ortp_ev_queue_destroy(call->audiostream_app_evq);
1307
1308                 if (call->audiostream->ec){
1309                         const char *state_str=NULL;
1310                         ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_STATE_STRING,&state_str);
1311                         if (state_str){
1312                                 ms_message("Writing echo canceller state, %i bytes",(int)strlen(state_str));
1313                                 lp_config_set_string(call->core->config,"sound","ec_state",state_str);
1314                         }
1315                 }
1316                 linphone_call_log_fill_stats (call->log,call->audiostream);
1317                 if (call->endpoint){
1318                         linphone_call_remove_from_conf(call);
1319                 }
1320                 audio_stream_stop(call->audiostream);
1321                 call->audiostream=NULL;
1322         }
1323
1324
1325 #ifdef VIDEO_ENABLED
1326         if (call->videostream!=NULL){
1327                 rtp_session_unregister_event_queue(call->videostream->session,call->videostream_app_evq);
1328                 ortp_ev_queue_flush(call->videostream_app_evq);
1329                 ortp_ev_queue_destroy(call->videostream_app_evq);
1330                 video_stream_stop(call->videostream);
1331                 call->videostream=NULL;
1332         }
1333 #endif
1334         ms_event_queue_skip(call->core->msevq);
1335         
1336         if (call->audio_profile){
1337                 rtp_profile_clear_all(call->audio_profile);
1338                 rtp_profile_destroy(call->audio_profile);
1339                 call->audio_profile=NULL;
1340         }
1341         if (call->video_profile){
1342                 rtp_profile_clear_all(call->video_profile);
1343                 rtp_profile_destroy(call->video_profile);
1344                 call->video_profile=NULL;
1345         }
1346 }
1347
1348
1349
1350 void linphone_call_enable_echo_cancellation(LinphoneCall *call, bool_t enable) {
1351         if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1352                 bool_t bypass_mode = !enable;
1353                 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_SET_BYPASS_MODE,&bypass_mode);
1354         }
1355 }
1356 bool_t linphone_call_echo_cancellation_enabled(LinphoneCall *call) {
1357         if (call!=NULL && call->audiostream!=NULL && call->audiostream->ec){
1358                 bool_t val;
1359                 ms_filter_call_method(call->audiostream->ec,MS_ECHO_CANCELLER_GET_BYPASS_MODE,&val);
1360                 return !val;
1361         } else {
1362                 return linphone_core_echo_cancellation_enabled(call->core);
1363         }
1364 }
1365
1366 void linphone_call_enable_echo_limiter(LinphoneCall *call, bool_t val){
1367         if (call!=NULL && call->audiostream!=NULL ) {
1368                 if (val) {
1369                 const char *type=lp_config_get_string(call->core->config,"sound","el_type","mic");
1370                 if (strcasecmp(type,"mic")==0)
1371                         audio_stream_enable_echo_limiter(call->audiostream,ELControlMic);
1372                 else if (strcasecmp(type,"full")==0)
1373                         audio_stream_enable_echo_limiter(call->audiostream,ELControlFull);
1374                 } else {
1375                         audio_stream_enable_echo_limiter(call->audiostream,ELInactive);
1376                 }
1377         }
1378 }
1379
1380 bool_t linphone_call_echo_limiter_enabled(const LinphoneCall *call){
1381         if (call!=NULL && call->audiostream!=NULL ){
1382                 return call->audiostream->el_type !=ELInactive ;
1383         } else {
1384                 return linphone_core_echo_limiter_enabled(call->core);
1385         }
1386 }
1387
1388 /**
1389  * @addtogroup call_misc
1390  * @{
1391 **/
1392
1393 /**
1394  * Returns the measured sound volume played locally (received from remote)
1395  * It is expressed in dbm0.
1396 **/
1397 float linphone_call_get_play_volume(LinphoneCall *call){
1398         AudioStream *st=call->audiostream;
1399         if (st && st->volrecv){
1400                 float vol=0;
1401                 ms_filter_call_method(st->volrecv,MS_VOLUME_GET,&vol);
1402                 return vol;
1403
1404         }
1405         return LINPHONE_VOLUME_DB_LOWEST;
1406 }
1407
1408 /**
1409  * Returns the measured sound volume recorded locally (sent to remote)
1410  * It is expressed in dbm0.
1411 **/
1412 float linphone_call_get_record_volume(LinphoneCall *call){
1413         AudioStream *st=call->audiostream;
1414         if (st && st->volsend && !call->audio_muted && call->state==LinphoneCallStreamsRunning){
1415                 float vol=0;
1416                 ms_filter_call_method(st->volsend,MS_VOLUME_GET,&vol);
1417                 return vol;
1418
1419         }
1420         return LINPHONE_VOLUME_DB_LOWEST;
1421 }
1422
1423 /**
1424  * Obtain real-time quality rating of the call
1425  *
1426  * Based on local RTP statistics and RTCP feedback, a quality rating is computed and updated
1427  * during all the duration of the call. This function returns its value at the time of the function call.
1428  * It is expected that the rating is updated at least every 5 seconds or so.
1429  * The rating is a floating point number comprised between 0 and 5.
1430  *
1431  * 4-5 = good quality <br>
1432  * 3-4 = average quality <br>
1433  * 2-3 = poor quality <br>
1434  * 1-2 = very poor quality <br>
1435  * 0-1 = can't be worse, mostly unusable <br>
1436  *
1437  * @returns The function returns -1 if no quality measurement is available, for example if no
1438  * active audio stream exist. Otherwise it returns the quality rating.
1439 **/
1440 float linphone_call_get_current_quality(LinphoneCall *call){
1441         if (call->audiostream){
1442                 return audio_stream_get_quality_rating(call->audiostream);
1443         }
1444         return -1;
1445 }
1446
1447 /**
1448  * Returns call quality averaged over all the duration of the call.
1449  *
1450  * See linphone_call_get_current_quality() for more details about quality measurement.
1451 **/
1452 float linphone_call_get_average_quality(LinphoneCall *call){
1453         if (call->audiostream){
1454                 return audio_stream_get_average_quality_rating(call->audiostream);
1455         }
1456         return -1;
1457 }
1458
1459 /**
1460  * @}
1461 **/
1462
1463 static void display_bandwidth(RtpSession *as, RtpSession *vs){
1464         ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
1465         (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
1466         (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
1467         (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
1468         (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
1469 }
1470
1471 static void linphone_core_disconnected(LinphoneCore *lc, LinphoneCall *call){
1472         char temp[256];
1473         char *from=NULL;
1474         if(call)
1475                 from = linphone_call_get_remote_address_as_string(call);
1476         if (from)
1477         {
1478                 snprintf(temp,sizeof(temp),"Remote end %s seems to have disconnected, the call is going to be closed.",from);
1479                 free(from);
1480         }
1481         else
1482         {
1483                 snprintf(temp,sizeof(temp),"Remote end seems to have disconnected, the call is going to be closed.");
1484         }
1485         if (lc->vtable.display_warning!=NULL)
1486                 lc->vtable.display_warning(lc,temp);
1487         linphone_core_terminate_call(lc,call);
1488 }
1489
1490 void linphone_call_background_tasks(LinphoneCall *call, bool_t one_second_elapsed){
1491         int disconnect_timeout = linphone_core_get_nortp_timeout(call->core);
1492         bool_t disconnected=FALSE;
1493
1494         if (call->state==LinphoneCallStreamsRunning && one_second_elapsed){
1495                 RtpSession *as=NULL,*vs=NULL;
1496                 float audio_load=0, video_load=0;
1497                 if (call->audiostream!=NULL){
1498                         as=call->audiostream->session;
1499                         if (call->audiostream->ticker)
1500                                 audio_load=ms_ticker_get_average_load(call->audiostream->ticker);
1501                 }
1502                 if (call->videostream!=NULL){
1503                         if (call->videostream->ticker)
1504                                 video_load=ms_ticker_get_average_load(call->videostream->ticker);
1505                         vs=call->videostream->session;
1506                 }
1507                 display_bandwidth(as,vs);
1508                 ms_message("Thread processing load: audio=%f\tvideo=%f",audio_load,video_load);
1509         }
1510 #ifdef VIDEO_ENABLED
1511         if (call->videostream!=NULL) {
1512                 // Beware that the application queue should not depend on treatments fron the
1513                 // mediastreamer queue.
1514                 video_stream_iterate(call->videostream);
1515
1516                 if (call->videostream_app_evq){
1517                         OrtpEvent *ev;
1518                         while (NULL != (ev=ortp_ev_queue_get(call->videostream_app_evq))){
1519                                 OrtpEventType evt=ortp_event_get_type(ev);
1520                                 if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
1521                                         OrtpEventData *evd=ortp_event_get_data(ev);
1522                                         linphone_call_videostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
1523                                 }
1524                                 ortp_event_destroy(ev);
1525                         }
1526                 }
1527         }
1528 #endif
1529         if (call->audiostream!=NULL) {
1530                 // Beware that the application queue should not depend on treatments fron the
1531                 // mediastreamer queue.
1532                 audio_stream_iterate(call->audiostream);
1533
1534                 if (call->audiostream->evq){
1535                         OrtpEvent *ev;
1536                         while (NULL != (ev=ortp_ev_queue_get(call->audiostream_app_evq))){
1537                                 OrtpEventType evt=ortp_event_get_type(ev);
1538                                 if (evt == ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED){
1539                                         OrtpEventData *evd=ortp_event_get_data(ev);
1540                                         linphone_call_audiostream_encryption_changed(call, evd->info.zrtp_stream_encrypted);
1541                                 } else if (evt == ORTP_EVENT_ZRTP_SAS_READY) {
1542                                         OrtpEventData *evd=ortp_event_get_data(ev);
1543                                         linphone_call_audiostream_auth_token_ready(call, evd->info.zrtp_sas.sas, evd->info.zrtp_sas.verified);
1544                                 }
1545                                 ortp_event_destroy(ev);
1546                         }
1547                 }
1548         }
1549         if (call->state==LinphoneCallStreamsRunning && one_second_elapsed && call->audiostream!=NULL && disconnect_timeout>0 )
1550                 disconnected=!audio_stream_alive(call->audiostream,disconnect_timeout);
1551         if (disconnected)
1552                 linphone_core_disconnected(call->core,call);
1553 }
1554
1555 void linphone_call_log_completed(LinphoneCall *call){
1556         LinphoneCore *lc=call->core;
1557
1558         call->log->duration=time(NULL)-call->start_time;
1559
1560         if (call->log->status==LinphoneCallMissed){
1561                 char *info;
1562                 lc->missed_calls++;
1563                 info=ortp_strdup_printf(ngettext("You have missed %i call.",
1564                                          "You have missed %i calls.", lc->missed_calls),
1565                                 lc->missed_calls);
1566         if (lc->vtable.display_status!=NULL)
1567             lc->vtable.display_status(lc,info);
1568                 ms_free(info);
1569         }
1570         lc->call_logs=ms_list_prepend(lc->call_logs,(void *)call->log);
1571         if (ms_list_size(lc->call_logs)>lc->max_call_logs){
1572                 MSList *elem,*prevelem=NULL;
1573                 /*find the last element*/
1574                 for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
1575                         prevelem=elem;
1576                 }
1577                 elem=prevelem;
1578                 linphone_call_log_destroy((LinphoneCallLog*)elem->data);
1579                 lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
1580         }
1581         if (lc->vtable.call_log_updated!=NULL){
1582                 lc->vtable.call_log_updated(lc,call->log);
1583         }
1584         call_logs_write_to_config_file(lc);
1585 }
1586
1587