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