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