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/input.h>
35 #include "vlc_httpd.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
64 httpd_host_t *p_httpd_host;
67 httpd_stream_t *p_httpd_stream;
69 /* gather header from stream */
70 int i_header_allocated;
73 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 { ".wma", "audio/x-ms-wma" },
100 { ".wmv", "video/x-ms-wmv" },
101 { ".wav", "audio/wav" },
105 static char *GetMime( char *psz_name )
109 psz_ext = strrchr( psz_name, '.' );
114 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
116 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
118 return( http_mime[i].psz_mime );
122 return( "application/octet-stream" );
126 /*****************************************************************************
127 * Open: open the file
128 *****************************************************************************/
129 static int Open( vlc_object_t *p_this )
131 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
132 sout_access_out_sys_t *p_sys;
134 char *psz_parser, *psz_name;
140 char *psz_mime = NULL;
142 if( !( p_sys = p_access->p_sys =
143 malloc( sizeof( sout_access_out_sys_t ) ) ) )
145 msg_Err( p_access, "Not enough memory" );
146 return( VLC_EGENERIC );
149 /* p_access->psz_name host.name:port/filename */
150 psz_name = psz_parser = strdup( p_access->psz_name );
152 psz_bind_addr = psz_parser;
156 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
160 if( *psz_parser == ':' )
164 i_bind_port = atoi( psz_parser );
166 while( *psz_parser && *psz_parser != '/' )
171 if( *psz_parser == '/' )
175 psz_file_name = psz_parser;
178 if( i_bind_port <= 0 )
180 i_bind_port = DEFAULT_PORT;
183 if( !*psz_file_name )
185 psz_file_name = strdup( "/" );
187 else if( *psz_file_name != '/' )
189 char *p = psz_file_name;
191 psz_file_name = malloc( strlen( p ) + 2 );
192 strcpy( psz_file_name, "/" );
193 strcat( psz_file_name, p );
197 psz_file_name = strdup( psz_file_name );
200 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_access), psz_bind_addr, i_bind_port );
201 if( p_sys->p_httpd_host == NULL )
203 msg_Err( p_access, "cannot listen on %s:%d", psz_bind_addr, i_bind_port );
206 free( psz_file_name );
211 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
213 psz_mime = "video/x-ms-asf-stream";
216 p_sys->p_httpd_stream = httpd_StreamNew( p_sys->p_httpd_host,
217 psz_file_name, psz_mime,
218 sout_cfg_find_value( p_access->p_cfg, "user" ),
219 sout_cfg_find_value( p_access->p_cfg, "pwd" ) );
220 if( p_sys->p_httpd_stream == NULL )
222 msg_Err( p_access, "cannot add stream %s", psz_file_name );
223 httpd_HostDelete( p_sys->p_httpd_host );
226 free( psz_file_name );
231 free( psz_file_name );
234 p_sys->i_header_allocated = 1024;
235 p_sys->i_header_size = 0;
236 p_sys->p_header = malloc( p_sys->i_header_allocated );
237 p_sys->b_header_complete = VLC_FALSE;
239 p_access->pf_write = Write;
240 p_access->pf_seek = Seek;
243 /* update p_sout->i_out_pace_nocontrol */
244 p_access->p_sout->i_out_pace_nocontrol++;
249 /*****************************************************************************
250 * Close: close the target
251 *****************************************************************************/
252 static void Close( vlc_object_t * p_this )
254 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
255 sout_access_out_sys_t *p_sys = p_access->p_sys;
257 /* update p_sout->i_out_pace_nocontrol */
258 p_access->p_sout->i_out_pace_nocontrol--;
260 httpd_StreamDelete( p_sys->p_httpd_stream );
261 httpd_HostDelete( p_sys->p_httpd_host );
263 FREE( p_sys->p_header );
265 msg_Dbg( p_access, "Close" );
270 /*****************************************************************************
272 *****************************************************************************/
273 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
275 sout_access_out_sys_t *p_sys = p_access->p_sys;
280 sout_buffer_t *p_next;
282 if( p_buffer->i_flags & SOUT_BUFFER_FLAGS_HEADER )
285 if( p_sys->b_header_complete )
287 /* free previously gathered header */
288 p_sys->i_header_size = 0;
289 p_sys->b_header_complete = VLC_FALSE;
291 if( (int)(p_buffer->i_size + p_sys->i_header_size) > p_sys->i_header_allocated )
293 p_sys->i_header_allocated = p_buffer->i_size + p_sys->i_header_size + 1024;
294 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_allocated );
296 memcpy( &p_sys->p_header[p_sys->i_header_size],
299 p_sys->i_header_size += p_buffer->i_size;
301 else if( !p_sys->b_header_complete )
303 p_sys->b_header_complete = VLC_TRUE;
305 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header, p_sys->i_header_size );
309 i_err = httpd_StreamSend( p_sys->p_httpd_stream, 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_Warn( p_access, "HTTP sout access cannot seek" );