]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_sdp.c
Redesign ICE integration to prevent Sal from depending on ICE.
[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 "ortp/ortp_srtp.h"
23 #include "sal.h"
24 #include <eXosip2/eXosip.h>
25
26 #define keywordcmp(key,b) strncmp(key,b,sizeof(key))
27
28 #ifdef FOR_LATER
29
30 static char *make_relay_session_id(const char *username, const char *relay){
31         /*ideally this should be a hash of the parameters with a random part*/
32         char tmp[128];
33         int s1=(int)random();
34         int s2=(int)random();
35         long long int res=((long long int)s1)<<32 | (long long int) s2;
36         void *src=&res;
37         b64_encode(src, sizeof(long long int), tmp, sizeof(tmp));
38         return osip_strdup(tmp);
39 }
40
41
42 static void add_relay_info(sdp_message_t *sdp, int mline, const char *relay, const char *relay_session_id){
43
44         if (relay) sdp_message_a_attribute_add(sdp, mline,
45                                      osip_strdup ("relay-addr"),osip_strdup(relay));
46         if (relay_session_id) sdp_message_a_attribute_add(sdp, mline,
47                                      osip_strdup ("relay-session-id"), osip_strdup(relay_session_id));
48 }
49
50 #endif
51
52 static char * int_2char(int a){
53         char *p=osip_malloc(16);
54         snprintf(p,16,"%i",a);
55         return p;
56 }
57
58 /* return the value of attr "field" for payload pt at line pos (field=rtpmap,fmtp...)*/
59 static const char *sdp_message_a_attr_value_get_with_pt(sdp_message_t *sdp,int pos,int pt,const char *field)
60 {
61         int i,tmppt=0,scanned=0;
62         char *tmp;
63         sdp_attribute_t *attr;
64         for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
65                 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
66                         int nb = sscanf(attr->a_att_value,"%i %n",&tmppt,&scanned);
67                         /* the return value may depend on how %n is interpreted by the libc: see manpage*/
68                         if (nb == 1 || nb==2 ){
69                                 if (pt==tmppt){
70                                         tmp=attr->a_att_value+scanned;
71                                         if (strlen(tmp)>0)
72                                                 return tmp;
73                                 }
74                         }else ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
75                 }
76         }
77         return NULL;
78 }
79
80 #ifdef FOR_LATER
81 /* return the value of attr "field" */
82 static const char *sdp_message_a_attr_value_get(sdp_message_t *sdp,int pos,const char *field)
83 {
84         int i;
85         sdp_attribute_t *attr;
86         for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
87                 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
88                         return attr->a_att_value;
89                 }
90         }
91         return NULL;
92 }
93 #endif
94
95 static int _sdp_message_get_a_ptime(sdp_message_t *sdp, int mline){
96         int i,ret;
97         sdp_attribute_t *attr;
98         for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
99                 if (keywordcmp("ptime",attr->a_att_field)==0){
100                         int nb = sscanf(attr->a_att_value,"%i",&ret);
101                         /* the return value may depend on how %n is interpreted by the libc: see manpage*/
102                         if (nb == 1){
103                                 return ret;
104                         }else ms_warning("sdp has a strange a=ptime line (%s) ",attr->a_att_value);
105                 }
106         }
107         return 0;
108 }
109
110 static int _sdp_message_get_mline_dir(sdp_message_t *sdp, int mline){
111         int i;
112         sdp_attribute_t *attr;
113         for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
114                 if (keywordcmp("sendrecv",attr->a_att_field)==0){
115                         return SalStreamSendRecv;
116                 }else if (keywordcmp("sendonly",attr->a_att_field)==0){
117                         return SalStreamSendOnly;
118                 }else if (keywordcmp("recvonly",attr->a_att_field)==0){
119                         return SalStreamRecvOnly;
120                 }else if (keywordcmp("inactive",attr->a_att_field)==0){
121                         return SalStreamInactive;
122                 }
123         }
124         return SalStreamSendRecv;
125 }
126
127 static sdp_message_t *create_generic_sdp(const SalMediaDescription *desc)
128 {
129         sdp_message_t *local;
130         int inet6;
131         char sessid[16];
132         char sessver[16];
133         const char *rtp_addr = desc->addr;
134         
135         snprintf(sessid,16,"%i",desc->session_id);
136         snprintf(sessver,16,"%i",desc->session_ver);
137         sdp_message_init (&local);
138         if (strchr(desc->addr,':')!=NULL){
139                 inet6=1;
140         }else inet6=0;
141         sdp_message_v_version_set (local, osip_strdup ("0"));
142         sdp_message_o_origin_set (local, osip_strdup (desc->username),
143                           osip_strdup (sessid), osip_strdup (sessver),
144                           osip_strdup ("IN"), inet6 ? osip_strdup("IP6") : osip_strdup ("IP4"),
145                           osip_strdup (desc->addr));
146         sdp_message_s_name_set (local, osip_strdup ("Talk"));
147         if(!sal_media_description_has_dir (desc,SalStreamSendOnly))
148         {
149                 sdp_message_c_connection_add (local, -1,
150                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
151                                                 osip_strdup (rtp_addr), NULL, NULL);
152         }
153         else
154         {
155                 sdp_message_c_connection_add (local, -1,
156                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
157                                                 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
158         }               
159         sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
160         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
161                         int_2char(desc->bandwidth));
162         if (desc->ice_completed == TRUE) sdp_message_a_attribute_add(local, -1, osip_strdup("nortpproxy"), osip_strdup("yes"));
163         if (desc->ice_pwd[0] != '\0') sdp_message_a_attribute_add(local, -1, osip_strdup("ice-pwd"), osip_strdup(desc->ice_pwd));
164         if (desc->ice_ufrag[0] != '\0') sdp_message_a_attribute_add(local, -1, osip_strdup("ice-ufrag"), osip_strdup(desc->ice_ufrag));
165
166         return local;
167 }
168
169
170 static bool_t is_known_rtpmap(const PayloadType *pt){
171         switch(payload_type_get_number(pt)){
172                 case 0:
173                 case 8:
174                 case 3:
175                 case 34:
176                         return TRUE;
177         }
178         return FALSE;
179 }
180
181 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
182 {
183         char attr[256];
184         sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
185
186         if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
187                 if (pt->channels>1)
188                         snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt), 
189                                         pt->mime_type, pt->clock_rate,pt->channels);
190                 else
191                         snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt), 
192                                         pt->mime_type, pt->clock_rate);
193                 sdp_message_a_attribute_add (msg, line,
194                                                  osip_strdup ("rtpmap"), osip_strdup(attr));
195         }
196
197         if (pt->recv_fmtp != NULL)
198         {
199                 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
200                 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
201                                      osip_strdup(attr));
202         }
203 }
204
205 static void add_ice_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
206 {
207         char buffer[1024];
208         const SalIceCandidate *candidate;
209         int nb;
210         int i;
211
212         for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_ICE_CANDIDATES; i++) {
213                 candidate = &desc->ice_candidates[i];
214                 if ((candidate->addr[0] == '\0') || (candidate->port == 0)) break;
215                 nb = snprintf(buffer, sizeof(buffer), "%s %u UDP %u %s %d typ %s",
216                         candidate->foundation, candidate->componentID, candidate->priority, candidate->addr, candidate->port, candidate->type);
217                 if (nb < 0) {
218                         ms_error("Cannot add ICE candidate attribute!");
219                         return;
220                 }
221                 if (candidate->raddr[0] != '\0') {
222                         nb = snprintf(buffer + nb, sizeof(buffer) - nb, " raddr %s rport %d", candidate->raddr, candidate->rport);
223                         if (nb < 0) {
224                                 ms_error("Cannot add ICE candidate attribute!");
225                                 return;
226                         }
227                 }
228                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("candidate"), osip_strdup(buffer));
229         }
230 }
231
232 static void add_ice_remote_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
233 {
234         char buffer[1024];
235         char *ptr = buffer;
236         const SalIceRemoteCandidate *candidate;
237         int offset = 0;
238         int i;
239
240         buffer[0] = '\0';
241         for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES; i++) {
242                 candidate = &desc->ice_remote_candidates[i];
243                 if ((candidate->addr[0] != '\0') && (candidate->port != 0)) {
244                         offset = snprintf(ptr, buffer + sizeof(buffer) - ptr, "%s%d %s %d", (i > 0) ? " " : "", i + 1, candidate->addr, candidate->port);
245                         if (offset < 0) {
246                                 ms_error("Cannot add ICE remote-candidates attribute!");
247                                 return;
248                         }
249                         ptr += offset;
250                 }
251         }
252         if (buffer[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("remote-candidates"), osip_strdup(buffer));
253 }
254
255 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc){
256         const char *mt=NULL;
257         const MSList *elem;
258         const char *rtp_addr;
259         const char *rtcp_addr;
260         const char *dir="sendrecv";
261         int rtp_port;
262         int rtcp_port;
263         bool_t strip_well_known_rtpmaps;
264         bool_t different_rtp_and_rtcp_addr;
265         
266         switch (desc->type) {
267         case SalAudio:
268                 mt="audio";
269                 break;
270         case SalVideo:
271                 mt="video";
272                 break;
273         case SalOther:
274                 mt=desc->typeother;
275                 break;
276         }
277         rtp_addr=desc->rtp_addr;
278         rtcp_addr=desc->rtcp_addr;
279         rtp_port=desc->rtp_port;
280         rtcp_port=desc->rtcp_port;
281         if (desc->candidates[0].addr[0]!='\0'){
282                 rtp_addr=desc->candidates[0].addr;
283                 rtp_port=desc->candidates[0].port;
284         }
285
286         if (desc->proto == SalProtoRtpSavp) {
287                 int i;
288                 
289                 sdp_message_m_media_add (msg, osip_strdup (mt),
290                                          int_2char (rtp_port), NULL,
291                                          osip_strdup ("RTP/SAVP"));
292        
293                 /* add crypto lines */
294                 for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
295                         char buffer[1024];
296                         switch (desc->crypto[i].algo) {
297                                 case AES_128_SHA1_80:
298                                         snprintf(buffer, 1024, "%d %s inline:%s",
299                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_80", desc->crypto[i].master_key);
300                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
301                                                 osip_strdup(buffer));
302                                         break;
303                                 case AES_128_SHA1_32:
304                                         snprintf(buffer, 1024, "%d %s inline:%s",
305                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_32", desc->crypto[i].master_key);
306                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
307                                                 osip_strdup(buffer));
308                                         break;
309                                 case AES_128_NO_AUTH:
310                                         ms_warning("Unsupported crypto suite: AES_128_NO_AUTH");
311                                         break;
312                                 case NO_CIPHER_SHA1_80:
313                                         ms_warning("Unsupported crypto suite: NO_CIPHER_SHA1_80");
314                                         break; 
315                                 default:
316                                         i = SAL_CRYPTO_ALGO_MAX;
317                         }
318                 }
319                 
320         } else {
321                 sdp_message_m_media_add (msg, osip_strdup (mt),
322                                          int_2char (rtp_port), NULL,
323                                          osip_strdup ("RTP/AVP"));
324                 
325         }
326
327         /*only add a c= line within the stream description if address are differents*/
328         if (rtp_addr[0]!='\0' && strcmp(rtp_addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
329                 bool_t inet6;
330                 if (strchr(rtp_addr,':')!=NULL){
331                         inet6=TRUE;
332                 }else inet6=FALSE;
333                 sdp_message_c_connection_add (msg, lineno,
334                               osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
335                               osip_strdup (rtp_addr), NULL, NULL);
336         }
337
338         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
339                                      int_2char(desc->bandwidth));
340         if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
341                                 int_2char(desc->ptime));
342         strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
343         if (desc->payloads){
344                 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
345                         add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
346                 }
347         }else{
348                 /* to comply with SDP we cannot have an empty payload type number list */
349                 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
350                 sdp_message_m_payload_add (msg,lineno, int_2char (0));
351         }
352         switch(desc->dir){
353                 case SalStreamSendRecv:
354                         /*dir="sendrecv";*/
355                         dir=NULL;
356                 break;
357                 case SalStreamRecvOnly:
358                         dir="recvonly";
359                         break;
360                 case SalStreamSendOnly:
361                         dir="sendonly";
362                         break;
363                 case SalStreamInactive:
364                         dir="inactive";
365                         break;
366         }
367         if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
368         if (rtp_port != 0) {
369                 different_rtp_and_rtcp_addr = (rtcp_addr[0] != '\0') && (strcmp(rtp_addr, rtcp_addr) != 0);
370                 if ((rtcp_port != (rtp_port + 1)) || (different_rtp_and_rtcp_addr == TRUE)) {
371                         if (different_rtp_and_rtcp_addr == TRUE) {
372                                 char buffer[1024];
373                                 snprintf(buffer, sizeof(buffer), "%u IN IP4 %s", rtcp_port, rtcp_addr);
374                                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), osip_strdup(buffer));
375                         } else {
376                                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), int_2char(rtcp_port));
377                         }
378                 }
379         }
380         if (desc->ice_mismatch == TRUE) {
381                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-mismatch"), NULL);
382         } else {
383                 if (desc->ice_pwd[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-pwd"), osip_strdup(desc->ice_pwd));
384                 if (desc->ice_ufrag[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-ufrag"), osip_strdup(desc->ice_ufrag));
385                 add_ice_candidates(msg, lineno, desc);
386                 add_ice_remote_candidates(msg, lineno, desc);
387         }
388 }
389
390
391 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc){
392         int i;
393         sdp_message_t *msg=create_generic_sdp(desc);
394         for(i=0;i<desc->nstreams;++i){
395                 add_line(msg,i,&desc->streams[i]);
396         }
397         return msg;
398 }
399
400 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
401         if (rtpmap==NULL){
402                 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
403                 if (refpt){
404                         pt->mime_type=ms_strdup(refpt->mime_type);
405                         pt->clock_rate=refpt->clock_rate;
406                 }else{
407                         ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
408                             payload_type_get_number(pt));
409                         return -1;
410                 }
411         }else{
412                 char *mime=ms_strdup(rtpmap);
413                 char *p=strchr(mime,'/');
414                 if (p){
415                         char *chans;
416                         *p='\0';
417                         p++;
418                         chans=strchr(p,'/');
419                         if (chans){
420                                 *chans='\0';
421                                 chans++;
422                                 pt->channels=atoi(chans);
423                         }else pt->channels=1;
424                         pt->clock_rate=atoi(p);
425                 }
426                 pt->mime_type=mime;
427         }
428         return 0;
429 }
430
431 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){
432         int i,j;
433         const char *mtype,*proto,*rtp_port,*rtp_addr,*number;
434         sdp_bandwidth_t *sbw=NULL;
435         sdp_attribute_t *attr;
436         int nb_ice_candidates;
437
438         rtp_addr=sdp_message_c_addr_get (msg, -1, 0);
439         if (rtp_addr)
440                 strncpy(desc->addr,rtp_addr,sizeof(desc->addr));
441         for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
442                 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
443         }
444
445         /* Get ICE remote ufrag and remote pwd, and ice_lite flag */
446         for (i = 0; (i < SAL_MEDIA_DESCRIPTION_MAX_MESSAGE_ATTRIBUTES) && ((attr = sdp_message_attribute_get(msg, -1, i)) != NULL); i++) {
447                 if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
448                         strncpy(desc->ice_ufrag, attr->a_att_value, sizeof(desc->ice_ufrag));
449                 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
450                         strncpy(desc->ice_pwd, attr->a_att_value, sizeof(desc->ice_pwd));
451                 } else if (keywordcmp("ice-lite", attr->a_att_field) == 0) {
452                         desc->ice_lite = TRUE;
453                 }
454         }
455
456         /* for each m= line */
457         for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
458         {
459                 SalStreamDescription *stream=&desc->streams[i];
460                 nb_ice_candidates = 0;
461                 
462                 memset(stream,0,sizeof(*stream));
463                 mtype = sdp_message_m_media_get(msg, i);
464                 proto = sdp_message_m_proto_get (msg, i);
465                 rtp_port = sdp_message_m_port_get(msg, i);
466                 stream->proto=SalProtoUnknown;
467                 if (proto){
468                         if (strcasecmp(proto,"RTP/AVP")==0)
469                                 stream->proto=SalProtoRtpAvp;
470                         else if (strcasecmp(proto,"RTP/SAVP")==0){
471                                 stream->proto=SalProtoRtpSavp;
472                         }
473                 }
474                 rtp_addr = sdp_message_c_addr_get (msg, i, 0);
475                 if (rtp_addr != NULL)
476                         strncpy(stream->rtp_addr,rtp_addr,sizeof(stream->rtp_addr));
477                 if (rtp_port)
478                         stream->rtp_port=atoi(rtp_port);
479                 
480                 stream->ptime=_sdp_message_get_a_ptime(msg,i);
481                 if (strcasecmp("audio", mtype) == 0){
482                         stream->type=SalAudio;
483                 }else if (strcasecmp("video", mtype) == 0){
484                         stream->type=SalVideo;
485                 }else {
486                         stream->type=SalOther;
487                         strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
488                 }
489                 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
490                         if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
491                 }
492                 stream->dir=_sdp_message_get_mline_dir(msg,i);
493                 /* for each payload type */
494                 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
495                         const char *rtpmap,*fmtp;
496                         int ptn=atoi(number);
497                         PayloadType *pt=payload_type_new();
498                         payload_type_set_number(pt,ptn);
499                         /* get the rtpmap associated to this codec, if any */
500                         rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
501                         if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
502                                 /* get the fmtp, if any */
503                                 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
504                                 payload_type_set_send_fmtp(pt,fmtp);
505                                 stream->payloads=ms_list_append(stream->payloads,pt);
506                                 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
507                                         pt->send_fmtp ? pt->send_fmtp : "");
508                         }
509                 }
510
511                 /* Get media specific RTCP attribute */
512                 stream->rtcp_port = stream->rtp_port + 1;
513                 snprintf(stream->rtcp_addr, sizeof(stream->rtcp_addr), stream->rtp_addr);
514                 for (j = 0; ((attr = sdp_message_attribute_get(msg, i, j)) != NULL); j++) {
515                         if ((keywordcmp("rtcp", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
516                                 char tmp[256];
517                                 int nb = sscanf(attr->a_att_value, "%d IN IP4 %s", &stream->rtcp_port, tmp);
518                                 if (nb == 1) {
519                                         /* SDP rtcp attribute only contains the port */
520                                 } else if (nb == 2) {
521                                         strncpy(stream->rtcp_addr, tmp, sizeof(stream->rtcp_addr));
522                                 } else {
523                                         ms_warning("sdp has a strange a= line (%s) nb=%i", attr->a_att_value, nb);
524                                 }
525                         }
526                 }
527
528                 /* read crypto lines if any */
529                 if (stream->proto == SalProtoRtpSavp) {
530                         int k, valid_count = 0;
531                                 
532                         memset(&stream->crypto, 0, sizeof(stream->crypto));
533                         for (k=0;valid_count < SAL_CRYPTO_ALGO_MAX && (attr=sdp_message_attribute_get(msg,i,k))!=NULL;k++){
534                                 char tmp[256], tmp2[256];
535                                 if (keywordcmp("crypto",attr->a_att_field)==0 && attr->a_att_value!=NULL){
536                                         int nb = sscanf(attr->a_att_value, "%d %256s inline:%256s",
537                                                 &stream->crypto[valid_count].tag,
538                                                 tmp,
539                                                 tmp2);
540                                                 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
541                                                                 stream->crypto[valid_count].tag, 
542                                                                 tmp, 
543                                                                 tmp2);
544                                         if (nb == 3) {
545                                                 if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_80") == 0)
546                                                         stream->crypto[valid_count].algo = AES_128_SHA1_80;
547                                                 else if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_32") == 0)
548                                                         stream->crypto[valid_count].algo = AES_128_SHA1_32;
549                                                 else {
550                                                         ms_warning("Failed to parse crypto-algo: '%s'", tmp);
551                                                         stream->crypto[valid_count].algo = 0;
552                                                 }
553                                                 if (stream->crypto[valid_count].algo) {
554                                                         strncpy(stream->crypto[valid_count].master_key, tmp2, 41);
555                                                         stream->crypto[valid_count].master_key[40] = '\0';
556                                                         ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
557                                                                 stream->crypto[valid_count].tag, 
558                                                                 tmp, 
559                                                                 stream->crypto[valid_count].master_key);
560                                                         valid_count++;
561                                                 }
562                                         } else {
563                                                 ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
564                                         }
565                                 }
566                         }
567                         ms_message("Found: %d valid crypto lines", valid_count);
568                 }
569
570                 /* Get ICE candidate attributes if any */
571                 for (j = 0; (attr = sdp_message_attribute_get(msg, i, j)) != NULL; j++) {
572                         if ((keywordcmp("candidate", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
573                                 SalIceCandidate *candidate = &stream->ice_candidates[nb_ice_candidates];
574                                 int nb = sscanf(attr->a_att_value, "%s %u UDP %u %s %d typ %s raddr %s rport %d",
575                                         candidate->foundation, &candidate->componentID, &candidate->priority, candidate->addr, &candidate->port,
576                                         candidate->type, candidate->raddr, &candidate->rport);
577                                 if ((nb == 6) || (nb == 8)) nb_ice_candidates++;
578                                 else memset(candidate, 0, sizeof(*candidate));
579                         } else if ((keywordcmp("remote-candidates", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
580                                 SalIceRemoteCandidate candidate;
581                                 unsigned int componentID;
582                                 int offset;
583                                 char *ptr = attr->a_att_value;
584                                 while (3 == sscanf(ptr, "%u %s %u%n", &componentID, candidate.addr, &candidate.port, &offset)) {
585                                         if ((componentID > 0) && (componentID <= SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES)) {
586                                                 SalIceRemoteCandidate *remote_candidate = &stream->ice_remote_candidates[componentID - 1];
587                                                 strncpy(remote_candidate->addr, candidate.addr, sizeof(remote_candidate->addr));
588                                                 remote_candidate->port = candidate.port;
589                                         }
590                                         ptr += offset;
591                                         if (ptr[offset] == ' ') ptr += 1;
592                                 }
593                         } else if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
594                                 strncpy(stream->ice_ufrag, attr->a_att_value, sizeof(stream->ice_ufrag));
595                         } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
596                                 strncpy(stream->ice_pwd, attr->a_att_value, sizeof(stream->ice_pwd));
597                         } else if (keywordcmp("ice-mismatch", attr->a_att_field) == 0) {
598                                 stream->ice_mismatch = TRUE;
599                         }
600                 }
601         }
602         desc->nstreams=i;
603         return 0;
604 }