1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <arpa/inet.h>
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
39 #include <vlc_block.h>
42 #include <vlc_input.h>
43 #include <vlc_playlist.h>
44 #include <vlc_httpd.h>
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
52 #define SOUT_CFG_PREFIX "sout-http-"
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.")
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 )
87 /*****************************************************************************
89 *****************************************************************************/
90 static const char *const ppsz_sout_options[] = {
91 "user", "pwd", "mime", "metacube", NULL
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 );
98 struct sout_access_out_sys_t
101 httpd_host_t *p_httpd_host;
104 httpd_stream_t *p_httpd_stream;
106 /* gather header from stream */
107 int i_header_allocated;
110 bool b_header_complete;
112 bool b_has_keyframes;
115 /* Definitions for the Metacube2 protocol, used to communicate with Cubemap. */
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
121 struct metacube2_block_header
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. */
130 * Implementation of Metacube2 utility functions. Taken from the Cubemap
131 * distribution and then relicensed by the author to LGPL2.1+ for inclusion
136 * https://www.ece.cmu.edu/~koopman/pubs/KoopmanCRCWebinar9May2012.pdf
137 * recommends this for messages as short as ours (see table at page 34).
139 #define METACUBE2_CRC_POLYNOMIAL 0x8FDB
141 /* Semi-random starting value to make sure all-zero won't pass. */
142 #define METACUBE2_CRC_START 0x1234
144 /* This code is based on code generated by pycrc. */
145 static uint16_t metacube2_compute_crc(const struct metacube2_block_header *hdr)
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;
151 for (int i = 0; i < data_len; ++i) {
153 for (int j = 0; j < 8; j++) {
154 int bit = crc & 0x8000;
155 crc = (crc << 1) | ((c >> (7 - j)) & 0x01);
157 crc ^= METACUBE2_CRC_POLYNOMIAL;
163 for (int i = 0; i < 16; i++) {
164 int bit = crc & 0x8000;
167 crc ^= METACUBE2_CRC_POLYNOMIAL;
174 /*****************************************************************************
175 * Open: open the file
176 *****************************************************************************/
177 static int Open( vlc_object_t *p_this )
179 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
180 sout_access_out_sys_t *p_sys;
186 if( !( p_sys = p_access->p_sys =
187 malloc( sizeof( sout_access_out_sys_t ) ) ) )
190 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
192 const char *path = p_access->psz_path;
193 path += strcspn( path, "/" );
194 if( path > p_access->psz_path )
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 )
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." );
209 strncpy( host, p_access->psz_path, len );
212 var_Create( p_access, "http-host", VLC_VAR_STRING );
213 var_SetString( p_access, "http-host", host );
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 ); */
224 int bind_port = atoi( port );
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 );
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) );
241 p_sys->p_httpd_host = vlc_http_HostNew( VLC_OBJECT(p_access) );
243 if( p_sys->p_httpd_host == NULL )
245 msg_Err( p_access, "cannot start HTTP server" );
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" ) )
254 psz_mime = strdup( "video/x-ms-asf-stream" );
258 psz_mime = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "mime" );
261 p_sys->b_metacube = var_GetBool( p_access, SOUT_CFG_PREFIX "metacube" );
262 p_sys->b_has_keyframes = false;
264 p_sys->p_httpd_stream =
265 httpd_StreamNew( p_sys->p_httpd_host, path, psz_mime,
271 if( p_sys->p_httpd_stream == NULL )
273 msg_Err( p_access, "cannot add stream %s", path );
274 httpd_HostDelete( p_sys->p_httpd_host );
280 if( p_sys->b_metacube )
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 )
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;
296 p_access->pf_write = Write;
297 p_access->pf_seek = Seek;
298 p_access->pf_control = Control;
303 /*****************************************************************************
304 * Close: close the target
305 *****************************************************************************/
306 static void Close( vlc_object_t * p_this )
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;
311 httpd_StreamDelete( p_sys->p_httpd_stream );
312 httpd_HostDelete( p_sys->p_httpd_host );
314 free( p_sys->p_header );
316 msg_Dbg( p_access, "Close" );
321 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
327 case ACCESS_OUT_CONTROLS_PACE:
328 *va_arg( args, bool * ) = false;
337 /*****************************************************************************
339 *****************************************************************************/
340 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
342 sout_access_out_sys_t *p_sys = p_access->p_sys;
350 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
353 if( p_sys->b_header_complete )
355 /* free previously gathered header */
356 p_sys->i_header_size = 0;
357 p_sys->b_header_complete = false;
359 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
360 p_sys->i_header_allocated )
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 );
367 memcpy( &p_sys->p_header[p_sys->i_header_size],
369 p_buffer->i_buffer );
370 p_sys->i_header_size += p_buffer->i_buffer;
372 else if( !p_sys->b_header_complete )
374 p_sys->b_header_complete = true;
376 if ( p_sys->b_metacube )
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 ) );
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 );
389 httpd_StreamHeader( p_sys->p_httpd_stream, p_hdr_block, i_header_size );
395 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
396 p_sys->i_header_size );
400 i_len += p_buffer->i_buffer;
402 if( p_buffer->i_flags & BLOCK_FLAG_TYPE_I )
404 p_sys->b_has_keyframes = true;
407 p_next = p_buffer->p_next;
409 if( p_sys->b_metacube )
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 ) );
422 p_buffer = block_Realloc( p_buffer, sizeof( hdr ), p_buffer->i_buffer );
423 if( p_buffer == NULL ) {
424 block_ChainRelease( p_next );
427 memcpy( p_buffer->p_buffer, &hdr, sizeof( hdr ) );
431 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer );
433 block_Release( p_buffer );
444 block_ChainRelease( p_buffer );
447 return( i_err < 0 ? VLC_EGENERIC : i_len );
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 )
456 msg_Warn( p_access, "HTTP sout access cannot seek" );