1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Jon Lech Johansen <jon@nanocrew.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #include <vlc_block.h>
38 #include <vlc_input.h>
39 #include <vlc_playlist.h>
41 #ifdef HAVE_AVAHI_CLIENT
45 #define DIRECTORY_SEPARATOR '\\'
47 #define DIRECTORY_SEPARATOR '/'
51 #include "vlc_httpd.h"
53 #define DEFAULT_PORT 8080
54 #define DEFAULT_SSL_PORT 8443
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
62 #define SOUT_CFG_PREFIX "sout-http-"
64 #define USER_TEXT N_("Username")
65 #define USER_LONGTEXT N_("User name that will be " \
66 "requested to access the stream." )
67 #define PASS_TEXT N_("Password")
68 #define PASS_LONGTEXT N_("Password that will be " \
69 "requested to access the stream." )
70 #define MIME_TEXT N_("Mime")
71 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
72 "if not specified)." )
73 #define CERT_TEXT N_( "Certificate file" )
74 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
75 "be used for HTTPS." )
76 #define KEY_TEXT N_( "Private key file" )
77 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
78 "be used for HTTPS. Leave " \
79 "empty if you don't have one." )
80 #define CA_TEXT N_( "Root CA file" )
81 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
82 "(certificate authority) file that will be used for " \
83 "HTTPS. Leave empty if you " \
85 #define CRL_TEXT N_( "CRL file" )
86 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
87 "file that will be used for SSL. Leave " \
88 "empty if you don't have one." )
89 #define BONJOUR_TEXT N_( "Advertise with Bonjour")
90 #define BONJOUR_LONGTEXT N_( "Advertise the stream with the Bonjour protocol." )
94 set_description( _("HTTP stream output") );
95 set_capability( "sout access", 0 );
96 set_shortname( "HTTP" );
97 add_shortcut( "http" );
98 add_shortcut( "https" );
99 add_shortcut( "mmsh" );
100 set_category( CAT_SOUT );
101 set_subcategory( SUBCAT_SOUT_ACO );
102 add_string( SOUT_CFG_PREFIX "user", "", NULL,
103 USER_TEXT, USER_LONGTEXT, VLC_TRUE );
104 add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
105 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
106 add_string( SOUT_CFG_PREFIX "mime", "", NULL,
107 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
108 add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
109 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
110 add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
111 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
112 add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
113 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
114 add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
115 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
116 add_bool( SOUT_CFG_PREFIX "bonjour", VLC_FALSE, NULL,
117 BONJOUR_TEXT, BONJOUR_LONGTEXT, VLC_TRUE);
118 set_callbacks( Open, Close );
122 /*****************************************************************************
123 * Exported prototypes
124 *****************************************************************************/
125 static const char *ppsz_sout_options[] = {
126 "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
129 static ssize_t Write( sout_access_out_t *, block_t * );
130 static int Seek ( sout_access_out_t *, off_t );
132 struct sout_access_out_sys_t
135 httpd_host_t *p_httpd_host;
138 httpd_stream_t *p_httpd_stream;
140 /* gather header from stream */
141 int i_header_allocated;
144 vlc_bool_t b_header_complete;
146 #ifdef HAVE_AVAHI_CLIENT
151 /*****************************************************************************
152 * Open: open the file
153 *****************************************************************************/
154 static int Open( vlc_object_t *p_this )
156 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
157 sout_access_out_sys_t *p_sys;
164 char *psz_user = NULL;
165 char *psz_pwd = NULL;
166 char *psz_mime = NULL;
167 const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
171 if( !( p_sys = p_access->p_sys =
172 malloc( sizeof( sout_access_out_sys_t ) ) ) )
174 msg_Err( p_access, "Not enough memory" );
178 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
180 /* p_access->psz_path = "hostname:port/filename" */
181 psz_bind_addr = psz_parser = strdup( p_access->psz_path );
184 psz_file_name = NULL;
186 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
190 if( *psz_parser == ':' )
194 i_bind_port = atoi( psz_parser );
196 while( *psz_parser && *psz_parser != '/' )
201 if( *psz_parser == '/' )
205 psz_file_name = psz_parser;
208 if( psz_file_name == NULL )
210 psz_file_name = strdup( "/" );
212 else if( *psz_file_name != '/' )
214 char *p = psz_file_name;
216 psz_file_name = malloc( strlen( p ) + 2 );
217 strcpy( psz_file_name, "/" );
218 strcat( psz_file_name, p );
222 psz_file_name = strdup( psz_file_name );
226 if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
228 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
229 psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
230 psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
231 psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
233 if( i_bind_port <= 0 )
234 i_bind_port = DEFAULT_SSL_PORT;
238 if( i_bind_port <= 0 )
239 i_bind_port = DEFAULT_PORT;
242 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
243 psz_bind_addr, i_bind_port,
244 psz_cert, psz_key, psz_ca,
246 if( p_sys->p_httpd_host == NULL )
248 msg_Err( p_access, "cannot listen on %s:%d",
249 psz_bind_addr, i_bind_port );
250 free( psz_file_name );
251 free( psz_bind_addr );
255 free( psz_bind_addr );
257 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
259 psz_mime = strdup( "video/x-ms-asf-stream" );
263 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
264 if( *val.psz_string )
265 psz_mime = val.psz_string;
267 free( val.psz_string );
270 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
271 if( *val.psz_string )
272 psz_user = val.psz_string;
274 free( val.psz_string );
276 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
277 if( *val.psz_string )
278 psz_pwd = val.psz_string;
280 free( val.psz_string );
282 p_sys->p_httpd_stream =
283 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
284 psz_user, psz_pwd, NULL );
289 if( p_sys->p_httpd_stream == NULL )
291 msg_Err( p_access, "cannot add stream %s", psz_file_name );
292 httpd_HostDelete( p_sys->p_httpd_host );
294 free( psz_file_name );
299 #ifdef HAVE_AVAHI_CLIENT
300 if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
302 char *psz_txt, *psz_name;
303 playlist_t *p_playlist = pl_Yield( p_access );
305 char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
306 char *psz_newuri = psz_uri;
307 psz_name = strrchr( psz_newuri, DIRECTORY_SEPARATOR );
308 if( psz_name != NULL ) psz_name++;
309 else psz_name = psz_newuri;
312 asprintf( &psz_txt, "path=%s", psz_file_name ) == -1 )
314 pl_Release( p_playlist );
319 p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
320 strcmp( p_access->psz_access, "https" )
321 ? "_vlc-http._tcp" : "_vlc-https._tcp",
322 psz_name, i_bind_port, psz_txt );
324 free( (void *)psz_txt );
326 if( p_sys->p_bonjour == NULL )
327 msg_Err( p_access, "unable to start requested Bonjour announce" );
328 pl_Release( p_playlist );
331 p_sys->p_bonjour = NULL;
334 free( psz_file_name );
336 p_sys->i_header_allocated = 1024;
337 p_sys->i_header_size = 0;
338 p_sys->p_header = malloc( p_sys->i_header_allocated );
339 p_sys->b_header_complete = VLC_FALSE;
341 p_access->pf_write = Write;
342 p_access->pf_seek = Seek;
345 /* update p_sout->i_out_pace_nocontrol */
346 p_access->p_sout->i_out_pace_nocontrol++;
351 /*****************************************************************************
352 * Close: close the target
353 *****************************************************************************/
354 static void Close( vlc_object_t * p_this )
356 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
357 sout_access_out_sys_t *p_sys = p_access->p_sys;
359 #ifdef HAVE_AVAHI_CLIENT
360 if( p_sys->p_bonjour != NULL )
361 bonjour_stop_service( p_sys->p_bonjour );
364 /* update p_sout->i_out_pace_nocontrol */
365 p_access->p_sout->i_out_pace_nocontrol--;
367 httpd_StreamDelete( p_sys->p_httpd_stream );
368 httpd_HostDelete( p_sys->p_httpd_host );
370 free( p_sys->p_header );
372 msg_Dbg( p_access, "Close" );
377 /*****************************************************************************
379 *****************************************************************************/
380 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
382 sout_access_out_sys_t *p_sys = p_access->p_sys;
390 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
393 if( p_sys->b_header_complete )
395 /* free previously gathered header */
396 p_sys->i_header_size = 0;
397 p_sys->b_header_complete = VLC_FALSE;
399 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
400 p_sys->i_header_allocated )
402 p_sys->i_header_allocated =
403 p_buffer->i_buffer + p_sys->i_header_size + 1024;
405 realloc( p_sys->p_header, p_sys->i_header_allocated );
407 memcpy( &p_sys->p_header[p_sys->i_header_size],
409 p_buffer->i_buffer );
410 p_sys->i_header_size += p_buffer->i_buffer;
412 else if( !p_sys->b_header_complete )
414 p_sys->b_header_complete = VLC_TRUE;
416 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
417 p_sys->i_header_size );
420 i_len += p_buffer->i_buffer;
422 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
423 p_buffer->i_buffer );
425 p_next = p_buffer->p_next;
426 block_Release( p_buffer );
437 block_ChainRelease( p_buffer );
440 return( i_err < 0 ? VLC_EGENERIC : i_len );
443 /*****************************************************************************
444 * Seek: seek to a specific location in a file
445 *****************************************************************************/
446 static int Seek( sout_access_out_t *p_access, off_t i_pos )
448 msg_Warn( p_access, "HTTP sout access cannot seek" );