]> git.sesse.net Git - vlc/blob - modules/access_output/livehttp.c
demux: libmp4: add mp4a/mp4v esds restrictions
[vlc] / modules / access_output / livehttp.c
1 /*****************************************************************************
2  * livehttp.c: Live HTTP Streaming
3  *****************************************************************************
4  * Copyright © 2001, 2002, 2013 VLC authors and VideoLAN
5  * Copyright © 2009-2010 by Keary Griffin
6  *
7  * Authors: Keary Griffin <kearygriffin at gmail.com>
8  *          Ilkka Ollakka <ileoo at videolan dot org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <sys/types.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <unistd.h>
38
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>
46
47 #include <gcrypt.h>
48 #include <vlc_gcrypt.h>
49
50 #include <vlc_rand.h>
51
52 #ifndef O_LARGEFILE
53 #   define O_LARGEFILE 0
54 #endif
55
56 #define STR_ENDLIST "#EXT-X-ENDLIST\n"
57
58 #define MAX_RENAME_RETRIES        10
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 static int  Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
65
66 #define SOUT_CFG_PREFIX "sout-livehttp-"
67 #define SEGLEN_TEXT N_("Segment length")
68 #define SEGLEN_LONGTEXT N_("Length of TS stream segments")
69
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.")
73
74 #define NUMSEGS_TEXT N_("Number of segments")
75 #define NUMSEGS_LONGTEXT N_("Number of segments to include in index")
76
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")
79
80 #define INDEX_TEXT N_("Index file")
81 #define INDEX_LONGTEXT N_("Path to the index file to create")
82
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")
86
87 #define DELSEGS_TEXT N_("Delete segments")
88 #define DELSEGS_LONGTEXT N_("Delete segments when they are no longer needed")
89
90 #define RATECONTROL_TEXT N_("Use muxers rate control mechanism")
91
92 #define KEYURI_TEXT N_("AES key URI to place in playlist")
93
94 #define KEYFILE_TEXT N_("AES key file")
95 #define KEYFILE_LONGTEXT N_("File containing the 16 bytes encryption key")
96
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 assumet to be in format: "\
99                                 "key-uri\\nkey-file. File is read on the segment opening and "\
100                                 "values are used on that segment.")
101
102 #define RANDOMIV_TEXT N_("Use randomized IV for encryption")
103 #define RANDOMIV_LONGTEXT N_("Generate IV instead using segment-number as IV")
104
105 #define INTITIAL_SEG_TEXT N_("Number of first segment")
106 #define INITIAL_SEG_LONGTEXT N_("The number of the first segment generated")
107
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 ()
140
141
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
160 };
161
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 );
165
166 typedef struct output_segment
167 {
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;
176
177 struct sout_access_out_sys_t
178 {
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 *block_buffer;
191     int i_handle;
192     unsigned i_numsegs;
193     unsigned i_initial_segment;
194     bool b_delsegs;
195     bool b_ratecontrol;
196     bool b_splitanywhere;
197     bool b_caching;
198     bool b_generate_iv;
199     bool b_segment_has_data;
200     uint8_t aes_ivs[16];
201     gcry_cipher_hd_t aes_ctx;
202     char *key_uri;
203     uint8_t stuffing_bytes[16];
204     ssize_t stuffing_size;
205     vlc_array_t *segments_t;
206 };
207
208 static int LoadCryptFile( sout_access_out_t *p_access);
209 static int CryptSetup( sout_access_out_t *p_access, char *keyfile );
210 static int CheckSegmentChange( sout_access_out_t *p_access, block_t *p_buffer );
211 static ssize_t writeSegment( sout_access_out_t *p_access );
212 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys );
213 /*****************************************************************************
214  * Open: open the file
215  *****************************************************************************/
216 static int Open( vlc_object_t *p_this )
217 {
218     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
219     sout_access_out_sys_t *p_sys;
220     char *psz_idx;
221
222     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
223
224     if( !p_access->psz_path )
225     {
226         msg_Err( p_access, "no file name specified" );
227         return VLC_EGENERIC;
228     }
229
230     if( unlikely( !( p_sys = calloc ( 1, sizeof( *p_sys ) ) ) ) )
231         return VLC_ENOMEM;
232
233     p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
234     /* Try to get within asked segment length */
235     p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen;
236     p_sys->block_buffer = NULL;
237
238     p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
239     p_sys->i_initial_segment = var_GetInteger( p_access, SOUT_CFG_PREFIX "initial-segment-number" );
240     p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
241     p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
242     p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
243     p_sys->b_caching = var_GetBool( p_access, SOUT_CFG_PREFIX "caching") ;
244     p_sys->b_generate_iv = var_GetBool( p_access, SOUT_CFG_PREFIX "generate-iv") ;
245     p_sys->b_segment_has_data = false;
246
247     p_sys->segments_t = vlc_array_new();
248
249     p_sys->stuffing_size = 0;
250     p_sys->i_opendts = VLC_TS_INVALID;
251     p_sys->i_dts_offset  = 0;
252
253     p_sys->psz_indexPath = NULL;
254     psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
255     if ( psz_idx )
256     {
257         char *psz_tmp;
258         psz_tmp = str_format_time( psz_idx );
259         free( psz_idx );
260         if ( !psz_tmp )
261         {
262             free( p_sys );
263             return VLC_ENOMEM;
264         }
265         path_sanitize( psz_tmp );
266         p_sys->psz_indexPath = psz_tmp;
267         vlc_unlink( p_sys->psz_indexPath );
268     }
269
270     p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
271     p_sys->psz_keyfile  = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-loadfile" );
272     p_sys->key_uri      = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-uri" );
273
274     p_access->p_sys = p_sys;
275
276     if( p_sys->psz_keyfile && ( LoadCryptFile( p_access ) < 0 ) )
277     {
278         free( p_sys->psz_indexUrl );
279         free( p_sys->psz_indexPath );
280         free( p_sys );
281         msg_Err( p_access, "Encryption init failed" );
282         return VLC_EGENERIC;
283     }
284     else if( !p_sys->psz_keyfile && ( CryptSetup( p_access, NULL ) < 0 ) )
285     {
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;
291     }
292
293     p_sys->i_handle = -1;
294     p_sys->i_segment = p_sys->i_initial_segment > 0 ? p_sys->i_initial_segment -1 : 0;
295     p_sys->psz_cursegPath = NULL;
296
297     p_access->pf_write = Write;
298     p_access->pf_seek  = Seek;
299     p_access->pf_control = Control;
300
301     return VLC_SUCCESS;
302 }
303
304 /************************************************************************
305  * CryptSetup: Initialize encryption
306  ************************************************************************/
307 static int CryptSetup( sout_access_out_t *p_access, char *key_file )
308 {
309     sout_access_out_sys_t *p_sys = p_access->p_sys;
310     uint8_t key[16];
311     char *keyfile = NULL;
312
313     if( !p_sys->key_uri ) /*No key uri, assume no encryption wanted*/
314     {
315         msg_Dbg( p_access, "No key uri, no encryption");
316         return VLC_SUCCESS;
317     }
318
319     if( key_file )
320         keyfile = strdup( key_file );
321     else
322         keyfile = var_InheritString( p_access, SOUT_CFG_PREFIX "key-file" );
323
324     if( unlikely(keyfile == NULL) )
325     {
326         msg_Err( p_access, "No key-file, no encryption" );
327         return VLC_EGENERIC;
328     }
329
330     vlc_gcrypt_init();
331
332     /*Setup encryption cipher*/
333     gcry_error_t err = gcry_cipher_open( &p_sys->aes_ctx, GCRY_CIPHER_AES,
334                                          GCRY_CIPHER_MODE_CBC, 0 );
335     if( err )
336     {
337         msg_Err( p_access, "Openin AES Cipher failed: %s", gpg_strerror(err));
338         free( keyfile );
339         return VLC_EGENERIC;
340     }
341
342     int keyfd = vlc_open( keyfile, O_RDONLY | O_NONBLOCK );
343     if( unlikely( keyfd == -1 ) )
344     {
345         msg_Err( p_access, "Unable to open keyfile %s: %s", keyfile,
346                  vlc_strerror_c(errno) );
347         free( keyfile );
348         gcry_cipher_close( p_sys->aes_ctx );
349         return VLC_EGENERIC;
350     }
351     free( keyfile );
352
353     ssize_t keylen = read( keyfd, key, 16 );
354
355     close( keyfd );
356     if( keylen < 16 )
357     {
358         msg_Err( p_access, "No key at least 16 octects (you provided %zd), no encryption", keylen );
359         gcry_cipher_close( p_sys->aes_ctx );
360         return VLC_EGENERIC;
361     }
362
363     err = gcry_cipher_setkey( p_sys->aes_ctx, key, 16 );
364     if(err)
365     {
366         msg_Err(p_access, "Setting AES key failed: %s", gpg_strerror(err));
367         gcry_cipher_close( p_sys->aes_ctx );
368         return VLC_EGENERIC;
369     }
370
371     if( p_sys->b_generate_iv )
372         vlc_rand_bytes( p_sys->aes_ivs, sizeof(uint8_t)*16);
373
374     return VLC_SUCCESS;
375 }
376
377
378 /************************************************************************
379  * LoadCryptFile: Try to parse key_uri and keyfile-location from file
380  ************************************************************************/
381 static int LoadCryptFile( sout_access_out_t *p_access )
382 {
383     sout_access_out_sys_t *p_sys = p_access->p_sys;
384
385     FILE *stream = vlc_fopen( p_sys->psz_keyfile, "rt" );
386     char *key_file=NULL,*key_uri=NULL;
387
388     if( unlikely( stream == NULL ) )
389     {
390         msg_Err( p_access, "Unable to open keyloadfile %s: %s",
391                  p_sys->psz_keyfile, vlc_strerror_c(errno) );
392         return VLC_EGENERIC;
393     }
394
395
396     //First read key_uri
397     ssize_t len = getline( &key_uri, &(size_t){0}, stream );
398     if( unlikely( len == -1 ) )
399     {
400         msg_Err( p_access, "Cannot read %s: %s", p_sys->psz_keyfile,
401                  vlc_strerror_c(errno) );
402         clearerr( stream );
403         fclose( stream );
404         free( key_uri );
405         return VLC_EGENERIC;
406     }
407     //Strip the newline from uri, maybe scanf would be better?
408     key_uri[len-1]='\0';
409
410     len = getline( &key_file, &(size_t){0}, stream );
411     if( unlikely( len == -1 ) )
412     {
413         msg_Err( p_access, "Cannot read %s: %s", p_sys->psz_keyfile,
414                  vlc_strerror_c(errno) );
415         clearerr( stream );
416         fclose( stream );
417
418         free( key_uri );
419         free( key_file );
420         return VLC_EGENERIC;
421     }
422     // Strip the last newline from filename
423     key_file[len-1]='\0';
424     fclose( stream );
425
426     int returncode = VLC_SUCCESS;
427     if( !p_sys->key_uri || strcmp( p_sys->key_uri, key_uri ) )
428     {
429         if( p_sys->key_uri )
430         {
431             free( p_sys->key_uri );
432             p_sys->key_uri = NULL;
433         }
434         p_sys->key_uri = strdup( key_uri );
435         returncode = CryptSetup( p_access, key_file );
436     }
437     free( key_file );
438     free( key_uri );
439     return returncode;
440 }
441
442 /************************************************************************
443  * CryptKey: Set encryption IV to current segment number
444  ************************************************************************/
445 static int CryptKey( sout_access_out_t *p_access, uint32_t i_segment )
446 {
447     sout_access_out_sys_t *p_sys = p_access->p_sys;
448
449     if( !p_sys->b_generate_iv )
450     {
451         /* Use segment number as IV if randomIV isn't selected*/
452         memset( p_sys->aes_ivs, 0, 16 * sizeof(uint8_t));
453         p_sys->aes_ivs[15] = i_segment & 0xff;
454         p_sys->aes_ivs[14] = (i_segment >> 8 ) & 0xff;
455         p_sys->aes_ivs[13] = (i_segment >> 16 ) & 0xff;
456         p_sys->aes_ivs[12] = (i_segment >> 24 ) & 0xff;
457     }
458
459     gcry_error_t err = gcry_cipher_setiv( p_sys->aes_ctx,
460                                           p_sys->aes_ivs, 16);
461     if( err )
462     {
463         msg_Err(p_access, "Setting AES IVs failed: %s", gpg_strerror(err) );
464         gcry_cipher_close( p_sys->aes_ctx);
465         return VLC_EGENERIC;
466     }
467     return VLC_SUCCESS;
468 }
469
470
471 #define SEG_NUMBER_PLACEHOLDER "#"
472 /*****************************************************************************
473  * formatSegmentPath: create segment path name based on seg #
474  *****************************************************************************/
475 static char *formatSegmentPath( char *psz_path, uint32_t i_seg, bool b_sanitize )
476 {
477     char *psz_result;
478     char *psz_firstNumSign;
479
480     if ( ! ( psz_result  = str_format_time( psz_path ) ) )
481         return NULL;
482
483     psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
484     if ( *psz_firstNumSign )
485     {
486         char *psz_newResult;
487         int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
488         int ret;
489
490         *psz_firstNumSign = '\0';
491         ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
492         free ( psz_result );
493         if ( ret < 0 )
494             return NULL;
495         psz_result = psz_newResult;
496     }
497
498     if ( b_sanitize )
499         path_sanitize( psz_result );
500
501     return psz_result;
502 }
503
504 static void destroySegment( output_segment_t *segment )
505 {
506     free( segment->psz_filename );
507     free( segment->psz_duration );
508     free( segment->psz_uri );
509     free( segment->psz_key_uri );
510     free( segment );
511 }
512
513 /************************************************************************
514  * segmentAmountNeeded: check that playlist has atleast 3*p_sys->i_seglength of segments
515  * return how many segments are needed for that (max of p_sys->i_segment )
516  ************************************************************************/
517 static uint32_t segmentAmountNeeded( sout_access_out_sys_t *p_sys )
518 {
519     float duration = .0f;
520     for( unsigned index = 1; (int)index <= vlc_array_count( p_sys->segments_t ); index++ )
521     {
522         output_segment_t* segment = vlc_array_item_at_index( p_sys->segments_t, vlc_array_count( p_sys->segments_t ) - index );
523         duration += segment->f_seglength;
524
525         if( duration >= (float)( 3 * p_sys->i_seglen ) )
526             return __MAX(index, p_sys->i_numsegs);
527     }
528     return vlc_array_count( p_sys->segments_t )-1;
529
530 }
531
532
533 /************************************************************************
534  * isFirstItemRemovable: Check for draft 11 section 6.2.2
535  * check that the first item has been around outside playlist
536  * segment->f_seglength + (p_sys->i_numsegs * p_sys->i_seglen) before it is removed.
537  ************************************************************************/
538 static bool isFirstItemRemovable( sout_access_out_sys_t *p_sys, uint32_t i_firstseg, uint32_t i_index_offset )
539 {
540     float duration = .0f;
541
542     /* Check that segment has been out of playlist for seglenght + (p_sys->i_numsegs * p_sys->i_seglen) amount
543      * We check this by calculating duration of the items that replaced first item in playlist
544      */
545     for( unsigned int index = 0; index < i_index_offset; index++ )
546     {
547         output_segment_t *segment = vlc_array_item_at_index( p_sys->segments_t, p_sys->i_segment - i_firstseg + index );
548         duration += segment->f_seglength;
549     }
550     output_segment_t *first = vlc_array_item_at_index( p_sys->segments_t, 0 );
551
552     return duration >= (first->f_seglength + (float)(p_sys->i_numsegs * p_sys->i_seglen));
553 }
554
555 /************************************************************************
556  * updateIndexAndDel: If necessary, update index file & delete old segments
557  ************************************************************************/
558 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
559 {
560
561     uint32_t i_firstseg;
562     unsigned i_index_offset = 0;
563
564     if ( p_sys->i_numsegs == 0 ||
565          p_sys->i_segment < ( p_sys->i_numsegs + p_sys->i_initial_segment ) )
566     {
567         i_firstseg = p_sys->i_initial_segment == 0 ? 1 : p_sys->i_initial_segment;
568     }
569     else
570     {
571         unsigned numsegs = segmentAmountNeeded( p_sys );
572         i_firstseg = ( p_sys->i_segment - numsegs ) + 1;
573         i_index_offset = vlc_array_count( p_sys->segments_t ) - numsegs;
574     }
575
576     // First update index
577     if ( p_sys->psz_indexPath )
578     {
579         int val;
580         FILE *fp;
581         char *psz_idxTmp;
582         if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
583             return -1;
584
585         fp = vlc_fopen( psz_idxTmp, "wt");
586         if ( !fp )
587         {
588             msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
589             free( psz_idxTmp );
590             return -1;
591         }
592
593         if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%zu\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:%s"
594                           "%s\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n", p_sys->i_seglen,
595                           p_sys->b_caching ? "YES" : "NO",
596                           p_sys->i_numsegs > 0 ? "" : b_isend ? "\n#EXT-X-PLAYLIST-TYPE:VOD" : "\n#EXT-X-PLAYLIST-TYPE:EVENT",
597                           i_firstseg ) < 0 )
598         {
599             free( psz_idxTmp );
600             fclose( fp );
601             return -1;
602         }
603         char *psz_current_uri=NULL;
604
605
606         for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
607         {
608             //scale to i_index_offset..numsegs + i_index_offset
609             uint32_t index = i - i_firstseg + i_index_offset;
610
611             output_segment_t *segment = (output_segment_t *)vlc_array_item_at_index( p_sys->segments_t, index );
612             if( p_sys->key_uri &&
613                 ( !psz_current_uri ||  strcmp( psz_current_uri, segment->psz_key_uri ) )
614               )
615             {
616                 int ret = 0;
617                 free( psz_current_uri );
618                 psz_current_uri = strdup( segment->psz_key_uri );
619                 if( p_sys->b_generate_iv )
620                 {
621                     unsigned long long iv_hi = segment->aes_ivs[0];
622                     unsigned long long iv_lo = segment->aes_ivs[8];
623                     for( unsigned short i = 1; i < 8; i++ )
624                     {
625                         iv_hi <<= 8;
626                         iv_hi |= segment->aes_ivs[i] & 0xff;
627                         iv_lo <<= 8;
628                         iv_lo |= segment->aes_ivs[8+i] & 0xff;
629                     }
630                     ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\",IV=0X%16.16llx%16.16llx\n",
631                                    segment->psz_key_uri, iv_hi, iv_lo );
632
633                 } else {
634                     ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"\n", segment->psz_key_uri );
635                 }
636                 if( ret < 0 )
637                 {
638                     free( psz_current_uri );
639                     free( psz_idxTmp );
640                     fclose( fp );
641                     return -1;
642                 }
643             }
644
645             val = fprintf( fp, "#EXTINF:%s,\n%s\n", segment->psz_duration, segment->psz_uri);
646             if ( val < 0 )
647             {
648                 free( psz_current_uri );
649                 free( psz_idxTmp );
650                 fclose( fp );
651                 return -1;
652             }
653         }
654         free( psz_current_uri );
655
656         if ( b_isend )
657         {
658             if ( fputs ( STR_ENDLIST, fp ) < 0)
659             {
660                 free( psz_idxTmp );
661                 fclose( fp ) ;
662                 return -1;
663             }
664
665         }
666         fclose( fp );
667
668         val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
669
670         if ( val < 0 )
671         {
672             vlc_unlink( psz_idxTmp );
673             msg_Err( p_access, "Error moving LiveHttp index file" );
674         }
675         else
676             msg_Dbg( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
677
678         free( psz_idxTmp );
679     }
680
681     // Then take care of deletion
682     // Try to follow pantos draft 11 section 6.2.2
683     while( p_sys->b_delsegs && p_sys->i_numsegs &&
684            isFirstItemRemovable( p_sys, i_firstseg, i_index_offset )
685          )
686     {
687          output_segment_t *segment = vlc_array_item_at_index( p_sys->segments_t, 0 );
688          msg_Dbg( p_access, "Removing segment number %d", segment->i_segment_number );
689          vlc_array_remove( p_sys->segments_t, 0 );
690
691          if ( segment->psz_filename )
692          {
693              vlc_unlink( segment->psz_filename );
694          }
695
696          destroySegment( segment );
697          i_index_offset -=1;
698     }
699
700
701     return 0;
702 }
703
704 /*****************************************************************************
705  * closeCurrentSegment: Close the segment file
706  *****************************************************************************/
707 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
708 {
709     if ( p_sys->i_handle >= 0 )
710     {
711         output_segment_t *segment = (output_segment_t *)vlc_array_item_at_index( p_sys->segments_t, vlc_array_count( p_sys->segments_t ) - 1 );
712
713         if( p_sys->key_uri )
714         {
715             size_t pad = 16 - p_sys->stuffing_size;
716             memset(&p_sys->stuffing_bytes[p_sys->stuffing_size], pad, pad);
717             gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx, p_sys->stuffing_bytes, 16, NULL, 0 );
718
719             if( err ) {
720                msg_Err( p_access, "Couldn't encrypt 16 bytes: %s", gpg_strerror(err) );
721             } else {
722             int ret = write( p_sys->i_handle, p_sys->stuffing_bytes, 16 );
723             if( ret != 16 )
724                 msg_Err( p_access, "Couldn't write 16 bytes" );
725             }
726             p_sys->stuffing_size = 0;
727         }
728
729
730         close( p_sys->i_handle );
731         p_sys->i_handle = -1;
732
733         if( ! ( us_asprintf( &segment->psz_duration, "%.2f", p_sys->f_seglen ) ) )
734         {
735             msg_Err( p_access, "Couldn't set duration on closed segment");
736             return;
737         }
738         segment->f_seglength = p_sys->f_seglen;
739
740         segment->i_segment_number = p_sys->i_segment;
741
742         if ( p_sys->psz_cursegPath )
743         {
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 );
748         }
749     }
750 }
751
752 /*****************************************************************************
753  * Close: close the target
754  *****************************************************************************/
755 static void Close( vlc_object_t * p_this )
756 {
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;
759     block_t *output_block = p_sys->block_buffer;
760     p_sys->block_buffer = NULL;
761
762     while( output_block )
763     {
764         block_t *p_next = output_block->p_next;
765         output_block->p_next = NULL;
766
767         /* Since we are flushing, check the segment change by hand and don't wait
768          * possible keyframe*/
769         if( p_sys->b_segment_has_data &&  (float)(output_block->i_length + p_sys->i_dts_offset +
770                      output_block->i_dts - p_sys->i_opendts) >= p_sys->i_seglenm )
771         {
772             closeCurrentSegment( p_access, p_sys, false );
773             p_sys->i_dts_offset = 0;
774             if( unlikely(openNextFile( p_access, p_sys ) < 0 ) )
775             {
776                 block_ChainRelease( output_block );
777                 output_block = NULL;
778                 block_ChainRelease( p_next );
779
780                 /* Jump out of the loop so we can close rest of the stuff*/
781                 continue;
782             }
783             p_sys->i_opendts = p_sys->block_buffer ? p_sys->block_buffer->i_dts : output_block->i_dts;
784         }
785         Write( p_access, output_block );
786         output_block = p_next;
787     }
788
789     ssize_t writevalue = writeSegment( p_access );
790     msg_Dbg( p_access, "Writing.. %zd", writevalue );
791     if( unlikely( writevalue < 0 ) )
792     {
793         block_ChainRelease( p_sys->block_buffer );
794         p_sys->block_buffer = NULL;
795     }
796
797     closeCurrentSegment( p_access, p_sys, true );
798
799     if( p_sys->key_uri )
800     {
801         gcry_cipher_close( p_sys->aes_ctx );
802         free( p_sys->key_uri );
803     }
804
805     while( vlc_array_count( p_sys->segments_t ) > 0 )
806     {
807         output_segment_t *segment = vlc_array_item_at_index( p_sys->segments_t, 0 );
808         vlc_array_remove( p_sys->segments_t, 0 );
809         if( p_sys->b_delsegs && p_sys->i_numsegs && segment->psz_filename )
810         {
811             msg_Dbg( p_access, "Removing segment number %d name %s", segment->i_segment_number, segment->psz_filename );
812             vlc_unlink( segment->psz_filename );
813         }
814
815         destroySegment( segment );
816     }
817     vlc_array_destroy( p_sys->segments_t );
818
819     free( p_sys->psz_indexUrl );
820     free( p_sys->psz_indexPath );
821     free( p_sys );
822
823     msg_Dbg( p_access, "livehttp access output closed" );
824 }
825
826 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
827 {
828     sout_access_out_sys_t *p_sys = p_access->p_sys;
829
830     switch( i_query )
831     {
832         case ACCESS_OUT_CONTROLS_PACE:
833         {
834             bool *pb = va_arg( args, bool * );
835             *pb = !p_sys->b_ratecontrol;
836             //*pb = true;
837             break;
838         }
839
840         default:
841             return VLC_EGENERIC;
842     }
843     return VLC_SUCCESS;
844 }
845
846 /*****************************************************************************
847  * openNextFile: Open the segment file
848  *****************************************************************************/
849 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
850 {
851     int fd;
852
853     uint32_t i_newseg = p_sys->i_segment + 1;
854
855     /* Create segment and fill it info that we can (everything excluding duration */
856     output_segment_t *segment = (output_segment_t*)calloc(1, sizeof(output_segment_t));
857     if( unlikely( !segment ) )
858         return -1;
859
860     segment->i_segment_number = i_newseg;
861     segment->psz_filename = formatSegmentPath( p_access->psz_path, i_newseg, true );
862     char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
863     segment->psz_uri = formatSegmentPath( psz_idxFormat , i_newseg, false );
864
865     if ( unlikely( !segment->psz_filename ) )
866     {
867         msg_Err( p_access, "Format segmentpath failed");
868         destroySegment( segment );
869         return -1;
870     }
871
872     fd = vlc_open( segment->psz_filename, O_WRONLY | O_CREAT | O_LARGEFILE |
873                      O_TRUNC, 0666 );
874     if ( fd == -1 )
875     {
876         msg_Err( p_access, "cannot open `%s' (%s)", segment->psz_filename,
877                  vlc_strerror_c(errno) );
878         destroySegment( segment );
879         return -1;
880     }
881
882     vlc_array_append( p_sys->segments_t, segment);
883
884     if( p_sys->psz_keyfile )
885     {
886         LoadCryptFile( p_access );
887     }
888
889     if( p_sys->key_uri )
890     {
891         segment->psz_key_uri = strdup( p_sys->key_uri );
892         CryptKey( p_access, i_newseg );
893         if( p_sys->b_generate_iv )
894             memcpy( segment->aes_ivs, p_sys->aes_ivs, sizeof(uint8_t)*16 );
895     }
896     msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , segment->psz_filename, i_newseg );
897
898     p_sys->psz_cursegPath = strdup(segment->psz_filename);
899     p_sys->i_handle = fd;
900     p_sys->i_segment = i_newseg;
901     p_sys->b_segment_has_data = false;
902     return fd;
903 }
904 /*****************************************************************************
905  * CheckSegmentChange: Check if segment needs to be closed and new opened
906  *****************************************************************************/
907 static int CheckSegmentChange( sout_access_out_t *p_access, block_t *p_buffer )
908 {
909     sout_access_out_sys_t *p_sys = p_access->p_sys;
910     block_t *output = p_sys->block_buffer;
911
912     /* let's check if we need to store offset to keep
913      * better count of actual duration */
914     if( unlikely( p_buffer->i_dts < p_sys->i_opendts ) )
915     {
916         block_t *last_buffer = p_sys->block_buffer;
917         while( last_buffer->p_next )
918             last_buffer = last_buffer->p_next;
919         p_sys->i_dts_offset += last_buffer->i_dts - p_sys->i_opendts;
920         p_sys->i_opendts    = p_buffer->i_dts;
921         msg_Dbg( p_access, "dts offset %"PRId64, p_sys->i_dts_offset );
922     }
923
924     if( p_sys->i_handle > 0 && p_sys->b_segment_has_data &&
925        (( p_buffer->i_length + p_buffer->i_dts - p_sys->i_opendts +
926           p_sys->i_dts_offset ) >= p_sys->i_seglenm ) )
927     {
928         closeCurrentSegment( p_access, p_sys, false );
929     }
930
931     if ( unlikely( p_sys->i_handle < 0 ) )
932     {
933         p_sys->i_dts_offset = 0;
934         p_sys->i_opendts = output ? output->i_dts : p_buffer->i_dts;
935         //For first segment we can get negative duration otherwise...?
936         if( ( p_sys->i_opendts != VLC_TS_INVALID ) &&
937             ( p_buffer->i_dts < p_sys->i_opendts ) )
938             p_sys->i_opendts = p_buffer->i_dts;
939
940         if ( openNextFile( p_access, p_sys ) < 0 )
941            return VLC_EGENERIC;
942     }
943     return VLC_SUCCESS;
944 }
945
946 static ssize_t writeSegment( sout_access_out_t *p_access )
947 {
948     sout_access_out_sys_t *p_sys = p_access->p_sys;
949     block_t *output = p_sys->block_buffer ? block_ChainGather( p_sys->block_buffer ) : NULL;
950     p_sys->block_buffer = NULL;
951     ssize_t i_write=0;
952     bool crypted = false;
953     while( output )
954     {
955         if( p_sys->key_uri && !crypted )
956         {
957             if( p_sys->stuffing_size )
958             {
959                 output = block_Realloc( output, p_sys->stuffing_size, output->i_buffer );
960                 if( unlikely(!output ) )
961                     return VLC_ENOMEM;
962                 memcpy( output->p_buffer, p_sys->stuffing_bytes, p_sys->stuffing_size );
963                 p_sys->stuffing_size = 0;
964             }
965             size_t original = output->i_buffer;
966             size_t padded = (output->i_buffer + 15 ) & ~15;
967             size_t pad = padded - original;
968             if( pad )
969             {
970                 p_sys->stuffing_size = 16-pad;
971                 output->i_buffer -= p_sys->stuffing_size;
972                 memcpy(p_sys->stuffing_bytes, &output->p_buffer[output->i_buffer], p_sys->stuffing_size);
973             }
974
975             gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx,
976                                 output->p_buffer, output->i_buffer, NULL, 0 );
977             if( err )
978             {
979                 msg_Err( p_access, "Encryption failure: %s ", gpg_strerror(err) );
980                 return -1;
981             }
982             crypted=true;
983
984         }
985         ssize_t val = write( p_sys->i_handle, output->p_buffer, output->i_buffer );
986         if ( val == -1 )
987         {
988            if ( errno == EINTR )
989               continue;
990            return -1;
991         }
992
993         p_sys->f_seglen =
994             (float)(output->i_length +
995                     output->i_dts - p_sys->i_opendts + p_sys->i_dts_offset) / CLOCK_FREQ;
996
997         if ( (size_t)val >= output->i_buffer )
998         {
999            block_t *p_next = output->p_next;
1000            block_Release (output);
1001            output = p_next;
1002            crypted=false;
1003         }
1004         else
1005         {
1006            output->p_buffer += val;
1007            output->i_buffer -= val;
1008         }
1009         i_write += val;
1010     }
1011     return i_write;
1012 }
1013
1014 /*****************************************************************************
1015  * Write: standard write on a file descriptor.
1016  *****************************************************************************/
1017 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
1018 {
1019     size_t i_write = 0;
1020     sout_access_out_sys_t *p_sys = p_access->p_sys;
1021     block_t *p_temp;
1022     while( p_buffer )
1023     {
1024         if( ( p_sys->b_splitanywhere  || ( p_buffer->i_flags & BLOCK_FLAG_HEADER ) ) )
1025         {
1026             if( unlikely( CheckSegmentChange( p_access, p_buffer ) != VLC_SUCCESS ) )
1027             {
1028                 block_ChainRelease ( p_buffer );
1029                 return -1;
1030             }
1031
1032             ssize_t writevalue = writeSegment( p_access );
1033             if( unlikely( writevalue < 0 ) )
1034             {
1035                 block_ChainRelease ( p_buffer );
1036                 return -1;
1037             }
1038             p_sys->b_segment_has_data = true;
1039             i_write += writevalue;
1040         }
1041
1042         p_temp = p_buffer->p_next;
1043         p_buffer->p_next = NULL;
1044         block_ChainAppend( &p_sys->block_buffer, p_buffer );
1045         p_buffer = p_temp;
1046     }
1047
1048     return i_write;
1049 }
1050
1051 /*****************************************************************************
1052  * Seek: seek to a specific location in a file
1053  *****************************************************************************/
1054 static int Seek( sout_access_out_t *p_access, off_t i_pos )
1055 {
1056     (void) i_pos;
1057     msg_Err( p_access, "livehttp sout access cannot seek" );
1058     return -1;
1059 }