]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
* Bonjour services discovery module using avahi.
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Jon Lech Johansen <jon@nanocrew.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/sout.h>
32
33 #ifdef HAVE_AVAHI_CLIENT
34     #include <vlc/intf.h>
35
36     #include "bonjour.h"
37
38     #if defined( WIN32 )
39         #define DIRECTORY_SEPARATOR '\\'
40     #else
41         #define DIRECTORY_SEPARATOR '/'
42     #endif
43 #endif
44
45 #include "vlc_httpd.h"
46
47 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
48
49 #define DEFAULT_PORT        8080
50 #define DEFAULT_SSL_PORT    8443
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 static int  Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
57
58 #define SOUT_CFG_PREFIX "sout-http-"
59
60 #define USER_TEXT N_("Username")
61 #define USER_LONGTEXT N_("Allows you to give a user name that will be " \
62                          "requested to access the stream." )
63 #define PASS_TEXT N_("Password")
64 #define PASS_LONGTEXT N_("Allows you to give a password that will be " \
65                          "requested to access the stream." )
66 #define MIME_TEXT N_("Mime")
67 #define MIME_LONGTEXT N_("Allows you to give the mime returned by the server." )
68
69 #define CERT_TEXT N_( "Certificate file" )
70 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
71                           "be used by the HTTP/SSL stream output" )
72 #define KEY_TEXT N_( "Private key file" )
73 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
74                          " be used by the HTTP/SSL stream output. Leave " \
75                          "empty if you don't have one." )
76 #define CA_TEXT N_( "Root CA file" )
77 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
78                         "(certificate authority) file that will be used by " \
79                         "the HTTP/SSL stream output. Leave empty if you " \
80                         "don't have one." )
81 #define CRL_TEXT N_( "CRL file" )
82 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
83                          "file that will be HTTP/SSL stream output. Leave " \
84                          "empty if you don't have one." )
85
86 vlc_module_begin();
87     set_description( _("HTTP stream output") );
88     set_capability( "sout access", 0 );
89     set_shortname( N_("HTTP" ) );
90     add_shortcut( "http" );
91     add_shortcut( "https" );
92     add_shortcut( "mmsh" );
93     set_category( CAT_SOUT );
94     set_subcategory( SUBCAT_SOUT_ACO );
95     add_string( SOUT_CFG_PREFIX "user", "", NULL,
96                 USER_TEXT, USER_LONGTEXT, VLC_TRUE );
97     add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
98                 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
99     add_string( SOUT_CFG_PREFIX "mime", "", NULL,
100                 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
101     add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
102                 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
103     add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
104                 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
105     add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
106                 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
107     add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
108                 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
109     set_callbacks( Open, Close );
110 vlc_module_end();
111
112
113 /*****************************************************************************
114  * Exported prototypes
115  *****************************************************************************/
116 static const char *ppsz_sout_options[] = {
117     "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
118 };
119
120 static int Write( sout_access_out_t *, block_t * );
121 static int Seek ( sout_access_out_t *, off_t  );
122
123 struct sout_access_out_sys_t
124 {
125     /* host */
126     httpd_host_t        *p_httpd_host;
127
128     /* stream */
129     httpd_stream_t      *p_httpd_stream;
130
131     /* gather header from stream */
132     int                 i_header_allocated;
133     int                 i_header_size;
134     uint8_t             *p_header;
135     vlc_bool_t          b_header_complete;
136
137 #ifdef HAVE_AVAHI_CLIENT
138     void                *p_bonjour;
139 #endif
140 };
141
142 /*****************************************************************************
143  * Open: open the file
144  *****************************************************************************/
145 static int Open( vlc_object_t *p_this )
146 {
147     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
148     sout_access_out_sys_t   *p_sys;
149
150     char                *psz_parser, *psz_name;
151
152     char                *psz_bind_addr;
153     int                 i_bind_port;
154     char                *psz_file_name;
155     char                *psz_user = NULL;
156     char                *psz_pwd = NULL;
157     char                *psz_mime = NULL;
158     const char          *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
159                         *psz_crl = NULL;
160     vlc_value_t         val;
161
162 #ifdef HAVE_AVAHI_CLIENT
163     playlist_t          *p_playlist;
164     char                *psz_txt;
165 #endif
166
167     if( !( p_sys = p_access->p_sys =
168                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
169     {
170         msg_Err( p_access, "Not enough memory" );
171         return VLC_ENOMEM ;
172     }
173
174     sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
175
176     /* p_access->psz_name host.name:port/filename */
177     psz_name = psz_parser = strdup( p_access->psz_name );
178
179     psz_bind_addr = psz_parser;
180     i_bind_port = 0;
181     psz_file_name = "";
182
183     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
184     {
185         psz_parser++;
186     }
187     if( *psz_parser == ':' )
188     {
189         *psz_parser = '\0';
190         psz_parser++;
191         i_bind_port = atoi( psz_parser );
192
193         while( *psz_parser && *psz_parser != '/' )
194         {
195             psz_parser++;
196         }
197     }
198     if( *psz_parser == '/' )
199     {
200         *psz_parser = '\0';
201         psz_parser++;
202         psz_file_name = psz_parser;
203     }
204
205     if( !*psz_file_name )
206     {
207         psz_file_name = strdup( "/" );
208     }
209     else if( *psz_file_name != '/' )
210     {
211         char *p = psz_file_name;
212
213         psz_file_name = malloc( strlen( p ) + 2 );
214         strcpy( psz_file_name, "/" );
215         strcat( psz_file_name, p );
216     }
217     else
218     {
219         psz_file_name = strdup( psz_file_name );
220     }
221
222     /* SSL support */
223     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
224     {
225         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
226         psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
227         psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
228         psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
229
230         if( i_bind_port <= 0 )
231             i_bind_port = DEFAULT_SSL_PORT;
232     }
233     else
234     {
235         if( i_bind_port <= 0 )
236             i_bind_port = DEFAULT_PORT;
237     }
238
239     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
240                                             psz_bind_addr, i_bind_port,
241                                             psz_cert, psz_key, psz_ca,
242                                             psz_crl );
243     if( p_sys->p_httpd_host == NULL )
244     {
245         msg_Err( p_access, "cannot listen on %s:%d",
246                  psz_bind_addr, i_bind_port );
247         free( psz_name );
248         free( psz_file_name );
249         free( p_sys );
250         return VLC_EGENERIC;
251     }
252
253     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
254     {
255         psz_mime = strdup( "video/x-ms-asf-stream" );
256     }
257     else
258     {
259         var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
260         if( *val.psz_string )
261             psz_mime = val.psz_string;
262         else
263             free( val.psz_string );
264     }
265
266     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
267     if( *val.psz_string )
268         psz_user = val.psz_string;
269     else
270         free( val.psz_string );
271
272     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
273     if( *val.psz_string )
274         psz_pwd = val.psz_string;
275     else
276         free( val.psz_string );
277
278     p_sys->p_httpd_stream =
279         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
280                          psz_user, psz_pwd, NULL );
281     if( psz_user ) free( psz_user );
282     if( psz_pwd ) free( psz_pwd );
283     if( psz_mime ) free( psz_mime );
284
285     if( p_sys->p_httpd_stream == NULL )
286     {
287         msg_Err( p_access, "cannot add stream %s", psz_file_name );
288         httpd_HostDelete( p_sys->p_httpd_host );
289
290         free( psz_name );
291         free( psz_file_name );
292         free( p_sys );
293         return VLC_EGENERIC;
294     }
295
296 #ifdef HAVE_AVAHI_CLIENT
297     asprintf( &psz_txt, "path=%s", psz_file_name );
298 #endif
299
300     free( psz_file_name );
301     free( psz_name );
302
303 #ifdef HAVE_AVAHI_CLIENT
304     p_playlist = (playlist_t *)vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
305                                                 FIND_ANYWHERE );
306     if( p_playlist == NULL )
307     {
308         msg_Err( p_access, "unable to find playlist" );
309         httpd_HostDelete( p_sys->p_httpd_host );
310         free( (void *)psz_txt );
311         free( (void *)p_sys );
312         return VLC_EGENERIC;
313     }
314
315     psz_name = strrchr( p_playlist->status.p_item->input.psz_uri,
316                         DIRECTORY_SEPARATOR );
317     if( psz_name != NULL ) psz_name++;
318     else psz_name = p_playlist->status.p_item->input.psz_uri;
319
320     p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
321                                               "_vlc-http._tcp",
322                                               psz_name, i_bind_port, psz_txt );
323     free( (void *)psz_txt );
324     if( p_sys->p_bonjour == NULL )
325     {
326         vlc_object_release( p_playlist );
327         httpd_HostDelete( p_sys->p_httpd_host );
328         free( (void *)p_sys );
329         return VLC_EGENERIC;
330     }
331
332     vlc_object_release( p_playlist );
333 #endif
334
335     p_sys->i_header_allocated = 1024;
336     p_sys->i_header_size      = 0;
337     p_sys->p_header           = malloc( p_sys->i_header_allocated );
338     p_sys->b_header_complete  = VLC_FALSE;
339
340     p_access->pf_write       = Write;
341     p_access->pf_seek        = Seek;
342
343
344     /* update p_sout->i_out_pace_nocontrol */
345     p_access->p_sout->i_out_pace_nocontrol++;
346
347     return VLC_SUCCESS;
348 }
349
350 /*****************************************************************************
351  * Close: close the target
352  *****************************************************************************/
353 static void Close( vlc_object_t * p_this )
354 {
355     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
356     sout_access_out_sys_t   *p_sys = p_access->p_sys;
357
358 #ifdef HAVE_AVAHI_CLIENT
359     bonjour_stop_service( p_sys->p_bonjour );
360 #endif
361
362     /* update p_sout->i_out_pace_nocontrol */
363     p_access->p_sout->i_out_pace_nocontrol--;
364
365     httpd_StreamDelete( p_sys->p_httpd_stream );
366     httpd_HostDelete( p_sys->p_httpd_host );
367
368     FREE( p_sys->p_header );
369
370     msg_Dbg( p_access, "Close" );
371
372     free( p_sys );
373 }
374
375 /*****************************************************************************
376  * Write:
377  *****************************************************************************/
378 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
379 {
380     sout_access_out_sys_t *p_sys = p_access->p_sys;
381     int i_err = 0;
382
383     while( p_buffer )
384     {
385         block_t *p_next;
386
387         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
388         {
389             /* gather header */
390             if( p_sys->b_header_complete )
391             {
392                 /* free previously gathered header */
393                 p_sys->i_header_size = 0;
394                 p_sys->b_header_complete = VLC_FALSE;
395             }
396             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
397                 p_sys->i_header_allocated )
398             {
399                 p_sys->i_header_allocated =
400                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
401                 p_sys->p_header =
402                     realloc( p_sys->p_header, p_sys->i_header_allocated );
403             }
404             memcpy( &p_sys->p_header[p_sys->i_header_size],
405                     p_buffer->p_buffer,
406                     p_buffer->i_buffer );
407             p_sys->i_header_size += p_buffer->i_buffer;
408         }
409         else if( !p_sys->b_header_complete )
410         {
411             p_sys->b_header_complete = VLC_TRUE;
412
413             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
414                                 p_sys->i_header_size );
415         }
416
417         /* send data */
418         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
419                                   p_buffer->i_buffer );
420
421         p_next = p_buffer->p_next;
422         block_Release( p_buffer );
423         p_buffer = p_next;
424
425         if( i_err < 0 )
426         {
427             break;
428         }
429     }
430
431     if( i_err < 0 )
432     {
433         block_ChainRelease( p_buffer );
434     }
435
436     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
437 }
438
439 /*****************************************************************************
440  * Seek: seek to a specific location in a file
441  *****************************************************************************/
442 static int Seek( sout_access_out_t *p_access, off_t i_pos )
443 {
444     msg_Warn( p_access, "HTTP sout access cannot seek" );
445     return VLC_EGENERIC;
446 }