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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
39 #include <vlc_input.h>
40 #include <vlc_playlist.h>
42 #ifdef HAVE_AVAHI_CLIENT
46 #define DIRECTORY_SEPARATOR '\\'
48 #define DIRECTORY_SEPARATOR '/'
52 #include "vlc_httpd.h"
54 #define DEFAULT_PORT 8080
55 #define DEFAULT_SSL_PORT 8443
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 #define SOUT_CFG_PREFIX "sout-http-"
65 #define USER_TEXT N_("Username")
66 #define USER_LONGTEXT N_("User name that will be " \
67 "requested to access the stream." )
68 #define PASS_TEXT N_("Password")
69 #define PASS_LONGTEXT N_("Password that will be " \
70 "requested to access the stream." )
71 #define MIME_TEXT N_("Mime")
72 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
73 "if not specified)." )
74 #define CERT_TEXT N_( "Certificate file" )
75 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
76 "be used for HTTPS." )
77 #define KEY_TEXT N_( "Private key file" )
78 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
79 "be used for HTTPS. Leave " \
80 "empty if you don't have one." )
81 #define CA_TEXT N_( "Root CA file" )
82 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
83 "(certificate authority) file that will be used for " \
84 "HTTPS. Leave empty if you " \
86 #define CRL_TEXT N_( "CRL file" )
87 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
88 "file that will be used for SSL. Leave " \
89 "empty if you don't have one." )
90 #define BONJOUR_TEXT N_( "Advertise with Bonjour")
91 #define BONJOUR_LONGTEXT N_( "Advertise the stream with the Bonjour protocol." )
95 set_description( N_("HTTP stream output") );
96 set_capability( "sout access", 0 );
97 set_shortname( "HTTP" );
98 add_shortcut( "http" );
99 add_shortcut( "https" );
100 add_shortcut( "mmsh" );
101 set_category( CAT_SOUT );
102 set_subcategory( SUBCAT_SOUT_ACO );
103 add_string( SOUT_CFG_PREFIX "user", "", NULL,
104 USER_TEXT, USER_LONGTEXT, true );
105 add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
106 PASS_TEXT, PASS_LONGTEXT, true );
107 add_string( SOUT_CFG_PREFIX "mime", "", NULL,
108 MIME_TEXT, MIME_LONGTEXT, true );
109 add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
110 CERT_TEXT, CERT_LONGTEXT, true );
111 add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
112 KEY_TEXT, KEY_LONGTEXT, true );
113 add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
114 CA_TEXT, CA_LONGTEXT, true );
115 add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
116 CRL_TEXT, CRL_LONGTEXT, true );
117 add_bool( SOUT_CFG_PREFIX "bonjour", false, NULL,
118 BONJOUR_TEXT, BONJOUR_LONGTEXT, true);
119 set_callbacks( Open, Close );
123 /*****************************************************************************
124 * Exported prototypes
125 *****************************************************************************/
126 static const char *const ppsz_sout_options[] = {
127 "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
130 static ssize_t Write( sout_access_out_t *, block_t * );
131 static int Seek ( sout_access_out_t *, off_t );
132 static int Control( sout_access_out_t *, int, va_list );
134 struct sout_access_out_sys_t
137 httpd_host_t *p_httpd_host;
140 httpd_stream_t *p_httpd_stream;
142 /* gather header from stream */
143 int i_header_allocated;
146 bool b_header_complete;
148 #ifdef HAVE_AVAHI_CLIENT
153 /*****************************************************************************
154 * Open: open the file
155 *****************************************************************************/
156 static int Open( vlc_object_t *p_this )
158 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
159 sout_access_out_sys_t *p_sys;
166 char *psz_user = NULL;
167 char *psz_pwd = NULL;
168 char *psz_mime = NULL;
169 char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
173 if( !( p_sys = p_access->p_sys =
174 malloc( sizeof( sout_access_out_sys_t ) ) ) )
177 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
179 /* p_access->psz_path = "hostname:port/filename" */
180 psz_bind_addr = psz_parser = strdup( p_access->psz_path );
183 psz_file_name = NULL;
185 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
189 if( *psz_parser == ':' )
193 i_bind_port = atoi( psz_parser );
195 while( *psz_parser && *psz_parser != '/' )
200 if( *psz_parser == '/' )
204 psz_file_name = psz_parser;
207 if( psz_file_name == NULL )
209 psz_file_name = strdup( "/" );
211 else if( *psz_file_name != '/' )
213 char *p = psz_file_name;
215 psz_file_name = malloc( strlen( p ) + 2 );
216 strcpy( psz_file_name, "/" );
217 strcat( psz_file_name, p );
221 psz_file_name = strdup( psz_file_name );
225 if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
227 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
228 psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
229 psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
230 psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
232 if( i_bind_port <= 0 )
233 i_bind_port = DEFAULT_SSL_PORT;
237 if( i_bind_port <= 0 )
238 i_bind_port = DEFAULT_PORT;
241 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
242 psz_bind_addr, i_bind_port,
243 psz_cert, psz_key, psz_ca,
250 if( p_sys->p_httpd_host == NULL )
252 msg_Err( p_access, "cannot listen on %s:%d",
253 psz_bind_addr, i_bind_port );
254 free( psz_file_name );
255 free( psz_bind_addr );
259 free( psz_bind_addr );
261 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
263 psz_mime = strdup( "video/x-ms-asf-stream" );
267 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
268 if( *val.psz_string )
269 psz_mime = val.psz_string;
271 free( val.psz_string );
274 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
275 if( *val.psz_string )
276 psz_user = val.psz_string;
278 free( val.psz_string );
280 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
281 if( *val.psz_string )
282 psz_pwd = val.psz_string;
284 free( val.psz_string );
286 p_sys->p_httpd_stream =
287 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
288 psz_user, psz_pwd, NULL );
293 if( p_sys->p_httpd_stream == NULL )
295 msg_Err( p_access, "cannot add stream %s", psz_file_name );
296 httpd_HostDelete( p_sys->p_httpd_host );
298 free( psz_file_name );
303 #ifdef HAVE_AVAHI_CLIENT
304 if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
306 char *psz_txt, *psz_name;
307 playlist_t *p_playlist = pl_Yield( p_access );
309 char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
310 char *psz_newuri = psz_uri;
311 psz_name = strrchr( psz_newuri, DIRECTORY_SEPARATOR );
312 if( psz_name != NULL ) psz_name++;
313 else psz_name = psz_newuri;
316 asprintf( &psz_txt, "path=%s", psz_file_name ) == -1 )
318 pl_Release( p_access );
323 p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
324 strcmp( p_access->psz_access, "https" )
325 ? "_vlc-http._tcp" : "_vlc-https._tcp",
326 psz_name, i_bind_port, psz_txt );
330 if( p_sys->p_bonjour == NULL )
331 msg_Err( p_access, "unable to start requested Bonjour announce" );
332 pl_Release( p_access );
335 p_sys->p_bonjour = NULL;
338 free( psz_file_name );
340 p_sys->i_header_allocated = 1024;
341 p_sys->i_header_size = 0;
342 p_sys->p_header = malloc( p_sys->i_header_allocated );
343 p_sys->b_header_complete = false;
345 p_access->pf_write = Write;
346 p_access->pf_seek = Seek;
347 p_access->pf_control = Control;
352 /*****************************************************************************
353 * Close: close the target
354 *****************************************************************************/
355 static void Close( vlc_object_t * p_this )
357 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
358 sout_access_out_sys_t *p_sys = p_access->p_sys;
360 #ifdef HAVE_AVAHI_CLIENT
361 if( p_sys->p_bonjour != NULL )
362 bonjour_stop_service( p_sys->p_bonjour );
365 httpd_StreamDelete( p_sys->p_httpd_stream );
366 httpd_HostDelete( p_sys->p_httpd_host );
368 free( p_sys->p_header );
370 msg_Dbg( p_access, "Close" );
375 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
381 case ACCESS_OUT_CONTROLS_PACE:
382 *va_arg( args, bool * ) = false;
391 /*****************************************************************************
393 *****************************************************************************/
394 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
396 sout_access_out_sys_t *p_sys = p_access->p_sys;
404 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
407 if( p_sys->b_header_complete )
409 /* free previously gathered header */
410 p_sys->i_header_size = 0;
411 p_sys->b_header_complete = false;
413 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
414 p_sys->i_header_allocated )
416 p_sys->i_header_allocated =
417 p_buffer->i_buffer + p_sys->i_header_size + 1024;
419 realloc( p_sys->p_header, p_sys->i_header_allocated );
421 memcpy( &p_sys->p_header[p_sys->i_header_size],
423 p_buffer->i_buffer );
424 p_sys->i_header_size += p_buffer->i_buffer;
426 else if( !p_sys->b_header_complete )
428 p_sys->b_header_complete = true;
430 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
431 p_sys->i_header_size );
434 i_len += p_buffer->i_buffer;
436 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
437 p_buffer->i_buffer );
439 p_next = p_buffer->p_next;
440 block_Release( p_buffer );
451 block_ChainRelease( p_buffer );
454 return( i_err < 0 ? VLC_EGENERIC : i_len );
457 /*****************************************************************************
458 * Seek: seek to a specific location in a file
459 *****************************************************************************/
460 static int Seek( sout_access_out_t *p_access, off_t i_pos )
463 msg_Warn( p_access, "HTTP sout access cannot seek" );