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")
96 #define KEYFILE_TEXT N_("AES key file")
97 #define KEYFILE_LONGTEXT N_("File containing the 16 bytes encryption key")
99 #define KEYLOADFILE_TEXT N_("File where vlc reads key-uri and keyfile-location")
100 #define KEYLOADFILE_LONGTEXT N_("File is read when segment starts and is assumet to be in format: "\
101 "key-uri\\nkey-file. File is read on the segment opening and "\
102 "values are used on that segment.")
104 #define RANDOMIV_TEXT N_("Use randomized IV for encryption")
105 #define RANDOMIV_LONGTEXT N_("Generate IV instead using segment-number as IV")
108 set_description( N_("HTTP Live streaming output") )
109 set_shortname( N_("LiveHTTP" ))
110 add_shortcut( "livehttp" )
111 set_capability( "sout access", 0 )
112 set_category( CAT_SOUT )
113 set_subcategory( SUBCAT_SOUT_ACO )
114 add_integer( SOUT_CFG_PREFIX "seglen", 10, SEGLEN_TEXT, SEGLEN_LONGTEXT, false )
115 add_integer( SOUT_CFG_PREFIX "numsegs", 0, NUMSEGS_TEXT, NUMSEGS_LONGTEXT, false )
116 add_bool( SOUT_CFG_PREFIX "splitanywhere", false,
117 SPLITANYWHERE_TEXT, SPLITANYWHERE_LONGTEXT, true )
118 add_bool( SOUT_CFG_PREFIX "delsegs", true,
119 DELSEGS_TEXT, DELSEGS_LONGTEXT, true )
120 add_bool( SOUT_CFG_PREFIX "ratecontrol", false,
121 RATECONTROL_TEXT, RATECONTROL_TEXT, true )
122 add_bool( SOUT_CFG_PREFIX "caching", false,
123 NOCACHE_TEXT, NOCACHE_LONGTEXT, true )
124 add_bool( SOUT_CFG_PREFIX "generate-iv", false,
125 RANDOMIV_TEXT, RANDOMIV_LONGTEXT, true )
126 add_string( SOUT_CFG_PREFIX "index", NULL,
127 INDEX_TEXT, INDEX_LONGTEXT, false )
128 add_string( SOUT_CFG_PREFIX "index-url", NULL,
129 INDEXURL_TEXT, INDEXURL_LONGTEXT, false )
130 add_string( SOUT_CFG_PREFIX "key-uri", NULL,
131 KEYURI_TEXT, KEYURI_TEXT, true )
132 add_loadfile( SOUT_CFG_PREFIX "key-file", NULL,
133 KEYFILE_TEXT, KEYFILE_LONGTEXT, true )
134 add_loadfile( SOUT_CFG_PREFIX "key-loadfile", NULL,
135 KEYLOADFILE_TEXT, KEYLOADFILE_LONGTEXT, true )
136 set_callbacks( Open, Close )
140 /*****************************************************************************
141 * Exported prototypes
142 *****************************************************************************/
143 static const char *const ppsz_sout_options[] = {
159 static ssize_t Write( sout_access_out_t *, block_t * );
160 static int Seek ( sout_access_out_t *, off_t );
161 static int Control( sout_access_out_t *, int, va_list );
163 typedef struct output_segment
169 uint32_t i_segment_number;
173 struct sout_access_out_sys_t
175 char *psz_cursegPath;
179 mtime_t i_keyfile_modification;
185 block_t *block_buffer;
190 bool b_splitanywhere;
194 gcry_cipher_hd_t aes_ctx;
196 uint8_t stuffing_bytes[16];
197 ssize_t stuffing_size;
198 vlc_array_t *segments_t;
201 static int LoadCryptFile( sout_access_out_t *p_access);
202 static int CryptSetup( sout_access_out_t *p_access, char *keyfile );
203 /*****************************************************************************
204 * Open: open the file
205 *****************************************************************************/
206 static int Open( vlc_object_t *p_this )
208 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
209 sout_access_out_sys_t *p_sys;
212 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
214 if( !p_access->psz_path )
216 msg_Err( p_access, "no file name specified" );
220 if( unlikely( !( p_sys = malloc ( sizeof( *p_sys ) ) ) ) )
223 p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
224 /* Try to get within asked segment length */
225 p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen;
226 p_sys->block_buffer = NULL;
228 p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
229 p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
230 p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
231 p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
232 p_sys->b_caching = var_GetBool( p_access, SOUT_CFG_PREFIX "caching") ;
233 p_sys->b_generate_iv = var_GetBool( p_access, SOUT_CFG_PREFIX "generate-iv") ;
235 p_sys->segments_t = vlc_array_new();
237 p_sys->stuffing_size = 0;
239 p_sys->psz_indexPath = NULL;
240 psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
244 psz_tmp = str_format_time( psz_idx );
251 path_sanitize( psz_tmp );
252 p_sys->psz_indexPath = psz_tmp;
253 vlc_unlink( p_sys->psz_indexPath );
256 p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
257 p_sys->psz_keyfile = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-loadfile" );
258 p_sys->key_uri = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-uri" );
260 p_access->p_sys = p_sys;
262 if( p_sys->psz_keyfile && ( LoadCryptFile( p_access ) < 0 ) )
264 free( p_sys->psz_indexUrl );
265 free( p_sys->psz_indexPath );
267 msg_Err( p_access, "Encryption init failed" );
270 else if( !p_sys->psz_keyfile && ( CryptSetup( p_access, NULL ) < 0 ) )
272 free( p_sys->psz_indexUrl );
273 free( p_sys->psz_indexPath );
275 msg_Err( p_access, "Encryption init failed" );
279 p_sys->i_handle = -1;
280 p_sys->i_segment = 0;
281 p_sys->psz_cursegPath = NULL;
283 p_access->pf_write = Write;
284 p_access->pf_seek = Seek;
285 p_access->pf_control = Control;
290 /************************************************************************
291 * CryptSetup: Initialize encryption
292 ************************************************************************/
293 static int CryptSetup( sout_access_out_t *p_access, char *key_file )
295 sout_access_out_sys_t *p_sys = p_access->p_sys;
297 char *keyfile = NULL;
300 keyfile = strdup( key_file );
302 keyfile = var_InheritString( p_access, SOUT_CFG_PREFIX "key-file" );
304 if( !p_sys->key_uri ) /*No key uri, assume no encryption wanted*/
306 msg_Dbg( p_access, "No key uri, no encryption");
311 /*Setup encryption cipher*/
312 gcry_error_t err = gcry_cipher_open( &p_sys->aes_ctx, GCRY_CIPHER_AES,
313 GCRY_CIPHER_MODE_CBC, 0 );
316 msg_Err( p_access, "Openin AES Cipher failed: %s", gpg_strerror(err));
320 if( unlikely(keyfile == NULL) )
322 msg_Err( p_access, "No key-file, no encryption" );
326 int keyfd = vlc_open( keyfile, O_RDONLY | O_NONBLOCK );
327 if( unlikely( keyfd == -1 ) )
329 msg_Err( p_access, "Unable to open keyfile %s: %m", keyfile );
335 ssize_t keylen = read( keyfd, key, 16 );
340 msg_Err( p_access, "No key at least 16 octects (you provided %zd), no encryption", keylen );
344 err = gcry_cipher_setkey( p_sys->aes_ctx, key, 16 );
347 msg_Err(p_access, "Setting AES key failed: %s", gpg_strerror(err));
348 gcry_cipher_close( p_sys->aes_ctx);
352 if( p_sys->b_generate_iv )
353 vlc_rand_bytes( p_sys->aes_ivs, sizeof(uint8_t)*16);
359 /************************************************************************
360 * LoadCryptFile: Try to parse key_uri and keyfile-location from file
361 ************************************************************************/
362 static int LoadCryptFile( sout_access_out_t *p_access )
364 sout_access_out_sys_t *p_sys = p_access->p_sys;
366 FILE *stream = vlc_fopen( p_sys->psz_keyfile, "rt" );
367 char *key_file=NULL,*key_uri=NULL;
369 if( unlikely( stream == NULL ) )
371 msg_Err( p_access, "Unable to open keyloadfile %s: %m", p_sys->psz_keyfile );
377 ssize_t len = getline( &key_uri, &(size_t){0}, stream );
378 if( unlikely( len == -1 ) )
380 msg_Err( p_access, "Cannot read %s: %m", p_sys->psz_keyfile );
386 //Strip the newline from uri, maybe scanf would be better?
389 len = getline( &key_file, &(size_t){0}, stream );
390 if( unlikely( len == -1 ) )
392 msg_Err( p_access, "Cannot read %s: %m", p_sys->psz_keyfile );
400 // Strip the last newline from filename
401 key_file[len-1]='\0';
404 int returncode = VLC_SUCCESS;
405 if( !p_sys->key_uri || strcmp( p_sys->key_uri, key_uri ) )
409 free( p_sys->key_uri );
410 p_sys->key_uri = NULL;
412 p_sys->key_uri = strdup( key_uri );
413 returncode = CryptSetup( p_access, key_file );
420 /************************************************************************
421 * CryptKey: Set encryption IV to current segment number
422 ************************************************************************/
423 static int CryptKey( sout_access_out_t *p_access, uint32_t i_segment )
425 sout_access_out_sys_t *p_sys = p_access->p_sys;
427 if( !p_sys->b_generate_iv )
429 /* Use segment number as IV if randomIV isn't selected*/
430 memset( p_sys->aes_ivs, 0, 16 * sizeof(uint8_t));
431 p_sys->aes_ivs[15] = i_segment & 0xff;
432 p_sys->aes_ivs[14] = (i_segment >> 8 ) & 0xff;
433 p_sys->aes_ivs[13] = (i_segment >> 16 ) & 0xff;
434 p_sys->aes_ivs[12] = (i_segment >> 24 ) & 0xff;
437 gcry_error_t err = gcry_cipher_setiv( p_sys->aes_ctx,
441 msg_Err(p_access, "Setting AES IVs failed: %s", gpg_strerror(err) );
442 gcry_cipher_close( p_sys->aes_ctx);
449 #define SEG_NUMBER_PLACEHOLDER "#"
450 /*****************************************************************************
451 * formatSegmentPath: create segment path name based on seg #
452 *****************************************************************************/
453 static char *formatSegmentPath( char *psz_path, uint32_t i_seg, bool b_sanitize )
456 char *psz_firstNumSign;
458 if ( ! ( psz_result = str_format_time( psz_path ) ) )
461 psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
462 if ( *psz_firstNumSign )
465 int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
468 *psz_firstNumSign = '\0';
469 ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
473 psz_result = psz_newResult;
477 path_sanitize( psz_result );
482 static void destroySegment( output_segment_t *segment )
484 free( segment->psz_filename );
485 free( segment->psz_duration );
486 free( segment->psz_uri );
487 free( segment->psz_key_uri );
491 /************************************************************************
492 * updateIndexAndDel: If necessary, update index file & delete old segments
493 ************************************************************************/
494 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
499 if ( p_sys->i_numsegs == 0 || p_sys->i_segment < p_sys->i_numsegs )
502 i_firstseg = ( p_sys->i_segment - p_sys->i_numsegs ) + 1;
504 // First update index
505 if ( p_sys->psz_indexPath )
510 if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
513 fp = vlc_fopen( psz_idxTmp, "wt");
516 msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
521 if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%zu\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:%s"
522 "%s\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n", p_sys->i_seglen,
523 p_sys->b_caching ? "YES" : "NO",
524 p_sys->i_numsegs > 0 ? "" : b_isend ? "\n#EXT-X-PLAYLIST-TYPE:VOD" : "\n#EXT-X-PLAYLIST-TYPE:EVENT",
531 char *psz_current_uri=NULL;
534 for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
536 //scale to 0..numsegs-1
537 uint32_t index = i-i_firstseg;
539 output_segment_t *segment = (output_segment_t *)vlc_array_item_at_index( p_sys->segments_t, index );
540 if( p_sys->key_uri &&
541 ( !psz_current_uri || strcmp( psz_current_uri, segment->psz_key_uri ) )
545 free( psz_current_uri );
546 psz_current_uri = strdup( segment->psz_key_uri );
547 if( p_sys->b_generate_iv )
549 unsigned long long iv_hi = 0, iv_lo = 0;
550 for( unsigned short i = 0; i < 8; i++ )
552 iv_hi |= segment->aes_ivs[i] & 0xff;
554 iv_lo |= segment->aes_ivs[8+i] & 0xff;
557 ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\",IV=0X%16.16llx%16.16llx\n",
558 segment->psz_key_uri, iv_hi, iv_lo );
561 ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"\n", segment->psz_key_uri );
571 val = fprintf( fp, "#EXTINF:%s,\n%s\n", segment->psz_duration, segment->psz_uri);
578 free( psz_current_uri );
582 if ( fputs ( STR_ENDLIST, fp ) < 0)
592 val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
596 vlc_unlink( psz_idxTmp );
597 msg_Err( p_access, "Error moving LiveHttp index file" );
600 msg_Dbg( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
605 // Then take care of deletion
606 while( p_sys->b_delsegs && p_sys->i_numsegs && ( (vlc_array_count( p_sys->segments_t ) ) >= p_sys->i_numsegs ) )
608 output_segment_t *segment = vlc_array_item_at_index( p_sys->segments_t, 0 );
609 vlc_array_remove( p_sys->segments_t, 0 );
610 if ( segment->psz_filename )
612 vlc_unlink( segment->psz_filename );
614 destroySegment( segment );
621 /*****************************************************************************
622 * closeCurrentSegment: Close the segment file
623 *****************************************************************************/
624 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
626 if ( p_sys->i_handle >= 0 )
628 output_segment_t *segment = (output_segment_t *)vlc_array_item_at_index( p_sys->segments_t, vlc_array_count( p_sys->segments_t ) - 1 );
632 size_t pad = 16 - p_sys->stuffing_size;
633 memset(&p_sys->stuffing_bytes[p_sys->stuffing_size], pad, pad);
634 gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx, p_sys->stuffing_bytes, 16, NULL, 0 );
637 msg_Err( p_access, "Couldn't encrypt 16 bytes: %s", gpg_strerror(err) );
639 int ret = write( p_sys->i_handle, p_sys->stuffing_bytes, 16 );
641 msg_Err( p_access, "Couldn't write 16 bytes" );
643 p_sys->stuffing_size = 0;
647 close( p_sys->i_handle );
648 p_sys->i_handle = -1;
650 if( ! ( us_asprintf( &segment->psz_duration, "%.2f", p_sys->f_seglen ) ) )
652 msg_Err( p_access, "Couldn't set duration on closed segment");
656 segment->i_segment_number = p_sys->i_segment;
658 if ( p_sys->psz_cursegPath )
660 msg_Dbg( p_access, "LiveHttpSegmentComplete: %s (%"PRIu32")" , p_sys->psz_cursegPath, p_sys->i_segment );
661 free( p_sys->psz_cursegPath );
662 p_sys->psz_cursegPath = 0;
663 updateIndexAndDel( p_access, p_sys, b_isend );
668 /*****************************************************************************
669 * Close: close the target
670 *****************************************************************************/
671 static void Close( vlc_object_t * p_this )
673 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
674 sout_access_out_sys_t *p_sys = p_access->p_sys;
676 msg_Dbg( p_access, "Flushing buffer to last file");
677 bool crypted = false;
678 while( p_sys->block_buffer )
680 if( p_sys->key_uri && !crypted)
682 if( p_sys->stuffing_size )
684 p_sys->block_buffer = block_Realloc( p_sys->block_buffer, p_sys->stuffing_size, p_sys->block_buffer->i_buffer );
685 if( unlikely(!p_sys->block_buffer) )
687 memcpy( p_sys->block_buffer->p_buffer, p_sys->stuffing_bytes, p_sys->stuffing_size );
688 p_sys->stuffing_size = 0;
690 size_t original = p_sys->block_buffer->i_buffer;
691 size_t padded = (original + 15 ) & ~15;
692 size_t pad = padded - original;
695 p_sys->stuffing_size = 16 - pad;
696 p_sys->block_buffer->i_buffer -= p_sys->stuffing_size;
697 memcpy( p_sys->stuffing_bytes, &p_sys->block_buffer->p_buffer[p_sys->block_buffer->i_buffer], p_sys->stuffing_size );
700 gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx,
701 p_sys->block_buffer->p_buffer, p_sys->block_buffer->i_buffer, NULL, 0 );
704 msg_Err( p_access, "Encryption failure: %s ", gpg_strerror(err) );
709 ssize_t val = write( p_sys->i_handle, p_sys->block_buffer->p_buffer, p_sys->block_buffer->i_buffer );
712 if ( errno == EINTR )
714 block_ChainRelease ( p_sys->block_buffer);
717 if( !p_sys->block_buffer->p_next )
719 p_sys->f_seglen = (float)( p_sys->block_buffer->i_length / (1000000)) +
720 (float)(p_sys->block_buffer->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
723 if ( likely( (size_t)val >= p_sys->block_buffer->i_buffer ) )
725 block_t *p_next = p_sys->block_buffer->p_next;
726 block_Release (p_sys->block_buffer);
727 p_sys->block_buffer = p_next;
732 p_sys->block_buffer->p_buffer += val;
733 p_sys->block_buffer->i_buffer -= val;
737 closeCurrentSegment( p_access, p_sys, true );
741 gcry_cipher_close( p_sys->aes_ctx );
742 free( p_sys->key_uri );
745 while( vlc_array_count( p_sys->segments_t ) > 0 )
747 output_segment_t *segment = vlc_array_item_at_index( p_sys->segments_t, 0 );
748 vlc_array_remove( p_sys->segments_t, 0 );
749 destroySegment( segment );
751 vlc_array_destroy( p_sys->segments_t );
753 free( p_sys->psz_indexUrl );
754 free( p_sys->psz_indexPath );
757 msg_Dbg( p_access, "livehttp access output closed" );
760 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
762 sout_access_out_sys_t *p_sys = p_access->p_sys;
766 case ACCESS_OUT_CONTROLS_PACE:
768 bool *pb = va_arg( args, bool * );
769 *pb = !p_sys->b_ratecontrol;
780 /*****************************************************************************
781 * openNextFile: Open the segment file
782 *****************************************************************************/
783 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
787 uint32_t i_newseg = p_sys->i_segment + 1;
789 /* Create segment and fill it info that we can (everything excluding duration */
790 output_segment_t *segment = (output_segment_t*)malloc(sizeof(output_segment_t));
791 if( unlikely( !segment ) )
794 memset( segment, 0 , sizeof( output_segment_t ) );
796 segment->i_segment_number = i_newseg;
797 segment->psz_filename = formatSegmentPath( p_access->psz_path, i_newseg, true );
798 char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
799 segment->psz_uri = formatSegmentPath( psz_idxFormat , i_newseg, false );
801 if ( unlikely( !segment->psz_filename ) )
803 msg_Err( p_access, "Format segmentpath failed");
807 fd = vlc_open( segment->psz_filename, O_WRONLY | O_CREAT | O_LARGEFILE |
811 msg_Err( p_access, "cannot open `%s' (%m)", segment->psz_filename );
812 destroySegment( segment );
816 vlc_array_append( p_sys->segments_t, segment);
818 if( p_sys->psz_keyfile )
820 LoadCryptFile( p_access );
825 segment->psz_key_uri = strdup( p_sys->key_uri );
826 CryptKey( p_access, i_newseg );
827 if( p_sys->b_generate_iv )
828 memcpy( segment->aes_ivs, p_sys->aes_ivs, sizeof(uint8_t)*16 );
830 msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , segment->psz_filename, i_newseg );
832 p_sys->psz_cursegPath = strdup(segment->psz_filename);
833 p_sys->i_handle = fd;
834 p_sys->i_segment = i_newseg;
838 /*****************************************************************************
839 * Write: standard write on a file descriptor.
840 *****************************************************************************/
841 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
844 sout_access_out_sys_t *p_sys = p_access->p_sys;
849 if ( ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_HEADER ) ) )
851 bool crypted = false;
852 block_t *output = p_sys->block_buffer;
853 p_sys->block_buffer = NULL;
856 if( p_sys->i_handle > 0 &&
857 ( p_buffer->i_dts - p_sys->i_opendts +
858 p_buffer->i_length * CLOCK_FREQ / INT64_C(1000000)
859 ) >= p_sys->i_seglenm )
860 closeCurrentSegment( p_access, p_sys, false );
862 if ( p_sys->i_handle < 0 )
864 p_sys->i_opendts = output ? output->i_dts : p_buffer->i_dts;
865 if ( openNextFile( p_access, p_sys ) < 0 )
871 if( p_sys->key_uri && !crypted )
873 if( p_sys->stuffing_size )
875 output = block_Realloc( output, p_sys->stuffing_size, output->i_buffer );
876 if( unlikely(!output ) )
878 memcpy( output->p_buffer, p_sys->stuffing_bytes, p_sys->stuffing_size );
879 p_sys->stuffing_size = 0;
881 size_t original = output->i_buffer;
882 size_t padded = (output->i_buffer + 15 ) & ~15;
883 size_t pad = padded - original;
886 p_sys->stuffing_size = 16-pad;
887 output->i_buffer -= p_sys->stuffing_size;
888 memcpy(p_sys->stuffing_bytes, &output->p_buffer[output->i_buffer], p_sys->stuffing_size);
891 gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx,
892 output->p_buffer, output->i_buffer, NULL, 0 );
895 msg_Err( p_access, "Encryption failure: %s ", gpg_strerror(err) );
901 ssize_t val = write( p_sys->i_handle, output->p_buffer, output->i_buffer );
904 if ( errno == EINTR )
906 block_ChainRelease ( p_buffer );
910 (float)output->i_length / INT64_C(1000000) +
911 (float)(output->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
913 if ( (size_t)val >= output->i_buffer )
915 block_t *p_next = output->p_next;
916 block_Release (output);
922 output->p_buffer += val;
923 output->i_buffer -= val;
929 p_temp = p_buffer->p_next;
930 p_buffer->p_next = NULL;
931 block_ChainAppend( &p_sys->block_buffer, p_buffer );
938 /*****************************************************************************
939 * Seek: seek to a specific location in a file
940 *****************************************************************************/
941 static int Seek( sout_access_out_t *p_access, off_t i_pos )
944 msg_Err( p_access, "livehttp sout access cannot seek" );