]> git.sesse.net Git - vlc/blob - src/stream_output/sdp.c
Fix *printf usage
[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 char *sdp_Start (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)
93 {
94     uint64_t now = NTPtime64 ();
95     char *sdp;
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=";
99
100     gethostname (hostname, sizeof (hostname));
101
102     if (name == NULL)
103         name = "Unnamed";
104     if (description == NULL)
105         description = "N/A";
106     if (url == NULL)
107         preurl = url = "";
108     if (email == NULL)
109         premail = email = "";
110     if (phone == NULL)
111         prephone = phone = "";
112
113     if (!IsSDPString (name) || !IsSDPString (description)
114      || !IsSDPString (url) || !IsSDPString (email) || !IsSDPString (phone)
115      || (AddressToSDP (addr, addrlen, connection) == NULL))
116         return NULL;
117
118     strcpy (sfilter, "");
119     if (srclen > 0)
120     {
121         char machine[MAXSDPADDRESS];
122
123         if (AddressToSDP (src, srclen, machine) != NULL)
124             sprintf (sfilter, "\r\na=source-filter: incl IN IP%c * %s",
125                      machine[5], machine + 7);
126     }
127
128     if (asprintf (&sdp, "v=0"
129                     "\r\no=- "I64Fu" "I64Fu" IN IP%c %s"
130                     "\r\ns=%s"
131                     "\r\ni=%s"
132                     "%s%s" // optional URL
133                     "%s%s" // optional email
134                     "%s%s" // optional phone number
135                     "\r\nc=%s"
136                         // bandwidth not specified
137                     "\r\nt=0 0" // one dummy time span
138                         // no repeating
139                         // no time zone adjustment (silly idea anyway)
140                         // no encryption key (deprecated)
141                     "\r\na=tool:"PACKAGE_STRING
142                     "\r\na=recvonly"
143                     "\r\na=type:broadcast"
144                     "\r\na=charset:UTF-8"
145                     "%s" // optional source filter
146                     "\r\n",
147                /* o= */ now, now, connection[5], hostname,
148                /* s= */ name,
149                /* i= */ description,
150                /* u= */ preurl, url,
151                /* e= */ premail, email,
152                /* p= */ prephone, phone,
153                /* c= */ connection,
154     /* source-filter */ sfilter) == -1)
155         return NULL;
156     return sdp;
157 }
158
159
160 static char *
161 vsdp_AddAttribute (char **sdp, const char *name, const char *fmt, va_list ap)
162 {
163     size_t oldlen = strlen (*sdp);
164     size_t addlen =
165         sizeof ("a=:\r\n") + strlen (name) + vsnprintf (NULL, 0, fmt, ap);
166     char *ret = realloc (*sdp, oldlen + addlen);
167
168     if (ret == NULL)
169         return NULL;
170
171     oldlen += sprintf (ret + oldlen, "a=%s:", name);
172     oldlen += vsprintf (ret + oldlen, fmt, ap);
173     strcpy (ret + oldlen, "\r\n");
174     return *sdp = ret;
175 }
176
177
178 char *sdp_AddAttribute (char **sdp, const char *name, const char *fmt, ...)
179 {
180     char *ret;
181
182     if (fmt != NULL)
183     {
184         va_list ap;
185
186         va_start (ap, fmt);
187         ret = vsdp_AddAttribute (sdp, name, fmt, ap);
188         va_end (ap);
189     }
190     else
191     {
192         size_t oldlen = strlen (*sdp);
193         ret = realloc (*sdp, oldlen + strlen (name) + sizeof ("a=\r\n"));
194         if (ret == NULL)
195             return NULL;
196
197         sprintf (ret + oldlen, "a=%s\r\n", name);
198     }
199     return ret;
200 }
201
202
203 char *sdp_AddMedia (char **sdp,
204                     const char *type, const char *protocol, int dport,
205                     unsigned pt, vlc_bool_t bw_indep, unsigned bw,
206                     const char *rtpmap, const char *fmtp)
207 {
208     char *newsdp, *ptr;
209     size_t inlen = strlen (*sdp), outlen = inlen;
210
211     /* Some default values */
212     if (type == NULL)
213         type = "video";
214     if (protocol == NULL)
215         protocol = "RTP/AVP";
216     assert (pt < 128u);
217
218     outlen += snprintf (NULL, 0,
219                         "m=%s %u %s %d\r\n"
220                         "b=RR:0\r\n",
221                         type, dport, protocol, pt);
222
223     newsdp = realloc (*sdp, outlen + 1);
224     if (newsdp == NULL)
225         return NULL;
226
227     *sdp = newsdp;
228     ptr = newsdp + inlen;
229
230     ptr += sprintf (ptr, "m=%s %u %s %u\r\n"
231                          "b=RR:0\r\n",
232                          type, dport, protocol, pt);
233
234     /* RTP payload type map */
235     if (rtpmap != NULL)
236         sdp_AddAttribute (sdp, "rtpmap", "%u %s", pt, rtpmap);
237     /* Format parameters */
238     if (fmtp != NULL)
239         sdp_AddAttribute (sdp, "fmtp", "%u %s", pt, fmtp);
240
241     return newsdp;
242 }