]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
Fix Metacube header handling with multiple header blocks.
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Remi Denis-Courmont
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 <stdint.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_sout.h>
38 #include <vlc_block.h>
39
40
41 #include <vlc_input.h>
42 #include <vlc_httpd.h>
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
49
50 #define SOUT_CFG_PREFIX "sout-http-"
51
52 #define USER_TEXT N_("Username")
53 #define USER_LONGTEXT N_("Username that will be " \
54                          "requested to access the stream." )
55 #define PASS_TEXT N_("Password")
56 #define PASS_LONGTEXT N_("Password that will be " \
57                          "requested to access the stream." )
58 #define MIME_TEXT N_("Mime")
59 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
60                         "if not specified)." )
61 #define METACUBE_TEXT N_("Metacube")
62 #define METACUBE_LONGTEXT N_("Use the Metacube protocol. Needed for streaming " \
63                              "to the Cubemap reflector.")
64
65
66 vlc_module_begin ()
67     set_description( N_("HTTP stream output") )
68     set_capability( "sout access", 0 )
69     set_shortname( "HTTP" )
70     add_shortcut( "http", "https", "mmsh" )
71     set_category( CAT_SOUT )
72     set_subcategory( SUBCAT_SOUT_ACO )
73     add_string( SOUT_CFG_PREFIX "user", "",
74                 USER_TEXT, USER_LONGTEXT, true )
75     add_password( SOUT_CFG_PREFIX "pwd", "",
76                   PASS_TEXT, PASS_LONGTEXT, true )
77     add_string( SOUT_CFG_PREFIX "mime", "",
78                 MIME_TEXT, MIME_LONGTEXT, true )
79     add_bool( SOUT_CFG_PREFIX "metacube", false,
80               METACUBE_TEXT, METACUBE_LONGTEXT, true )
81     set_callbacks( Open, Close )
82 vlc_module_end ()
83
84
85 /*****************************************************************************
86  * Exported prototypes
87  *****************************************************************************/
88 static const char *const ppsz_sout_options[] = {
89     "user", "pwd", "mime", "metacube", NULL
90 };
91
92 static ssize_t Write( sout_access_out_t *, block_t * );
93 static int Seek ( sout_access_out_t *, off_t  );
94 static int Control( sout_access_out_t *, int, va_list );
95
96 struct sout_access_out_sys_t
97 {
98     /* host */
99     httpd_host_t        *p_httpd_host;
100
101     /* stream */
102     httpd_stream_t      *p_httpd_stream;
103
104     /* gather header from stream */
105     int                 i_header_allocated;
106     int                 i_header_size;
107     uint8_t             *p_header;
108     bool          b_header_complete;
109     bool                b_metacube;
110     bool                b_has_keyframes;
111 };
112
113 /* Definitions for the Metacube2 protocol, used to communicate with Cubemap. */
114
115 static const uint8_t METACUBE2_SYNC[8] = {'c', 'u', 'b', 'e', '!', 'm', 'a', 'p'};
116 #define METACUBE_FLAGS_HEADER 0x1
117 #define METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START 0x2
118
119 struct metacube2_block_header
120 {
121     char sync[8];    /* METACUBE2_SYNC */
122     uint32_t size;   /* Network byte order. Does not include header. */
123     uint16_t flags;  /* Network byte order. METACUBE_FLAGS_*. */
124     uint16_t csum;   /* Network byte order. CRC16 of size and flags. */
125 };
126
127 /*
128  * Implementation of Metacube2 utility functions. Taken from the Cubemap
129  * distribution and then relicensed by the author to LGPL2.1+ for inclusion
130  * into VLC.
131  */
132
133 /*
134  * https://www.ece.cmu.edu/~koopman/pubs/KoopmanCRCWebinar9May2012.pdf
135  * recommends this for messages as short as ours (see table at page 34).
136  */
137 #define METACUBE2_CRC_POLYNOMIAL 0x8FDB
138
139 /* Semi-random starting value to make sure all-zero won't pass. */
140 #define METACUBE2_CRC_START 0x1234
141
142 /* This code is based on code generated by pycrc. */
143 static uint16_t metacube2_compute_crc(const struct metacube2_block_header *hdr)
144 {
145     static const int data_len = sizeof(hdr->size) + sizeof(hdr->flags);
146     const uint8_t *data = (uint8_t *)&hdr->size;
147     uint16_t crc = METACUBE2_CRC_START;
148
149     for (int i = 0; i < data_len; ++i) {
150         uint8_t c = data[i];
151         for (int j = 0; j < 8; j++) {
152             int bit = crc & 0x8000;
153             crc = (crc << 1) | ((c >> (7 - j)) & 0x01);
154             if (bit) {
155                 crc ^= METACUBE2_CRC_POLYNOMIAL;
156             }
157         }
158     }
159
160     /* Finalize. */
161     for (int i = 0; i < 16; i++) {
162         int bit = crc & 0x8000;
163         crc = crc << 1;
164         if (bit) {
165             crc ^= METACUBE2_CRC_POLYNOMIAL;
166         }
167     }
168
169     return crc;
170 }
171
172 /*****************************************************************************
173  * Open: open the file
174  *****************************************************************************/
175 static int Open( vlc_object_t *p_this )
176 {
177     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
178     sout_access_out_sys_t   *p_sys;
179
180     char                *psz_user;
181     char                *psz_pwd;
182     char                *psz_mime;
183
184     if( !( p_sys = p_access->p_sys =
185                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
186         return VLC_ENOMEM ;
187
188     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
189
190     const char *path = p_access->psz_path;
191     path += strcspn( path, "/" );
192     if( path > p_access->psz_path )
193     {
194         const char *port = strrchr( p_access->psz_path, ':' );
195         if( port != NULL && strchr( port, ']' ) != NULL )
196             port = NULL; /* IPv6 numeral */
197         if( port != p_access->psz_path )
198         {
199             int len = (port ? port : path) - p_access->psz_path;
200             msg_Warn( p_access, "\"%.*s\" HTTP host might be ignored in "
201                       "multiple-host configurations, use at your own risks.",
202                       len, p_access->psz_path );
203             msg_Info( p_access, "Consider passing --http-host=IP on the "
204                                 "command line instead." );
205
206             char host[len + 1];
207             strncpy( host, p_access->psz_path, len );
208             host[len] = '\0';
209
210             var_Create( p_access, "http-host", VLC_VAR_STRING );
211             var_SetString( p_access, "http-host", host );
212         }
213         if( port != NULL )
214         {
215             /* int len = path - ++port;
216             msg_Info( p_access, "Consider passing --%s-port=%.*s on the "
217                                 "command line instead.",
218                       strcasecmp( p_access->psz_access, "https" )
219                       ? "http" : "https", len, port ); */
220             port++;
221
222             int bind_port = atoi( port );
223             if( bind_port > 0 )
224             {
225                 const char *var = strcasecmp( p_access->psz_access, "https" )
226                                   ? "http-port" : "https-port";
227                 var_Create( p_access, var, VLC_VAR_INTEGER );
228                 var_SetInteger( p_access, var, bind_port );
229             }
230         }
231     }
232     if( !*path )
233         path = "/";
234
235     /* TLS support */
236     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
237         p_sys->p_httpd_host = vlc_https_HostNew( VLC_OBJECT(p_access) );
238     else
239         p_sys->p_httpd_host = vlc_http_HostNew( VLC_OBJECT(p_access) );
240
241     if( p_sys->p_httpd_host == NULL )
242     {
243         msg_Err( p_access, "cannot start HTTP server" );
244         free( p_sys );
245         return VLC_EGENERIC;
246     }
247
248     psz_user = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "user" );
249     psz_pwd = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "pwd" );
250     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
251     {
252         psz_mime = strdup( "video/x-ms-asf-stream" );
253     }
254     else
255     {
256         psz_mime = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "mime" );
257     }
258
259     p_sys->b_metacube = var_GetBool( p_access, SOUT_CFG_PREFIX "metacube" );
260     p_sys->b_has_keyframes = false;
261
262     p_sys->p_httpd_stream =
263         httpd_StreamNew( p_sys->p_httpd_host, path, psz_mime,
264                          psz_user, psz_pwd );
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", path );
272         httpd_HostDelete( p_sys->p_httpd_host );
273
274         free( p_sys );
275         return VLC_EGENERIC;
276     }
277
278     if( p_sys->b_metacube )
279     {
280         httpd_header headers[] = {{ "Content-encoding", "metacube" }};
281         int err = httpd_StreamSetHTTPHeaders( p_sys->p_httpd_stream, headers, sizeof( headers ) / sizeof( httpd_header ) );
282         if( err != VLC_SUCCESS )
283         {
284             free( p_sys );
285             return err;
286         }
287     }
288
289     p_sys->i_header_allocated = 1024;
290     p_sys->i_header_size      = 0;
291     p_sys->p_header           = xmalloc( p_sys->i_header_allocated );
292     p_sys->b_header_complete  = false;
293
294     p_access->pf_write       = Write;
295     p_access->pf_seek        = Seek;
296     p_access->pf_control     = Control;
297
298     return VLC_SUCCESS;
299 }
300
301 /*****************************************************************************
302  * Close: close the target
303  *****************************************************************************/
304 static void Close( vlc_object_t * p_this )
305 {
306     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
307     sout_access_out_sys_t   *p_sys = p_access->p_sys;
308
309     httpd_StreamDelete( p_sys->p_httpd_stream );
310     httpd_HostDelete( p_sys->p_httpd_host );
311
312     free( p_sys->p_header );
313
314     msg_Dbg( p_access, "Close" );
315
316     free( p_sys );
317 }
318
319 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
320 {
321     (void)p_access;
322
323     switch( i_query )
324     {
325         case ACCESS_OUT_CONTROLS_PACE:
326             *va_arg( args, bool * ) = false;
327             break;
328
329         default:
330             return VLC_EGENERIC;
331     }
332     return VLC_SUCCESS;
333 }
334
335 /*****************************************************************************
336  * Write:
337  *****************************************************************************/
338 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
339 {
340     sout_access_out_sys_t *p_sys = p_access->p_sys;
341     int i_err = 0;
342     int i_len = 0;
343
344     while( p_buffer )
345     {
346         block_t *p_next;
347
348         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
349         {
350             /* gather header */
351             if( p_sys->b_header_complete )
352             {
353                 /* free previously gathered header */
354                 p_sys->i_header_size = 0;
355                 p_sys->b_header_complete = false;
356             }
357             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
358                 p_sys->i_header_allocated )
359             {
360                 p_sys->i_header_allocated =
361                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
362                 p_sys->p_header = xrealloc( p_sys->p_header,
363                                                   p_sys->i_header_allocated );
364             }
365             memcpy( &p_sys->p_header[p_sys->i_header_size],
366                     p_buffer->p_buffer,
367                     p_buffer->i_buffer );
368             p_sys->i_header_size += p_buffer->i_buffer;
369         }
370         else if( !p_sys->b_header_complete )
371         {
372             p_sys->b_header_complete = true;
373
374             if ( p_sys->b_metacube )
375             {
376                 struct metacube2_block_header hdr;
377                 memcpy( hdr.sync, METACUBE2_SYNC, sizeof( METACUBE2_SYNC ) );
378                 hdr.size = hton32( p_sys->i_header_size );
379                 hdr.flags = hton16( METACUBE_FLAGS_HEADER );
380                 hdr.csum = hton16( metacube2_compute_crc( &hdr ) );
381
382                 int i_header_size = p_sys->i_header_size + sizeof( hdr );
383                 block_t* p_hdr_block = block_Alloc( i_header_size );
384                 p_hdr_block->i_flags = 0;
385                 memcpy( p_hdr_block->p_buffer, &hdr, sizeof( hdr ) );
386                 memcpy( p_hdr_block->p_buffer + sizeof( hdr ), p_sys->p_header, p_sys->i_header_size );
387
388                 /* send the combined header here instead of sending them as regular
389                  * data, so that we get them as a single Metacube header block */
390                 httpd_StreamHeader( p_sys->p_httpd_stream, p_hdr_block->p_buffer, p_hdr_block->i_buffer );
391                 httpd_StreamSend( p_sys->p_httpd_stream, p_hdr_block );
392
393                 block_Release( p_hdr_block );
394             }
395             else
396             {
397                 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
398                                     p_sys->i_header_size );
399             }
400         }
401
402         i_len += p_buffer->i_buffer;
403
404         if( p_buffer->i_flags & BLOCK_FLAG_TYPE_I )
405         {
406             p_sys->b_has_keyframes = true;
407         }
408
409         p_next = p_buffer->p_next;
410
411         if( p_sys->b_metacube )
412         {
413             /* header data is combined into one packet and sent earlier */
414             if( p_buffer->i_flags & BLOCK_FLAG_HEADER ) {
415                 block_Release( p_buffer );
416                 p_buffer = p_next;
417                 continue;
418             }
419
420             /* prepend Metacube header */
421             struct metacube2_block_header hdr;
422             memcpy( hdr.sync, METACUBE2_SYNC, sizeof( METACUBE2_SYNC ) );
423             hdr.size = hton32( p_buffer->i_buffer );
424             hdr.flags = hton16( 0 );
425             if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
426                 hdr.flags |= hton16( METACUBE_FLAGS_HEADER );
427             if( p_sys->b_has_keyframes && !( p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) )
428                 hdr.flags |= hton16( METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START );
429             hdr.csum = hton16( metacube2_compute_crc( &hdr ) );
430
431             p_buffer = block_Realloc( p_buffer, sizeof( hdr ), p_buffer->i_buffer );
432             if( p_buffer == NULL ) {
433                 block_ChainRelease( p_next );
434                 return VLC_ENOMEM;
435             }
436             memcpy( p_buffer->p_buffer, &hdr, sizeof( hdr ) );
437         }
438
439         /* send data */
440         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer );
441
442         block_Release( p_buffer );
443         p_buffer = p_next;
444
445         if( i_err < 0 )
446         {
447             break;
448         }
449     }
450
451     if( i_err < 0 )
452     {
453         block_ChainRelease( p_buffer );
454     }
455
456     return( i_err < 0 ? VLC_EGENERIC : i_len );
457 }
458
459 /*****************************************************************************
460  * Seek: seek to a specific location in a file
461  *****************************************************************************/
462 static int Seek( sout_access_out_t *p_access, off_t i_pos )
463 {
464     (void)i_pos;
465     msg_Warn( p_access, "HTTP sout access cannot seek" );
466     return VLC_EGENERIC;
467 }