qt: playlist: use item title if available
[vlc.git] / modules / stream_out / sdp_helper.c
blobcaa646b30bddc68a5a60d54de248260b3a597545
1 /*****************************************************************************
2 * sdp.c : SDP creation helpers
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <string.h>
27 #include <vlc_common.h>
28 #include <vlc_charset.h>
29 #include <vlc_network.h>
30 #include <vlc_memstream.h>
32 #include "sdp_helper.h"
34 #define MAXSDPADDRESS 47
36 static
37 char *AddressToSDP (const struct sockaddr *addr, size_t addrlen, char *buf)
39 if (addrlen < offsetof (struct sockaddr, sa_family)
40 + sizeof (addr->sa_family))
41 return NULL;
43 strcpy (buf, "IN IP* ");
45 if (vlc_getnameinfo (addr, addrlen, buf + 7, MAXSDPADDRESS - 7, NULL,
46 NI_NUMERICHOST))
47 return NULL;
49 switch (addr->sa_family)
51 case AF_INET:
53 if (net_SockAddrIsMulticast (addr, addrlen))
54 strcat (buf, "/255"); // obsolete in RFC4566, dummy value
55 buf[5] = '4';
56 break;
59 #ifdef AF_INET6
60 case AF_INET6:
62 char *ptr = strchr (buf, '%');
63 if (ptr != NULL)
64 *ptr = '\0'; // remove scope ID
65 buf[5] = '6';
66 break;
68 #endif
70 default:
71 return NULL;
74 return buf;
78 static bool IsSDPString (const char *str)
80 if (strchr (str, '\r') != NULL)
81 return false;
82 if (strchr (str, '\n') != NULL)
83 return false;
84 if (!IsUTF8 (str))
85 return false;
86 return true;
89 int vlc_sdp_Start(struct vlc_memstream *restrict stream,
90 vlc_object_t *obj, const char *cfgpref,
91 const struct sockaddr *src, size_t srclen,
92 const struct sockaddr *addr, size_t addrlen)
94 char connection[MAXSDPADDRESS];
95 char *str = NULL;
97 size_t cfglen = strlen(cfgpref);
98 if (cfglen >= 128)
99 return -1;
101 char varname[cfglen + sizeof ("description")];
102 char *subvar = varname + cfglen;
104 strcpy(varname, cfgpref);
106 vlc_memstream_open(stream);
107 vlc_memstream_puts(stream, "v=0\r\n");
109 if (AddressToSDP(addr, addrlen, connection) == NULL)
110 goto error;
112 const uint_fast64_t now = NTPtime64();
113 char hostname[256];
115 gethostname(hostname, sizeof (hostname));
117 vlc_memstream_printf(stream, "o=- %"PRIu64" %"PRIu64" IN IP%c %s\r\n",
118 now, now, connection[5], hostname);
121 strcpy(subvar, "name");
122 str = var_GetNonEmptyString(obj, varname);
123 if (str != NULL)
125 if (!IsSDPString(str))
126 goto error;
128 vlc_memstream_printf(stream, "s=%s\r\n", str);
129 free(str);
131 else
132 vlc_memstream_printf(stream, "s=%s\r\n", "Unnamed");
134 strcpy(subvar, "description");
135 str = var_GetNonEmptyString(obj, varname);
136 if (str != NULL)
138 if (!IsSDPString(str))
139 goto error;
141 vlc_memstream_printf(stream, "i=%s\r\n", str);
142 free(str);
145 // no URL, email, no phone (useless)
147 vlc_memstream_printf(stream, "c=%s\r\n", connection);
148 // bandwidth not specified
149 vlc_memstream_puts(stream, "t=0 0\r\n"); // one dummy time span
150 // no repeating
151 // no time zone adjustment (silly idea anyway)
152 // no encryption key (deprecated)
154 vlc_memstream_printf(stream, "a=tool:%s\r\n", PACKAGE_STRING);
155 vlc_memstream_puts(stream, "a=recvonly\r\n");
156 vlc_memstream_puts(stream, "a=type:broadcast\r\n");
157 vlc_memstream_puts(stream, "a=charset:UTF-8\r\n");
159 if (srclen > 0)
161 char machine[MAXSDPADDRESS];
163 if (AddressToSDP(src, srclen, machine) != NULL)
164 vlc_memstream_printf(stream,
165 "a=source-filter: incl IN IP%c * %s\r\n",
166 machine[5], machine + 7);
169 strcpy(subvar, "cat");
170 str = var_GetNonEmptyString(obj, varname);
171 if (str != NULL)
173 if (IsSDPString(str))
174 goto error;
176 vlc_memstream_printf(stream, "a=cat:%s\r\n", str);
177 vlc_memstream_printf(stream, "a=x-plgroup:%s\r\n", str);
178 free(str);
180 return 0;
181 error:
182 free(str);
183 if (vlc_memstream_close(stream) == 0)
184 free(stream->ptr);
185 return -1;