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