]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
* http: added alias "mmsh" to output as mmsh (work only with asfh). Only
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: http.c,v 1.7 2003/08/25 01:33:25 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
195     p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_access), VLC_TRUE );
196     if( !p_sys->p_httpd )
197     {
198         msg_Err( p_access, "cannot start httpd daemon" );
199
200         free( psz_name );
201         free( psz_file_name );
202         free( p_sys );
203         return( VLC_EGENERIC );
204     }
205
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 );
209
210     if( !p_sys->p_httpd_host )
211     {
212         msg_Err( p_access, "cannot listen on %s:%d", psz_bind_addr, i_bind_port );
213         httpd_Release( p_sys->p_httpd );
214
215         free( psz_name );
216         free( psz_file_name );
217         free( p_sys );
218         return( VLC_EGENERIC );
219     }
220
221     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
222     {
223         psz_mime = "video/x-ms-asf-stream";
224     }
225     else
226     {
227         psz_mime = GetMime( psz_file_name );
228     }
229     p_sys->p_httpd_stream =
230         p_sys->p_httpd->pf_register_stream( p_sys->p_httpd,
231                                             psz_file_name,
232                                             psz_mime,
233                                             sout_cfg_find_value( p_access->p_cfg, "user" ),
234                                             sout_cfg_find_value( p_access->p_cfg, "pwd" ) );
235
236     if( !p_sys->p_httpd_stream )
237     {
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 );
241
242         free( psz_name );
243         free( psz_file_name );
244         free( p_sys );
245
246         return( VLC_EGENERIC );
247     }
248
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;
253
254     p_access->pf_write       = Write;
255     p_access->pf_seek        = Seek;
256
257     return VLC_SUCCESS;
258 }
259
260 /*****************************************************************************
261  * Close: close the target
262  *****************************************************************************/
263 static void Close( vlc_object_t * p_this )
264 {
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;
267
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 );
270
271     httpd_Release( p_sys->p_httpd );
272
273     FREE( p_sys->p_header );
274
275     msg_Info( p_access, "Close" );
276
277     free( p_sys );
278 }
279
280 /*****************************************************************************
281  * Write:
282  *****************************************************************************/
283 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
284 {
285     sout_access_out_sys_t   *p_sys = p_access->p_sys;
286     int i_err = 0;
287
288     while( p_buffer )
289     {
290         sout_buffer_t *p_next;
291
292         if( p_buffer->i_flags & SOUT_BUFFER_FLAGS_HEADER )
293         {
294             /* gather header */
295             if( p_sys->b_header_complete )
296             {
297                 /* free previously gathered header */
298                 p_sys->i_header_size = 0;
299                 p_sys->b_header_complete = VLC_FALSE;
300             }
301             if( p_buffer->i_size + p_sys->i_header_size > p_sys->i_header_allocated )
302             {
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 );
305             }
306             memcpy( &p_sys->p_header[p_sys->i_header_size],
307                     p_buffer->p_buffer,
308                     p_buffer->i_size );
309             p_sys->i_header_size += p_buffer->i_size;
310         }
311         else if( !p_sys->b_header_complete )
312         {
313             p_sys->b_header_complete = VLC_TRUE;
314
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 );
318         }
319             /* send data */
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 );
322
323         p_next = p_buffer->p_next;
324         sout_BufferDelete( p_access->p_sout, p_buffer );
325         p_buffer = p_next;
326
327         if( i_err < 0 )
328         {
329             break;
330         }
331     }
332
333     if( i_err < 0 )
334     {
335         sout_buffer_t *p_next;
336         while( p_buffer )
337         {
338             p_next = p_buffer->p_next;
339             sout_BufferDelete( p_access->p_sout, p_buffer );
340             p_buffer = p_next;
341         }
342     }
343
344     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
345 }
346
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 )
351 {
352     msg_Err( p_access, "http sout access cannot seek" );
353     return( VLC_EGENERIC );
354 }
355