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