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 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_block.h>
41 #include <vlc_input.h>
42 #include <vlc_httpd.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t
* );
48 static void Close( vlc_object_t
* );
50 #define SOUT_CFG_PREFIX "sout-http-"
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.")
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
)
85 /*****************************************************************************
87 *****************************************************************************/
88 static const char *const ppsz_sout_options
[] = {
89 "user", "pwd", "mime", "metacube", NULL
92 static ssize_t
Write( sout_access_out_t
*, block_t
* );
93 static int Control( sout_access_out_t
*, int, va_list );
95 struct sout_access_out_sys_t
98 httpd_host_t
*p_httpd_host
;
101 httpd_stream_t
*p_httpd_stream
;
103 /* gather header from stream */
104 int i_header_allocated
;
107 bool b_header_complete
;
109 bool b_has_keyframes
;
112 /* Definitions for the Metacube2 protocol, used to communicate with Cubemap. */
114 static const uint8_t METACUBE2_SYNC
[8] = {'c', 'u', 'b', 'e', '!', 'm', 'a', 'p'};
115 #define METACUBE_FLAGS_HEADER 0x1
116 #define METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START 0x2
118 struct metacube2_block_header
120 char sync
[8]; /* METACUBE2_SYNC */
121 uint32_t size
; /* Network byte order. Does not include header. */
122 uint16_t flags
; /* Network byte order. METACUBE_FLAGS_*. */
123 uint16_t csum
; /* Network byte order. CRC16 of size and flags. */
127 * Implementation of Metacube2 utility functions. Taken from the Cubemap
128 * distribution and then relicensed by the author to LGPL2.1+ for inclusion
133 * https://www.ece.cmu.edu/~koopman/pubs/KoopmanCRCWebinar9May2012.pdf
134 * recommends this for messages as short as ours (see table at page 34).
136 #define METACUBE2_CRC_POLYNOMIAL 0x8FDB
138 /* Semi-random starting value to make sure all-zero won't pass. */
139 #define METACUBE2_CRC_START 0x1234
141 /* This code is based on code generated by pycrc. */
142 static uint16_t metacube2_compute_crc(const struct metacube2_block_header
*hdr
)
144 static const int data_len
= sizeof(hdr
->size
) + sizeof(hdr
->flags
);
145 const uint8_t *data
= (uint8_t *)&hdr
->size
;
146 uint16_t crc
= METACUBE2_CRC_START
;
148 for (int i
= 0; i
< data_len
; ++i
) {
150 for (int j
= 0; j
< 8; j
++) {
151 int bit
= crc
& 0x8000;
152 crc
= (crc
<< 1) | ((c
>> (7 - j
)) & 0x01);
154 crc
^= METACUBE2_CRC_POLYNOMIAL
;
160 for (int i
= 0; i
< 16; i
++) {
161 int bit
= crc
& 0x8000;
164 crc
^= METACUBE2_CRC_POLYNOMIAL
;
171 /*****************************************************************************
172 * Open: open the file
173 *****************************************************************************/
174 static int Open( vlc_object_t
*p_this
)
176 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
177 sout_access_out_sys_t
*p_sys
;
183 if( !( p_sys
= p_access
->p_sys
=
184 malloc( sizeof( sout_access_out_sys_t
) ) ) )
187 config_ChainParse( p_access
, SOUT_CFG_PREFIX
, ppsz_sout_options
, p_access
->p_cfg
);
189 const char *path
= p_access
->psz_path
;
190 path
+= strcspn( path
, "/" );
191 if( path
> p_access
->psz_path
)
193 const char *port
= strrchr( p_access
->psz_path
, ':' );
194 if( port
!= NULL
&& strchr( port
, ']' ) != NULL
)
195 port
= NULL
; /* IPv6 numeral */
196 if( port
!= p_access
->psz_path
)
198 int len
= (port
? port
: path
) - p_access
->psz_path
;
199 msg_Warn( p_access
, "\"%.*s\" HTTP host might be ignored in "
200 "multiple-host configurations, use at your own risks.",
201 len
, p_access
->psz_path
);
202 msg_Info( p_access
, "Consider passing --http-host=IP on the "
203 "command line instead." );
206 strncpy( host
, p_access
->psz_path
, len
);
209 var_Create( p_access
, "http-host", VLC_VAR_STRING
);
210 var_SetString( p_access
, "http-host", host
);
214 /* int len = path - ++port;
215 msg_Info( p_access, "Consider passing --%s-port=%.*s on the "
216 "command line instead.",
217 strcasecmp( p_access->psz_access, "https" )
218 ? "http" : "https", len, port ); */
221 int bind_port
= atoi( port
);
224 const char *var
= strcasecmp( p_access
->psz_access
, "https" )
225 ? "http-port" : "https-port";
226 var_Create( p_access
, var
, VLC_VAR_INTEGER
);
227 var_SetInteger( p_access
, var
, bind_port
);
235 if( p_access
->psz_access
&& !strcmp( p_access
->psz_access
, "https" ) )
236 p_sys
->p_httpd_host
= vlc_https_HostNew( VLC_OBJECT(p_access
) );
238 p_sys
->p_httpd_host
= vlc_http_HostNew( VLC_OBJECT(p_access
) );
240 if( p_sys
->p_httpd_host
== NULL
)
242 msg_Err( p_access
, "cannot start HTTP server" );
247 psz_user
= var_GetNonEmptyString( p_access
, SOUT_CFG_PREFIX
"user" );
248 psz_pwd
= var_GetNonEmptyString( p_access
, SOUT_CFG_PREFIX
"pwd" );
249 if( p_access
->psz_access
&& !strcmp( p_access
->psz_access
, "mmsh" ) )
251 psz_mime
= strdup( "video/x-ms-asf-stream" );
255 psz_mime
= var_GetNonEmptyString( p_access
, SOUT_CFG_PREFIX
"mime" );
258 p_sys
->b_metacube
= var_GetBool( p_access
, SOUT_CFG_PREFIX
"metacube" );
259 p_sys
->b_has_keyframes
= false;
261 p_sys
->p_httpd_stream
=
262 httpd_StreamNew( p_sys
->p_httpd_host
, path
, psz_mime
,
268 if( p_sys
->p_httpd_stream
== NULL
)
270 msg_Err( p_access
, "cannot add stream %s", path
);
271 httpd_HostDelete( p_sys
->p_httpd_host
);
277 if( p_sys
->b_metacube
)
279 const httpd_header headers
[] = {
280 { (char *)"Content-encoding", (char *)"metacube" }
282 int err
= httpd_StreamSetHTTPHeaders( p_sys
->p_httpd_stream
, headers
,
283 ARRAY_SIZE(headers
) );
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_control
= Control
;
302 /*****************************************************************************
303 * Close: close the target
304 *****************************************************************************/
305 static void Close( vlc_object_t
* p_this
)
307 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
308 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
310 httpd_StreamDelete( p_sys
->p_httpd_stream
);
311 httpd_HostDelete( p_sys
->p_httpd_host
);
313 free( p_sys
->p_header
);
315 msg_Dbg( p_access
, "Close" );
320 static int Control( sout_access_out_t
*p_access
, int i_query
, va_list args
)
326 case ACCESS_OUT_CONTROLS_PACE
:
327 *va_arg( args
, bool * ) = false;
336 /*****************************************************************************
338 *****************************************************************************/
339 static ssize_t
Write( sout_access_out_t
*p_access
, block_t
*p_buffer
)
341 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
349 if( p_buffer
->i_flags
& BLOCK_FLAG_HEADER
)
352 if( p_sys
->b_header_complete
)
354 /* free previously gathered header */
355 p_sys
->i_header_size
= 0;
356 p_sys
->b_header_complete
= false;
358 if( (int)(p_buffer
->i_buffer
+ p_sys
->i_header_size
) >
359 p_sys
->i_header_allocated
)
361 p_sys
->i_header_allocated
=
362 p_buffer
->i_buffer
+ p_sys
->i_header_size
+ 1024;
363 p_sys
->p_header
= xrealloc( p_sys
->p_header
,
364 p_sys
->i_header_allocated
);
366 memcpy( &p_sys
->p_header
[p_sys
->i_header_size
],
368 p_buffer
->i_buffer
);
369 p_sys
->i_header_size
+= p_buffer
->i_buffer
;
371 else if( !p_sys
->b_header_complete
)
373 p_sys
->b_header_complete
= true;
375 if ( p_sys
->b_metacube
)
377 struct metacube2_block_header hdr
;
378 memcpy( hdr
.sync
, METACUBE2_SYNC
, sizeof( METACUBE2_SYNC
) );
379 hdr
.size
= hton32( p_sys
->i_header_size
);
380 hdr
.flags
= hton16( METACUBE_FLAGS_HEADER
);
381 hdr
.csum
= hton16( metacube2_compute_crc( &hdr
) );
383 int i_header_size
= p_sys
->i_header_size
+ sizeof( hdr
);
384 block_t
*p_hdr_block
= block_Alloc( i_header_size
);
385 if( p_hdr_block
== NULL
) {
386 block_ChainRelease( p_buffer
);
389 p_hdr_block
->i_flags
= 0;
390 memcpy( p_hdr_block
->p_buffer
, &hdr
, sizeof( hdr
) );
391 memcpy( p_hdr_block
->p_buffer
+ sizeof( hdr
), p_sys
->p_header
, p_sys
->i_header_size
);
393 /* send the combined header here instead of sending them as regular
394 * data, so that we get them as a single Metacube header block */
395 httpd_StreamHeader( p_sys
->p_httpd_stream
, p_hdr_block
->p_buffer
, p_hdr_block
->i_buffer
);
396 httpd_StreamSend( p_sys
->p_httpd_stream
, p_hdr_block
);
398 block_Release( p_hdr_block
);
402 httpd_StreamHeader( p_sys
->p_httpd_stream
, p_sys
->p_header
,
403 p_sys
->i_header_size
);
407 i_len
+= p_buffer
->i_buffer
;
409 if( p_buffer
->i_flags
& BLOCK_FLAG_TYPE_I
)
411 p_sys
->b_has_keyframes
= true;
414 p_next
= p_buffer
->p_next
;
416 if( p_sys
->b_metacube
)
418 /* header data is combined into one packet and sent earlier */
419 if( p_buffer
->i_flags
& BLOCK_FLAG_HEADER
) {
420 block_Release( p_buffer
);
425 /* prepend Metacube header */
426 struct metacube2_block_header hdr
;
427 memcpy( hdr
.sync
, METACUBE2_SYNC
, sizeof( METACUBE2_SYNC
) );
428 hdr
.size
= hton32( p_buffer
->i_buffer
);
429 hdr
.flags
= hton16( 0 );
430 if( p_buffer
->i_flags
& BLOCK_FLAG_HEADER
)
431 hdr
.flags
|= hton16( METACUBE_FLAGS_HEADER
);
432 if( p_sys
->b_has_keyframes
&& !( p_buffer
->i_flags
& BLOCK_FLAG_TYPE_I
) )
433 hdr
.flags
|= hton16( METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START
);
434 hdr
.csum
= hton16( metacube2_compute_crc( &hdr
) );
436 p_buffer
= block_Realloc( p_buffer
, sizeof( hdr
), p_buffer
->i_buffer
);
437 if( p_buffer
== NULL
) {
438 block_ChainRelease( p_next
);
441 memcpy( p_buffer
->p_buffer
, &hdr
, sizeof( hdr
) );
445 i_err
= httpd_StreamSend( p_sys
->p_httpd_stream
, p_buffer
);
447 block_Release( p_buffer
);
458 block_ChainRelease( p_buffer
);
461 return( i_err
< 0 ? VLC_EGENERIC
: i_len
);