1 /*****************************************************************************
2 * sdp.c : SDP creation helpers
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
28 #include <vlc_network.h>
29 #include <vlc_charset.h>
31 #include "stream_output.h"
33 #define MAXSDPADDRESS 47
36 char *AddressToSDP (const struct sockaddr *addr, socklen_t addrlen, char *buf)
38 if (addrlen < offsetof (struct sockaddr, sa_family)
39 + sizeof (addr->sa_family))
42 strcpy (buf, "IN IP* ");
44 if (vlc_getnameinfo (addr, addrlen, buf + 7, MAXSDPADDRESS - 7, NULL,
48 switch (addr->sa_family)
52 if (net_SockAddrIsMulticast (addr, addrlen))
53 strcat (buf, "/255"); // obsolete in RFC4566, dummy value
61 char *ptr = strchr (buf, '%');
63 *ptr = '\0'; // remove scope ID
77 static vlc_bool_t IsSDPString (const char *str)
79 if (strchr (str, '\r') != NULL)
81 if (strchr (str, '\n') != NULL)
90 char *sdp_Start (const char *name, const char *description, const char *url,
91 const char *email, const char *phone,
92 const struct sockaddr *src, size_t srclen,
93 const struct sockaddr *addr, size_t addrlen)
95 uint64_t now = NTPtime64 ();
97 char connection[MAXSDPADDRESS], hostname[256],
98 sfilter[MAXSDPADDRESS + sizeof ("\r\na=source-filter: incl * ")];
99 const char *preurl = "\r\nu=", *premail = "\r\ne=", *prephone = "\r\np=";
101 gethostname (hostname, sizeof (hostname));
105 if (description == NULL)
110 premail = email = "";
112 prephone = phone = "";
114 if (!IsSDPString (name) || !IsSDPString (description)
115 || !IsSDPString (url) || !IsSDPString (email) || !IsSDPString (phone)
116 || (AddressToSDP (addr, addrlen, connection) == NULL))
119 strcpy (sfilter, "");
122 char machine[MAXSDPADDRESS];
124 if (AddressToSDP (src, srclen, machine) != NULL)
125 sprintf (sfilter, "\r\na=source-filter: incl IN IP%c * %s",
126 machine[5], machine + 7);
129 if (asprintf (&sdp, "v=0"
130 "\r\no=- "I64Fu" "I64Fu" IN IP%c %s"
133 "%s%s" // optional URL
134 "%s%s" // optional email
135 "%s%s" // optional phone number
137 // bandwidth not specified
138 "\r\nt=0 0" // one dummy time span
140 // no time zone adjustment (silly idea anyway)
141 // no encryption key (deprecated)
142 "\r\na=tool:"PACKAGE_STRING
144 "\r\na=type:broadcast"
145 "\r\na=charset:UTF-8"
146 "%s" // optional source filter
148 /* o= */ now, now, connection[5], hostname,
150 /* i= */ description,
151 /* u= */ preurl, url,
152 /* e= */ premail, email,
153 /* p= */ prephone, phone,
155 /* source-filter */ sfilter) == -1)
162 vsdp_AddAttribute (char **sdp, const char *name, const char *fmt, va_list ap)
164 size_t oldlen = strlen (*sdp);
165 size_t addlen = sizeof ("a=\r\n") + strlen (name);
172 addlen += 1 + vsnprintf (NULL, 0, fmt, aq);
176 char *ret = realloc (*sdp, oldlen + addlen);
180 oldlen += sprintf (ret + oldlen, "a=%s", name);
184 oldlen += vsprintf (ret + oldlen, fmt, ap);
187 strcpy (ret + oldlen, "\r\n");
192 char *sdp_AddAttribute (char **sdp, const char *name, const char *fmt, ...)
198 ret = vsdp_AddAttribute (sdp, name, fmt, ap);
205 char *sdp_AddMedia (char **sdp,
206 const char *type, const char *protocol, int dport,
207 unsigned pt, vlc_bool_t bw_indep, unsigned bw,
208 const char *ptname, unsigned clock, unsigned chans,
212 size_t inlen = strlen (*sdp), outlen = inlen;
214 /* Some default values */
217 if (protocol == NULL)
218 protocol = "RTP/AVP";
221 outlen += snprintf (NULL, 0,
225 type, dport, protocol, pt, bw);
227 newsdp = realloc (*sdp, outlen + 1);
232 ptr = newsdp + inlen;
234 ptr += sprintf (ptr, "m=%s %u %s %u\r\n",
235 type, dport, protocol, pt);
237 ptr += sprintf (ptr, "b=%s:%u\r\n", bw_indep ? "TIAS" : "AS", bw);
238 ptr += sprintf (ptr, "b=RR:0\r\n");
240 /* RTP payload type map */
243 if ((strcmp (type, "audio") == 0) && (chans != 1))
244 sdp_AddAttribute (sdp, "rtpmap", "%u %s/%u/%u", pt, ptname, clock,
247 sdp_AddAttribute (sdp, "rtpmap", "%u %s/%u", pt, ptname, clock);
249 /* Format parameters */
251 sdp_AddAttribute (sdp, "fmtp", "%u %s", pt, fmtp);
257 char *vlc_sdp_Start (vlc_object_t *obj, const char *cfgpref,
258 const struct sockaddr *src, size_t srclen,
259 const struct sockaddr *addr, size_t addrlen)
261 size_t cfglen = strlen (cfgpref);
265 char varname[cfglen + sizeof ("description")], *subvar = varname + cfglen;
266 strcpy (varname, cfgpref);
268 session_descriptor_t *p_session = calloc (1, sizeof (*p_session));
269 if (p_session == NULL)
272 strcpy (subvar, "name");
273 char *name = var_GetNonEmptyString (obj, varname);
274 strcpy (subvar, "description");
275 char *description = var_GetNonEmptyString (obj, varname);
276 strcpy (subvar, "url");
277 char *url = var_GetNonEmptyString (obj, varname);
278 strcpy (subvar, "email");
279 char *email = var_GetNonEmptyString (obj, varname);
280 strcpy (subvar, "phone");
281 char *phone = var_GetNonEmptyString (obj, varname);
283 char *sdp = sdp_Start (name, description, url, email, phone,
284 src, srclen, addr, addrlen);
294 /* Totally non-standard */
295 strcpy (subvar, "group");
296 char *group = var_GetNonEmptyString (obj, varname);
299 sdp_AddAttribute (&sdp, "x-plgroup", "%s", group);