]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_sdp.c
implement edge detection, and automatic configuration of call parameters for low...
[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         /* Do not set the c= line to 0.0.0.0 if there is an ICE session. */
148         if((desc->ice_ufrag[0] != '\0') || !sal_media_description_has_dir (desc,SalStreamSendOnly))
149         {
150                 sdp_message_c_connection_add (local, -1,
151                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
152                                                 osip_strdup (rtp_addr), NULL, NULL);
153         }
154         else
155         {
156                 sdp_message_c_connection_add (local, -1,
157                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
158                                                 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
159         }               
160         sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
161         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
162                         int_2char(desc->bandwidth));
163         if (desc->ice_completed == TRUE) sdp_message_a_attribute_add(local, -1, osip_strdup("nortpproxy"), osip_strdup("yes"));
164         if (desc->ice_pwd[0] != '\0') sdp_message_a_attribute_add(local, -1, osip_strdup("ice-pwd"), osip_strdup(desc->ice_pwd));
165         if (desc->ice_ufrag[0] != '\0') sdp_message_a_attribute_add(local, -1, osip_strdup("ice-ufrag"), osip_strdup(desc->ice_ufrag));
166
167         return local;
168 }
169
170
171 static bool_t is_known_rtpmap(const PayloadType *pt){
172         switch(payload_type_get_number(pt)){
173                 case 0:
174                 case 8:
175                 case 3:
176                 case 34:
177                         return TRUE;
178         }
179         return FALSE;
180 }
181
182 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
183 {
184         char attr[256];
185         sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
186
187         if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
188                 if (pt->channels>1)
189                         snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt), 
190                                         pt->mime_type, pt->clock_rate,pt->channels);
191                 else
192                         snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt), 
193                                         pt->mime_type, pt->clock_rate);
194                 sdp_message_a_attribute_add (msg, line,
195                                                  osip_strdup ("rtpmap"), osip_strdup(attr));
196         }
197
198         if (pt->recv_fmtp != NULL)
199         {
200                 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
201                 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
202                                      osip_strdup(attr));
203         }
204 }
205
206 static void add_ice_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
207 {
208         char buffer[1024];
209         const SalIceCandidate *candidate;
210         int nb;
211         int i;
212
213         for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_ICE_CANDIDATES; i++) {
214                 candidate = &desc->ice_candidates[i];
215                 if ((candidate->addr[0] == '\0') || (candidate->port == 0)) break;
216                 nb = snprintf(buffer, sizeof(buffer), "%s %u UDP %u %s %d typ %s",
217                         candidate->foundation, candidate->componentID, candidate->priority, candidate->addr, candidate->port, candidate->type);
218                 if (nb < 0) {
219                         ms_error("Cannot add ICE candidate attribute!");
220                         return;
221                 }
222                 if (candidate->raddr[0] != '\0') {
223                         nb = snprintf(buffer + nb, sizeof(buffer) - nb, " raddr %s rport %d", candidate->raddr, candidate->rport);
224                         if (nb < 0) {
225                                 ms_error("Cannot add ICE candidate attribute!");
226                                 return;
227                         }
228                 }
229                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("candidate"), osip_strdup(buffer));
230         }
231 }
232
233 static void add_ice_remote_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
234 {
235         char buffer[1024];
236         char *ptr = buffer;
237         const SalIceRemoteCandidate *candidate;
238         int offset = 0;
239         int i;
240
241         buffer[0] = '\0';
242         for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES; i++) {
243                 candidate = &desc->ice_remote_candidates[i];
244                 if ((candidate->addr[0] != '\0') && (candidate->port != 0)) {
245                         offset = snprintf(ptr, buffer + sizeof(buffer) - ptr, "%s%d %s %d", (i > 0) ? " " : "", i + 1, candidate->addr, candidate->port);
246                         if (offset < 0) {
247                                 ms_error("Cannot add ICE remote-candidates attribute!");
248                                 return;
249                         }
250                         ptr += offset;
251                 }
252         }
253         if (buffer[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("remote-candidates"), osip_strdup(buffer));
254 }
255
256 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc){
257         const char *mt=NULL;
258         const MSList *elem;
259         const char *rtp_addr;
260         const char *rtcp_addr;
261         const char *dir="sendrecv";
262         int rtp_port;
263         int rtcp_port;
264         bool_t strip_well_known_rtpmaps;
265         bool_t different_rtp_and_rtcp_addr;
266         
267         switch (desc->type) {
268         case SalAudio:
269                 mt="audio";
270                 break;
271         case SalVideo:
272                 mt="video";
273                 break;
274         case SalOther:
275                 mt=desc->typeother;
276                 break;
277         }
278         rtp_addr=desc->rtp_addr;
279         rtcp_addr=desc->rtcp_addr;
280         rtp_port=desc->rtp_port;
281         rtcp_port=desc->rtcp_port;
282
283         if (desc->proto == SalProtoRtpSavp) {
284                 int i;
285                 
286                 sdp_message_m_media_add (msg, osip_strdup (mt),
287                                          int_2char (rtp_port), NULL,
288                                          osip_strdup ("RTP/SAVP"));
289        
290                 /* add crypto lines */
291                 for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
292                         char buffer[1024];
293                         switch (desc->crypto[i].algo) {
294                                 case AES_128_SHA1_80:
295                                         snprintf(buffer, 1024, "%d %s inline:%s",
296                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_80", desc->crypto[i].master_key);
297                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
298                                                 osip_strdup(buffer));
299                                         break;
300                                 case AES_128_SHA1_32:
301                                         snprintf(buffer, 1024, "%d %s inline:%s",
302                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_32", desc->crypto[i].master_key);
303                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
304                                                 osip_strdup(buffer));
305                                         break;
306                                 case AES_128_NO_AUTH:
307                                         ms_warning("Unsupported crypto suite: AES_128_NO_AUTH");
308                                         break;
309                                 case NO_CIPHER_SHA1_80:
310                                         ms_warning("Unsupported crypto suite: NO_CIPHER_SHA1_80");
311                                         break; 
312                                 default:
313                                         i = SAL_CRYPTO_ALGO_MAX;
314                         }
315                 }
316                 
317         } else {
318                 sdp_message_m_media_add (msg, osip_strdup (mt),
319                                          int_2char (rtp_port), NULL,
320                                          osip_strdup ("RTP/AVP"));
321                 
322         }
323
324         /*only add a c= line within the stream description if address are differents*/
325         if (rtp_addr[0]!='\0' && strcmp(rtp_addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
326                 bool_t inet6;
327                 if (strchr(rtp_addr,':')!=NULL){
328                         inet6=TRUE;
329                 }else inet6=FALSE;
330                 sdp_message_c_connection_add (msg, lineno,
331                               osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
332                               osip_strdup (rtp_addr), NULL, NULL);
333         }
334
335         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
336                                      int_2char(desc->bandwidth));
337         if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
338                                 int_2char(desc->ptime));
339         strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
340         if (desc->payloads){
341                 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
342                         add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
343                 }
344         }else{
345                 /* to comply with SDP we cannot have an empty payload type number list */
346                 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
347                 sdp_message_m_payload_add (msg,lineno, int_2char (0));
348         }
349         switch(desc->dir){
350                 case SalStreamSendRecv:
351                         /*dir="sendrecv";*/
352                         dir=NULL;
353                 break;
354                 case SalStreamRecvOnly:
355                         dir="recvonly";
356                         break;
357                 case SalStreamSendOnly:
358                         dir="sendonly";
359                         break;
360                 case SalStreamInactive:
361                         dir="inactive";
362                         break;
363         }
364         if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
365         if (rtp_port != 0) {
366                 different_rtp_and_rtcp_addr = (rtcp_addr[0] != '\0') && (strcmp(rtp_addr, rtcp_addr) != 0);
367                 if ((rtcp_port != (rtp_port + 1)) || (different_rtp_and_rtcp_addr == TRUE)) {
368                         if (different_rtp_and_rtcp_addr == TRUE) {
369                                 char buffer[1024];
370                                 snprintf(buffer, sizeof(buffer), "%u IN IP4 %s", rtcp_port, rtcp_addr);
371                                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), osip_strdup(buffer));
372                         } else {
373                                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), int_2char(rtcp_port));
374                         }
375                 }
376         }
377         if (desc->ice_completed == TRUE) {
378                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("nortpproxy"), osip_strdup("yes"));
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->rtp_port != 0) {
384                         if (desc->ice_pwd[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-pwd"), osip_strdup(desc->ice_pwd));
385                         if (desc->ice_ufrag[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-ufrag"), osip_strdup(desc->ice_ufrag));
386                         add_ice_candidates(msg, lineno, desc);
387                         add_ice_remote_candidates(msg, lineno, desc);
388                 }
389         }
390 }
391
392
393 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc){
394         int i;
395         sdp_message_t *msg=create_generic_sdp(desc);
396         for(i=0;i<desc->nstreams;++i){
397                 add_line(msg,i,&desc->streams[i]);
398         }
399         return msg;
400 }
401
402 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
403         if (rtpmap==NULL){
404                 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
405                 if (refpt){
406                         pt->mime_type=ms_strdup(refpt->mime_type);
407                         pt->clock_rate=refpt->clock_rate;
408                 }else{
409                         ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
410                             payload_type_get_number(pt));
411                         return -1;
412                 }
413         }else{
414                 char *mime=ms_strdup(rtpmap);
415                 char *p=strchr(mime,'/');
416                 if (p){
417                         char *chans;
418                         *p='\0';
419                         p++;
420                         chans=strchr(p,'/');
421                         if (chans){
422                                 *chans='\0';
423                                 chans++;
424                                 pt->channels=atoi(chans);
425                         }else pt->channels=1;
426                         pt->clock_rate=atoi(p);
427                 }
428                 pt->mime_type=mime;
429         }
430         return 0;
431 }
432
433 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){
434         int i,j;
435         const char *mtype,*proto,*rtp_port,*rtp_addr,*number;
436         sdp_bandwidth_t *sbw=NULL;
437         sdp_attribute_t *attr;
438         int nb_ice_candidates;
439
440         rtp_addr=sdp_message_c_addr_get (msg, -1, 0);
441         if (rtp_addr)
442                 strncpy(desc->addr,rtp_addr,sizeof(desc->addr));
443         for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
444                 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
445         }
446
447         /* Get ICE remote ufrag and remote pwd, and ice_lite flag */
448         for (i = 0; (i < SAL_MEDIA_DESCRIPTION_MAX_MESSAGE_ATTRIBUTES) && ((attr = sdp_message_attribute_get(msg, -1, i)) != NULL); i++) {
449                 if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
450                         strncpy(desc->ice_ufrag, attr->a_att_value, sizeof(desc->ice_ufrag));
451                 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
452                         strncpy(desc->ice_pwd, attr->a_att_value, sizeof(desc->ice_pwd));
453                 } else if (keywordcmp("ice-lite", attr->a_att_field) == 0) {
454                         desc->ice_lite = TRUE;
455                 }
456         }
457
458         /* for each m= line */
459         for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
460         {
461                 SalStreamDescription *stream=&desc->streams[i];
462                 nb_ice_candidates = 0;
463                 
464                 memset(stream,0,sizeof(*stream));
465                 mtype = sdp_message_m_media_get(msg, i);
466                 proto = sdp_message_m_proto_get (msg, i);
467                 rtp_port = sdp_message_m_port_get(msg, i);
468                 stream->proto=SalProtoUnknown;
469                 if (proto){
470                         if (strcasecmp(proto,"RTP/AVP")==0)
471                                 stream->proto=SalProtoRtpAvp;
472                         else if (strcasecmp(proto,"RTP/SAVP")==0){
473                                 stream->proto=SalProtoRtpSavp;
474                         }
475                 }
476                 rtp_addr = sdp_message_c_addr_get (msg, i, 0);
477                 if (rtp_addr != NULL)
478                         strncpy(stream->rtp_addr,rtp_addr,sizeof(stream->rtp_addr));
479                 if (rtp_port)
480                         stream->rtp_port=atoi(rtp_port);
481                 
482                 stream->ptime=_sdp_message_get_a_ptime(msg,i);
483                 if (strcasecmp("audio", mtype) == 0){
484                         stream->type=SalAudio;
485                 }else if (strcasecmp("video", mtype) == 0){
486                         stream->type=SalVideo;
487                 }else {
488                         stream->type=SalOther;
489                         strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
490                 }
491                 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
492                         if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
493                 }
494                 stream->dir=_sdp_message_get_mline_dir(msg,i);
495                 /* for each payload type */
496                 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
497                         const char *rtpmap,*fmtp;
498                         int ptn=atoi(number);
499                         PayloadType *pt=payload_type_new();
500                         payload_type_set_number(pt,ptn);
501                         /* get the rtpmap associated to this codec, if any */
502                         rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
503                         if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
504                                 /* get the fmtp, if any */
505                                 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
506                                 payload_type_set_send_fmtp(pt,fmtp);
507                                 stream->payloads=ms_list_append(stream->payloads,pt);
508                                 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
509                                         pt->send_fmtp ? pt->send_fmtp : "");
510                         }
511                 }
512
513                 /* Get media specific RTCP attribute */
514                 stream->rtcp_port = stream->rtp_port + 1;
515                 snprintf(stream->rtcp_addr, sizeof(stream->rtcp_addr), "%s", stream->rtp_addr);
516                 for (j = 0; ((attr = sdp_message_attribute_get(msg, i, j)) != NULL); j++) {
517                         if ((keywordcmp("rtcp", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
518                                 char tmp[256];
519                                 int nb = sscanf(attr->a_att_value, "%d IN IP4 %s", &stream->rtcp_port, tmp);
520                                 if (nb == 1) {
521                                         /* SDP rtcp attribute only contains the port */
522                                 } else if (nb == 2) {
523                                         strncpy(stream->rtcp_addr, tmp, sizeof(stream->rtcp_addr));
524                                 } else {
525                                         ms_warning("sdp has a strange a= line (%s) nb=%i", attr->a_att_value, nb);
526                                 }
527                         }
528                 }
529
530                 /* read crypto lines if any */
531                 if (stream->proto == SalProtoRtpSavp) {
532                         int k, valid_count = 0;
533                                 
534                         memset(&stream->crypto, 0, sizeof(stream->crypto));
535                         for (k=0;valid_count < SAL_CRYPTO_ALGO_MAX && (attr=sdp_message_attribute_get(msg,i,k))!=NULL;k++){
536                                 char tmp[256], tmp2[256];
537                                 if (keywordcmp("crypto",attr->a_att_field)==0 && attr->a_att_value!=NULL){
538                                         int nb = sscanf(attr->a_att_value, "%d %256s inline:%256s",
539                                                 &stream->crypto[valid_count].tag,
540                                                 tmp,
541                                                 tmp2);
542                                                 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
543                                                                 stream->crypto[valid_count].tag, 
544                                                                 tmp, 
545                                                                 tmp2);
546                                         if (nb == 3) {
547                                                 if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_80") == 0)
548                                                         stream->crypto[valid_count].algo = AES_128_SHA1_80;
549                                                 else if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_32") == 0)
550                                                         stream->crypto[valid_count].algo = AES_128_SHA1_32;
551                                                 else {
552                                                         ms_warning("Failed to parse crypto-algo: '%s'", tmp);
553                                                         stream->crypto[valid_count].algo = 0;
554                                                 }
555                                                 if (stream->crypto[valid_count].algo) {
556                                                         strncpy(stream->crypto[valid_count].master_key, tmp2, 41);
557                                                         stream->crypto[valid_count].master_key[40] = '\0';
558                                                         ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
559                                                                 stream->crypto[valid_count].tag, 
560                                                                 tmp, 
561                                                                 stream->crypto[valid_count].master_key);
562                                                         valid_count++;
563                                                 }
564                                         } else {
565                                                 ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
566                                         }
567                                 }
568                         }
569                         ms_message("Found: %d valid crypto lines", valid_count);
570                 }
571
572                 /* Get ICE candidate attributes if any */
573                 for (j = 0; (attr = sdp_message_attribute_get(msg, i, j)) != NULL; j++) {
574                         if ((keywordcmp("candidate", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
575                                 SalIceCandidate *candidate = &stream->ice_candidates[nb_ice_candidates];
576                                 int nb = sscanf(attr->a_att_value, "%s %u UDP %u %s %d typ %s raddr %s rport %d",
577                                         candidate->foundation, &candidate->componentID, &candidate->priority, candidate->addr, &candidate->port,
578                                         candidate->type, candidate->raddr, &candidate->rport);
579                                 if ((nb == 6) || (nb == 8)) nb_ice_candidates++;
580                                 else memset(candidate, 0, sizeof(*candidate));
581                         } else if ((keywordcmp("remote-candidates", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
582                                 SalIceRemoteCandidate candidate;
583                                 unsigned int componentID;
584                                 int offset;
585                                 char *ptr = attr->a_att_value;
586                                 while (3 == sscanf(ptr, "%u %s %u%n", &componentID, candidate.addr, &candidate.port, &offset)) {
587                                         if ((componentID > 0) && (componentID <= SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES)) {
588                                                 SalIceRemoteCandidate *remote_candidate = &stream->ice_remote_candidates[componentID - 1];
589                                                 strncpy(remote_candidate->addr, candidate.addr, sizeof(remote_candidate->addr));
590                                                 remote_candidate->port = candidate.port;
591                                         }
592                                         ptr += offset;
593                                         if (ptr[offset] == ' ') ptr += 1;
594                                 }
595                         } else if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
596                                 strncpy(stream->ice_ufrag, attr->a_att_value, sizeof(stream->ice_ufrag));
597                         } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
598                                 strncpy(stream->ice_pwd, attr->a_att_value, sizeof(stream->ice_pwd));
599                         } else if (keywordcmp("ice-mismatch", attr->a_att_field) == 0) {
600                                 stream->ice_mismatch = TRUE;
601                         }
602                 }
603         }
604         desc->nstreams=i;
605         return 0;
606 }