]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
* modules/access_out/http.c
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: http.c,v 1.4 2003/07/31 19:10:14 bigben 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     set_callbacks( Open, Close );
58     add_string( "sout-http-user", NULL, NULL, "HTTP Stream Ouput user name", "User name for Basic Authentification on HTTP Stream Output module", VLC_TRUE );
59     add_string( "sout-http-pwd", NULL, NULL, "HTTP Stream Ouput password", "Password for Basic Authentification on HTTP Stream Output module", VLC_TRUE );
60 vlc_module_end();
61
62 struct sout_access_out_sys_t
63 {
64     httpd_t             *p_httpd;
65
66     /* host */
67     httpd_host_t        *p_httpd_host;
68
69     /* stream */
70     httpd_stream_t      *p_httpd_stream;
71
72     /* gather header from stream */
73     int                 i_header_allocated;
74     int                 i_header_size;
75     uint8_t             *p_header;
76     vlc_bool_t          b_header_complete;
77 };
78
79 static struct
80 {
81     char *psz_ext;
82     char *psz_mime;
83 } http_mime[] =
84 {
85     { ".avi",   "video/avi" },
86     { ".asf",   "video/x-ms-asf" },
87     { ".m1a",   "audio/mpeg" },
88     { ".m2a",   "audio/mpeg" },
89     { ".m1v",   "video/mpeg" },
90     { ".m2v",   "video/mpeg" },
91     { ".mp2",   "audio/mpeg" },
92     { ".mp3",   "audio/mpeg" },
93     { ".mpa",   "audio/mpeg" },
94     { ".mpg",   "video/mpeg" },
95     { ".mpeg",  "video/mpeg" },
96     { ".mpe",   "video/mpeg" },
97     { ".mov",   "video/quicktime" },
98     { ".moov",  "video/quicktime" },
99     { ".ogg",   "application/ogg" },
100     { ".ogm",   "application/ogg" },
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 /*****************************************************************************
125  * Open: open the file
126  *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
128 {
129     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
130     sout_access_out_sys_t   *p_sys;
131
132     char                *psz_parser, *psz_name;
133
134     char                *psz_bind_addr;
135     int                 i_bind_port;
136     char                *psz_file_name;
137
138     if( !( p_sys = p_access->p_sys =
139                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
140     {
141         msg_Err( p_access, "Not enough memory" );
142         return( VLC_EGENERIC );
143     }
144
145     /* *** parse p_access->psz_name to extract bind address, port and file name *** */
146     /* p_access->psz_name host.name:port/filename */
147     psz_name = psz_parser = strdup( p_access->psz_name );
148
149     psz_bind_addr = psz_parser;
150     i_bind_port = 0;
151     psz_file_name = "";
152
153     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
154     {
155         psz_parser++;
156     }
157     if( *psz_parser == ':' )
158     {
159         *psz_parser = '\0';
160         psz_parser++;
161         i_bind_port = atoi( psz_parser );
162
163         while( *psz_parser && *psz_parser != '/' )
164         {
165             psz_parser++;
166         }
167     }
168     if( *psz_parser == '/' )
169     {
170         *psz_parser = '\0';
171         psz_parser++;
172         psz_file_name = psz_parser;
173     }
174
175     if( i_bind_port <= 0 )
176     {
177         i_bind_port = DEFAULT_PORT;
178     }
179
180     if( !*psz_file_name )
181     {
182         psz_file_name = strdup( "/" );
183     }
184     else if( *psz_file_name != '/' )
185     {
186         char *p = psz_file_name;
187
188         psz_file_name = malloc( strlen( p ) + 2 );
189         strcpy( psz_file_name, "/" );
190         strcat( psz_file_name, p );
191     }
192
193     p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_access), VLC_TRUE );
194     if( !p_sys->p_httpd )
195     {
196         msg_Err( p_access, "cannot start httpd daemon" );
197
198         free( psz_name );
199         free( psz_file_name );
200         free( p_sys );
201         return( VLC_EGENERIC );
202     }
203
204     p_sys->p_httpd_host =
205         p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
206                                           psz_bind_addr, i_bind_port );
207
208     if( !p_sys->p_httpd_host )
209     {
210         msg_Err( p_access, "cannot listen on %s:%d", psz_bind_addr, i_bind_port );
211         httpd_Release( p_sys->p_httpd );
212
213         free( psz_name );
214         free( psz_file_name );
215         free( p_sys );
216         return( VLC_EGENERIC );
217     }
218
219     p_sys->p_httpd_stream =
220         p_sys->p_httpd->pf_register_stream( p_sys->p_httpd,
221                                             psz_file_name,
222                                             GetMime( psz_file_name ),
223                                             config_GetPsz( p_access, "sout-http-user" ), 
224                                             config_GetPsz( p_access, "sout-http-pwd" ) );
225
226     if( !p_sys->p_httpd_stream )
227     {
228         msg_Err( p_access, "cannot add stream %s", psz_file_name );
229         p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd, p_sys->p_httpd_host );
230         httpd_Release( p_sys->p_httpd );
231
232         free( psz_name );
233         free( psz_file_name );
234         free( p_sys );
235
236         return( VLC_EGENERIC );
237     }
238
239     p_sys->i_header_allocated = 1024;
240     p_sys->i_header_size      = 0;
241     p_sys->p_header           = malloc( p_sys->i_header_allocated );
242     p_sys->b_header_complete  = VLC_FALSE;
243
244     p_access->pf_write       = Write;
245     p_access->pf_seek        = Seek;
246
247     return VLC_SUCCESS;
248 }
249
250 /*****************************************************************************
251  * Close: close the target
252  *****************************************************************************/
253 static void Close( vlc_object_t * p_this )
254 {
255     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
256     sout_access_out_sys_t   *p_sys = p_access->p_sys;
257
258     p_sys->p_httpd->pf_unregister_stream( p_sys->p_httpd, p_sys->p_httpd_stream );
259     p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd, p_sys->p_httpd_host );
260
261     httpd_Release( p_sys->p_httpd );
262
263     FREE( p_sys->p_header );
264
265     msg_Info( p_access, "Close" );
266
267     free( p_sys );
268 }
269
270 /*****************************************************************************
271  * Write:
272  *****************************************************************************/
273 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
274 {
275     sout_access_out_sys_t   *p_sys = p_access->p_sys;
276     int i_err = 0;
277
278     while( p_buffer )
279     {
280         sout_buffer_t *p_next;
281
282         if( p_buffer->i_flags & SOUT_BUFFER_FLAGS_HEADER )
283         {
284             /* gather header */
285             if( p_sys->b_header_complete )
286             {
287                 /* free previously gathered header */
288                 p_sys->i_header_size = 0;
289                 p_sys->b_header_complete = VLC_FALSE;
290             }
291             if( p_buffer->i_size + p_sys->i_header_size > p_sys->i_header_allocated )
292             {
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 );
295             }
296             memcpy( &p_sys->p_header[p_sys->i_header_size],
297                     p_buffer->p_buffer,
298                     p_buffer->i_size );
299             p_sys->i_header_size += p_buffer->i_size;
300         }
301         else if( !p_sys->b_header_complete )
302         {
303             p_sys->b_header_complete = VLC_TRUE;
304
305             p_sys->p_httpd->pf_header_stream( p_sys->p_httpd,
306                                               p_sys->p_httpd_stream,
307                                               p_sys->p_header, p_sys->i_header_size );
308         }
309             /* send data */
310         i_err = p_sys->p_httpd->pf_send_stream( p_sys->p_httpd, p_sys->p_httpd_stream,
311                                                 p_buffer->p_buffer, p_buffer->i_size );
312
313         p_next = p_buffer->p_next;
314         sout_BufferDelete( p_access->p_sout, p_buffer );
315         p_buffer = p_next;
316
317         if( i_err < 0 )
318         {
319             break;
320         }
321     }
322
323     if( i_err < 0 )
324     {
325         sout_buffer_t *p_next;
326         while( p_buffer )
327         {
328             p_next = p_buffer->p_next;
329             sout_BufferDelete( p_access->p_sout, p_buffer );
330             p_buffer = p_next;
331         }
332     }
333
334     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
335 }
336
337 /*****************************************************************************
338  * Seek: seek to a specific location in a file
339  *****************************************************************************/
340 static int Seek( sout_access_out_t *p_access, off_t i_pos )
341 {
342     msg_Err( p_access, "http sout access cannot seek" );
343     return( VLC_EGENERIC );
344 }
345