1 /*****************************************************************************
2 * sdp.c : SDP creation helpers
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
30 #include <vlc_network.h>
31 #include <vlc_charset.h>
33 #include "stream_output.h"
35 #define MAXSDPADDRESS 47
38 char *AddressToSDP (const struct sockaddr *addr, socklen_t addrlen, char *buf)
40 if (addrlen < offsetof (struct sockaddr, sa_family)
41 + sizeof (addr->sa_family))
44 strcpy (buf, "IN IP* ");
46 if (vlc_getnameinfo (addr, addrlen, buf + 7, MAXSDPADDRESS - 7, NULL,
50 switch (addr->sa_family)
54 if (net_SockAddrIsMulticast (addr, addrlen))
55 strcat (buf, "/255"); // obsolete in RFC4566, dummy value
63 char *ptr = strchr (buf, '%');
65 *ptr = '\0'; // remove scope ID
79 static vlc_bool_t IsSDPString (const char *str)
81 if (strchr (str, '\r') != NULL)
83 if (strchr (str, '\n') != NULL)
91 char *StartSDP (const char *name, const char *description, const char *url,
92 const char *email, const char *phone, vlc_bool_t ssm,
93 const struct sockaddr *orig, socklen_t origlen,
94 const struct sockaddr *addr, socklen_t addrlen)
96 uint64_t t = NTPtime64 ();
97 char *sdp, machine[MAXSDPADDRESS], conn[MAXSDPADDRESS],
98 sfilter[MAXSDPADDRESS + sizeof ("\r\na=source-filter: incl * ")];
99 const char *preurl = "\r\nu=", *premail = "\r\ne=", *prephone = "\r\np=";
103 if (description == NULL)
108 premail = email = "";
110 prephone = phone = "";
112 if (!IsSDPString (name) || !IsSDPString (description)
113 || !IsSDPString (url) || !IsSDPString (email) || !IsSDPString (phone)
114 || (AddressToSDP (orig, origlen, machine) == NULL)
115 || (AddressToSDP (addr, addrlen, conn) == NULL))
119 sprintf (sfilter, "\r\na=source-filter: incl IN IP%c * %s",
120 machine[5], machine + 7);
124 if (asprintf (&sdp, "v=0"
125 "\r\no=- "I64Fu" "I64Fu" %s"
128 "%s%s" // optional URL
129 "%s%s" // optional email
130 "%s%s" // optional phone number
132 // bandwidth not specified
133 "\r\nt=0 0" // one dummy time span
135 // no time zone adjustment (silly idea anyway)
136 // no encryption key (deprecated)
137 "\r\na=tool:"PACKAGE_STRING
139 "\r\na=type:broadcast"
140 "\r\na=charset:UTF-8"
141 "%s" // optional source filter
143 /* o= */ t, t, machine,
145 /* i= */ description,
146 /* u= */ preurl, url,
147 /* e= */ premail, email,
148 /* p= */ prephone, phone,
150 /* source-filter */ sfilter) == -1)
156 char *MakeSDPMedia (const char *type, int dport, const char *protocol,
157 unsigned pt, const char *rtpmap, const char *fmtpfmt, ...)
159 char *sdp_media = NULL;
161 /* Some default values */
166 if (protocol == NULL)
167 protocol = "RTP/AVP";
170 /* RTP payload type map */
171 char sdp_rtpmap[rtpmap ? (sizeof ("a=rtpmap:123 *\r\n") + strlen (rtpmap)) : 1];
173 sprintf (sdp_rtpmap, "a=rtpmap:%u %s\r\n", pt, rtpmap);
177 /* Format parameters */
183 va_start (ap, fmtpfmt);
184 if (vasprintf (&fmtp, fmtpfmt, ap) == -1)
192 char sdp_fmtp[fmtp ? (sizeof ("a=fmtp:123 *\r\n") + strlen (fmtp)) : 1];
195 sprintf (sdp_fmtp, "a=fmtp:%u %s\r\n", pt, fmtp);
201 if (asprintf (&sdp_media, "m=%s %u %s %d\r\n" "%s" "%s",
202 type, dport, protocol, pt,
203 sdp_rtpmap, sdp_fmtp) == -1)