]> git.sesse.net Git - vlc/blob - modules/access_output/livehttp.c
livehttp: give more exact segment duration in extinf
[vlc] / modules / access_output / livehttp.c
1 /*****************************************************************************
2  * livehttp.c: Live HTTP Streaming
3  *****************************************************************************
4  * Copyright © 2001, 2002 VLC authors and VideoLAN
5  * Copyright © 2009-2010 by Keary Griffin
6  *
7  * Authors: Keary Griffin <kearygriffin at gmail.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <sys/types.h>
33 #include <time.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_sout.h>
43 #include <vlc_block.h>
44 #include <vlc_fs.h>
45 #include <vlc_strings.h>
46 #include <vlc_charset.h>
47
48 #ifndef O_LARGEFILE
49 #   define O_LARGEFILE 0
50 #endif
51
52 #define STR_ENDLIST "#EXT-X-ENDLIST\n"
53
54 #define MAX_RENAME_RETRIES        10
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 static int  Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
61
62 #define SOUT_CFG_PREFIX "sout-livehttp-"
63 #define SEGLEN_TEXT N_("Segment length")
64 #define SEGLEN_LONGTEXT N_("Length of TS stream segments")
65
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.")
69
70 #define NUMSEGS_TEXT N_("Number of segments")
71 #define NUMSEGS_LONGTEXT N_("Number of segments to include in index")
72
73 #define INDEX_TEXT N_("Index file")
74 #define INDEX_LONGTEXT N_("Path to the index file to create")
75
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")
79
80 #define DELSEGS_TEXT N_("Delete segments")
81 #define DELSEGS_LONGTEXT N_("Delete segments when they are no longer needed")
82
83 #define RATECONTROL_TEXT N_("Use muxers rate control mechanism")
84
85 vlc_module_begin ()
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 )
105 vlc_module_end ()
106
107
108 /*****************************************************************************
109  * Exported prototypes
110  *****************************************************************************/
111 static const char *const ppsz_sout_options[] = {
112     "seglen",
113     "splitanywhere",
114     "numsegs",
115     "delsegs",
116     "index",
117     "index-url",
118     "ratecontrol",
119     NULL
120 };
121
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 );
125
126 struct sout_access_out_sys_t
127 {
128     char *psz_cursegPath;
129     char *psz_indexPath;
130     char *psz_indexUrl;
131     mtime_t i_opendts;
132     mtime_t  i_seglenm;
133     uint32_t i_segment;
134     size_t  i_seglen;
135     float   *p_seglens;
136     block_t *block_buffer;
137     int i_handle;
138     unsigned i_numsegs;
139     unsigned i_seglens;
140     bool b_delsegs;
141     bool b_ratecontrol;
142     bool b_splitanywhere;
143 };
144
145 /*****************************************************************************
146  * Open: open the file
147  *****************************************************************************/
148 static int Open( vlc_object_t *p_this )
149 {
150     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
151     sout_access_out_sys_t *p_sys;
152     char *psz_idx;
153
154     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
155
156     if( !p_access->psz_path )
157     {
158         msg_Err( p_access, "no file name specified" );
159         return VLC_EGENERIC;
160     }
161
162     if( unlikely( !( p_sys = malloc ( sizeof( *p_sys ) ) ) ) )
163         return VLC_ENOMEM;
164
165     p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
166     /* Try to get within +-10% of asked segment length, so limit is +10% of segment length*/
167     p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen * 1.10;
168     p_sys->block_buffer = NULL;
169
170     p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
171     p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
172     p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
173     p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
174
175
176     /* 5 elements is from harrison-stetson algorithm to start from some number
177      * if we don't have numsegs defined
178      */
179     p_sys->i_seglens = 5;
180     if( p_sys->i_numsegs )
181         p_sys->i_seglens = p_sys->i_numsegs+1;
182     p_sys->p_seglens = malloc( sizeof(float) * p_sys->i_seglens  );
183     if( unlikely( !p_sys->p_seglens ) )
184     {
185         free( p_sys );
186         return VLC_ENOMEM;
187     }
188
189     p_sys->psz_indexPath = NULL;
190     psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
191     if ( psz_idx )
192     {
193         char *psz_tmp;
194         psz_tmp = str_format_time( psz_idx );
195         free( psz_idx );
196         if ( !psz_tmp )
197         {
198             free( p_sys );
199             return VLC_ENOMEM;
200         }
201         path_sanitize( psz_tmp );
202         p_sys->psz_indexPath = psz_tmp;
203         vlc_unlink( p_sys->psz_indexPath );
204     }
205
206     p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
207
208     p_access->p_sys = p_sys;
209     p_sys->i_handle = -1;
210     p_sys->i_segment = 0;
211     p_sys->psz_cursegPath = NULL;
212
213     p_access->pf_write = Write;
214     p_access->pf_seek  = Seek;
215     p_access->pf_control = Control;
216
217     return VLC_SUCCESS;
218 }
219
220 #define SEG_NUMBER_PLACEHOLDER "#"
221 /*****************************************************************************
222  * formatSegmentPath: create segment path name based on seg #
223  *****************************************************************************/
224 static char *formatSegmentPath( char *psz_path, uint32_t i_seg, bool b_sanitize )
225 {
226     char *psz_result;
227     char *psz_firstNumSign;
228
229     if ( ! ( psz_result  = str_format_time( psz_path ) ) )
230         return NULL;
231
232     psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
233     if ( *psz_firstNumSign ) {
234         char *psz_newResult;
235         int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
236         int ret;
237
238         *psz_firstNumSign = '\0';
239         ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
240         free ( psz_result );
241         if ( ret < 0 )
242             return NULL;
243         psz_result = psz_newResult;
244     }
245
246     if ( b_sanitize )
247         path_sanitize( psz_result );
248
249     return psz_result;
250 }
251
252 /************************************************************************
253  * updateIndexAndDel: If necessary, update index file & delete old segments
254  ************************************************************************/
255 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
256 {
257
258     uint32_t i_firstseg;
259
260     if ( p_sys->i_numsegs == 0 || p_sys->i_segment < p_sys->i_numsegs )
261     {
262         i_firstseg = 1;
263
264         if( p_sys->i_segment >= (p_sys->i_seglens-1) )
265         {
266             p_sys->i_seglens <<= 1;
267             msg_Dbg( p_access, "Segment amount %u", p_sys->i_seglens );
268             p_sys->p_seglens = realloc( p_sys->p_seglens, sizeof(float) * p_sys->i_seglens );
269             if( unlikely( !p_sys->p_seglens ) )
270              return -1;
271         }
272     }
273     else
274         i_firstseg = ( p_sys->i_segment - p_sys->i_numsegs ) + 1;
275
276     // First update index
277     if ( p_sys->psz_indexPath )
278     {
279         int val;
280         FILE *fp;
281         char *psz_idxTmp;
282         if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
283             return -1;
284
285         fp = vlc_fopen( psz_idxTmp, "wt");
286         if ( !fp )
287         {
288             msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
289             free( psz_idxTmp );
290             return -1;
291         }
292
293         if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%zu\n#EXT-X-VERSION:3\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n", p_sys->i_seglen, i_firstseg ) < 0 )
294         {
295             free( psz_idxTmp );
296             fclose( fp );
297             return -1;
298         }
299
300         char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
301         for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
302         {
303             char *psz_name;
304             char *psz_duration = NULL;
305             if ( ! ( psz_name = formatSegmentPath( psz_idxFormat, i, false ) ) )
306             {
307                 free( psz_idxTmp );
308                 fclose( fp );
309                 return -1;
310             }
311             if( ! ( us_asprintf( &psz_duration, "%.2f", p_sys->p_seglens[i % p_sys->i_seglens ], psz_name ) ) )
312             {
313                 free( psz_idxTmp );
314                 fclose( fp );
315                 return -1;
316             }
317             val = fprintf( fp, "#EXTINF:%s,\n%s\n", psz_duration, psz_name );
318             free( psz_duration );
319             free( psz_name );
320             if ( val < 0 )
321             {
322                 free( psz_idxTmp );
323                 fclose( fp );
324                 return -1;
325             }
326         }
327
328         if ( b_isend )
329         {
330             if ( fputs ( STR_ENDLIST, fp ) < 0)
331             {
332                 free( psz_idxTmp );
333                 fclose( fp ) ;
334                 return -1;
335             }
336
337         }
338         fclose( fp );
339
340         val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
341
342         if ( val < 0 )
343         {
344             vlc_unlink( psz_idxTmp );
345             msg_Err( p_access, "Error moving LiveHttp index file" );
346         }
347         else
348             msg_Info( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
349
350         free( psz_idxTmp );
351     }
352
353     // Then take care of deletion
354     if ( p_sys->b_delsegs && i_firstseg > 1 )
355     {
356         char *psz_name = formatSegmentPath( p_access->psz_path, i_firstseg-1, true );
357          if ( psz_name )
358          {
359              vlc_unlink( psz_name );
360              free( psz_name );
361          }
362     }
363     return 0;
364 }
365
366 /*****************************************************************************
367  * closeCurrentSegment: Close the segment file
368  *****************************************************************************/
369 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
370 {
371     if ( p_sys->i_handle >= 0 )
372     {
373         close( p_sys->i_handle );
374         p_sys->i_handle = -1;
375         if ( p_sys->psz_cursegPath )
376         {
377             msg_Info( p_access, "LiveHttpSegmentComplete: %s (%"PRIu32")" , p_sys->psz_cursegPath, p_sys->i_segment );
378             free( p_sys->psz_cursegPath );
379             p_sys->psz_cursegPath = 0;
380             updateIndexAndDel( p_access, p_sys, b_isend );
381         }
382     }
383 }
384
385 /*****************************************************************************
386  * Close: close the target
387  *****************************************************************************/
388 static void Close( vlc_object_t * p_this )
389 {
390     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
391     sout_access_out_sys_t *p_sys = p_access->p_sys;
392
393     msg_Dbg( p_access, "Flushing buffer to last file");
394     while( p_sys->block_buffer )
395     {
396         ssize_t val = write( p_sys->i_handle, p_sys->block_buffer->p_buffer, p_sys->block_buffer->i_buffer );
397         if ( val == -1 )
398         {
399            if ( errno == EINTR )
400               continue;
401            block_ChainRelease ( p_sys->block_buffer);
402            break;
403         }
404         if( !p_sys->block_buffer->p_next )
405         {
406             p_sys->p_seglens[p_sys->i_segment % p_sys->i_seglens ] =
407                 (float)( p_sys->block_buffer->i_length / (1000000)) +
408                 (float)(p_sys->block_buffer->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
409         }
410
411         if ( (size_t)val >= p_sys->block_buffer->i_buffer )
412         {
413            block_t *p_next = p_sys->block_buffer->p_next;
414            block_Release (p_sys->block_buffer);
415            p_sys->block_buffer = p_next;
416         }
417         else
418         {
419            p_sys->block_buffer->p_buffer += val;
420            p_sys->block_buffer->i_buffer -= val;
421         }
422     }
423
424     closeCurrentSegment( p_access, p_sys, true );
425     free( p_sys->psz_indexUrl );
426     free( p_sys->psz_indexPath );
427     free( p_sys->p_seglens );
428     free( p_sys );
429
430     msg_Dbg( p_access, "livehttp access output closed" );
431 }
432
433 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
434 {
435     sout_access_out_sys_t *p_sys = p_access->p_sys;
436
437     switch( i_query )
438     {
439         case ACCESS_OUT_CONTROLS_PACE:
440         {
441             bool *pb = va_arg( args, bool * );
442             *pb = !p_sys->b_ratecontrol;
443             //*pb = true;
444             break;
445         }
446
447         default:
448             return VLC_EGENERIC;
449     }
450     return VLC_SUCCESS;
451 }
452
453 /*****************************************************************************
454  * openNextFile: Open the segment file
455  *****************************************************************************/
456 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
457 {
458     int fd;
459
460     uint32_t i_newseg = p_sys->i_segment + 1;
461
462     char *psz_seg = formatSegmentPath( p_access->psz_path, i_newseg, true );
463     if ( !psz_seg )
464         return -1;
465
466     fd = vlc_open( psz_seg, O_WRONLY | O_CREAT | O_LARGEFILE |
467                      O_TRUNC, 0666 );
468     if ( fd == -1 )
469     {
470         msg_Err( p_access, "cannot open `%s' (%m)", psz_seg );
471         free( psz_seg );
472         return -1;
473     }
474
475     msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , psz_seg, i_newseg );
476
477     //free( psz_seg );
478     p_sys->psz_cursegPath = psz_seg;
479     p_sys->i_handle = fd;
480     p_sys->i_segment = i_newseg;
481     return fd;
482 }
483
484 /*****************************************************************************
485  * Write: standard write on a file descriptor.
486  *****************************************************************************/
487 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
488 {
489     size_t i_write = 0;
490     sout_access_out_sys_t *p_sys = p_access->p_sys;
491     block_t *p_temp;
492
493     while( p_buffer )
494     {
495         if ( ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_HEADER ) ) )
496         {
497             block_t *output = p_sys->block_buffer;
498             p_sys->block_buffer = NULL;
499
500
501             if( p_sys->i_handle > 0 &&
502                 ( p_buffer->i_dts - p_sys->i_opendts +
503                   p_buffer->i_length * CLOCK_FREQ / INT64_C(1000000)
504                 ) >= p_sys->i_seglenm )
505                 closeCurrentSegment( p_access, p_sys, false );
506
507             if ( p_sys->i_handle < 0 )
508             {
509                 p_sys->i_opendts = output ? output->i_dts : p_buffer->i_dts;
510                 if ( openNextFile( p_access, p_sys ) < 0 )
511                    return -1;
512             }
513
514             while( output )
515             {
516                 ssize_t val = write( p_sys->i_handle, output->p_buffer, output->i_buffer );
517                 if ( val == -1 )
518                 {
519                    if ( errno == EINTR )
520                       continue;
521                    block_ChainRelease ( p_buffer );
522                    return -1;
523                 }
524                 p_sys->p_seglens[p_sys->i_segment % p_sys->i_seglens ] =
525                     (float)output->i_length / INT64_C(1000000) +
526                     (float)(output->i_dts - p_sys->i_opendts) / CLOCK_FREQ;
527
528                 if ( (size_t)val >= output->i_buffer )
529                 {
530                    block_t *p_next = output->p_next;
531                    block_Release (output);
532                    output = p_next;
533                 }
534                 else
535                 {
536                    output->p_buffer += val;
537                    output->i_buffer -= val;
538                 }
539                 i_write += val;
540             }
541         }
542
543         p_temp = p_buffer->p_next;
544         p_buffer->p_next = NULL;
545         block_ChainAppend( &p_sys->block_buffer, p_buffer );
546         p_buffer = p_temp;
547     }
548
549     return i_write;
550 }
551
552 /*****************************************************************************
553  * Seek: seek to a specific location in a file
554  *****************************************************************************/
555 static int Seek( sout_access_out_t *p_access, off_t i_pos )
556 {
557     (void) i_pos;
558     msg_Err( p_access, "livehttp sout access cannot seek" );
559     return -1;
560 }