]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_sdp.c
fix empty mline again and update ms2
[linphone] / coreapi / sal_eXosip2_sdp.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20
21 #include "ortp/b64.h"
22 #include "sal.h"
23 #include <eXosip2/eXosip.h>
24
25 #define keywordcmp(key,b) strncmp(key,b,sizeof(key))
26
27 #ifdef FOR_LATER
28
29 static char *make_relay_session_id(const char *username, const char *relay){
30         /*ideally this should be a hash of the parameters with a random part*/
31         char tmp[128];
32         int s1=(int)random();
33         int s2=(int)random();
34         long long int res=((long long int)s1)<<32 | (long long int) s2;
35         void *src=&res;
36         b64_encode(src, sizeof(long long int), tmp, sizeof(tmp));
37         return osip_strdup(tmp);
38 }
39
40
41 static void add_relay_info(sdp_message_t *sdp, int mline, const char *relay, const char *relay_session_id){
42
43         if (relay) sdp_message_a_attribute_add(sdp, mline,
44                                      osip_strdup ("relay-addr"),osip_strdup(relay));
45         if (relay_session_id) sdp_message_a_attribute_add(sdp, mline,
46                                      osip_strdup ("relay-session-id"), osip_strdup(relay_session_id));
47 }
48
49 #endif
50
51 static char * int_2char(int a){
52         char *p=osip_malloc(16);
53         snprintf(p,16,"%i",a);
54         return p;
55 }
56
57 /* return the value of attr "field" for payload pt at line pos (field=rtpmap,fmtp...)*/
58 static const char *sdp_message_a_attr_value_get_with_pt(sdp_message_t *sdp,int pos,int pt,const char *field)
59 {
60         int i,tmppt=0,scanned=0;
61         char *tmp;
62         sdp_attribute_t *attr;
63         for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
64                 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
65                         int nb = sscanf(attr->a_att_value,"%i %n",&tmppt,&scanned);
66                         /* the return value may depend on how %n is interpreted by the libc: see manpage*/
67                         if (nb == 1 || nb==2 ){
68                                 if (pt==tmppt){
69                                         tmp=attr->a_att_value+scanned;
70                                         if (strlen(tmp)>0)
71                                                 return tmp;
72                                 }
73                         }else ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
74                 }
75         }
76         return NULL;
77 }
78
79 #ifdef FOR_LATER
80 /* return the value of attr "field" */
81 static const char *sdp_message_a_attr_value_get(sdp_message_t *sdp,int pos,const char *field)
82 {
83         int i;
84         sdp_attribute_t *attr;
85         for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
86                 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
87                         return attr->a_att_value;
88                 }
89         }
90         return NULL;
91 }
92 #endif
93
94 static int _sdp_message_get_a_ptime(sdp_message_t *sdp, int mline){
95         int i,ret;
96         sdp_attribute_t *attr;
97         for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
98                 if (keywordcmp("ptime",attr->a_att_field)==0){
99                         int nb = sscanf(attr->a_att_value,"%i",&ret);
100                         /* the return value may depend on how %n is interpreted by the libc: see manpage*/
101                         if (nb == 1){
102                                 return ret;
103                         }else ms_warning("sdp has a strange a=ptime line (%s) ",attr->a_att_value);
104                 }
105         }
106         return 0;
107 }
108
109 static int _sdp_message_get_mline_dir(sdp_message_t *sdp, int mline){
110         int i;
111         sdp_attribute_t *attr;
112         for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
113                 if (keywordcmp("sendrecv",attr->a_att_field)==0){
114                         return SalStreamSendRecv;
115                 }else if (keywordcmp("sendonly",attr->a_att_field)==0){
116                         return SalStreamSendOnly;
117                 }else if (keywordcmp("recvonly",attr->a_att_field)==0){
118                         return SalStreamSendOnly;
119                 }else if (keywordcmp("inactive",attr->a_att_field)==0){
120                         return SalStreamInactive;
121                 }
122         }
123         return SalStreamSendRecv;
124 }
125
126 static sdp_message_t *create_generic_sdp(const SalMediaDescription *desc)
127 {
128         sdp_message_t *local;
129         int inet6;
130         char sessid[16];
131         char sessver[16];
132         
133         snprintf(sessid,16,"%i",desc->session_id);
134         snprintf(sessver,16,"%i",desc->session_ver);
135         sdp_message_init (&local);
136         if (strchr(desc->addr,':')!=NULL){
137                 inet6=1;
138         }else inet6=0;
139         sdp_message_v_version_set (local, osip_strdup ("0"));
140         sdp_message_o_origin_set (local, osip_strdup (desc->username),
141                           osip_strdup (sessid), osip_strdup (sessver),
142                           osip_strdup ("IN"), inet6 ? osip_strdup("IP6") : osip_strdup ("IP4"),
143                           osip_strdup (desc->addr));
144         sdp_message_s_name_set (local, osip_strdup ("Talk"));
145         if(!sal_media_description_has_dir (desc,SalStreamSendOnly))
146         {
147                 sdp_message_c_connection_add (local, -1,
148                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
149                                                 osip_strdup (desc->addr), NULL, NULL);
150         }
151         else
152         {
153                 sdp_message_c_connection_add (local, -1,
154                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
155                                                 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
156         }               
157         sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
158         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
159                         int_2char(desc->bandwidth));
160         return local;
161 }
162
163
164 static bool_t is_known_rtpmap(const PayloadType *pt){
165         switch(payload_type_get_number(pt)){
166                 case 0:
167                 case 8:
168                 case 3:
169                 case 34:
170                         return TRUE;
171         }
172         return FALSE;
173 }
174
175 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
176 {
177         char attr[256];
178         sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
179
180         if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
181                 if (pt->channels>1)
182                         snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt), 
183                                         pt->mime_type, pt->clock_rate,pt->channels);
184                 else
185                         snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt), 
186                                         pt->mime_type, pt->clock_rate);
187                 sdp_message_a_attribute_add (msg, line,
188                                                  osip_strdup ("rtpmap"), osip_strdup(attr));
189         }
190
191         if (pt->recv_fmtp != NULL)
192         {
193                 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
194                 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
195                                      osip_strdup(attr));
196         }
197 }
198
199
200 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc){
201         const char *mt=NULL;
202         const MSList *elem;
203         const char *addr;
204         const char *dir="sendrecv";
205         int port;
206         bool_t strip_well_known_rtpmaps;
207         
208         switch (desc->type) {
209         case SalAudio:
210                 mt="audio";
211                 break;
212         case SalVideo:
213                 mt="video";
214                 break;
215         case SalOther:
216                 mt=desc->typeother;
217                 break;
218         }
219         if (desc->candidates[0].addr[0]!='\0'){
220                 addr=desc->candidates[0].addr;
221                 port=desc->candidates[0].port;
222         }else{
223                 addr=desc->addr;
224                 port=desc->port;
225         }
226         /*only add a c= line within the stream description if address are differents*/
227         if (strcmp(addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
228                 bool_t inet6;
229                 if (strchr(addr,':')!=NULL){
230                         inet6=TRUE;
231                 }else inet6=FALSE;
232                 sdp_message_c_connection_add (msg, lineno,
233                               osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
234                               osip_strdup (addr), NULL, NULL);
235         }
236         sdp_message_m_media_add (msg, osip_strdup (mt),
237                                  int_2char (port), NULL,
238                                  osip_strdup ("RTP/AVP"));
239         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
240                                      int_2char(desc->bandwidth));
241         if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
242                                 int_2char(desc->ptime));
243         strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
244         if (desc->payloads){
245                 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
246                         add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
247                 }
248         }else{
249                 /* to comply with SDP we cannot have an empty payload type number list */
250                 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
251                 sdp_message_m_payload_add (msg,lineno, int_2char (0));
252         }
253         switch(desc->dir){
254                 case SalStreamSendRecv:
255                         /*dir="sendrecv";*/
256                         dir=NULL;
257                 break;
258                 case SalStreamRecvOnly:
259                         dir="recvonly";
260                         break;
261                 case SalStreamSendOnly:
262                         dir="sendonly";
263                         break;
264                 case SalStreamInactive:
265                         dir="inactive";
266                         break;
267         }
268         if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
269 }
270
271 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc){
272         int i;
273         sdp_message_t *msg=create_generic_sdp(desc);
274         for(i=0;i<desc->nstreams;++i){
275                 add_line(msg,i,&desc->streams[i]);
276         }
277         return msg;
278 }
279
280 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
281         if (rtpmap==NULL){
282                 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
283                 if (refpt){
284                         pt->mime_type=ms_strdup(refpt->mime_type);
285                         pt->clock_rate=refpt->clock_rate;
286                 }else{
287                         ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
288                             payload_type_get_number(pt));
289                         return -1;
290                 }
291         }else{
292                 char *mime=ms_strdup(rtpmap);
293                 char *p=strchr(mime,'/');
294                 if (p){
295                         char *chans;
296                         *p='\0';
297                         p++;
298                         chans=strchr(p,'/');
299                         if (chans){
300                                 *chans='\0';
301                                 chans++;
302                                 pt->channels=atoi(chans);
303                         }else pt->channels=1;
304                         pt->clock_rate=atoi(p);
305                 }
306                 pt->mime_type=mime;
307         }
308         return 0;
309 }
310
311 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){
312         int i,j;
313         const char *mtype,*proto,*port,*addr,*number;
314         sdp_bandwidth_t *sbw=NULL;
315         
316         addr=sdp_message_c_addr_get (msg, -1, 0);
317         if (addr)
318                 strncpy(desc->addr,addr,sizeof(desc->addr));
319         for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
320                 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
321         }
322         
323         /* for each m= line */
324         for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
325         {
326                 SalStreamDescription *stream=&desc->streams[i];
327                 
328                 memset(stream,0,sizeof(*stream));
329                 mtype = sdp_message_m_media_get(msg, i);
330                 proto = sdp_message_m_proto_get (msg, i);
331                 port = sdp_message_m_port_get(msg, i);
332                 stream->proto=SalProtoUnknown;
333                 if (proto){
334                         if (strcasecmp(proto,"RTP/AVP")==0)
335                                 stream->proto=SalProtoRtpAvp;
336                         else if (strcasecmp(proto,"RTP/SAVP")==0){
337                                 stream->proto=SalProtoRtpSavp;
338                         }
339                 }
340                 addr = sdp_message_c_addr_get (msg, i, 0);
341                 if (addr != NULL)
342                         strncpy(stream->addr,addr,sizeof(stream->addr));
343                 if (port)
344                         stream->port=atoi(port);
345                 
346                 stream->ptime=_sdp_message_get_a_ptime(msg,i);
347                 if (strcasecmp("audio", mtype) == 0){
348                         stream->type=SalAudio;
349                 }else if (strcasecmp("video", mtype) == 0){
350                         stream->type=SalVideo;
351                 }else {
352                         stream->type=SalOther;
353                         strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
354                 }
355                 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
356                         if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
357                 }
358                 stream->dir=_sdp_message_get_mline_dir(msg,i);
359                 /* for each payload type */
360                 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
361                         const char *rtpmap,*fmtp;
362                         int ptn=atoi(number);
363                         PayloadType *pt=payload_type_new();
364                         payload_type_set_number(pt,ptn);
365                         /* get the rtpmap associated to this codec, if any */
366                         rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
367                         if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
368                                 /* get the fmtp, if any */
369                                 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
370                                 payload_type_set_send_fmtp(pt,fmtp);
371                                 stream->payloads=ms_list_append(stream->payloads,pt);
372                                 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
373                                         pt->send_fmtp ? pt->send_fmtp : "");
374                         }
375                 }
376         }
377         desc->nstreams=i;
378         return 0;
379 }