1 /*****************************************************************************
2 * livehttp.c: Live HTTP Streaming
3 *****************************************************************************
4 * Copyright © 2001, 2002, 2013 VLC authors and VideoLAN
5 * Copyright © 2009-2010 by Keary Griffin
7 * Authors: Keary Griffin <kearygriffin at gmail.com>
8 * Ilkka Ollakka <ileoo at videolan dot org>
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 <sys/types.h>
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
44 #include <vlc_block.h>
46 #include <vlc_strings.h>
47 #include <vlc_charset.h>
50 #include <vlc_gcrypt.h>
55 # define O_LARGEFILE 0
58 #define STR_ENDLIST "#EXT-X-ENDLIST\n"
60 #define MAX_RENAME_RETRIES 10
62 /*****************************************************************************
64 *****************************************************************************/
65 static int Open ( vlc_object_t * );
66 static void Close( vlc_object_t * );
68 #define SOUT_CFG_PREFIX "sout-livehttp-"
69 #define SEGLEN_TEXT N_("Segment length")
70 #define SEGLEN_LONGTEXT N_("Length of TS stream segments")
72 #define SPLITANYWHERE_TEXT N_("Split segments anywhere")
73 #define SPLITANYWHERE_LONGTEXT N_("Don't require a keyframe before splitting "\
74 "a segment. Needed for audio only.")
76 #define NUMSEGS_TEXT N_("Number of segments")
77 #define NUMSEGS_LONGTEXT N_("Number of segments to include in index")
79 #define NOCACHE_TEXT N_("Allow cache")
80 #define NOCACHE_LONGTEXT N_("Add EXT-X-ALLOW-CACHE:NO directive in playlist-file if this is disabled")
82 #define INDEX_TEXT N_("Index file")
83 #define INDEX_LONGTEXT N_("Path to the index file to create")
85 #define INDEXURL_TEXT N_("Full URL to put in index file")
86 #define INDEXURL_LONGTEXT N_("Full URL to put in index file. "\
87 "Use #'s to represent segment number")
89 #define DELSEGS_TEXT N_("Delete segments")
90 #define DELSEGS_LONGTEXT N_("Delete segments when they are no longer needed")
92 #define RATECONTROL_TEXT N_("Use muxers rate control mechanism")
94 #define KEYURI_TEXT N_("AES key URI to place in playlist")
95 #define KEYURI_LONGTEXT N_("Location from where client will retrieve the stream decryption key")
97 #define KEYFILE_TEXT N_("AES key file")
98 #define KEYFILE_LONGTEXT N_("File containing the 16 bytes encryption key")
100 #define RANDOMIV_TEXT N_("Use randomized IV for encryption")
101 #define RANDOMIV_LONGTEXT N_("Generate IV instead using segment-number as IV")
104 set_description( N_("HTTP Live streaming output") )
105 set_shortname( N_("LiveHTTP" ))
106 add_shortcut( "livehttp" )
107 set_capability( "sout access", 0 )
108 set_category( CAT_SOUT )
109 set_subcategory( SUBCAT_SOUT_ACO )
110 add_integer( SOUT_CFG_PREFIX "seglen", 10, SEGLEN_TEXT, SEGLEN_LONGTEXT, false )
111 add_integer( SOUT_CFG_PREFIX "numsegs", 0, NUMSEGS_TEXT, NUMSEGS_LONGTEXT, false )
112 add_bool( SOUT_CFG_PREFIX "splitanywhere", false,
113 SPLITANYWHERE_TEXT, SPLITANYWHERE_LONGTEXT, true )
114 add_bool( SOUT_CFG_PREFIX "delsegs", true,
115 DELSEGS_TEXT, DELSEGS_LONGTEXT, true )
116 add_bool( SOUT_CFG_PREFIX "ratecontrol", false,
117 RATECONTROL_TEXT, RATECONTROL_TEXT, true )
118 add_bool( SOUT_CFG_PREFIX "caching", false,
119 NOCACHE_TEXT, NOCACHE_LONGTEXT, true )
120 add_bool( SOUT_CFG_PREFIX "generate-iv", false,
121 RANDOMIV_TEXT, RANDOMIV_LONGTEXT, true )
122 add_string( SOUT_CFG_PREFIX "index", NULL,
123 INDEX_TEXT, INDEX_LONGTEXT, false )
124 add_string( SOUT_CFG_PREFIX "index-url", NULL,
125 INDEXURL_TEXT, INDEXURL_LONGTEXT, false )
126 add_string( SOUT_CFG_PREFIX "key-uri", NULL,
127 KEYURI_TEXT, KEYURI_TEXT, true )
128 add_loadfile( SOUT_CFG_PREFIX "key-file", NULL,
129 KEYFILE_TEXT, KEYFILE_LONGTEXT, true )
130 set_callbacks( Open, Close )
134 /*****************************************************************************
135 * Exported prototypes
136 *****************************************************************************/
137 static const char *const ppsz_sout_options[] = {
152 static ssize_t Write( sout_access_out_t *, block_t * );
153 static int Seek ( sout_access_out_t *, off_t );
154 static int Control( sout_access_out_t *, int, va_list );
156 struct sout_access_out_sys_t
158 char *psz_cursegPath;
166 block_t *block_buffer;
172 bool b_splitanywhere;
176 gcry_cipher_hd_t aes_ctx;
180 static int CryptSetup( sout_access_out_t *p_access );
181 /*****************************************************************************
182 * Open: open the file
183 *****************************************************************************/
184 static int Open( vlc_object_t *p_this )
186 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
187 sout_access_out_sys_t *p_sys;
190 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
192 if( !p_access->psz_path )
194 msg_Err( p_access, "no file name specified" );
198 if( unlikely( !( p_sys = malloc ( sizeof( *p_sys ) ) ) ) )
201 p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
202 /* Try to get within asked segment length */
203 p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen;
204 p_sys->block_buffer = NULL;
206 p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
207 p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
208 p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
209 p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
210 p_sys->b_caching = var_GetBool( p_access, SOUT_CFG_PREFIX "caching") ;
211 p_sys->b_generate_iv = var_GetBool( p_access, SOUT_CFG_PREFIX "generate-iv") ;
214 /* 5 elements is from harrison-stetson algorithm to start from some number
215 * if we don't have numsegs defined
217 p_sys->i_seglens = 5;
218 if( p_sys->i_numsegs )
219 p_sys->i_seglens = p_sys->i_numsegs+1;
220 p_sys->p_seglens = malloc( sizeof(float) * p_sys->i_seglens );
221 if( unlikely( !p_sys->p_seglens ) )
227 p_sys->psz_indexPath = NULL;
228 psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
232 psz_tmp = str_format_time( psz_idx );
236 free( p_sys->p_seglens );
240 path_sanitize( psz_tmp );
241 p_sys->psz_indexPath = psz_tmp;
242 vlc_unlink( p_sys->psz_indexPath );
245 p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
247 p_access->p_sys = p_sys;
249 if( CryptSetup( p_access ) < 0 )
251 free( p_sys->psz_indexUrl );
252 free( p_sys->psz_indexPath );
253 free( p_sys->p_seglens );
255 msg_Err( p_access, "Encryption init failed" );
259 p_sys->i_handle = -1;
260 p_sys->i_segment = 0;
261 p_sys->psz_cursegPath = NULL;
263 p_access->pf_write = Write;
264 p_access->pf_seek = Seek;
265 p_access->pf_control = Control;
270 /************************************************************************
271 * CryptSetup: Initialize encryption
272 ************************************************************************/
273 static int CryptSetup( sout_access_out_t *p_access )
275 sout_access_out_sys_t *p_sys = p_access->p_sys;
278 p_sys->key_uri = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-uri" );
279 if( !p_sys->key_uri ) /*No key uri, assume no encryption wanted*/
281 msg_Dbg( p_access, "No key uri, no encryption");
286 /*Setup encryption cipher*/
287 gcry_error_t err = gcry_cipher_open( &p_sys->aes_ctx, GCRY_CIPHER_AES,
288 GCRY_CIPHER_MODE_CBC, 0 );
291 msg_Err( p_access, "Openin AES Cipher failed: %s", gpg_strerror(err));
295 char *keyfile = var_InheritString( p_access, SOUT_CFG_PREFIX "key-file" );
296 if( unlikely(keyfile == NULL) )
298 msg_Err( p_access, "No key-file, no encryption" );
302 int keyfd = vlc_open( keyfile, O_RDONLY | O_NONBLOCK );
303 if( unlikely( keyfd == -1 ) )
305 msg_Err( p_access, "Unable to open keyfile %s: %m", keyfile );
311 ssize_t keylen = read( keyfd, key, 16 );
316 msg_Err( p_access, "No key at least 16 octects (you provided %zd), no encryption", keylen );
320 err = gcry_cipher_setkey( p_sys->aes_ctx, key, 16 );
323 msg_Err(p_access, "Setting AES key failed: %s", gpg_strerror(err));
324 gcry_cipher_close( p_sys->aes_ctx);
328 if( p_sys->b_generate_iv )
329 vlc_rand_bytes( p_sys->aes_ivs, sizeof(uint8_t)*16);
334 /************************************************************************
335 * CryptKey: Set encryption IV to current segment number
336 ************************************************************************/
337 static int CryptKey( sout_access_out_t *p_access, uint32_t i_segment )
339 sout_access_out_sys_t *p_sys = p_access->p_sys;
341 if( !p_sys->b_generate_iv )
343 /* Use segment number as IV if randomIV isn't selected*/
344 memset( p_sys->aes_ivs, 0, 16 * sizeof(uint8_t));
345 p_sys->aes_ivs[15] = i_segment & 0xff;
346 p_sys->aes_ivs[14] = (i_segment >> 8 ) & 0xff;
347 p_sys->aes_ivs[13] = (i_segment >> 16 ) & 0xff;
348 p_sys->aes_ivs[12] = (i_segment >> 24 ) & 0xff;
351 gcry_error_t err = gcry_cipher_setiv( p_sys->aes_ctx,
355 msg_Err(p_access, "Setting AES IVs failed: %s", gpg_strerror(err) );
356 gcry_cipher_close( p_sys->aes_ctx);
363 #define SEG_NUMBER_PLACEHOLDER "#"
364 /*****************************************************************************
365 * formatSegmentPath: create segment path name based on seg #
366 *****************************************************************************/
367 static char *formatSegmentPath( char *psz_path, uint32_t i_seg, bool b_sanitize )
370 char *psz_firstNumSign;
372 if ( ! ( psz_result = str_format_time( psz_path ) ) )
375 psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
376 if ( *psz_firstNumSign )
379 int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
382 *psz_firstNumSign = '\0';
383 ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
387 psz_result = psz_newResult;
391 path_sanitize( psz_result );
396 /************************************************************************
397 * updateIndexAndDel: If necessary, update index file & delete old segments
398 ************************************************************************/
399 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
404 if ( p_sys->i_numsegs == 0 || p_sys->i_segment < p_sys->i_numsegs )
408 if( p_sys->i_segment >= (p_sys->i_seglens-1) )
410 p_sys->i_seglens <<= 1;
411 msg_Dbg( p_access, "Segment amount %u", p_sys->i_seglens );
412 p_sys->p_seglens = realloc( p_sys->p_seglens, sizeof(float) * p_sys->i_seglens );
413 if( unlikely( !p_sys->p_seglens ) )
418 i_firstseg = ( p_sys->i_segment - p_sys->i_numsegs ) + 1;
420 // First update index
421 if ( p_sys->psz_indexPath )
426 if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
429 fp = vlc_fopen( psz_idxTmp, "wt");
432 msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
437 if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%zu\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:%s"
438 "%s\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n", p_sys->i_seglen,
439 p_sys->b_caching ? "YES" : "NO",
440 p_sys->i_numsegs > 0 ? "" : b_isend ? "\n#EXT-X-PLAYLIST-TYPE:VOD" : "\n#EXT-X-PLAYLIST-TYPE:EVENT",
451 if( p_sys->b_generate_iv )
453 unsigned long long iv_hi = 0, iv_lo = 0;
454 for( unsigned short i = 0; i < 8; i++ )
456 iv_hi |= p_sys->aes_ivs[i] & 0xff;
458 iv_lo |= p_sys->aes_ivs[8+i] & 0xff;
461 ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\",IV=0X%16.16llx%16.16llx\n",
462 p_sys->key_uri, iv_hi, iv_lo );
465 ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"\n", p_sys->key_uri );
475 char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
476 for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
479 char *psz_duration = NULL;
480 if ( ! ( psz_name = formatSegmentPath( psz_idxFormat, i, false ) ) )
486 if( ! ( us_asprintf( &psz_duration, "%.2f", p_sys->p_seglens[i % p_sys->i_seglens ], psz_name ) ) )
492 val = fprintf( fp, "#EXTINF:%s,\n%s\n", psz_duration, psz_name );
493 free( psz_duration );
505 if ( fputs ( STR_ENDLIST, fp ) < 0)
515 val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
519 vlc_unlink( psz_idxTmp );
520 msg_Err( p_access, "Error moving LiveHttp index file" );
523 msg_Info( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
528 // Then take care of deletion
529 if ( p_sys->b_delsegs && i_firstseg > 1 )
531 char *psz_name = formatSegmentPath( p_access->psz_path, i_firstseg-1, true );
534 vlc_unlink( psz_name );
541 /*****************************************************************************
542 * closeCurrentSegment: Close the segment file
543 *****************************************************************************/
544 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
546 if ( p_sys->i_handle >= 0 )
548 close( p_sys->i_handle );
549 p_sys->i_handle = -1;
550 if ( p_sys->psz_cursegPath )
552 msg_Info( p_access, "LiveHttpSegmentComplete: %s (%"PRIu32")" , p_sys->psz_cursegPath, p_sys->i_segment );
553 free( p_sys->psz_cursegPath );
554 p_sys->psz_cursegPath = 0;
555 updateIndexAndDel( p_access, p_sys, b_isend );
560 /*****************************************************************************
561 * Close: close the target
562 *****************************************************************************/
563 static void Close( vlc_object_t * p_this )
565 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
566 sout_access_out_sys_t *p_sys = p_access->p_sys;
568 msg_Dbg( p_access, "Flushing buffer to last file");
569 while( p_sys->block_buffer )
573 size_t original = p_sys->block_buffer->i_buffer;
574 size_t padded = (p_sys->block_buffer->i_buffer + 15 ) & ~15;
575 if( padded == p_sys->block_buffer->i_buffer )
577 p_sys->block_buffer = block_Realloc( p_sys->block_buffer, 0, padded );
578 if( !p_sys->block_buffer )
580 int pad = padded - original;
581 memset( &p_sys->block_buffer->p_buffer[original], pad, pad );
583 gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx,
584 p_sys->block_buffer->p_buffer, padded, NULL, 0 );
587 msg_Err( p_access, "Encryption failure: %s ", gpg_strerror(err) );
592 ssize_t val = write( p_sys->i_handle, p_sys->block_buffer->p_buffer, p_sys->block_buffer->i_buffer );
595 if ( errno == EINTR )
597 block_ChainRelease ( p_sys->block_buffer);
600 if( !p_sys->block_buffer->p_next )
602 p_sys->p_seglens[p_sys->i_segment % p_sys->i_seglens ] =
603 (float)( p_sys->block_buffer->i_length / (1000000)) +
604 (float)(p_sys->block_buffer->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
607 if ( (size_t)val >= p_sys->block_buffer->i_buffer )
609 block_t *p_next = p_sys->block_buffer->p_next;
610 block_Release (p_sys->block_buffer);
611 p_sys->block_buffer = p_next;
615 p_sys->block_buffer->p_buffer += val;
616 p_sys->block_buffer->i_buffer -= val;
620 closeCurrentSegment( p_access, p_sys, true );
621 free( p_sys->psz_indexUrl );
622 free( p_sys->psz_indexPath );
623 free( p_sys->p_seglens );
628 gcry_cipher_close( p_sys->aes_ctx );
629 free( p_sys->key_uri );
631 msg_Dbg( p_access, "livehttp access output closed" );
634 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
636 sout_access_out_sys_t *p_sys = p_access->p_sys;
640 case ACCESS_OUT_CONTROLS_PACE:
642 bool *pb = va_arg( args, bool * );
643 *pb = !p_sys->b_ratecontrol;
654 /*****************************************************************************
655 * openNextFile: Open the segment file
656 *****************************************************************************/
657 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
661 uint32_t i_newseg = p_sys->i_segment + 1;
663 char *psz_seg = formatSegmentPath( p_access->psz_path, i_newseg, true );
667 fd = vlc_open( psz_seg, O_WRONLY | O_CREAT | O_LARGEFILE |
671 msg_Err( p_access, "cannot open `%s' (%m)", psz_seg );
676 CryptKey( p_access, i_newseg );
677 msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , psz_seg, i_newseg );
680 p_sys->psz_cursegPath = psz_seg;
681 p_sys->i_handle = fd;
682 p_sys->i_segment = i_newseg;
686 /*****************************************************************************
687 * Write: standard write on a file descriptor.
688 *****************************************************************************/
689 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
692 sout_access_out_sys_t *p_sys = p_access->p_sys;
697 if ( ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_HEADER ) ) )
699 block_t *output = p_sys->block_buffer;
700 p_sys->block_buffer = NULL;
703 if( p_sys->i_handle > 0 &&
704 ( p_buffer->i_dts - p_sys->i_opendts +
705 p_buffer->i_length * CLOCK_FREQ / INT64_C(1000000)
706 ) >= p_sys->i_seglenm )
707 closeCurrentSegment( p_access, p_sys, false );
709 if ( p_sys->i_handle < 0 )
711 p_sys->i_opendts = output ? output->i_dts : p_buffer->i_dts;
712 if ( openNextFile( p_access, p_sys ) < 0 )
720 size_t original = output->i_buffer;
721 size_t padded = (output->i_buffer + 15 ) & ~15;
722 if( padded == output->i_buffer )
724 output = block_Realloc( output, 0, padded );
727 int pad = padded - original;
728 memset( &output->p_buffer[original], pad, pad );
730 gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx,
731 output->p_buffer, padded, NULL, 0 );
734 msg_Err( p_access, "Encryption failure: %s ", gpg_strerror(err) );
739 ssize_t val = write( p_sys->i_handle, output->p_buffer, output->i_buffer );
742 if ( errno == EINTR )
744 block_ChainRelease ( p_buffer );
747 p_sys->p_seglens[p_sys->i_segment % p_sys->i_seglens ] =
748 (float)output->i_length / INT64_C(1000000) +
749 (float)(output->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
751 if ( (size_t)val >= output->i_buffer )
753 block_t *p_next = output->p_next;
754 block_Release (output);
759 output->p_buffer += val;
760 output->i_buffer -= val;
766 p_temp = p_buffer->p_next;
767 p_buffer->p_next = NULL;
768 block_ChainAppend( &p_sys->block_buffer, p_buffer );
775 /*****************************************************************************
776 * Seek: seek to a specific location in a file
777 *****************************************************************************/
778 static int Seek( sout_access_out_t *p_access, off_t i_pos )
781 msg_Err( p_access, "livehttp sout access cannot seek" );