]> git.sesse.net Git - vlc/blob - src/stream_output/sdp.c
Try to improve and export the SDP formatting helpers
[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     sprintf (ret + oldlen, fmt, ap);
173     return *sdp = ret;
174 }
175
176
177 char *sdp_AddAttribute (char **sdp, const char *name, const char *fmt, ...)
178 {
179     char *ret;
180
181     if (fmt != NULL)
182     {
183         va_list ap;
184
185         va_start (ap, fmt);
186         ret = vsdp_AddAttribute (sdp, name, fmt, ap);
187         va_end (ap);
188     }
189     else
190     {
191         size_t oldlen = strlen (*sdp);
192         ret = realloc (*sdp, oldlen + strlen (name) + sizeof ("a=\r\n"));
193         if (ret == NULL)
194             return NULL;
195
196         sprintf (ret + oldlen, "a=%s\r\n", name);
197     }
198     return ret;
199 }
200
201
202 char *sdp_AddMedia (char **sdp,
203                     const char *type, const char *protocol, int dport,
204                     unsigned pt, vlc_bool_t bw_indep, unsigned bw,
205                     const char *rtpmap, const char *fmtp)
206 {
207     char *newsdp, *ptr;
208     size_t inlen = strlen (*sdp), outlen = inlen;
209
210     /* Some default values */
211     if (type == NULL)
212         type = "video";
213     if (protocol == NULL)
214         protocol = "RTP/AVP";
215     assert (pt < 128u);
216
217     outlen += snprintf (NULL, 0,
218                         "m=%s %u %s %d\r\n"
219                         "b=RR:0\r\n",
220                         type, dport, protocol, pt);
221
222     newsdp = realloc (*sdp, outlen + 1);
223     if (newsdp == NULL)
224         return NULL;
225
226     *sdp = newsdp;
227     ptr = newsdp + inlen;
228
229     ptr += sprintf (ptr, "m=%s %u %s %d\r\n"
230                          "b=RR:0\r\n",
231                          type, dport, protocol, pt);
232
233     /* RTP payload type map */
234     if (rtpmap != NULL)
235         sdp_AddAttribute ("rtpmap", "%u %s", pt, rtpmap);
236     /* Format parameters */
237     if (fmtp != NULL)
238         sdp_AddAttribute ("fmtp", "%u %s", pt, fmtp);
239
240     return newsdp;
241 }