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 * );
45 set_description( _("HTTP stream ouput") );
46 set_capability( "sout access", 0 );
47 add_shortcut( "http" );
48 add_shortcut( "mmsh" );
49 set_callbacks( Open, Close );
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Write( sout_access_out_t *, block_t * );
57 static int Seek ( sout_access_out_t *, off_t );
59 struct sout_access_out_sys_t
62 httpd_host_t *p_httpd_host;
65 httpd_stream_t *p_httpd_stream;
67 /* gather header from stream */
68 int i_header_allocated;
71 vlc_bool_t b_header_complete;
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
79 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
80 sout_access_out_sys_t *p_sys;
82 char *psz_parser, *psz_name;
88 char *psz_mime = NULL;
90 if( !( p_sys = p_access->p_sys =
91 malloc( sizeof( sout_access_out_sys_t ) ) ) )
93 msg_Err( p_access, "Not enough memory" );
94 return( VLC_EGENERIC );
97 /* p_access->psz_name host.name:port/filename */
98 psz_name = psz_parser = strdup( p_access->psz_name );
100 psz_bind_addr = psz_parser;
104 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
108 if( *psz_parser == ':' )
112 i_bind_port = atoi( psz_parser );
114 while( *psz_parser && *psz_parser != '/' )
119 if( *psz_parser == '/' )
123 psz_file_name = psz_parser;
126 if( i_bind_port <= 0 )
128 i_bind_port = DEFAULT_PORT;
131 if( !*psz_file_name )
133 psz_file_name = strdup( "/" );
135 else if( *psz_file_name != '/' )
137 char *p = psz_file_name;
139 psz_file_name = malloc( strlen( p ) + 2 );
140 strcpy( psz_file_name, "/" );
141 strcat( psz_file_name, p );
145 psz_file_name = strdup( psz_file_name );
148 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_access), psz_bind_addr,
150 if( p_sys->p_httpd_host == NULL )
152 msg_Err( p_access, "cannot listen on %s:%d",
153 psz_bind_addr, i_bind_port );
156 free( psz_file_name );
161 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
163 psz_mime = "video/x-ms-asf-stream";
166 p_sys->p_httpd_stream =
167 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
168 sout_cfg_find_value( p_access->p_cfg, "user" ),
169 sout_cfg_find_value( p_access->p_cfg, "pwd" ) );
170 if( p_sys->p_httpd_stream == NULL )
172 msg_Err( p_access, "cannot add stream %s", psz_file_name );
173 httpd_HostDelete( p_sys->p_httpd_host );
176 free( psz_file_name );
181 free( psz_file_name );
184 p_sys->i_header_allocated = 1024;
185 p_sys->i_header_size = 0;
186 p_sys->p_header = malloc( p_sys->i_header_allocated );
187 p_sys->b_header_complete = VLC_FALSE;
189 p_access->pf_write = Write;
190 p_access->pf_seek = Seek;
193 /* update p_sout->i_out_pace_nocontrol */
194 p_access->p_sout->i_out_pace_nocontrol++;
199 /*****************************************************************************
200 * Close: close the target
201 *****************************************************************************/
202 static void Close( vlc_object_t * p_this )
204 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
205 sout_access_out_sys_t *p_sys = p_access->p_sys;
207 /* update p_sout->i_out_pace_nocontrol */
208 p_access->p_sout->i_out_pace_nocontrol--;
210 httpd_StreamDelete( p_sys->p_httpd_stream );
211 httpd_HostDelete( p_sys->p_httpd_host );
213 FREE( p_sys->p_header );
215 msg_Dbg( p_access, "Close" );
220 /*****************************************************************************
222 *****************************************************************************/
223 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
225 sout_access_out_sys_t *p_sys = p_access->p_sys;
232 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
235 if( p_sys->b_header_complete )
237 /* free previously gathered header */
238 p_sys->i_header_size = 0;
239 p_sys->b_header_complete = VLC_FALSE;
241 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
242 p_sys->i_header_allocated )
244 p_sys->i_header_allocated =
245 p_buffer->i_buffer + p_sys->i_header_size + 1024;
247 realloc( p_sys->p_header, p_sys->i_header_allocated );
249 memcpy( &p_sys->p_header[p_sys->i_header_size],
251 p_buffer->i_buffer );
252 p_sys->i_header_size += p_buffer->i_buffer;
254 else if( !p_sys->b_header_complete )
256 p_sys->b_header_complete = VLC_TRUE;
258 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
259 p_sys->i_header_size );
263 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
264 p_buffer->i_buffer );
266 p_next = p_buffer->p_next;
267 block_Release( p_buffer );
278 block_ChainRelease( p_buffer );
281 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
284 /*****************************************************************************
285 * Seek: seek to a specific location in a file
286 *****************************************************************************/
287 static int Seek( sout_access_out_t *p_access, off_t i_pos )
289 msg_Warn( p_access, "HTTP sout access cannot seek" );