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 Seek ( sout_access_out_t
*, off_t
);
94 static int Control( sout_access_out_t
*, int, va_list );
96 struct sout_access_out_sys_t
99 httpd_host_t
*p_httpd_host
;
102 httpd_stream_t
*p_httpd_stream
;
104 /* gather header from stream */
105 int i_header_allocated
;
108 bool b_header_complete
;
110 bool b_has_keyframes
;
113 /* Definitions for the Metacube2 protocol, used to communicate with Cubemap. */
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
119 struct metacube2_block_header
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. */
128 * Implementation of Metacube2 utility functions. Taken from the Cubemap
129 * distribution and then relicensed by the author to LGPL2.1+ for inclusion
134 * https://www.ece.cmu.edu/~koopman/pubs/KoopmanCRCWebinar9May2012.pdf
135 * recommends this for messages as short as ours (see table at page 34).
137 #define METACUBE2_CRC_POLYNOMIAL 0x8FDB
139 /* Semi-random starting value to make sure all-zero won't pass. */
140 #define METACUBE2_CRC_START 0x1234
142 /* This code is based on code generated by pycrc. */
143 static uint16_t metacube2_compute_crc(const struct metacube2_block_header
*hdr
)
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
;
149 for (int i
= 0; i
< data_len
; ++i
) {
151 for (int j
= 0; j
< 8; j
++) {
152 int bit
= crc
& 0x8000;
153 crc
= (crc
<< 1) | ((c
>> (7 - j
)) & 0x01);
155 crc
^= METACUBE2_CRC_POLYNOMIAL
;
161 for (int i
= 0; i
< 16; i
++) {
162 int bit
= crc
& 0x8000;
165 crc
^= METACUBE2_CRC_POLYNOMIAL
;
172 /*****************************************************************************
173 * Open: open the file
174 *****************************************************************************/
175 static int Open( vlc_object_t
*p_this
)
177 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
178 sout_access_out_sys_t
*p_sys
;
184 if( !( p_sys
= p_access
->p_sys
=
185 malloc( sizeof( sout_access_out_sys_t
) ) ) )
188 config_ChainParse( p_access
, SOUT_CFG_PREFIX
, ppsz_sout_options
, p_access
->p_cfg
);
190 const char *path
= p_access
->psz_path
;
191 path
+= strcspn( path
, "/" );
192 if( path
> p_access
->psz_path
)
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
)
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." );
207 strncpy( host
, p_access
->psz_path
, len
);
210 var_Create( p_access
, "http-host", VLC_VAR_STRING
);
211 var_SetString( p_access
, "http-host", host
);
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 ); */
222 int bind_port
= atoi( port
);
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
);
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
) );
239 p_sys
->p_httpd_host
= vlc_http_HostNew( VLC_OBJECT(p_access
) );
241 if( p_sys
->p_httpd_host
== NULL
)
243 msg_Err( p_access
, "cannot start HTTP server" );
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" ) )
252 psz_mime
= strdup( "video/x-ms-asf-stream" );
256 psz_mime
= var_GetNonEmptyString( p_access
, SOUT_CFG_PREFIX
"mime" );
259 p_sys
->b_metacube
= var_GetBool( p_access
, SOUT_CFG_PREFIX
"metacube" );
260 p_sys
->b_has_keyframes
= false;
262 p_sys
->p_httpd_stream
=
263 httpd_StreamNew( p_sys
->p_httpd_host
, path
, psz_mime
,
269 if( p_sys
->p_httpd_stream
== NULL
)
271 msg_Err( p_access
, "cannot add stream %s", path
);
272 httpd_HostDelete( p_sys
->p_httpd_host
);
278 if( p_sys
->b_metacube
)
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
)
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;
294 p_access
->pf_write
= Write
;
295 p_access
->pf_seek
= Seek
;
296 p_access
->pf_control
= Control
;
301 /*****************************************************************************
302 * Close: close the target
303 *****************************************************************************/
304 static void Close( vlc_object_t
* p_this
)
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
;
309 httpd_StreamDelete( p_sys
->p_httpd_stream
);
310 httpd_HostDelete( p_sys
->p_httpd_host
);
312 free( p_sys
->p_header
);
314 msg_Dbg( p_access
, "Close" );
319 static int Control( sout_access_out_t
*p_access
, int i_query
, va_list args
)
325 case ACCESS_OUT_CONTROLS_PACE
:
326 *va_arg( args
, bool * ) = false;
335 /*****************************************************************************
337 *****************************************************************************/
338 static ssize_t
Write( sout_access_out_t
*p_access
, block_t
*p_buffer
)
340 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
348 if( p_buffer
->i_flags
& BLOCK_FLAG_HEADER
)
351 if( p_sys
->b_header_complete
)
353 /* free previously gathered header */
354 p_sys
->i_header_size
= 0;
355 p_sys
->b_header_complete
= false;
357 if( (int)(p_buffer
->i_buffer
+ p_sys
->i_header_size
) >
358 p_sys
->i_header_allocated
)
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
);
365 memcpy( &p_sys
->p_header
[p_sys
->i_header_size
],
367 p_buffer
->i_buffer
);
368 p_sys
->i_header_size
+= p_buffer
->i_buffer
;
370 else if( !p_sys
->b_header_complete
)
372 p_sys
->b_header_complete
= true;
374 if ( p_sys
->b_metacube
)
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
) );
382 int i_header_size
= p_sys
->i_header_size
+ sizeof( hdr
);
383 block_t
*p_hdr_block
= block_Alloc( i_header_size
);
384 if( p_hdr_block
== NULL
) {
385 block_ChainRelease( p_buffer
);
388 p_hdr_block
->i_flags
= 0;
389 memcpy( p_hdr_block
->p_buffer
, &hdr
, sizeof( hdr
) );
390 memcpy( p_hdr_block
->p_buffer
+ sizeof( hdr
), p_sys
->p_header
, p_sys
->i_header_size
);
392 /* send the combined header here instead of sending them as regular
393 * data, so that we get them as a single Metacube header block */
394 httpd_StreamHeader( p_sys
->p_httpd_stream
, p_hdr_block
->p_buffer
, p_hdr_block
->i_buffer
);
395 httpd_StreamSend( p_sys
->p_httpd_stream
, p_hdr_block
);
397 block_Release( p_hdr_block
);
401 httpd_StreamHeader( p_sys
->p_httpd_stream
, p_sys
->p_header
,
402 p_sys
->i_header_size
);
406 i_len
+= p_buffer
->i_buffer
;
408 if( p_buffer
->i_flags
& BLOCK_FLAG_TYPE_I
)
410 p_sys
->b_has_keyframes
= true;
413 p_next
= p_buffer
->p_next
;
415 if( p_sys
->b_metacube
)
417 /* header data is combined into one packet and sent earlier */
418 if( p_buffer
->i_flags
& BLOCK_FLAG_HEADER
) {
419 block_Release( p_buffer
);
424 /* prepend Metacube header */
425 struct metacube2_block_header hdr
;
426 memcpy( hdr
.sync
, METACUBE2_SYNC
, sizeof( METACUBE2_SYNC
) );
427 hdr
.size
= hton32( p_buffer
->i_buffer
);
428 hdr
.flags
= hton16( 0 );
429 if( p_buffer
->i_flags
& BLOCK_FLAG_HEADER
)
430 hdr
.flags
|= hton16( METACUBE_FLAGS_HEADER
);
431 if( p_sys
->b_has_keyframes
&& !( p_buffer
->i_flags
& BLOCK_FLAG_TYPE_I
) )
432 hdr
.flags
|= hton16( METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START
);
433 hdr
.csum
= hton16( metacube2_compute_crc( &hdr
) );
435 p_buffer
= block_Realloc( p_buffer
, sizeof( hdr
), p_buffer
->i_buffer
);
436 if( p_buffer
== NULL
) {
437 block_ChainRelease( p_next
);
440 memcpy( p_buffer
->p_buffer
, &hdr
, sizeof( hdr
) );
444 i_err
= httpd_StreamSend( p_sys
->p_httpd_stream
, p_buffer
);
446 block_Release( p_buffer
);
457 block_ChainRelease( p_buffer
);
460 return( i_err
< 0 ? VLC_EGENERIC
: i_len
);
463 /*****************************************************************************
464 * Seek: seek to a specific location in a file
465 *****************************************************************************/
466 static int Seek( sout_access_out_t
*p_access
, off_t i_pos
)
469 msg_Warn( p_access
, "HTTP sout access cannot seek" );