3 Copyright (C) 2010 Simon MORLAT (simon.morlat@free.fr)
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.
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.
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.
22 #include "ortp/ortp_srtp.h"
24 #include <eXosip2/eXosip.h>
26 #define keywordcmp(key,b) strncmp(key,b,sizeof(key))
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*/
35 long long int res=((long long int)s1)<<32 | (long long int) s2;
37 b64_encode(src, sizeof(long long int), tmp, sizeof(tmp));
38 return osip_strdup(tmp);
42 static void add_relay_info(sdp_message_t *sdp, int mline, const char *relay, const char *relay_session_id){
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));
52 static char * int_2char(int a){
53 char *p=osip_malloc(16);
54 snprintf(p,16,"%i",a);
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)
61 int i,tmppt=0,scanned=0;
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 ){
70 tmp=attr->a_att_value+scanned;
74 }else ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
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)
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;
95 static int _sdp_message_get_a_ptime(sdp_message_t *sdp, int mline){
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*/
104 }else ms_warning("sdp has a strange a=ptime line (%s) ",attr->a_att_value);
110 static int _sdp_message_get_mline_dir(sdp_message_t *sdp, int mline){
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 SalStreamSendOnly;
120 }else if (keywordcmp("inactive",attr->a_att_field)==0){
121 return SalStreamInactive;
124 return SalStreamSendRecv;
127 static sdp_message_t *create_generic_sdp(const SalMediaDescription *desc)
129 sdp_message_t *local;
134 snprintf(sessid,16,"%i",desc->session_id);
135 snprintf(sessver,16,"%i",desc->session_ver);
136 sdp_message_init (&local);
137 if (strchr(desc->addr,':')!=NULL){
140 sdp_message_v_version_set (local, osip_strdup ("0"));
141 sdp_message_o_origin_set (local, osip_strdup (desc->username),
142 osip_strdup (sessid), osip_strdup (sessver),
143 osip_strdup ("IN"), inet6 ? osip_strdup("IP6") : osip_strdup ("IP4"),
144 osip_strdup (desc->addr));
145 sdp_message_s_name_set (local, osip_strdup ("Talk"));
146 if(!sal_media_description_has_dir (desc,SalStreamSendOnly))
148 sdp_message_c_connection_add (local, -1,
149 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
150 osip_strdup (desc->addr), NULL, NULL);
154 sdp_message_c_connection_add (local, -1,
155 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
156 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
158 sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
159 if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
160 int_2char(desc->bandwidth));
165 static bool_t is_known_rtpmap(const PayloadType *pt){
166 switch(payload_type_get_number(pt)){
176 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
179 sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
181 if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
183 snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt),
184 pt->mime_type, pt->clock_rate,pt->channels);
186 snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt),
187 pt->mime_type, pt->clock_rate);
188 sdp_message_a_attribute_add (msg, line,
189 osip_strdup ("rtpmap"), osip_strdup(attr));
192 if (pt->recv_fmtp != NULL)
194 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
195 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
201 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc){
205 const char *dir="sendrecv";
207 bool_t strip_well_known_rtpmaps;
209 switch (desc->type) {
220 if (desc->candidates[0].addr[0]!='\0'){
221 addr=desc->candidates[0].addr;
222 port=desc->candidates[0].port;
227 /*only add a c= line within the stream description if address are differents*/
228 if (strcmp(addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
230 if (strchr(addr,':')!=NULL){
233 sdp_message_c_connection_add (msg, lineno,
234 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
235 osip_strdup (addr), NULL, NULL);
237 sdp_message_m_media_add (msg, osip_strdup (mt),
238 int_2char (port), NULL,
239 osip_strdup ("RTP/AVP"));
240 if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
241 int_2char(desc->bandwidth));
242 if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
243 int_2char(desc->ptime));
244 strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
246 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
247 add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
250 /* to comply with SDP we cannot have an empty payload type number list */
251 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
252 sdp_message_m_payload_add (msg,lineno, int_2char (0));
255 case SalStreamSendRecv:
259 case SalStreamRecvOnly:
262 case SalStreamSendOnly:
265 case SalStreamInactive:
269 if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
272 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc){
274 sdp_message_t *msg=create_generic_sdp(desc);
275 for(i=0;i<desc->nstreams;++i){
276 add_line(msg,i,&desc->streams[i]);
281 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
283 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
285 pt->mime_type=ms_strdup(refpt->mime_type);
286 pt->clock_rate=refpt->clock_rate;
288 ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
289 payload_type_get_number(pt));
293 char *mime=ms_strdup(rtpmap);
294 char *p=strchr(mime,'/');
303 pt->channels=atoi(chans);
304 }else pt->channels=1;
305 pt->clock_rate=atoi(p);
312 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){
314 const char *mtype,*proto,*port,*addr,*number;
315 sdp_bandwidth_t *sbw=NULL;
317 addr=sdp_message_c_addr_get (msg, -1, 0);
319 strncpy(desc->addr,addr,sizeof(desc->addr));
320 for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
321 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
324 /* for each m= line */
325 for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
327 SalStreamDescription *stream=&desc->streams[i];
329 memset(stream,0,sizeof(*stream));
330 mtype = sdp_message_m_media_get(msg, i);
331 proto = sdp_message_m_proto_get (msg, i);
332 port = sdp_message_m_port_get(msg, i);
333 stream->proto=SalProtoUnknown;
335 if (strcasecmp(proto,"RTP/AVP")==0)
336 stream->proto=SalProtoRtpAvp;
337 else if (strcasecmp(proto,"RTP/SAVP")==0){
338 stream->proto=SalProtoRtpSavp;
341 addr = sdp_message_c_addr_get (msg, i, 0);
343 strncpy(stream->addr,addr,sizeof(stream->addr));
345 stream->port=atoi(port);
347 stream->ptime=_sdp_message_get_a_ptime(msg,i);
348 if (strcasecmp("audio", mtype) == 0){
349 stream->type=SalAudio;
350 }else if (strcasecmp("video", mtype) == 0){
351 stream->type=SalVideo;
353 stream->type=SalOther;
354 strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
356 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
357 if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
359 stream->dir=_sdp_message_get_mline_dir(msg,i);
360 /* for each payload type */
361 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
362 const char *rtpmap,*fmtp;
363 int ptn=atoi(number);
364 PayloadType *pt=payload_type_new();
365 payload_type_set_number(pt,ptn);
366 /* get the rtpmap associated to this codec, if any */
367 rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
368 if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
369 /* get the fmtp, if any */
370 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
371 payload_type_set_send_fmtp(pt,fmtp);
372 stream->payloads=ms_list_append(stream->payloads,pt);
373 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
374 pt->send_fmtp ? pt->send_fmtp : "");