1 /*****************************************************************************
2 * livehttp.c: Live HTTP Streaming
3 *****************************************************************************
4 * Copyright © 2001, 2002 VLC authors and VideoLAN
5 * Copyright © 2009-2010 by Keary Griffin
7 * Authors: Keary Griffin <kearygriffin at gmail.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <sys/types.h>
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
43 #include <vlc_block.h>
45 #include <vlc_strings.h>
46 #include <vlc_charset.h>
49 # define O_LARGEFILE 0
52 #define STR_ENDLIST "#EXT-X-ENDLIST\n"
54 #define MAX_RENAME_RETRIES 10
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
62 #define SOUT_CFG_PREFIX "sout-livehttp-"
63 #define SEGLEN_TEXT N_("Segment length")
64 #define SEGLEN_LONGTEXT N_("Length of TS stream segments")
66 #define SPLITANYWHERE_TEXT N_("Split segments anywhere")
67 #define SPLITANYWHERE_LONGTEXT N_("Don't require a keyframe before splitting "\
68 "a segment. Needed for audio only.")
70 #define NUMSEGS_TEXT N_("Number of segments")
71 #define NUMSEGS_LONGTEXT N_("Number of segments to include in index")
73 #define INDEX_TEXT N_("Index file")
74 #define INDEX_LONGTEXT N_("Path to the index file to create")
76 #define INDEXURL_TEXT N_("Full URL to put in index file")
77 #define INDEXURL_LONGTEXT N_("Full URL to put in index file. "\
78 "Use #'s to represent segment number")
80 #define DELSEGS_TEXT N_("Delete segments")
81 #define DELSEGS_LONGTEXT N_("Delete segments when they are no longer needed")
83 #define RATECONTROL_TEXT N_("Use muxers rate control mechanism")
86 set_description( N_("HTTP Live streaming output") )
87 set_shortname( N_("LiveHTTP" ))
88 add_shortcut( "livehttp" )
89 set_capability( "sout access", 0 )
90 set_category( CAT_SOUT )
91 set_subcategory( SUBCAT_SOUT_ACO )
92 add_integer( SOUT_CFG_PREFIX "seglen", 10, SEGLEN_TEXT, SEGLEN_LONGTEXT, true )
93 add_integer( SOUT_CFG_PREFIX "numsegs", 0, NUMSEGS_TEXT, NUMSEGS_LONGTEXT, true )
94 add_bool( SOUT_CFG_PREFIX "splitanywhere", false,
95 SPLITANYWHERE_TEXT, SPLITANYWHERE_LONGTEXT, true )
96 add_bool( SOUT_CFG_PREFIX "delsegs", true,
97 DELSEGS_TEXT, DELSEGS_LONGTEXT, true )
98 add_bool( SOUT_CFG_PREFIX "ratecontrol", false,
99 RATECONTROL_TEXT, RATECONTROL_TEXT, true )
100 add_string( SOUT_CFG_PREFIX "index", NULL,
101 INDEX_TEXT, INDEX_LONGTEXT, true )
102 add_string( SOUT_CFG_PREFIX "index-url", NULL,
103 INDEXURL_TEXT, INDEXURL_LONGTEXT, true )
104 set_callbacks( Open, Close )
108 /*****************************************************************************
109 * Exported prototypes
110 *****************************************************************************/
111 static const char *const ppsz_sout_options[] = {
122 static ssize_t Write( sout_access_out_t *, block_t * );
123 static int Seek ( sout_access_out_t *, off_t );
124 static int Control( sout_access_out_t *, int, va_list );
126 struct sout_access_out_sys_t
128 char *psz_cursegPath;
139 bool b_splitanywhere;
142 /*****************************************************************************
143 * Open: open the file
144 *****************************************************************************/
145 static int Open( vlc_object_t *p_this )
147 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
148 sout_access_out_sys_t *p_sys;
151 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
153 if( !p_access->psz_path )
155 msg_Err( p_access, "no file name specified" );
159 if( !( p_sys = malloc ( sizeof( *p_sys ) ) ) )
162 p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
163 p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen;
165 p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
166 p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
167 p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
168 p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
170 p_sys->psz_indexPath = NULL;
171 psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
175 psz_tmp = str_format( p_access, psz_idx );
182 path_sanitize( psz_tmp );
183 p_sys->psz_indexPath = psz_tmp;
184 vlc_unlink( p_sys->psz_indexPath );
187 p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
189 p_access->p_sys = p_sys;
190 p_sys->i_handle = -1;
191 p_sys->i_segment = 0;
192 p_sys->psz_cursegPath = NULL;
194 p_access->pf_write = Write;
195 p_access->pf_seek = Seek;
196 p_access->pf_control = Control;
201 #define SEG_NUMBER_PLACEHOLDER "#"
202 /*****************************************************************************
203 * formatSegmentPath: create segment path name based on seg #
204 *****************************************************************************/
205 static char *formatSegmentPath( sout_access_out_t *p_access, char *psz_path, uint32_t i_seg, bool b_sanitize )
208 char *psz_firstNumSign;
210 if ( ! ( psz_result = str_format( p_access, psz_path ) ) )
213 psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
214 if ( *psz_firstNumSign ) {
216 int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
219 *psz_firstNumSign = '\0';
220 ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
224 psz_result = psz_newResult;
228 path_sanitize( psz_result );
233 /************************************************************************
234 * updateIndexAndDel: If necessary, update index file & delete old segments
235 ************************************************************************/
236 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
241 if ( p_sys->i_numsegs == 0 || p_sys->i_segment < p_sys->i_numsegs )
244 i_firstseg = ( p_sys->i_segment - p_sys->i_numsegs ) + 1;
246 // First update index
247 if ( p_sys->psz_indexPath )
252 if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
255 fp = vlc_fopen( psz_idxTmp, "wt");
258 msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
263 if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%zu\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n", p_sys->i_seglen, i_firstseg ) < 0 )
270 char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
271 for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
274 if ( ! ( psz_name = formatSegmentPath( p_access, psz_idxFormat, i, false ) ) )
280 val = fprintf( fp, "#EXTINF:%zu,\n%s\n", p_sys->i_seglen, psz_name );
292 if ( fputs ( STR_ENDLIST, fp ) < 0)
302 val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
306 vlc_unlink( psz_idxTmp );
307 msg_Err( p_access, "Error moving LiveHttp index file" );
310 msg_Info( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
315 // Then take care of deletion
316 if ( p_sys->b_delsegs && i_firstseg > 1 )
318 char *psz_name = formatSegmentPath( p_access, p_access->psz_path, i_firstseg-1, true );
321 vlc_unlink( psz_name );
328 /*****************************************************************************
329 * closeCurrentSegment: Close the segment file
330 *****************************************************************************/
331 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
333 if ( p_sys->i_handle >= 0 )
335 close( p_sys->i_handle );
336 p_sys->i_handle = -1;
337 if ( p_sys->psz_cursegPath )
339 msg_Info( p_access, "LiveHttpSegmentComplete: %s (%"PRIu32")" , p_sys->psz_cursegPath, p_sys->i_segment );
340 free( p_sys->psz_cursegPath );
341 p_sys->psz_cursegPath = 0;
342 updateIndexAndDel( p_access, p_sys, b_isend );
347 /*****************************************************************************
348 * Close: close the target
349 *****************************************************************************/
350 static void Close( vlc_object_t * p_this )
352 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
353 sout_access_out_sys_t *p_sys = p_access->p_sys;
356 closeCurrentSegment( p_access, p_sys, true );
357 free( p_sys->psz_indexUrl );
358 free( p_sys->psz_indexPath );
361 msg_Dbg( p_access, "livehttp access output closed" );
364 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
366 sout_access_out_sys_t *p_sys = p_access->p_sys;
370 case ACCESS_OUT_CONTROLS_PACE:
372 bool *pb = va_arg( args, bool * );
373 *pb = !p_sys->b_ratecontrol;
384 /*****************************************************************************
385 * openNextFile: Open the segment file
386 *****************************************************************************/
387 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
391 uint32_t i_newseg = p_sys->i_segment + 1;
393 char *psz_seg = formatSegmentPath( p_access, p_access->psz_path, i_newseg, true );
397 fd = vlc_open( psz_seg, O_WRONLY | O_CREAT | O_LARGEFILE |
401 msg_Err( p_access, "cannot open `%s' (%m)", psz_seg );
406 msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , psz_seg, i_newseg );
409 p_sys->psz_cursegPath = psz_seg;
410 p_sys->i_handle = fd;
411 p_sys->i_segment = i_newseg;
415 /*****************************************************************************
416 * Write: standard write on a file descriptor.
417 *****************************************************************************/
418 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
421 sout_access_out_sys_t *p_sys = p_access->p_sys;
425 if ( p_sys->i_handle >= 0 && ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) ) && ( p_buffer->i_dts-p_sys->i_opendts ) > p_sys->i_seglenm )
427 closeCurrentSegment( p_access, p_sys, false );
429 if ( p_buffer->i_buffer > 0 && p_sys->i_handle < 0 )
431 p_sys->i_opendts = p_buffer->i_dts;
432 if ( openNextFile( p_access, p_sys ) < 0 )
435 ssize_t val = write ( p_sys->i_handle,
436 p_buffer->p_buffer, p_buffer->i_buffer );
439 if ( errno == EINTR )
441 block_ChainRelease ( p_buffer );
445 if ( (size_t)val >= p_buffer->i_buffer )
447 block_t *p_next = p_buffer->p_next;
448 block_Release (p_buffer);
453 p_buffer->p_buffer += val;
454 p_buffer->i_buffer -= val;
461 /*****************************************************************************
462 * Seek: seek to a specific location in a file
463 *****************************************************************************/
464 static int Seek( sout_access_out_t *p_access, off_t i_pos )
467 msg_Err( p_access, "livehttp sout access cannot seek" );