]> git.sesse.net Git - vlc/blob - modules/access_filter/timeshift.c
macosx: Make sure empty nodes are shown as node.
[vlc] / modules / access_filter / timeshift.c
1 /*****************************************************************************
2  * timeshift.c: access filter implementing timeshifting capabilities
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34
35 #include <errno.h>
36
37 #include <vlc_access.h>
38 #include <vlc_charset.h>
39 #include <vlc_input.h>
40
41 #include <unistd.h>
42
43 #ifdef WIN32
44 #  include <direct.h>                                        /* _wgetcwd  */
45 #endif
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 static int  Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
52
53 #define GRANULARITY_TEXT N_("Timeshift granularity")
54 /// \bug [String] typo
55 #define GRANULARITY_LONGTEXT N_( "This is the size of the temporary files " \
56   "that will be used to store the timeshifted streams." )
57 #define DIR_TEXT N_("Timeshift directory")
58 #define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \
59   "files." )
60 #define FORCE_TEXT N_("Force use of the timeshift module")
61 #define FORCE_LONGTEXT N_("Force use of the timeshift module even if the " \
62   "access declares that it can control pace or pause." )
63
64 vlc_module_begin();
65     set_shortname( N_("Timeshift") );
66     set_description( N_("Timeshift") );
67     set_category( CAT_INPUT );
68     set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
69     set_capability( "access_filter", 0 );
70     add_shortcut( "timeshift" );
71     set_callbacks( Open, Close );
72
73     add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
74                  GRANULARITY_LONGTEXT, true );
75     add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, false );
76         change_unsafe();
77     add_bool( "timeshift-force", false, NULL, FORCE_TEXT, FORCE_LONGTEXT,
78               false );
79 vlc_module_end();
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84
85 static int      Seek( access_t *, int64_t );
86 static block_t *Block  ( access_t *p_access );
87 static int      Control( access_t *, int i_query, va_list args );
88 static void     Thread ( access_t *p_access );
89 static int      WriteBlockToFile( access_t *p_access, block_t *p_block );
90 static block_t *ReadBlockFromFile( access_t *p_access );
91 static char    *GetTmpFilePath( access_t *p_access );
92
93 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
94 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
95 #define TMP_FILE_MAX 256
96
97 typedef struct ts_entry_t
98 {
99     FILE *file;
100     struct ts_entry_t *p_next;
101
102 } ts_entry_t;
103
104 struct access_sys_t
105 {
106     block_fifo_t *p_fifo;
107
108     int  i_files;
109     int  i_file_size;
110     int  i_write_size;
111
112     ts_entry_t *p_read_list;
113     ts_entry_t **pp_read_last;
114     ts_entry_t *p_write_list;
115     ts_entry_t **pp_write_last;
116
117     char *psz_filename_base;
118     char *psz_filename;
119
120     int64_t i_data;
121 };
122
123 /*****************************************************************************
124  * Open:
125  *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
127 {
128     access_t *p_access = (access_t*)p_this;
129     access_t *p_src = p_access->p_source;
130     access_sys_t *p_sys;
131     bool b_bool;
132
133     var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
134     if( var_GetBool( p_access, "timeshift-force" ) )
135     {
136         msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
137     }
138     else
139     {
140         /* Only work with not pace controled access */
141         if( access_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
142             b_bool )
143         {
144             msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
145             return VLC_EGENERIC;
146         }
147         /* Refuse access that can be paused */
148         if( access_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
149         {
150             msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
151             return VLC_EGENERIC;
152         }
153     }
154
155     /* */
156     p_access->pf_read = NULL;
157     p_access->pf_block = Block;
158     p_access->pf_seek = Seek;
159     p_access->pf_control = Control;
160     p_access->info = p_src->info;
161
162     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
163
164     /* */
165     p_sys->p_fifo = block_FifoNew();
166     p_sys->i_write_size = 0;
167     p_sys->i_files = 0;
168     p_sys->i_data = 0;
169
170     p_sys->p_read_list = NULL;
171     p_sys->pp_read_last = &p_sys->p_read_list;
172     p_sys->p_write_list = NULL;
173     p_sys->pp_write_last = &p_sys->p_write_list;
174
175     var_Create( p_access, "timeshift-dir",
176                 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
177     var_Create( p_access, "timeshift-granularity",
178                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179     p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
180     if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
181     p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
182
183     p_sys->psz_filename_base = GetTmpFilePath( p_access );
184     p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
185
186     if( vlc_thread_create( p_access, "timeshift thread", Thread,
187                            VLC_THREAD_PRIORITY_LOW, false ) )
188     {
189         msg_Err( p_access, "cannot spawn timeshift access thread" );
190         return VLC_EGENERIC;
191     }
192
193     return VLC_SUCCESS;
194 }
195
196 /*****************************************************************************
197  * Close:
198  *****************************************************************************/
199 static void Close( vlc_object_t *p_this )
200 {
201     access_t     *p_access = (access_t*)p_this;
202     access_sys_t *p_sys = p_access->p_sys;
203     ts_entry_t *p_entry;
204     int i;
205
206     msg_Dbg( p_access, "timeshift close called" );
207     vlc_thread_join( p_access );
208
209     for( p_entry = p_sys->p_write_list; p_entry; )
210     {
211         ts_entry_t *p_next = p_entry->p_next;
212         fclose( p_entry->file );
213         free( p_entry );
214         p_entry = p_next;
215     }
216     for( p_entry = p_sys->p_read_list; p_entry; )
217     {
218         ts_entry_t *p_next = p_entry->p_next;
219         fclose( p_entry->file );
220         free( p_entry );
221         p_entry = p_next;
222     }
223     for( i = 0; i < p_sys->i_files; i++ )
224     {
225         sprintf( p_sys->psz_filename, "%s%i.dat",
226                  p_sys->psz_filename_base, i );
227         unlink( p_sys->psz_filename );
228     }
229
230     free( p_sys->psz_filename );
231     free( p_sys->psz_filename_base );
232     block_FifoRelease( p_sys->p_fifo );
233     free( p_sys );
234 }
235
236 /*****************************************************************************
237  *
238  *****************************************************************************/
239 static block_t *Block( access_t *p_access )
240 {
241     access_sys_t *p_sys = p_access->p_sys;
242     access_t *p_src = p_access->p_source;
243     block_t *p_block = NULL;
244
245     /* Update info (we probably ought to be time caching that as well) */
246     if( p_src->info.i_update & INPUT_UPDATE_META )
247     {
248         p_src->info.i_update &= ~INPUT_UPDATE_META;
249         p_access->info.i_update |= INPUT_UPDATE_META;
250     }
251
252     /* Get data from timeshift fifo */
253     if( !p_access->info.b_eof )
254         p_block = block_FifoGet( p_sys->p_fifo );
255
256     if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
257     { block_Release( p_block ); p_block = 0; }
258
259     if( p_block )
260     {
261         p_sys->i_data -= p_block->i_buffer;
262         return p_block;
263     }
264
265     p_access->info.b_eof = p_src->info.b_eof;
266     return NULL;
267 }
268
269 /*****************************************************************************
270  *
271  *****************************************************************************/
272 static void Thread( access_t *p_access )
273 {
274     access_sys_t *p_sys = p_access->p_sys;
275     access_t     *p_src = p_access->p_source;
276     block_t      *p_block;
277
278     while( vlc_object_alive (p_access) )
279     {
280         /* Get a new block from the source */
281         if( p_src->pf_block )
282         {
283             p_block = p_src->pf_block( p_src );
284         }
285         else
286         {
287             if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
288
289             p_block->i_buffer =
290                 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
291
292             if( p_block->i_buffer <= 0 )
293             {
294               block_Release( p_block );
295               p_block = NULL;
296             }
297         }
298
299         if( p_block == NULL )
300         {
301           if( p_src->info.b_eof ) break;
302           msleep( 10000 );
303           continue;
304         }
305
306         p_sys->i_data += p_block->i_buffer;
307
308         /* Write block */
309         if( !p_sys->p_write_list && !p_sys->p_read_list &&
310             block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MAX )
311         {
312             /* If there isn't too much timeshifted data,
313              * write directly to FIFO */
314             block_FifoPut( p_sys->p_fifo, p_block );
315             continue;
316         }
317
318         WriteBlockToFile( p_access, p_block );
319         block_Release( p_block );
320
321         /* Read from file to fill up the fifo */
322         while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
323                vlc_object_alive (p_access) )
324         {
325             p_block = ReadBlockFromFile( p_access );
326             if( !p_block ) break;
327
328             block_FifoPut( p_sys->p_fifo, p_block );
329         }
330     }
331
332     msg_Dbg( p_access, "timeshift: no more input data" );
333
334     while( vlc_object_alive (p_access) &&
335            (p_sys->p_read_list || block_FifoSize( p_sys->p_fifo ) ) )
336     {
337         /* Read from file to fill up the fifo */
338         while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
339                vlc_object_alive (p_access) && p_sys->p_read_list )
340         {
341             p_block = ReadBlockFromFile( p_access );
342             if( !p_block ) break;
343
344             block_FifoPut( p_sys->p_fifo, p_block );
345         }
346
347         msleep( 100000 );
348     }
349
350     msg_Dbg( p_access, "timeshift: EOF" );
351     p_src->info.b_eof = true;
352
353     /* Send dummy packet to avoid deadlock in Block() */
354     block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
355 }
356
357 /*****************************************************************************
358  * NextFileWrite:
359  *****************************************************************************/
360 static void NextFileWrite( access_t *p_access )
361 {
362     access_sys_t *p_sys = p_access->p_sys;
363     ts_entry_t   *p_next;
364
365     if( !p_sys->p_write_list )
366     {
367         p_sys->i_write_size = 0;
368         return;
369     }
370
371     p_next = p_sys->p_write_list->p_next;
372
373     /* Put written file in read list */
374     if( p_sys->i_write_size < p_sys->i_file_size )
375         ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
376
377     fseek( p_sys->p_write_list->file, 0, SEEK_SET );
378     *p_sys->pp_read_last = p_sys->p_write_list;
379     p_sys->pp_read_last = &p_sys->p_write_list->p_next;
380     p_sys->p_write_list->p_next = 0;
381
382     /* Switch to next file to write */
383     p_sys->p_write_list = p_next;
384     if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
385
386     p_sys->i_write_size = 0;
387 }
388
389 /*****************************************************************************
390  * NextFileRead:
391  *****************************************************************************/
392 static void NextFileRead( access_t *p_access )
393 {
394     access_sys_t *p_sys = p_access->p_sys;
395     ts_entry_t   *p_next;
396
397     if( !p_sys->p_read_list ) return;
398
399     p_next = p_sys->p_read_list->p_next;
400
401     /* Put read file in write list */
402     fseek( p_sys->p_read_list->file, 0, SEEK_SET );
403     *p_sys->pp_write_last = p_sys->p_read_list;
404     p_sys->pp_write_last = &p_sys->p_read_list->p_next;
405     p_sys->p_read_list->p_next = 0;
406
407     /* Switch to next file to read */
408     p_sys->p_read_list = p_next;
409     if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
410 }
411
412 /*****************************************************************************
413  * WriteBlockToFile:
414  *****************************************************************************/
415 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
416 {
417     access_sys_t *p_sys = p_access->p_sys;
418     int i_write, i_buffer;
419
420     if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
421
422     /* Open new file if necessary */
423     if( !p_sys->p_write_list )
424     {
425         FILE *file;
426
427         sprintf( p_sys->psz_filename, "%s%i.dat",
428                  p_sys->psz_filename_base, p_sys->i_files );
429         file = utf8_fopen( p_sys->psz_filename, "w+b" );
430
431         if( !file && p_sys->i_files < 2 )
432         {
433             /* We just can't work with less than 2 buffer files */
434             msg_Err( p_access, "cannot open temporary file '%s' (%m)",
435                      p_sys->psz_filename );
436             return VLC_EGENERIC;
437         }
438         else if( !file ) return VLC_EGENERIC;
439
440         p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
441         p_sys->p_write_list->p_next = 0;
442         p_sys->p_write_list->file = file;
443         p_sys->pp_write_last = &p_sys->p_write_list->p_next;
444
445         p_sys->i_files++;
446     }
447
448     /* Write to file */
449     i_buffer = __MIN( p_block->i_buffer,
450                       p_sys->i_file_size - p_sys->i_write_size );
451
452     i_write = fwrite( p_block->p_buffer, 1, i_buffer,
453                       p_sys->p_write_list->file );
454
455     if( i_write > 0 ) p_sys->i_write_size += i_write;
456
457     //p_access->info.i_size += i_write;
458     //p_access->info.i_update |= INPUT_UPDATE_SIZE;
459
460     if( i_write < i_buffer )
461     {
462         /* Looks like we're short of space */
463
464         if( !p_sys->p_write_list->p_next )
465         {
466             msg_Warn( p_access, "no more space, overwritting old data" );
467             NextFileRead( p_access );
468             NextFileRead( p_access );
469         }
470
471         /* Make sure we switch to next file in write list */
472         p_sys->i_write_size = p_sys->i_file_size;
473     }
474
475     p_block->p_buffer += i_write;
476     p_block->i_buffer -= i_write;
477
478     /* Check if we have some data left */
479     if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
480
481     return VLC_SUCCESS;
482 }
483
484 /*****************************************************************************
485  * ReadBlockFromFile:
486  *****************************************************************************/
487 static block_t *ReadBlockFromFile( access_t *p_access )
488 {
489     access_sys_t *p_sys = p_access->p_sys;
490     block_t *p_block;
491
492     if( !p_sys->p_read_list && p_sys->p_write_list )
493     {
494         /* Force switching to next write file, that should
495          * give us something to read */
496         NextFileWrite( p_access );
497     }
498
499     if( !p_sys->p_read_list ) return 0;
500
501     p_block = block_New( p_access, 4096 );
502     p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
503                                p_sys->p_read_list->file );
504
505     if( p_block->i_buffer == 0 ) NextFileRead( p_access );
506
507     //p_access->info.i_size -= p_block->i_buffer;
508     //p_access->info.i_update |= INPUT_UPDATE_SIZE;
509
510     return p_block;
511 }
512
513 /*****************************************************************************
514  * Seek: seek to a specific location in a file
515  *****************************************************************************/
516 static int Seek( access_t *p_access, int64_t i_pos )
517 {
518     //access_sys_t *p_sys = p_access->p_sys;
519     return VLC_SUCCESS;
520 }
521
522 /*****************************************************************************
523  *
524  *****************************************************************************/
525 static int Control( access_t *p_access, int i_query, va_list args )
526 {
527     bool   *pb_bool;
528     int          *pi_int;
529
530     switch( i_query )
531     {
532     case ACCESS_CAN_SEEK:
533     case ACCESS_CAN_FASTSEEK:
534         pb_bool = (bool*)va_arg( args, bool* );
535         *pb_bool = true;
536         break;
537
538     case ACCESS_CAN_CONTROL_PACE:   /* Not really true */
539     case ACCESS_CAN_PAUSE:
540         pb_bool = (bool*)va_arg( args, bool* );
541         *pb_bool = true;
542         break;
543
544     case ACCESS_GET_MTU:
545         pi_int = (int*)va_arg( args, int * );
546         *pi_int = 0;
547         break;
548
549     case ACCESS_SET_PAUSE_STATE:
550         break;
551
552     /* Forward everything else to the source access */
553     default:
554         return access_vaControl( p_access->p_source, i_query, args );
555     }
556     return VLC_SUCCESS;
557 }
558
559 /*****************************************************************************
560  * GetTmpFilePath:
561  *****************************************************************************/
562 #ifdef WIN32
563 #define getpid() (int)GetCurrentProcessId()
564 #endif
565 static char *GetTmpFilePath( access_t *p_access )
566 {
567     char *psz_dir = var_GetNonEmptyString( p_access, "timeshift-dir" );
568     char *psz_filename_base;
569
570     if( psz_dir == NULL )
571     {
572 #ifdef WIN32
573         DWORD ret = GetTempPathW (0, NULL);
574         wchar_t wdir[ret + 3]; // can at least old "C:" + nul
575         const wchar_t *pwdir = wdir;
576         wchar_t *pwdir_free = NULL;
577
578         if (GetTempPathW (ret + 1, wdir) == 0)
579         {
580             pwdir_free = pwdir = _wgetcwd (NULL, 0);
581             if (pwdir == NULL)
582                 pwdir = L"C:";
583         }
584
585         psz_dir = FromWide (pwdir);
586         if (pwdir_free != NULL)
587             free (pwdir_free);
588
589         /* remove trailing antislash if any */
590         if (psz_dir[strlen (psz_dir) - 1] == '\\')
591             psz_dir[strlen (psz_dir) - 1] = '\0';
592 #else
593         psz_dir = strdup( "/tmp" );
594 #endif
595     }
596
597     asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
598               psz_dir, getpid(), p_access->i_object_id );
599     free( psz_dir );
600
601     return psz_filename_base;
602 }