]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_sdp.c
2aed6283a010a915ff00c67d0f0b9f9494a1cddf
[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 sdp_message_t *create_generic_sdp(const SalMediaDescription *desc)
110 {
111         sdp_message_t *local;
112         int inet6;
113
114         sdp_message_init (&local);
115         if (strchr(desc->addr,':')!=NULL){
116                 inet6=1;
117         }else inet6=0;
118         sdp_message_v_version_set (local, osip_strdup ("0"));
119         sdp_message_o_origin_set (local, osip_strdup (desc->username),
120                           osip_strdup ("123456"), osip_strdup ("654321"),
121                           osip_strdup ("IN"), inet6 ? osip_strdup("IP6") : osip_strdup ("IP4"),
122                           osip_strdup (desc->addr));
123         sdp_message_s_name_set (local, osip_strdup ("A conversation"));
124         sdp_message_c_connection_add (local, -1,
125                               osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
126                               osip_strdup (desc->addr), NULL, NULL);
127         sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
128         return local;
129 }
130
131
132
133 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt)
134 {
135         char attr[256];
136         sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
137         if (pt->type==PAYLOAD_AUDIO_CONTINUOUS || pt->type==PAYLOAD_AUDIO_PACKETIZED)
138                 snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt), 
139                         pt->mime_type, pt->clock_rate,pt->channels);
140         else
141                 snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt), 
142                         pt->mime_type, pt->clock_rate);
143         sdp_message_a_attribute_add (msg, line,
144                                      osip_strdup ("rtpmap"), osip_strdup(attr));
145
146         if (pt->recv_fmtp != NULL)
147         {
148                 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
149                 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
150                                      osip_strdup(attr));
151         }
152 }
153
154
155 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc){
156         const char *mt=desc->type==SAL_AUDIO ? "audio" : "video";
157         const MSList *elem;
158         sdp_message_m_media_add (msg, osip_strdup (mt),
159                                  int_2char (desc->port), NULL,
160                                  osip_strdup ("RTP/AVP"));
161         sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
162                                      int_2char(desc->bandwidth));
163         for(elem=desc->payloads;elem!=NULL;elem=elem->next){
164                 add_payload(msg, lineno, (PayloadType*)elem->data);
165         }
166 }
167
168 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc){
169         int i;
170         sdp_message_t *msg=create_generic_sdp(desc);
171         for(i=0;i<desc->nstreams;++i){
172                 add_line(msg,i,&desc->streams[i]);
173         }
174         return msg;
175 }
176
177 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
178         if (rtpmap==NULL){
179                 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
180                 if (refpt){
181                         pt->mime_type=ms_strdup(refpt->mime_type);
182                         pt->clock_rate=refpt->clock_rate;
183                 }else{
184                         ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
185                             payload_type_get_number(pt));
186                         return -1;
187                 }
188         }else{
189                 char *mime=ms_strdup(rtpmap);
190                 char *p=strchr(mime,'/');
191                 if (p){
192                         char *chans;
193                         *p='\0';
194                         p++;
195                         chans=strchr(p,'/');
196                         if (chans){
197                                 *chans='\0';
198                                 chans++;
199                                 pt->channels=atoi(chans);
200                         }else pt->channels=1;
201                         pt->clock_rate=atoi(p);
202                 }
203                 pt->mime_type=mime;
204         }
205         return 0;
206 }
207
208 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){
209         int i,j;
210         const char *mtype,*proto,*port,*addr,*number;
211         sdp_bandwidth_t *sbw=NULL;
212         
213         addr=sdp_message_c_addr_get (msg, -1, 0);
214         if (addr)
215                 strncpy(desc->addr,addr,sizeof(desc->addr));
216         /* for each m= line */
217         for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
218         {
219                 SalStreamDescription *stream=&desc->streams[i];
220                 
221                 memset(stream,0,sizeof(*stream));
222                 mtype = sdp_message_m_media_get(msg, i);
223                 proto = sdp_message_m_proto_get (msg, i);
224                 port = sdp_message_m_port_get(msg, i);
225                 stream->proto=SAL_PROTO_UNKNOWN;
226                 if (proto){
227                         if (strcasecmp(proto,"RTP/AVP")==0)
228                                 stream->proto=SAL_PROTO_RTP_AVP;
229                         else if (strcasecmp(proto,"RTP/SAVP")==0){
230                                 stream->proto=SAL_PROTO_RTP_SAVP;
231                         }
232                 }
233                 addr = sdp_message_c_addr_get (msg, i, 0);
234                 if (addr != NULL)
235                         strncpy(stream->addr,addr,sizeof(stream->addr));
236                 stream->ptime=_sdp_message_get_a_ptime(msg,i);
237                 if (strcasecmp("audio", mtype) == 0){
238                         stream->type=SAL_AUDIO;
239                 }else if (strcasecmp("video", mtype) == 0){
240                         stream->type=SAL_VIDEO;
241                 }else stream->type=SAL_OTHER;
242                 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
243                         if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
244                 }
245                 /* for each payload type */
246                 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
247                         const char *rtpmap,*fmtp;
248                         int ptn=atoi(number);
249                         PayloadType *pt=payload_type_new();
250                         payload_type_set_number(pt,ptn);
251                         /* get the rtpmap associated to this codec, if any */
252                         rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
253                         payload_type_fill_from_rtpmap(pt,rtpmap);
254                         /* get the fmtp, if any */
255                         fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
256                         payload_type_set_send_fmtp(pt,fmtp);
257                 }
258         }
259         return 0;
260 }