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-"
47 set_description( _("HTTP stream ouput") );
48 set_capability( "sout access", 0 );
49 add_shortcut( "http" );
50 add_shortcut( "mmsh" );
51 add_string( SOUT_CFG_PREFIX "user", "", NULL, "User", "", VLC_TRUE );
52 add_string( SOUT_CFG_PREFIX "pwd", "", NULL, "Password", "", VLC_TRUE );
53 set_callbacks( Open, Close );
57 /*****************************************************************************
59 *****************************************************************************/
60 static const char *ppsz_sout_options[] = {
64 static int Write( sout_access_out_t *, block_t * );
65 static int Seek ( sout_access_out_t *, off_t );
67 struct sout_access_out_sys_t
70 httpd_host_t *p_httpd_host;
73 httpd_stream_t *p_httpd_stream;
75 /* gather header from stream */
76 int i_header_allocated;
79 vlc_bool_t b_header_complete;
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
87 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
88 sout_access_out_sys_t *p_sys;
90 char *psz_parser, *psz_name;
95 char *psz_user = NULL;
97 char *psz_mime = NULL;
100 if( !( p_sys = p_access->p_sys =
101 malloc( sizeof( sout_access_out_sys_t ) ) ) )
103 msg_Err( p_access, "Not enough memory" );
104 return( VLC_EGENERIC );
107 sout_ParseCfg( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
109 /* p_access->psz_name host.name:port/filename */
110 psz_name = psz_parser = strdup( p_access->psz_name );
112 psz_bind_addr = psz_parser;
116 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
120 if( *psz_parser == ':' )
124 i_bind_port = atoi( psz_parser );
126 while( *psz_parser && *psz_parser != '/' )
131 if( *psz_parser == '/' )
135 psz_file_name = psz_parser;
138 if( i_bind_port <= 0 )
140 i_bind_port = DEFAULT_PORT;
143 if( !*psz_file_name )
145 psz_file_name = strdup( "/" );
147 else if( *psz_file_name != '/' )
149 char *p = psz_file_name;
151 psz_file_name = malloc( strlen( p ) + 2 );
152 strcpy( psz_file_name, "/" );
153 strcat( psz_file_name, p );
157 psz_file_name = strdup( psz_file_name );
160 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_access), psz_bind_addr,
162 if( p_sys->p_httpd_host == NULL )
164 msg_Err( p_access, "cannot listen on %s:%d",
165 psz_bind_addr, i_bind_port );
168 free( psz_file_name );
173 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
175 psz_mime = "video/x-ms-asf-stream";
178 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
179 if( val.psz_string && *val.psz_string )
180 psz_user = val.psz_string;
181 else if( val.psz_string )
182 free( val.psz_string );
184 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
185 if( val.psz_string && *val.psz_string )
186 psz_pwd = val.psz_string;
187 else if( val.psz_string )
188 free( val.psz_string );
190 p_sys->p_httpd_stream =
191 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
193 if( psz_user ) free( psz_user );
194 if( psz_pwd ) free( psz_pwd );
196 if( p_sys->p_httpd_stream == NULL )
198 msg_Err( p_access, "cannot add stream %s", psz_file_name );
199 httpd_HostDelete( p_sys->p_httpd_host );
202 free( psz_file_name );
207 free( psz_file_name );
210 p_sys->i_header_allocated = 1024;
211 p_sys->i_header_size = 0;
212 p_sys->p_header = malloc( p_sys->i_header_allocated );
213 p_sys->b_header_complete = VLC_FALSE;
215 p_access->pf_write = Write;
216 p_access->pf_seek = Seek;
219 /* update p_sout->i_out_pace_nocontrol */
220 p_access->p_sout->i_out_pace_nocontrol++;
225 /*****************************************************************************
226 * Close: close the target
227 *****************************************************************************/
228 static void Close( vlc_object_t * p_this )
230 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
231 sout_access_out_sys_t *p_sys = p_access->p_sys;
233 /* update p_sout->i_out_pace_nocontrol */
234 p_access->p_sout->i_out_pace_nocontrol--;
236 httpd_StreamDelete( p_sys->p_httpd_stream );
237 httpd_HostDelete( p_sys->p_httpd_host );
239 FREE( p_sys->p_header );
241 msg_Dbg( p_access, "Close" );
246 /*****************************************************************************
248 *****************************************************************************/
249 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
251 sout_access_out_sys_t *p_sys = p_access->p_sys;
258 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
261 if( p_sys->b_header_complete )
263 /* free previously gathered header */
264 p_sys->i_header_size = 0;
265 p_sys->b_header_complete = VLC_FALSE;
267 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
268 p_sys->i_header_allocated )
270 p_sys->i_header_allocated =
271 p_buffer->i_buffer + p_sys->i_header_size + 1024;
273 realloc( p_sys->p_header, p_sys->i_header_allocated );
275 memcpy( &p_sys->p_header[p_sys->i_header_size],
277 p_buffer->i_buffer );
278 p_sys->i_header_size += p_buffer->i_buffer;
280 else if( !p_sys->b_header_complete )
282 p_sys->b_header_complete = VLC_TRUE;
284 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
285 p_sys->i_header_size );
289 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
290 p_buffer->i_buffer );
292 p_next = p_buffer->p_next;
293 block_Release( p_buffer );
304 block_ChainRelease( p_buffer );
307 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
310 /*****************************************************************************
311 * Seek: seek to a specific location in a file
312 *****************************************************************************/
313 static int Seek( sout_access_out_t *p_access, off_t i_pos )
315 msg_Warn( p_access, "HTTP sout access cannot seek" );