]> git.sesse.net Git - vlc/blob - src/stream_output/sdp.c
Provide va_list variant to MakeSDPMedia
[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 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 *StartSDP (const char *name, const char *description, const char *url,
90                 const char *email, const char *phone, vlc_bool_t ssm,
91                 const struct sockaddr *orig, socklen_t origlen,
92                 const struct sockaddr *addr, socklen_t addrlen)
93 {
94     uint64_t t = NTPtime64 ();
95     char *sdp, machine[MAXSDPADDRESS], conn[MAXSDPADDRESS],
96          sfilter[MAXSDPADDRESS + sizeof ("\r\na=source-filter: incl * ")];
97     const char *preurl = "\r\nu=", *premail = "\r\ne=", *prephone = "\r\np=";
98
99     if (name == NULL)
100         name = "Unnamed";
101     if (description == NULL)
102         description = "N/A";
103     if (url == NULL)
104         preurl = url = "";
105     if (email == NULL)
106         premail = email = "";
107     if (phone == NULL)
108         prephone = phone = "";
109
110     if (!IsSDPString (name) || !IsSDPString (description)
111      || !IsSDPString (url) || !IsSDPString (email) || !IsSDPString (phone)
112      || (AddressToSDP (orig, origlen, machine) == NULL)
113      || (AddressToSDP (addr, addrlen, conn) == NULL))
114         return NULL;
115
116     if (ssm)
117         sprintf (sfilter, "\r\na=source-filter: incl IN IP%c * %s",
118                  machine[5], machine + 7);
119     else
120         *sfilter = '\0';
121
122     if (asprintf (&sdp, "v=0"
123                     "\r\no=- "I64Fu" "I64Fu" %s"
124                     "\r\ns=%s"
125                     "\r\ni=%s"
126                     "%s%s" // optional URL
127                     "%s%s" // optional email
128                     "%s%s" // optional phone number
129                     "\r\nc=%s"
130                         // bandwidth not specified
131                     "\r\nt=0 0" // one dummy time span
132                         // no repeating
133                         // no time zone adjustment (silly idea anyway)
134                         // no encryption key (deprecated)
135                     "\r\na=tool:"PACKAGE_STRING
136                     "\r\na=recvonly"
137                     "\r\na=type:broadcast"
138                     "\r\na=charset:UTF-8"
139                     "%s" // optional source filter
140                     "\r\n",
141                /* o= */ t, t, machine,
142                /* s= */ name,
143                /* i= */ description,
144                /* u= */ preurl, url,
145                /* e= */ premail, email,
146                /* p= */ prephone, phone,
147                /* c= */ conn,
148     /* source-filter */ sfilter) == -1)
149         return NULL;
150     return sdp;
151 }
152
153
154 char *vMakeSDPMedia (const char *type, int dport, const char *protocol,
155                     unsigned pt, const char *rtpmap,
156                     const char *fmtpfmt, va_list ap)
157 {
158     char *sdp_media = NULL;
159
160     /* Some default values */
161     if (type == NULL)
162         type = "video";
163     if (dport == 0)
164         dport = 9;
165     if (protocol == NULL)
166         protocol = "RTP/AVP";
167     assert (pt < 128u);
168
169     /* RTP payload type map */
170     char sdp_rtpmap[rtpmap ? (sizeof ("a=rtpmap:123 *\r\n") + strlen (rtpmap)) : 1];
171     if (rtpmap != NULL)
172         sprintf (sdp_rtpmap, "a=rtpmap:%u %s\r\n", pt, rtpmap);
173     else
174         *sdp_rtpmap = '\0';
175
176     /* Format parameters */
177     char *fmtp = NULL;
178     if ((fmtpfmt != NULL)
179      && (vasprintf (&fmtp, fmtpfmt, ap) == -1))
180         return NULL;
181
182     char sdp_fmtp[fmtp ? (sizeof ("a=fmtp:123 *\r\n") + strlen (fmtp)) : 1];
183     if (fmtp != NULL)
184     {
185         sprintf (sdp_fmtp, "a=fmtp:%u %s\r\n", pt, fmtp);
186         free (fmtp);
187     }
188     else
189         *sdp_fmtp = '\0';
190
191     if (asprintf (&sdp_media, "m=%s %u %s %d\r\n" "%s" "%s",
192                   type, dport, protocol, pt,
193                   sdp_rtpmap, sdp_fmtp) == -1)
194         return NULL;
195
196     return sdp_media;
197 }