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 DEFAULT_PORT 8080
48 #define DEFAULT_SSL_PORT 8443
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
56 #define SOUT_CFG_PREFIX "sout-http-"
58 #define USER_TEXT N_("Username")
59 #define USER_LONGTEXT N_("User name that will be " \
60 "requested to access the stream." )
61 #define PASS_TEXT N_("Password")
62 #define PASS_LONGTEXT N_("Password that will be " \
63 "requested to access the stream." )
65 /// \bug [String] missing closing parenthesis
66 #define MIME_TEXT N_("Mime")
67 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
70 #define CERT_TEXT N_( "Certificate file" )
71 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
72 "be used for HTTPS." )
73 #define KEY_TEXT N_( "Private key file" )
74 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
75 "be used for HTTPS. Leave " \
76 "empty if you don't have one." )
77 #define CA_TEXT N_( "Root CA file" )
78 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
79 "(certificate authority) file that will be used for " \
80 "HTTPS. Leave empty if you " \
82 #define CRL_TEXT N_( "CRL file" )
83 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
84 "file that will be used for SSL. Leave " \
85 "empty if you don't have one." )
86 #define BONJOUR_TEXT N_( "Advertise with Bonjour")
87 #define BONJOUR_LONGTEXT N_( "Advertise the stream with the Bonjour protocol." )
91 set_description( _("HTTP stream output") );
92 set_capability( "sout access", 0 );
93 set_shortname( _("HTTP" ) );
94 add_shortcut( "http" );
95 add_shortcut( "https" );
96 add_shortcut( "mmsh" );
97 set_category( CAT_SOUT );
98 set_subcategory( SUBCAT_SOUT_ACO );
99 add_string( SOUT_CFG_PREFIX "user", "", NULL,
100 USER_TEXT, USER_LONGTEXT, VLC_TRUE );
101 add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
102 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
103 add_string( SOUT_CFG_PREFIX "mime", "", NULL,
104 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
105 add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
106 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
107 add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
108 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
109 add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
110 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
111 add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
112 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
113 add_bool( SOUT_CFG_PREFIX "bonjour", VLC_FALSE, NULL,
114 BONJOUR_TEXT, BONJOUR_LONGTEXT, VLC_TRUE);
115 set_callbacks( Open, Close );
119 /*****************************************************************************
120 * Exported prototypes
121 *****************************************************************************/
122 static const char *ppsz_sout_options[] = {
123 "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
126 static int Write( sout_access_out_t *, block_t * );
127 static int Seek ( sout_access_out_t *, off_t );
129 struct sout_access_out_sys_t
132 httpd_host_t *p_httpd_host;
135 httpd_stream_t *p_httpd_stream;
137 /* gather header from stream */
138 int i_header_allocated;
141 vlc_bool_t b_header_complete;
143 #ifdef HAVE_AVAHI_CLIENT
148 /*****************************************************************************
149 * Open: open the file
150 *****************************************************************************/
151 static int Open( vlc_object_t *p_this )
153 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
154 sout_access_out_sys_t *p_sys;
161 char *psz_user = NULL;
162 char *psz_pwd = NULL;
163 char *psz_mime = NULL;
164 const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
168 if( !( p_sys = p_access->p_sys =
169 malloc( sizeof( sout_access_out_sys_t ) ) ) )
171 msg_Err( p_access, "Not enough memory" );
175 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
177 /* p_access->psz_name = "hostname:port/filename" */
178 psz_bind_addr = psz_parser = strdup( p_access->psz_name );
183 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
187 if( *psz_parser == ':' )
191 i_bind_port = atoi( psz_parser );
193 while( *psz_parser && *psz_parser != '/' )
198 if( *psz_parser == '/' )
202 psz_file_name = psz_parser;
205 if( !*psz_file_name )
207 psz_file_name = strdup( "/" );
209 else if( *psz_file_name != '/' )
211 char *p = psz_file_name;
213 psz_file_name = malloc( strlen( p ) + 2 );
214 strcpy( psz_file_name, "/" );
215 strcat( psz_file_name, p );
219 psz_file_name = strdup( psz_file_name );
223 if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
225 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
226 psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
227 psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
228 psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
230 if( i_bind_port <= 0 )
231 i_bind_port = DEFAULT_SSL_PORT;
235 if( i_bind_port <= 0 )
236 i_bind_port = DEFAULT_PORT;
239 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
240 psz_bind_addr, i_bind_port,
241 psz_cert, psz_key, psz_ca,
243 if( p_sys->p_httpd_host == NULL )
245 msg_Err( p_access, "cannot listen on %s:%d",
246 psz_bind_addr, i_bind_port );
247 free( psz_file_name );
248 free( psz_bind_addr );
252 free( psz_bind_addr );
254 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
256 psz_mime = strdup( "video/x-ms-asf-stream" );
260 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
261 if( *val.psz_string )
262 psz_mime = val.psz_string;
264 free( val.psz_string );
267 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
268 if( *val.psz_string )
269 psz_user = val.psz_string;
271 free( val.psz_string );
273 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
274 if( *val.psz_string )
275 psz_pwd = val.psz_string;
277 free( val.psz_string );
279 p_sys->p_httpd_stream =
280 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
281 psz_user, psz_pwd, NULL );
282 if( psz_user ) free( psz_user );
283 if( psz_pwd ) free( psz_pwd );
284 if( psz_mime ) free( psz_mime );
286 if( p_sys->p_httpd_stream == NULL )
288 msg_Err( p_access, "cannot add stream %s", psz_file_name );
289 httpd_HostDelete( p_sys->p_httpd_host );
291 free( psz_file_name );
296 #ifdef HAVE_AVAHI_CLIENT
297 if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
299 char *psz_txt, *psz_name;
300 playlist_t *p_playlist = pl_Yield( p_access );
302 psz_name = strrchr( p_playlist->status.p_item->p_input->psz_uri,
303 DIRECTORY_SEPARATOR );
304 if( psz_name != NULL ) psz_name++;
305 else psz_name = p_playlist->status.p_item->p_input->psz_uri;
307 asprintf( &psz_txt, "path=%s", psz_file_name );
309 p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
310 strcmp( p_access->psz_access, "https" )
311 ? "_vlc-http._tcp" : "_vlc-https._tcp",
312 psz_name, i_bind_port, psz_txt );
313 free( (void *)psz_txt );
315 if( p_sys->p_bonjour == NULL )
316 msg_Err( p_access, "unable to start requested Bonjour announce" );
317 vlc_object_release( p_playlist );
320 p_sys->p_bonjour = NULL;
323 free( psz_file_name );
325 p_sys->i_header_allocated = 1024;
326 p_sys->i_header_size = 0;
327 p_sys->p_header = malloc( p_sys->i_header_allocated );
328 p_sys->b_header_complete = VLC_FALSE;
330 p_access->pf_write = Write;
331 p_access->pf_seek = Seek;
334 /* update p_sout->i_out_pace_nocontrol */
335 p_access->p_sout->i_out_pace_nocontrol++;
340 /*****************************************************************************
341 * Close: close the target
342 *****************************************************************************/
343 static void Close( vlc_object_t * p_this )
345 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
346 sout_access_out_sys_t *p_sys = p_access->p_sys;
348 #ifdef HAVE_AVAHI_CLIENT
349 if( p_sys->p_bonjour != NULL )
350 bonjour_stop_service( p_sys->p_bonjour );
353 /* update p_sout->i_out_pace_nocontrol */
354 p_access->p_sout->i_out_pace_nocontrol--;
356 httpd_StreamDelete( p_sys->p_httpd_stream );
357 httpd_HostDelete( p_sys->p_httpd_host );
359 FREE( p_sys->p_header );
361 msg_Dbg( p_access, "Close" );
366 /*****************************************************************************
368 *****************************************************************************/
369 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
371 sout_access_out_sys_t *p_sys = p_access->p_sys;
378 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
381 if( p_sys->b_header_complete )
383 /* free previously gathered header */
384 p_sys->i_header_size = 0;
385 p_sys->b_header_complete = VLC_FALSE;
387 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
388 p_sys->i_header_allocated )
390 p_sys->i_header_allocated =
391 p_buffer->i_buffer + p_sys->i_header_size + 1024;
393 realloc( p_sys->p_header, p_sys->i_header_allocated );
395 memcpy( &p_sys->p_header[p_sys->i_header_size],
397 p_buffer->i_buffer );
398 p_sys->i_header_size += p_buffer->i_buffer;
400 else if( !p_sys->b_header_complete )
402 p_sys->b_header_complete = VLC_TRUE;
404 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
405 p_sys->i_header_size );
409 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
410 p_buffer->i_buffer );
412 p_next = p_buffer->p_next;
413 block_Release( p_buffer );
424 block_ChainRelease( p_buffer );
427 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
430 /*****************************************************************************
431 * Seek: seek to a specific location in a file
432 *****************************************************************************/
433 static int Seek( sout_access_out_t *p_access, off_t i_pos )
435 msg_Warn( p_access, "HTTP sout access cannot seek" );