]> git.sesse.net Git - vlc/blob - src/stream_output/sdp.c
Use size_t instead of socklen_t
[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, size_t srclen,
92                 const struct sockaddr *addr, size_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 = sizeof ("a=\r\n") + strlen (name);
165
166     if (fmt != NULL)
167     {
168         va_list aq;
169
170         va_copy (aq, ap);
171         addlen += 1 + vsnprintf (NULL, 0, fmt, aq);
172         va_end (aq);
173     }
174
175     char *ret = realloc (*sdp, oldlen + addlen);
176     if (ret == NULL)
177         return NULL;
178
179     oldlen += sprintf (ret + oldlen, "a=%s", name);
180     if (fmt != NULL)
181     {
182         ret[oldlen++] = ':';
183         oldlen += vsprintf (ret + oldlen, fmt, ap);
184     }
185
186     strcpy (ret + oldlen, "\r\n");
187     return *sdp = ret;
188 }
189
190
191 char *sdp_AddAttribute (char **sdp, const char *name, const char *fmt, ...)
192 {
193     char *ret;
194     va_list ap;
195
196     va_start (ap, fmt);
197     ret = vsdp_AddAttribute (sdp, name, fmt, ap);
198     va_end (ap);
199
200     return ret;
201 }
202
203
204 char *sdp_AddMedia (char **sdp,
205                     const char *type, const char *protocol, int dport,
206                     unsigned pt, vlc_bool_t bw_indep, unsigned bw,
207                     const char *rtpmap, const char *fmtp)
208 {
209     char *newsdp, *ptr;
210     size_t inlen = strlen (*sdp), outlen = inlen;
211
212     /* Some default values */
213     if (type == NULL)
214         type = "video";
215     if (protocol == NULL)
216         protocol = "RTP/AVP";
217     assert (pt < 128u);
218
219     outlen += snprintf (NULL, 0,
220                         "m=%s %u %s %d\r\n"
221                         "b=TIAS:%u\r\n"
222                         "b=RR:0\r\n",
223                         type, dport, protocol, pt, bw);
224
225     newsdp = realloc (*sdp, outlen + 1);
226     if (newsdp == NULL)
227         return NULL;
228
229     *sdp = newsdp;
230     ptr = newsdp + inlen;
231
232     ptr += sprintf (ptr, "m=%s %u %s %u\r\n",
233                          type, dport, protocol, pt);
234     if (bw > 0)
235         ptr += sprintf (ptr, "b=%s:%u\r\n", bw_indep ? "TIAS" : "AS", bw);
236     ptr += sprintf (ptr, "b=RR:0\r\n");
237
238     /* RTP payload type map */
239     if (rtpmap != NULL)
240         sdp_AddAttribute (sdp, "rtpmap", "%u %s", pt, rtpmap);
241     /* Format parameters */
242     if (fmtp != NULL)
243         sdp_AddAttribute (sdp, "fmtp", "%u %s", pt, fmtp);
244
245     return newsdp;
246 }