]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2009 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37
38
39 #include <vlc_input.h>
40 #include <vlc_playlist.h>
41
42 #if 0 //def HAVE_AVAHI_CLIENT
43     #include "bonjour.h"
44
45     #if defined( WIN32 )
46         #define DIRECTORY_SEPARATOR '\\'
47     #else
48         #define DIRECTORY_SEPARATOR '/'
49     #endif
50 #endif
51
52 #include <vlc_httpd.h>
53
54 #define DEFAULT_PORT        8080
55 #define DEFAULT_SSL_PORT    8443
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 static int  Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
62
63 #define SOUT_CFG_PREFIX "sout-http-"
64
65 #define USER_TEXT N_("Username")
66 #define USER_LONGTEXT N_("User name that will be " \
67                          "requested to access the stream." )
68 #define PASS_TEXT N_("Password")
69 #define PASS_LONGTEXT N_("Password that will be " \
70                          "requested to access the stream." )
71 #define MIME_TEXT N_("Mime")
72 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
73                         "if not specified)." )
74 #define CERT_TEXT N_( "Certificate file" )
75 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
76                           "be used for HTTPS." )
77 #define KEY_TEXT N_( "Private key file" )
78 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
79                          "be used for HTTPS. Leave " \
80                          "empty if you don't have one." )
81 #define CA_TEXT N_( "Root CA file" )
82 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
83                         "(certificate authority) file that will be used for " \
84                         "HTTPS. Leave empty if you " \
85                         "don't have one." )
86 #define CRL_TEXT N_( "CRL file" )
87 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
88                          "file that will be used for SSL. Leave " \
89                          "empty if you don't have one." )
90 #define BONJOUR_TEXT N_( "Advertise with Bonjour")
91 #define BONJOUR_LONGTEXT N_( "Advertise the stream with the Bonjour protocol." )
92
93
94 vlc_module_begin ()
95     set_description( N_("HTTP stream output") )
96     set_capability( "sout access", 0 )
97     set_shortname( "HTTP" )
98     add_shortcut( "http", "https", "mmsh" )
99     set_category( CAT_SOUT )
100     set_subcategory( SUBCAT_SOUT_ACO )
101     add_string( SOUT_CFG_PREFIX "user", "", NULL,
102                 USER_TEXT, USER_LONGTEXT, true )
103     add_password( SOUT_CFG_PREFIX "pwd", "", NULL,
104                   PASS_TEXT, PASS_LONGTEXT, true )
105     add_string( SOUT_CFG_PREFIX "mime", "", NULL,
106                 MIME_TEXT, MIME_LONGTEXT, true )
107     add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
108                 CERT_TEXT, CERT_LONGTEXT, true )
109     add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
110                 KEY_TEXT, KEY_LONGTEXT, true )
111     add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
112                 CA_TEXT, CA_LONGTEXT, true )
113     add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
114                 CRL_TEXT, CRL_LONGTEXT, true )
115 #if 0 //def HAVE_AVAHI_CLIENT
116     add_bool( SOUT_CFG_PREFIX "bonjour", false, NULL,
117               BONJOUR_TEXT, BONJOUR_LONGTEXT, true);
118 #endif
119     set_callbacks( Open, Close )
120 vlc_module_end ()
121
122
123 /*****************************************************************************
124  * Exported prototypes
125  *****************************************************************************/
126 static const char *const ppsz_sout_options[] = {
127     "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
128 };
129
130 static ssize_t Write( sout_access_out_t *, block_t * );
131 static int Seek ( sout_access_out_t *, off_t  );
132 static int Control( sout_access_out_t *, int, va_list );
133
134 struct sout_access_out_sys_t
135 {
136     /* host */
137     httpd_host_t        *p_httpd_host;
138
139     /* stream */
140     httpd_stream_t      *p_httpd_stream;
141
142     /* gather header from stream */
143     int                 i_header_allocated;
144     int                 i_header_size;
145     uint8_t             *p_header;
146     bool          b_header_complete;
147
148 #if 0 //def HAVE_AVAHI_CLIENT
149     void                *p_bonjour;
150 #endif
151 };
152
153 /*****************************************************************************
154  * Open: open the file
155  *****************************************************************************/
156 static int Open( vlc_object_t *p_this )
157 {
158     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
159     sout_access_out_sys_t   *p_sys;
160
161     char                *psz_parser;
162
163     char                *psz_bind_addr;
164     int                 i_bind_port;
165     char                *psz_file_name;
166     char                *psz_user;
167     char                *psz_pwd;
168     char                *psz_mime;
169     char                *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
170                         *psz_crl = NULL;
171
172     if( !( p_sys = p_access->p_sys =
173                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
174         return VLC_ENOMEM ;
175
176     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
177
178     /* p_access->psz_path = "hostname:port/filename" */
179     psz_bind_addr = strdup( p_access->psz_path );
180
181     i_bind_port = 0;
182
183     psz_parser = strchr( psz_bind_addr, '/' );
184     if( psz_parser )
185     {
186         psz_file_name = strdup( psz_parser );
187         *psz_parser = '\0';
188     }
189     else
190         psz_file_name = strdup( "/" );
191
192     if( psz_bind_addr[0] == '[' )
193     {
194         psz_bind_addr++;
195         psz_parser = strstr( psz_bind_addr, "]:" );
196         if( psz_parser )
197         {
198             *psz_parser = '\0';
199             i_bind_port = atoi( psz_parser + 2 );
200         }
201         psz_parser = psz_bind_addr - 1;
202     }
203     else
204     {
205         psz_parser = strrchr( psz_bind_addr, ':' );
206         if( psz_parser )
207         {
208             *psz_parser = '\0';
209             i_bind_port = atoi( psz_parser + 1 );
210         }
211         psz_parser = psz_bind_addr;
212     }
213
214     /* SSL support */
215     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
216     {
217         psz_cert = var_CreateGetNonEmptyString( p_this, SOUT_CFG_PREFIX"cert" );
218         psz_key = var_CreateGetNonEmptyString( p_this, SOUT_CFG_PREFIX"key" );
219         psz_ca = var_CreateGetNonEmptyString( p_this, SOUT_CFG_PREFIX"ca" );
220         psz_crl = var_CreateGetNonEmptyString( p_this, SOUT_CFG_PREFIX"crl" );
221
222         if( i_bind_port <= 0 )
223             i_bind_port = DEFAULT_SSL_PORT;
224     }
225     else
226     {
227         if( i_bind_port <= 0 )
228             i_bind_port = DEFAULT_PORT;
229     }
230
231     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
232                                             psz_bind_addr, i_bind_port,
233                                             psz_cert, psz_key, psz_ca,
234                                             psz_crl );
235     free( psz_cert );
236     free( psz_key );
237     free( psz_ca );
238     free( psz_crl );
239
240     if( p_sys->p_httpd_host == NULL )
241     {
242         msg_Err( p_access, "cannot listen on %s port %d",
243                  psz_bind_addr, i_bind_port );
244         free( psz_file_name );
245         free( psz_parser );
246         free( p_sys );
247         return VLC_EGENERIC;
248     }
249     free( psz_parser );
250
251     psz_user = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "user" );
252     psz_pwd = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "pwd" );
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         psz_mime = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "mime" );
260     }
261
262     p_sys->p_httpd_stream =
263         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
264                          psz_user, psz_pwd, NULL );
265     free( psz_user );
266     free( psz_pwd );
267     free( psz_mime );
268
269     if( p_sys->p_httpd_stream == NULL )
270     {
271         msg_Err( p_access, "cannot add stream %s", psz_file_name );
272         httpd_HostDelete( p_sys->p_httpd_host );
273
274         free( psz_file_name );
275         free( p_sys );
276         return VLC_EGENERIC;
277     }
278
279 #if 0 //def HAVE_AVAHI_CLIENT
280     if( var_InheritBool(p_this, SOUT_CFG_PREFIX "bonjour") )
281     {
282         char                *psz_txt, *psz_name;
283         playlist_t          *p_playlist = pl_Get( p_access );
284
285         char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
286         char *psz_newuri = psz_uri;
287         psz_name = strrchr( psz_newuri, DIRECTORY_SEPARATOR );
288         if( psz_name != NULL ) psz_name++;
289         else psz_name = psz_newuri;
290
291         if( psz_file_name &&
292             asprintf( &psz_txt, "path=%s", psz_file_name ) == -1 )
293             {
294                 free( psz_uri );
295                 return VLC_ENOMEM;
296             }
297
298         p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
299                                     strcmp( p_access->psz_access, "https" )
300                                        ? "_vlc-http._tcp" : "_vlc-https._tcp",
301                                              psz_name, i_bind_port, psz_txt );
302         free( psz_uri );
303         free( psz_txt );
304
305         if( p_sys->p_bonjour == NULL )
306             msg_Err( p_access, "unable to start requested Bonjour announce" );
307     }
308     else
309         p_sys->p_bonjour = NULL;
310 #endif
311
312     free( psz_file_name );
313
314     p_sys->i_header_allocated = 1024;
315     p_sys->i_header_size      = 0;
316     p_sys->p_header           = xmalloc( p_sys->i_header_allocated );
317     p_sys->b_header_complete  = false;
318
319     p_access->pf_write       = Write;
320     p_access->pf_seek        = Seek;
321     p_access->pf_control     = Control;
322
323     return VLC_SUCCESS;
324 }
325
326 /*****************************************************************************
327  * Close: close the target
328  *****************************************************************************/
329 static void Close( vlc_object_t * p_this )
330 {
331     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
332     sout_access_out_sys_t   *p_sys = p_access->p_sys;
333
334 #if 0 //def HAVE_AVAHI_CLIENT
335     if( p_sys->p_bonjour != NULL )
336         bonjour_stop_service( p_sys->p_bonjour );
337 #endif
338
339     httpd_StreamDelete( p_sys->p_httpd_stream );
340     httpd_HostDelete( p_sys->p_httpd_host );
341
342     free( p_sys->p_header );
343
344     msg_Dbg( p_access, "Close" );
345
346     free( p_sys );
347 }
348
349 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
350 {
351     (void)p_access;
352
353     switch( i_query )
354     {
355         case ACCESS_OUT_CONTROLS_PACE:
356             *va_arg( args, bool * ) = false;
357             break;
358
359         default:
360             return VLC_EGENERIC;
361     }
362     return VLC_SUCCESS;
363 }
364
365 /*****************************************************************************
366  * Write:
367  *****************************************************************************/
368 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
369 {
370     sout_access_out_sys_t *p_sys = p_access->p_sys;
371     int i_err = 0;
372     int i_len = 0;
373
374     while( p_buffer )
375     {
376         block_t *p_next;
377
378         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
379         {
380             /* gather header */
381             if( p_sys->b_header_complete )
382             {
383                 /* free previously gathered header */
384                 p_sys->i_header_size = 0;
385                 p_sys->b_header_complete = false;
386             }
387             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
388                 p_sys->i_header_allocated )
389             {
390                 p_sys->i_header_allocated =
391                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
392                 p_sys->p_header = xrealloc( p_sys->p_header,
393                                                   p_sys->i_header_allocated );
394             }
395             memcpy( &p_sys->p_header[p_sys->i_header_size],
396                     p_buffer->p_buffer,
397                     p_buffer->i_buffer );
398             p_sys->i_header_size += p_buffer->i_buffer;
399         }
400         else if( !p_sys->b_header_complete )
401         {
402             p_sys->b_header_complete = true;
403
404             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
405                                 p_sys->i_header_size );
406         }
407
408         i_len += p_buffer->i_buffer;
409         /* send data */
410         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
411                                   p_buffer->i_buffer );
412
413         p_next = p_buffer->p_next;
414         block_Release( p_buffer );
415         p_buffer = p_next;
416
417         if( i_err < 0 )
418         {
419             break;
420         }
421     }
422
423     if( i_err < 0 )
424     {
425         block_ChainRelease( p_buffer );
426     }
427
428     return( i_err < 0 ? VLC_EGENERIC : i_len );
429 }
430
431 /*****************************************************************************
432  * Seek: seek to a specific location in a file
433  *****************************************************************************/
434 static int Seek( sout_access_out_t *p_access, off_t i_pos )
435 {
436     (void)i_pos;
437     msg_Warn( p_access, "HTTP sout access cannot seek" );
438     return VLC_EGENERIC;
439 }