3
Copyright (C) 2000 Simon MORLAT (simon.morlat@free.fr)
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.
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.
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.
21
#include "linphonecore.h"
24
/* these are useful messages for the status */
27
extern gchar *contacting;
28
extern gchar *connected;
30
extern gchar *contacted;
32
int invite_accepted_cb(OsipDialog *dialog, transaction_t *trn, sip_t *sipmsg, void *data)
34
OsipUA *ua=osip_dialog_get_ua(dialog);
35
LinphoneCore *lc=(LinphoneCore*)ua->data;
38
LinphoneDialogParams *diaparams=(LinphoneDialogParams *)lc->call->data;
39
StreamParams *audio_params=&diaparams->audio_params;
41
linphone_core_thread_enter(lc);
43
lc->vtable.display_status(lc,connected);
45
if (lc->ringstream!=NULL){
46
ring_stop(lc->ringstream);
48
}else err=try_open_dsp(lc);
51
/* adjust rtp jitter compensation. It must be at least the latency of the sound card */
52
jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
53
lc->audiostream=audio_stream_start_with_sndcard(diaparams->profile,audio_params->localport,
54
audio_params->remoteaddr,audio_params->remoteport,
55
audio_params->pt,jitt_comp,lc->sound_conf.sndcard);
58
linphone_core_thread_leave(lc);
63
int bye_cb(OsipDialog *dialog, transaction_t *trn, sip_t *msg, void *p2)
65
OsipUA *ua=osip_dialog_get_ua(dialog);
66
LinphoneCore *lc=(LinphoneCore*)ua->data;
67
LinphoneDialogParams *diaparams;
69
linphone_core_thread_enter(lc);
70
linphone_core_lock(lc);
72
diaparams=(LinphoneDialogParams*)dialog->data;
74
/*stop ringing if necessary*/
75
if (lc->ringstream!=NULL) {
76
ring_stop(lc->ringstream);
79
if (lc->audiostream!=NULL) {
80
audio_stream_stop(lc->audiostream);
84
lc->vtable.display_status(lc,end);
87
if (diaparams!=NULL) linphone_dialog_params_destroy(diaparams);
88
restore_sound_daemons();
89
linphone_core_thread_leave(lc);
90
linphone_core_unlock(lc);
94
int faillure_cb(OsipDialog *dialog, transaction_t *trn, sip_t *msg, void *p2)
96
int err=((int*)p2)[0];
100
char *msg486=_("User is busy.");
101
char *msg480=_("User is temporarily unavaillable.");
102
char *retrymsg=_("%s. Retry after %i minute(s).");
103
char *msg600=_("User does not want to be disturbed.");
104
char *msg603=_("Call declined.");
106
OsipUA *ua=osip_dialog_get_ua(dialog);
107
LinphoneCore *lc=(LinphoneCore*)ua->data;
108
LinphoneDialogParams *diaparams;
109
linphone_core_lock(lc);
110
diaparams=(LinphoneDialogParams*)dialog->data;
111
linphone_core_thread_enter(lc);
116
lc->vtable.display_status(lc,_("User cannot be found at given address."));
119
lc->vtable.display_status(lc,_("Remote user cannot support any of proposed codecs."));
124
msg_header_getbyname(msg,"retry-after",0,&retry);
127
umsg=g_malloc(strlen(tmpmsg)+strlen(retrymsg)+13);
128
sprintf(umsg,retrymsg,tmpmsg,atoi(retry->hvalue)/60);
129
lc->vtable.display_message(lc,umsg);
132
lc->vtable.display_message(lc,tmpmsg);
136
lc->vtable.display_message(lc,msg600);
139
lc->vtable.display_status(lc,msg603);
141
case -110: /* time out, call leg is lost */
142
lc->vtable.display_status(lc,_("Timeout."));
145
lc->vtable.display_status(lc,_("Remote host was found but refused connection."));
151
reason=msg_getreason(err);
152
lc->vtable.display_status(lc,reason);
155
else g_message("faillure_cb: %s\n",strerror(-err));
157
if (lc->ringstream!=NULL) {
158
ring_stop(lc->ringstream);
160
restore_sound_daemons();
162
if (diaparams!=NULL) linphone_dialog_params_destroy(diaparams);
164
linphone_core_thread_leave(lc);
165
linphone_core_unlock(lc);
169
int invite_cb(OsipDialog *dialog, transaction_t *trn, sip_t *sipmsg, void *p)
171
from_t *from=sipmsg->from;
172
char *barmesg,*rem_url;
175
OsipUA *ua=osip_dialog_get_ua(dialog);
176
LinphoneCore *lc=(LinphoneCore*)ua->data;
177
linphone_core_lock(lc);
178
linphone_core_thread_enter(lc);
179
/* first check if we can answer successfully to this invite */
182
osip_dialog_respond(dialog,trn,486);
186
sdpctx=osip_dialog_get_body_context(dialog,"application/sdp",0);
187
/* get the result of the negociation */
188
switch(SDP_CONTEXT(sdpctx)->negoc_status)
191
g_warning("Error during sdp negociation. Cannot accept incoming call.\n");
192
osip_dialog_respond(dialog,trn,606);
195
from_2char_without_params(from,&rem_url);
197
barmesg=g_strdup_printf("%s %s.",rem_url,contacted);
199
else barmesg=g_strdup_printf("Somebody %s.",contacted);
202
lc->vtable.display_status(lc,barmesg);
203
lc->vtable.inv_recv(lc,rem_url);
207
if (try_open_dsp(lc)>0){
208
g_message("Starting local ring...");
209
lc->ringstream=ring_start(lc->sound_conf.local_ring,2,lc->sound_conf.sndcard);
215
osip_dialog_respond(dialog,trn,SDP_CONTEXT(sdpctx)->negoc_status);
219
linphone_core_unlock(lc);
220
linphone_core_thread_leave(lc);
225
int informative_cb(OsipDialog *dialog, transaction_t *trn, sip_t *msg, void *p2)
227
int err=((int*)p2)[0];
230
OsipUA *ua=osip_dialog_get_ua(dialog);
231
LinphoneCore *lc=(LinphoneCore*)ua->data;
232
linphone_core_thread_enter(lc);
233
linphone_core_lock(lc);
239
//g_message("We are in 180 switch of informative_cb()");
240
if (try_open_dsp(lc)>0)
241
lc->ringstream=ring_start(lc->sound_conf.remote_ring,2,lc->sound_conf.sndcard);
244
// this is used by cisco gateways
246
case 200: /* for the moment only REGISTER request followed by 200 OK are said to the user*/
247
if (lc->sip_conf.reg_conf.expires) lc->vtable.display_status(lc,_("Registration successfull."));
248
else lc->vtable.display_status(lc,_("Unregistration successfull."));
251
msg_getcontact(msg,0,&contact);
254
contact_2char(contact,&ctt);
255
}else ctt=sgetcopy(_("(No contact information !)"));
257
lc->vtable.display_url(lc,_("User is not reachable at the moment but he invites you\nto contact him using the following alternate resource:"),ctt);
262
g_warning("unsupported informative message.\n");
264
linphone_core_thread_leave(lc);
265
linphone_core_unlock(lc);
271
/* these are the SdpHandler callbacks: we are called in to be aware of the content
272
of the SDP messages exchanged */
274
int set_audio_offer(SdpHandler *sdph,SdpContext *ctx)
281
ua=body_handler_get_ua(BODY_HANDLER(sdph));
282
lc=(LinphoneCore*)ua->data;
284
/* set the nat address in the SDP, if given */
285
if (lc->net_conf.nat_address!=NULL && lc->net_conf.use_nat){
286
sdp_t *sdp=ctx->offer;
287
sfree(sdp->c_connection->c_addr);
288
sdp->c_connection->c_addr=sgetcopy(lc->net_conf.nat_address);
292
elem=lc->codecs_conf.audio_codecs;
294
codec=(PayloadType*) elem->data;
295
if (payload_type_usable(codec) && payload_type_enabled(codec)){
297
sdp_payload_init(&payload);
298
payload.a_rtpmap=g_strdup_printf("%s/%i/1",codec->mime_type,codec->clock_rate);
299
payload.pt=rtp_profile_get_payload_number_from_rtpmap(lc->local_profile,payload.a_rtpmap);
300
payload.localport=lc->rtp_conf.audio_rtp_port;
301
/* when setting speex, add a b=<AS>: to choose bitrate */
302
if (strcmp(codec->mime_type,"speex")==0){
303
payload.b_as_bandwidth=codec->normal_bitrate/1000;
305
sdp_handler_add_audio_payload(sdph,ctx,&payload);
306
g_free(payload.a_rtpmap);
308
elem=g_list_next(elem);
310
/* add telephone-event payload*/
311
sdp_payload_init(&payload);
312
payload.pt=rtp_profile_get_payload_number_from_mime(lc->local_profile,"telephone-event");
313
payload.a_rtpmap="telephone-event/8000";
314
payload.a_fmtp="0-11";
315
sdp_handler_add_audio_payload(sdph,ctx,&payload);
320
int set_video_offer(SdpHandler *sdph,SdpContext *ctx)
326
ua=body_handler_get_ua(BODY_HANDLER(sdph));
327
lc=(LinphoneCore*)ua->data;
328
elem=lc->codecs_conf.video_codecs;
330
codec=(PayloadType*) elem->data;
331
if (payload_type_usable(codec) && payload_type_enabled(codec)){
334
sdp_payload_init(&payload);
335
payload.a_rtpmap=g_strdup_printf("%s/%i",codec->mime_type,codec->clock_rate);
336
payload.localport=lc->rtp_conf.video_rtp_port;
337
sdp_handler_add_video_payload(sdph,ctx,&payload);
338
g_free(payload.a_rtpmap);
340
elem=g_list_next(elem);
345
int payload_is_supported(SdpPayload *payload,RtpProfile *local_profile,RtpProfile *dialog_profile)
348
if (payload->a_rtpmap!=NULL){
349
localpt=rtp_profile_get_payload_number_from_rtpmap(local_profile,payload->a_rtpmap);
352
g_warning("payload has no rtpmap.");
356
/* this payload is understood, but does the user want to use it ?? */
357
PayloadType *rtppayload;
358
rtppayload=rtp_profile_get_payload(local_profile,localpt);
359
if (rtppayload==NULL) {
360
g_warning("strange error !!");
363
if (strcmp(rtppayload->mime_type,"telephone-event")!=0){
364
if (!(payload_type_usable(rtppayload) && payload_type_enabled(rtppayload))) {
365
g_warning("payload %s is not usable or enabled.",rtppayload->mime_type);
369
/* this payload is supported in our local rtp profile, so add it to the dialog rtp
371
rtppayload=payload_type_clone(rtppayload);
372
rtp_profile_set_payload(dialog_profile,payload->pt,rtppayload);
373
/* add to the rtp payload type some other parameters (bandwidth) */
374
if (payload->b_as_bandwidth!=0) rtppayload->normal_bitrate=payload->b_as_bandwidth*1000;
380
int accept_audio_offer(SdpHandler *sdph,SdpContext *ctx,SdpPayload *payload)
382
OsipDialog *dialog=BODY_CONTEXT(ctx)->dialog;
383
LinphoneDialogParams *diaparams;
384
LinphoneCore *lc=(LinphoneCore*) BODY_HANDLER(sdph)->ua->data;
385
RtpProfile *remote_profile;
386
StreamParams *params;
388
if (dialog->data==NULL){
389
diaparams=linphone_dialog_params_new();
390
dialog->data=(void*) diaparams;
391
}else diaparams=(LinphoneDialogParams*)dialog->data;
392
if (diaparams->profile==NULL){
393
/* create a remote user agent profile */
394
diaparams->profile=remote_profile=rtp_profile_new("remote");
395
}else remote_profile=diaparams->profile;
396
/* see if this codec is supported in our local rtp profile*/
397
supported=payload_is_supported(payload,lc->local_profile,remote_profile);
399
g_message("Refusing codec %i (%s)",payload->pt,payload->a_rtpmap);
402
params=&diaparams->audio_params;
403
if (params->initialized==0){
404
/* this is the first codec we may accept*/
405
params->localport=payload->localport=lc->rtp_conf.audio_rtp_port;
406
params->remoteport=payload->remoteport;
407
params->line=payload->line;
408
params->pt=payload->pt; /* remember the first payload accepted */
409
params->remoteaddr=payload->c_addr;
410
params->initialized=1;
412
/* refuse all other audio lines*/
413
if(params->line!=payload->line) return -1;
418
int accept_video_offer(SdpHandler *sdph,SdpContext *ctx,SdpPayload *payload)
420
OsipDialog *dialog=BODY_CONTEXT(ctx)->dialog;
421
LinphoneDialogParams *diaparams;
422
LinphoneCore *lc=(LinphoneCore*) BODY_HANDLER(sdph)->ua->data;
423
RtpProfile *remote_profile;
424
StreamParams *params;
426
if (dialog->data==NULL){
427
diaparams=linphone_dialog_params_new();
428
dialog->data=(void*) diaparams;
429
}else diaparams=(LinphoneDialogParams*)dialog->data;
430
if (diaparams->profile==NULL){
431
/* create a remote user agent profile */
432
diaparams->profile=remote_profile=rtp_profile_new("remote");
433
}else remote_profile=diaparams->profile;
434
/* see if this codec is supported in our local rtp profile*/
435
supported=payload_is_supported(payload,lc->local_profile,remote_profile);
436
if (!supported) return -1;
437
params=&diaparams->video_params;
438
if (params->initialized==0){
439
/* this is the first codec we may accept*/
440
params->localport=payload->localport=lc->rtp_conf.video_rtp_port;
441
params->remoteport=payload->remoteport;
442
params->line=payload->line;
443
params->pt=payload->pt; /* remember the first payload accepted */
444
params->remoteaddr=payload->c_addr;
445
params->initialized=1;
447
/* refuse all other audio lines*/
448
if(params->line!=payload->line) return -1;
453
int read_audio_answer(SdpHandler *sdph,SdpContext *ctx,SdpPayload *payload)
455
OsipDialog *dialog=BODY_CONTEXT(ctx)->dialog;
456
LinphoneDialogParams *diaparams;
457
LinphoneCore *lc=(LinphoneCore*) BODY_HANDLER(sdph)->ua->data;
458
StreamParams *params;
460
if (dialog->data==NULL){
461
diaparams=linphone_dialog_params_new();
462
dialog->data=(void*) diaparams;
463
diaparams->profile=rtp_profile_clone_full(lc->local_profile);
464
}else diaparams=(LinphoneDialogParams*)dialog->data;
465
/* paranoid check: see if this codec is supported in our local rtp profile*/
466
supported=payload_is_supported(payload,lc->local_profile,diaparams->profile);
468
g_warning("This remote sip phone did not answer properly to my sdp offer!");
471
params=&diaparams->audio_params;
472
if (params->initialized==0){
473
/* this is the first codec we may accept*/
474
params->localport=lc->rtp_conf.audio_rtp_port;
475
params->remoteport=payload->remoteport;
476
params->line=payload->line;
477
params->pt=payload->pt; /* remember the first payload accepted */
478
params->remoteaddr=payload->c_addr;
479
params->initialized=1;
483
int read_video_answer(SdpHandler *sdph,SdpContext *ctx,SdpPayload *payload)
485
OsipDialog *dialog=BODY_CONTEXT(ctx)->dialog;
486
LinphoneDialogParams *diaparams;
487
LinphoneCore *lc=(LinphoneCore*) BODY_HANDLER(sdph)->ua->data;
488
StreamParams *params;
490
if (dialog->data==NULL){
491
diaparams=linphone_dialog_params_new();
492
dialog->data=(void*) diaparams;
493
diaparams->profile=rtp_profile_clone_full(lc->local_profile);
494
}else diaparams=(LinphoneDialogParams*)dialog->data;
495
/* paranoid check: see if this codec is supported in our local rtp profile*/
496
supported=payload_is_supported(payload,lc->local_profile,diaparams->profile);
498
g_warning("This remote sip phone did not answer properly to my sdp offer!");
501
params=&diaparams->video_params;
502
if (params->initialized==0){
503
/* this is the first codec we may accept*/
504
params->localport=lc->rtp_conf.video_rtp_port;
505
params->remoteport=payload->remoteport;
506
params->line=payload->line;
507
params->pt=payload->pt; /* remember the first payload accepted */
508
params->remoteaddr=payload->c_addr;
509
params->initialized=1;