4 Copyright (C) 2010 Belledonne Communications SARL
5 (simon.morlat@linphone.org)
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.
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.
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.
22 #include "linphonecore.h"
30 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, bool_t only_one_codec){
33 for(it=codecs;it!=NULL;it=it->next){
34 PayloadType *pt=(PayloadType*)it->data;
35 if ((pt->flags & PAYLOAD_TYPE_ENABLED) && linphone_core_check_payload_type_usability(lc,pt)){
36 l=ms_list_append(l,payload_type_clone(pt));
37 if (only_one_codec) break;
43 static SalMediaDescription *create_local_media_description(LinphoneCore *lc,
44 const char *localip, const char *username, bool_t only_one_codec){
47 SalMediaDescription *md=sal_media_description_new();
49 strncpy(md->addr,localip,sizeof(md->addr));
50 strncpy(md->username,username,sizeof(md->username));
51 md->bandwidth=linphone_core_get_download_bandwidth(lc);
52 /*set audio capabilities */
53 strncpy(md->streams[0].addr,localip,sizeof(md->streams[0].addr));
54 md->streams[0].port=linphone_core_get_audio_port(lc);
55 md->streams[0].proto=SalProtoRtpAvp;
56 md->streams[0].type=SalAudio;
57 md->streams[0].ptime=lc->net_conf.down_ptime;
58 l=make_codec_list(lc,lc->codecs_conf.audio_codecs,only_one_codec);
59 pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
60 l=ms_list_append(l,pt);
61 md->streams[0].payloads=l;
63 if (lc->dw_audio_bw>0)
64 md->streams[0].bandwidth=lc->dw_audio_bw;
66 if (linphone_core_video_enabled (lc)){
68 md->streams[1].port=linphone_core_get_video_port(lc);
69 md->streams[1].proto=SalProtoRtpAvp;
70 md->streams[1].type=SalVideo;
71 l=make_codec_list(lc,lc->codecs_conf.video_codecs,only_one_codec);
72 md->streams[1].payloads=l;
74 md->streams[1].bandwidth=lc->dw_video_bw;
79 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
81 call->state=LinphoneCallInit;
82 call->start_time=time(NULL);
83 call->media_start_time=0;
84 call->log=linphone_call_log_new(call, from, to);
85 linphone_core_notify_all_friends(call->core,LINPHONE_STATUS_ONTHEPHONE);
86 if (linphone_core_get_firewall_policy(call->core)==LINPHONE_POLICY_USE_STUN)
87 linphone_core_run_stun_tests(call->core,call);
90 static void discover_mtu(LinphoneCore *lc, const char *remote){
92 if (lc->net_conf.mtu==0 ){
93 /*attempt to discover mtu*/
94 mtu=ms_discover_mtu(remote);
97 ms_message("Discovered mtu is %i, RTP payload max size is %i",
98 mtu, ms_get_payload_max_size());
103 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to)
105 LinphoneCall *call=ms_new0(LinphoneCall,1);
106 call->dir=LinphoneCallOutgoing;
107 call->op=sal_op_new(lc->sal);
108 sal_op_set_user_pointer(call->op,call);
110 linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
111 call->localdesc=create_local_media_description (lc,call->localip,
112 linphone_address_get_username(from),FALSE);
113 linphone_call_init_common(call,from,to);
114 discover_mtu(lc,linphone_address_get_domain (to));
118 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
119 LinphoneCall *call=ms_new0(LinphoneCall,1);
120 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
124 call->dir=LinphoneCallIncoming;
125 sal_op_set_user_pointer(op,call);
129 if (lc->sip_conf.ping_with_options){
130 /*the following sends an option request back to the caller so that
131 we get a chance to discover our nat'd address before answering.*/
132 call->ping_op=sal_op_new(lc->sal);
133 to_str=linphone_address_as_string(to);
134 from_str=linphone_address_as_string(from);
135 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
136 sal_ping(call->ping_op,to_str,from_str);
141 linphone_address_clean(from);
142 linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
143 call->localdesc=create_local_media_description (lc,call->localip,
144 linphone_address_get_username(me),lc->sip_conf.only_one_codec);
145 linphone_call_init_common(call, from, to);
146 discover_mtu(lc,linphone_address_get_domain(from));
147 linphone_address_destroy(me);
151 void linphone_call_set_terminated(LinphoneCall *call){
152 LinphoneCallStatus status=LinphoneCallAborted;
153 if (call->state==LinphoneCallAVRunning){
154 status=LinphoneCallSuccess;
156 linphone_call_log_completed(call->log,call, status);
157 call->state=LinphoneCallTerminated;
160 static void linphone_call_destroy(LinphoneCall *obj)
162 linphone_core_notify_all_friends(obj->core,obj->core->prev_mode);
164 linphone_core_update_allocated_audio_bandwidth(obj->core);
166 sal_op_release(obj->op);
169 if (obj->resultdesc!=NULL) {
170 sal_media_description_unref(obj->resultdesc);
171 obj->resultdesc=NULL;
173 if (obj->localdesc!=NULL) {
174 sal_media_description_unref(obj->localdesc);
178 sal_op_release(obj->ping_op);
180 if(linphone_core_del_call(obj->core,obj) != 0)
182 ms_error("could not remove the call from the list !!!");
184 if(obj == linphone_core_get_current_call(obj->core))
186 ms_message("destroying the current call\n");
187 linphone_core_unset_the_current_call(obj->core);
192 void linphone_call_ref(LinphoneCall *obj){
196 void linphone_call_unref(LinphoneCall *obj){
199 linphone_call_destroy(obj);
202 bool_t linphone_call_paused(LinphoneCall *call){
203 return call->state==LinphoneCallPaused;
206 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
207 return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
210 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
211 return linphone_address_as_string(linphone_call_get_remote_address(call));
214 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){