1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: http.c,v 1.5 2003/08/03 16:36:20 bigben Exp $
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/input.h>
37 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
39 #define DEFAULT_PORT 8080
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
47 static int Write( sout_access_out_t *, sout_buffer_t * );
48 static int Seek ( sout_access_out_t *, off_t );
50 /*****************************************************************************
52 *****************************************************************************/
54 set_description( _("HTTP stream ouput") );
55 set_capability( "sout access", 0 );
56 add_shortcut( "http" );
57 set_callbacks( Open, Close );
60 struct sout_access_out_sys_t
65 httpd_host_t *p_httpd_host;
68 httpd_stream_t *p_httpd_stream;
70 /* gather header from stream */
71 int i_header_allocated;
74 vlc_bool_t b_header_complete;
83 { ".avi", "video/avi" },
84 { ".asf", "video/x-ms-asf" },
85 { ".m1a", "audio/mpeg" },
86 { ".m2a", "audio/mpeg" },
87 { ".m1v", "video/mpeg" },
88 { ".m2v", "video/mpeg" },
89 { ".mp2", "audio/mpeg" },
90 { ".mp3", "audio/mpeg" },
91 { ".mpa", "audio/mpeg" },
92 { ".mpg", "video/mpeg" },
93 { ".mpeg", "video/mpeg" },
94 { ".mpe", "video/mpeg" },
95 { ".mov", "video/quicktime" },
96 { ".moov", "video/quicktime" },
97 { ".ogg", "application/ogg" },
98 { ".ogm", "application/ogg" },
99 { ".wav", "audio/wav" },
103 static char *GetMime( char *psz_name )
107 psz_ext = strrchr( psz_name, '.' );
112 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
114 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
116 return( http_mime[i].psz_mime );
120 return( "application/octet-stream" );
122 /*****************************************************************************
123 * Open: open the file
124 *****************************************************************************/
125 static int Open( vlc_object_t *p_this )
127 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
128 sout_access_out_sys_t *p_sys;
130 char *psz_parser, *psz_name;
136 if( !( p_sys = p_access->p_sys =
137 malloc( sizeof( sout_access_out_sys_t ) ) ) )
139 msg_Err( p_access, "Not enough memory" );
140 return( VLC_EGENERIC );
143 /* *** parse p_access->psz_name to extract bind address, port and file name *** */
144 /* p_access->psz_name host.name:port/filename */
145 psz_name = psz_parser = strdup( p_access->psz_name );
147 psz_bind_addr = psz_parser;
151 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
155 if( *psz_parser == ':' )
159 i_bind_port = atoi( psz_parser );
161 while( *psz_parser && *psz_parser != '/' )
166 if( *psz_parser == '/' )
170 psz_file_name = psz_parser;
173 if( i_bind_port <= 0 )
175 i_bind_port = DEFAULT_PORT;
178 if( !*psz_file_name )
180 psz_file_name = strdup( "/" );
182 else if( *psz_file_name != '/' )
184 char *p = psz_file_name;
186 psz_file_name = malloc( strlen( p ) + 2 );
187 strcpy( psz_file_name, "/" );
188 strcat( psz_file_name, p );
191 p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_access), VLC_TRUE );
192 if( !p_sys->p_httpd )
194 msg_Err( p_access, "cannot start httpd daemon" );
197 free( psz_file_name );
199 return( VLC_EGENERIC );
202 p_sys->p_httpd_host =
203 p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
204 psz_bind_addr, i_bind_port );
206 if( !p_sys->p_httpd_host )
208 msg_Err( p_access, "cannot listen on %s:%d", psz_bind_addr, i_bind_port );
209 httpd_Release( p_sys->p_httpd );
212 free( psz_file_name );
214 return( VLC_EGENERIC );
217 p_sys->p_httpd_stream =
218 p_sys->p_httpd->pf_register_stream( p_sys->p_httpd,
220 GetMime( psz_file_name ),
221 sout_cfg_find_value( p_access->p_cfg, "user" ),
222 sout_cfg_find_value( p_access->p_cfg, "pwd" ) );
224 if( !p_sys->p_httpd_stream )
226 msg_Err( p_access, "cannot add stream %s", psz_file_name );
227 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd, p_sys->p_httpd_host );
228 httpd_Release( p_sys->p_httpd );
231 free( psz_file_name );
234 return( VLC_EGENERIC );
237 p_sys->i_header_allocated = 1024;
238 p_sys->i_header_size = 0;
239 p_sys->p_header = malloc( p_sys->i_header_allocated );
240 p_sys->b_header_complete = VLC_FALSE;
242 p_access->pf_write = Write;
243 p_access->pf_seek = Seek;
248 /*****************************************************************************
249 * Close: close the target
250 *****************************************************************************/
251 static void Close( vlc_object_t * p_this )
253 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
254 sout_access_out_sys_t *p_sys = p_access->p_sys;
256 p_sys->p_httpd->pf_unregister_stream( p_sys->p_httpd, p_sys->p_httpd_stream );
257 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd, p_sys->p_httpd_host );
259 httpd_Release( p_sys->p_httpd );
261 FREE( p_sys->p_header );
263 msg_Info( p_access, "Close" );
268 /*****************************************************************************
270 *****************************************************************************/
271 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
273 sout_access_out_sys_t *p_sys = p_access->p_sys;
278 sout_buffer_t *p_next;
280 if( p_buffer->i_flags & SOUT_BUFFER_FLAGS_HEADER )
283 if( p_sys->b_header_complete )
285 /* free previously gathered header */
286 p_sys->i_header_size = 0;
287 p_sys->b_header_complete = VLC_FALSE;
289 if( p_buffer->i_size + p_sys->i_header_size > p_sys->i_header_allocated )
291 p_sys->i_header_allocated = p_buffer->i_size + p_sys->i_header_size + 1024;
292 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_allocated );
294 memcpy( &p_sys->p_header[p_sys->i_header_size],
297 p_sys->i_header_size += p_buffer->i_size;
299 else if( !p_sys->b_header_complete )
301 p_sys->b_header_complete = VLC_TRUE;
303 p_sys->p_httpd->pf_header_stream( p_sys->p_httpd,
304 p_sys->p_httpd_stream,
305 p_sys->p_header, p_sys->i_header_size );
308 i_err = p_sys->p_httpd->pf_send_stream( p_sys->p_httpd, p_sys->p_httpd_stream,
309 p_buffer->p_buffer, p_buffer->i_size );
311 p_next = p_buffer->p_next;
312 sout_BufferDelete( p_access->p_sout, p_buffer );
323 sout_buffer_t *p_next;
326 p_next = p_buffer->p_next;
327 sout_BufferDelete( p_access->p_sout, p_buffer );
332 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
335 /*****************************************************************************
336 * Seek: seek to a specific location in a file
337 *****************************************************************************/
338 static int Seek( sout_access_out_t *p_access, off_t i_pos )
340 msg_Err( p_access, "http sout access cannot seek" );
341 return( VLC_EGENERIC );