1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: http.c,v 1.8 2003/12/06 22:50:08 jpsaman 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 add_shortcut( "mmsh" );
58 set_callbacks( Open, Close );
61 struct sout_access_out_sys_t
66 httpd_host_t *p_httpd_host;
69 httpd_stream_t *p_httpd_stream;
71 /* gather header from stream */
72 int i_header_allocated;
75 vlc_bool_t b_header_complete;
84 { ".avi", "video/avi" },
85 { ".asf", "video/x-ms-asf" },
86 { ".m1a", "audio/mpeg" },
87 { ".m2a", "audio/mpeg" },
88 { ".m1v", "video/mpeg" },
89 { ".m2v", "video/mpeg" },
90 { ".mp2", "audio/mpeg" },
91 { ".mp3", "audio/mpeg" },
92 { ".mpa", "audio/mpeg" },
93 { ".mpg", "video/mpeg" },
94 { ".mpeg", "video/mpeg" },
95 { ".mpe", "video/mpeg" },
96 { ".mov", "video/quicktime" },
97 { ".moov", "video/quicktime" },
98 { ".ogg", "application/ogg" },
99 { ".ogm", "application/ogg" },
100 { ".wma", "audio/x-ms-wma" },
101 { ".wmv", "video/x-ms-wmv" },
102 { ".wav", "audio/wav" },
106 static char *GetMime( char *psz_name )
110 psz_ext = strrchr( psz_name, '.' );
115 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
117 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
119 return( http_mime[i].psz_mime );
123 return( "application/octet-stream" );
125 /*****************************************************************************
126 * Open: open the file
127 *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
130 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
131 sout_access_out_sys_t *p_sys;
133 char *psz_parser, *psz_name;
141 if( !( p_sys = p_access->p_sys =
142 malloc( sizeof( sout_access_out_sys_t ) ) ) )
144 msg_Err( p_access, "Not enough memory" );
145 return( VLC_EGENERIC );
148 /* p_access->psz_name host.name:port/filename */
149 psz_name = psz_parser = strdup( p_access->psz_name );
151 psz_bind_addr = psz_parser;
155 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
159 if( *psz_parser == ':' )
163 i_bind_port = atoi( psz_parser );
165 while( *psz_parser && *psz_parser != '/' )
170 if( *psz_parser == '/' )
174 psz_file_name = psz_parser;
177 if( i_bind_port <= 0 )
179 i_bind_port = DEFAULT_PORT;
182 if( !*psz_file_name )
184 psz_file_name = strdup( "/" );
186 else if( *psz_file_name != '/' )
188 char *p = psz_file_name;
190 psz_file_name = malloc( strlen( p ) + 2 );
191 strcpy( psz_file_name, "/" );
192 strcat( psz_file_name, p );
195 p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_access), VLC_TRUE );
196 if( !p_sys->p_httpd )
198 msg_Err( p_access, "cannot start httpd daemon" );
201 free( psz_file_name );
203 return( VLC_EGENERIC );
206 p_sys->p_httpd_host =
207 p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
208 psz_bind_addr, i_bind_port );
210 if( !p_sys->p_httpd_host )
212 msg_Err( p_access, "cannot listen on %s:%d", psz_bind_addr, i_bind_port );
213 httpd_Release( p_sys->p_httpd );
216 free( psz_file_name );
218 return( VLC_EGENERIC );
221 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
223 psz_mime = "video/x-ms-asf-stream";
227 psz_mime = GetMime( psz_file_name );
229 p_sys->p_httpd_stream =
230 p_sys->p_httpd->pf_register_stream( p_sys->p_httpd,
233 sout_cfg_find_value( p_access->p_cfg, "user" ),
234 sout_cfg_find_value( p_access->p_cfg, "pwd" ) );
236 if( !p_sys->p_httpd_stream )
238 msg_Err( p_access, "cannot add stream %s", psz_file_name );
239 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd, p_sys->p_httpd_host );
240 httpd_Release( p_sys->p_httpd );
243 free( psz_file_name );
246 return( VLC_EGENERIC );
249 p_sys->i_header_allocated = 1024;
250 p_sys->i_header_size = 0;
251 p_sys->p_header = malloc( p_sys->i_header_allocated );
252 p_sys->b_header_complete = VLC_FALSE;
254 p_access->pf_write = Write;
255 p_access->pf_seek = Seek;
260 /*****************************************************************************
261 * Close: close the target
262 *****************************************************************************/
263 static void Close( vlc_object_t * p_this )
265 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
266 sout_access_out_sys_t *p_sys = p_access->p_sys;
268 p_sys->p_httpd->pf_unregister_stream( p_sys->p_httpd, p_sys->p_httpd_stream );
269 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd, p_sys->p_httpd_host );
271 httpd_Release( p_sys->p_httpd );
273 FREE( p_sys->p_header );
275 msg_Info( p_access, "Close" );
280 /*****************************************************************************
282 *****************************************************************************/
283 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
285 sout_access_out_sys_t *p_sys = p_access->p_sys;
290 sout_buffer_t *p_next;
292 if( p_buffer->i_flags & SOUT_BUFFER_FLAGS_HEADER )
295 if( p_sys->b_header_complete )
297 /* free previously gathered header */
298 p_sys->i_header_size = 0;
299 p_sys->b_header_complete = VLC_FALSE;
301 if( (int)(p_buffer->i_size + p_sys->i_header_size) > p_sys->i_header_allocated )
303 p_sys->i_header_allocated = p_buffer->i_size + p_sys->i_header_size + 1024;
304 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_allocated );
306 memcpy( &p_sys->p_header[p_sys->i_header_size],
309 p_sys->i_header_size += p_buffer->i_size;
311 else if( !p_sys->b_header_complete )
313 p_sys->b_header_complete = VLC_TRUE;
315 p_sys->p_httpd->pf_header_stream( p_sys->p_httpd,
316 p_sys->p_httpd_stream,
317 p_sys->p_header, p_sys->i_header_size );
320 i_err = p_sys->p_httpd->pf_send_stream( p_sys->p_httpd, p_sys->p_httpd_stream,
321 p_buffer->p_buffer, p_buffer->i_size );
323 p_next = p_buffer->p_next;
324 sout_BufferDelete( p_access->p_sout, p_buffer );
335 sout_buffer_t *p_next;
338 p_next = p_buffer->p_next;
339 sout_BufferDelete( p_access->p_sout, p_buffer );
344 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
347 /*****************************************************************************
348 * Seek: seek to a specific location in a file
349 *****************************************************************************/
350 static int Seek( sout_access_out_t *p_access, off_t i_pos )
352 msg_Err( p_access, "http sout access cannot seek" );
353 return( VLC_EGENERIC );