1 /*****************************************************************************
2 * livehttp.c: Live HTTP Streaming
3 *****************************************************************************
4 * Copyright © 2001, 2002, 2013 VLC authors and VideoLAN
5 * Copyright © 2009-2010 by Keary Griffin
7 * Authors: Keary Griffin <kearygriffin at gmail.com>
8 * Ilkka Ollakka <ileoo at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <sys/types.h>
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
44 #include <vlc_block.h>
46 #include <vlc_strings.h>
47 #include <vlc_charset.h>
50 # define O_LARGEFILE 0
53 #define STR_ENDLIST "#EXT-X-ENDLIST\n"
55 #define MAX_RENAME_RETRIES 10
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 #define SOUT_CFG_PREFIX "sout-livehttp-"
64 #define SEGLEN_TEXT N_("Segment length")
65 #define SEGLEN_LONGTEXT N_("Length of TS stream segments")
67 #define SPLITANYWHERE_TEXT N_("Split segments anywhere")
68 #define SPLITANYWHERE_LONGTEXT N_("Don't require a keyframe before splitting "\
69 "a segment. Needed for audio only.")
71 #define NUMSEGS_TEXT N_("Number of segments")
72 #define NUMSEGS_LONGTEXT N_("Number of segments to include in index")
74 #define NOCACHE_TEXT N_("Allow cache")
75 #define NOCACHE_LONGTEXT N_("Add EXT-X-ALLOW-CACHE:NO directive in playlist-file if this is disabled")
77 #define INDEX_TEXT N_("Index file")
78 #define INDEX_LONGTEXT N_("Path to the index file to create")
80 #define INDEXURL_TEXT N_("Full URL to put in index file")
81 #define INDEXURL_LONGTEXT N_("Full URL to put in index file. "\
82 "Use #'s to represent segment number")
84 #define DELSEGS_TEXT N_("Delete segments")
85 #define DELSEGS_LONGTEXT N_("Delete segments when they are no longer needed")
87 #define RATECONTROL_TEXT N_("Use muxers rate control mechanism")
90 set_description( N_("HTTP Live streaming output") )
91 set_shortname( N_("LiveHTTP" ))
92 add_shortcut( "livehttp" )
93 set_capability( "sout access", 0 )
94 set_category( CAT_SOUT )
95 set_subcategory( SUBCAT_SOUT_ACO )
96 add_integer( SOUT_CFG_PREFIX "seglen", 10, SEGLEN_TEXT, SEGLEN_LONGTEXT, false )
97 add_integer( SOUT_CFG_PREFIX "numsegs", 0, NUMSEGS_TEXT, NUMSEGS_LONGTEXT, false )
98 add_bool( SOUT_CFG_PREFIX "splitanywhere", false,
99 SPLITANYWHERE_TEXT, SPLITANYWHERE_LONGTEXT, true )
100 add_bool( SOUT_CFG_PREFIX "delsegs", true,
101 DELSEGS_TEXT, DELSEGS_LONGTEXT, true )
102 add_bool( SOUT_CFG_PREFIX "ratecontrol", false,
103 RATECONTROL_TEXT, RATECONTROL_TEXT, true )
104 add_bool( SOUT_CFG_PREFIX "caching", false,
105 NOCACHE_TEXT, NOCACHE_LONGTEXT, true )
106 add_string( SOUT_CFG_PREFIX "index", NULL,
107 INDEX_TEXT, INDEX_LONGTEXT, false )
108 add_string( SOUT_CFG_PREFIX "index-url", NULL,
109 INDEXURL_TEXT, INDEXURL_LONGTEXT, false )
110 set_callbacks( Open, Close )
114 /*****************************************************************************
115 * Exported prototypes
116 *****************************************************************************/
117 static const char *const ppsz_sout_options[] = {
129 static ssize_t Write( sout_access_out_t *, block_t * );
130 static int Seek ( sout_access_out_t *, off_t );
131 static int Control( sout_access_out_t *, int, va_list );
133 struct sout_access_out_sys_t
135 char *psz_cursegPath;
143 block_t *block_buffer;
149 bool b_splitanywhere;
153 /*****************************************************************************
154 * Open: open the file
155 *****************************************************************************/
156 static int Open( vlc_object_t *p_this )
158 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
159 sout_access_out_sys_t *p_sys;
162 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
164 if( !p_access->psz_path )
166 msg_Err( p_access, "no file name specified" );
170 if( unlikely( !( p_sys = malloc ( sizeof( *p_sys ) ) ) ) )
173 p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
174 /* Try to get within asked segment length */
175 p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen;
176 p_sys->block_buffer = NULL;
178 p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
179 p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
180 p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
181 p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
182 p_sys->b_caching = var_GetBool( p_access, SOUT_CFG_PREFIX "caching") ;
185 /* 5 elements is from harrison-stetson algorithm to start from some number
186 * if we don't have numsegs defined
188 p_sys->i_seglens = 5;
189 if( p_sys->i_numsegs )
190 p_sys->i_seglens = p_sys->i_numsegs+1;
191 p_sys->p_seglens = malloc( sizeof(float) * p_sys->i_seglens );
192 if( unlikely( !p_sys->p_seglens ) )
198 p_sys->psz_indexPath = NULL;
199 psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
203 psz_tmp = str_format_time( psz_idx );
210 path_sanitize( psz_tmp );
211 p_sys->psz_indexPath = psz_tmp;
212 vlc_unlink( p_sys->psz_indexPath );
215 p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
217 p_access->p_sys = p_sys;
218 p_sys->i_handle = -1;
219 p_sys->i_segment = 0;
220 p_sys->psz_cursegPath = NULL;
222 p_access->pf_write = Write;
223 p_access->pf_seek = Seek;
224 p_access->pf_control = Control;
229 #define SEG_NUMBER_PLACEHOLDER "#"
230 /*****************************************************************************
231 * formatSegmentPath: create segment path name based on seg #
232 *****************************************************************************/
233 static char *formatSegmentPath( char *psz_path, uint32_t i_seg, bool b_sanitize )
236 char *psz_firstNumSign;
238 if ( ! ( psz_result = str_format_time( psz_path ) ) )
241 psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
242 if ( *psz_firstNumSign ) {
244 int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
247 *psz_firstNumSign = '\0';
248 ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
252 psz_result = psz_newResult;
256 path_sanitize( psz_result );
261 /************************************************************************
262 * updateIndexAndDel: If necessary, update index file & delete old segments
263 ************************************************************************/
264 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
269 if ( p_sys->i_numsegs == 0 || p_sys->i_segment < p_sys->i_numsegs )
273 if( p_sys->i_segment >= (p_sys->i_seglens-1) )
275 p_sys->i_seglens <<= 1;
276 msg_Dbg( p_access, "Segment amount %u", p_sys->i_seglens );
277 p_sys->p_seglens = realloc( p_sys->p_seglens, sizeof(float) * p_sys->i_seglens );
278 if( unlikely( !p_sys->p_seglens ) )
283 i_firstseg = ( p_sys->i_segment - p_sys->i_numsegs ) + 1;
285 // First update index
286 if ( p_sys->psz_indexPath )
291 if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
294 fp = vlc_fopen( psz_idxTmp, "wt");
297 msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
302 if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%zu\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:%s\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n", p_sys->i_seglen, p_sys->b_caching ? "YES" : "NO",i_firstseg ) < 0 )
309 char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
310 for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
313 char *psz_duration = NULL;
314 if ( ! ( psz_name = formatSegmentPath( psz_idxFormat, i, false ) ) )
320 if( ! ( us_asprintf( &psz_duration, "%.2f", p_sys->p_seglens[i % p_sys->i_seglens ], psz_name ) ) )
326 val = fprintf( fp, "#EXTINF:%s,\n%s\n", psz_duration, psz_name );
327 free( psz_duration );
339 if ( fputs ( STR_ENDLIST, fp ) < 0)
349 val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
353 vlc_unlink( psz_idxTmp );
354 msg_Err( p_access, "Error moving LiveHttp index file" );
357 msg_Info( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
362 // Then take care of deletion
363 if ( p_sys->b_delsegs && i_firstseg > 1 )
365 char *psz_name = formatSegmentPath( p_access->psz_path, i_firstseg-1, true );
368 vlc_unlink( psz_name );
375 /*****************************************************************************
376 * closeCurrentSegment: Close the segment file
377 *****************************************************************************/
378 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
380 if ( p_sys->i_handle >= 0 )
382 close( p_sys->i_handle );
383 p_sys->i_handle = -1;
384 if ( p_sys->psz_cursegPath )
386 msg_Info( p_access, "LiveHttpSegmentComplete: %s (%"PRIu32")" , p_sys->psz_cursegPath, p_sys->i_segment );
387 free( p_sys->psz_cursegPath );
388 p_sys->psz_cursegPath = 0;
389 updateIndexAndDel( p_access, p_sys, b_isend );
394 /*****************************************************************************
395 * Close: close the target
396 *****************************************************************************/
397 static void Close( vlc_object_t * p_this )
399 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
400 sout_access_out_sys_t *p_sys = p_access->p_sys;
402 msg_Dbg( p_access, "Flushing buffer to last file");
403 while( p_sys->block_buffer )
405 ssize_t val = write( p_sys->i_handle, p_sys->block_buffer->p_buffer, p_sys->block_buffer->i_buffer );
408 if ( errno == EINTR )
410 block_ChainRelease ( p_sys->block_buffer);
413 if( !p_sys->block_buffer->p_next )
415 p_sys->p_seglens[p_sys->i_segment % p_sys->i_seglens ] =
416 (float)( p_sys->block_buffer->i_length / (1000000)) +
417 (float)(p_sys->block_buffer->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
420 if ( (size_t)val >= p_sys->block_buffer->i_buffer )
422 block_t *p_next = p_sys->block_buffer->p_next;
423 block_Release (p_sys->block_buffer);
424 p_sys->block_buffer = p_next;
428 p_sys->block_buffer->p_buffer += val;
429 p_sys->block_buffer->i_buffer -= val;
433 closeCurrentSegment( p_access, p_sys, true );
434 free( p_sys->psz_indexUrl );
435 free( p_sys->psz_indexPath );
436 free( p_sys->p_seglens );
439 msg_Dbg( p_access, "livehttp access output closed" );
442 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
444 sout_access_out_sys_t *p_sys = p_access->p_sys;
448 case ACCESS_OUT_CONTROLS_PACE:
450 bool *pb = va_arg( args, bool * );
451 *pb = !p_sys->b_ratecontrol;
462 /*****************************************************************************
463 * openNextFile: Open the segment file
464 *****************************************************************************/
465 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
469 uint32_t i_newseg = p_sys->i_segment + 1;
471 char *psz_seg = formatSegmentPath( p_access->psz_path, i_newseg, true );
475 fd = vlc_open( psz_seg, O_WRONLY | O_CREAT | O_LARGEFILE |
479 msg_Err( p_access, "cannot open `%s' (%m)", psz_seg );
484 msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , psz_seg, i_newseg );
487 p_sys->psz_cursegPath = psz_seg;
488 p_sys->i_handle = fd;
489 p_sys->i_segment = i_newseg;
493 /*****************************************************************************
494 * Write: standard write on a file descriptor.
495 *****************************************************************************/
496 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
499 sout_access_out_sys_t *p_sys = p_access->p_sys;
504 if ( ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_HEADER ) ) )
506 block_t *output = p_sys->block_buffer;
507 p_sys->block_buffer = NULL;
510 if( p_sys->i_handle > 0 &&
511 ( p_buffer->i_dts - p_sys->i_opendts +
512 p_buffer->i_length * CLOCK_FREQ / INT64_C(1000000)
513 ) >= p_sys->i_seglenm )
514 closeCurrentSegment( p_access, p_sys, false );
516 if ( p_sys->i_handle < 0 )
518 p_sys->i_opendts = output ? output->i_dts : p_buffer->i_dts;
519 if ( openNextFile( p_access, p_sys ) < 0 )
525 ssize_t val = write( p_sys->i_handle, output->p_buffer, output->i_buffer );
528 if ( errno == EINTR )
530 block_ChainRelease ( p_buffer );
533 p_sys->p_seglens[p_sys->i_segment % p_sys->i_seglens ] =
534 (float)output->i_length / INT64_C(1000000) +
535 (float)(output->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
537 if ( (size_t)val >= output->i_buffer )
539 block_t *p_next = output->p_next;
540 block_Release (output);
545 output->p_buffer += val;
546 output->i_buffer -= val;
552 p_temp = p_buffer->p_next;
553 p_buffer->p_next = NULL;
554 block_ChainAppend( &p_sys->block_buffer, p_buffer );
561 /*****************************************************************************
562 * Seek: seek to a specific location in a file
563 *****************************************************************************/
564 static int Seek( sout_access_out_t *p_access, off_t i_pos )
567 msg_Err( p_access, "livehttp sout access cannot seek" );