]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
* http: Err -> Warn for Seek.
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: http.c,v 1.11 2004/02/03 20:12: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 "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     httpd_t             *p_httpd;
64
65     /* host */
66     httpd_host_t        *p_httpd_host;
67
68     /* stream */
69     httpd_stream_t      *p_httpd_stream;
70
71     /* gather header from stream */
72     int                 i_header_allocated;
73     int                 i_header_size;
74     uint8_t             *p_header;
75     vlc_bool_t          b_header_complete;
76 };
77
78 static struct
79 {
80     char *psz_ext;
81     char *psz_mime;
82 } http_mime[] =
83 {
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" },
103     { NULL,     NULL }
104 };
105
106 static char *GetMime( char *psz_name )
107 {
108     char *psz_ext;
109
110     psz_ext = strrchr( psz_name, '.' );
111     if( psz_ext )
112     {
113         int i;
114
115         for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
116         {
117             if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
118             {
119                 return( http_mime[i].psz_mime );
120             }
121         }
122     }
123     return( "application/octet-stream" );
124 }
125 /*****************************************************************************
126  * Open: open the file
127  *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
129 {
130     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
131     sout_access_out_sys_t   *p_sys;
132
133     char                *psz_parser, *psz_name;
134
135     char                *psz_bind_addr;
136     int                 i_bind_port;
137     char                *psz_file_name;
138
139     char                *psz_mime;
140
141     if( !( p_sys = p_access->p_sys =
142                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
143     {
144         msg_Err( p_access, "Not enough memory" );
145         return( VLC_EGENERIC );
146     }
147
148     /* p_access->psz_name host.name:port/filename */
149     psz_name = psz_parser = strdup( p_access->psz_name );
150
151     psz_bind_addr = psz_parser;
152     i_bind_port = 0;
153     psz_file_name = "";
154
155     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
156     {
157         psz_parser++;
158     }
159     if( *psz_parser == ':' )
160     {
161         *psz_parser = '\0';
162         psz_parser++;
163         i_bind_port = atoi( psz_parser );
164
165         while( *psz_parser && *psz_parser != '/' )
166         {
167             psz_parser++;
168         }
169     }
170     if( *psz_parser == '/' )
171     {
172         *psz_parser = '\0';
173         psz_parser++;
174         psz_file_name = psz_parser;
175     }
176
177     if( i_bind_port <= 0 )
178     {
179         i_bind_port = DEFAULT_PORT;
180     }
181
182     if( !*psz_file_name )
183     {
184         psz_file_name = strdup( "/" );
185     }
186     else if( *psz_file_name != '/' )
187     {
188         char *p = psz_file_name;
189
190         psz_file_name = malloc( strlen( p ) + 2 );
191         strcpy( psz_file_name, "/" );
192         strcat( psz_file_name, p );
193     }
194     else
195     {
196         psz_file_name = strdup( psz_file_name );
197     }
198
199     p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_access), VLC_TRUE );
200     if( !p_sys->p_httpd )
201     {
202         msg_Err( p_access, "cannot start httpd daemon" );
203
204         free( psz_name );
205         free( psz_file_name );
206         free( p_sys );
207         return( VLC_EGENERIC );
208     }
209
210     p_sys->p_httpd_host =
211         p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
212                                           psz_bind_addr, i_bind_port );
213
214     if( !p_sys->p_httpd_host )
215     {
216         msg_Err( p_access, "cannot listen on %s:%d", psz_bind_addr, i_bind_port );
217         httpd_Release( p_sys->p_httpd );
218
219         free( psz_name );
220         free( psz_file_name );
221         free( p_sys );
222         return( VLC_EGENERIC );
223     }
224
225     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
226     {
227         psz_mime = "video/x-ms-asf-stream";
228     }
229     else
230     {
231         psz_mime = GetMime( psz_file_name );
232     }
233     p_sys->p_httpd_stream =
234         p_sys->p_httpd->pf_register_stream( p_sys->p_httpd,
235                                             psz_file_name,
236                                             psz_mime,
237                                             sout_cfg_find_value( p_access->p_cfg, "user" ),
238                                             sout_cfg_find_value( p_access->p_cfg, "pwd" ) );
239
240     if( !p_sys->p_httpd_stream )
241     {
242         msg_Err( p_access, "cannot add stream %s", psz_file_name );
243         p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd, p_sys->p_httpd_host );
244         httpd_Release( p_sys->p_httpd );
245
246         free( psz_name );
247         free( psz_file_name );
248         free( p_sys );
249
250         return( VLC_EGENERIC );
251     }
252
253     free( psz_file_name );
254     free( psz_name );
255
256     p_sys->i_header_allocated = 1024;
257     p_sys->i_header_size      = 0;
258     p_sys->p_header           = malloc( p_sys->i_header_allocated );
259     p_sys->b_header_complete  = VLC_FALSE;
260
261     p_access->pf_write       = Write;
262     p_access->pf_seek        = Seek;
263
264     return VLC_SUCCESS;
265 }
266
267 /*****************************************************************************
268  * Close: close the target
269  *****************************************************************************/
270 static void Close( vlc_object_t * p_this )
271 {
272     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
273     sout_access_out_sys_t   *p_sys = p_access->p_sys;
274
275     p_sys->p_httpd->pf_unregister_stream( p_sys->p_httpd, p_sys->p_httpd_stream );
276     p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd, p_sys->p_httpd_host );
277
278     httpd_Release( p_sys->p_httpd );
279
280     FREE( p_sys->p_header );
281
282     msg_Info( p_access, "Close" );
283
284     free( p_sys );
285 }
286
287 /*****************************************************************************
288  * Write:
289  *****************************************************************************/
290 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
291 {
292     sout_access_out_sys_t   *p_sys = p_access->p_sys;
293     int i_err = 0;
294
295     while( p_buffer )
296     {
297         sout_buffer_t *p_next;
298
299         if( p_buffer->i_flags & SOUT_BUFFER_FLAGS_HEADER )
300         {
301             /* gather header */
302             if( p_sys->b_header_complete )
303             {
304                 /* free previously gathered header */
305                 p_sys->i_header_size = 0;
306                 p_sys->b_header_complete = VLC_FALSE;
307             }
308             if( (int)(p_buffer->i_size + p_sys->i_header_size) > p_sys->i_header_allocated )
309             {
310                 p_sys->i_header_allocated = p_buffer->i_size + p_sys->i_header_size + 1024;
311                 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_allocated );
312             }
313             memcpy( &p_sys->p_header[p_sys->i_header_size],
314                     p_buffer->p_buffer,
315                     p_buffer->i_size );
316             p_sys->i_header_size += p_buffer->i_size;
317         }
318         else if( !p_sys->b_header_complete )
319         {
320             p_sys->b_header_complete = VLC_TRUE;
321
322             p_sys->p_httpd->pf_header_stream( p_sys->p_httpd,
323                                               p_sys->p_httpd_stream,
324                                               p_sys->p_header, p_sys->i_header_size );
325         }
326             /* send data */
327         i_err = p_sys->p_httpd->pf_send_stream( p_sys->p_httpd, p_sys->p_httpd_stream,
328                                                 p_buffer->p_buffer, p_buffer->i_size );
329
330         p_next = p_buffer->p_next;
331         sout_BufferDelete( p_access->p_sout, p_buffer );
332         p_buffer = p_next;
333
334         if( i_err < 0 )
335         {
336             break;
337         }
338     }
339
340     if( i_err < 0 )
341     {
342         sout_buffer_t *p_next;
343         while( p_buffer )
344         {
345             p_next = p_buffer->p_next;
346             sout_BufferDelete( p_access->p_sout, p_buffer );
347             p_buffer = p_next;
348         }
349     }
350
351     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
352 }
353
354 /*****************************************************************************
355  * Seek: seek to a specific location in a file
356  *****************************************************************************/
357 static int Seek( sout_access_out_t *p_access, off_t i_pos )
358 {
359     msg_Warn( p_access, "HTTP sout access cannot seek" );
360     return VLC_EGENERIC;
361 }
362