]> sjero.net Git - linphone/blob - coreapi/callbacks.c
Merge branch 'master' of belledonne-communications.com:linphone-private
[linphone] / coreapi / callbacks.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20
21 #include "sal.h"
22
23 #include "linphonecore.h"
24 #include "private.h"
25 #include "mediastreamer2/mediastream.h"
26
27
28 static void linphone_connect_incoming(LinphoneCore *lc, LinphoneCall *call){
29         if (lc->vtable.show)
30                 lc->vtable.show(lc);
31         if (lc->vtable.display_status)
32                 lc->vtable.display_status(lc,_("Connected."));
33         call->state=LCStateAVRunning;
34         if (lc->ringstream!=NULL){
35                 ring_stop(lc->ringstream);
36                 lc->ringstream=NULL;
37         }
38         linphone_core_start_media_streams(lc,call);
39 }
40
41 static void call_received(SalOp *h){
42         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
43         char *barmesg;
44         LinphoneCall *call;
45         const char *from,*to;
46         char *tmp;
47         LinphoneAddress *from_parsed;
48
49         /* first check if we can answer successfully to this invite */
50         if (lc->presence_mode!=LINPHONE_STATUS_ONLINE){
51                 ms_message("Not present !! presence mode : %d\n",lc->presence_mode);
52                 if (lc->presence_mode==LINPHONE_STATUS_BUSY)
53                         sal_call_decline(h,SalReasonBusy,NULL);
54                 else if (lc->presence_mode==LINPHONE_STATUS_AWAY
55                          ||lc->presence_mode==LINPHONE_STATUS_BERIGHTBACK
56                          ||lc->presence_mode==LINPHONE_STATUS_ONTHEPHONE
57                          ||lc->presence_mode==LINPHONE_STATUS_OUTTOLUNCH
58                          ||lc->presence_mode==LINPHONE_STATUS_OFFLINE)
59                         sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
60                 else if (lc->presence_mode==LINPHONE_STATUS_NOT_DISTURB)
61                         sal_call_decline(h,SalReasonTemporarilyUnavailable,NULL);
62                 else if (lc->alt_contact!=NULL && lc->presence_mode==LINPHONE_STATUS_MOVED)
63                         sal_call_decline(h,SalReasonRedirect,lc->alt_contact);
64                 else
65                         sal_call_decline(h,SalReasonBusy,NULL);
66                 sal_op_release(h);
67                 return;
68         }
69         if (lc->call!=NULL){/*busy*/
70                 sal_call_decline(h,SalReasonBusy,NULL);
71                 sal_op_release(h);
72                 return;
73         }
74         from=sal_op_get_from(h);
75         to=sal_op_get_to(h);
76         
77         call=linphone_call_new_incoming(lc,linphone_address_new(from),linphone_address_new(to),h);
78         lc->call=call;
79         sal_call_set_local_media_description(h,call->localdesc);
80         call->resultdesc=sal_call_get_final_media_description(h);
81         if (call->resultdesc)
82                 sal_media_description_ref(call->resultdesc);
83         if (call->resultdesc && sal_media_description_empty(call->resultdesc)){
84                 sal_call_decline(h,SalReasonMedia,NULL);
85                 linphone_call_destroy(call);
86                 lc->call=NULL;
87                 return;
88         }
89         
90         from_parsed=linphone_address_new(sal_op_get_from(h));
91         linphone_address_clean(from_parsed);
92         tmp=linphone_address_as_string(from_parsed);
93         linphone_address_destroy(from_parsed);
94         gstate_new_state(lc, GSTATE_CALL_IN_INVITE, tmp);
95         barmesg=ortp_strdup_printf("%s %s%s",tmp,_("is contacting you"),
96             (sal_call_autoanswer_asked(h)) ?_(" and asked autoanswer."):_("."));
97         if (lc->vtable.show) lc->vtable.show(lc);
98         if (lc->vtable.display_status) 
99             lc->vtable.display_status(lc,barmesg);
100
101         /* play the ring */
102         if (lc->sound_conf.ring_sndcard!=NULL){
103                 ms_message("Starting local ring...");
104                 lc->ringstream=ring_start(lc->sound_conf.local_ring,2000,lc->sound_conf.ring_sndcard);
105         }
106         linphone_call_set_state(call,LCStateRinging);
107         sal_call_notify_ringing(h);
108         linphone_core_init_media_streams(lc,lc->call);
109         if (lc->vtable.inv_recv) lc->vtable.inv_recv(lc,tmp);
110         ms_free(barmesg);
111         ms_free(tmp);
112 }
113
114 static void call_ringing(SalOp *h){
115         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
116         LinphoneCall *call=lc->call;
117         SalMediaDescription *md;
118         if (call==NULL) return;
119         if (lc->vtable.display_status)
120                 lc->vtable.display_status(lc,_("Remote ringing."));
121         md=sal_call_get_final_media_description(h);
122         if (md==NULL){
123                 if (lc->ringstream!=NULL) return;       /*already ringing !*/
124                 if (lc->sound_conf.play_sndcard!=NULL){
125                         ms_message("Remote ringing...");
126                         lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,lc->sound_conf.play_sndcard);
127                         gstate_new_state(lc, GSTATE_CALL_OUT_RINGING, NULL);
128                 }
129         }else{
130                 /*accept early media */
131                 if (lc->audiostream && lc->audiostream->ticker!=NULL){
132                         /*streams already started */
133                         ms_message("Early media already started.");
134                         return;
135                 }
136                 sal_media_description_ref(md);
137                 call->resultdesc=md;
138                 if (lc->vtable.show) lc->vtable.show(lc);
139                 if (lc->vtable.display_status) 
140                         lc->vtable.display_status(lc,_("Early media."));
141                 gstate_new_state(lc, GSTATE_CALL_OUT_RINGING, NULL);
142                 if (lc->ringstream!=NULL){
143                         ring_stop(lc->ringstream);
144                         lc->ringstream=NULL;
145                 }
146                 ms_message("Doing early media...");
147                 linphone_core_start_media_streams(lc,call);
148                 call->media_pending=TRUE;
149         }
150         call->state=LCStateRinging;
151 }
152
153 static void call_accepted(SalOp *op){
154         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
155         LinphoneCall *call=lc->call;
156         if (call==NULL){
157                 ms_warning("No call to accept.");
158                 return ;
159         }
160         if (sal_op_get_user_pointer(op)!=lc->call){
161                 ms_warning("call_accepted: ignoring.");
162                 return;
163         }
164         if (call->state==LCStateAVRunning){
165                 return ; /*already accepted*/
166         }
167         if (lc->audiostream->ticker!=NULL){
168                 /*case where we accepted early media */
169                 linphone_core_stop_media_streams(lc,call);
170                 linphone_core_init_media_streams(lc,call);
171         }
172         if (call->resultdesc)
173                 sal_media_description_unref(call->resultdesc);
174         call->resultdesc=sal_call_get_final_media_description(op);
175         if (call->resultdesc){
176                 sal_media_description_ref(call->resultdesc);
177                 call->media_pending=FALSE;
178         }
179         if (call->resultdesc && !sal_media_description_empty(call->resultdesc)){
180                 gstate_new_state(lc, GSTATE_CALL_OUT_CONNECTED, NULL);
181                 linphone_connect_incoming(lc,call);
182         }else{
183                 /*send a bye*/
184                 ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
185                 linphone_core_terminate_call(lc,NULL);
186         }
187 }
188
189 static void call_ack(SalOp *op){
190         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
191         LinphoneCall *call=lc->call;
192         if (call==NULL){
193                 ms_warning("No call to be ACK'd");
194                 return ;
195         }
196         if (sal_op_get_user_pointer(op)!=lc->call){
197                 ms_warning("call_ack: ignoring.");
198                 return;
199         }
200         if (call->media_pending){
201                 if (lc->audiostream->ticker!=NULL){
202                         /*case where we accepted early media */
203                         linphone_core_stop_media_streams(lc,call);
204                         linphone_core_init_media_streams(lc,call);
205                 }
206                 if (call->resultdesc)
207                         sal_media_description_unref(call->resultdesc);
208                 call->resultdesc=sal_call_get_final_media_description(op);
209                 if (call->resultdesc)
210                         sal_media_description_ref(call->resultdesc);
211                 if (call->resultdesc && !sal_media_description_empty(call->resultdesc)){
212                         gstate_new_state(lc, GSTATE_CALL_IN_CONNECTED, NULL);
213                         linphone_connect_incoming(lc,call);
214                 }else{
215                         /*send a bye*/
216                         ms_error("Incompatible SDP response received in ACK, need to abort the call");
217                         linphone_core_terminate_call(lc,NULL);
218                 }
219                 call->media_pending=FALSE;
220         }
221 }
222
223 static void call_updated(SalOp *op){
224         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
225         LinphoneCall *call=(LinphoneCall*)sal_op_get_user_pointer(op);
226         linphone_core_stop_media_streams(lc,call);
227         linphone_core_init_media_streams(lc,call);
228         if (call->resultdesc)
229                 sal_media_description_unref(call->resultdesc);
230         call->resultdesc=sal_call_get_final_media_description(op);
231         if (call->resultdesc){
232                 sal_media_description_ref(call->resultdesc);
233                 if (!sal_media_description_empty(call->resultdesc)){
234                         linphone_connect_incoming(lc,call);
235                 }
236         }
237 }
238
239 static void call_terminated(SalOp *op, const char *from){
240         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
241         if (sal_op_get_user_pointer(op)!=lc->call){
242                 ms_warning("call_terminated: ignoring.");
243                 return;
244         }
245         ms_message("Current call terminated...");
246         if (lc->ringstream!=NULL) {
247                 ring_stop(lc->ringstream);
248                 lc->ringstream=NULL;
249         }
250         linphone_core_stop_media_streams(lc,lc->call);
251         lc->vtable.show(lc);
252         lc->vtable.display_status(lc,_("Call terminated."));
253         gstate_new_state(lc, GSTATE_CALL_END, NULL);
254         if (lc->vtable.bye_recv!=NULL){
255                 LinphoneAddress *addr=linphone_address_new(from);
256                 char *tmp;
257                 linphone_address_clean(addr);
258                 tmp=linphone_address_as_string(addr);
259                 lc->vtable.bye_recv(lc,tmp);
260                 ms_free(tmp);
261                 linphone_address_destroy(addr);
262         }
263         linphone_call_destroy(lc->call);
264         lc->call=NULL;
265 }
266
267 static void call_failure(SalOp *op, SalError error, SalReason sr, const char *details){
268         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
269         char *msg486=_("User is busy.");
270         char *msg480=_("User is temporarily unavailable.");
271         /*char *retrymsg=_("%s. Retry after %i minute(s).");*/
272         char *msg600=_("User does not want to be disturbed.");
273         char *msg603=_("Call declined.");
274         char *msg=(char*)details;
275         LinphoneCall *call=lc->call;
276
277         if (sal_op_get_user_pointer(op)!=lc->call){
278                 ms_warning("call_failure: ignoring.");
279                 return;
280         }
281         if (lc->vtable.show) lc->vtable.show(lc);
282
283         if (error==SalErrorNoResponse){
284                 if (lc->vtable.display_status)
285                         lc->vtable.display_status(lc,_("No response."));
286         }else if (error==SalErrorProtocol){
287                 if (lc->vtable.display_status)
288                         lc->vtable.display_status(lc, details ? details : _("Protocol error."));
289         }else if (error==SalErrorFailure){
290                 switch(sr){
291                         case SalReasonDeclined:
292                                 msg=msg603;
293                                 if (lc->vtable.display_status)
294                                         lc->vtable.display_status(lc,msg603);
295                         break;
296                         case SalReasonBusy:
297                                 msg=msg486;
298                                 if (lc->vtable.display_status)
299                                         lc->vtable.display_status(lc,msg486);
300                         break;
301                         case SalReasonRedirect:
302                                 msg=_("Redirected");
303                                 if (lc->vtable.display_status)
304                                         lc->vtable.display_status(lc,msg);
305                         break;
306                         case SalReasonTemporarilyUnavailable:
307                                 msg=msg480;
308                                 if (lc->vtable.display_status)
309                                         lc->vtable.display_status(lc,msg480);
310                         break;
311                         case SalReasonNotFound:
312                                 msg=_("Not found");
313                                 if (lc->vtable.display_status)
314                                         lc->vtable.display_status(lc,msg);
315                         break;
316                         case SalReasonDoNotDisturb:
317                                 msg=msg600;
318                                 if (lc->vtable.display_status)
319                                         lc->vtable.display_status(lc,msg600);
320                         break;
321                         case SalReasonMedia:
322                                 msg=_("No common codecs");
323                                 if (lc->vtable.display_status)
324                                         lc->vtable.display_status(lc,msg);
325                         break;
326                         default:
327                                 if (lc->vtable.display_status)
328                                         lc->vtable.display_status(lc,_("Call failed."));
329                 }
330         }
331         if (lc->ringstream!=NULL) {
332                 ring_stop(lc->ringstream);
333                 lc->ringstream=NULL;
334         }
335         linphone_core_stop_media_streams(lc,call);
336         if (call!=NULL) {
337                 linphone_call_destroy(call);
338                 if (sr!=SalReasonDeclined) gstate_new_state(lc, GSTATE_CALL_ERROR, msg);
339                 else gstate_new_state(lc, GSTATE_CALL_END, NULL);
340                 lc->call=NULL;
341         }
342 }
343
344 static void auth_requested(SalOp *h, const char *realm, const char *username){
345         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
346         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
347         ms_message("auth_requested() for realm=%s, username=%s",realm,username);
348         if (ai && (ai->works || ai->usecount<3)){
349                 SalAuthInfo sai;
350                 sai.username=ai->username;
351                 sai.userid=ai->userid;
352                 sai.realm=ai->realm;
353                 sai.password=ai->passwd;
354                 ms_message("auth_requested(): authenticating realm=%s, username=%s",realm,username);
355                 sal_op_authenticate(h,&sai);
356                 ai->usecount++;
357         }else{
358                 if (lc->vtable.auth_info_requested)
359                         lc->vtable.auth_info_requested(lc,realm,username);
360         }
361 }
362
363 static void auth_success(SalOp *h, const char *realm, const char *username){
364         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h));
365         LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
366         if (ai){
367                 ms_message("%s/%s authentication works.",realm,username);
368                 ai->works=TRUE;
369         }
370 }
371
372 static void register_success(SalOp *op, bool_t registered){
373         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
374         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)sal_op_get_user_pointer(op);
375         char *msg;
376         cfg->registered=registered;
377         gstate_new_state(lc, GSTATE_REG_OK, NULL);
378         if (cfg->registered) msg=ms_strdup_printf(_("Registration on %s successful."),sal_op_get_proxy(op));
379         else msg=ms_strdup_printf(_("Unregistration on %s done."),sal_op_get_proxy(op));
380         if (lc->vtable.display_status) 
381                 lc->vtable.display_status(lc,msg);
382         ms_free(msg);
383 }
384
385 static void register_failure(SalOp *op, SalError error, SalReason reason, const char *details){
386         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
387         char *msg=ortp_strdup_printf(_("Registration on %s failed: %s"),sal_op_get_proxy(op),(details!=NULL) ? details : _("no response timeout"));
388         if (lc->vtable.display_status) lc->vtable.display_status(lc,msg);
389         gstate_new_state(lc, GSTATE_REG_FAILED, msg);
390         ms_free(msg);
391 }
392
393 static void vfu_request(SalOp *op){
394 #ifdef VIDEO_ENABLED
395         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
396         if (lc->videostream)
397                 video_stream_send_vfu(lc->videostream);
398 #endif
399 }
400
401 static void dtmf_received(SalOp *op, char dtmf){
402         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
403         if (lc->vtable.dtmf_received != NULL)
404                 lc->vtable.dtmf_received(lc, dtmf);
405 }
406
407 static void refer_received(Sal *sal, SalOp *op, const char *referto){
408         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
409         if (lc->vtable.refer_received){
410                 lc->vtable.refer_received(lc,referto);
411                 if (op) sal_refer_accept(op);
412         }
413 }
414
415 static void text_received(Sal *sal, const char *from, const char *msg){
416         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
417         linphone_core_text_received(lc,from,msg);
418 }
419
420 static void notify(SalOp *op, const char *from, const char *msg){
421         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
422
423         ms_message("get a %s notify from %s",msg,from);
424         if(lc->vtable.notify_recv)
425                 lc->vtable.notify_recv(lc,from,msg);
426 }
427
428 static void notify_presence(SalOp *op, SalSubscribeState ss, SalPresenceStatus status, const char *msg){
429         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
430         linphone_notify_recv(lc,op,ss,status);
431 }
432
433 static void subscribe_received(SalOp *op, const char *from){
434         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
435         linphone_subscription_new(lc,op,from);
436 }
437
438 static void subscribe_closed(SalOp *op, const char *from){
439         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op));
440         linphone_subscription_closed(lc,op);
441 }
442
443 static void internal_message(Sal *sal, const char *msg){
444         LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal);
445         if (lc->vtable.show)
446                 lc->vtable.show(lc);
447 }
448
449 static void ping_reply(SalOp *op){
450         LinphoneCall *call=(LinphoneCall*) sal_op_get_user_pointer(op);
451         ms_message("ping reply !");
452         if (call){
453                 if (call->state==LCStatePreEstablishing){
454                         linphone_core_start_invite(call->core,call,NULL);
455                 }
456         }
457 }
458
459 SalCallbacks linphone_sal_callbacks={
460         call_received,
461         call_ringing,
462         call_accepted,
463         call_ack,
464         call_updated,
465         call_terminated,
466         call_failure,
467         auth_requested,
468         auth_success,
469         register_success,
470         register_failure,
471         vfu_request,
472         dtmf_received,
473         refer_received,
474         text_received,
475         notify,
476         notify_presence,
477         subscribe_received,
478         subscribe_closed,
479         internal_message,
480         ping_reply
481 };
482
483