1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_block.h>
40 #include <vlc_httpd.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t
* );
46 static void Close( vlc_object_t
* );
48 #define SOUT_CFG_PREFIX "sout-http-"
50 #define USER_TEXT N_("Username")
51 #define USER_LONGTEXT N_("Username that will be " \
52 "requested to access the stream." )
53 #define PASS_TEXT N_("Password")
54 #define PASS_LONGTEXT N_("Password that will be " \
55 "requested to access the stream." )
56 #define MIME_TEXT N_("Mime")
57 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
58 "if not specified)." )
59 #define METACUBE_TEXT N_("Metacube")
60 #define METACUBE_LONGTEXT N_("Use the Metacube protocol. Needed for streaming " \
61 "to the Cubemap reflector.")
65 set_description( N_("HTTP stream output") )
66 set_capability( "sout access", 0 )
67 set_shortname( "HTTP" )
68 add_shortcut( "http", "https", "mmsh" )
69 set_category( CAT_SOUT
)
70 set_subcategory( SUBCAT_SOUT_ACO
)
71 add_string( SOUT_CFG_PREFIX
"user", "",
72 USER_TEXT
, USER_LONGTEXT
, true )
73 add_password(SOUT_CFG_PREFIX
"pwd", "", PASS_TEXT
, PASS_LONGTEXT
)
74 add_string( SOUT_CFG_PREFIX
"mime", "",
75 MIME_TEXT
, MIME_LONGTEXT
, true )
76 add_bool( SOUT_CFG_PREFIX
"metacube", false,
77 METACUBE_TEXT
, METACUBE_LONGTEXT
, true )
78 set_callbacks( Open
, Close
)
82 /*****************************************************************************
84 *****************************************************************************/
85 static const char *const ppsz_sout_options
[] = {
86 "user", "pwd", "mime", "metacube", NULL
89 static ssize_t
Write( sout_access_out_t
*, block_t
* );
90 static int Control( sout_access_out_t
*, int, va_list );
95 httpd_host_t
*p_httpd_host
;
98 httpd_stream_t
*p_httpd_stream
;
100 /* gather header from stream */
101 int i_header_allocated
;
104 bool b_header_complete
;
106 bool b_has_keyframes
;
107 } sout_access_out_sys_t
;
109 /* Definitions for the Metacube2 protocol, used to communicate with Cubemap. */
111 static const uint8_t METACUBE2_SYNC
[8] = {'c', 'u', 'b', 'e', '!', 'm', 'a', 'p'};
112 #define METACUBE_FLAGS_HEADER 0x1
113 #define METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START 0x2
115 struct metacube2_block_header
117 char sync
[8]; /* METACUBE2_SYNC */
118 uint32_t size
; /* Network byte order. Does not include header. */
119 uint16_t flags
; /* Network byte order. METACUBE_FLAGS_*. */
120 uint16_t csum
; /* Network byte order. CRC16 of size and flags. */
124 * Implementation of Metacube2 utility functions. Taken from the Cubemap
125 * distribution and then relicensed by the author to LGPL2.1+ for inclusion
130 * https://www.ece.cmu.edu/~koopman/pubs/KoopmanCRCWebinar9May2012.pdf
131 * recommends this for messages as short as ours (see table at page 34).
133 #define METACUBE2_CRC_POLYNOMIAL 0x8FDB
135 /* Semi-random starting value to make sure all-zero won't pass. */
136 #define METACUBE2_CRC_START 0x1234
138 /* This code is based on code generated by pycrc. */
139 static uint16_t metacube2_compute_crc(const struct metacube2_block_header
*hdr
)
141 static const int data_len
= sizeof(hdr
->size
) + sizeof(hdr
->flags
);
142 const uint8_t *data
= (uint8_t *)&hdr
->size
;
143 uint16_t crc
= METACUBE2_CRC_START
;
145 for (int i
= 0; i
< data_len
; ++i
) {
147 for (int j
= 0; j
< 8; j
++) {
148 int bit
= crc
& 0x8000;
149 crc
= (crc
<< 1) | ((c
>> (7 - j
)) & 0x01);
151 crc
^= METACUBE2_CRC_POLYNOMIAL
;
157 for (int i
= 0; i
< 16; i
++) {
158 int bit
= crc
& 0x8000;
161 crc
^= METACUBE2_CRC_POLYNOMIAL
;
168 /*****************************************************************************
169 * Open: open the file
170 *****************************************************************************/
171 static int Open( vlc_object_t
*p_this
)
173 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
174 sout_access_out_sys_t
*p_sys
;
180 if( !( p_sys
= p_access
->p_sys
=
181 malloc( sizeof( sout_access_out_sys_t
) ) ) )
184 config_ChainParse( p_access
, SOUT_CFG_PREFIX
, ppsz_sout_options
, p_access
->p_cfg
);
186 const char *path
= p_access
->psz_path
;
187 path
+= strcspn( path
, "/" );
188 if( path
> p_access
->psz_path
)
190 const char *port
= strrchr( p_access
->psz_path
, ':' );
191 if( port
!= NULL
&& strchr( port
, ']' ) != NULL
)
192 port
= NULL
; /* IPv6 numeral */
193 if( port
!= p_access
->psz_path
)
195 int len
= (port
? port
: path
) - p_access
->psz_path
;
196 msg_Warn( p_access
, "\"%.*s\" HTTP host might be ignored in "
197 "multiple-host configurations, use at your own risks.",
198 len
, p_access
->psz_path
);
199 msg_Info( p_access
, "Consider passing --http-host=IP on the "
200 "command line instead." );
203 strncpy( host
, p_access
->psz_path
, len
);
206 var_Create( p_access
, "http-host", VLC_VAR_STRING
);
207 var_SetString( p_access
, "http-host", host
);
211 /* int len = path - ++port;
212 msg_Info( p_access, "Consider passing --%s-port=%.*s on the "
213 "command line instead.",
214 strcasecmp( p_access->psz_access, "https" )
215 ? "http" : "https", len, port ); */
218 int bind_port
= atoi( port
);
221 const char *var
= strcasecmp( p_access
->psz_access
, "https" )
222 ? "http-port" : "https-port";
223 var_Create( p_access
, var
, VLC_VAR_INTEGER
);
224 var_SetInteger( p_access
, var
, bind_port
);
232 if( p_access
->psz_access
&& !strcmp( p_access
->psz_access
, "https" ) )
233 p_sys
->p_httpd_host
= vlc_https_HostNew( VLC_OBJECT(p_access
) );
235 p_sys
->p_httpd_host
= vlc_http_HostNew( VLC_OBJECT(p_access
) );
237 if( p_sys
->p_httpd_host
== NULL
)
239 msg_Err( p_access
, "cannot start HTTP server" );
244 psz_user
= var_GetNonEmptyString( p_access
, SOUT_CFG_PREFIX
"user" );
245 psz_pwd
= var_GetNonEmptyString( p_access
, SOUT_CFG_PREFIX
"pwd" );
246 if( p_access
->psz_access
&& !strcmp( p_access
->psz_access
, "mmsh" ) )
248 psz_mime
= strdup( "video/x-ms-asf-stream" );
252 psz_mime
= var_GetNonEmptyString( p_access
, SOUT_CFG_PREFIX
"mime" );
255 p_sys
->b_metacube
= var_GetBool( p_access
, SOUT_CFG_PREFIX
"metacube" );
256 p_sys
->b_has_keyframes
= false;
258 p_sys
->p_httpd_stream
=
259 httpd_StreamNew( p_sys
->p_httpd_host
, path
, psz_mime
,
265 if( p_sys
->p_httpd_stream
== NULL
)
267 msg_Err( p_access
, "cannot add stream %s", path
);
268 httpd_HostDelete( p_sys
->p_httpd_host
);
274 if( p_sys
->b_metacube
)
276 const httpd_header headers
[] = {
277 { (char *)"Content-encoding", (char *)"metacube" }
279 int err
= httpd_StreamSetHTTPHeaders( p_sys
->p_httpd_stream
, headers
,
280 ARRAY_SIZE(headers
) );
281 if( err
!= VLC_SUCCESS
)
288 p_sys
->i_header_allocated
= 1024;
289 p_sys
->i_header_size
= 0;
290 p_sys
->p_header
= xmalloc( p_sys
->i_header_allocated
);
291 p_sys
->b_header_complete
= false;
293 p_access
->pf_write
= Write
;
294 p_access
->pf_control
= Control
;
299 /*****************************************************************************
300 * Close: close the target
301 *****************************************************************************/
302 static void Close( vlc_object_t
* p_this
)
304 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
305 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
307 httpd_StreamDelete( p_sys
->p_httpd_stream
);
308 httpd_HostDelete( p_sys
->p_httpd_host
);
310 free( p_sys
->p_header
);
312 msg_Dbg( p_access
, "Close" );
317 static int Control( sout_access_out_t
*p_access
, int i_query
, va_list args
)
323 case ACCESS_OUT_CONTROLS_PACE
:
324 *va_arg( args
, bool * ) = false;
333 /*****************************************************************************
335 *****************************************************************************/
336 static ssize_t
Write( sout_access_out_t
*p_access
, block_t
*p_buffer
)
338 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
346 if( p_buffer
->i_flags
& BLOCK_FLAG_HEADER
)
349 if( p_sys
->b_header_complete
)
351 /* free previously gathered header */
352 p_sys
->i_header_size
= 0;
353 p_sys
->b_header_complete
= false;
355 if( (int)(p_buffer
->i_buffer
+ p_sys
->i_header_size
) >
356 p_sys
->i_header_allocated
)
358 p_sys
->i_header_allocated
=
359 p_buffer
->i_buffer
+ p_sys
->i_header_size
+ 1024;
360 p_sys
->p_header
= xrealloc( p_sys
->p_header
,
361 p_sys
->i_header_allocated
);
363 memcpy( &p_sys
->p_header
[p_sys
->i_header_size
],
365 p_buffer
->i_buffer
);
366 p_sys
->i_header_size
+= p_buffer
->i_buffer
;
368 else if( !p_sys
->b_header_complete
)
370 p_sys
->b_header_complete
= true;
372 if ( p_sys
->b_metacube
)
374 struct metacube2_block_header hdr
;
375 memcpy( hdr
.sync
, METACUBE2_SYNC
, sizeof( METACUBE2_SYNC
) );
376 hdr
.size
= hton32( p_sys
->i_header_size
);
377 hdr
.flags
= hton16( METACUBE_FLAGS_HEADER
);
378 hdr
.csum
= hton16( metacube2_compute_crc( &hdr
) );
380 int i_header_size
= p_sys
->i_header_size
+ sizeof( hdr
);
381 block_t
*p_hdr_block
= block_Alloc( i_header_size
);
382 if( p_hdr_block
== NULL
) {
383 block_ChainRelease( p_buffer
);
386 p_hdr_block
->i_flags
= 0;
387 memcpy( p_hdr_block
->p_buffer
, &hdr
, sizeof( hdr
) );
388 memcpy( p_hdr_block
->p_buffer
+ sizeof( hdr
), p_sys
->p_header
, p_sys
->i_header_size
);
390 /* send the combined header here instead of sending them as regular
391 * data, so that we get them as a single Metacube header block */
392 httpd_StreamHeader( p_sys
->p_httpd_stream
, p_hdr_block
->p_buffer
, p_hdr_block
->i_buffer
);
393 httpd_StreamSend( p_sys
->p_httpd_stream
, p_hdr_block
);
395 block_Release( p_hdr_block
);
399 httpd_StreamHeader( p_sys
->p_httpd_stream
, p_sys
->p_header
,
400 p_sys
->i_header_size
);
404 i_len
+= p_buffer
->i_buffer
;
406 if( p_buffer
->i_flags
& BLOCK_FLAG_TYPE_I
)
408 p_sys
->b_has_keyframes
= true;
411 p_next
= p_buffer
->p_next
;
413 if( p_sys
->b_metacube
)
415 /* header data is combined into one packet and sent earlier */
416 if( p_buffer
->i_flags
& BLOCK_FLAG_HEADER
) {
417 block_Release( p_buffer
);
422 /* prepend Metacube header */
423 struct metacube2_block_header hdr
;
424 memcpy( hdr
.sync
, METACUBE2_SYNC
, sizeof( METACUBE2_SYNC
) );
425 hdr
.size
= hton32( p_buffer
->i_buffer
);
426 hdr
.flags
= hton16( 0 );
427 if( p_buffer
->i_flags
& BLOCK_FLAG_HEADER
)
428 hdr
.flags
|= hton16( METACUBE_FLAGS_HEADER
);
429 if( p_sys
->b_has_keyframes
&& !( p_buffer
->i_flags
& BLOCK_FLAG_TYPE_I
) )
430 hdr
.flags
|= hton16( METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START
);
431 hdr
.csum
= hton16( metacube2_compute_crc( &hdr
) );
433 p_buffer
= block_Realloc( p_buffer
, sizeof( hdr
), p_buffer
->i_buffer
);
434 if( p_buffer
== NULL
) {
435 block_ChainRelease( p_next
);
438 memcpy( p_buffer
->p_buffer
, &hdr
, sizeof( hdr
) );
442 i_err
= httpd_StreamSend( p_sys
->p_httpd_stream
, p_buffer
);
444 block_Release( p_buffer
);
455 block_ChainRelease( p_buffer
);
458 return( i_err
< 0 ? VLC_EGENERIC
: i_len
);