]> git.sesse.net Git - vlc/blob - src/stream_output/sdp.c
Add support for session description
[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  * Authors: Rémi Denis-Courmont
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25
26 #include <string.h>
27 #include <vlc_network.h>
28 #include <vlc_charset.h>
29
30 #include "stream_output.h"
31
32 #define MAXSDPADDRESS 47
33
34 static
35 char *AddressToSDP (const struct sockaddr *addr, socklen_t addrlen, char *buf)
36 {
37     const char *ttl = NULL;
38     strcpy (buf, "IN IP* ");
39
40     switch (addr->sa_family)
41     {
42         case AF_INET:
43         {
44             if (net_SockAddrIsMulticast (addr, addrlen))
45                 ttl = "/255"; // obsolete in RFC4566, dummy value
46             buf[5] = '4';
47             break;
48         }
49
50 #ifdef AF_INET6
51         case AF_INET6:
52             buf[5] = '6';
53             break;
54 #endif
55
56         default:
57             return NULL;
58     }
59
60     if (vlc_getnameinfo (addr, addrlen, buf + 4, MAXSDPADDRESS - 4, NULL,
61                          NI_NUMERICHOST))
62         return NULL;
63
64     if (ttl != NULL)
65         strcat (buf, ttl);
66
67     return buf;
68 }
69
70
71 static vlc_bool_t IsSDPString (const char *str)
72 {
73     if (strchr (str, '\r') != NULL)
74         return VLC_FALSE;
75     if (strchr (str, '\n') != NULL)
76         return VLC_FALSE;
77     if (!IsUTF8 (str))
78         return VLC_FALSE;
79     return VLC_TRUE;
80 }
81
82
83 char *StartSDP (const char *name, const char *description, const char *url,
84                 const char *email, const char *phone,
85                 const struct sockaddr *orig, socklen_t origlen,
86                 const struct sockaddr *addr, socklen_t addrlen)
87 {
88     uint64_t t = NTPtime64 ();
89     char *sdp, machine[MAXSDPADDRESS], conn[MAXSDPADDRESS];
90     const char *preurl = "\r\nu=", *premail = "\r\ne=", *prephone = "\r\np=";
91
92     if (name == NULL)
93         name = "Unnamed";
94     if (description == NULL)
95         description = "N/A";
96     if (url == NULL)
97         preurl = url = "";
98     if (email == NULL)
99         premail = email = "";
100     if (phone == NULL)
101         prephone = phone = "";
102
103     if (!IsSDPString (name) || !IsSDPString (description)
104      || !IsSDPString (url) || !IsSDPString (email) || !IsSDPString (phone)
105      || (AddressToSDP ((struct sockaddr *)&orig, origlen, machine) == NULL)
106      || (AddressToSDP ((struct sockaddr *)&addr, addrlen, conn) == NULL))
107         return NULL;
108
109     if (asprintf (&sdp, "v=0"
110                     "\r\no=- "I64Fu" "I64Fu" %s"
111                     "\r\ns=%s"
112                     "\r\ni=%s"
113                     "%s%s" // optional URL
114                     "%s%s" // optional email
115                     "%s%s" // optional phone number
116                     "\r\nc=%s"
117                         // bandwidth not specified
118                     "\r\nt= 0 0" // one dummy time span
119                         // no repeating
120                         // no time zone adjustment (silly idea anyway)
121                         // no encryption key (deprecated)
122                     "\r\na=tool:"PACKAGE_STRING
123                     "\r\na=recvonly"
124                     "\r\na=type:broadcast"
125                     "\r\na=charset:UTF-8"
126                     "\r\n",
127                /* o= */ t, t, machine,
128                /* s= */ name,
129                /* i= */ description,
130                /* u= */ preurl, url,
131                /* e= */ premail, email,
132                /* p= */ prephone, phone,
133                /* c= */ conn) == -1)
134         return NULL;
135     return sdp;
136 }
137