]> git.sesse.net Git - vlc/blob - modules/access_output/livehttp.c
Relicense livehttp output to LGPLv2.1+
[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
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_sout.h>
40 #include <vlc_block.h>
41 #include <vlc_fs.h>
42 #include <vlc_strings.h>
43 #include <vlc_charset.h>
44
45 #ifndef O_LARGEFILE
46 #   define O_LARGEFILE 0
47 #endif
48
49 #define STR_ENDLIST "#EXT-X-ENDLIST\n"
50
51 #define MAX_RENAME_RETRIES        10
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 static int  Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
58
59 #define SOUT_CFG_PREFIX "sout-livehttp-"
60 #define SEGLEN_TEXT N_("Segment length")
61 #define SEGLEN_LONGTEXT N_("Length of TS stream segments")
62
63 #define SPLITANYWHERE_TEXT N_("Split segments anywhere")
64 #define SPLITANYWHERE_LONGTEXT N_("Don't require a keyframe before splitting "\
65                                 "a segment. Needed for audio only.")
66
67 #define NUMSEGS_TEXT N_("Number of segments")
68 #define NUMSEGS_LONGTEXT N_("Number of segments to include in index")
69
70 #define INDEX_TEXT N_("Index file")
71 #define INDEX_LONGTEXT N_("Path to the index file to create")
72
73 #define INDEXURL_TEXT N_("Full URL to put in index file")
74 #define INDEXURL_LONGTEXT N_("Full URL to put in index file. "\
75                           "Use #'s to represent segment number")
76
77 #define DELSEGS_TEXT N_("Delete segments")
78 #define DELSEGS_LONGTEXT N_("Delete segments when they are no longer needed")
79
80 #define RATECONTROL_TEXT N_("Use muxers rate control mechanism")
81
82 vlc_module_begin ()
83     set_description( N_("HTTP Live streaming output") )
84     set_shortname( N_("LiveHTTP" ))
85     add_shortcut( "livehttp" )
86     set_capability( "sout access", 0 )
87     set_category( CAT_SOUT )
88     set_subcategory( SUBCAT_SOUT_ACO )
89     add_integer( SOUT_CFG_PREFIX "seglen", 10, SEGLEN_TEXT, SEGLEN_LONGTEXT, true )
90     add_integer( SOUT_CFG_PREFIX "numsegs", 0, NUMSEGS_TEXT, NUMSEGS_LONGTEXT, true )
91     add_bool( SOUT_CFG_PREFIX "splitanywhere", false,
92               SPLITANYWHERE_TEXT, SPLITANYWHERE_LONGTEXT, true )
93     add_bool( SOUT_CFG_PREFIX "delsegs", true,
94               DELSEGS_TEXT, DELSEGS_LONGTEXT, true )
95     add_bool( SOUT_CFG_PREFIX "ratecontrol", false,
96               RATECONTROL_TEXT, RATECONTROL_TEXT, true )
97     add_string( SOUT_CFG_PREFIX "index", NULL,
98                 INDEX_TEXT, INDEX_LONGTEXT, true )
99     add_string( SOUT_CFG_PREFIX "index-url", NULL,
100                 INDEXURL_TEXT, INDEXURL_LONGTEXT, true )
101     set_callbacks( Open, Close )
102 vlc_module_end ()
103
104
105 /*****************************************************************************
106  * Exported prototypes
107  *****************************************************************************/
108 static const char *const ppsz_sout_options[] = {
109     "seglen",
110     "splitanywhere",
111     "numsegs",
112     "delsegs",
113     "index",
114     "index-url",
115     "ratecontrol",
116     NULL
117 };
118
119 static ssize_t Write( sout_access_out_t *, block_t * );
120 static int Seek ( sout_access_out_t *, off_t  );
121 static int Control( sout_access_out_t *, int, va_list );
122
123 struct sout_access_out_sys_t
124 {
125     char *psz_cursegPath;
126     char *psz_indexPath;
127     char *psz_indexUrl;
128     mtime_t i_opendts;
129     mtime_t  i_seglenm;
130     uint32_t i_segment;
131     size_t  i_seglen;
132     int i_handle;
133     unsigned i_numsegs;
134     bool b_delsegs;
135     bool b_ratecontrol;
136     bool b_splitanywhere;
137 };
138
139 /*****************************************************************************
140  * Open: open the file
141  *****************************************************************************/
142 static int Open( vlc_object_t *p_this )
143 {
144     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
145     sout_access_out_sys_t *p_sys;
146     char *psz_idx;
147
148     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
149
150     if( !p_access->psz_path )
151     {
152         msg_Err( p_access, "no file name specified" );
153         return VLC_EGENERIC;
154     }
155
156     if( !( p_sys = malloc ( sizeof( *p_sys ) ) ) )
157         return VLC_ENOMEM;
158
159     p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
160     p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen;
161
162     p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
163     p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
164     p_sys->b_delsegs = var_GetBool( p_access, SOUT_CFG_PREFIX "delsegs" );
165     p_sys->b_ratecontrol = var_GetBool( p_access, SOUT_CFG_PREFIX "ratecontrol") ;
166
167     p_sys->psz_indexPath = NULL;
168     psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
169     if ( psz_idx )
170     {
171         char *psz_tmp;
172         psz_tmp = str_format( p_access, psz_idx );
173         free( psz_idx );
174         if ( !psz_tmp )
175         {
176             free( p_sys );
177             return VLC_ENOMEM;
178         }
179         path_sanitize( psz_tmp );
180         p_sys->psz_indexPath = psz_tmp;
181         vlc_unlink( p_sys->psz_indexPath );
182     }
183
184     p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
185
186     p_access->p_sys = p_sys;
187     p_sys->i_handle = -1;
188     p_sys->i_segment = 0;
189     p_sys->psz_cursegPath = NULL;
190
191     p_access->pf_write = Write;
192     p_access->pf_seek  = Seek;
193     p_access->pf_control = Control;
194
195     return VLC_SUCCESS;
196 }
197
198 #define SEG_NUMBER_PLACEHOLDER "#"
199 /*****************************************************************************
200  * formatSegmentPath: create segment path name based on seg #
201  *****************************************************************************/
202 static char *formatSegmentPath( sout_access_out_t *p_access, char *psz_path, uint32_t i_seg, bool b_sanitize )
203 {
204     char *psz_result;
205     char *psz_firstNumSign;
206
207     if ( ! ( psz_result  = str_format( p_access, psz_path ) ) )
208         return NULL;
209
210     psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
211     if ( *psz_firstNumSign ) {
212         char *psz_newResult;
213         int i_cnt = strspn( psz_firstNumSign, SEG_NUMBER_PLACEHOLDER );
214         int ret;
215
216         *psz_firstNumSign = '\0';
217         ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result, i_cnt, i_seg, psz_firstNumSign + i_cnt );
218         free ( psz_result );
219         if ( ret < 0 )
220             return NULL;
221         psz_result = psz_newResult;
222     }
223
224     if ( b_sanitize )
225         path_sanitize( psz_result );
226
227     return psz_result;
228 }
229
230 /************************************************************************
231  * updateIndexAndDel: If necessary, update index file & delete old segments
232  ************************************************************************/
233 static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
234 {
235
236     uint32_t i_firstseg;
237
238     if ( p_sys->i_numsegs == 0 || p_sys->i_segment < p_sys->i_numsegs )
239         i_firstseg = 1;
240     else
241         i_firstseg = ( p_sys->i_segment - p_sys->i_numsegs ) + 1;
242
243     // First update index
244     if ( p_sys->psz_indexPath )
245     {
246         int val;
247         FILE *fp;
248         char *psz_idxTmp;
249         if ( asprintf( &psz_idxTmp, "%s.tmp", p_sys->psz_indexPath ) < 0)
250             return -1;
251
252         fp = vlc_fopen( psz_idxTmp, "wt");
253         if ( !fp )
254         {
255             msg_Err( p_access, "cannot open index file `%s'", psz_idxTmp );
256             free( psz_idxTmp );
257             return -1;
258         }
259
260         if ( fprintf( fp, "#EXTM3U\n#EXT-X-TARGETDURATION:%zu\n#EXT-X-MEDIA-SEQUENCE:%"PRIu32"\n", p_sys->i_seglen, i_firstseg ) < 0 )
261         {
262             free( psz_idxTmp );
263             fclose( fp );
264             return -1;
265         }
266
267         char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
268         for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
269         {
270             char *psz_name;
271             if ( ! ( psz_name = formatSegmentPath( p_access, psz_idxFormat, i, false ) ) )
272             {
273                 free( psz_idxTmp );
274                 fclose( fp );
275                 return -1;
276             }
277             val = fprintf( fp, "#EXTINF:%zu,\n%s\n", p_sys->i_seglen, psz_name );
278             free( psz_name );
279             if ( val < 0 )
280             {
281                 free( psz_idxTmp );
282                 fclose( fp );
283                 return -1;
284             }
285         }
286
287         if ( b_isend )
288         {
289             if ( fputs ( STR_ENDLIST, fp ) < 0)
290             {
291                 free( psz_idxTmp );
292                 fclose( fp ) ;
293                 return -1;
294             }
295
296         }
297         fclose( fp );
298
299         val = vlc_rename ( psz_idxTmp, p_sys->psz_indexPath);
300
301         if ( val < 0 )
302         {
303             vlc_unlink( psz_idxTmp );
304             msg_Err( p_access, "Error moving LiveHttp index file" );
305         }
306         else
307             msg_Info( p_access, "LiveHttpIndexComplete: %s" , p_sys->psz_indexPath );
308
309         free( psz_idxTmp );
310     }
311
312     // Then take care of deletion
313     if ( p_sys->b_delsegs && i_firstseg > 1 )
314     {
315         char *psz_name = formatSegmentPath( p_access, p_access->psz_path, i_firstseg-1, true );
316          if ( psz_name )
317          {
318              vlc_unlink( psz_name );
319              free( psz_name );
320          }
321     }
322     return 0;
323 }
324
325 /*****************************************************************************
326  * closeCurrentSegment: Close the segment file
327  *****************************************************************************/
328 static void closeCurrentSegment( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys, bool b_isend )
329 {
330     if ( p_sys->i_handle >= 0 )
331     {
332         close( p_sys->i_handle );
333         p_sys->i_handle = -1;
334         if ( p_sys->psz_cursegPath )
335         {
336             msg_Info( p_access, "LiveHttpSegmentComplete: %s (%"PRIu32")" , p_sys->psz_cursegPath, p_sys->i_segment );
337             free( p_sys->psz_cursegPath );
338             p_sys->psz_cursegPath = 0;
339             updateIndexAndDel( p_access, p_sys, b_isend );
340         }
341     }
342 }
343
344 /*****************************************************************************
345  * Close: close the target
346  *****************************************************************************/
347 static void Close( vlc_object_t * p_this )
348 {
349     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
350     sout_access_out_sys_t *p_sys = p_access->p_sys;
351
352
353     closeCurrentSegment( p_access, p_sys, true );
354     free( p_sys->psz_indexUrl );
355     free( p_sys->psz_indexPath );
356     free( p_sys );
357
358     msg_Dbg( p_access, "livehttp access output closed" );
359 }
360
361 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
362 {
363     sout_access_out_sys_t *p_sys = p_access->p_sys;
364
365     switch( i_query )
366     {
367         case ACCESS_OUT_CONTROLS_PACE:
368         {
369             bool *pb = va_arg( args, bool * );
370             *pb = !p_sys->b_ratecontrol;
371             //*pb = true;
372             break;
373         }
374
375         default:
376             return VLC_EGENERIC;
377     }
378     return VLC_SUCCESS;
379 }
380
381 /*****************************************************************************
382  * openNextFile: Open the segment file
383  *****************************************************************************/
384 static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t *p_sys )
385 {
386     int fd;
387
388     uint32_t i_newseg = p_sys->i_segment + 1;
389
390     char *psz_seg = formatSegmentPath( p_access, p_access->psz_path, i_newseg, true );
391     if ( !psz_seg )
392         return -1;
393
394     fd = vlc_open( psz_seg, O_WRONLY | O_CREAT | O_LARGEFILE |
395                      O_TRUNC, 0666 );
396     if ( fd == -1 )
397     {
398         msg_Err( p_access, "cannot open `%s' (%m)", psz_seg );
399         free( psz_seg );
400         return -1;
401     }
402
403     msg_Dbg( p_access, "Successfully opened livehttp file: %s (%"PRIu32")" , psz_seg, i_newseg );
404
405     //free( psz_seg );
406     p_sys->psz_cursegPath = psz_seg;
407     p_sys->i_handle = fd;
408     p_sys->i_segment = i_newseg;
409     return fd;
410 }
411
412 /*****************************************************************************
413  * Write: standard write on a file descriptor.
414  *****************************************************************************/
415 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
416 {
417     size_t i_write = 0;
418     sout_access_out_sys_t *p_sys = p_access->p_sys;
419
420     while( p_buffer )
421     {
422         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 )
423         {
424             closeCurrentSegment( p_access, p_sys, false );
425         }
426         if ( p_buffer->i_buffer > 0 && p_sys->i_handle < 0 )
427         {
428             p_sys->i_opendts = p_buffer->i_dts;
429             if ( openNextFile( p_access, p_sys ) < 0 )
430                 return -1;
431         }
432         ssize_t val = write ( p_sys->i_handle,
433                              p_buffer->p_buffer, p_buffer->i_buffer );
434         if ( val == -1 )
435         {
436             if ( errno == EINTR )
437                 continue;
438             block_ChainRelease ( p_buffer );
439             return -1;
440         }
441
442         if ( (size_t)val >= p_buffer->i_buffer )
443         {
444             block_t *p_next = p_buffer->p_next;
445             block_Release (p_buffer);
446             p_buffer = p_next;
447         }
448         else
449         {
450             p_buffer->p_buffer += val;
451             p_buffer->i_buffer -= val;
452         }
453         i_write += val;
454     }
455     return i_write;
456 }
457
458 /*****************************************************************************
459  * Seek: seek to a specific location in a file
460  *****************************************************************************/
461 static int Seek( sout_access_out_t *p_access, off_t i_pos )
462 {
463     (void) i_pos;
464     msg_Err( p_access, "livehttp sout access cannot seek" );
465     return -1;
466 }