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." )
55 set_description( _("HTTP stream ouput") );
56 set_capability( "sout access", 0 );
57 add_shortcut( "http" );
58 add_shortcut( "mmsh" );
59 add_string( SOUT_CFG_PREFIX "user", "", NULL, "User", "", VLC_TRUE );
60 add_string( SOUT_CFG_PREFIX "pwd", "", NULL, "Password", "", VLC_TRUE );
61 set_callbacks( Open, Close );
65 /*****************************************************************************
67 *****************************************************************************/
68 static const char *ppsz_sout_options[] = {
72 static int Write( sout_access_out_t *, block_t * );
73 static int Seek ( sout_access_out_t *, off_t );
75 struct sout_access_out_sys_t
78 httpd_host_t *p_httpd_host;
81 httpd_stream_t *p_httpd_stream;
83 /* gather header from stream */
84 int i_header_allocated;
87 vlc_bool_t b_header_complete;
90 /*****************************************************************************
92 *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
95 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
96 sout_access_out_sys_t *p_sys;
98 char *psz_parser, *psz_name;
103 char *psz_user = NULL;
104 char *psz_pwd = NULL;
105 char *psz_mime = NULL;
108 if( !( p_sys = p_access->p_sys =
109 malloc( sizeof( sout_access_out_sys_t ) ) ) )
111 msg_Err( p_access, "Not enough memory" );
112 return( VLC_EGENERIC );
115 sout_ParseCfg( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
117 /* p_access->psz_name host.name:port/filename */
118 psz_name = psz_parser = strdup( p_access->psz_name );
120 psz_bind_addr = psz_parser;
124 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
128 if( *psz_parser == ':' )
132 i_bind_port = atoi( psz_parser );
134 while( *psz_parser && *psz_parser != '/' )
139 if( *psz_parser == '/' )
143 psz_file_name = psz_parser;
146 if( i_bind_port <= 0 )
148 i_bind_port = DEFAULT_PORT;
151 if( !*psz_file_name )
153 psz_file_name = strdup( "/" );
155 else if( *psz_file_name != '/' )
157 char *p = psz_file_name;
159 psz_file_name = malloc( strlen( p ) + 2 );
160 strcpy( psz_file_name, "/" );
161 strcat( psz_file_name, p );
165 psz_file_name = strdup( psz_file_name );
168 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_access), psz_bind_addr,
170 if( p_sys->p_httpd_host == NULL )
172 msg_Err( p_access, "cannot listen on %s:%d",
173 psz_bind_addr, i_bind_port );
176 free( psz_file_name );
181 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
183 psz_mime = "video/x-ms-asf-stream";
186 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
187 if( val.psz_string && *val.psz_string )
188 psz_user = val.psz_string;
189 else if( val.psz_string )
190 free( val.psz_string );
192 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
193 if( val.psz_string && *val.psz_string )
194 psz_pwd = val.psz_string;
195 else if( val.psz_string )
196 free( val.psz_string );
198 p_sys->p_httpd_stream =
199 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
201 if( psz_user ) free( psz_user );
202 if( psz_pwd ) free( psz_pwd );
204 if( p_sys->p_httpd_stream == NULL )
206 msg_Err( p_access, "cannot add stream %s", psz_file_name );
207 httpd_HostDelete( p_sys->p_httpd_host );
210 free( psz_file_name );
215 free( psz_file_name );
218 p_sys->i_header_allocated = 1024;
219 p_sys->i_header_size = 0;
220 p_sys->p_header = malloc( p_sys->i_header_allocated );
221 p_sys->b_header_complete = VLC_FALSE;
223 p_access->pf_write = Write;
224 p_access->pf_seek = Seek;
227 /* update p_sout->i_out_pace_nocontrol */
228 p_access->p_sout->i_out_pace_nocontrol++;
233 /*****************************************************************************
234 * Close: close the target
235 *****************************************************************************/
236 static void Close( vlc_object_t * p_this )
238 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
239 sout_access_out_sys_t *p_sys = p_access->p_sys;
241 /* update p_sout->i_out_pace_nocontrol */
242 p_access->p_sout->i_out_pace_nocontrol--;
244 httpd_StreamDelete( p_sys->p_httpd_stream );
245 httpd_HostDelete( p_sys->p_httpd_host );
247 FREE( p_sys->p_header );
249 msg_Dbg( p_access, "Close" );
254 /*****************************************************************************
256 *****************************************************************************/
257 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
259 sout_access_out_sys_t *p_sys = p_access->p_sys;
266 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
269 if( p_sys->b_header_complete )
271 /* free previously gathered header */
272 p_sys->i_header_size = 0;
273 p_sys->b_header_complete = VLC_FALSE;
275 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
276 p_sys->i_header_allocated )
278 p_sys->i_header_allocated =
279 p_buffer->i_buffer + p_sys->i_header_size + 1024;
281 realloc( p_sys->p_header, p_sys->i_header_allocated );
283 memcpy( &p_sys->p_header[p_sys->i_header_size],
285 p_buffer->i_buffer );
286 p_sys->i_header_size += p_buffer->i_buffer;
288 else if( !p_sys->b_header_complete )
290 p_sys->b_header_complete = VLC_TRUE;
292 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
293 p_sys->i_header_size );
297 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
298 p_buffer->i_buffer );
300 p_next = p_buffer->p_next;
301 block_Release( p_buffer );
312 block_ChainRelease( p_buffer );
315 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
318 /*****************************************************************************
319 * Seek: seek to a specific location in a file
320 *****************************************************************************/
321 static int Seek( sout_access_out_t *p_access, off_t i_pos )
323 msg_Warn( p_access, "HTTP sout access cannot seek" );