]> git.sesse.net Git - vlc/blob - modules/access_output/livehttp.c
livehttp: show segment specific iv/key location if they have changed
[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 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_sout.h>
44 #include <vlc_block.h>
45 #include <vlc_fs.h>
46 #include <vlc_strings.h>
47 #include <vlc_charset.h>
48
49 #include <gcrypt.h>
50 #include <vlc_gcrypt.h>
51
52 #include <vlc_rand.h>
53
54 #ifndef O_LARGEFILE
55 #   define O_LARGEFILE 0
56 #endif
57
58 #define STR_ENDLIST "#EXT-X-ENDLIST\n"
59
60 #define MAX_RENAME_RETRIES        10
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 static int  Open ( vlc_object_t * );
66 static void Close( vlc_object_t * );
67
68 #define SOUT_CFG_PREFIX "sout-livehttp-"
69 #define SEGLEN_TEXT N_("Segment length")
70 #define SEGLEN_LONGTEXT N_("Length of TS stream segments")
71
72 #define SPLITANYWHERE_TEXT N_("Split segments anywhere")
73 #define SPLITANYWHERE_LONGTEXT N_("Don't require a keyframe before splitting "\
74                                 "a segment. Needed for audio only.")
75
76 #define NUMSEGS_TEXT N_("Number of segments")
77 #define NUMSEGS_LONGTEXT N_("Number of segments to include in index")
78
79 #define NOCACHE_TEXT N_("Allow cache")
80 #define NOCACHE_LONGTEXT N_("Add EXT-X-ALLOW-CACHE:NO directive in playlist-file if this is disabled")
81
82 #define INDEX_TEXT N_("Index file")
83 #define INDEX_LONGTEXT N_("Path to the index file to create")
84
85 #define INDEXURL_TEXT N_("Full URL to put in index file")
86 #define INDEXURL_LONGTEXT N_("Full URL to put in index file. "\
87                           "Use #'s to represent segment number")
88
89 #define DELSEGS_TEXT N_("Delete segments")
90 #define DELSEGS_LONGTEXT N_("Delete segments when they are no longer needed")
91
92 #define RATECONTROL_TEXT N_("Use muxers rate control mechanism")
93
94 #define KEYURI_TEXT N_("AES key URI to place in playlist")
95 #define KEYURI_LONGTEXT N_("Location from where client will retrieve the stream decryption key")
96
97 #define KEYFILE_TEXT N_("AES key file")
98 #define KEYFILE_LONGTEXT N_("File containing the 16 bytes encryption key")
99
100 #define RANDOMIV_TEXT N_("Use randomized IV for encryption")
101 #define RANDOMIV_LONGTEXT N_("Generate IV instead using segment-number as IV")
102
103 vlc_module_begin ()
104     set_description( N_("HTTP Live streaming output") )
105     set_shortname( N_("LiveHTTP" ))
106     add_shortcut( "livehttp" )
107     set_capability( "sout access", 0 )
108     set_category( CAT_SOUT )
109     set_subcategory( SUBCAT_SOUT_ACO )
110     add_integer( SOUT_CFG_PREFIX "seglen", 10, SEGLEN_TEXT, SEGLEN_LONGTEXT, false )
111     add_integer( SOUT_CFG_PREFIX "numsegs", 0, NUMSEGS_TEXT, NUMSEGS_LONGTEXT, false )
112     add_bool( SOUT_CFG_PREFIX "splitanywhere", false,
113               SPLITANYWHERE_TEXT, SPLITANYWHERE_LONGTEXT, true )
114     add_bool( SOUT_CFG_PREFIX "delsegs", true,
115               DELSEGS_TEXT, DELSEGS_LONGTEXT, true )
116     add_bool( SOUT_CFG_PREFIX "ratecontrol", false,
117               RATECONTROL_TEXT, RATECONTROL_TEXT, true )
118     add_bool( SOUT_CFG_PREFIX "caching", false,
119               NOCACHE_TEXT, NOCACHE_LONGTEXT, true )
120     add_bool( SOUT_CFG_PREFIX "generate-iv", false,
121               RANDOMIV_TEXT, RANDOMIV_LONGTEXT, true )
122     add_string( SOUT_CFG_PREFIX "index", NULL,
123                 INDEX_TEXT, INDEX_LONGTEXT, false )
124     add_string( SOUT_CFG_PREFIX "index-url", NULL,
125                 INDEXURL_TEXT, INDEXURL_LONGTEXT, false )
126     add_string( SOUT_CFG_PREFIX "key-uri", NULL,
127                 KEYURI_TEXT, KEYURI_TEXT, true )
128     add_loadfile( SOUT_CFG_PREFIX "key-file", NULL,
129                 KEYFILE_TEXT, KEYFILE_LONGTEXT, true )
130     set_callbacks( Open, Close )
131 vlc_module_end ()
132
133
134 /*****************************************************************************
135  * Exported prototypes
136  *****************************************************************************/
137 static const char *const ppsz_sout_options[] = {
138     "seglen",
139     "splitanywhere",
140     "numsegs",
141     "delsegs",
142     "index",
143     "index-url",
144     "ratecontrol",
145     "caching",
146     "key-uri",
147     "key-file",
148     "generate-iv",
149     NULL
150 };
151
152 static ssize_t Write( sout_access_out_t *, block_t * );
153 static int Seek ( sout_access_out_t *, off_t  );
154 static int Control( sout_access_out_t *, int, va_list );
155
156 typedef struct output_segment
157 {
158     char *psz_filename;
159     char *psz_uri;
160     char *psz_key_uri;
161     char *psz_duration;
162     uint32_t i_segment_number;
163     uint8_t aes_ivs[16];
164 } output_segment_t;
165
166 struct sout_access_out_sys_t
167 {
168     char *psz_cursegPath;
169     char *psz_indexPath;
170     char *psz_indexUrl;
171     mtime_t i_opendts;
172     mtime_t  i_seglenm;
173     uint32_t i_segment;
174     size_t  i_seglen;
175     float   f_seglen;
176     block_t *block_buffer;
177     int i_handle;
178     unsigned i_numsegs;
179     bool b_delsegs;
180     bool b_ratecontrol;
181     bool b_splitanywhere;
182     bool b_caching;
183     bool b_generate_iv;
184     uint8_t aes_ivs[16];
185     gcry_cipher_hd_t aes_ctx;
186     char *key_uri;
187     uint8_t stuffing_bytes[16];
188     ssize_t stuffing_size;
189     vlc_array_t *segments_t;
190 };
191
192 static int CryptSetup( sout_access_out_t *p_access );
193 /*****************************************************************************
194  * Open: open the file
195  *****************************************************************************/
196 static int Open( vlc_object_t *p_this )
197 {
198     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
199     sout_access_out_sys_t *p_sys;
200     char *psz_idx;
201
202     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
203
204     if( !p_access->psz_path )
205     {
206         msg_Err( p_access, "no file name specified" );
207         return VLC_EGENERIC;
208     }
209
210     if( unlikely( !( p_sys = malloc ( sizeof( *p_sys ) ) ) ) )
211         return VLC_ENOMEM;
212
213     p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
214     /* Try to get within asked segment length */
215     p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen;
216     p_sys->block_buffer = NULL;
217
218     p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
219     p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
220     p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
221     p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
222     p_sys->b_caching = var_GetBool( p_access, SOUT_CFG_PREFIX "caching") ;
223     p_sys->b_generate_iv = var_GetBool( p_access, SOUT_CFG_PREFIX "generate-iv") ;
224
225     p_sys->segments_t = vlc_array_new();
226
227     p_sys->stuffing_size = 0;
228
229     p_sys->psz_indexPath = NULL;
230     psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
231     if ( psz_idx )
232     {
233         char *psz_tmp;
234         psz_tmp = str_format_time( psz_idx );
235         free( psz_idx );
236         if ( !psz_tmp )
237         {
238             free( p_sys );
239             return VLC_ENOMEM;
240         }
241         path_sanitize( psz_tmp );
242         p_sys->psz_indexPath = psz_tmp;
243         vlc_unlink( p_sys->psz_indexPath );
244     }
245
246     p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
247
248     p_access->p_sys = p_sys;
249
250     if( CryptSetup( p_access ) < 0 )
251     {
252         free( p_sys->psz_indexUrl );
253         free( p_sys->psz_indexPath );
254         free( p_sys );
255         msg_Err( p_access, "Encryption init failed" );
256         return VLC_EGENERIC;
257     }
258
259     p_sys->i_handle = -1;
260     p_sys->i_segment = 0;
261     p_sys->psz_cursegPath = NULL;
262
263     p_access->pf_write = Write;
264     p_access->pf_seek  = Seek;
265     p_access->pf_control = Control;
266
267     return VLC_SUCCESS;
268 }
269
270 /************************************************************************
271  * CryptSetup: Initialize encryption
272  ************************************************************************/
273 static int CryptSetup( sout_access_out_t *p_access )
274 {
275     sout_access_out_sys_t *p_sys = p_access->p_sys;
276     uint8_t key[16];
277
278     p_sys->key_uri = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-uri" );
279     if( !p_sys->key_uri ) /*No key uri, assume no encryption wanted*/
280     {
281         msg_Dbg( p_access, "No key uri, no encryption");
282         return VLC_SUCCESS;
283     }
284     vlc_gcrypt_init();
285
286     /*Setup encryption cipher*/
287     gcry_error_t err = gcry_cipher_open( &p_sys->aes_ctx, GCRY_CIPHER_AES,
288                                          GCRY_CIPHER_MODE_CBC, 0 );
289     if( err )
290     {
291         msg_Err( p_access, "Openin AES Cipher failed: %s", gpg_strerror(err));
292         return VLC_EGENERIC;
293     }
294
295     char *keyfile = var_InheritString( p_access, SOUT_CFG_PREFIX "key-file" );
296     if( unlikely(keyfile == NULL) )
297     {
298         msg_Err( p_access, "No key-file, no encryption" );
299         return VLC_EGENERIC;
300     }
301
302     int keyfd = vlc_open( keyfile, O_RDONLY | O_NONBLOCK );
303     if( unlikely( keyfd == -1 ) )
304     {
305         msg_Err( p_access, "Unable to open keyfile %s: %m", keyfile );
306         free( keyfile );
307         return VLC_EGENERIC;
308     }
309     free( keyfile );
310
311     ssize_t keylen = read( keyfd, key, 16 );
312
313     close( keyfd );
314     if( keylen < 16 )
315     {
316         msg_Err( p_access, "No key at least 16 octects (you provided %zd), no encryption", keylen );
317         return VLC_EGENERIC;
318     }
319
320     err = gcry_cipher_setkey( p_sys->aes_ctx, key, 16 );
321     if(err)
322     {
323         msg_Err(p_access, "Setting AES key failed: %s", gpg_strerror(err));
324         gcry_cipher_close( p_sys->aes_ctx);
325         return VLC_EGENERIC;
326     }
327
328     if( p_sys->b_generate_iv )
329         vlc_rand_bytes( p_sys->aes_ivs, sizeof(uint8_t)*16);
330
331     return VLC_SUCCESS;
332 }
333
334 /************************************************************************
335  * CryptKey: Set encryption IV to current segment number
336  ************************************************************************/
337 static int CryptKey( sout_access_out_t *p_access, uint32_t i_segment )
338 {
339     sout_access_out_sys_t *p_sys = p_access->p_sys;
340
341     if( !p_sys->b_generate_iv )
342     {
343         /* Use segment number as IV if randomIV isn't selected*/
344         memset( p_sys->aes_ivs, 0, 16 * sizeof(uint8_t));
345         p_sys->aes_ivs[15] = i_segment & 0xff;
346         p_sys->aes_ivs[14] = (i_segment >> 8 ) & 0xff;
347         p_sys->aes_ivs[13] = (i_segment >> 16 ) & 0xff;
348         p_sys->aes_ivs[12] = (i_segment >> 24 ) & 0xff;
349     }
350
351     gcry_error_t err = gcry_cipher_setiv( p_sys->aes_ctx,
352                                           p_sys->aes_ivs, 16);
353     if( err )
354     {
355         msg_Err(p_access, "Setting AES IVs failed: %s", gpg_strerror(err) );
356         gcry_cipher_close( p_sys->aes_ctx);
357         return VLC_EGENERIC;
358     }
359     return VLC_SUCCESS;
360 }
361
362
363 #define SEG_NUMBER_PLACEHOLDER "#"
364 /*****************************************************************************
365  * formatSegmentPath: create segment path name based on seg #
366  *****************************************************************************/
367 static char *formatSegmentPath( char *psz_path, uint32_t i_seg, bool b_sanitize )
368 {
369     char *psz_result;
370     char *psz_firstNumSign;
371
372     if ( ! ( psz_result  = str_format_time( psz_path ) ) )
373         return NULL;
374
375     psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
376     if ( *psz_firstNumSign )
377     {
378         char *psz_newResult;
379         int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
380         int ret;
381
382         *psz_firstNumSign = '\0';
383         ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
384         free ( psz_result );
385         if ( ret < 0 )
386             return NULL;
387         psz_result = psz_newResult;
388     }
389
390     if ( b_sanitize )
391         path_sanitize( psz_result );
392
393     return psz_result;
394 }
395
396 static void destroySegment( output_segment_t *segment )
397 {
398     free( segment->psz_filename );
399     free( segment->psz_duration );
400     free( segment->psz_uri );
401     free( segment->psz_key_uri );
402     free( segment );
403 }
404
405 /************************************************************************
406  * updateIndexAndDel: If necessary, update index file & delete old segments
407  ************************************************************************/
408 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
409 {
410
411     uint32_t i_firstseg;
412
413     if ( p_sys->i_numsegs == 0 || p_sys->i_segment < p_sys->i_numsegs )
414         i_firstseg = 1;
415     else
416         i_firstseg = ( p_sys->i_segment - p_sys->i_numsegs ) + 1;
417
418     // First update index
419     if ( p_sys->psz_indexPath )
420     {
421         int val;
422         FILE *fp;
423         char *psz_idxTmp;
424         if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
425             return -1;
426
427         fp = vlc_fopen( psz_idxTmp, "wt");
428         if ( !fp )
429         {
430             msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
431             free( psz_idxTmp );
432             return -1;
433         }
434
435         if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%zu\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:%s"
436                           "%s\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n", p_sys->i_seglen,
437                           p_sys->b_caching ? "YES" : "NO",
438                           p_sys->i_numsegs > 0 ? "" : b_isend ? "\n#EXT-X-PLAYLIST-TYPE:VOD" : "\n#EXT-X-PLAYLIST-TYPE:EVENT",
439                           i_firstseg ) < 0 )
440         {
441             free( psz_idxTmp );
442             fclose( fp );
443             return -1;
444         }
445         char *psz_current_uri=NULL;
446
447
448         for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
449         {
450             //scale to 0..numsegs-1
451             uint32_t index = i-i_firstseg;
452
453             output_segment_t *segment = (output_segment_t *)vlc_array_item_at_index( p_sys->segments_t, index );
454             if( p_sys->key_uri &&
455                 ( !psz_current_uri ||  strcmp( psz_current_uri, segment->psz_key_uri ) )
456               )
457             {
458                 int ret = 0;
459                 free( psz_current_uri );
460                 psz_current_uri = strdup( segment->psz_key_uri );
461                 if( p_sys->b_generate_iv )
462                 {
463                     unsigned long long iv_hi = 0, iv_lo = 0;
464                     for( unsigned short i = 0; i < 8; i++ )
465                     {
466                         iv_hi |= segment->aes_ivs[i] & 0xff;
467                         iv_hi <<= 8;
468                         iv_lo |= segment->aes_ivs[8+i] & 0xff;
469                         iv_lo <<= 8;
470                     }
471                     ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\",IV=0X%16.16llx%16.16llx\n",
472                                    segment->psz_key_uri, iv_hi, iv_lo );
473
474                 } else {
475                     ret = fprintf( fp, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"\n", segment->psz_key_uri );
476                 }
477                 if( ret < 0 )
478                 {
479                     free( psz_idxTmp );
480                     fclose( fp );
481                     return -1;
482                 }
483             }
484
485             val = fprintf( fp, "#EXTINF:%s,\n%s\n", segment->psz_duration, segment->psz_uri);
486             if ( val < 0 )
487             {
488                 fclose( fp );
489                 return -1;
490             }
491         }
492         free( psz_current_uri );
493
494         if ( b_isend )
495         {
496             if ( fputs ( STR_ENDLIST, fp ) < 0)
497             {
498                 free( psz_idxTmp );
499                 fclose( fp ) ;
500                 return -1;
501             }
502
503         }
504         fclose( fp );
505
506         val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
507
508         if ( val < 0 )
509         {
510             vlc_unlink( psz_idxTmp );
511             msg_Err( p_access, "Error moving LiveHttp index file" );
512         }
513         else
514             msg_Dbg( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
515
516         free( psz_idxTmp );
517     }
518
519     // Then take care of deletion
520     while( p_sys->b_delsegs && p_sys->i_numsegs && ( (vlc_array_count( p_sys->segments_t ) ) >= p_sys->i_numsegs ) )
521     {
522          output_segment_t *segment = vlc_array_item_at_index( p_sys->segments_t, 0 );
523          vlc_array_remove( p_sys->segments_t, 0 );
524          if ( segment->psz_filename )
525          {
526              vlc_unlink( segment->psz_filename );
527          }
528          destroySegment( segment );
529     }
530
531
532     return 0;
533 }
534
535 /*****************************************************************************
536  * closeCurrentSegment: Close the segment file
537  *****************************************************************************/
538 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
539 {
540     if ( p_sys->i_handle >= 0 )
541     {
542         output_segment_t *segment = (output_segment_t *)vlc_array_item_at_index( p_sys->segments_t, vlc_array_count( p_sys->segments_t ) - 1 );
543
544         if( p_sys->key_uri )
545         {
546             size_t pad = 16 - p_sys->stuffing_size;
547             memset(&p_sys->stuffing_bytes[p_sys->stuffing_size], pad, pad);
548             gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx, p_sys->stuffing_bytes, 16, NULL, 0 );
549
550             if( err ) {
551                msg_Err( p_access, "Couldn't encrypt 16 bytes: %s", gpg_strerror(err) );
552             } else {
553             int ret = write( p_sys->i_handle, p_sys->stuffing_bytes, 16 );
554             if( ret != 16 )
555                 msg_Err( p_access, "Couldn't write 16 bytes" );
556             }
557             p_sys->stuffing_size = 0;
558         }
559
560
561         close( p_sys->i_handle );
562         p_sys->i_handle = -1;
563
564         if( ! ( us_asprintf( &segment->psz_duration, "%.2f", p_sys->f_seglen ) ) )
565         {
566             msg_Err( p_access, "Couldn't set duration on closed segment");
567             return;
568         }
569
570         segment->i_segment_number = p_sys->i_segment;
571
572         if ( p_sys->psz_cursegPath )
573         {
574             msg_Dbg( p_access, "LiveHttpSegmentComplete: %s (%"PRIu32")" , p_sys->psz_cursegPath, p_sys->i_segment );
575             free( p_sys->psz_cursegPath );
576             p_sys->psz_cursegPath = 0;
577             updateIndexAndDel( p_access, p_sys, b_isend );
578         }
579     }
580 }
581
582 /*****************************************************************************
583  * Close: close the target
584  *****************************************************************************/
585 static void Close( vlc_object_t * p_this )
586 {
587     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
588     sout_access_out_sys_t *p_sys = p_access->p_sys;
589
590     msg_Dbg( p_access, "Flushing buffer to last file");
591     bool crypted = false;
592     while( p_sys->block_buffer )
593     {
594         if( p_sys->key_uri && !crypted)
595         {
596             if( p_sys->stuffing_size )
597             {
598                 p_sys->block_buffer = block_Realloc( p_sys->block_buffer, p_sys->stuffing_size, p_sys->block_buffer->i_buffer );
599                 if( unlikely(!p_sys->block_buffer) )
600                     return;
601                 memcpy( p_sys->block_buffer->p_buffer, p_sys->stuffing_bytes, p_sys->stuffing_size );
602                 p_sys->stuffing_size = 0;
603             }
604             size_t original = p_sys->block_buffer->i_buffer;
605             size_t padded = (original + 15 ) & ~15;
606             size_t pad = padded - original;
607             if( pad )
608             {
609                 p_sys->stuffing_size = 16 - pad;
610                 p_sys->block_buffer->i_buffer -= p_sys->stuffing_size;
611                 memcpy( p_sys->stuffing_bytes, &p_sys->block_buffer->p_buffer[p_sys->block_buffer->i_buffer], p_sys->stuffing_size );
612             }
613
614             gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx,
615                                 p_sys->block_buffer->p_buffer, p_sys->block_buffer->i_buffer, NULL, 0 );
616             if( err )
617             {
618                 msg_Err( p_access, "Encryption failure: %s ", gpg_strerror(err) );
619                 break;
620             }
621             crypted = true;
622         }
623         ssize_t val = write( p_sys->i_handle, p_sys->block_buffer->p_buffer, p_sys->block_buffer->i_buffer );
624         if ( val == -1 )
625         {
626            if ( errno == EINTR )
627               continue;
628            block_ChainRelease ( p_sys->block_buffer);
629            break;
630         }
631         if( !p_sys->block_buffer->p_next )
632         {
633             p_sys->f_seglen = (float)( p_sys->block_buffer->i_length / (1000000)) +
634                                (float)(p_sys->block_buffer->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
635         }
636
637         if ( likely( (size_t)val >= p_sys->block_buffer->i_buffer ) )
638         {
639            block_t *p_next = p_sys->block_buffer->p_next;
640            block_Release (p_sys->block_buffer);
641            p_sys->block_buffer = p_next;
642            crypted=false;
643         }
644         else
645         {
646            p_sys->block_buffer->p_buffer += val;
647            p_sys->block_buffer->i_buffer -= val;
648         }
649     }
650
651     closeCurrentSegment( p_access, p_sys, true );
652
653     if( p_sys->key_uri )
654     {
655         gcry_cipher_close( p_sys->aes_ctx );
656         free( p_sys->key_uri );
657     }
658
659     while( vlc_array_count( p_sys->segments_t ) > 0 )
660     {
661         output_segment_t *segment = vlc_array_item_at_index( p_sys->segments_t, 0 );
662         vlc_array_remove( p_sys->segments_t, 0 );
663         destroySegment( segment );
664     }
665     vlc_array_destroy( p_sys->segments_t );
666
667     free( p_sys->psz_indexUrl );
668     free( p_sys->psz_indexPath );
669     free( p_sys );
670
671     msg_Dbg( p_access, "livehttp access output closed" );
672 }
673
674 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
675 {
676     sout_access_out_sys_t *p_sys = p_access->p_sys;
677
678     switch( i_query )
679     {
680         case ACCESS_OUT_CONTROLS_PACE:
681         {
682             bool *pb = va_arg( args, bool * );
683             *pb = !p_sys->b_ratecontrol;
684             //*pb = true;
685             break;
686         }
687
688         default:
689             return VLC_EGENERIC;
690     }
691     return VLC_SUCCESS;
692 }
693
694 /*****************************************************************************
695  * openNextFile: Open the segment file
696  *****************************************************************************/
697 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
698 {
699     int fd;
700
701     uint32_t i_newseg = p_sys->i_segment + 1;
702
703     /* Create segment and fill it info that we can (everything excluding duration */
704     output_segment_t *segment = (output_segment_t*)malloc(sizeof(output_segment_t));
705     if( unlikely( !segment ) )
706         return -1;
707
708     memset( segment, 0 , sizeof( output_segment_t ) );
709
710     segment->i_segment_number = i_newseg;
711     segment->psz_filename = formatSegmentPath( p_access->psz_path, i_newseg, true );
712     char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
713     segment->psz_uri = formatSegmentPath( psz_idxFormat , i_newseg, true );
714
715     if ( unlikely( !segment->psz_filename ) )
716     {
717         msg_Err( p_access, "Format segmentpath failed");
718         return -1;
719     }
720
721
722
723     fd = vlc_open( segment->psz_filename, O_WRONLY | O_CREAT | O_LARGEFILE |
724                      O_TRUNC, 0666 );
725     if ( fd == -1 )
726     {
727         msg_Err( p_access, "cannot open `%s' (%m)", segment->psz_filename );
728         destroySegment( segment );
729         return -1;
730     }
731
732     vlc_array_append( p_sys->segments_t, segment);
733
734     if( p_sys->key_uri )
735     {
736         segment->psz_key_uri = strdup( p_sys->key_uri );
737         CryptKey( p_access, i_newseg );
738         if( p_sys->b_generate_iv )
739             memcpy( segment->aes_ivs, p_sys->aes_ivs, sizeof(uint8_t)*16 );
740     }
741     msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , segment->psz_filename, i_newseg );
742
743     p_sys->psz_cursegPath = strdup(segment->psz_filename);
744     p_sys->i_handle = fd;
745     p_sys->i_segment = i_newseg;
746     return fd;
747 }
748
749 /*****************************************************************************
750  * Write: standard write on a file descriptor.
751  *****************************************************************************/
752 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
753 {
754     size_t i_write = 0;
755     sout_access_out_sys_t *p_sys = p_access->p_sys;
756     block_t *p_temp;
757
758     while( p_buffer )
759     {
760         if ( ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_HEADER ) ) )
761         {
762             bool crypted = false;
763             block_t *output = p_sys->block_buffer;
764             p_sys->block_buffer = NULL;
765
766
767             if( p_sys->i_handle > 0 &&
768                 ( p_buffer->i_dts - p_sys->i_opendts +
769                   p_buffer->i_length * CLOCK_FREQ / INT64_C(1000000)
770                 ) >= p_sys->i_seglenm )
771                 closeCurrentSegment( p_access, p_sys, false );
772
773             if ( p_sys->i_handle < 0 )
774             {
775                 p_sys->i_opendts = output ? output->i_dts : p_buffer->i_dts;
776                 if ( openNextFile( p_access, p_sys ) < 0 )
777                    return -1;
778             }
779
780             while( output )
781             {
782                 if( p_sys->key_uri && !crypted )
783                 {
784                     if( p_sys->stuffing_size )
785                     {
786                         output = block_Realloc( output, p_sys->stuffing_size, output->i_buffer );
787                         if( unlikely(!output ) )
788                             return VLC_ENOMEM;
789                         memcpy( output->p_buffer, p_sys->stuffing_bytes, p_sys->stuffing_size );
790                         p_sys->stuffing_size = 0;
791                     }
792                     size_t original = output->i_buffer;
793                     size_t padded = (output->i_buffer + 15 ) & ~15;
794                     size_t pad = padded - original;
795                     if( pad )
796                     {
797                         p_sys->stuffing_size = 16-pad;
798                         output->i_buffer -= p_sys->stuffing_size;
799                         memcpy(p_sys->stuffing_bytes, &output->p_buffer[output->i_buffer], p_sys->stuffing_size);
800                     }
801
802                     gcry_error_t err = gcry_cipher_encrypt( p_sys->aes_ctx,
803                                         output->p_buffer, output->i_buffer, NULL, 0 );
804                     if( err )
805                     {
806                         msg_Err( p_access, "Encryption failure: %s ", gpg_strerror(err) );
807                         return -1;
808                     }
809                     crypted=true;
810
811                 }
812                 ssize_t val = write( p_sys->i_handle, output->p_buffer, output->i_buffer );
813                 if ( val == -1 )
814                 {
815                    if ( errno == EINTR )
816                       continue;
817                    block_ChainRelease ( p_buffer );
818                    return -1;
819                 }
820                 p_sys->f_seglen =
821                     (float)output->i_length / INT64_C(1000000) +
822                     (float)(output->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
823
824                 if ( (size_t)val >= output->i_buffer )
825                 {
826                    block_t *p_next = output->p_next;
827                    block_Release (output);
828                    output = p_next;
829                    crypted=false;
830                 }
831                 else
832                 {
833                    output->p_buffer += val;
834                    output->i_buffer -= val;
835                 }
836                 i_write += val;
837             }
838         }
839
840         p_temp = p_buffer->p_next;
841         p_buffer->p_next = NULL;
842         block_ChainAppend( &p_sys->block_buffer, p_buffer );
843         p_buffer = p_temp;
844     }
845
846     return i_write;
847 }
848
849 /*****************************************************************************
850  * Seek: seek to a specific location in a file
851  *****************************************************************************/
852 static int Seek( sout_access_out_t *p_access, off_t i_pos )
853 {
854     (void) i_pos;
855     msg_Err( p_access, "livehttp sout access cannot seek" );
856     return -1;
857 }