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., 59 Temple Place - Suite 330, Boston, MA 02111, 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_("Allows you to give a user name that will be " \
62 "requested to access the stream." )
63 #define PASS_TEXT N_("Password")
64 #define PASS_LONGTEXT N_("Allows you to give a password that will be " \
65 "requested to access the stream." )
66 #define MIME_TEXT N_("Mime")
67 #define MIME_LONGTEXT N_("Allows you to give the mime returned by the server." )
69 #define CERT_TEXT N_( "Certificate file" )
70 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
71 "be used by the HTTP/SSL stream output" )
72 #define KEY_TEXT N_( "Private key file" )
73 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
74 " be used by the HTTP/SSL stream output. Leave " \
75 "empty if you don't have one." )
76 #define CA_TEXT N_( "Root CA file" )
77 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
78 "(certificate authority) file that will be used by " \
79 "the HTTP/SSL stream output. Leave empty if you " \
81 #define CRL_TEXT N_( "CRL file" )
82 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
83 "file that will be HTTP/SSL stream output. Leave " \
84 "empty if you don't have one." )
87 set_description( _("HTTP stream output") );
88 set_capability( "sout access", 0 );
89 set_shortname( N_("HTTP" ) );
90 add_shortcut( "http" );
91 add_shortcut( "https" );
92 add_shortcut( "mmsh" );
93 set_category( CAT_SOUT );
94 set_subcategory( SUBCAT_SOUT_ACO );
95 add_string( SOUT_CFG_PREFIX "user", "", NULL,
96 USER_TEXT, USER_LONGTEXT, VLC_TRUE );
97 add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
98 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
99 add_string( SOUT_CFG_PREFIX "mime", "", NULL,
100 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
101 add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
102 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
103 add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
104 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
105 add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
106 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
107 add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
108 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
109 set_callbacks( Open, Close );
113 /*****************************************************************************
114 * Exported prototypes
115 *****************************************************************************/
116 static const char *ppsz_sout_options[] = {
117 "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
120 static int Write( sout_access_out_t *, block_t * );
121 static int Seek ( sout_access_out_t *, off_t );
123 struct sout_access_out_sys_t
126 httpd_host_t *p_httpd_host;
129 httpd_stream_t *p_httpd_stream;
131 /* gather header from stream */
132 int i_header_allocated;
135 vlc_bool_t b_header_complete;
137 #ifdef HAVE_AVAHI_CLIENT
142 /*****************************************************************************
143 * Open: open the file
144 *****************************************************************************/
145 static int Open( vlc_object_t *p_this )
147 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
148 sout_access_out_sys_t *p_sys;
150 char *psz_parser, *psz_name;
155 char *psz_user = NULL;
156 char *psz_pwd = NULL;
157 char *psz_mime = NULL;
158 const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
162 #ifdef HAVE_AVAHI_CLIENT
163 playlist_t *p_playlist;
167 if( !( p_sys = p_access->p_sys =
168 malloc( sizeof( sout_access_out_sys_t ) ) ) )
170 msg_Err( p_access, "Not enough memory" );
174 sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
176 /* p_access->psz_name host.name:port/filename */
177 psz_name = psz_parser = strdup( p_access->psz_name );
179 psz_bind_addr = psz_parser;
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 );
248 free( psz_file_name );
253 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
255 psz_mime = strdup( "video/x-ms-asf-stream" );
259 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
260 if( *val.psz_string )
261 psz_mime = val.psz_string;
263 free( val.psz_string );
266 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
267 if( *val.psz_string )
268 psz_user = val.psz_string;
270 free( val.psz_string );
272 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
273 if( *val.psz_string )
274 psz_pwd = val.psz_string;
276 free( val.psz_string );
278 p_sys->p_httpd_stream =
279 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
280 psz_user, psz_pwd, NULL );
281 if( psz_user ) free( psz_user );
282 if( psz_pwd ) free( psz_pwd );
283 if( psz_mime ) free( psz_mime );
285 if( p_sys->p_httpd_stream == NULL )
287 msg_Err( p_access, "cannot add stream %s", psz_file_name );
288 httpd_HostDelete( p_sys->p_httpd_host );
291 free( psz_file_name );
296 #ifdef HAVE_AVAHI_CLIENT
297 asprintf( &psz_txt, "path=%s", psz_file_name );
300 free( psz_file_name );
303 #ifdef HAVE_AVAHI_CLIENT
304 p_playlist = (playlist_t *)vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
306 if( p_playlist == NULL )
308 msg_Err( p_access, "unable to find playlist" );
309 httpd_HostDelete( p_sys->p_httpd_host );
310 free( (void *)psz_txt );
311 free( (void *)p_sys );
315 psz_name = strrchr( p_playlist->status.p_item->input.psz_uri,
316 DIRECTORY_SEPARATOR );
317 if( psz_name != NULL ) psz_name++;
318 else psz_name = p_playlist->status.p_item->input.psz_uri;
320 p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
322 psz_name, i_bind_port, psz_txt );
323 free( (void *)psz_txt );
324 if( p_sys->p_bonjour == NULL )
326 vlc_object_release( p_playlist );
327 httpd_HostDelete( p_sys->p_httpd_host );
328 free( (void *)p_sys );
332 vlc_object_release( p_playlist );
335 p_sys->i_header_allocated = 1024;
336 p_sys->i_header_size = 0;
337 p_sys->p_header = malloc( p_sys->i_header_allocated );
338 p_sys->b_header_complete = VLC_FALSE;
340 p_access->pf_write = Write;
341 p_access->pf_seek = Seek;
344 /* update p_sout->i_out_pace_nocontrol */
345 p_access->p_sout->i_out_pace_nocontrol++;
350 /*****************************************************************************
351 * Close: close the target
352 *****************************************************************************/
353 static void Close( vlc_object_t * p_this )
355 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
356 sout_access_out_sys_t *p_sys = p_access->p_sys;
358 #ifdef HAVE_AVAHI_CLIENT
359 bonjour_stop_service( p_sys->p_bonjour );
362 /* update p_sout->i_out_pace_nocontrol */
363 p_access->p_sout->i_out_pace_nocontrol--;
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 /*****************************************************************************
377 *****************************************************************************/
378 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
380 sout_access_out_sys_t *p_sys = p_access->p_sys;
387 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
390 if( p_sys->b_header_complete )
392 /* free previously gathered header */
393 p_sys->i_header_size = 0;
394 p_sys->b_header_complete = VLC_FALSE;
396 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
397 p_sys->i_header_allocated )
399 p_sys->i_header_allocated =
400 p_buffer->i_buffer + p_sys->i_header_size + 1024;
402 realloc( p_sys->p_header, p_sys->i_header_allocated );
404 memcpy( &p_sys->p_header[p_sys->i_header_size],
406 p_buffer->i_buffer );
407 p_sys->i_header_size += p_buffer->i_buffer;
409 else if( !p_sys->b_header_complete )
411 p_sys->b_header_complete = VLC_TRUE;
413 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
414 p_sys->i_header_size );
418 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
419 p_buffer->i_buffer );
421 p_next = p_buffer->p_next;
422 block_Release( p_buffer );
433 block_ChainRelease( p_buffer );
436 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
439 /*****************************************************************************
440 * Seek: seek to a specific location in a file
441 *****************************************************************************/
442 static int Seek( sout_access_out_t *p_access, off_t i_pos )
444 msg_Warn( p_access, "HTTP sout access cannot seek" );