1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include "vlc_httpd.h"
34 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
36 #define DEFAULT_PORT 8080
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
44 #define SOUT_CFG_PREFIX "sout-http-"
46 #define USER_TEXT N_("Username")
47 #define USER_LONGTEXT N_("Allows you to give a user name that will be " \
48 "requested to access the stream." )
49 #define PASS_TEXT N_("Password")
50 #define PASS_LONGTEXT N_("Allows you to give a password that will be " \
51 "requested to access the stream." )
52 #define MIME_TEXT N_("Mime")
53 #define MIME_LONGTEXT N_("Allows you to give the mime returned by the server." )
56 set_description( _("HTTP stream output") );
57 set_capability( "sout access", 0 );
58 add_shortcut( "http" );
59 add_shortcut( "mmsh" );
60 add_string( SOUT_CFG_PREFIX "user", "", NULL, USER_TEXT, USER_LONGTEXT, VLC_TRUE );
61 add_string( SOUT_CFG_PREFIX "pwd", "", NULL, PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
62 add_string( SOUT_CFG_PREFIX "mime", "", NULL, MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
63 set_callbacks( Open, Close );
67 /*****************************************************************************
69 *****************************************************************************/
70 static const char *ppsz_sout_options[] = {
71 "user", "pwd", "mime", NULL
74 static int Write( sout_access_out_t *, block_t * );
75 static int Seek ( sout_access_out_t *, off_t );
77 struct sout_access_out_sys_t
80 httpd_host_t *p_httpd_host;
83 httpd_stream_t *p_httpd_stream;
85 /* gather header from stream */
86 int i_header_allocated;
89 vlc_bool_t b_header_complete;
92 /*****************************************************************************
94 *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
97 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
98 sout_access_out_sys_t *p_sys;
100 char *psz_parser, *psz_name;
105 char *psz_user = NULL;
106 char *psz_pwd = NULL;
107 char *psz_mime = NULL;
110 if( !( p_sys = p_access->p_sys =
111 malloc( sizeof( sout_access_out_sys_t ) ) ) )
113 msg_Err( p_access, "Not enough memory" );
114 return( VLC_EGENERIC );
117 sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
119 /* p_access->psz_name host.name:port/filename */
120 psz_name = psz_parser = strdup( p_access->psz_name );
122 psz_bind_addr = psz_parser;
126 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
130 if( *psz_parser == ':' )
134 i_bind_port = atoi( psz_parser );
136 while( *psz_parser && *psz_parser != '/' )
141 if( *psz_parser == '/' )
145 psz_file_name = psz_parser;
148 if( i_bind_port <= 0 )
150 i_bind_port = DEFAULT_PORT;
153 if( !*psz_file_name )
155 psz_file_name = strdup( "/" );
157 else if( *psz_file_name != '/' )
159 char *p = psz_file_name;
161 psz_file_name = malloc( strlen( p ) + 2 );
162 strcpy( psz_file_name, "/" );
163 strcat( psz_file_name, p );
167 psz_file_name = strdup( psz_file_name );
170 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_access), psz_bind_addr,
172 if( p_sys->p_httpd_host == NULL )
174 msg_Err( p_access, "cannot listen on %s:%d",
175 psz_bind_addr, i_bind_port );
178 free( psz_file_name );
183 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
185 psz_mime = strdup( "video/x-ms-asf-stream" );
189 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
190 if( *val.psz_string )
191 psz_mime = val.psz_string;
193 free( val.psz_string );
196 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
197 if( *val.psz_string )
198 psz_user = val.psz_string;
200 free( val.psz_string );
202 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
203 if( *val.psz_string )
204 psz_pwd = val.psz_string;
206 free( val.psz_string );
208 p_sys->p_httpd_stream =
209 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
211 if( psz_user ) free( psz_user );
212 if( psz_pwd ) free( psz_pwd );
213 if( psz_mime ) free( psz_mime );
215 if( p_sys->p_httpd_stream == NULL )
217 msg_Err( p_access, "cannot add stream %s", psz_file_name );
218 httpd_HostDelete( p_sys->p_httpd_host );
221 free( psz_file_name );
226 free( psz_file_name );
229 p_sys->i_header_allocated = 1024;
230 p_sys->i_header_size = 0;
231 p_sys->p_header = malloc( p_sys->i_header_allocated );
232 p_sys->b_header_complete = VLC_FALSE;
234 p_access->pf_write = Write;
235 p_access->pf_seek = Seek;
238 /* update p_sout->i_out_pace_nocontrol */
239 p_access->p_sout->i_out_pace_nocontrol++;
244 /*****************************************************************************
245 * Close: close the target
246 *****************************************************************************/
247 static void Close( vlc_object_t * p_this )
249 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
250 sout_access_out_sys_t *p_sys = p_access->p_sys;
252 /* update p_sout->i_out_pace_nocontrol */
253 p_access->p_sout->i_out_pace_nocontrol--;
255 httpd_StreamDelete( p_sys->p_httpd_stream );
256 httpd_HostDelete( p_sys->p_httpd_host );
258 FREE( p_sys->p_header );
260 msg_Dbg( p_access, "Close" );
265 /*****************************************************************************
267 *****************************************************************************/
268 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
270 sout_access_out_sys_t *p_sys = p_access->p_sys;
277 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
280 if( p_sys->b_header_complete )
282 /* free previously gathered header */
283 p_sys->i_header_size = 0;
284 p_sys->b_header_complete = VLC_FALSE;
286 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
287 p_sys->i_header_allocated )
289 p_sys->i_header_allocated =
290 p_buffer->i_buffer + p_sys->i_header_size + 1024;
292 realloc( p_sys->p_header, p_sys->i_header_allocated );
294 memcpy( &p_sys->p_header[p_sys->i_header_size],
296 p_buffer->i_buffer );
297 p_sys->i_header_size += p_buffer->i_buffer;
299 else if( !p_sys->b_header_complete )
301 p_sys->b_header_complete = VLC_TRUE;
303 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
304 p_sys->i_header_size );
308 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
309 p_buffer->i_buffer );
311 p_next = p_buffer->p_next;
312 block_Release( p_buffer );
323 block_ChainRelease( p_buffer );
326 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
329 /*****************************************************************************
330 * Seek: seek to a specific location in a file
331 *****************************************************************************/
332 static int Seek( sout_access_out_t *p_access, off_t i_pos )
334 msg_Warn( p_access, "HTTP sout access cannot seek" );