]> sjero.net Git - linphone/blob - coreapi/linphonecall.c
merge multicall patch from Aurelien
[linphone] / coreapi / linphonecall.c
1
2 /*
3 linphone
4 Copyright (C) 2010  Belledonne Communications SARL 
5  (simon.morlat@linphone.org)
6
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.
11
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.
16
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.
20 */
21
22 #include "linphonecore.h"
23 #include "sipsetup.h"
24 #include "lpconfig.h"
25 #include "private.h"
26
27
28
29
30 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, bool_t only_one_codec){
31         MSList *l=NULL;
32         const MSList *it;
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;
38                 }
39         }
40         return l;
41 }
42
43 static SalMediaDescription *create_local_media_description(LinphoneCore *lc, 
44                 const char *localip, const char *username, bool_t only_one_codec){
45         MSList *l;
46         PayloadType *pt;
47         SalMediaDescription *md=sal_media_description_new();
48         md->nstreams=1;
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;
62         
63         if (lc->dw_audio_bw>0)
64                 md->streams[0].bandwidth=lc->dw_audio_bw;
65
66         if (linphone_core_video_enabled (lc)){
67                 md->nstreams++;
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;
73                 if (lc->dw_video_bw)
74                         md->streams[1].bandwidth=lc->dw_video_bw;
75         }
76         return md;
77 }
78
79 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
80         call->refcnt=1;
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);
88 }
89
90 static void discover_mtu(LinphoneCore *lc, const char *remote){
91         int mtu;
92         if (lc->net_conf.mtu==0 ){
93                 /*attempt to discover mtu*/
94                 mtu=ms_discover_mtu(remote);
95                 if (mtu>0){
96                         ms_set_mtu(mtu);
97                         ms_message("Discovered mtu is %i, RTP payload max size is %i",
98                                 mtu, ms_get_payload_max_size());
99                 }
100         }
101 }
102
103 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to)
104 {
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);
109         call->core=lc;
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));
115         return call;
116 }
117
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);
121         char *to_str;
122         char *from_str;
123
124         call->dir=LinphoneCallIncoming;
125         sal_op_set_user_pointer(op,call);
126         call->op=op;
127         call->core=lc;
128
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);
137                 ms_free(to_str);
138                 ms_free(from_str);
139         }
140         
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);
148         return call;
149 }
150
151 void linphone_call_set_terminated(LinphoneCall *call){
152         LinphoneCallStatus status=LinphoneCallAborted;
153         if (call->state==LinphoneCallAVRunning){
154                 status=LinphoneCallSuccess;
155         }
156         linphone_call_log_completed(call->log,call, status);
157         call->state=LinphoneCallTerminated;
158 }
159
160 static void linphone_call_destroy(LinphoneCall *obj)
161 {
162         linphone_core_notify_all_friends(obj->core,obj->core->prev_mode);
163         
164         linphone_core_update_allocated_audio_bandwidth(obj->core);
165         if (obj->op!=NULL) {
166                 sal_op_release(obj->op);
167                 obj->op=NULL;
168         }
169         if (obj->resultdesc!=NULL) {
170                 sal_media_description_unref(obj->resultdesc);
171                 obj->resultdesc=NULL;
172         }
173         if (obj->localdesc!=NULL) {
174                 sal_media_description_unref(obj->localdesc);
175                 obj->localdesc=NULL;
176         }
177         if (obj->ping_op) {
178                 sal_op_release(obj->ping_op);
179         }
180         if(linphone_core_del_call(obj->core,obj) != 0)
181         {
182                 ms_error("could not remove the call from the list !!!");
183         }
184         if(obj == linphone_core_get_current_call(obj->core))
185         {
186                 ms_message("destroying the current call\n");
187                 linphone_core_unset_the_current_call(obj->core);
188         }
189         ms_free(obj);
190 }
191
192 void linphone_call_ref(LinphoneCall *obj){
193         obj->refcnt++;
194 }
195
196 void linphone_call_unref(LinphoneCall *obj){
197         obj->refcnt--;
198         if (obj->refcnt==0)
199                 linphone_call_destroy(obj);
200 }
201
202 bool_t linphone_call_paused(LinphoneCall *call){
203         return call->state==LinphoneCallPaused;
204 }
205
206 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
207         return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
208 }
209
210 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
211         return linphone_address_as_string(linphone_call_get_remote_address(call));
212 }
213
214 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
215         return call->state;
216 }
217