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