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