]> sjero.net Git - linphone/blob - coreapi/linphonecall.c
keep sending streams when put on hold by remote
[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 #ifdef WIN32
22 #include <time.h>
23 #endif
24 #include "linphonecore.h"
25 #include "sipsetup.h"
26 #include "lpconfig.h"
27 #include "private.h"
28
29
30
31
32 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, bool_t only_one_codec){
33         MSList *l=NULL;
34         const MSList *it;
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;
40                 }
41         }
42         return l;
43 }
44
45 static SalMediaDescription *create_local_media_description(LinphoneCore *lc, 
46                 const char *localip, const char *username, bool_t only_one_codec){
47         MSList *l;
48         PayloadType *pt;
49         SalMediaDescription *md=sal_media_description_new();
50         md->nstreams=1;
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;
64         
65         if (lc->dw_audio_bw>0)
66                 md->streams[0].bandwidth=lc->dw_audio_bw;
67
68         if (linphone_core_video_enabled (lc)){
69                 md->nstreams++;
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;
75                 if (lc->dw_video_bw)
76                         md->streams[1].bandwidth=lc->dw_video_bw;
77         }
78         return md;
79 }
80
81 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
82         call->refcnt=1;
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);
90 }
91
92 static void discover_mtu(LinphoneCore *lc, const char *remote){
93         int mtu;
94         if (lc->net_conf.mtu==0 ){
95                 /*attempt to discover mtu*/
96                 mtu=ms_discover_mtu(remote);
97                 if (mtu>0){
98                         ms_set_mtu(mtu);
99                         ms_message("Discovered mtu is %i, RTP payload max size is %i",
100                                 mtu, ms_get_payload_max_size());
101                 }
102         }
103 }
104
105 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to)
106 {
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);
111         call->core=lc;
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));
117         return call;
118 }
119
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);
123         char *to_str;
124         char *from_str;
125
126         call->dir=LinphoneCallIncoming;
127         sal_op_set_user_pointer(op,call);
128         call->op=op;
129         call->core=lc;
130
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);
139                 ms_free(to_str);
140                 ms_free(from_str);
141         }
142         
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);
150         return call;
151 }
152
153 void linphone_call_set_terminated(LinphoneCall *call){
154         LinphoneCallStatus status=LinphoneCallAborted;
155         if (call->state==LinphoneCallAVRunning){
156                 status=LinphoneCallSuccess;
157         }
158         linphone_call_log_completed(call->log,call, status);
159         call->state=LinphoneCallTerminated;
160 }
161
162 static void linphone_call_destroy(LinphoneCall *obj)
163 {
164         linphone_core_notify_all_friends(obj->core,obj->core->prev_mode);
165         
166         linphone_core_update_allocated_audio_bandwidth(obj->core);
167         if (obj->op!=NULL) {
168                 sal_op_release(obj->op);
169                 obj->op=NULL;
170         }
171         if (obj->resultdesc!=NULL) {
172                 sal_media_description_unref(obj->resultdesc);
173                 obj->resultdesc=NULL;
174         }
175         if (obj->localdesc!=NULL) {
176                 sal_media_description_unref(obj->localdesc);
177                 obj->localdesc=NULL;
178         }
179         if (obj->ping_op) {
180                 sal_op_release(obj->ping_op);
181         }
182         if(linphone_core_del_call(obj->core,obj) != 0)
183         {
184                 ms_error("could not remove the call from the list !!!");
185         }
186         if(obj == linphone_core_get_current_call(obj->core))
187         {
188                 ms_message("destroying the current call\n");
189                 linphone_core_unset_the_current_call(obj->core);
190         }
191         ms_free(obj);
192 }
193
194 void linphone_call_ref(LinphoneCall *obj){
195         obj->refcnt++;
196 }
197
198 void linphone_call_unref(LinphoneCall *obj){
199         obj->refcnt--;
200         if (obj->refcnt==0)
201                 linphone_call_destroy(obj);
202 }
203
204 bool_t linphone_call_paused(LinphoneCall *call){
205         return call->state==LinphoneCallPaused;
206 }
207
208 const LinphoneAddress * linphone_call_get_remote_address(const LinphoneCall *call){
209         return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
210 }
211
212 char *linphone_call_get_remote_address_as_string(const LinphoneCall *call){
213         return linphone_address_as_string(linphone_call_get_remote_address(call));
214 }
215
216 LinphoneCallState linphone_call_get_state(const LinphoneCall *call){
217         return call->state;
218 }
219
220 /**
221  * Get the user_pointer in the LinphoneCall
222  *
223  * @ingroup call_control
224  *
225  * return user_pointer an opaque user pointer that can be retrieved at any time
226 **/
227 void *linphone_call_get_user_pointer(LinphoneCall *call)
228 {
229         return call->user_pointer;
230 }
231
232 /**
233  * Set the user_pointer in the LinphoneCall
234  *
235  * @ingroup call_control
236  *
237  * the user_pointer is an opaque user pointer that can be retrieved at any time in the LinphoneCall
238 **/
239 void linphone_call_set_user_pointer(LinphoneCall *call, void *user_pointer)
240 {
241         call->user_pointer = user_pointer;
242 }