]> git.sesse.net Git - vlc/blob - src/stream_output/sdp.c
Make sdp_Start static since it is used nowhere outside
[vlc] / src / stream_output / sdp.c
1 /*****************************************************************************
2  * sdp.c : SDP creation helpers
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
22 #include <vlc/vlc.h>
23
24 #include <string.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <assert.h>
28 #include <vlc_network.h>
29 #include <vlc_charset.h>
30
31 #include "stream_output.h"
32
33 #define MAXSDPADDRESS 47
34
35 static
36 char *AddressToSDP (const struct sockaddr *addr, socklen_t addrlen, char *buf)
37 {
38     if (addrlen < offsetof (struct sockaddr, sa_family)
39                  + sizeof (addr->sa_family))
40         return NULL;
41
42     strcpy (buf, "IN IP* ");
43
44     if (vlc_getnameinfo (addr, addrlen, buf + 7, MAXSDPADDRESS - 7, NULL,
45                          NI_NUMERICHOST))
46         return NULL;
47
48     switch (addr->sa_family)
49     {
50         case AF_INET:
51         {
52             if (net_SockAddrIsMulticast (addr, addrlen))
53                 strcat (buf, "/255"); // obsolete in RFC4566, dummy value
54             buf[5] = '4';
55             break;
56         }
57
58 #ifdef AF_INET6
59         case AF_INET6:
60         {
61             char *ptr = strchr (buf, '%');
62             if (ptr != NULL)
63                 *ptr = '\0'; // remove scope ID
64             buf[5] = '6';
65             break;
66         }
67 #endif
68
69         default:
70             return NULL;
71     }
72
73     return buf;
74 }
75
76
77 static vlc_bool_t IsSDPString (const char *str)
78 {
79     if (strchr (str, '\r') != NULL)
80         return VLC_FALSE;
81     if (strchr (str, '\n') != NULL)
82         return VLC_FALSE;
83     if (!IsUTF8 (str))
84         return VLC_FALSE;
85     return VLC_TRUE;
86 }
87
88
89 static
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)
94 {
95     uint64_t now = NTPtime64 ();
96     char *sdp;
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=";
100
101     gethostname (hostname, sizeof (hostname));
102
103     if (name == NULL)
104         name = "Unnamed";
105     if (description == NULL)
106         description = "N/A";
107     if (url == NULL)
108         preurl = url = "";
109     if (email == NULL)
110         premail = email = "";
111     if (phone == NULL)
112         prephone = phone = "";
113
114     if (!IsSDPString (name) || !IsSDPString (description)
115      || !IsSDPString (url) || !IsSDPString (email) || !IsSDPString (phone)
116      || (AddressToSDP (addr, addrlen, connection) == NULL))
117         return NULL;
118
119     strcpy (sfilter, "");
120     if (srclen > 0)
121     {
122         char machine[MAXSDPADDRESS];
123
124         if (AddressToSDP (src, srclen, machine) != NULL)
125             sprintf (sfilter, "\r\na=source-filter: incl IN IP%c * %s",
126                      machine[5], machine + 7);
127     }
128
129     if (asprintf (&sdp, "v=0"
130                     "\r\no=- "I64Fu" "I64Fu" IN IP%c %s"
131                     "\r\ns=%s"
132                     "\r\ni=%s"
133                     "%s%s" // optional URL
134                     "%s%s" // optional email
135                     "%s%s" // optional phone number
136                     "\r\nc=%s"
137                         // bandwidth not specified
138                     "\r\nt=0 0" // one dummy time span
139                         // no repeating
140                         // no time zone adjustment (silly idea anyway)
141                         // no encryption key (deprecated)
142                     "\r\na=tool:"PACKAGE_STRING
143                     "\r\na=recvonly"
144                     "\r\na=type:broadcast"
145                     "\r\na=charset:UTF-8"
146                     "%s" // optional source filter
147                     "\r\n",
148                /* o= */ now, now, connection[5], hostname,
149                /* s= */ name,
150                /* i= */ description,
151                /* u= */ preurl, url,
152                /* e= */ premail, email,
153                /* p= */ prephone, phone,
154                /* c= */ connection,
155     /* source-filter */ sfilter) == -1)
156         return NULL;
157     return sdp;
158 }
159
160
161 static char *
162 vsdp_AddAttribute (char **sdp, const char *name, const char *fmt, va_list ap)
163 {
164     size_t oldlen = strlen (*sdp);
165     size_t addlen = sizeof ("a=\r\n") + strlen (name);
166
167     if (fmt != NULL)
168     {
169         va_list aq;
170
171         va_copy (aq, ap);
172         addlen += 1 + vsnprintf (NULL, 0, fmt, aq);
173         va_end (aq);
174     }
175
176     char *ret = realloc (*sdp, oldlen + addlen);
177     if (ret == NULL)
178         return NULL;
179
180     oldlen += sprintf (ret + oldlen, "a=%s", name);
181     if (fmt != NULL)
182     {
183         ret[oldlen++] = ':';
184         oldlen += vsprintf (ret + oldlen, fmt, ap);
185     }
186
187     strcpy (ret + oldlen, "\r\n");
188     return *sdp = ret;
189 }
190
191
192 char *sdp_AddAttribute (char **sdp, const char *name, const char *fmt, ...)
193 {
194     char *ret;
195     va_list ap;
196
197     va_start (ap, fmt);
198     ret = vsdp_AddAttribute (sdp, name, fmt, ap);
199     va_end (ap);
200
201     return ret;
202 }
203
204
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 *rtpmap, const char *fmtp)
209 {
210     char *newsdp, *ptr;
211     size_t inlen = strlen (*sdp), outlen = inlen;
212
213     /* Some default values */
214     if (type == NULL)
215         type = "video";
216     if (protocol == NULL)
217         protocol = "RTP/AVP";
218     assert (pt < 128u);
219
220     outlen += snprintf (NULL, 0,
221                         "m=%s %u %s %d\r\n"
222                         "b=TIAS:%u\r\n"
223                         "b=RR:0\r\n",
224                         type, dport, protocol, pt, bw);
225
226     newsdp = realloc (*sdp, outlen + 1);
227     if (newsdp == NULL)
228         return NULL;
229
230     *sdp = newsdp;
231     ptr = newsdp + inlen;
232
233     ptr += sprintf (ptr, "m=%s %u %s %u\r\n",
234                          type, dport, protocol, pt);
235     if (bw > 0)
236         ptr += sprintf (ptr, "b=%s:%u\r\n", bw_indep ? "TIAS" : "AS", bw);
237     ptr += sprintf (ptr, "b=RR:0\r\n");
238
239     /* RTP payload type map */
240     if (rtpmap != NULL)
241         sdp_AddAttribute (sdp, "rtpmap", "%u %s", pt, rtpmap);
242     /* Format parameters */
243     if (fmtp != NULL)
244         sdp_AddAttribute (sdp, "fmtp", "%u %s", pt, fmtp);
245
246     return newsdp;
247 }
248
249
250 char *vlc_sdp_Start (vlc_object_t *obj, const char *cfgpref,
251                      const struct sockaddr *src, size_t srclen,
252                      const struct sockaddr *addr, size_t addrlen)
253 {
254     size_t cfglen = strlen (cfgpref);
255     if (cfglen > 100)
256         return NULL;
257
258     char varname[cfglen + sizeof ("description")], *subvar = varname + cfglen;
259     strcpy (varname, cfgpref);
260
261     session_descriptor_t *p_session = calloc (1, sizeof (*p_session));
262     if (p_session == NULL)
263         return NULL;
264
265     strcpy (subvar, "name");
266     char *name = var_GetNonEmptyString (obj, varname);
267     strcpy (subvar, "description");
268     char *description = var_GetNonEmptyString (obj, varname);
269     strcpy (subvar, "url");
270     char *url = var_GetNonEmptyString (obj, varname);
271     strcpy (subvar, "email");
272     char *email = var_GetNonEmptyString (obj, varname);
273     strcpy (subvar, "phone");
274     char *phone = var_GetNonEmptyString (obj, varname);
275
276     char *sdp = sdp_Start (name, description, url, email, phone,
277                            src, srclen, addr, addrlen);
278     free (name);
279     free (description);
280     free (url);
281     free (email);
282     free (phone);
283
284     if (sdp == NULL)
285         return NULL;
286
287     /* Totally non-standard */
288     strcpy (subvar, "group");
289     char *group = var_GetNonEmptyString (obj, varname);
290     if (group != NULL)
291     {
292         sdp_AddAttribute (&sdp, "x-plgroup", "%s", group);
293         free (group);
294     }
295
296     return sdp;
297 }