qt: playlist: use item title if available
[vlc.git] / modules / access_output / livehttp.c
blobbbb2c5fc45d4e033d0158eab7799591d697ef638
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)
136 add_loadfile(SOUT_CFG_PREFIX "key-loadfile", NULL,
137 KEYLOADFILE_TEXT, KEYLOADFILE_LONGTEXT)
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 Control( sout_access_out_t *, int, va_list );
165 typedef struct output_segment
167 char *psz_filename;
168 char *psz_uri;
169 char *psz_key_uri;
170 char *psz_duration;
171 vlc_tick_t segment_length;
172 uint32_t i_segment_number;
173 uint8_t aes_ivs[16];
174 } output_segment_t;
176 typedef struct
178 char *psz_cursegPath;
179 char *psz_indexPath;
180 char *psz_indexUrl;
181 char *psz_keyfile;
182 vlc_tick_t i_keyfile_modification;
183 vlc_tick_t segment_max_length;
184 vlc_tick_t current_segment_length;
185 uint32_t i_segment;
186 block_t *full_segments;
187 block_t **full_segments_end;
188 block_t *ongoing_segment;
189 block_t **ongoing_segment_end;
190 int i_handle;
191 unsigned i_numsegs;
192 unsigned i_initial_segment;
193 bool b_delsegs;
194 bool b_ratecontrol;
195 bool b_splitanywhere;
196 bool b_caching;
197 bool b_generate_iv;
198 bool b_segment_has_data;
199 uint8_t aes_ivs[16];
200 gcry_cipher_hd_t aes_ctx;
201 char *key_uri;
202 uint8_t stuffing_bytes[16];
203 ssize_t stuffing_size;
204 vlc_array_t segments_t;
205 } sout_access_out_sys_t;
207 static int LoadCryptFile( sout_access_out_t *p_access);
208 static int CryptSetup( sout_access_out_t *p_access, char *keyfile );
209 static int CheckSegmentChange( sout_access_out_t *p_access, block_t *p_buffer );
210 static ssize_t writeSegment( sout_access_out_t *p_access );
211 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys );
212 /*****************************************************************************
213 * Open: open the file
214 *****************************************************************************/
215 static int Open( vlc_object_t *p_this )
217 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
218 sout_access_out_sys_t *p_sys;
219 char *psz_idx;
221 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
223 if( !p_access->psz_path )
225 msg_Err( p_access, "no file name specified" );
226 return VLC_EGENERIC;
229 if( unlikely( !( p_sys = calloc ( 1, sizeof( *p_sys ) ) ) ) )
230 return VLC_ENOMEM;
232 /* Try to get within asked segment length */
233 size_t i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
235 p_sys->segment_max_length = vlc_tick_from_sec( i_seglen );
236 p_sys->full_segments = NULL;
237 p_sys->full_segments_end = &p_sys->full_segments;
239 p_sys->ongoing_segment = NULL;
240 p_sys->ongoing_segment_end = &p_sys->ongoing_segment;
242 p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
243 p_sys->i_initial_segment = var_GetInteger( p_access, SOUT_CFG_PREFIX "initial-segment-number" );
244 p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
245 p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
246 p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
247 p_sys->b_caching = var_GetBool( p_access, SOUT_CFG_PREFIX "caching") ;
248 p_sys->b_generate_iv = var_GetBool( p_access, SOUT_CFG_PREFIX "generate-iv") ;
249 p_sys->b_segment_has_data = false;
251 vlc_array_init( &p_sys->segments_t );
253 p_sys->stuffing_size = 0;
255 p_sys->psz_indexPath = NULL;
256 psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
257 if ( psz_idx )
259 char *psz_tmp;
260 psz_tmp = vlc_strftime( psz_idx );
261 free( psz_idx );
262 if ( !psz_tmp )
264 free( p_sys );
265 return VLC_ENOMEM;
267 p_sys->psz_indexPath = psz_tmp;
268 if( p_sys->i_initial_segment != 1 )
269 vlc_unlink( p_sys->psz_indexPath );
272 p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
273 p_sys->psz_keyfile = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-loadfile" );
274 p_sys->key_uri = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-uri" );
276 p_access->p_sys = p_sys;
278 if( p_sys->psz_keyfile && ( LoadCryptFile( p_access ) < 0 ) )
280 free( p_sys->psz_indexUrl );
281 free( p_sys->psz_indexPath );
282 free( p_sys );
283 msg_Err( p_access, "Encryption init failed" );
284 return VLC_EGENERIC;
286 else if( !p_sys->psz_keyfile && ( CryptSetup( p_access, NULL ) < 0 ) )
288 free( p_sys->psz_indexUrl );
289 free( p_sys->psz_indexPath );
290 free( p_sys );
291 msg_Err( p_access, "Encryption init failed" );
292 return VLC_EGENERIC;
295 p_sys->i_handle = -1;
296 p_sys->i_segment = p_sys->i_initial_segment-1;
297 p_sys->psz_cursegPath = NULL;
299 p_access->pf_write = Write;
300 p_access->pf_control = Control;
302 return VLC_SUCCESS;
305 /************************************************************************
306 * CryptSetup: Initialize encryption
307 ************************************************************************/
308 static int CryptSetup( sout_access_out_t *p_access, char *key_file )
310 sout_access_out_sys_t *p_sys = p_access->p_sys;
311 uint8_t key[16];
312 char *keyfile = NULL;
314 if( !p_sys->key_uri ) /*No key uri, assume no encryption wanted*/
316 msg_Dbg( p_access, "No key uri, no encryption");
317 return VLC_SUCCESS;
320 if( key_file )
321 keyfile = strdup( key_file );
322 else
323 keyfile = var_InheritString( p_access, SOUT_CFG_PREFIX "key-file" );
325 if( unlikely(keyfile == NULL) )
327 msg_Err( p_access, "No key-file, no encryption" );
328 return VLC_EGENERIC;
331 vlc_gcrypt_init();
333 /*Setup encryption cipher*/
334 gcry_error_t err = gcry_cipher_open( &p_sys->aes_ctx, GCRY_CIPHER_AES,
335 GCRY_CIPHER_MODE_CBC, 0 );
336 if( err )
338 msg_Err( p_access, "Openin AES Cipher failed: %s", gpg_strerror(err));
339 free( keyfile );
340 return VLC_EGENERIC;
343 int keyfd = vlc_open( keyfile, O_RDONLY | O_NONBLOCK );
344 if( unlikely( keyfd == -1 ) )
346 msg_Err( p_access, "Unable to open keyfile %s: %s", keyfile,
347 vlc_strerror_c(errno) );
348 free( keyfile );
349 gcry_cipher_close( p_sys->aes_ctx );
350 return VLC_EGENERIC;
352 free( keyfile );
354 ssize_t keylen = read( keyfd, key, 16 );
356 vlc_close( keyfd );
357 if( keylen < 16 )
359 msg_Err( p_access, "No key at least 16 octects (you provided %zd), no encryption", keylen );
360 gcry_cipher_close( p_sys->aes_ctx );
361 return VLC_EGENERIC;
364 err = gcry_cipher_setkey( p_sys->aes_ctx, key, 16 );
365 if(err)
367 msg_Err(p_access, "Setting AES key failed: %s", gpg_strerror(err));
368 gcry_cipher_close( p_sys->aes_ctx );
369 return VLC_EGENERIC;
372 if( p_sys->b_generate_iv )
373 vlc_rand_bytes( p_sys->aes_ivs, sizeof(uint8_t)*16);
375 return VLC_SUCCESS;
379 /************************************************************************
380 * LoadCryptFile: Try to parse key_uri and keyfile-location from file
381 ************************************************************************/
382 static int LoadCryptFile( sout_access_out_t *p_access )
384 sout_access_out_sys_t *p_sys = p_access->p_sys;
386 FILE *stream = vlc_fopen( p_sys->psz_keyfile, "rt" );
387 char *key_file=NULL,*key_uri=NULL;
389 if( unlikely( stream == NULL ) )
391 msg_Err( p_access, "Unable to open keyloadfile %s: %s",
392 p_sys->psz_keyfile, vlc_strerror_c(errno) );
393 return VLC_EGENERIC;
397 //First read key_uri
398 ssize_t len = getline( &key_uri, &(size_t){0}, stream );
399 if( unlikely( len == -1 ) )
401 msg_Err( p_access, "Cannot read %s: %s", p_sys->psz_keyfile,
402 vlc_strerror_c(errno) );
403 clearerr( stream );
404 fclose( stream );
405 free( key_uri );
406 return VLC_EGENERIC;
408 //Strip the newline from uri, maybe scanf would be better?
409 key_uri[len-1]='\0';
411 len = getline( &key_file, &(size_t){0}, stream );
412 if( unlikely( len == -1 ) )
414 msg_Err( p_access, "Cannot read %s: %s", p_sys->psz_keyfile,
415 vlc_strerror_c(errno) );
416 clearerr( stream );
417 fclose( stream );
419 free( key_uri );
420 free( key_file );
421 return VLC_EGENERIC;
423 // Strip the last newline from filename
424 key_file[len-1]='\0';
425 fclose( stream );
427 int returncode = VLC_SUCCESS;
428 if( !p_sys->key_uri || strcmp( p_sys->key_uri, key_uri ) )
430 if( p_sys->key_uri )
432 free( p_sys->key_uri );
433 p_sys->key_uri = NULL;
435 p_sys->key_uri = strdup( key_uri );
436 returncode = CryptSetup( p_access, key_file );
438 free( key_file );
439 free( key_uri );
440 return returncode;
443 /************************************************************************
444 * CryptKey: Set encryption IV to current segment number
445 ************************************************************************/
446 static int CryptKey( sout_access_out_t *p_access, uint32_t i_segment )
448 sout_access_out_sys_t *p_sys = p_access->p_sys;
450 if( !p_sys->b_generate_iv )
452 /* Use segment number as IV if randomIV isn't selected*/
453 memset( p_sys->aes_ivs, 0, 16 * sizeof(uint8_t));
454 p_sys->aes_ivs[15] = i_segment & 0xff;
455 p_sys->aes_ivs[14] = (i_segment >> 8 ) & 0xff;
456 p_sys->aes_ivs[13] = (i_segment >> 16 ) & 0xff;
457 p_sys->aes_ivs[12] = (i_segment >> 24 ) & 0xff;
460 gcry_error_t err = gcry_cipher_setiv( p_sys->aes_ctx,
461 p_sys->aes_ivs, 16);
462 if( err )
464 msg_Err(p_access, "Setting AES IVs failed: %s", gpg_strerror(err) );
465 gcry_cipher_close( p_sys->aes_ctx);
466 return VLC_EGENERIC;
468 return VLC_SUCCESS;
472 #define SEG_NUMBER_PLACEHOLDER "#"
473 /*****************************************************************************
474 * formatSegmentPath: create segment path name based on seg #
475 *****************************************************************************/
476 static char *formatSegmentPath( char *psz_path, uint32_t i_seg )
478 char *psz_result;
479 char *psz_firstNumSign;
481 if ( ! ( psz_result = vlc_strftime( psz_path ) ) )
482 return NULL;
484 psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
485 if ( *psz_firstNumSign )
487 char *psz_newResult;
488 int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
489 int ret;
491 *psz_firstNumSign = '\0';
492 ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
493 free ( psz_result );
494 if ( ret < 0 )
495 return NULL;
496 psz_result = psz_newResult;
499 return psz_result;
502 static void destroySegment( output_segment_t *segment )
504 free( segment->psz_filename );
505 free( segment->psz_duration );
506 free( segment->psz_uri );
507 free( segment->psz_key_uri );
508 free( segment );
511 /************************************************************************
512 * segmentAmountNeeded: check that playlist has atleast 3*p_sys->segment_max_length of segments
513 * return how many segments are needed for that (max of p_sys->i_segment )
514 ************************************************************************/
515 static uint32_t segmentAmountNeeded( sout_access_out_sys_t *p_sys )
517 vlc_tick_t duration = 0;
518 for( size_t index = 1; index <= vlc_array_count( &p_sys->segments_t ); index++ )
520 output_segment_t* segment = vlc_array_item_at_index( &p_sys->segments_t, vlc_array_count( &p_sys->segments_t ) - index );
521 duration += segment->segment_length;
523 if( duration >= ( 3 * p_sys->segment_max_length ) )
524 return __MAX(index, p_sys->i_numsegs);
526 return vlc_array_count( &p_sys->segments_t ) - 1;
531 /************************************************************************
532 * isFirstItemRemovable: Check for draft 11 section 6.2.2
533 * check that the first item has been around outside playlist
534 * segment->segment_length + (p_sys->i_numsegs * p_sys->segment_max_length) before it is removed.
535 ************************************************************************/
536 static bool isFirstItemRemovable( sout_access_out_sys_t *p_sys, uint32_t i_firstseg, uint32_t i_index_offset )
538 vlc_tick_t duration = 0;
540 /* Check that segment has been out of playlist for segment_length + (p_sys->i_numsegs * p_sys->segment_max_length) amount
541 * We check this by calculating duration of the items that replaced first item in playlist
543 for( unsigned int index = 0; index < i_index_offset; index++ )
545 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, p_sys->i_segment - i_firstseg + index );
546 duration += segment->segment_length;
548 output_segment_t *first = vlc_array_item_at_index( &p_sys->segments_t, 0 );
550 return duration >= (first->segment_length + (p_sys->i_numsegs * p_sys->segment_max_length));
553 /************************************************************************
554 * updateIndexAndDel: If necessary, update index file & delete old segments
555 ************************************************************************/
556 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
559 uint32_t i_firstseg;
560 unsigned i_index_offset = 0;
562 if ( p_sys->i_numsegs == 0 ||
563 p_sys->i_segment < ( p_sys->i_numsegs + p_sys->i_initial_segment ) )
565 i_firstseg = p_sys->i_initial_segment;
567 else
569 unsigned numsegs = segmentAmountNeeded( p_sys );
570 i_firstseg = ( p_sys->i_segment - numsegs ) + 1;
571 i_index_offset = vlc_array_count( &p_sys->segments_t ) - numsegs;
574 // First update index
575 if ( p_sys->psz_indexPath )
577 int val;
578 FILE *fp;
579 char *psz_idxTmp;
580 if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
581 return -1;
583 fp = vlc_fopen( psz_idxTmp, "wt");
584 if ( !fp )
586 msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
587 free( psz_idxTmp );
588 return -1;
591 if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%.0f\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:%s"
592 "%s\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n%s", ceil(secf_from_vlc_tick( p_sys->segment_max_length )) ,
593 p_sys->b_caching ? "YES" : "NO",
594 p_sys->i_numsegs > 0 ? "" : b_isend ? "\n#EXT-X-PLAYLIST-TYPE:VOD" : "\n#EXT-X-PLAYLIST-TYPE:EVENT",
595 i_firstseg, ((p_sys->i_initial_segment > 1) && (p_sys->i_initial_segment == i_firstseg)) ? "#EXT-X-DISCONTINUITY\n" : ""
596 ) < 0 )
598 free( psz_idxTmp );
599 fclose( fp );
600 return -1;
602 char *psz_current_uri=NULL;
605 for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
607 //scale to i_index_offset..numsegs + i_index_offset
608 uint32_t index = i - i_firstseg + i_index_offset;
610 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, index );
611 if( p_sys->key_uri &&
612 ( !psz_current_uri || strcmp( psz_current_uri, segment->psz_key_uri ) )
615 int ret = 0;
616 free( psz_current_uri );
617 psz_current_uri = strdup( segment->psz_key_uri );
618 if( p_sys->b_generate_iv )
620 unsigned long long iv_hi = segment->aes_ivs[0];
621 unsigned long long iv_lo = segment->aes_ivs[8];
622 for( unsigned short j = 1; j < 8; j++ )
624 iv_hi <<= 8;
625 iv_hi |= segment->aes_ivs[j] & 0xff;
626 iv_lo <<= 8;
627 iv_lo |= segment->aes_ivs[8+j] & 0xff;
629 ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\",IV=0X%16.16llx%16.16llx\n",
630 segment->psz_key_uri, iv_hi, iv_lo );
632 } else {
633 ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"\n", segment->psz_key_uri );
635 if( ret < 0 )
637 free( psz_current_uri );
638 free( psz_idxTmp );
639 fclose( fp );
640 return -1;
644 val = fprintf( fp, "#EXTINF:%s,\n%s\n", segment->psz_duration, segment->psz_uri);
645 if ( val < 0 )
647 free( psz_current_uri );
648 free( psz_idxTmp );
649 fclose( fp );
650 return -1;
653 free( psz_current_uri );
655 if ( b_isend )
657 if ( fputs ( STR_ENDLIST, fp ) < 0)
659 free( psz_idxTmp );
660 fclose( fp ) ;
661 return -1;
665 fclose( fp );
667 val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
669 if ( val < 0 )
671 vlc_unlink( psz_idxTmp );
672 msg_Err( p_access, "Error moving LiveHttp index file" );
674 else
675 msg_Dbg( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
677 free( psz_idxTmp );
680 // Then take care of deletion
681 // Try to follow pantos draft 11 section 6.2.2
682 while( p_sys->b_delsegs && p_sys->i_numsegs &&
683 isFirstItemRemovable( p_sys, i_firstseg, i_index_offset )
686 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, 0 );
687 msg_Dbg( p_access, "Removing segment number %d", segment->i_segment_number );
688 vlc_array_remove( &p_sys->segments_t, 0 );
690 if ( segment->psz_filename )
692 vlc_unlink( segment->psz_filename );
695 destroySegment( segment );
696 i_index_offset -=1;
700 return 0;
703 /*****************************************************************************
704 * closeCurrentSegment: Close the segment file
705 *****************************************************************************/
706 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
708 if ( p_sys->i_handle >= 0 )
710 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, vlc_array_count( &p_sys->segments_t ) - 1 );
712 if( p_sys->key_uri )
714 size_t pad = 16 - p_sys->stuffing_size;
715 memset(&p_sys->stuffing_bytes[p_sys->stuffing_size], pad, pad);
716 gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx, p_sys->stuffing_bytes, 16, NULL, 0 );
718 if( err ) {
719 msg_Err( p_access, "Couldn't encrypt 16 bytes: %s", gpg_strerror(err) );
720 } else {
722 int ret = vlc_write( p_sys->i_handle, p_sys->stuffing_bytes, 16 );
723 if( ret != 16 )
724 msg_Err( p_access, "Couldn't write 16 bytes" );
726 p_sys->stuffing_size = 0;
730 vlc_close( p_sys->i_handle );
731 p_sys->i_handle = -1;
733 if( ! ( us_asprintf( &segment->psz_duration, "%.2f", secf_from_vlc_tick( p_sys->current_segment_length )) ) )
735 msg_Err( p_access, "Couldn't set duration on closed segment");
736 return;
738 segment->segment_length = p_sys->current_segment_length;
740 segment->i_segment_number = p_sys->i_segment;
742 if ( p_sys->psz_cursegPath )
744 msg_Dbg( p_access, "LiveHttpSegmentComplete: %s (%"PRIu32")" , p_sys->psz_cursegPath, p_sys->i_segment );
745 free( p_sys->psz_cursegPath );
746 p_sys->psz_cursegPath = 0;
747 updateIndexAndDel( p_access, p_sys, b_isend );
752 /*****************************************************************************
753 * Close: close the target
754 *****************************************************************************/
755 static void Close( vlc_object_t * p_this )
757 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
758 sout_access_out_sys_t *p_sys = p_access->p_sys;
760 if( p_sys->ongoing_segment )
761 block_ChainLastAppend( &p_sys->full_segments_end, p_sys->ongoing_segment );
762 p_sys->ongoing_segment = NULL;
763 p_sys->ongoing_segment_end = &p_sys->ongoing_segment;
765 block_t *output_block = p_sys->full_segments;
766 p_sys->full_segments = NULL;
767 p_sys->full_segments_end = &p_sys->full_segments;
769 while( output_block )
771 block_t *p_next = output_block->p_next;
772 output_block->p_next = NULL;
774 Write( p_access, output_block );
775 output_block = p_next;
777 if( p_sys->ongoing_segment )
779 block_ChainLastAppend( &p_sys->full_segments_end, p_sys->ongoing_segment );
780 p_sys->ongoing_segment = NULL;
781 p_sys->ongoing_segment_end = &p_sys->ongoing_segment;
784 ssize_t writevalue = writeSegment( p_access );
785 msg_Dbg( p_access, "Writing.. %zd", writevalue );
786 if( unlikely( writevalue < 0 ) )
788 if( p_sys->full_segments )
789 block_ChainRelease( p_sys->full_segments );
790 if( p_sys->ongoing_segment )
791 block_ChainRelease( p_sys->ongoing_segment );
794 closeCurrentSegment( p_access, p_sys, true );
796 if( p_sys->key_uri )
798 gcry_cipher_close( p_sys->aes_ctx );
799 free( p_sys->key_uri );
802 while( vlc_array_count( &p_sys->segments_t ) > 0 )
804 output_segment_t *segment = vlc_array_item_at_index( &p_sys->segments_t, 0 );
805 vlc_array_remove( &p_sys->segments_t, 0 );
806 if( p_sys->b_delsegs && p_sys->i_numsegs && segment->psz_filename )
808 msg_Dbg( p_access, "Removing segment number %d name %s", segment->i_segment_number, segment->psz_filename );
809 vlc_unlink( segment->psz_filename );
812 destroySegment( segment );
815 free( p_sys->psz_indexUrl );
816 free( p_sys->psz_indexPath );
817 free( p_sys );
819 msg_Dbg( p_access, "livehttp access output closed" );
822 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
824 sout_access_out_sys_t *p_sys = p_access->p_sys;
826 switch( i_query )
828 case ACCESS_OUT_CONTROLS_PACE:
830 bool *pb = va_arg( args, bool * );
831 *pb = !p_sys->b_ratecontrol;
832 //*pb = true;
833 break;
836 default:
837 return VLC_EGENERIC;
839 return VLC_SUCCESS;
842 /*****************************************************************************
843 * openNextFile: Open the segment file
844 *****************************************************************************/
845 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
847 int fd;
849 uint32_t i_newseg = p_sys->i_segment + 1;
851 /* Create segment and fill it info that we can (everything excluding duration */
852 output_segment_t *segment = (output_segment_t*)calloc(1, sizeof(output_segment_t));
853 if( unlikely( !segment ) )
854 return -1;
856 segment->i_segment_number = i_newseg;
857 segment->psz_filename = formatSegmentPath( p_access->psz_path, i_newseg );
858 char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
859 segment->psz_uri = formatSegmentPath( psz_idxFormat , i_newseg );
861 if ( unlikely( !segment->psz_filename ) )
863 msg_Err( p_access, "Format segmentpath failed");
864 destroySegment( segment );
865 return -1;
868 fd = vlc_open( segment->psz_filename, O_WRONLY | O_CREAT | O_LARGEFILE |
869 O_TRUNC, 0666 );
870 if ( fd == -1 )
872 msg_Err( p_access, "cannot open `%s' (%s)", segment->psz_filename,
873 vlc_strerror_c(errno) );
874 destroySegment( segment );
875 return -1;
878 vlc_array_append_or_abort( &p_sys->segments_t, segment );
880 if( p_sys->psz_keyfile )
882 LoadCryptFile( p_access );
885 if( p_sys->key_uri )
887 segment->psz_key_uri = strdup( p_sys->key_uri );
888 CryptKey( p_access, i_newseg );
889 if( p_sys->b_generate_iv )
890 memcpy( segment->aes_ivs, p_sys->aes_ivs, sizeof(uint8_t)*16 );
892 msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , segment->psz_filename, i_newseg );
894 p_sys->psz_cursegPath = strdup(segment->psz_filename);
895 p_sys->i_handle = fd;
896 p_sys->i_segment = i_newseg;
897 p_sys->b_segment_has_data = false;
898 return fd;
900 /*****************************************************************************
901 * CheckSegmentChange: Check if segment needs to be closed and new opened
902 *****************************************************************************/
903 static int CheckSegmentChange( sout_access_out_t *p_access, block_t *p_buffer )
905 sout_access_out_sys_t *p_sys = p_access->p_sys;
906 ssize_t writevalue = 0;
908 vlc_tick_t current_length = 0;
909 vlc_tick_t ongoing_length = 0;
911 block_ChainProperties( p_sys->full_segments, NULL, NULL, &current_length );
912 block_ChainProperties( p_sys->ongoing_segment, NULL, NULL, &ongoing_length );
914 if( p_sys->i_handle > 0 &&
915 (( p_buffer->i_length + current_length + ongoing_length ) >= p_sys->segment_max_length ) )
917 writevalue = writeSegment( p_access );
918 if( unlikely( writevalue < 0 ) )
920 block_ChainRelease ( p_buffer );
921 return -1;
923 closeCurrentSegment( p_access, p_sys, false );
924 return writevalue;
927 if ( unlikely( p_sys->i_handle < 0 ) )
929 if ( openNextFile( p_access, p_sys ) < 0 )
930 return -1;
932 return writevalue;
935 static ssize_t writeSegment( sout_access_out_t *p_access )
937 sout_access_out_sys_t *p_sys = p_access->p_sys;
938 msg_Dbg( p_access, "Writing all full segments" );
940 block_t *output = p_sys->full_segments;
941 p_sys->full_segments = NULL;
942 p_sys->full_segments_end = &p_sys->full_segments;
944 vlc_tick_t current_length = 0;
945 block_ChainProperties( output, NULL, NULL, &current_length );
947 ssize_t i_write=0;
948 bool crypted = false;
949 p_sys->current_segment_length = current_length;
950 while( output )
952 if( p_sys->key_uri && !crypted )
954 if( p_sys->stuffing_size )
956 output = block_Realloc( output, p_sys->stuffing_size, output->i_buffer );
957 if( unlikely(!output ) )
958 return VLC_ENOMEM;
959 memcpy( output->p_buffer, p_sys->stuffing_bytes, p_sys->stuffing_size );
960 p_sys->stuffing_size = 0;
962 size_t original = output->i_buffer;
963 size_t padded = (output->i_buffer + 15 ) & ~15;
964 size_t pad = padded - original;
965 if( pad )
967 p_sys->stuffing_size = 16-pad;
968 output->i_buffer -= p_sys->stuffing_size;
969 memcpy(p_sys->stuffing_bytes, &output->p_buffer[output->i_buffer], p_sys->stuffing_size);
972 gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx,
973 output->p_buffer, output->i_buffer, NULL, 0 );
974 if( err )
976 msg_Err( p_access, "Encryption failure: %s ", gpg_strerror(err) );
977 return -1;
979 crypted=true;
983 ssize_t val = vlc_write( p_sys->i_handle, output->p_buffer, output->i_buffer );
984 if ( val == -1 )
986 if ( errno == EINTR )
987 continue;
988 return -1;
992 if ( (size_t)val >= output->i_buffer )
994 block_t *p_next = output->p_next;
995 block_Release (output);
996 output = p_next;
997 crypted=false;
999 else
1001 output->p_buffer += val;
1002 output->i_buffer -= val;
1004 i_write += val;
1006 return i_write;
1009 /*****************************************************************************
1010 * Write: standard write on a file descriptor.
1011 *****************************************************************************/
1012 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
1014 size_t i_write = 0;
1015 sout_access_out_sys_t *p_sys = p_access->p_sys;
1016 while( p_buffer )
1018 /* Check if current block is already past segment-length
1019 and we want to write gathered blocks into segment
1020 and update playlist */
1021 if( p_sys->ongoing_segment && ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_HEADER ) ) )
1023 msg_Dbg( p_access, "Moving ongoing segment to full segments-queue" );
1024 block_ChainLastAppend( &p_sys->full_segments_end, p_sys->ongoing_segment );
1025 p_sys->ongoing_segment = NULL;
1026 p_sys->ongoing_segment_end = &p_sys->ongoing_segment;
1027 p_sys->b_segment_has_data = true;
1030 ssize_t ret = CheckSegmentChange( p_access, p_buffer );
1031 if( ret < 0 )
1033 msg_Err( p_access, "Error in write loop");
1034 return ret;
1036 i_write += ret;
1038 block_t *p_temp = p_buffer->p_next;
1039 p_buffer->p_next = NULL;
1040 block_ChainLastAppend( &p_sys->ongoing_segment_end, p_buffer );
1041 p_buffer = p_temp;
1044 return i_write;