| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
#ifdef HAVE_CONFIG_H |
|---|
| 30 |
# include "config.h" |
|---|
| 31 |
#endif |
|---|
| 32 |
|
|---|
| 33 |
#include <vlc_common.h> |
|---|
| 34 |
|
|---|
| 35 |
#include <stdlib.h> |
|---|
| 36 |
#include <stdio.h> |
|---|
| 37 |
#include <string.h> |
|---|
| 38 |
#include <ctype.h> |
|---|
| 39 |
#include <assert.h> |
|---|
| 40 |
|
|---|
| 41 |
#include <vlc_sout.h> |
|---|
| 42 |
#include <vlc_network.h> |
|---|
| 43 |
|
|---|
| 44 |
#include "stream_output.h" |
|---|
| 45 |
#include "libvlc.h" |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
#define IPPORT_SAP 9875 |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
typedef struct sap_session_t |
|---|
| 52 |
{ |
|---|
| 53 |
struct sap_session_t *next; |
|---|
| 54 |
const session_descriptor_t *p_sd; |
|---|
| 55 |
size_t length; |
|---|
| 56 |
uint8_t data[]; |
|---|
| 57 |
} sap_session_t; |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
typedef struct sap_address_t |
|---|
| 62 |
{ |
|---|
| 63 |
struct sap_address_t *next; |
|---|
| 64 |
|
|---|
| 65 |
vlc_thread_t thread; |
|---|
| 66 |
vlc_mutex_t lock; |
|---|
| 67 |
vlc_cond_t wait; |
|---|
| 68 |
|
|---|
| 69 |
char group[NI_MAXNUMERICHOST]; |
|---|
| 70 |
struct sockaddr_storage orig; |
|---|
| 71 |
socklen_t origlen; |
|---|
| 72 |
int fd; |
|---|
| 73 |
unsigned interval; |
|---|
| 74 |
|
|---|
| 75 |
unsigned session_count; |
|---|
| 76 |
sap_session_t *first; |
|---|
| 77 |
} sap_address_t; |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
struct sap_handler_t |
|---|
| 81 |
{ |
|---|
| 82 |
VLC_COMMON_MEMBERS |
|---|
| 83 |
|
|---|
| 84 |
vlc_mutex_t lock; |
|---|
| 85 |
sap_address_t *first; |
|---|
| 86 |
}; |
|---|
| 87 |
|
|---|
| 88 |
#define SAP_MAX_BUFFER 65534 |
|---|
| 89 |
#define MIN_INTERVAL 2 |
|---|
| 90 |
#define MAX_INTERVAL 300 |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
static void *RunThread (void *); |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
sap_handler_t *SAP_Create (vlc_object_t *p_announce) |
|---|
| 104 |
{ |
|---|
| 105 |
sap_handler_t *p_sap; |
|---|
| 106 |
|
|---|
| 107 |
p_sap = vlc_custom_create (p_announce, sizeof (*p_sap), |
|---|
| 108 |
VLC_OBJECT_GENERIC, "sap sender"); |
|---|
| 109 |
if (p_sap == NULL) |
|---|
| 110 |
return NULL; |
|---|
| 111 |
|
|---|
| 112 |
vlc_mutex_init (&p_sap->lock); |
|---|
| 113 |
p_sap->first = NULL; |
|---|
| 114 |
return p_sap; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
void SAP_Destroy (sap_handler_t *p_sap) |
|---|
| 118 |
{ |
|---|
| 119 |
assert (p_sap->first == NULL); |
|---|
| 120 |
vlc_mutex_destroy (&p_sap->lock); |
|---|
| 121 |
vlc_object_release (p_sap); |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
static sap_address_t *AddressCreate (vlc_object_t *obj, const char *group) |
|---|
| 125 |
{ |
|---|
| 126 |
int fd = net_ConnectUDP (obj, group, IPPORT_SAP, 255); |
|---|
| 127 |
if (fd == -1) |
|---|
| 128 |
return NULL; |
|---|
| 129 |
|
|---|
| 130 |
sap_address_t *addr = malloc (sizeof (*addr)); |
|---|
| 131 |
if (addr == NULL) |
|---|
| 132 |
{ |
|---|
| 133 |
net_Close (fd); |
|---|
| 134 |
return NULL; |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
strlcpy (addr->group, group, sizeof (addr->group)); |
|---|
| 138 |
addr->fd = fd; |
|---|
| 139 |
addr->origlen = sizeof (addr->orig); |
|---|
| 140 |
getsockname (fd, (struct sockaddr *)&addr->orig, &addr->origlen); |
|---|
| 141 |
|
|---|
| 142 |
addr->interval = var_CreateGetInteger (obj, "sap-interval"); |
|---|
| 143 |
vlc_mutex_init (&addr->lock); |
|---|
| 144 |
vlc_cond_init (&addr->wait); |
|---|
| 145 |
addr->session_count = 0; |
|---|
| 146 |
addr->first = NULL; |
|---|
| 147 |
|
|---|
| 148 |
if (vlc_clone (&addr->thread, RunThread, addr, VLC_THREAD_PRIORITY_LOW)) |
|---|
| 149 |
{ |
|---|
| 150 |
msg_Err (obj, "unable to spawn SAP announce thread"); |
|---|
| 151 |
net_Close (fd); |
|---|
| 152 |
free (addr); |
|---|
| 153 |
return NULL; |
|---|
| 154 |
} |
|---|
| 155 |
return addr; |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
static void AddressDestroy (sap_address_t *addr) |
|---|
| 159 |
{ |
|---|
| 160 |
assert (addr->first == NULL); |
|---|
| 161 |
|
|---|
| 162 |
vlc_cancel (addr->thread); |
|---|
| 163 |
vlc_join (addr->thread, NULL); |
|---|
| 164 |
vlc_cond_destroy (&addr->wait); |
|---|
| 165 |
vlc_mutex_destroy (&addr->lock); |
|---|
| 166 |
net_Close (addr->fd); |
|---|
| 167 |
free (addr); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
static void *RunThread (void *self) |
|---|
| 176 |
{ |
|---|
| 177 |
sap_address_t *addr = self; |
|---|
| 178 |
|
|---|
| 179 |
vlc_mutex_lock (&addr->lock); |
|---|
| 180 |
mutex_cleanup_push (&addr->lock); |
|---|
| 181 |
|
|---|
| 182 |
for (;;) |
|---|
| 183 |
{ |
|---|
| 184 |
sap_session_t *p_session; |
|---|
| 185 |
mtime_t deadline; |
|---|
| 186 |
|
|---|
| 187 |
while (addr->first == NULL) |
|---|
| 188 |
vlc_cond_wait (&addr->wait, &addr->lock); |
|---|
| 189 |
|
|---|
| 190 |
assert (addr->session_count > 0); |
|---|
| 191 |
|
|---|
| 192 |
deadline = mdate (); |
|---|
| 193 |
for (p_session = addr->first; p_session; p_session = p_session->next) |
|---|
| 194 |
{ |
|---|
| 195 |
send (addr->fd, p_session->data, p_session->length, 0); |
|---|
| 196 |
deadline += addr->interval * CLOCK_FREQ / addr->session_count; |
|---|
| 197 |
|
|---|
| 198 |
if (vlc_cond_timedwait (&addr->wait, &addr->lock, deadline) == 0) |
|---|
| 199 |
break; |
|---|
| 200 |
} |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|
| 203 |
vlc_cleanup_pop (); |
|---|
| 204 |
assert (0); |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
int SAP_Add (sap_handler_t *p_sap, session_descriptor_t *p_session) |
|---|
| 211 |
{ |
|---|
| 212 |
int i; |
|---|
| 213 |
char psz_addr[NI_MAXNUMERICHOST]; |
|---|
| 214 |
bool b_ipv6 = false, b_ssm = false; |
|---|
| 215 |
sap_session_t *p_sap_session; |
|---|
| 216 |
mtime_t i_hash; |
|---|
| 217 |
struct sockaddr_storage addr; |
|---|
| 218 |
socklen_t addrlen; |
|---|
| 219 |
|
|---|
| 220 |
addrlen = p_session->addrlen; |
|---|
| 221 |
if ((addrlen == 0) || (addrlen > sizeof (addr))) |
|---|
| 222 |
{ |
|---|
| 223 |
msg_Err( p_sap, "No/invalid address specified for SAP announce" ); |
|---|
| 224 |
return VLC_EGENERIC; |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
memcpy (&addr, &p_session->addr, addrlen); |
|---|
| 229 |
|
|---|
| 230 |
switch( p_session->addr.ss_family ) |
|---|
| 231 |
{ |
|---|
| 232 |
#if defined (HAVE_INET_PTON) || defined (WIN32) |
|---|
| 233 |
case AF_INET6: |
|---|
| 234 |
{ |
|---|
| 235 |
|
|---|
| 236 |
struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr; |
|---|
| 237 |
|
|---|
| 238 |
memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00" |
|---|
| 239 |
"\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 ); |
|---|
| 240 |
if( IN6_IS_ADDR_MULTICAST( a6 ) ) |
|---|
| 241 |
{ |
|---|
| 242 |
|
|---|
| 243 |
b_ssm = (U32_AT (a6->s6_addr) & 0xfff0ffff) == 0xff300000; |
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
a6->s6_addr[1] &= 0xf; |
|---|
| 247 |
} |
|---|
| 248 |
else |
|---|
| 249 |
|
|---|
| 250 |
memcpy( a6->s6_addr, "\xff\x0e", 2 ); |
|---|
| 251 |
|
|---|
| 252 |
b_ipv6 = true; |
|---|
| 253 |
break; |
|---|
| 254 |
} |
|---|
| 255 |
#endif |
|---|
| 256 |
|
|---|
| 257 |
case AF_INET: |
|---|
| 258 |
{ |
|---|
| 259 |
|
|---|
| 260 |
uint32_t ipv4; |
|---|
| 261 |
|
|---|
| 262 |
ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr ); |
|---|
| 263 |
|
|---|
| 264 |
if ((ipv4 & 0xffffff00) == 0xe0000000) |
|---|
| 265 |
ipv4 = 0xe00000ff; |
|---|
| 266 |
else |
|---|
| 267 |
|
|---|
| 268 |
if ((ipv4 & 0xffff0000) == 0xefff0000) |
|---|
| 269 |
ipv4 = 0xefffffff; |
|---|
| 270 |
else |
|---|
| 271 |
|
|---|
| 272 |
if ((ipv4 & 0xfffc0000) == 0xefc00000) |
|---|
| 273 |
ipv4 = 0xefc3ffff; |
|---|
| 274 |
else |
|---|
| 275 |
if ((ipv4 & 0xff000000) == 0xef000000) |
|---|
| 276 |
ipv4 = 0; |
|---|
| 277 |
else |
|---|
| 278 |
|
|---|
| 279 |
{ |
|---|
| 280 |
|
|---|
| 281 |
b_ssm = (ipv4 >> 24) == 232; |
|---|
| 282 |
ipv4 = 0xe0027ffe; |
|---|
| 283 |
} |
|---|
| 284 |
|
|---|
| 285 |
if( ipv4 == 0 ) |
|---|
| 286 |
{ |
|---|
| 287 |
msg_Err( p_sap, "Out-of-scope multicast address " |
|---|
| 288 |
"not supported by SAP" ); |
|---|
| 289 |
return VLC_EGENERIC; |
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 ); |
|---|
| 293 |
break; |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
default: |
|---|
| 297 |
msg_Err( p_sap, "Address family %d not supported by SAP", |
|---|
| 298 |
addr.ss_family ); |
|---|
| 299 |
return VLC_EGENERIC; |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
i = vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, |
|---|
| 303 |
psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST ); |
|---|
| 304 |
|
|---|
| 305 |
if( i ) |
|---|
| 306 |
{ |
|---|
| 307 |
msg_Err( p_sap, "%s", vlc_gai_strerror( i ) ); |
|---|
| 308 |
return VLC_EGENERIC; |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
msg_Dbg( p_sap, "using SAP address: %s", psz_addr); |
|---|
| 313 |
|
|---|
| 314 |
vlc_mutex_lock (&p_sap->lock); |
|---|
| 315 |
sap_address_t *sap_addr; |
|---|
| 316 |
for (sap_addr = p_sap->first; sap_addr; sap_addr = sap_addr->next) |
|---|
| 317 |
if (!strcmp (psz_addr, sap_addr->group)) |
|---|
| 318 |
break; |
|---|
| 319 |
|
|---|
| 320 |
if (sap_addr == NULL) |
|---|
| 321 |
{ |
|---|
| 322 |
sap_addr = AddressCreate (VLC_OBJECT(p_sap), psz_addr); |
|---|
| 323 |
if (sap_addr == NULL) |
|---|
| 324 |
{ |
|---|
| 325 |
vlc_mutex_unlock (&p_sap->lock); |
|---|
| 326 |
return VLC_EGENERIC; |
|---|
| 327 |
} |
|---|
| 328 |
sap_addr->next = p_sap->first; |
|---|
| 329 |
p_sap->first = sap_addr; |
|---|
| 330 |
} |
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
vlc_mutex_lock (&sap_addr->lock); |
|---|
| 334 |
vlc_mutex_unlock (&p_sap->lock); |
|---|
| 335 |
|
|---|
| 336 |
memcpy (&p_session->orig, &sap_addr->orig, sap_addr->origlen); |
|---|
| 337 |
p_session->origlen = sap_addr->origlen; |
|---|
| 338 |
|
|---|
| 339 |
size_t headsize = 20, length; |
|---|
| 340 |
switch (p_session->orig.ss_family) |
|---|
| 341 |
{ |
|---|
| 342 |
#ifdef AF_INET6 |
|---|
| 343 |
case AF_INET6: |
|---|
| 344 |
headsize += 16; |
|---|
| 345 |
break; |
|---|
| 346 |
#endif |
|---|
| 347 |
case AF_INET: |
|---|
| 348 |
headsize += 4; |
|---|
| 349 |
break; |
|---|
| 350 |
default: |
|---|
| 351 |
assert (0); |
|---|
| 352 |
} |
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
length = headsize + strlen (p_session->psz_sdp); |
|---|
| 356 |
p_sap_session = malloc (sizeof (*p_sap_session) + length + 1); |
|---|
| 357 |
if (p_sap_session == NULL) |
|---|
| 358 |
{ |
|---|
| 359 |
vlc_mutex_unlock (&sap_addr->lock); |
|---|
| 360 |
return VLC_EGENERIC; |
|---|
| 361 |
} |
|---|
| 362 |
p_sap_session->next = sap_addr->first; |
|---|
| 363 |
sap_addr->first = p_sap_session; |
|---|
| 364 |
p_sap_session->p_sd = p_session; |
|---|
| 365 |
p_sap_session->length = length; |
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
uint8_t *psz_head = p_sap_session->data; |
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
psz_head[0] = 0x20; |
|---|
| 372 |
psz_head[1] = 0x00; |
|---|
| 373 |
|
|---|
| 374 |
i_hash = mdate(); |
|---|
| 375 |
psz_head[2] = i_hash >> 8; |
|---|
| 376 |
psz_head[3] = i_hash; |
|---|
| 377 |
|
|---|
| 378 |
headsize = 4; |
|---|
| 379 |
switch (p_session->orig.ss_family) |
|---|
| 380 |
{ |
|---|
| 381 |
#ifdef AF_INET6 |
|---|
| 382 |
case AF_INET6: |
|---|
| 383 |
{ |
|---|
| 384 |
struct in6_addr *a6 = |
|---|
| 385 |
&((struct sockaddr_in6 *)&p_session->orig)->sin6_addr; |
|---|
| 386 |
memcpy (psz_head + headsize, a6, 16); |
|---|
| 387 |
psz_head[0] |= 0x10; |
|---|
| 388 |
headsize += 16; |
|---|
| 389 |
break; |
|---|
| 390 |
} |
|---|
| 391 |
#endif |
|---|
| 392 |
case AF_INET: |
|---|
| 393 |
{ |
|---|
| 394 |
uint32_t ipv4 = |
|---|
| 395 |
(((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr); |
|---|
| 396 |
memcpy (psz_head + headsize, &ipv4, 4); |
|---|
| 397 |
headsize += 4; |
|---|
| 398 |
break; |
|---|
| 399 |
} |
|---|
| 400 |
|
|---|
| 401 |
} |
|---|
| 402 |
|
|---|
| 403 |
memcpy (psz_head + headsize, "application/sdp", 16); |
|---|
| 404 |
headsize += 16; |
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
strcpy( (char *)psz_head + headsize, p_session->psz_sdp); |
|---|
| 408 |
|
|---|
| 409 |
sap_addr->session_count++; |
|---|
| 410 |
vlc_cond_signal (&sap_addr->wait); |
|---|
| 411 |
vlc_mutex_unlock (&sap_addr->lock); |
|---|
| 412 |
return VLC_SUCCESS; |
|---|
| 413 |
} |
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
void SAP_Del (sap_handler_t *p_sap, const session_descriptor_t *p_session) |
|---|
| 419 |
{ |
|---|
| 420 |
vlc_mutex_lock (&p_sap->lock); |
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
sap_address_t *addr, **paddr; |
|---|
| 424 |
sap_session_t *session, **psession; |
|---|
| 425 |
|
|---|
| 426 |
paddr = &p_sap->first; |
|---|
| 427 |
for (addr = p_sap->first; addr; addr = addr->next) |
|---|
| 428 |
{ |
|---|
| 429 |
psession = &addr->first; |
|---|
| 430 |
vlc_mutex_lock (&addr->lock); |
|---|
| 431 |
for (session = addr->first; session; session = session->next) |
|---|
| 432 |
{ |
|---|
| 433 |
if (session->p_sd == p_session) |
|---|
| 434 |
goto found; |
|---|
| 435 |
psession = &session->next; |
|---|
| 436 |
} |
|---|
| 437 |
vlc_mutex_unlock (&addr->lock); |
|---|
| 438 |
paddr = &addr->next; |
|---|
| 439 |
} |
|---|
| 440 |
assert (0); |
|---|
| 441 |
|
|---|
| 442 |
found: |
|---|
| 443 |
*psession = session->next; |
|---|
| 444 |
|
|---|
| 445 |
if (addr->first == NULL) |
|---|
| 446 |
|
|---|
| 447 |
*paddr = addr->next; |
|---|
| 448 |
vlc_mutex_unlock (&p_sap->lock); |
|---|
| 449 |
|
|---|
| 450 |
if (addr->first == NULL) |
|---|
| 451 |
{ |
|---|
| 452 |
|
|---|
| 453 |
vlc_mutex_unlock (&addr->lock); |
|---|
| 454 |
AddressDestroy (addr); |
|---|
| 455 |
} |
|---|
| 456 |
else |
|---|
| 457 |
{ |
|---|
| 458 |
addr->session_count--; |
|---|
| 459 |
vlc_cond_signal (&addr->wait); |
|---|
| 460 |
vlc_mutex_unlock (&addr->lock); |
|---|
| 461 |
} |
|---|
| 462 |
|
|---|
| 463 |
free (session); |
|---|
| 464 |
} |
|---|