00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdlib.h>
00029
00030 #include <vlc/vlc.h>
00031 #include <vlc/sout.h>
00032
00033 #ifdef HAVE_AVAHI_CLIENT
00034 #include <vlc/intf.h>
00035
00036 #include "bonjour.h"
00037
00038 #if defined( WIN32 )
00039 #define DIRECTORY_SEPARATOR '\\'
00040 #else
00041 #define DIRECTORY_SEPARATOR '/'
00042 #endif
00043 #endif
00044
00045 #include "vlc_httpd.h"
00046
00047 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
00048
00049 #define DEFAULT_PORT 8080
00050 #define DEFAULT_SSL_PORT 8443
00051
00052
00053
00054
00055 static int Open ( vlc_object_t * );
00056 static void Close( vlc_object_t * );
00057
00058 #define SOUT_CFG_PREFIX "sout-http-"
00059
00060 #define USER_TEXT N_("Username")
00061 #define USER_LONGTEXT N_("Allows you to give a user name that will be " \
00062 "requested to access the stream." )
00063 #define PASS_TEXT N_("Password")
00064 #define PASS_LONGTEXT N_("Allows you to give a password that will be " \
00065 "requested to access the stream." )
00066 #define MIME_TEXT N_("Mime")
00067 #define MIME_LONGTEXT N_("Allows you to give the mime returned by the server." )
00068
00069 #define CERT_TEXT N_( "Certificate file" )
00070 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
00071 "be used by the HTTP/SSL stream output" )
00072 #define KEY_TEXT N_( "Private key file" )
00073 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
00074 " be used by the HTTP/SSL stream output. Leave " \
00075 "empty if you don't have one." )
00076 #define CA_TEXT N_( "Root CA file" )
00077 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
00078 "(certificate authority) file that will be used by " \
00079 "the HTTP/SSL stream output. Leave empty if you " \
00080 "don't have one." )
00081 #define CRL_TEXT N_( "CRL file" )
00082 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
00083 "file that will be HTTP/SSL stream output. Leave " \
00084 "empty if you don't have one." )
00085
00086 vlc_module_begin();
00087 set_description( _("HTTP stream output") );
00088 set_capability( "sout access", 0 );
00089 set_shortname( N_("HTTP" ) );
00090 add_shortcut( "http" );
00091 add_shortcut( "https" );
00092 add_shortcut( "mmsh" );
00093 set_category( CAT_SOUT );
00094 set_subcategory( SUBCAT_SOUT_ACO );
00095 add_string( SOUT_CFG_PREFIX "user", "", NULL,
00096 USER_TEXT, USER_LONGTEXT, VLC_TRUE );
00097 add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
00098 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
00099 add_string( SOUT_CFG_PREFIX "mime", "", NULL,
00100 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
00101 add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
00102 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
00103 add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
00104 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
00105 add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
00106 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
00107 add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
00108 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
00109 set_callbacks( Open, Close );
00110 vlc_module_end();
00111
00112
00113
00114
00115
00116 static const char *ppsz_sout_options[] = {
00117 "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
00118 };
00119
00120 static int Write( sout_access_out_t *, block_t * );
00121 static int Seek ( sout_access_out_t *, off_t );
00122
00123 struct sout_access_out_sys_t
00124 {
00125
00126 httpd_host_t *p_httpd_host;
00127
00128
00129 httpd_stream_t *p_httpd_stream;
00130
00131
00132 int i_header_allocated;
00133 int i_header_size;
00134 uint8_t *p_header;
00135 vlc_bool_t b_header_complete;
00136
00137 #ifdef HAVE_AVAHI_CLIENT
00138 void *p_bonjour;
00139 #endif
00140 };
00141
00142
00143
00144
00145 static int Open( vlc_object_t *p_this )
00146 {
00147 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
00148 sout_access_out_sys_t *p_sys;
00149
00150 char *psz_parser, *psz_name;
00151
00152 char *psz_bind_addr;
00153 int i_bind_port;
00154 char *psz_file_name;
00155 char *psz_user = NULL;
00156 char *psz_pwd = NULL;
00157 char *psz_mime = NULL;
00158 const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
00159 *psz_crl = NULL;
00160 vlc_value_t val;
00161
00162 #ifdef HAVE_AVAHI_CLIENT
00163 playlist_t *p_playlist;
00164 char *psz_txt;
00165 #endif
00166
00167 if( !( p_sys = p_access->p_sys =
00168 malloc( sizeof( sout_access_out_sys_t ) ) ) )
00169 {
00170 msg_Err( p_access, "Not enough memory" );
00171 return VLC_ENOMEM ;
00172 }
00173
00174 sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
00175
00176
00177 psz_name = psz_parser = strdup( p_access->psz_name );
00178
00179 psz_bind_addr = psz_parser;
00180 i_bind_port = 0;
00181 psz_file_name = "";
00182
00183 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
00184 {
00185 psz_parser++;
00186 }
00187 if( *psz_parser == ':' )
00188 {
00189 *psz_parser = '\0';
00190 psz_parser++;
00191 i_bind_port = atoi( psz_parser );
00192
00193 while( *psz_parser && *psz_parser != '/' )
00194 {
00195 psz_parser++;
00196 }
00197 }
00198 if( *psz_parser == '/' )
00199 {
00200 *psz_parser = '\0';
00201 psz_parser++;
00202 psz_file_name = psz_parser;
00203 }
00204
00205 if( !*psz_file_name )
00206 {
00207 psz_file_name = strdup( "/" );
00208 }
00209 else if( *psz_file_name != '/' )
00210 {
00211 char *p = psz_file_name;
00212
00213 psz_file_name = malloc( strlen( p ) + 2 );
00214 strcpy( psz_file_name, "/" );
00215 strcat( psz_file_name, p );
00216 }
00217 else
00218 {
00219 psz_file_name = strdup( psz_file_name );
00220 }
00221
00222
00223 if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
00224 {
00225 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
00226 psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
00227 psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
00228 psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
00229
00230 if( i_bind_port <= 0 )
00231 i_bind_port = DEFAULT_SSL_PORT;
00232 }
00233 else
00234 {
00235 if( i_bind_port <= 0 )
00236 i_bind_port = DEFAULT_PORT;
00237 }
00238
00239 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
00240 psz_bind_addr, i_bind_port,
00241 psz_cert, psz_key, psz_ca,
00242 psz_crl );
00243 if( p_sys->p_httpd_host == NULL )
00244 {
00245 msg_Err( p_access, "cannot listen on %s:%d",
00246 psz_bind_addr, i_bind_port );
00247 free( psz_name );
00248 free( psz_file_name );
00249 free( p_sys );
00250 return VLC_EGENERIC;
00251 }
00252
00253 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
00254 {
00255 psz_mime = strdup( "video/x-ms-asf-stream" );
00256 }
00257 else
00258 {
00259 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
00260 if( *val.psz_string )
00261 psz_mime = val.psz_string;
00262 else
00263 free( val.psz_string );
00264 }
00265
00266 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
00267 if( *val.psz_string )
00268 psz_user = val.psz_string;
00269 else
00270 free( val.psz_string );
00271
00272 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
00273 if( *val.psz_string )
00274 psz_pwd = val.psz_string;
00275 else
00276 free( val.psz_string );
00277
00278 p_sys->p_httpd_stream =
00279 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
00280 psz_user, psz_pwd, NULL );
00281 if( psz_user ) free( psz_user );
00282 if( psz_pwd ) free( psz_pwd );
00283 if( psz_mime ) free( psz_mime );
00284
00285 if( p_sys->p_httpd_stream == NULL )
00286 {
00287 msg_Err( p_access, "cannot add stream %s", psz_file_name );
00288 httpd_HostDelete( p_sys->p_httpd_host );
00289
00290 free( psz_name );
00291 free( psz_file_name );
00292 free( p_sys );
00293 return VLC_EGENERIC;
00294 }
00295
00296 #ifdef HAVE_AVAHI_CLIENT
00297 asprintf( &psz_txt, "path=%s", psz_file_name );
00298 #endif
00299
00300 free( psz_file_name );
00301 free( psz_name );
00302
00303 #ifdef HAVE_AVAHI_CLIENT
00304 p_playlist = (playlist_t *)vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
00305 FIND_ANYWHERE );
00306 if( p_playlist == NULL )
00307 {
00308 msg_Err( p_access, "unable to find playlist" );
00309 httpd_HostDelete( p_sys->p_httpd_host );
00310 free( (void *)psz_txt );
00311 free( (void *)p_sys );
00312 return VLC_EGENERIC;
00313 }
00314
00315 psz_name = strrchr( p_playlist->status.p_item->input.psz_uri,
00316 DIRECTORY_SEPARATOR );
00317 if( psz_name != NULL ) psz_name++;
00318 else psz_name = p_playlist->status.p_item->input.psz_uri;
00319
00320 p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
00321 "_vlc-http._tcp",
00322 psz_name, i_bind_port, psz_txt );
00323 free( (void *)psz_txt );
00324 if( p_sys->p_bonjour == NULL )
00325 {
00326 vlc_object_release( p_playlist );
00327 httpd_HostDelete( p_sys->p_httpd_host );
00328 free( (void *)p_sys );
00329 return VLC_EGENERIC;
00330 }
00331
00332 vlc_object_release( p_playlist );
00333 #endif
00334
00335 p_sys->i_header_allocated = 1024;
00336 p_sys->i_header_size = 0;
00337 p_sys->p_header = malloc( p_sys->i_header_allocated );
00338 p_sys->b_header_complete = VLC_FALSE;
00339
00340 p_access->pf_write = Write;
00341 p_access->pf_seek = Seek;
00342
00343
00344
00345 p_access->p_sout->i_out_pace_nocontrol++;
00346
00347 return VLC_SUCCESS;
00348 }
00349
00350
00351
00352
00353 static void Close( vlc_object_t * p_this )
00354 {
00355 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
00356 sout_access_out_sys_t *p_sys = p_access->p_sys;
00357
00358 #ifdef HAVE_AVAHI_CLIENT
00359 bonjour_stop_service( p_sys->p_bonjour );
00360 #endif
00361
00362
00363 p_access->p_sout->i_out_pace_nocontrol--;
00364
00365 httpd_StreamDelete( p_sys->p_httpd_stream );
00366 httpd_HostDelete( p_sys->p_httpd_host );
00367
00368 FREE( p_sys->p_header );
00369
00370 msg_Dbg( p_access, "Close" );
00371
00372 free( p_sys );
00373 }
00374
00375
00376
00377
00378 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
00379 {
00380 sout_access_out_sys_t *p_sys = p_access->p_sys;
00381 int i_err = 0;
00382
00383 while( p_buffer )
00384 {
00385 block_t *p_next;
00386
00387 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
00388 {
00389
00390 if( p_sys->b_header_complete )
00391 {
00392
00393 p_sys->i_header_size = 0;
00394 p_sys->b_header_complete = VLC_FALSE;
00395 }
00396 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
00397 p_sys->i_header_allocated )
00398 {
00399 p_sys->i_header_allocated =
00400 p_buffer->i_buffer + p_sys->i_header_size + 1024;
00401 p_sys->p_header =
00402 realloc( p_sys->p_header, p_sys->i_header_allocated );
00403 }
00404 memcpy( &p_sys->p_header[p_sys->i_header_size],
00405 p_buffer->p_buffer,
00406 p_buffer->i_buffer );
00407 p_sys->i_header_size += p_buffer->i_buffer;
00408 }
00409 else if( !p_sys->b_header_complete )
00410 {
00411 p_sys->b_header_complete = VLC_TRUE;
00412
00413 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
00414 p_sys->i_header_size );
00415 }
00416
00417
00418 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
00419 p_buffer->i_buffer );
00420
00421 p_next = p_buffer->p_next;
00422 block_Release( p_buffer );
00423 p_buffer = p_next;
00424
00425 if( i_err < 0 )
00426 {
00427 break;
00428 }
00429 }
00430
00431 if( i_err < 0 )
00432 {
00433 block_ChainRelease( p_buffer );
00434 }
00435
00436 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
00437 }
00438
00439
00440
00441
00442 static int Seek( sout_access_out_t *p_access, off_t i_pos )
00443 {
00444 msg_Warn( p_access, "HTTP sout access cannot seek" );
00445 return VLC_EGENERIC;
00446 }