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.
24 #include "linphonecore.h"
32 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, bool_t only_one_codec){
35 for(it=codecs;it!=NULL;it=it->next){
36 PayloadType *pt=(PayloadType*)it->data;
37 if ((pt->flags & PAYLOAD_TYPE_ENABLED) && linphone_core_check_payload_type_usability(lc,pt)){
38 l=ms_list_append(l,payload_type_clone(pt));
39 if (only_one_codec) break;
45 static SalMediaDescription *create_local_media_description(LinphoneCore *lc,
46 const char *localip, const char *username, bool_t only_one_codec){
49 SalMediaDescription *md=sal_media_description_new();
51 strncpy(md->addr,localip,sizeof(md->addr));
52 strncpy(md->username,username,sizeof(md->username));
53 md->bandwidth=linphone_core_get_download_bandwidth(lc);
54 /*set audio capabilities */
55 strncpy(md->streams[0].addr,localip,sizeof(md->streams[0].addr));
56 md->streams[0].port=linphone_core_get_audio_port(lc);
57 md->streams[0].proto=SalProtoRtpAvp;
58 md->streams[0].type=SalAudio;
59 md->streams[0].ptime=lc->net_conf.down_ptime;
60 l=make_codec_list(lc,lc->codecs_conf.audio_codecs,only_one_codec);
61 pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
62 l=ms_list_append(l,pt);
63 md->streams[0].payloads=l;
65 if (lc->dw_audio_bw>0)
66 md->streams[0].bandwidth=lc->dw_audio_bw;
68 if (linphone_core_video_enabled (lc)){
70 md->streams[1].port=linphone_core_get_video_port(lc);
71 md->streams[1].proto=SalProtoRtpAvp;
72 md->streams[1].type=SalVideo;
73 l=make_codec_list(lc,lc->codecs_conf.video_codecs,only_one_codec);
74 md->streams[1].payloads=l;
76 md->streams[1].bandwidth=lc->dw_video_bw;
81 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
83 call->state=LinphoneCallInit;
84 call->start_time=time(NULL);
85 call->media_start_time=0;
86 call->log=linphone_call_log_new(call, from, to);
87 linphone_core_notify_all_friends(call->core,LINPHONE_STATUS_ONTHEPHONE);
88 if (linphone_core_get_firewall_policy(call->core)==LINPHONE_POLICY_USE_STUN)
89 linphone_core_run_stun_tests(call->core,call);
92 static void discover_mtu(LinphoneCore *lc, const char *remote){
94 if (lc->net_conf.mtu==0 ){
95 /*attempt to discover mtu*/
96 mtu=ms_discover_mtu(remote);
99 ms_message("Discovered mtu is %i, RTP payload max size is %i",
100 mtu, ms_get_payload_max_size());
105 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to)
107 LinphoneCall *call=ms_new0(LinphoneCall,1);
108 call->dir=LinphoneCallOutgoing;
109 call->op=sal_op_new(lc->sal);
110 sal_op_set_user_pointer(call->op,call);
112 linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
113 call->localdesc=create_local_media_description (lc,call->localip,
114 linphone_address_get_username(from),FALSE);
115 linphone_call_init_common(call,from,to);
116 discover_mtu(lc,linphone_address_get_domain (to));
120 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
121 LinphoneCall *call=ms_new0(LinphoneCall,1);
122 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
126 call->dir=LinphoneCallIncoming;
127 sal_op_set_user_pointer(op,call);
131 if (lc->sip_conf.ping_with_options){
132 /*the following sends an option request back to the caller so that
133 we get a chance to discover our nat'd address before answering.*/
134 call->ping_op=sal_op_new(lc->sal);
135 to_str=linphone_address_as_string(to);
136 from_str=linphone_address_as_string(from);
137 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
138 sal_ping(call->ping_op,to_str,from_str);
143 linphone_address_clean(from);
144 linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
145 call->localdesc=create_local_media_description (lc,call->localip,
146 linphone_address_get_username(me),lc->sip_conf.only_one_codec);
147 linphone_call_init_common(call, from, to);
148 discover_mtu(lc,linphone_address_get_domain(from));
149 linphone_address_destroy(me);
153 void linphone_call_set_terminated(LinphoneCall *call){
154 LinphoneCallStatus status=LinphoneCallAborted;
155 if (call->state==LinphoneCallAVRunning){
156 status=LinphoneCallSuccess;
158 linphone_call_log_completed(call->log,call, status);
159 call->state=LinphoneCallTerminated;
162 static void linphone_call_destroy(LinphoneCall *obj)
164 linphone_core_notify_all_friends(obj->core,obj->core->prev_mode);
166 linphone_core_update_allocated_audio_bandwidth(obj->core);
168 sal_op_release(obj->op);
171 if (obj->resultdesc!=NULL) {
172 sal_media_description_unref(obj->resultdesc);
173 obj->resultdesc=NULL;
175 if (obj->localdesc!=NULL) {
176 sal_media_description_unref(obj->localdesc);
180 sal_op_release(obj->ping_op);
182 if(linphone_core_del_call(obj->core,obj) != 0)
184 ms_error("could not remove the call from the list !!!");
186 if(obj == linphone_core_get_current_call(obj->core))
188 ms_message("destroying the current call\n");
189 linphone_core_unset_the_current_call(obj->core);
194 void linphone_call_ref(LinphoneCall *obj){
198 void linphone_call_unref(LinphoneCall *obj){
201 linphone_call_destroy(obj);
204 bool_t linphone_call_paused(LinphoneCall *call){
205 return call->state==LinphoneCallPaused;
208 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
209 return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
212 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
213 return linphone_address_as_string(linphone_call_get_remote_address(call));
216 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
221 * Get the user_pointer in the LinphoneCall
223 * @ingroup call_control
225 * return user_pointer an opaque user pointer that can be retrieved at any time
227 void *linphone_call_get_user_pointer(LinphoneCall *call)
229 return call->user_pointer;
233 * Set the user_pointer in the LinphoneCall
235 * @ingroup call_control
237 * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
239 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
241 call->user_pointer = user_pointer;