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 #ifdef HAVE_AVAHI_CLIENT
39 #define DIRECTORY_SEPARATOR '\\'
41 #define DIRECTORY_SEPARATOR '/'
45 #include "vlc_httpd.h"
47 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
49 #define DEFAULT_PORT 8080
50 #define DEFAULT_SSL_PORT 8443
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
58 #define SOUT_CFG_PREFIX "sout-http-"
60 #define USER_TEXT N_("Username")
61 #define USER_LONGTEXT N_("User name that will be " \
62 "requested to access the stream." )
63 #define PASS_TEXT N_("Password")
64 #define PASS_LONGTEXT N_("Password that will be " \
65 "requested to access the stream." )
67 /// \bug [String] missing closing parenthesis
68 #define MIME_TEXT N_("Mime")
69 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
72 #define CERT_TEXT N_( "Certificate file" )
73 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
74 "be used for HTTPS." )
75 #define KEY_TEXT N_( "Private key file" )
76 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
77 "be used for HTTPS. Leave " \
78 "empty if you don't have one." )
79 #define CA_TEXT N_( "Root CA file" )
80 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
81 "(certificate authority) file that will be used for " \
82 "HTTPS. Leave empty if you " \
84 #define CRL_TEXT N_( "CRL file" )
85 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
86 "file that will be used for SSL. Leave " \
87 "empty if you don't have one." )
88 #define BONJOUR_TEXT N_( "Advertise with Bonjour")
89 #define BONJOUR_LONGTEXT N_( "Advertise the stream with the Bonjour protocol." )
93 set_description( _("HTTP stream output") );
94 set_capability( "sout access", 0 );
95 set_shortname( N_("HTTP" ) );
96 add_shortcut( "http" );
97 add_shortcut( "https" );
98 add_shortcut( "mmsh" );
99 set_category( CAT_SOUT );
100 set_subcategory( SUBCAT_SOUT_ACO );
101 add_string( SOUT_CFG_PREFIX "user", "", NULL,
102 USER_TEXT, USER_LONGTEXT, VLC_TRUE );
103 add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
104 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
105 add_string( SOUT_CFG_PREFIX "mime", "", NULL,
106 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
107 add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
108 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
109 add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
110 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
111 add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
112 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
113 add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
114 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
115 add_bool( SOUT_CFG_PREFIX "bonjour", VLC_FALSE, NULL,
116 BONJOUR_TEXT, BONJOUR_LONGTEXT,VLC_TRUE);
117 set_callbacks( Open, Close );
121 /*****************************************************************************
122 * Exported prototypes
123 *****************************************************************************/
124 static const char *ppsz_sout_options[] = {
125 "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
128 static int Write( sout_access_out_t *, block_t * );
129 static int Seek ( sout_access_out_t *, off_t );
131 struct sout_access_out_sys_t
134 httpd_host_t *p_httpd_host;
137 httpd_stream_t *p_httpd_stream;
139 /* gather header from stream */
140 int i_header_allocated;
143 vlc_bool_t b_header_complete;
145 #ifdef HAVE_AVAHI_CLIENT
150 /*****************************************************************************
151 * Open: open the file
152 *****************************************************************************/
153 static int Open( vlc_object_t *p_this )
155 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
156 sout_access_out_sys_t *p_sys;
163 char *psz_user = NULL;
164 char *psz_pwd = NULL;
165 char *psz_mime = NULL;
166 const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
170 if( !( p_sys = p_access->p_sys =
171 malloc( sizeof( sout_access_out_sys_t ) ) ) )
173 msg_Err( p_access, "Not enough memory" );
177 sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
179 /* p_access->psz_name = "hostname:port/filename" */
180 psz_bind_addr = psz_parser = strdup( p_access->psz_name );
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 )
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,
245 if( p_sys->p_httpd_host == NULL )
247 msg_Err( p_access, "cannot listen on %s:%d",
248 psz_bind_addr, i_bind_port );
249 free( psz_file_name );
250 free( psz_bind_addr );
254 free( psz_bind_addr );
256 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
258 psz_mime = strdup( "video/x-ms-asf-stream" );
262 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
263 if( *val.psz_string )
264 psz_mime = val.psz_string;
266 free( val.psz_string );
269 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
270 if( *val.psz_string )
271 psz_user = val.psz_string;
273 free( val.psz_string );
275 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
276 if( *val.psz_string )
277 psz_pwd = val.psz_string;
279 free( val.psz_string );
281 p_sys->p_httpd_stream =
282 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
283 psz_user, psz_pwd, NULL );
284 if( psz_user ) free( psz_user );
285 if( psz_pwd ) free( psz_pwd );
286 if( psz_mime ) free( psz_mime );
288 if( p_sys->p_httpd_stream == NULL )
290 msg_Err( p_access, "cannot add stream %s", psz_file_name );
291 httpd_HostDelete( p_sys->p_httpd_host );
293 free( psz_file_name );
298 #ifdef HAVE_AVAHI_CLIENT
299 if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
301 playlist_t *p_playlist;
302 char *psz_txt, *psz_name;
304 p_playlist = (playlist_t *)vlc_object_find( p_access,
307 if( p_playlist == NULL )
309 msg_Err( p_access, "unable to find playlist" );
310 httpd_StreamDelete( p_sys->p_httpd_stream );
311 httpd_HostDelete( p_sys->p_httpd_host );
312 free( (void *)p_sys );
316 psz_name = strrchr( p_playlist->status.p_item->input.psz_uri,
317 DIRECTORY_SEPARATOR );
318 if( psz_name != NULL ) psz_name++;
319 else psz_name = p_playlist->status.p_item->input.psz_uri;
321 asprintf( &psz_txt, "path=%s", psz_file_name );
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 );
327 free( (void *)psz_txt );
329 if( p_sys->p_bonjour == NULL )
331 vlc_object_release( p_playlist );
332 httpd_StreamDelete( p_sys->p_httpd_stream );
333 httpd_HostDelete( p_sys->p_httpd_host );
334 free( (void *)p_sys );
337 vlc_object_release( p_playlist );
340 p_sys->p_bonjour = NULL;
343 free( psz_file_name );
345 p_sys->i_header_allocated = 1024;
346 p_sys->i_header_size = 0;
347 p_sys->p_header = malloc( p_sys->i_header_allocated );
348 p_sys->b_header_complete = VLC_FALSE;
350 p_access->pf_write = Write;
351 p_access->pf_seek = Seek;
354 /* update p_sout->i_out_pace_nocontrol */
355 p_access->p_sout->i_out_pace_nocontrol++;
360 /*****************************************************************************
361 * Close: close the target
362 *****************************************************************************/
363 static void Close( vlc_object_t * p_this )
365 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
366 sout_access_out_sys_t *p_sys = p_access->p_sys;
368 #ifdef HAVE_AVAHI_CLIENT
369 if( p_sys->p_bonjour != NULL )
370 bonjour_stop_service( p_sys->p_bonjour );
373 /* update p_sout->i_out_pace_nocontrol */
374 p_access->p_sout->i_out_pace_nocontrol--;
376 httpd_StreamDelete( p_sys->p_httpd_stream );
377 httpd_HostDelete( p_sys->p_httpd_host );
379 FREE( p_sys->p_header );
381 msg_Dbg( p_access, "Close" );
386 /*****************************************************************************
388 *****************************************************************************/
389 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
391 sout_access_out_sys_t *p_sys = p_access->p_sys;
398 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
401 if( p_sys->b_header_complete )
403 /* free previously gathered header */
404 p_sys->i_header_size = 0;
405 p_sys->b_header_complete = VLC_FALSE;
407 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
408 p_sys->i_header_allocated )
410 p_sys->i_header_allocated =
411 p_buffer->i_buffer + p_sys->i_header_size + 1024;
413 realloc( p_sys->p_header, p_sys->i_header_allocated );
415 memcpy( &p_sys->p_header[p_sys->i_header_size],
417 p_buffer->i_buffer );
418 p_sys->i_header_size += p_buffer->i_buffer;
420 else if( !p_sys->b_header_complete )
422 p_sys->b_header_complete = VLC_TRUE;
424 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
425 p_sys->i_header_size );
429 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
430 p_buffer->i_buffer );
432 p_next = p_buffer->p_next;
433 block_Release( p_buffer );
444 block_ChainRelease( p_buffer );
447 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
450 /*****************************************************************************
451 * Seek: seek to a specific location in a file
452 *****************************************************************************/
453 static int Seek( sout_access_out_t *p_access, off_t i_pos )
455 msg_Warn( p_access, "HTTP sout access cannot seek" );