codec:jpeg: set the fmt_out.i_codec early
[vlc.git] / modules / access_output / livehttp.c
blob02ff70e5bd7d98cb2e93b95a94edbfed32f8ac6e
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <sys/types.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <unistd.h>
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_sout.h>
42 #include <vlc_block.h>
43 #include <vlc_fs.h>
44 #include <vlc_strings.h>
45 #include <vlc_charset.h>
47 #include <gcrypt.h>
48 #include <vlc_gcrypt.h>
50 #include <vlc_rand.h>
52 #ifndef O_LARGEFILE
53 # define O_LARGEFILE 0
54 #endif
56 #define STR_ENDLIST "#EXT-X-ENDLIST\n"
58 #define MAX_RENAME_RETRIES 10
60 /*****************************************************************************
61 * Module descriptor
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
66 #define SOUT_CFG_PREFIX "sout-livehttp-"
67 #define SEGLEN_TEXT N_("Segment length")
68 #define SEGLEN_LONGTEXT N_("Length of TS stream segments")
70 #define SPLITANYWHERE_TEXT N_("Split segments anywhere")
71 #define SPLITANYWHERE_LONGTEXT N_("Don't require a keyframe before splitting "\
72 "a segment. Needed for audio only.")
74 #define NUMSEGS_TEXT N_("Number of segments")
75 #define NUMSEGS_LONGTEXT N_("Number of segments to include in index")
77 #define NOCACHE_TEXT N_("Allow cache")
78 #define NOCACHE_LONGTEXT N_("Add EXT-X-ALLOW-CACHE:NO directive in playlist-file if this is disabled")
80 #define INDEX_TEXT N_("Index file")
81 #define INDEX_LONGTEXT N_("Path to the index file to create")
83 #define INDEXURL_TEXT N_("Full URL to put in index file")
84 #define INDEXURL_LONGTEXT N_("Full URL to put in index file. "\
85 "Use #'s to represent segment number")
87 #define DELSEGS_TEXT N_("Delete segments")
88 #define DELSEGS_LONGTEXT N_("Delete segments when they are no longer needed")
90 #define RATECONTROL_TEXT N_("Use muxers rate control mechanism")
92 #define KEYURI_TEXT N_("AES key URI to place in playlist")
94 #define KEYFILE_TEXT N_("AES key file")
95 #define KEYFILE_LONGTEXT N_("File containing the 16 bytes encryption key")
97 #define KEYLOADFILE_TEXT N_("File where vlc reads key-uri and keyfile-location")
98 #define KEYLOADFILE_LONGTEXT N_("File is read when segment starts and is assumed to be in format: "\
99 "key-uri\\nkey-file. File is read on the segment opening and "\
100 "values are used on that segment.")
102 #define RANDOMIV_TEXT N_("Use randomized IV for encryption")
103 #define RANDOMIV_LONGTEXT N_("Generate IV instead using segment-number as IV")
105 #define INTITIAL_SEG_TEXT N_("Number of first segment")
106 #define INITIAL_SEG_LONGTEXT N_("The number of the first segment generated")
108 vlc_module_begin ()
109 set_description( N_("HTTP Live streaming output") )
110 set_shortname( N_("LiveHTTP" ))
111 add_shortcut( "livehttp" )
112 set_capability( "sout access", 0 )
113 set_category( CAT_SOUT )
114 set_subcategory( SUBCAT_SOUT_ACO )
115 add_integer( SOUT_CFG_PREFIX "seglen", 10, SEGLEN_TEXT, SEGLEN_LONGTEXT, false )
116 add_integer( SOUT_CFG_PREFIX "numsegs", 0, NUMSEGS_TEXT, NUMSEGS_LONGTEXT, false )
117 add_integer( SOUT_CFG_PREFIX "initial-segment-number", 1, INTITIAL_SEG_TEXT, INITIAL_SEG_LONGTEXT, false )
118 add_bool( SOUT_CFG_PREFIX "splitanywhere", false,
119 SPLITANYWHERE_TEXT, SPLITANYWHERE_LONGTEXT, true )
120 add_bool( SOUT_CFG_PREFIX "delsegs", true,
121 DELSEGS_TEXT, DELSEGS_LONGTEXT, true )
122 add_bool( SOUT_CFG_PREFIX "ratecontrol", false,
123 RATECONTROL_TEXT, RATECONTROL_TEXT, true )
124 add_bool( SOUT_CFG_PREFIX "caching", false,
125 NOCACHE_TEXT, NOCACHE_LONGTEXT, true )
126 add_bool( SOUT_CFG_PREFIX "generate-iv", false,
127 RANDOMIV_TEXT, RANDOMIV_LONGTEXT, true )
128 add_string( SOUT_CFG_PREFIX "index", NULL,
129 INDEX_TEXT, INDEX_LONGTEXT, false )
130 add_string( SOUT_CFG_PREFIX "index-url", NULL,
131 INDEXURL_TEXT, INDEXURL_LONGTEXT, false )
132 add_string( SOUT_CFG_PREFIX "key-uri", NULL,
133 KEYURI_TEXT, KEYURI_TEXT, true )
134 add_loadfile( SOUT_CFG_PREFIX "key-file", NULL,
135 KEYFILE_TEXT, KEYFILE_LONGTEXT, true )
136 add_loadfile( SOUT_CFG_PREFIX "key-loadfile", NULL,
137 KEYLOADFILE_TEXT, KEYLOADFILE_LONGTEXT, true )
138 set_callbacks( Open, Close )
139 vlc_module_end ()
142 /*****************************************************************************
143 * Exported prototypes
144 *****************************************************************************/
145 static const char *const ppsz_sout_options[] = {
146 "seglen",
147 "splitanywhere",
148 "numsegs",
149 "delsegs",
150 "index",
151 "index-url",
152 "ratecontrol",
153 "caching",
154 "key-uri",
155 "key-file",
156 "key-loadfile",
157 "generate-iv",
158 "initial-segment-number",
159 NULL
162 static ssize_t Write( sout_access_out_t *, block_t * );
163 static int Seek ( sout_access_out_t *, off_t );
164 static int Control( sout_access_out_t *, int, va_list );
166 typedef struct output_segment
168 char *psz_filename;
169 char *psz_uri;
170 char *psz_key_uri;
171 char *psz_duration;
172 float f_seglength;
173 uint32_t i_segment_number;
174 uint8_t aes_ivs[16];
175 } output_segment_t;
177 struct sout_access_out_sys_t
179 char *psz_cursegPath;
180 char *psz_indexPath;
181 char *psz_indexUrl;
182 char *psz_keyfile;
183 mtime_t i_keyfile_modification;
184 mtime_t i_opendts;
185 mtime_t i_dts_offset;
186 mtime_t i_seglenm;
187 uint32_t i_segment;
188 size_t i_seglen;
189 float f_seglen;
190 block_t *full_segments;
191 block_t **full_segments_end;
192 block_t *ongoing_segment;
193 block_t **ongoing_segment_end;
194 int i_handle;
195 unsigned i_numsegs;
196 unsigned i_initial_segment;
197 bool b_delsegs;
198 bool b_ratecontrol;
199 bool b_splitanywhere;
200 bool b_caching;
201 bool b_generate_iv;
202 bool b_segment_has_data;
203 uint8_t aes_ivs[16];
204 gcry_cipher_hd_t aes_ctx;
205 char *key_uri;
206 uint8_t stuffing_bytes[16];
207 ssize_t stuffing_size;
208 vlc_array_t segments_t;
211 static int LoadCryptFile( sout_access_out_t *p_access);
212 static int CryptSetup( sout_access_out_t *p_access, char *keyfile );
213 static int CheckSegmentChange( sout_access_out_t *p_access, block_t *p_buffer );
214 static ssize_t writeSegment( sout_access_out_t *p_access );
215 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys );
216 /*****************************************************************************
217 * Open: open the file
218 *****************************************************************************/
219 static int Open( vlc_object_t *p_this )
221 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
222 sout_access_out_sys_t *p_sys;
223 char *psz_idx;
225 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
227 if( !p_access->psz_path )
229 msg_Err( p_access, "no file name specified" );
230 return VLC_EGENERIC;
233 if( unlikely( !( p_sys = calloc ( 1, sizeof( *p_sys ) ) ) ) )
234 return VLC_ENOMEM;
236 /* Try to get within asked segment length */
237 p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
239 p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen;
240 p_sys->full_segments = NULL;
241 p_sys->full_segments_end = &p_sys->full_segments;
243 p_sys->ongoing_segment = NULL;
244 p_sys->ongoing_segment_end = &p_sys->ongoing_segment;
246 p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
247 p_sys->i_initial_segment = var_GetInteger( p_access, SOUT_CFG_PREFIX "initial-segment-number" );
248 p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
249 p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
250 p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
251 p_sys->b_caching = var_GetBool( p_access, SOUT_CFG_PREFIX "caching") ;
252 p_sys->b_generate_iv = var_GetBool( p_access, SOUT_CFG_PREFIX "generate-iv") ;
253 p_sys->b_segment_has_data = false;
255 vlc_array_init( &p_sys->segments_t );
257 p_sys->stuffing_size = 0;
258 p_sys->i_opendts = VLC_TS_INVALID;
259 p_sys->i_dts_offset = 0;
261 p_sys->psz_indexPath = NULL;
262 psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
263 if ( psz_idx )
265 char *psz_tmp;
266 psz_tmp = vlc_strftime( psz_idx );
267 free( psz_idx );
268 if ( !psz_tmp )
270 free( p_sys );
271 return VLC_ENOMEM;
273 p_sys->psz_indexPath = psz_tmp;
274 if( p_sys->i_initial_segment != 1 )
275 vlc_unlink( p_sys->psz_indexPath );
278 p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
279 p_sys->psz_keyfile = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-loadfile" );
280 p_sys->key_uri = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-uri" );
282 p_access->p_sys = p_sys;
284 if( p_sys->psz_keyfile && ( LoadCryptFile( p_access ) < 0 ) )
286 free( p_sys->psz_indexUrl );
287 free( p_sys->psz_indexPath );
288 free( p_sys );
289 msg_Err( p_access, "Encryption init failed" );
290 return VLC_EGENERIC;
292 else if( !p_sys->psz_keyfile && ( CryptSetup( p_access, NULL ) < 0 ) )
294 free( p_sys->psz_indexUrl );
295 free( p_sys->psz_indexPath );
296 free( p_sys );
297 msg_Err( p_access, "Encryption init failed" );
298 return VLC_EGENERIC;
301 p_sys->i_handle = -1;
302 p_sys->i_segment = p_sys->i_initial_segment-1;
303 p_sys->psz_cursegPath = NULL;
305 p_access->pf_write = Write;
306 p_access->pf_seek = Seek;
307 p_access->pf_control = Control;
309 return VLC_SUCCESS;
312 /************************************************************************
313 * CryptSetup: Initialize encryption
314 ************************************************************************/
315 static int CryptSetup( sout_access_out_t *p_access, char *key_file )
317 sout_access_out_sys_t *p_sys = p_access->p_sys;
318 uint8_t key[16];
319 char *keyfile = NULL;
321 if( !p_sys->key_uri ) /*No key uri, assume no encryption wanted*/
323 msg_Dbg( p_access, "No key uri, no encryption");
324 return VLC_SUCCESS;
327 if( key_file )
328 keyfile = strdup( key_file );
329 else
330 keyfile = var_InheritString( p_access, SOUT_CFG_PREFIX "key-file" );
332 if( unlikely(keyfile == NULL) )
334 msg_Err( p_access, "No key-file, no encryption" );
335 return VLC_EGENERIC;
338 vlc_gcrypt_init();
340 /*Setup encryption cipher*/
341 gcry_error_t err = gcry_cipher_open( &p_sys->aes_ctx, GCRY_CIPHER_AES,
342 GCRY_CIPHER_MODE_CBC, 0 );
343 if( err )
345 msg_Err( p_access, "Openin AES Cipher failed: %s", gpg_strerror(err));
346 free( keyfile );
347 return VLC_EGENERIC;
350 int keyfd = vlc_open( keyfile, O_RDONLY | O_NONBLOCK );
351 if( unlikely( keyfd == -1 ) )
353 msg_Err( p_access, "Unable to open keyfile %s: %s", keyfile,
354 vlc_strerror_c(errno) );
355 free( keyfile );
356 gcry_cipher_close( p_sys->aes_ctx );
357 return VLC_EGENERIC;
359 free( keyfile );
361 ssize_t keylen = read( keyfd, key, 16 );
363 vlc_close( keyfd );
364 if( keylen < 16 )
366 msg_Err( p_access, "No key at least 16 octects (you provided %zd), no encryption", keylen );
367 gcry_cipher_close( p_sys->aes_ctx );
368 return VLC_EGENERIC;
371 err = gcry_cipher_setkey( p_sys->aes_ctx, key, 16 );
372 if(err)
374 msg_Err(p_access, "Setting AES key failed: %s", gpg_strerror(err));
375 gcry_cipher_close( p_sys->aes_ctx );
376 return VLC_EGENERIC;
379 if( p_sys->b_generate_iv )
380 vlc_rand_bytes( p_sys->aes_ivs, sizeof(uint8_t)*16);
382 return VLC_SUCCESS;
386 /************************************************************************
387 * LoadCryptFile: Try to parse key_uri and keyfile-location from file
388 ************************************************************************/
389 static int LoadCryptFile( sout_access_out_t *p_access )
391 sout_access_out_sys_t *p_sys = p_access->p_sys;
393 FILE *stream = vlc_fopen( p_sys->psz_keyfile, "rt" );
394 char *key_file=NULL,*key_uri=NULL;
396 if( unlikely( stream == NULL ) )
398 msg_Err( p_access, "Unable to open keyloadfile %s: %s",
399 p_sys->psz_keyfile, vlc_strerror_c(errno) );
400 return VLC_EGENERIC;
404 //First read key_uri
405 ssize_t len = getline( &key_uri, &(size_t){0}, stream );
406 if( unlikely( len == -1 ) )
408 msg_Err( p_access, "Cannot read %s: %s", p_sys->psz_keyfile,
409 vlc_strerror_c(errno) );
410 clearerr( stream );
411 fclose( stream );
412 free( key_uri );
413 return VLC_EGENERIC;
415 //Strip the newline from uri, maybe scanf would be better?
416 key_uri[len-1]='\0';
418 len = getline( &key_file, &(size_t){0}, stream );
419 if( unlikely( len == -1 ) )
421 msg_Err( p_access, "Cannot read %s: %s", p_sys->psz_keyfile,
422 vlc_strerror_c(errno) );
423 clearerr( stream );
424 fclose( stream );
426 free( key_uri );
427 free( key_file );
428 return VLC_EGENERIC;
430 // Strip the last newline from filename
431 key_file[len-1]='\0';
432 fclose( stream );
434 int returncode = VLC_SUCCESS;
435 if( !p_sys->key_uri || strcmp( p_sys->key_uri, key_uri ) )
437 if( p_sys->key_uri )
439 free( p_sys->key_uri );
440 p_sys->key_uri = NULL;
442 p_sys->key_uri = strdup( key_uri );
443 returncode = CryptSetup( p_access, key_file );
445 free( key_file );
446 free( key_uri );
447 return returncode;
450 /************************************************************************
451 * CryptKey: Set encryption IV to current segment number
452 ************************************************************************/
453 static int CryptKey( sout_access_out_t *p_access, uint32_t i_segment )
455 sout_access_out_sys_t *p_sys = p_access->p_sys;
457 if( !p_sys->b_generate_iv )
459 /* Use segment number as IV if randomIV isn't selected*/
460 memset( p_sys->aes_ivs, 0, 16 * sizeof(uint8_t));
461 p_sys->aes_ivs[15] = i_segment & 0xff;
462 p_sys->aes_ivs[14] = (i_segment >> 8 ) & 0xff;
463 p_sys->aes_ivs[13] = (i_segment >> 16 ) & 0xff;
464 p_sys->aes_ivs[12] = (i_segment >> 24 ) & 0xff;
467 gcry_error_t err = gcry_cipher_setiv( p_sys->aes_ctx,
468 p_sys->aes_ivs, 16);
469 if( err )
471 msg_Err(p_access, "Setting AES IVs failed: %s", gpg_strerror(err) );
472 gcry_cipher_close( p_sys->aes_ctx);
473 return VLC_EGENERIC;
475 return VLC_SUCCESS;
479 #define SEG_NUMBER_PLACEHOLDER "#"
480 /*****************************************************************************
481 * formatSegmentPath: create segment path name based on seg #
482 *****************************************************************************/
483 static char *formatSegmentPath( char *psz_path, uint32_t i_seg )
485 char *psz_result;
486 char *psz_firstNumSign;
488 if ( ! ( psz_result = vlc_strftime( psz_path ) ) )
489 return NULL;
491 psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
492 if ( *psz_firstNumSign )
494 char *psz_newResult;
495 int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
496 int ret;
498 *psz_firstNumSign = '\0';
499 ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
500 free ( psz_result );
501 if ( ret < 0 )
502 return NULL;
503 psz_result = psz_newResult;
506 return psz_result;
509 static void destroySegment( output_segment_t *segment )
511 free( segment->psz_filename );
512 free( segment->psz_duration );
513 free( segment->psz_uri );
514 free( segment->psz_key_uri );
515 free( segment );
518 /************************************************************************
519 * segmentAmountNeeded: check that playlist has atleast 3*p_sys->i_seglength of segments
520 * return how many segments are needed for that (max of p_sys->i_segment )
521 ************************************************************************/
522 static uint32_t segmentAmountNeeded( sout_access_out_sys_t *p_sys )
524 float duration = .0f;
525 for( size_t index = 1; index <= vlc_array_count( &p_sys->segments_t ); index++ )
527 output_segment_t* segment = vlc_array_item_at_index( &p_sys->segments_t, vlc_array_count( &p_sys->segments_t ) - index );
528 duration += segment->f_seglength;
530 if( duration >= (float)( 3 * p_sys->i_seglen ) )
531 return __MAX(index, p_sys->i_numsegs);
533 return vlc_array_count( &p_sys->segments_t ) - 1;
538 /************************************************************************
539 * isFirstItemRemovable: Check for draft 11 section 6.2.2
540 * check that the first item has been around outside playlist
541 * segment->f_seglength + (p_sys->i_numsegs * p_sys->i_seglen) before it is removed.
542 ************************************************************************/
543 static bool isFirstItemRemovable( sout_access_out_sys_t *p_sys, uint32_t i_firstseg, uint32_t i_index_offset )
545 float duration = .0f;
547 /* Check that segment has been out of playlist for seglength + (p_sys->i_numsegs * p_sys->i_seglen) amount
548 * We check this by calculating duration of the items that replaced first item in playlist
550 for( unsigned int index = 0; index < i_index_offset; index++ )
552 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, p_sys->i_segment - i_firstseg + index );
553 duration += segment->f_seglength;
555 output_segment_t *first = vlc_array_item_at_index( &p_sys->segments_t, 0 );
557 return duration >= (first->f_seglength + (float)(p_sys->i_numsegs * p_sys->i_seglen));
560 /************************************************************************
561 * updateIndexAndDel: If necessary, update index file & delete old segments
562 ************************************************************************/
563 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
566 uint32_t i_firstseg;
567 unsigned i_index_offset = 0;
569 if ( p_sys->i_numsegs == 0 ||
570 p_sys->i_segment < ( p_sys->i_numsegs + p_sys->i_initial_segment ) )
572 i_firstseg = p_sys->i_initial_segment;
574 else
576 unsigned numsegs = segmentAmountNeeded( p_sys );
577 i_firstseg = ( p_sys->i_segment - numsegs ) + 1;
578 i_index_offset = vlc_array_count( &p_sys->segments_t ) - numsegs;
581 // First update index
582 if ( p_sys->psz_indexPath )
584 int val;
585 FILE *fp;
586 char *psz_idxTmp;
587 if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
588 return -1;
590 fp = vlc_fopen( psz_idxTmp, "wt");
591 if ( !fp )
593 msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
594 free( psz_idxTmp );
595 return -1;
598 if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%zu\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:%s"
599 "%s\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n%s", p_sys->i_seglen,
600 p_sys->b_caching ? "YES" : "NO",
601 p_sys->i_numsegs > 0 ? "" : b_isend ? "\n#EXT-X-PLAYLIST-TYPE:VOD" : "\n#EXT-X-PLAYLIST-TYPE:EVENT",
602 i_firstseg, ((p_sys->i_initial_segment > 1) && (p_sys->i_initial_segment == i_firstseg)) ? "#EXT-X-DISCONTINUITY\n" : ""
603 ) < 0 )
605 free( psz_idxTmp );
606 fclose( fp );
607 return -1;
609 char *psz_current_uri=NULL;
612 for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
614 //scale to i_index_offset..numsegs + i_index_offset
615 uint32_t index = i - i_firstseg + i_index_offset;
617 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, index );
618 if( p_sys->key_uri &&
619 ( !psz_current_uri || strcmp( psz_current_uri, segment->psz_key_uri ) )
622 int ret = 0;
623 free( psz_current_uri );
624 psz_current_uri = strdup( segment->psz_key_uri );
625 if( p_sys->b_generate_iv )
627 unsigned long long iv_hi = segment->aes_ivs[0];
628 unsigned long long iv_lo = segment->aes_ivs[8];
629 for( unsigned short j = 1; j < 8; j++ )
631 iv_hi <<= 8;
632 iv_hi |= segment->aes_ivs[j] & 0xff;
633 iv_lo <<= 8;
634 iv_lo |= segment->aes_ivs[8+j] & 0xff;
636 ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\",IV=0X%16.16llx%16.16llx\n",
637 segment->psz_key_uri, iv_hi, iv_lo );
639 } else {
640 ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"\n", segment->psz_key_uri );
642 if( ret < 0 )
644 free( psz_current_uri );
645 free( psz_idxTmp );
646 fclose( fp );
647 return -1;
651 val = fprintf( fp, "#EXTINF:%s,\n%s\n", segment->psz_duration, segment->psz_uri);
652 if ( val < 0 )
654 free( psz_current_uri );
655 free( psz_idxTmp );
656 fclose( fp );
657 return -1;
660 free( psz_current_uri );
662 if ( b_isend )
664 if ( fputs ( STR_ENDLIST, fp ) < 0)
666 free( psz_idxTmp );
667 fclose( fp ) ;
668 return -1;
672 fclose( fp );
674 val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
676 if ( val < 0 )
678 vlc_unlink( psz_idxTmp );
679 msg_Err( p_access, "Error moving LiveHttp index file" );
681 else
682 msg_Dbg( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
684 free( psz_idxTmp );
687 // Then take care of deletion
688 // Try to follow pantos draft 11 section 6.2.2
689 while( p_sys->b_delsegs && p_sys->i_numsegs &&
690 isFirstItemRemovable( p_sys, i_firstseg, i_index_offset )
693 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, 0 );
694 msg_Dbg( p_access, "Removing segment number %d", segment->i_segment_number );
695 vlc_array_remove( &p_sys->segments_t, 0 );
697 if ( segment->psz_filename )
699 vlc_unlink( segment->psz_filename );
702 destroySegment( segment );
703 i_index_offset -=1;
707 return 0;
710 /*****************************************************************************
711 * closeCurrentSegment: Close the segment file
712 *****************************************************************************/
713 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
715 if ( p_sys->i_handle >= 0 )
717 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, vlc_array_count( &p_sys->segments_t ) - 1 );
719 if( p_sys->key_uri )
721 size_t pad = 16 - p_sys->stuffing_size;
722 memset(&p_sys->stuffing_bytes[p_sys->stuffing_size], pad, pad);
723 gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx, p_sys->stuffing_bytes, 16, NULL, 0 );
725 if( err ) {
726 msg_Err( p_access, "Couldn't encrypt 16 bytes: %s", gpg_strerror(err) );
727 } else {
729 int ret = vlc_write( p_sys->i_handle, p_sys->stuffing_bytes, 16 );
730 if( ret != 16 )
731 msg_Err( p_access, "Couldn't write 16 bytes" );
733 p_sys->stuffing_size = 0;
737 vlc_close( p_sys->i_handle );
738 p_sys->i_handle = -1;
740 if( ! ( us_asprintf( &segment->psz_duration, "%.2f", p_sys->f_seglen ) ) )
742 msg_Err( p_access, "Couldn't set duration on closed segment");
743 return;
745 segment->f_seglength = p_sys->f_seglen;
747 segment->i_segment_number = p_sys->i_segment;
749 if ( p_sys->psz_cursegPath )
751 msg_Dbg( p_access, "LiveHttpSegmentComplete: %s (%"PRIu32")" , p_sys->psz_cursegPath, p_sys->i_segment );
752 free( p_sys->psz_cursegPath );
753 p_sys->psz_cursegPath = 0;
754 updateIndexAndDel( p_access, p_sys, b_isend );
759 /*****************************************************************************
760 * Close: close the target
761 *****************************************************************************/
762 static void Close( vlc_object_t * p_this )
764 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
765 sout_access_out_sys_t *p_sys = p_access->p_sys;
767 if( p_sys->ongoing_segment )
768 block_ChainLastAppend( &p_sys->full_segments_end, p_sys->ongoing_segment );
769 p_sys->ongoing_segment = NULL;
770 p_sys->ongoing_segment_end = &p_sys->ongoing_segment;
772 block_t *output_block = p_sys->full_segments;
773 p_sys->full_segments = NULL;
774 p_sys->full_segments_end = &p_sys->full_segments;
776 while( output_block )
778 block_t *p_next = output_block->p_next;
779 output_block->p_next = NULL;
781 Write( p_access, output_block );
782 output_block = p_next;
784 if( p_sys->ongoing_segment )
786 block_ChainLastAppend( &p_sys->full_segments_end, p_sys->ongoing_segment );
787 p_sys->ongoing_segment = NULL;
788 p_sys->ongoing_segment_end = &p_sys->ongoing_segment;
791 ssize_t writevalue = writeSegment( p_access );
792 msg_Dbg( p_access, "Writing.. %zd", writevalue );
793 if( unlikely( writevalue < 0 ) )
795 if( p_sys->full_segments )
796 block_ChainRelease( p_sys->full_segments );
797 if( p_sys->ongoing_segment )
798 block_ChainRelease( p_sys->ongoing_segment );
801 closeCurrentSegment( p_access, p_sys, true );
803 if( p_sys->key_uri )
805 gcry_cipher_close( p_sys->aes_ctx );
806 free( p_sys->key_uri );
809 while( vlc_array_count( &p_sys->segments_t ) > 0 )
811 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, 0 );
812 vlc_array_remove( &p_sys->segments_t, 0 );
813 if( p_sys->b_delsegs && p_sys->i_numsegs && segment->psz_filename )
815 msg_Dbg( p_access, "Removing segment number %d name %s", segment->i_segment_number, segment->psz_filename );
816 vlc_unlink( segment->psz_filename );
819 destroySegment( segment );
822 free( p_sys->psz_indexUrl );
823 free( p_sys->psz_indexPath );
824 free( p_sys );
826 msg_Dbg( p_access, "livehttp access output closed" );
829 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
831 sout_access_out_sys_t *p_sys = p_access->p_sys;
833 switch( i_query )
835 case ACCESS_OUT_CONTROLS_PACE:
837 bool *pb = va_arg( args, bool * );
838 *pb = !p_sys->b_ratecontrol;
839 //*pb = true;
840 break;
843 default:
844 return VLC_EGENERIC;
846 return VLC_SUCCESS;
849 /*****************************************************************************
850 * openNextFile: Open the segment file
851 *****************************************************************************/
852 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
854 int fd;
856 uint32_t i_newseg = p_sys->i_segment + 1;
858 /* Create segment and fill it info that we can (everything excluding duration */
859 output_segment_t *segment = (output_segment_t*)calloc(1, sizeof(output_segment_t));
860 if( unlikely( !segment ) )
861 return -1;
863 segment->i_segment_number = i_newseg;
864 segment->psz_filename = formatSegmentPath( p_access->psz_path, i_newseg );
865 char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
866 segment->psz_uri = formatSegmentPath( psz_idxFormat , i_newseg );
868 if ( unlikely( !segment->psz_filename ) )
870 msg_Err( p_access, "Format segmentpath failed");
871 destroySegment( segment );
872 return -1;
875 fd = vlc_open( segment->psz_filename, O_WRONLY | O_CREAT | O_LARGEFILE |
876 O_TRUNC, 0666 );
877 if ( fd == -1 )
879 msg_Err( p_access, "cannot open `%s' (%s)", segment->psz_filename,
880 vlc_strerror_c(errno) );
881 destroySegment( segment );
882 return -1;
885 vlc_array_append( &p_sys->segments_t, segment );
887 if( p_sys->psz_keyfile )
889 LoadCryptFile( p_access );
892 if( p_sys->key_uri )
894 segment->psz_key_uri = strdup( p_sys->key_uri );
895 CryptKey( p_access, i_newseg );
896 if( p_sys->b_generate_iv )
897 memcpy( segment->aes_ivs, p_sys->aes_ivs, sizeof(uint8_t)*16 );
899 msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , segment->psz_filename, i_newseg );
901 p_sys->psz_cursegPath = strdup(segment->psz_filename);
902 p_sys->i_handle = fd;
903 p_sys->i_segment = i_newseg;
904 p_sys->b_segment_has_data = false;
905 return fd;
907 /*****************************************************************************
908 * CheckSegmentChange: Check if segment needs to be closed and new opened
909 *****************************************************************************/
910 static int CheckSegmentChange( sout_access_out_t *p_access, block_t *p_buffer )
912 sout_access_out_sys_t *p_sys = p_access->p_sys;
913 ssize_t writevalue = 0;
915 if( p_sys->i_handle > 0 && p_sys->b_segment_has_data &&
916 (( p_buffer->i_length + p_buffer->i_dts - p_sys->i_opendts ) >= p_sys->i_seglenm ) )
918 writevalue = writeSegment( p_access );
919 if( unlikely( writevalue < 0 ) )
921 block_ChainRelease ( p_buffer );
922 return -1;
924 closeCurrentSegment( p_access, p_sys, false );
925 return writevalue;
928 if ( unlikely( p_sys->i_handle < 0 ) )
930 p_sys->i_opendts = p_buffer->i_dts;
932 if( p_sys->ongoing_segment && ( p_sys->ongoing_segment->i_dts < p_sys->i_opendts) )
933 p_sys->i_opendts = p_sys->ongoing_segment->i_dts;
935 if( p_sys->full_segments && ( p_sys->full_segments->i_dts < p_sys->i_opendts) )
936 p_sys->i_opendts = p_sys->full_segments->i_dts;
938 msg_Dbg( p_access, "Setting new opendts %"PRId64, p_sys->i_opendts );
940 if ( openNextFile( p_access, p_sys ) < 0 )
941 return -1;
943 return writevalue;
946 static ssize_t writeSegment( sout_access_out_t *p_access )
948 sout_access_out_sys_t *p_sys = p_access->p_sys;
949 msg_Dbg( p_access, "Writing all full segments" );
951 block_t *output = p_sys->full_segments;
952 mtime_t output_last_length = 0;
953 if( output )
954 output_last_length = output->i_length;
955 if( *p_sys->full_segments_end )
956 output_last_length = (*p_sys->full_segments_end)->i_length;
957 p_sys->full_segments = NULL;
958 p_sys->full_segments_end = &p_sys->full_segments;
960 ssize_t i_write=0;
961 bool crypted = false;
962 while( output )
964 if( p_sys->key_uri && !crypted )
966 if( p_sys->stuffing_size )
968 output = block_Realloc( output, p_sys->stuffing_size, output->i_buffer );
969 if( unlikely(!output ) )
970 return VLC_ENOMEM;
971 memcpy( output->p_buffer, p_sys->stuffing_bytes, p_sys->stuffing_size );
972 p_sys->stuffing_size = 0;
974 size_t original = output->i_buffer;
975 size_t padded = (output->i_buffer + 15 ) & ~15;
976 size_t pad = padded - original;
977 if( pad )
979 p_sys->stuffing_size = 16-pad;
980 output->i_buffer -= p_sys->stuffing_size;
981 memcpy(p_sys->stuffing_bytes, &output->p_buffer[output->i_buffer], p_sys->stuffing_size);
984 gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx,
985 output->p_buffer, output->i_buffer, NULL, 0 );
986 if( err )
988 msg_Err( p_access, "Encryption failure: %s ", gpg_strerror(err) );
989 return -1;
991 crypted=true;
995 ssize_t val = vlc_write( p_sys->i_handle, output->p_buffer, output->i_buffer );
996 if ( val == -1 )
998 if ( errno == EINTR )
999 continue;
1000 return -1;
1003 p_sys->f_seglen =
1004 (float)(output_last_length +
1005 output->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
1007 if ( (size_t)val >= output->i_buffer )
1009 block_t *p_next = output->p_next;
1010 block_Release (output);
1011 output = p_next;
1012 crypted=false;
1014 else
1016 output->p_buffer += val;
1017 output->i_buffer -= val;
1019 i_write += val;
1021 return i_write;
1024 /*****************************************************************************
1025 * Write: standard write on a file descriptor.
1026 *****************************************************************************/
1027 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
1029 size_t i_write = 0;
1030 sout_access_out_sys_t *p_sys = p_access->p_sys;
1031 while( p_buffer )
1033 /* Check if current block is already past segment-length
1034 and we want to write gathered blocks into segment
1035 and update playlist */
1036 if( p_sys->ongoing_segment && ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_HEADER ) ) )
1038 msg_Dbg( p_access, "Moving ongoing segment to full segments-queue" );
1039 block_ChainLastAppend( &p_sys->full_segments_end, p_sys->ongoing_segment );
1040 p_sys->ongoing_segment = NULL;
1041 p_sys->ongoing_segment_end = &p_sys->ongoing_segment;
1042 p_sys->b_segment_has_data = true;
1045 ssize_t ret = CheckSegmentChange( p_access, p_buffer );
1046 if( ret < 0 )
1048 msg_Err( p_access, "Error in write loop");
1049 return ret;
1051 i_write += ret;
1053 block_t *p_temp = p_buffer->p_next;
1054 p_buffer->p_next = NULL;
1055 block_ChainLastAppend( &p_sys->ongoing_segment_end, p_buffer );
1056 p_buffer = p_temp;
1059 return i_write;
1062 /*****************************************************************************
1063 * Seek: seek to a specific location in a file
1064 *****************************************************************************/
1065 static int Seek( sout_access_out_t *p_access, off_t i_pos )
1067 (void) i_pos;
1068 msg_Err( p_access, "livehttp sout access cannot seek" );
1069 return -1;