1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2009 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 #if 0 //def HAVE_AVAHI_CLIENT
46 #define DIRECTORY_SEPARATOR '\\'
48 #define DIRECTORY_SEPARATOR '/'
52 #include <vlc_httpd.h>
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
60 #define SOUT_CFG_PREFIX "sout-http-"
62 #define USER_TEXT N_("Username")
63 #define USER_LONGTEXT N_("User name that will be " \
64 "requested to access the stream." )
65 #define PASS_TEXT N_("Password")
66 #define PASS_LONGTEXT N_("Password that will be " \
67 "requested to access the stream." )
68 #define MIME_TEXT N_("Mime")
69 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
70 "if not specified)." )
71 #define BONJOUR_TEXT N_( "Advertise with Bonjour")
72 #define BONJOUR_LONGTEXT N_( "Advertise the stream with the Bonjour protocol." )
76 set_description( N_("HTTP stream output") )
77 set_capability( "sout access", 0 )
78 set_shortname( "HTTP" )
79 add_shortcut( "http", "https", "mmsh" )
80 set_category( CAT_SOUT )
81 set_subcategory( SUBCAT_SOUT_ACO )
82 add_string( SOUT_CFG_PREFIX "user", "",
83 USER_TEXT, USER_LONGTEXT, true )
84 add_password( SOUT_CFG_PREFIX "pwd", "",
85 PASS_TEXT, PASS_LONGTEXT, true )
86 add_string( SOUT_CFG_PREFIX "mime", "",
87 MIME_TEXT, MIME_LONGTEXT, true )
88 #if 0 //def HAVE_AVAHI_CLIENT
89 add_bool( SOUT_CFG_PREFIX "bonjour", false,
90 BONJOUR_TEXT, BONJOUR_LONGTEXT, true);
92 set_callbacks( Open, Close )
96 /*****************************************************************************
98 *****************************************************************************/
99 static const char *const ppsz_sout_options[] = {
100 "user", "pwd", "mime", NULL
103 static ssize_t Write( sout_access_out_t *, block_t * );
104 static int Seek ( sout_access_out_t *, off_t );
105 static int Control( sout_access_out_t *, int, va_list );
107 struct sout_access_out_sys_t
110 httpd_host_t *p_httpd_host;
113 httpd_stream_t *p_httpd_stream;
115 /* gather header from stream */
116 int i_header_allocated;
119 bool b_header_complete;
121 #if 0 //def HAVE_AVAHI_CLIENT
126 /*****************************************************************************
127 * Open: open the file
128 *****************************************************************************/
129 static int Open( vlc_object_t *p_this )
131 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
132 sout_access_out_sys_t *p_sys;
138 if( !( p_sys = p_access->p_sys =
139 malloc( sizeof( sout_access_out_sys_t ) ) ) )
142 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
144 const char *path = p_access->psz_path;
145 /* Skip everything before / - backward compatibiltiy with VLC 1.1 */
146 path += strcspn( path, "/" );
147 if( path > p_access->psz_path )
149 const char *port = strrchr( p_access->psz_path, ':' );
150 if( port != NULL && strchr( port, ']' ) != NULL )
151 port = NULL; /* IPv6 numeral */
152 if( port != p_access->psz_path )
154 int len = (port ? port : path) - p_access->psz_path;
155 /* msg_Err( p_access, "\"%.*s\" HTTP host ignored", len,
156 p_access->psz_path );
158 "Pass --http-host=IP on the command line instead." ); */
161 strncpy( host, p_access->psz_path, len );
164 var_Create( p_access, "http-host", VLC_VAR_STRING );
165 var_SetString( p_access, "http-host", host );
169 /* int len = path - ++port;
170 msg_Err( p_access, "\"%.*s\" HTTP port ignored", len, port );
171 msg_Info( p_access, "Pass --%s-port=%.*s on the command line "
172 "instead.", strcasecmp( p_access->psz_access, "https" )
173 ? "http" : "https", len, port ); */
176 int bind_port = atoi( port );
179 const char *var = strcasecmp( p_access->psz_access, "https" )
180 ? "http-port" : "https-port";
181 var_Create( p_access, var, VLC_VAR_INTEGER );
182 var_SetInteger( p_access, var, bind_port );
190 if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
191 p_sys->p_httpd_host = vlc_https_HostNew( VLC_OBJECT(p_access) );
193 p_sys->p_httpd_host = vlc_http_HostNew( VLC_OBJECT(p_access) );
195 if( p_sys->p_httpd_host == NULL )
197 msg_Err( p_access, "cannot start HTTP server" );
202 psz_user = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "user" );
203 psz_pwd = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "pwd" );
204 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
206 psz_mime = strdup( "video/x-ms-asf-stream" );
210 psz_mime = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "mime" );
213 p_sys->p_httpd_stream =
214 httpd_StreamNew( p_sys->p_httpd_host, path, psz_mime,
215 psz_user, psz_pwd, NULL );
220 if( p_sys->p_httpd_stream == NULL )
222 msg_Err( p_access, "cannot add stream %s", path );
223 httpd_HostDelete( p_sys->p_httpd_host );
229 #if 0 //def HAVE_AVAHI_CLIENT
230 if( var_InheritBool(p_this, SOUT_CFG_PREFIX "bonjour") )
232 char *psz_txt, *psz_name;
233 playlist_t *p_playlist = pl_Get( p_access );
235 char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
236 char *psz_newuri = psz_uri;
237 psz_name = strrchr( psz_newuri, DIRECTORY_SEPARATOR );
238 if( psz_name != NULL ) psz_name++;
239 else psz_name = psz_newuri;
241 if( asprintf( &psz_txt, "path=%s", path ) == -1 )
247 p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
248 strcmp( p_access->psz_access, "https" )
249 ? "_vlc-http._tcp" : "_vlc-https._tcp",
250 psz_name, i_bind_port, psz_txt );
254 if( p_sys->p_bonjour == NULL )
255 msg_Err( p_access, "unable to start requested Bonjour announce" );
258 p_sys->p_bonjour = NULL;
261 p_sys->i_header_allocated = 1024;
262 p_sys->i_header_size = 0;
263 p_sys->p_header = xmalloc( p_sys->i_header_allocated );
264 p_sys->b_header_complete = false;
266 p_access->pf_write = Write;
267 p_access->pf_seek = Seek;
268 p_access->pf_control = Control;
273 /*****************************************************************************
274 * Close: close the target
275 *****************************************************************************/
276 static void Close( vlc_object_t * p_this )
278 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
279 sout_access_out_sys_t *p_sys = p_access->p_sys;
281 #if 0 //def HAVE_AVAHI_CLIENT
282 if( p_sys->p_bonjour != NULL )
283 bonjour_stop_service( p_sys->p_bonjour );
286 httpd_StreamDelete( p_sys->p_httpd_stream );
287 httpd_HostDelete( p_sys->p_httpd_host );
289 free( p_sys->p_header );
291 msg_Dbg( p_access, "Close" );
296 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
302 case ACCESS_OUT_CONTROLS_PACE:
303 *va_arg( args, bool * ) = false;
312 /*****************************************************************************
314 *****************************************************************************/
315 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
317 sout_access_out_sys_t *p_sys = p_access->p_sys;
325 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
328 if( p_sys->b_header_complete )
330 /* free previously gathered header */
331 p_sys->i_header_size = 0;
332 p_sys->b_header_complete = false;
334 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
335 p_sys->i_header_allocated )
337 p_sys->i_header_allocated =
338 p_buffer->i_buffer + p_sys->i_header_size + 1024;
339 p_sys->p_header = xrealloc( p_sys->p_header,
340 p_sys->i_header_allocated );
342 memcpy( &p_sys->p_header[p_sys->i_header_size],
344 p_buffer->i_buffer );
345 p_sys->i_header_size += p_buffer->i_buffer;
347 else if( !p_sys->b_header_complete )
349 p_sys->b_header_complete = true;
351 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
352 p_sys->i_header_size );
355 i_len += p_buffer->i_buffer;
357 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
358 p_buffer->i_buffer );
360 p_next = p_buffer->p_next;
361 block_Release( p_buffer );
372 block_ChainRelease( p_buffer );
375 return( i_err < 0 ? VLC_EGENERIC : i_len );
378 /*****************************************************************************
379 * Seek: seek to a specific location in a file
380 *****************************************************************************/
381 static int Seek( sout_access_out_t *p_access, off_t i_pos )
384 msg_Warn( p_access, "HTTP sout access cannot seek" );