]> git.sesse.net Git - vlc/blob - modules/stream_out/announce.c
use "video" and not "audio" as media type, and 33 (mpeg2 ts) and not 14
[vlc] / modules / stream_out / announce.c
1 /*****************************************************************************
2  * announce.c : Session announcement
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  *
6  * Authors: ClĂ©ment Stenac <zorglub@via.ecp.fr>
7  *          Damien Lucas <nitrox@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdio.h>                                              /* sprintf() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/sout.h>
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>
37 #endif
38
39 #ifdef WIN32
40 #   include <winsock2.h>
41 #   include <ws2tcpip.h>
42 #   ifndef IN_MULTICAST
43 #       define IN_MULTICAST(a) IN_CLASSD(a)
44 #   endif
45 #else
46 #   include <sys/socket.h>
47 #endif
48
49 #ifdef HAVE_SLP_H
50 # include <slp.h>
51 #endif
52
53 #include "announce.h"
54 #include "network.h"
55
56 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
57 #define SAP_PORT 9875
58
59 #define SAP_IPV6_ADDR_1 "FF0"
60 #define SAP_IPV6_ADDR_2 "::2:7FFE"
61
62 #define DEFAULT_PORT "1234"
63
64 /****************************************************************************
65  *  Split : split a string into two parts: the one which is before the delim
66  *               and the one which is after.
67  *               NULL is returned if delim is not found
68  ****************************************************************************/
69 static char * split( char *psz_in, char *psz_out1, char *psz_out2, char delim)
70 {
71     unsigned int i_count = 0; /* pos in input string */
72     unsigned int i_pos1  = 0; /* pos in out2 string */
73     unsigned int i_pos2  = 0;
74     char *psz_cur; /* store the pos of the first delim found */
75
76     /* Skip spaces at the beginning */
77     while( psz_in[i_count] == ' ' )
78     {
79         i_count++;
80     }
81
82     if( psz_in[i_count] == '\0' )
83     {
84         return NULL;
85     }
86
87     /* Look for delim */
88     while( psz_in[i_count] && psz_in[i_count] != delim )
89     {
90         psz_out1[i_pos1] = psz_in[i_count];
91         i_count++;
92         i_pos1++;
93     }
94     /* Mark the end of out1 */
95     psz_out1[i_pos1] = '\0';
96
97     if( psz_in[i_count] == '\0' )
98     {
99         return NULL;
100     }
101
102     /* store pos of the first delim */
103     psz_cur = psz_in + i_count;
104
105     /* skip all delim and all spaces */
106     while( psz_in[i_count] == ' ' || psz_in[i_count] == delim )
107     {
108         i_count++;
109     }
110
111     if( psz_in[i_count] == '\0' )
112     {
113         return psz_cur;
114     }
115
116     /* Store the second string */
117     while( psz_in[i_count] )
118     {
119         psz_out2[i_pos2] = psz_in[i_count];
120         i_pos2++;
121         i_count++;
122     }
123     psz_out2[i_pos2] = '\0';
124
125     return psz_cur;
126 }
127
128 /*****************************************************************************
129  * sout_SAPNew: Creates a SAP Session
130  *****************************************************************************/
131 sap_session_t * sout_SAPNew ( sout_instance_t *p_sout,
132                                      char * psz_url_arg,
133                                      char * psz_name_arg,
134                                      int ip_version,
135                                      char * psz_v6_scope )
136 {
137     sap_session_t       *p_sap; /* The SAP structure */
138     module_t            *p_network; /* Network module */
139     network_socket_t    socket_desc; /* Socket descriptor */
140     char                *sap_ipv6_addr = NULL; /* IPv6 built address */
141     char                *psz_eol; /* Used to parse IPv6 URIs */
142     int                 i_port; /* Port in numerical format */
143
144     /* Allocate the SAP structure */
145     p_sap = (sap_session_t *) malloc( sizeof ( sap_session_t ) ) ;
146     if ( !p_sap )
147     {
148         msg_Err( p_sout, "out of memory" );
149         return NULL;
150     }
151
152     /* Fill the information in the structure */
153     if( strstr( psz_url_arg, "[" ) )
154     {
155         /* We have an IPv6 address. Do not use ':' as the port separator */
156         psz_eol = strchr( psz_url_arg, ']' );
157         if( !psz_eol )
158         {
159             msg_Warn( p_sout, "no matching ], unable to parse URI");
160             return NULL;
161         }
162
163         if( !psz_eol++ )
164         {
165             sprintf( p_sap->psz_url, "%s", psz_url_arg );
166             sprintf( p_sap->psz_port, "%s", DEFAULT_PORT );
167         }
168         else
169         {
170             *psz_eol = '\0';
171             sprintf( p_sap->psz_url, "%s", psz_url_arg );
172             psz_eol++;
173             if( psz_eol )
174             {
175                 sprintf( p_sap->psz_port, "%s", psz_eol );
176             }
177         }
178     }
179     else
180     {
181         split( psz_url_arg, p_sap->psz_url, p_sap->psz_port, ':' );
182     }
183
184     /* Check if we have a port */
185     if( !strlen( p_sap->psz_port ) )
186     {
187         sprintf( p_sap->psz_port, "%s", DEFAULT_PORT );
188     }
189
190     /* Make sure our port is valid and atoi it */
191     i_port = atoi( p_sap->psz_port );
192
193     if( !i_port )
194     {
195         sprintf( p_sap->psz_port, "%s", DEFAULT_PORT );
196     }
197     else
198     {
199         sprintf( p_sap->psz_port, "%i", i_port );
200     }
201
202     /* The name that we send */
203     sprintf( p_sap->psz_name, "%s", psz_name_arg );
204
205     p_sap->i_ip_version = ip_version;
206
207     /* Only "6" triggers IPv6. IPv4 is default */
208     if( ip_version != 6 )
209     {
210         msg_Dbg( p_sout, "creating IPv4 SAP socket" );
211
212         /* Fill the socket descriptor */
213         socket_desc.i_type            = NETWORK_UDP;
214         socket_desc.psz_bind_addr     = "";
215         socket_desc.i_bind_port       = 0;
216         socket_desc.psz_server_addr   = SAP_IPV4_ADDR;
217         socket_desc.i_server_port     = SAP_PORT;
218         socket_desc.i_handle          = 0;
219         socket_desc.i_ttl             = 0;
220
221         /* Call the network module */
222         p_sout->p_private = (void*) &socket_desc;
223         if( !( p_network = module_Need( p_sout, "network", "ipv4" ) ) )
224         {
225              msg_Warn( p_sout, "failed to open a connection (udp)" );
226              return NULL;
227         }
228         module_Unneed( p_sout, p_network );
229
230         p_sap->i_socket = socket_desc.i_handle;
231         if( p_sap->i_socket < 0 )
232         {
233             msg_Warn( p_sout, "unable to initialize SAP" );
234             return NULL;
235         }
236     }
237     else
238     {
239         msg_Dbg( p_sout, "creating IPv6 SAP socket with scope %s",
240                          psz_v6_scope );
241
242         /* Initialize and build the IPv6 address to broadcast to */
243         sap_ipv6_addr = (char *) malloc( 28 * sizeof(char) );
244         if ( !sap_ipv6_addr )
245         {
246             msg_Err( p_sout, "out of memory" );
247             return NULL;
248         }
249         sprintf( sap_ipv6_addr, "%s%c%s",
250                  SAP_IPV6_ADDR_1, psz_v6_scope[0], SAP_IPV6_ADDR_2 );
251
252         /* Fill the socket descriptor */
253         socket_desc.i_type          = NETWORK_UDP;
254         socket_desc.psz_bind_addr   = "";
255         socket_desc.i_bind_port     = 0;
256         socket_desc.psz_server_addr = sap_ipv6_addr;
257         socket_desc.i_server_port   = SAP_PORT;
258         socket_desc.i_handle        = 0;
259         socket_desc.i_ttl           = 0;
260
261         /* Call the network module */
262         p_sout->p_private = (void *) &socket_desc;
263         if( !( p_network = module_Need( p_sout, "network", "ipv6" ) ) )
264         {
265             msg_Warn( p_sout, "failed to open a connection (udp)" );
266             return NULL;
267         }
268         module_Unneed( p_sout, p_network );
269
270         p_sap->i_socket = socket_desc.i_handle;
271         if( p_sap->i_socket <= 0 )
272         {
273             msg_Warn( p_sout, "unable to initialize SAP" );
274             return NULL;
275         }
276
277         /* Free what we allocated */
278         if( sap_ipv6_addr )
279         {
280             free( sap_ipv6_addr );
281         }
282     }
283
284     msg_Dbg( p_sout, "SAP initialization complete" );
285
286     return p_sap;
287 }
288
289 /*****************************************************************************
290  * sout_SAPDelete: Deletes a SAP Session
291  *****************************************************************************/
292 void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t * p_sap )
293 {
294     int i_ret;
295
296 #if defined( UNDER_CE )
297     i_ret = CloseHandle( (HANDLE)p_sap->i_socket );
298 #elif defined( WIN32 )
299     i_ret = closesocket( p_sap->i_socket );
300 #else
301     i_ret = close( p_sap->i_socket );
302 #endif
303
304     if( i_ret )
305     {
306         msg_Err( p_sout, "unable to close SAP socket" );
307     }
308
309     free( p_sap );
310 }
311
312 /*****************************************************************************
313  * sout_SAPSend: Sends a SAP packet
314  *****************************************************************************/
315 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_sap )
316 {
317     char psz_msg[1000];                     /* SDP content */
318     char *psz_head;                         /* SAP header */
319     char *psz_send;                         /* What we send */
320     char *psz_type = "application/sdp";
321     int i_header_size;                      /* SAP header size */
322     int i_msg_size;                         /* SDP content size */
323     int i_size;                             /* Total size */
324     int i_ret = 0;
325
326     /* We send a packet every 24 calls to the function */
327     if( p_sap->i_calls++ < 24 )
328     {
329         return;
330     }
331
332     i_header_size = 8 + strlen( psz_type ) + 1;
333     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
334
335     if( ! psz_head )
336     {
337         msg_Err( p_sout, "out of memory" );
338         return;
339     }
340
341     /* Create the SAP headers */
342     psz_head[0] = 0x20; /* Means IPv4, not encrypted, not compressed */
343     psz_head[1] = 0x00; /* No authentification */
344     psz_head[2] = 0x42; /* Version */
345     psz_head[3] = 0x12; /* Version */
346
347     psz_head[4] = 0x01; /* Source IP  FIXME: we should get the real address */
348     psz_head[5] = 0x02; /* idem */
349     psz_head[6] = 0x03; /* idem */
350     psz_head[7] = 0x04; /* idem */
351
352     strncpy( psz_head + 8, psz_type, 15 );
353     psz_head[ i_header_size-1 ] = '\0';
354
355     /* Create the SDP content */
356     /* Do not add spaces at beginning of the lines ! */
357     sprintf( psz_msg, "v=0\n"
358                       "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n"
359                       "s=%s\n"
360                       "u=VideoLAN\n"
361                       "t=0 0\n"
362                       "m=video %s udp 33\n"
363                       "c=IN IP4 %s/15\n"
364                       "a=type:test\n",
365              p_sap->psz_name, p_sap->psz_port, p_sap->psz_url );
366
367     i_msg_size = strlen( psz_msg );
368     i_size = i_msg_size + i_header_size;
369
370     /* Create the message */
371     psz_send = (char *) malloc( i_size*sizeof(char) );
372     if( !psz_send )
373     {
374         msg_Err( p_sout, "out of memory" );
375         return;
376     }
377
378     memcpy( psz_send, psz_head, i_header_size );
379     memcpy( psz_send + i_header_size, psz_msg, i_msg_size );
380
381     if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */
382     {
383         i_ret = send( p_sap->i_socket, psz_send, i_size, 0 );
384     }
385
386     if( i_ret <= 0 )
387     {
388         msg_Warn( p_sout, "SAP send failed on socket %i (%s)",
389                           p_sap->i_socket, strerror(errno) );
390     }
391
392     p_sap->i_calls = 0;
393
394     /* Free what we allocated */
395     free( psz_send );
396     free( psz_head );
397 }
398
399 #ifdef HAVE_SLP_H
400 /*****************************************************************************
401  * sout_SLPBuildName: Builds a service name according to SLP standard
402  *****************************************************************************/
403 static char * sout_SLPBuildName(char *psz_url,char *psz_name)
404 {
405     char *psz_service;
406     unsigned int i_size;
407
408     /* name to build is: service:vlc.services.videolan://$(url) */
409
410     i_size =  8 + 12 + 12 + 5 + strlen(psz_url) + 1;
411
412     psz_service=(char *)malloc(i_size * sizeof(char));
413
414     snprintf( psz_service , i_size,
415               "service:vlc.services.videolan://udp:@%s",
416               psz_url);
417         /* How piggy  ! */
418
419     psz_service[i_size]='\0'; /* Just to make sure */
420
421     return psz_service;
422
423 }
424
425 /*****************************************************************************
426  * sout_SLPReport: Reporting function. Unused at the moment but needed
427  *****************************************************************************/
428 static void sout_SLPReport(SLPHandle slp_handle,SLPError slp_error,void* cookie)
429 {
430 }
431 #endif
432
433 /*****************************************************************************
434  * sout_SLPReg: Registers the program with SLP
435  *****************************************************************************/
436 int sout_SLPReg( sout_instance_t *p_sout, char * psz_url,
437                                char * psz_name)
438 {
439 #ifdef HAVE_SLP_H
440     SLPHandle   slp_handle;
441     SLPError    slp_res;
442     char *psz_service= sout_SLPBuildName(psz_url,psz_name);
443
444     if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
445     {
446         msg_Warn(p_sout,"Unable to initialize SLP");
447         return -1;
448     }
449
450     msg_Info(p_sout , "Registering %s (name: %s) in SLP",
451                       psz_service , psz_name);
452
453     slp_res = SLPReg ( slp_handle,
454             psz_service,
455             SLP_LIFETIME_MAXIMUM,
456             NULL,
457             psz_name,
458             SLP_TRUE,
459             sout_SLPReport,
460             NULL );
461
462     if( slp_res != SLP_OK )
463     {
464         msg_Warn(p_sout,"Error while registering service: %i", slp_res );
465         return -1;
466     }
467
468     return 0;
469
470 #else /* This function should never be called if this is false */
471     return -1;
472 #endif
473 }
474
475
476 /*****************************************************************************
477  * sout_SLDePReg: Unregisters the program from SLP
478  *****************************************************************************/
479 int sout_SLPDereg( sout_instance_t *p_sout, char * psz_url,
480                                char * psz_name)
481 {
482 #ifdef HAVE_SLP_H
483
484     SLPHandle   slp_handle;
485     SLPError    slp_res;
486     char *psz_service= sout_SLPBuildName(psz_url,psz_name);
487
488     if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
489     {
490         msg_Warn(p_sout,"Unable to initialize SLP");
491         return -1;
492     }
493
494     msg_Info(p_sout , "Unregistering %s from SLP",
495                       psz_service);
496
497     slp_res = SLPDereg ( slp_handle,
498             psz_service,
499             sout_SLPReport,
500             NULL );
501
502     if( slp_res != SLP_OK )
503     {
504         msg_Warn(p_sout,"Error while registering service: %i", slp_res );
505         return -1;
506     }
507
508     return 0;
509
510 #else /* This function should never be called if this is false */
511     return -1;
512 #endif
513 }