exevents.c 33.1 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
linphone
Copyright (C) 2000  Simon MORLAT (simon.morlat@free.fr)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "exevents.h"
#include "linphonecore.h"
#include "private.h"
#include "mediastreamer2/mediastream.h"
#include <eXosip2/eXosip.h>
#include <osipparser2/osip_message.h>
smorlat's avatar
smorlat committed
26
#include <osipparser2/osip_parser.h>
aymeric's avatar
aymeric committed
27 28 29

static int linphone_answer_sdp(LinphoneCore *lc, eXosip_event_t *ev, sdp_message_t *sdp);

smorlat's avatar
smorlat committed
30 31 32 33
static bool_t linphone_call_matches_event(LinphoneCall *call, eXosip_event_t *ev){
	return call->cid==ev->cid;
}

aymeric's avatar
aymeric committed
34
static void linphone_call_proceeding(LinphoneCore *lc, eXosip_event_t *ev){
smorlat's avatar
smorlat committed
35 36 37 38 39
	if (lc->call==NULL || (lc->call->cid!=-1 && !linphone_call_matches_event(lc->call,ev)) ) {
		ms_warning("This call has been canceled.");
		eXosip_lock();
		eXosip_call_terminate(ev->cid,ev->did);
		eXosip_unlock();
aymeric's avatar
aymeric committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		return;
	}
	lc->call->cid=ev->cid;
	lc->call->did=ev->did;
	lc->call->tid=ev->tid;
}

static void linphone_connect_incoming(LinphoneCore *lc){
	lc->vtable.show(lc);
	lc->vtable.display_status(lc,_("Connected."));
	lc->call->state=LCStateAVRunning;
	if (lc->ringstream!=NULL){
		ring_stop(lc->ringstream);
		lc->ringstream=NULL;
	}
	if (lc->audiostream->ticker!=NULL){
		/*case where we accepted early media */
		linphone_core_stop_media_streams(lc);
		linphone_core_init_media_streams(lc);
	}
	linphone_core_start_media_streams(lc,lc->call);
}

int linphone_call_accepted(LinphoneCore *lc, eXosip_event_t *ev)
{
	LinphoneCall *call=lc->call;
smorlat's avatar
smorlat committed
66
	sdp_message_t *sdp;
aymeric's avatar
aymeric committed
67 68 69 70 71 72 73 74
	const char *sdpanswer=NULL;
	osip_message_t *msg=NULL;
	int err;
	if (call==NULL){
		ms_warning("No call to accept.");
		return 0;
	}
	linphone_call_proceeding(lc,ev);
smorlat's avatar
smorlat committed
75
	if (!linphone_call_matches_event(lc->call,ev)) return 0;
aymeric's avatar
aymeric committed
76 77 78 79 80
	call->auth_pending=FALSE;
	if (call->state==LCStateAVRunning){
		return 0; /*already accepted*/
	}
	linphone_call_init_media_params(call);
smorlat's avatar
smorlat committed
81
	sdp=eXosip_get_sdp_info(ev->response);
aymeric's avatar
aymeric committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	if (!lc->sip_conf.sdp_200_ack){
		err=0;
		sdp_context_read_answer(call->sdpctx,sdp);
	}else{
		/*we receive a 200OK with an sdp offer*/
		err=linphone_answer_sdp(lc,ev,sdp);
		if (err==0) sdpanswer=call->sdpctx->answerstr;
	}
	if (err==0){
		gstate_new_state(lc, GSTATE_CALL_OUT_CONNECTED, NULL);
		linphone_connect_incoming(lc);
	}
	/*send the ack once streams are started*/
	eXosip_call_build_ack(ev->did,&msg);
	if (sdpanswer!=NULL) linphone_set_sdp(msg,sdpanswer);
	eXosip_call_send_ack(ev->did,msg);
	if (err!=0){
		/*send a bye*/
		ms_error("Incompatible SDP offer received in 200Ok, need to abort the call");
		linphone_core_terminate_call(lc,NULL);
	}
smorlat's avatar
smorlat committed
103
	sdp_message_free(sdp);
aymeric's avatar
aymeric committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	return 0;
}


int linphone_call_terminated(LinphoneCore *lc, eXosip_event_t *ev)
{
	/*stop ringing if necessary*/
	if (lc->call!=NULL){
		if (lc->call->cid!=ev->cid){
			/* this is not current call */
			ms_message("call %i terminated, this was not current call.",ev->cid);
			return 0;
		}
	}
	
	ms_message("Current call terminated...");
	if (lc->ringstream!=NULL) {
		ring_stop(lc->ringstream);
		lc->ringstream=NULL;
	}
	linphone_core_stop_media_streams(lc);
	lc->vtable.show(lc);
	lc->vtable.display_status(lc,_("Call terminated."));
	gstate_new_state(lc, GSTATE_CALL_END, NULL);
	if (lc->vtable.bye_recv!=NULL){
		char *from;
		osip_from_to_str(ev->request->from,&from);
		lc->vtable.bye_recv(lc,from);
		osip_free(from);
	}
	if (lc->call!=NULL){
		linphone_call_destroy(lc->call);
		lc->call=NULL;
	}
	return 0;
}


int linphone_call_released(LinphoneCore *lc, int cid){
	LinphoneCall *call=lc->call;
	if (call!=NULL && call->cid==cid){
		
		linphone_call_destroy(lc->call);
		lc->call=NULL;
		lc->vtable.display_status(lc,_("Could not reach destination."));
		gstate_new_state(lc, GSTATE_CALL_ERROR, NULL);
	}
	return 0;
}

int linphone_call_failure(LinphoneCore *lc, eXosip_event_t *ev)
{
	const char *reason="";
	char *msg486=_("User is busy.");
	char *msg480=_("User is temporarily unavailable.");
	char *msg487=_("Request Cancelled.");
	/*char *retrymsg=_("%s. Retry after %i minute(s).");*/
	char *msg600=_("User does not want to be disturbed.");
	char *msg603=_("Call declined.");
	char* tmpmsg=msg486;
	int code;
smorlat's avatar
smorlat committed
165 166 167 168 169 170 171 172 173
	LinphoneCall *call=lc->call;

	if (call){
		/*check that the faillure is related to this call, not an old one*/
		if (!linphone_call_matches_event(call,ev)) {
			ms_warning("Failure reported for an old call.");
			return 0;
		}
	}
aymeric's avatar
aymeric committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

	if (ev->response){
		code=osip_message_get_status_code(ev->response);
		reason=osip_message_get_reason_phrase(ev->response);
	}else code=-110;
	lc->vtable.show(lc);
	
	switch(code)
	{
		case 401:
		case 407:
			if (lc->call!=NULL)
				linphone_process_authentication(lc,ev);
			return 0;
			break;
		case 400:
			lc->vtable.display_status(lc,_("Bad request"));
		break;
		case 404:
			lc->vtable.display_status(lc,_("User cannot be found at given address."));
		break;
		case 415:
			lc->vtable.display_status(lc,_("Remote user cannot support any of proposed codecs."));
		break;
		case 480:
			tmpmsg=msg480;
		case 486:
			/*
			msg_header_getbyname(msg,"retry-after",0,&retry);
			if (retry!=NULL)
			{
				umsg=g_malloc(strlen(tmpmsg)+strlen(retrymsg)+13);
				sprintf(umsg,retrymsg,tmpmsg,atoi(retry->hvalue)/60);
				lc->vtable.display_message(lc,umsg);
				ms_free(umsg);
			}*/		
			lc->vtable.display_message(lc,tmpmsg);
		break;
		case 487:
			lc->vtable.display_status(lc,msg487);
		break;	
		case 600:
			lc->vtable.display_message(lc,msg600);
		break;
		case 603:
			lc->vtable.display_status(lc,msg603);
		break;
		case -110:  /* time out, call leg is lost */
			lc->vtable.display_status(lc,_("Timeout."));
		break;
		case -111:
			lc->vtable.display_status(lc,_("Remote host was found but refused connection."));
		break;
		
		default:
			if (code>0)
			{
				lc->vtable.display_status(lc,reason);
			}
			else ms_warning("failure_cb unknown code=%i\n",code);	
	}
	if (lc->ringstream!=NULL) {
		ring_stop(lc->ringstream);
		lc->ringstream=NULL;
	}
	linphone_core_stop_media_streams(lc);
smorlat's avatar
smorlat committed
240 241
	if (call!=NULL) {
		linphone_call_destroy(call);
aymeric's avatar
aymeric committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
		gstate_new_state(lc, GSTATE_CALL_ERROR, NULL);
		lc->call=NULL;
	}
	return 0;
}

extern sdp_handler_t linphone_sdphandler;

static int linphone_answer_sdp(LinphoneCore *lc, eXosip_event_t *ev, sdp_message_t *sdp){
	int status=200;
	sdp_context_t *ctx=NULL;
	
	ctx=lc->call->sdpctx;
	/* get the result of the negociation */
	sdp_context_get_answer(ctx,sdp);
	status=sdp_context_get_status(ctx);

	if (status==200){
		linphone_core_init_media_streams(lc);
		return 0;
	}else{
		if (status==-1) status=415;
	}
	return -1;
}

int linphone_inc_new_call(LinphoneCore *lc, eXosip_event_t *ev)
{
smorlat's avatar
smorlat committed
270
	sdp_message_t *sdp=NULL;
aymeric's avatar
aymeric committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	osip_from_t *from_url=ev->request->from;
	char *barmesg;
	char *from;
	char *to;
	int err;

	osip_from_to_str(ev->request->from,&from);
	osip_to_to_str(ev->request->to,&to);	
	
	/* first check if we can answer successfully to this invite */
	if (lc->presence_mode!=LINPHONE_STATUS_ONLINE){
		ms_message("Not present !! presence mode : %d\n",lc->presence_mode);
		eXosip_lock();
		if (lc->presence_mode==LINPHONE_STATUS_BUSY)
			eXosip_call_send_answer(ev->tid,486,NULL);
		else if (lc->presence_mode==LINPHONE_STATUS_AWAY
			 ||lc->presence_mode==LINPHONE_STATUS_BERIGHTBACK
			 ||lc->presence_mode==LINPHONE_STATUS_ONTHEPHONE
			 ||lc->presence_mode==LINPHONE_STATUS_OUTTOLUNCH
			 ||lc->presence_mode==LINPHONE_STATUS_OFFLINE)
		  eXosip_call_send_answer(ev->tid,480,NULL);
		else if (lc->presence_mode==LINPHONE_STATUS_NOT_DISTURB)
		  eXosip_call_send_answer(ev->tid,480,NULL);
		else if (lc->alt_contact!=NULL && lc->presence_mode==LINPHONE_STATUS_MOVED)
		  {
			osip_message_t *msg;
			eXosip_call_build_answer(ev->tid,302,&msg);
			osip_message_set_contact(msg,lc->alt_contact);
			eXosip_call_send_answer(ev->tid,302,msg);
		  }
		else if (lc->alt_contact!=NULL && lc->presence_mode==LINPHONE_STATUS_ALT_SERVICE)
		  {
			osip_message_t *msg;
			eXosip_call_build_answer(ev->tid,380,&msg);
			osip_message_set_contact(msg,lc->alt_contact);
			eXosip_call_send_answer(ev->tid,380,msg);
		  }
		else
		  eXosip_call_send_answer(ev->tid,486,NULL);
		eXosip_unlock();
		goto end;
	}
	if (lc->call!=NULL){/*busy*/
		eXosip_lock();
		eXosip_call_send_answer(ev->tid,486,NULL);
		eXosip_unlock();
		goto end;
	}
	lc->call=linphone_call_new_incoming(lc,from,to,ev->cid,ev->did,ev->tid);
smorlat's avatar
smorlat committed
320
	sdp=eXosip_get_sdp_info(ev->request);
aymeric's avatar
aymeric committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	if (sdp==NULL){
		ms_message("No sdp body in invite, 200-ack scheme");
		err=0;
	}else{
		err=linphone_answer_sdp(lc,ev,sdp);
	}
	if (!err){
		char *tmp;
		if (from_2char_without_params(from_url,&tmp)!=0){
			tmp=ms_strdup("Unknown user");
		}
		gstate_new_state(lc, GSTATE_CALL_IN_INVITE, tmp);
		barmesg=ortp_strdup_printf("%s %s",tmp,_("is contacting you."));
		lc->vtable.show(lc);
		lc->vtable.display_status(lc,barmesg);
		lc->vtable.inv_recv(lc,tmp);
		ms_free(barmesg);
		osip_free(tmp);
		
		linphone_call_set_state(lc->call,LCStateRinging);
		eXosip_lock();
		eXosip_call_send_answer(ev->tid,180,NULL);
		eXosip_unlock();
		/* play the ring */
		if (lc->sound_conf.ring_sndcard!=NULL){
			ms_message("Starting local ring...");
			lc->ringstream=ring_start(lc->sound_conf.local_ring,2000,lc->sound_conf.ring_sndcard);
		}
	}else{
		ms_error("Error during sdp negociation. ");
		eXosip_lock();
		eXosip_call_send_answer(ev->tid,415,NULL);
		eXosip_unlock();
		linphone_call_destroy(lc->call);
		lc->call=NULL;
	}
	end:
	osip_free(from);
	osip_free(to);
smorlat's avatar
smorlat committed
360
	if (sdp) sdp_message_free(sdp);
aymeric's avatar
aymeric committed
361 362 363 364 365 366 367 368
	return 0;
}

void linphone_handle_ack(LinphoneCore *lc, eXosip_event_t *ev){
	sdp_message_t *sdp=eXosip_get_sdp_info(ev->ack);
	if (sdp){
		sdp_context_read_answer(lc->call->sdpctx,sdp);
		linphone_connect_incoming(lc);
smorlat's avatar
smorlat committed
369
		sdp_message_free(sdp);
aymeric's avatar
aymeric committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	}
}

void linphone_handle_reinvite(LinphoneCore *lc, eXosip_event_t *ev){
	sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
	sdp_context_t *ctx;
	LinphoneCall *call=lc->call;
	char *answer;
	int status;
	if (sdp==NULL){
		ms_warning("No sdp in reinvite !");
		eXosip_lock();
		eXosip_call_send_answer(ev->tid,603,NULL);
		eXosip_unlock();
		return;
	}
	ctx=call->sdpctx;
	/* get the result of the negociation */
	linphone_call_init_media_params(call);
	answer=sdp_context_get_answer(ctx,sdp);
	status=sdp_context_get_status(ctx);
	if (status==200){
		osip_message_t *msg=NULL;
		linphone_core_stop_media_streams(lc);
		linphone_core_init_media_streams(lc);
		eXosip_lock();
		if (eXosip_call_build_answer(ev->tid,200,&msg)<0){
			ms_warning("Reinvite for closed call ?");
                        eXosip_unlock();
                        linphone_core_stop_media_streams(lc);
smorlat's avatar
smorlat committed
400
			sdp_message_free(sdp);
aymeric's avatar
aymeric committed
401 402 403 404 405 406 407 408 409 410 411 412
			return ;
		}
		answer=call->sdpctx->answerstr;	/* takes the sdp already computed*/
		linphone_set_sdp(msg,answer);
		eXosip_call_send_answer(ev->tid,200,msg);
		eXosip_unlock();
		linphone_core_start_media_streams(lc,call);
	}else{
		eXosip_lock();
		eXosip_call_send_answer(ev->tid,status,NULL);
		eXosip_unlock();
	}
smorlat's avatar
smorlat committed
413
	sdp_message_free(sdp);
aymeric's avatar
aymeric committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
}

void linphone_do_automatic_redirect(LinphoneCore *lc, const char *contact){
	char *msg=ortp_strdup_printf(_("Redirected to %s..."),contact);
	lc->vtable.display_status(lc,msg);
	ms_free(msg);
	if (lc->call!=NULL) linphone_call_destroy(lc->call);
	lc->call=NULL;
	linphone_core_invite(lc,contact);
}

void linphone_call_redirected(LinphoneCore *lc, eXosip_event_t *ev){
	int code=osip_message_get_status_code(ev->response);
	char *contact=NULL;
	osip_contact_t *ct;
	osip_message_get_contact(ev->response,0,&ct);
	if (ct) osip_contact_to_str(ct,&contact);
	switch(code){
		case 380:
			lc->vtable.display_url(lc,_("User is not reachable at the moment but he invites you\nto contact him using the following alternate resource:"),contact);
			if (lc->call!=NULL) linphone_call_destroy(lc->call);
			lc->call=NULL;
			break;
		case 302:
			linphone_do_automatic_redirect(lc,contact);
			break;
	}
	if (contact) osip_free(contact);
}


/* these are the SdpHandler callbacks: we are called in to be aware of the content
of the SDP messages exchanged */

int linphone_set_audio_offer(sdp_context_t *ctx)
{
	LinphoneCall *call=(LinphoneCall*)sdp_context_get_user_pointer(ctx);
	LinphoneCore *lc=call->core;
	PayloadType *codec;
	MSList *elem;
	sdp_payload_t payload;
	
	
	elem=lc->codecs_conf.audio_codecs;
	while(elem!=NULL){
		codec=(PayloadType*) elem->data;
		if (linphone_core_check_payload_type_usability(lc,codec) && payload_type_enabled(codec)){
			sdp_payload_init(&payload);
			payload.a_rtpmap=ortp_strdup_printf("%s/%i/1",codec->mime_type,codec->clock_rate);
			payload.pt=rtp_profile_get_payload_number_from_rtpmap(lc->local_profile,payload.a_rtpmap);
smorlat's avatar
smorlat committed
464 465
			payload.localport=call->audio_params.natd_port > 0 ? 
						call->audio_params.natd_port : lc->rtp_conf.audio_rtp_port;
aymeric's avatar
aymeric committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
			if (strcasecmp(codec->mime_type,"iLBC")==0){
				/* prefer the 30 ms mode */
				payload.a_fmtp="ptime=30";
			}
			sdp_context_add_audio_payload(ctx,&payload);
			ms_free(payload.a_rtpmap);
		}
		elem=ms_list_next(elem);
	}
	/* add telephone-event payload*/
	sdp_payload_init(&payload);
	payload.pt=rtp_profile_get_payload_number_from_mime(lc->local_profile,"telephone-event");
	payload.a_rtpmap="telephone-event/8000";
	payload.a_fmtp="0-11";
	if (lc->dw_audio_bw>0) payload.b_as_bandwidth=lc->dw_audio_bw;
	sdp_context_add_audio_payload(ctx,&payload);
	return 0;
}

static int find_payload_type_number(RtpProfile *prof, PayloadType *pt){
	int candidate=-1,i;
	PayloadType *it;
	for(i=0;i<127;++i){
		it=rtp_profile_get_payload(prof,i);
		if (it!=NULL && strcasecmp(pt->mime_type,it->mime_type)==0 
			&& (pt->clock_rate==it->clock_rate || pt->clock_rate<=0) ){
			if ( (pt->recv_fmtp && it->recv_fmtp && strcasecmp(pt->recv_fmtp,it->recv_fmtp)==0) ||
				(pt->recv_fmtp==NULL && it->recv_fmtp==NULL) ){
				/*exact match*/
				return i;
			}else candidate=i;
		}
	}
	if (candidate==-1) ms_fatal("Should not happen.");
	return candidate;
}

static int find_payload_type_number_best_match(RtpProfile *prof, const char *rtpmap, const char *fmtp){
	int localpt=rtp_profile_get_payload_number_from_rtpmap(prof,rtpmap);
	PayloadType *pt;
	char value[10];
	if (localpt<0) return -1;
	pt=rtp_profile_get_payload(prof,localpt);
	if (strcasecmp(pt->mime_type,"H264")==0){
		/*hack for H264: need to answer with same packetization-mode*/
smorlat's avatar
smorlat committed
511 512 513 514
		PayloadType tmp;
		memset(&tmp,0,sizeof(tmp));
		tmp.mime_type="H264";
		tmp.clock_rate=pt->clock_rate;
aymeric's avatar
aymeric committed
515 516 517
		if (fmtp && fmtp_get_value(fmtp,"packetization-mode",value,sizeof(value))){
			tmp.recv_fmtp=(atoi(value)==1) ? "packetization-mode=1" : NULL;
		}
smorlat's avatar
smorlat committed
518
		localpt=find_payload_type_number(prof,&tmp);
aymeric's avatar
aymeric committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	}
	return localpt;
}

int linphone_set_video_offer(sdp_context_t *ctx)
{
	LinphoneCall *call=(LinphoneCall*)sdp_context_get_user_pointer(ctx);
	LinphoneCore *lc=call->core;
	PayloadType *codec;
	MSList *elem;
	bool_t firsttime=TRUE;	

	if (!linphone_core_video_enabled(lc)) return -1;

	for(elem=lc->codecs_conf.video_codecs;elem!=NULL;elem=ms_list_next(elem)){
		codec=(PayloadType*) elem->data;
		if (linphone_core_check_payload_type_usability(lc,codec) && payload_type_enabled(codec)){
			sdp_payload_t payload;
			sdp_payload_init(&payload);
			payload.line=1;
			payload.a_rtpmap=ortp_strdup_printf("%s/%i",codec->mime_type,codec->clock_rate);
smorlat's avatar
smorlat committed
540 541
			payload.localport=call->video_params.natd_port>0 ? 
					call->video_params.natd_port : lc->rtp_conf.video_rtp_port;
aymeric's avatar
aymeric committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
			payload.pt=find_payload_type_number(lc->local_profile,codec);
			payload.a_fmtp=codec->recv_fmtp;
			if(firsttime){
				firsttime=FALSE;
				if (lc->dw_video_bw>0)
					payload.b_as_bandwidth=lc->dw_video_bw;
			}
			sdp_context_add_video_payload(ctx,&payload);
			ms_free(payload.a_rtpmap);
		}
	}
	return 0;
}

typedef enum {
	Unsupported,
	Supported,
	SupportedAndValid  /* valid= the presence of this codec is enough to make a call */
}SupportLevel;

SupportLevel linphone_payload_is_supported(LinphoneCore *lc, sdp_payload_t *payload,RtpProfile *local_profile,RtpProfile *dialog_profile, bool_t answering, PayloadType **local_payload_type)
{
	int localpt;
	SupportLevel ret;
	if (payload->a_rtpmap!=NULL){
		localpt=find_payload_type_number_best_match(local_profile,payload->a_rtpmap,payload->a_fmtp);
	}else{
		localpt=payload->pt;
		ms_warning("payload has no rtpmap.");
	}
	
	if (localpt>=0 && localpt <128 ){
		/* this payload is understood, but does the user want to use it ?? */
		PayloadType *rtppayload;
		rtppayload=rtp_profile_get_payload(local_profile,localpt);
		if (rtppayload==NULL) {
			ms_warning("strange error !!");
			return Unsupported;
		}
		*local_payload_type=rtppayload;
		if (strcmp(rtppayload->mime_type,"telephone-event")!=0){
			if (answering && !linphone_core_check_payload_type_usability(lc,rtppayload) ){
				ms_warning("payload %s is not usable",rtppayload->mime_type);
				return Unsupported;
			}
			if ( !payload_type_enabled(rtppayload)) {
				ms_warning("payload %s is not enabled.",rtppayload->mime_type);
				return Unsupported;
			}
			ret=SupportedAndValid;
		}else ret=Supported;
		if (dialog_profile!=NULL){
			int dbw,ubw;
			/* this payload is supported in our local rtp profile, so add it to the dialog rtp
			profile */
			rtppayload=payload_type_clone(rtppayload);
smorlat's avatar
smorlat committed
598 599 600
			if (rtp_profile_get_payload(dialog_profile,payload->pt)!=NULL){
				ms_error("Payload %s type already entered, should not happen !",rtppayload->mime_type);
			}
aymeric's avatar
aymeric committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
			rtp_profile_set_payload(dialog_profile,payload->pt,rtppayload);
			/* add to the rtp payload type some other parameters (bandwidth) */
			if (rtppayload->type==PAYLOAD_VIDEO){
				dbw=lc->dw_video_bw;
				ubw=lc->up_video_bw;
			}else{ 
				dbw=lc->dw_audio_bw;
				ubw=lc->up_audio_bw;
			}
			if (payload->b_as_bandwidth!=0){
				ms_message("Remote bandwidth constraint: %i",payload->b_as_bandwidth);
				/*obey to remote bandwidth constraint AND our own upbandwidth constraint*/
				rtppayload->normal_bitrate=1000*get_min_bandwidth(
					payload->b_as_bandwidth, ubw);
			}else{
				/*limit to upload bandwidth if exist, else no limit*/
smorlat's avatar
smorlat committed
617 618 619 620 621 622
				if (ubw>0) rtppayload->normal_bitrate=1000*ubw;
				else {
					if (rtppayload->type!=PAYLOAD_VIDEO){
						rtppayload->normal_bitrate=-1; /*allow speex to use maximum bitrate*/
					}
				}
aymeric's avatar
aymeric committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
			}
			if (payload->a_fmtp!=NULL){
				payload_type_set_send_fmtp(rtppayload,payload->a_fmtp);
			}
			payload->a_fmtp=rtppayload->recv_fmtp;
			if (payload->a_ptime>0){
				char tmp[30];
				snprintf(tmp,sizeof(tmp),"ptime=%i",payload->a_ptime);
				payload_type_append_send_fmtp(rtppayload,tmp);
				ms_message("%s attribute added to fmtp",tmp);
			}
		}
		return ret;
	}
	return Unsupported;
}

int linphone_accept_audio_offer(sdp_context_t *ctx,sdp_payload_t *payload)
{
	RtpProfile *remote_profile;
	StreamParams *params;
	SupportLevel supported;
	LinphoneCall *call=(LinphoneCall*)sdp_context_get_user_pointer(ctx);
	LinphoneCore *lc=call->core;
	PayloadType *lpt=NULL;
smorlat's avatar
smorlat committed
648 649

	params=&call->audio_params;
aymeric's avatar
aymeric committed
650 651 652 653 654 655 656
	remote_profile=call->profile;
	/* see if this codec is supported in our local rtp profile*/
	supported=linphone_payload_is_supported(lc,payload,lc->local_profile,remote_profile,TRUE,&lpt);
	if (supported==Unsupported) {
		ms_message("Refusing audio codec %i (%s)",payload->pt,payload->a_rtpmap);
		return -1;
	}
smorlat's avatar
smorlat committed
657
	if (lc->sip_conf.only_one_codec && params->initialized){
smorlat's avatar
smorlat committed
658
		ms_message("Only one codec has to be accepted.");
smorlat's avatar
smorlat committed
659 660 661
		return -1;
	}
	if (supported==SupportedAndValid) {		
aymeric's avatar
aymeric committed
662 663
		if (params->initialized==0){
			/* this is the first codec we accept, it is going to be used*/
smorlat's avatar
smorlat committed
664 665 666
			params->localport=lc->rtp_conf.audio_rtp_port;
			payload->localport=params->natd_port>0 ? 
				params->natd_port : lc->rtp_conf.audio_rtp_port;
aymeric's avatar
aymeric committed
667 668 669
			params->line=payload->line;
			params->pt=payload->pt; /* remember the first payload accepted */
			if (payload->relay_host!=NULL){
smorlat's avatar
smorlat committed
670
				strncpy(params->remoteaddr,payload->relay_host,sizeof(params->remoteaddr)-1);
aymeric's avatar
aymeric committed
671 672 673 674
				params->remoteport=payload->relay_port;
				params->remotertcpport=payload->relay_port;
				params->relay_session_id=payload->relay_session_id;
			}else{
smorlat's avatar
smorlat committed
675
				strncpy(params->remoteaddr,payload->c_addr,sizeof(params->remoteaddr)-1);
aymeric's avatar
aymeric committed
676 677 678 679 680
				params->remoteport=payload->remoteport;
				params->remotertcpport=payload->remoteport+1;
			}
			params->initialized=1;
			/* we can now update the allocated bandwidth for audio, and then video*/
681 682 683
			linphone_core_update_allocated_audio_bandwidth_in_call(lc,lpt);
			/* give our download bandwidth constraint*/
			payload->b_as_bandwidth=(lc->dw_audio_bw>0) ? lc->dw_audio_bw : 0;
aymeric's avatar
aymeric committed
684 685
		}else{
			/* refuse all other audio lines*/
smorlat's avatar
smorlat committed
686 687 688 689 690
			if(params->line!=payload->line) {
				ms_message("Only one audio line can be accepted.");
				abort();
				return -1;
			}
aymeric's avatar
aymeric committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
		}
	}
	return 0;
}

int linphone_accept_video_offer(sdp_context_t *ctx,sdp_payload_t *payload)
{
	LinphoneCall *call=(LinphoneCall*)sdp_context_get_user_pointer(ctx);
	LinphoneCore *lc=call->core;
	RtpProfile *remote_profile;
	StreamParams *params;
	SupportLevel supported;
	PayloadType *lpt=NULL;

	if (!linphone_core_video_enabled(lc)) return -1;

smorlat's avatar
smorlat committed
707
	params=&call->video_params;
aymeric's avatar
aymeric committed
708 709 710 711 712 713 714
	remote_profile=call->profile;
	/* see if this codec is supported in our local rtp profile*/
	supported=linphone_payload_is_supported(lc,payload,lc->local_profile,remote_profile,TRUE,&lpt);
	if (supported==Unsupported) {
		ms_message("Refusing video codec %i (%s)",payload->pt,payload->a_rtpmap);
		return -1;
	}
smorlat's avatar
smorlat committed
715 716 717
	if (lc->sip_conf.only_one_codec && params->initialized){
		return -1;
	}
aymeric's avatar
aymeric committed
718 719 720
	if (supported==SupportedAndValid){
		if (params->initialized==0){
			/* this is the first codec we may accept*/
smorlat's avatar
smorlat committed
721 722
			params->localport=lc->rtp_conf.video_rtp_port;
			payload->localport=params->natd_port>0 ? params->natd_port : lc->rtp_conf.video_rtp_port;
aymeric's avatar
aymeric committed
723 724 725
			params->line=payload->line;
			params->pt=payload->pt; /* remember the first payload accepted */
			if (payload->relay_host!=NULL){
smorlat's avatar
smorlat committed
726
				strncpy(params->remoteaddr,payload->relay_host,sizeof(params->remoteaddr)-1);
aymeric's avatar
aymeric committed
727 728 729 730
				params->remoteport=payload->relay_port;
				params->remotertcpport=payload->relay_port;
				params->relay_session_id=payload->relay_session_id;
			}else{
smorlat's avatar
smorlat committed
731
				strncpy(params->remoteaddr,payload->c_addr,sizeof(params->remoteaddr)-1);
aymeric's avatar
aymeric committed
732 733 734 735
				params->remoteport=payload->remoteport;
				params->remotertcpport=params->remoteport+1;
			}
			params->initialized=1;
736
			payload->b_as_bandwidth=(lc->dw_video_bw>0) ? lc->dw_video_bw : 0;
aymeric's avatar
aymeric committed
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
		}else{
			/* refuse all other video lines*/
			if(params->line!=payload->line) return -1;
		}
	}
	return 0;
}

int linphone_read_audio_answer(sdp_context_t *ctx,sdp_payload_t *payload)
{
	LinphoneCall *call=(LinphoneCall*)sdp_context_get_user_pointer(ctx);
	LinphoneCore *lc=call->core;
	StreamParams *params;
	SupportLevel supported;
	PayloadType *lpt=NULL;
	
	/* paranoid check: see if this codec is supported in our local rtp profile*/
	supported=linphone_payload_is_supported(lc, payload,lc->local_profile,call->profile,FALSE,&lpt);
	if (supported==Unsupported) {
		ms_warning("This remote sip phone did not answer properly to my sdp offer: rtpmap=%s",payload->a_rtpmap);
		return 0;
	}
	if (supported==SupportedAndValid){
		params=&call->audio_params;
		if (params->initialized==0){
			/* this is the first codec we accept, this is the one that is going to be used (at least for sending
			data.*/
			params->localport=lc->rtp_conf.audio_rtp_port;
			params->line=payload->line;
			params->pt=payload->pt; /* remember the first payload accepted */
			if (payload->relay_host!=NULL){
smorlat's avatar
smorlat committed
768
				strncpy(params->remoteaddr,payload->relay_host,sizeof(params->remoteaddr)-1);
aymeric's avatar
aymeric committed
769 770 771 772
				params->remoteport=payload->relay_port;
				params->remotertcpport=payload->relay_port;
				params->relay_session_id=payload->relay_session_id;
			}else{
smorlat's avatar
smorlat committed
773
				strncpy(params->remoteaddr,payload->c_addr,sizeof(params->remoteaddr)-1);
aymeric's avatar
aymeric committed
774 775 776 777 778
				params->remoteport=payload->remoteport;
				params->remotertcpport=payload->remoteport+1;
			}
			params->initialized=1;
			/* we can now update the allocated bandwidth for audio, and then video*/
779
			linphone_core_update_allocated_audio_bandwidth_in_call(lc,lpt);
aymeric's avatar
aymeric committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
		}
	}
	return 0;
}

int linphone_read_video_answer(sdp_context_t *ctx,sdp_payload_t *payload)
{
	LinphoneCall *call=(LinphoneCall*)sdp_context_get_user_pointer(ctx);
	LinphoneCore *lc=call->core;
	StreamParams *params;
	SupportLevel supported;
	PayloadType *lpt=NULL;
	
	/* paranoid check: see if this codec is supported in our local rtp profile*/
	supported=linphone_payload_is_supported(lc, payload,lc->local_profile,call->profile,FALSE,&lpt);
	if (supported==Unsupported) {
		ms_warning("This remote sip phone did not answer properly to my sdp offer: rtpmap=%s",payload->a_rtpmap);
		return 0;
	}
	if (supported==SupportedAndValid){
		params=&call->video_params;
		if (params->initialized==0){
			/* this is the first codec we may accept*/
			params->localport=lc->rtp_conf.video_rtp_port;
			params->line=payload->line;
			params->pt=payload->pt; /* remember the first payload accepted */
			if (payload->relay_host!=NULL){
smorlat's avatar
smorlat committed
807
				strncpy(params->remoteaddr,payload->relay_host,sizeof(params->remoteaddr)-1);
aymeric's avatar
aymeric committed
808 809 810 811
				params->remoteport=payload->relay_port;
				params->remotertcpport=payload->relay_port;
				params->relay_session_id=payload->relay_session_id;
			}else{
smorlat's avatar
smorlat committed
812
				strncpy(params->remoteaddr,payload->c_addr,sizeof(params->remoteaddr)-1);
aymeric's avatar
aymeric committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826
				params->remoteport=payload->remoteport;
				params->remotertcpport=payload->remoteport+1;
			}
			params->initialized=1;
		}
	}
	return 0;
}

void linphone_call_ringing(LinphoneCore *lc, eXosip_event_t *ev){
	sdp_message_t *sdp=eXosip_get_sdp_info(ev->response);
	LinphoneCall *call=lc->call;
	
	linphone_call_proceeding(lc,ev);
smorlat's avatar
smorlat committed
827
	if (call==NULL) return;
aymeric's avatar
aymeric committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
	if (sdp==NULL){
		if (lc->ringstream!=NULL) return;	/*already ringing !*/
		if (lc->sound_conf.play_sndcard!=NULL){
			ms_message("Remote ringing...");
			lc->ringstream=ring_start(lc->sound_conf.remote_ring,2000,lc->sound_conf.play_sndcard);
		}
	}else{
		/*accept early media */
		StreamParams *audio_params;
		if (call==NULL){
			ms_error("No call ?");
			goto end;
		}
		if (lc->audiostream->ticker!=NULL){
			/*streams already started */
			ms_message("Early media already started.");
			goto end;
		}
		audio_params=&call->audio_params;
		sdp_context_read_answer(lc->call->sdpctx,sdp);
		lc->vtable.show(lc);
		lc->vtable.display_status(lc,_("Early media."));
		gstate_new_state(lc, GSTATE_CALL_OUT_CONNECTED, NULL);
		if (lc->ringstream!=NULL){
			ring_stop(lc->ringstream);
			lc->ringstream=NULL;
		}
		ms_message("Doing early media...");
		linphone_core_start_media_streams(lc,call);
	}
	call->state=LCStateRinging;
	goto end;
	end:
		sdp_message_free(sdp);

}

void linphone_call_message_new(LinphoneCore *lc, eXosip_event_t *ev){
#ifdef VIDEO_ENABLED
	if (ev->request){
		if (MSG_IS_INFO(ev->request)){
			osip_content_type_t *ct;
			ct=osip_message_get_content_type(ev->request);
			if (ct && ct->subtype &&
				strcmp(ct->subtype,"media_control+xml")==0){
				osip_body_t *body=NULL;
				osip_message_get_body(ev->request,0,&body);
				if (body && body->body!=NULL &&
					strstr(body->body,"picture_fast_update")){
					osip_message_t *ans=NULL;
					ms_message("Receiving VFU request !");
					if (lc->videostream)
						video_stream_send_vfu(lc->videostream);
					eXosip_call_build_answer(ev->tid,200,&ans);
					if (ans)
						eXosip_call_send_answer(ev->tid,200,ans);
				}
			}	
		}
	}else ms_warning("linphone_call_message_new: No request ?");
#endif
}


void linphone_registration_faillure(LinphoneCore *lc, eXosip_event_t *ev){
	int status_code=0;
	char *msg;
	const char *reason=NULL;
	osip_uri_t *requri=osip_message_get_uri(ev->request);
	char *ru;
	
	osip_uri_to_str(requri,&ru);

	if (ev->response){
		status_code=osip_message_get_status_code(ev->response);
		reason=osip_message_get_reason_phrase(ev->response);
	}
	switch(status_code){
		case 401:
		case 407:
			linphone_process_authentication(lc,ev);
			break;
		default:
			msg=ortp_strdup_printf(_("Registration on %s failed: %s"),ru,(reason!=NULL) ? reason : _("no response timeout"));
			lc->vtable.display_status(lc,msg);
			gstate_new_state(lc, GSTATE_REG_FAILED, msg);
			ms_free(msg);
	}
	osip_free(ru);
}

void linphone_registration_success(LinphoneCore *lc,eXosip_event_t *ev){
	LinphoneProxyConfig *cfg;
	osip_uri_t *requri=osip_message_get_uri(ev->request);
	char *msg;
	char *ru;
smorlat's avatar
smorlat committed
924
	osip_header_t *h=NULL;
aymeric's avatar
aymeric committed
925 926 927 928 929 930 931 932 933 934
	osip_uri_to_str(requri,&ru);
	msg=ms_strdup_printf(_("Registration on %s successful."),ru);
	lc->vtable.display_status(lc,msg);
	ms_free(msg);
	osip_free(ru);
	cfg=linphone_core_get_proxy_config_from_rid(lc,ev->rid);
	ms_return_if_fail(cfg!=NULL);
	cfg->auth_pending=FALSE;
	cfg->registered=TRUE;
	gstate_new_state(lc, GSTATE_REG_OK, NULL);
smorlat's avatar
smorlat committed
935 936 937
	osip_message_get_expires(ev->request,0,&h);
	if (h!=NULL && atoi(h->hvalue)!=0)
		linphone_proxy_config_register_again_with_updated_contact(cfg,ev->request,ev->response);
aymeric's avatar
aymeric committed
938 939
}

smorlat's avatar
smorlat committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
static bool_t comes_from_local_if(osip_message_t *msg){
	osip_via_t *via=NULL;
	osip_message_get_via(msg,0,&via);
	if (via){
		const char *host;
		host=osip_via_get_host(via);
		if (strcmp(host,"127.0.0.1")==0 || strcmp(host,"::1")==0){
			osip_generic_param_t *param=NULL;
			osip_via_param_get_byname(via,"received",&param);
			if (param==NULL) return TRUE;
			if (param->gvalue && 
				(strcmp(param->gvalue,"127.0.0.1")==0 || strcmp(param->gvalue,"::1")==0)){
				return TRUE;
			}
		}
	}
	return FALSE;
}

aymeric's avatar
aymeric committed
959
static void linphone_other_request(LinphoneCore *lc, eXosip_event_t *ev){
smorlat's avatar
smorlat committed
960
	ms_message("in linphone_other_request");
aymeric's avatar
aymeric committed
961 962 963 964 965 966
	if (ev->request==NULL) return;
	if (strcmp(ev->request->sip_method,"MESSAGE")==0){
		linphone_core_text_received(lc,ev);
		eXosip_message_send_answer(ev->tid,200,NULL);
	}else if (strcmp(ev->request->sip_method,"OPTIONS")==0){
		eXosip_options_send_answer(ev->tid,200,NULL);
smorlat's avatar
smorlat committed
967 968 969 970 971 972 973
	}else if (strcmp(ev->request->sip_method,"WAKEUP")==0
		&& comes_from_local_if(ev->request)) {
		eXosip_message_send_answer(ev->tid,200,NULL);
		ms_message("Receiving WAKEUP request !");
		if (lc->vtable.show)
			lc->vtable.show(lc);
	}else {
aymeric's avatar
aymeric committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
		ms_message("Unsupported request received.");
		/*answer with a 501 Not implemented*/
		eXosip_message_send_answer(ev->tid,501,NULL);
	}
}

void linphone_core_process_event(LinphoneCore *lc,eXosip_event_t *ev)
{
	switch(ev->type){
		case EXOSIP_CALL_ANSWERED:
			ms_message("CALL_ANSWERED\n");
			linphone_call_accepted(lc,ev);
			linphone_authentication_ok(lc,ev);
			break;
		case EXOSIP_CALL_CLOSED:
		case EXOSIP_CALL_CANCELLED:
			ms_message("CALL_CLOSED or CANCELLED\n");
			linphone_call_terminated(lc,ev);
			break;
		case EXOSIP_CALL_TIMEOUT:
		case EXOSIP_CALL_NOANSWER:
			ms_message("CALL_TIMEOUT or NOANSWER\n");
			linphone_call_failure(lc,ev);
			break;
		case EXOSIP_CALL_REQUESTFAILURE:
		case EXOSIP_CALL_GLOBALFAILURE:
		case EXOSIP_CALL_SERVERFAILURE:
			ms_message("CALL_REQUESTFAILURE or GLOBALFAILURE or SERVERFAILURE\n");
			linphone_call_failure(lc,ev);
			break;
		case EXOSIP_CALL_INVITE:
			ms_message("CALL_NEW\n");
			/* CALL_NEW is used twice in qos mode : 
			 * when you receive invite (textinfo = "With QoS" or "Without QoS")
			 * and when you receive update (textinfo = "New Call") */
			linphone_inc_new_call(lc,ev);
			break;
		case EXOSIP_CALL_REINVITE:
			linphone_handle_reinvite(lc,ev);
			break;
		case EXOSIP_CALL_ACK:
			ms_message("CALL_ACK");
			linphone_handle_ack(lc,ev);
			break;
		case EXOSIP_CALL_REDIRECTED:
			ms_message("CALL_REDIRECTED");
			linphone_call_redirected(lc,ev);
			break;
		case EXOSIP_CALL_PROCEEDING:
			ms_message("CALL_PROCEEDING");
			linphone_call_proceeding(lc,ev);
			break;
		case EXOSIP_CALL_RINGING:
			ms_message("CALL_RINGING");
			linphone_call_ringing(lc,ev);
			break;
		case EXOSIP_CALL_MESSAGE_NEW:
			ms_message("EXOSIP_CALL_MESSAGE_NEW");
			linphone_call_message_new(lc,ev);
			break;
		case EXOSIP_CALL_MESSAGE_REQUESTFAILURE:
			if (ev->did<0 && ev->response && 
				(ev->response->status_code==407 || ev->response->status_code==401)){
				 eXosip_default_action(ev);
			}
			break;
		case EXOSIP_IN_SUBSCRIPTION_NEW:
			ms_message("CALL_SUBSCRIPTION_NEW or UPDATE");
			linphone_subscription_new(lc,ev);
			break;
		case EXOSIP_SUBSCRIPTION_UPDATE:
			break;
		case EXOSIP_SUBSCRIPTION_NOTIFY:
			ms_message("CALL_SUBSCRIPTION_NOTIFY");
			linphone_notify_recv(lc,ev);
			break;
		case EXOSIP_SUBSCRIPTION_ANSWERED:
			ms_message("EXOSIP_SUBSCRIPTION_ANSWERED, ev->sid=%i\n",ev->sid);
			linphone_subscription_answered(lc,ev);
			break;
		case EXOSIP_SUBSCRIPTION_CLOSED:
			ms_message("EXOSIP_SUBSCRIPTION_CLOSED\n");
			linphone_subscription_closed(lc,ev);
			break;
		case EXOSIP_CALL_RELEASED:
			ms_message("CALL_RELEASED\n");
			linphone_call_released(lc, ev->cid);
			break;
		case EXOSIP_REGISTRATION_FAILURE:
			ms_message("REGISTRATION_FAILURE\n");
			linphone_registration_faillure(lc,ev);
			break;
		case EXOSIP_REGISTRATION_SUCCESS:
			linphone_authentication_ok(lc,ev);
smorlat's avatar
smorlat committed
1068
			linphone_registration_success(lc,ev);
aymeric's avatar
aymeric committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
			break;
		case EXOSIP_MESSAGE_NEW:
			linphone_other_request(lc,ev);
			break;
		case EXOSIP_MESSAGE_REQUESTFAILURE:
			if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
				/*the user is expected to have registered to the proxy, thus password is known*/
				eXosip_default_action(ev);
			}
			break;
		default:
smorlat's avatar
smorlat committed
1080
			ms_message("Unhandled exosip event !");
aymeric's avatar
aymeric committed
1081 1082 1083 1084
			break;
	}
	eXosip_event_free(ev);
}