]> git.sesse.net Git - vlc/blob - src/stream_output/sdp.c
Factorize rtpmap formatting
[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 *ptname, unsigned clock, unsigned chans,
209                     const char *fmtp)
210 {
211     char *newsdp, *ptr;
212     size_t inlen = strlen (*sdp), outlen = inlen;
213
214     /* Some default values */
215     if (type == NULL)
216         type = "video";
217     if (protocol == NULL)
218         protocol = "RTP/AVP";
219     assert (pt < 128u);
220
221     outlen += snprintf (NULL, 0,
222                         "m=%s %u %s %d\r\n"
223                         "b=TIAS:%u\r\n"
224                         "b=RR:0\r\n",
225                         type, dport, protocol, pt, bw);
226
227     newsdp = realloc (*sdp, outlen + 1);
228     if (newsdp == NULL)
229         return NULL;
230
231     *sdp = newsdp;
232     ptr = newsdp + inlen;
233
234     ptr += sprintf (ptr, "m=%s %u %s %u\r\n",
235                          type, dport, protocol, pt);
236     if (bw > 0)
237         ptr += sprintf (ptr, "b=%s:%u\r\n", bw_indep ? "TIAS" : "AS", bw);
238     ptr += sprintf (ptr, "b=RR:0\r\n");
239
240     /* RTP payload type map */
241     if (ptname != NULL)
242     {
243         if ((strcmp (type, "audio") == 0) && (chans != 1))
244             sdp_AddAttribute (sdp, "rtpmap", "%u %s/%u/%u", pt, ptname, clock,
245                               chans);
246         else
247             sdp_AddAttribute (sdp, "rtpmap", "%u %s/%u", pt, ptname, clock);
248     }
249     /* Format parameters */
250     if (fmtp != NULL)
251         sdp_AddAttribute (sdp, "fmtp", "%u %s", pt, fmtp);
252
253     return newsdp;
254 }
255
256
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)
260 {
261     size_t cfglen = strlen (cfgpref);
262     if (cfglen > 100)
263         return NULL;
264
265     char varname[cfglen + sizeof ("description")], *subvar = varname + cfglen;
266     strcpy (varname, cfgpref);
267
268     session_descriptor_t *p_session = calloc (1, sizeof (*p_session));
269     if (p_session == NULL)
270         return NULL;
271
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);
282
283     char *sdp = sdp_Start (name, description, url, email, phone,
284                            src, srclen, addr, addrlen);
285     free (name);
286     free (description);
287     free (url);
288     free (email);
289     free (phone);
290
291     if (sdp == NULL)
292         return NULL;
293
294     /* Totally non-standard */
295     strcpy (subvar, "group");
296     char *group = var_GetNonEmptyString (obj, varname);
297     if (group != NULL)
298     {
299         sdp_AddAttribute (&sdp, "x-plgroup", "%s", group);
300         free (group);
301     }
302
303     return sdp;
304 }