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)
89 char *StartSDP (const char *name, const char *description, const char *url,
90 const char *email, const char *phone,
91 const struct sockaddr *src, socklen_t srclen,
92 const struct sockaddr *addr, socklen_t addrlen)
94 uint64_t now = NTPtime64 ();
96 char connection[MAXSDPADDRESS], hostname[256],
97 sfilter[MAXSDPADDRESS + sizeof ("\r\na=source-filter: incl * ")];
98 const char *preurl = "\r\nu=", *premail = "\r\ne=", *prephone = "\r\np=";
100 gethostname (hostname, sizeof (hostname));
104 if (description == NULL)
109 premail = email = "";
111 prephone = phone = "";
113 if (!IsSDPString (name) || !IsSDPString (description)
114 || !IsSDPString (url) || !IsSDPString (email) || !IsSDPString (phone)
115 || (AddressToSDP (addr, addrlen, connection) == NULL))
118 strcpy (sfilter, "");
121 char machine[MAXSDPADDRESS];
123 if (AddressToSDP (src, srclen, machine) != NULL)
124 sprintf (sfilter, "\r\na=source-filter: incl IN IP%c * %s",
125 machine[5], machine + 7);
128 if (asprintf (&sdp, "v=0"
129 "\r\no=- "I64Fu" "I64Fu" IN IP%c %s"
132 "%s%s" // optional URL
133 "%s%s" // optional email
134 "%s%s" // optional phone number
136 // bandwidth not specified
137 "\r\nt=0 0" // one dummy time span
139 // no time zone adjustment (silly idea anyway)
140 // no encryption key (deprecated)
141 "\r\na=tool:"PACKAGE_STRING
143 "\r\na=type:broadcast"
144 "\r\na=charset:UTF-8"
145 "%s" // optional source filter
147 /* o= */ now, now, connection[5], hostname,
149 /* i= */ description,
150 /* u= */ preurl, url,
151 /* e= */ premail, email,
152 /* p= */ prephone, phone,
154 /* source-filter */ sfilter) == -1)
160 char *vAddSDPMedia (const char *type, int dport, const char *protocol,
161 unsigned pt, const char *rtpmap,
162 const char *fmtpfmt, va_list ap)
164 char *sdp_media = NULL;
166 /* Some default values */
171 if (protocol == NULL)
172 protocol = "RTP/AVP";
175 /* RTP payload type map */
176 char sdp_rtpmap[rtpmap ? (sizeof ("a=rtpmap:123 *\r\n") + strlen (rtpmap)) : 1];
178 sprintf (sdp_rtpmap, "a=rtpmap:%u %s\r\n", pt, rtpmap);
182 /* Format parameters */
184 if ((fmtpfmt != NULL)
185 && (vasprintf (&fmtp, fmtpfmt, ap) == -1))
188 char sdp_fmtp[fmtp ? (sizeof ("a=fmtp:123 *\r\n") + strlen (fmtp)) : 1];
191 sprintf (sdp_fmtp, "a=fmtp:%u %s\r\n", pt, fmtp);
197 if (asprintf (&sdp_media, "m=%s %u %s %d\r\n" "b=RR:0\r\n" "%s" "%s",
198 type, dport, protocol, pt,
199 sdp_rtpmap, sdp_fmtp) == -1)
206 char *AddSDPMedia (const char *type, int dport, const char *protocol,
207 unsigned pt, const char *rtpmap, const char *fmtpfmt, ...)
211 va_start (ap, fmtpfmt);
212 ret = vAddSDPMedia (type, dport, protocol, pt, rtpmap, fmtpfmt, ap);