]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
* http: ported to new httpd.
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: http.c,v 1.12 2004/03/03 13:25:53 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/sout.h>
34
35 #include "vlc_httpd.h"
36
37 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
38
39 #define DEFAULT_PORT 8080
40
41 /*****************************************************************************
42  * Exported prototypes
43  *****************************************************************************/
44 static int     Open   ( vlc_object_t * );
45 static void    Close  ( vlc_object_t * );
46
47 static int     Write( sout_access_out_t *, sout_buffer_t * );
48 static int     Seek ( sout_access_out_t *, off_t  );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin();
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 );
59 vlc_module_end();
60
61 struct sout_access_out_sys_t
62 {
63     /* host */
64     httpd_host_t        *p_httpd_host;
65
66     /* stream */
67     httpd_stream_t      *p_httpd_stream;
68
69     /* gather header from stream */
70     int                 i_header_allocated;
71     int                 i_header_size;
72     uint8_t             *p_header;
73     vlc_bool_t          b_header_complete;
74 };
75
76 #if 0
77 static struct
78 {
79     char *psz_ext;
80     char *psz_mime;
81 } http_mime[] =
82 {
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" },
102     { NULL,     NULL }
103 };
104
105 static char *GetMime( char *psz_name )
106 {
107     char *psz_ext;
108
109     psz_ext = strrchr( psz_name, '.' );
110     if( psz_ext )
111     {
112         int i;
113
114         for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
115         {
116             if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
117             {
118                 return( http_mime[i].psz_mime );
119             }
120         }
121     }
122     return( "application/octet-stream" );
123 }
124 #endif
125
126 /*****************************************************************************
127  * Open: open the file
128  *****************************************************************************/
129 static int Open( vlc_object_t *p_this )
130 {
131     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
132     sout_access_out_sys_t   *p_sys;
133
134     char                *psz_parser, *psz_name;
135
136     char                *psz_bind_addr;
137     int                 i_bind_port;
138     char                *psz_file_name;
139
140     char                *psz_mime = NULL;
141
142     if( !( p_sys = p_access->p_sys =
143                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
144     {
145         msg_Err( p_access, "Not enough memory" );
146         return( VLC_EGENERIC );
147     }
148
149     /* p_access->psz_name host.name:port/filename */
150     psz_name = psz_parser = strdup( p_access->psz_name );
151
152     psz_bind_addr = psz_parser;
153     i_bind_port = 0;
154     psz_file_name = "";
155
156     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
157     {
158         psz_parser++;
159     }
160     if( *psz_parser == ':' )
161     {
162         *psz_parser = '\0';
163         psz_parser++;
164         i_bind_port = atoi( psz_parser );
165
166         while( *psz_parser && *psz_parser != '/' )
167         {
168             psz_parser++;
169         }
170     }
171     if( *psz_parser == '/' )
172     {
173         *psz_parser = '\0';
174         psz_parser++;
175         psz_file_name = psz_parser;
176     }
177
178     if( i_bind_port <= 0 )
179     {
180         i_bind_port = DEFAULT_PORT;
181     }
182
183     if( !*psz_file_name )
184     {
185         psz_file_name = strdup( "/" );
186     }
187     else if( *psz_file_name != '/' )
188     {
189         char *p = psz_file_name;
190
191         psz_file_name = malloc( strlen( p ) + 2 );
192         strcpy( psz_file_name, "/" );
193         strcat( psz_file_name, p );
194     }
195     else
196     {
197         psz_file_name = strdup( psz_file_name );
198     }
199
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 )
202     {
203         msg_Err( p_access, "cannot listen on %s:%d", psz_bind_addr, i_bind_port );
204
205         free( psz_name );
206         free( psz_file_name );
207         free( p_sys );
208         return VLC_EGENERIC;
209     }
210
211     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
212     {
213         psz_mime = "video/x-ms-asf-stream";
214     }
215
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 )
221     {
222         msg_Err( p_access, "cannot add stream %s", psz_file_name );
223         httpd_HostDelete( p_sys->p_httpd_host );
224
225         free( psz_name );
226         free( psz_file_name );
227         free( p_sys );
228         return VLC_EGENERIC;
229     }
230
231     free( psz_file_name );
232     free( psz_name );
233
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;
238
239     p_access->pf_write       = Write;
240     p_access->pf_seek        = Seek;
241
242     return VLC_SUCCESS;
243 }
244
245 /*****************************************************************************
246  * Close: close the target
247  *****************************************************************************/
248 static void Close( vlc_object_t * p_this )
249 {
250     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
251     sout_access_out_sys_t   *p_sys = p_access->p_sys;
252
253     httpd_StreamDelete( p_sys->p_httpd_stream );
254     httpd_HostDelete( p_sys->p_httpd_host );
255
256     FREE( p_sys->p_header );
257
258     msg_Dbg( p_access, "Close" );
259
260     free( p_sys );
261 }
262
263 /*****************************************************************************
264  * Write:
265  *****************************************************************************/
266 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
267 {
268     sout_access_out_sys_t   *p_sys = p_access->p_sys;
269     int i_err = 0;
270
271     while( p_buffer )
272     {
273         sout_buffer_t *p_next;
274
275         if( p_buffer->i_flags & SOUT_BUFFER_FLAGS_HEADER )
276         {
277             /* gather header */
278             if( p_sys->b_header_complete )
279             {
280                 /* free previously gathered header */
281                 p_sys->i_header_size = 0;
282                 p_sys->b_header_complete = VLC_FALSE;
283             }
284             if( (int)(p_buffer->i_size + p_sys->i_header_size) > p_sys->i_header_allocated )
285             {
286                 p_sys->i_header_allocated = p_buffer->i_size + p_sys->i_header_size + 1024;
287                 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_allocated );
288             }
289             memcpy( &p_sys->p_header[p_sys->i_header_size],
290                     p_buffer->p_buffer,
291                     p_buffer->i_size );
292             p_sys->i_header_size += p_buffer->i_size;
293         }
294         else if( !p_sys->b_header_complete )
295         {
296             p_sys->b_header_complete = VLC_TRUE;
297
298             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header, p_sys->i_header_size );
299         }
300
301         /* send data */
302         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer, p_buffer->i_size );
303
304         p_next = p_buffer->p_next;
305         sout_BufferDelete( p_access->p_sout, p_buffer );
306         p_buffer = p_next;
307
308         if( i_err < 0 )
309         {
310             break;
311         }
312     }
313
314     if( i_err < 0 )
315     {
316         sout_buffer_t *p_next;
317         while( p_buffer )
318         {
319             p_next = p_buffer->p_next;
320             sout_BufferDelete( p_access->p_sout, p_buffer );
321             p_buffer = p_next;
322         }
323     }
324
325     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
326 }
327
328 /*****************************************************************************
329  * Seek: seek to a specific location in a file
330  *****************************************************************************/
331 static int Seek( sout_access_out_t *p_access, off_t i_pos )
332 {
333     msg_Warn( p_access, "HTTP sout access cannot seek" );
334     return VLC_EGENERIC;
335 }
336