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