1 /*****************************************************************************
2 * timeshift.c: access filter implementing timeshifting capabilities
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
37 #include <vlc_access.h>
38 #include <vlc_charset.h>
39 #include <vlc_input.h>
44 # include <direct.h> /* _wgetcwd */
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
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 " \
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." )
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 );
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 );
77 add_bool( "timeshift-force", false, NULL, FORCE_TEXT, FORCE_LONGTEXT,
81 /*****************************************************************************
83 *****************************************************************************/
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 ( vlc_object_t *p_this );
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 );
93 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
94 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
95 #define TMP_FILE_MAX 256
97 typedef struct ts_entry_t
100 struct ts_entry_t *p_next;
106 block_fifo_t *p_fifo;
109 unsigned i_file_size;
110 unsigned i_write_size;
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;
117 char *psz_filename_base;
123 /*****************************************************************************
125 *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
128 access_t *p_access = (access_t*)p_this;
129 access_t *p_src = p_access->p_source;
133 var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
134 if( var_GetBool( p_access, "timeshift-force" ) )
136 msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
140 /* Only work with not pace controled access */
141 if( access_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
144 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
147 /* Refuse access that can be paused */
148 if( access_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
150 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
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;
162 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
167 p_sys->p_fifo = block_FifoNew();
168 p_sys->i_write_size = 0;
172 p_sys->p_read_list = NULL;
173 p_sys->pp_read_last = &p_sys->p_read_list;
174 p_sys->p_write_list = NULL;
175 p_sys->pp_write_last = &p_sys->p_write_list;
177 var_Create( p_access, "timeshift-dir",
178 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
179 var_Create( p_access, "timeshift-granularity",
180 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
181 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
182 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
183 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
185 p_sys->psz_filename_base = GetTmpFilePath( p_access );
186 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
188 if( vlc_thread_create( p_access, "timeshift thread", Thread,
189 VLC_THREAD_PRIORITY_LOW, false ) )
192 msg_Err( p_access, "cannot spawn timeshift access thread" );
199 /*****************************************************************************
201 *****************************************************************************/
202 static void Close( vlc_object_t *p_this )
204 access_t *p_access = (access_t*)p_this;
205 access_sys_t *p_sys = p_access->p_sys;
209 msg_Dbg( p_access, "timeshift close called" );
210 vlc_thread_join( p_access );
212 for( p_entry = p_sys->p_write_list; p_entry; )
214 ts_entry_t *p_next = p_entry->p_next;
215 fclose( p_entry->file );
219 for( p_entry = p_sys->p_read_list; p_entry; )
221 ts_entry_t *p_next = p_entry->p_next;
222 fclose( p_entry->file );
226 for( i = 0; i < p_sys->i_files; i++ )
228 sprintf( p_sys->psz_filename, "%s%i.dat",
229 p_sys->psz_filename_base, i );
230 unlink( p_sys->psz_filename );
233 free( p_sys->psz_filename );
234 free( p_sys->psz_filename_base );
235 block_FifoRelease( p_sys->p_fifo );
239 /*****************************************************************************
241 *****************************************************************************/
242 static block_t *Block( access_t *p_access )
244 access_sys_t *p_sys = p_access->p_sys;
245 access_t *p_src = p_access->p_source;
246 block_t *p_block = NULL;
248 /* Update info (we probably ought to be time caching that as well) */
249 if( p_src->info.i_update & INPUT_UPDATE_META )
251 p_src->info.i_update &= ~INPUT_UPDATE_META;
252 p_access->info.i_update |= INPUT_UPDATE_META;
255 /* Get data from timeshift fifo */
256 if( !p_access->info.b_eof )
257 p_block = block_FifoGet( p_sys->p_fifo );
259 if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
260 { block_Release( p_block ); p_block = 0; }
264 p_sys->i_data -= p_block->i_buffer;
268 p_access->info.b_eof = p_src->info.b_eof;
272 /*****************************************************************************
274 *****************************************************************************/
275 static void* Thread( vlc_object_t* p_this )
277 access_t *p_access = (access_t*)p_this;
278 access_sys_t *p_sys = p_access->p_sys;
279 access_t *p_src = p_access->p_source;
282 while( vlc_object_alive (p_access) )
284 /* Get a new block from the source */
285 if( p_src->pf_block )
287 p_block = p_src->pf_block( p_src );
293 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
295 i_read = p_src->pf_read( p_src, p_block->p_buffer, 2048 );
298 block_Release( p_block );
301 p_block->i_buffer = i_read;
304 if( p_block == NULL )
306 if( p_src->info.b_eof ) break;
311 p_sys->i_data += p_block->i_buffer;
314 if( !p_sys->p_write_list && !p_sys->p_read_list &&
315 block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MAX )
317 /* If there isn't too much timeshifted data,
318 * write directly to FIFO */
319 block_FifoPut( p_sys->p_fifo, p_block );
323 WriteBlockToFile( p_access, p_block );
324 block_Release( p_block );
326 /* Read from file to fill up the fifo */
327 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
328 vlc_object_alive (p_access) )
330 p_block = ReadBlockFromFile( p_access );
331 if( !p_block ) break;
333 block_FifoPut( p_sys->p_fifo, p_block );
337 msg_Dbg( p_access, "timeshift: no more input data" );
339 while( vlc_object_alive (p_access) &&
340 (p_sys->p_read_list || block_FifoSize( p_sys->p_fifo ) ) )
342 /* Read from file to fill up the fifo */
343 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
344 vlc_object_alive (p_access) && p_sys->p_read_list )
346 p_block = ReadBlockFromFile( p_access );
347 if( !p_block ) break;
349 block_FifoPut( p_sys->p_fifo, p_block );
355 msg_Dbg( p_access, "timeshift: EOF" );
356 p_src->info.b_eof = true;
358 /* Send dummy packet to avoid deadlock in Block() */
359 block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
363 /*****************************************************************************
365 *****************************************************************************/
366 static void NextFileWrite( access_t *p_access )
368 access_sys_t *p_sys = p_access->p_sys;
371 if( !p_sys->p_write_list )
373 p_sys->i_write_size = 0;
377 p_next = p_sys->p_write_list->p_next;
379 /* Put written file in read list */
380 if( p_sys->i_write_size < p_sys->i_file_size )
381 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
383 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
384 *p_sys->pp_read_last = p_sys->p_write_list;
385 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
386 p_sys->p_write_list->p_next = 0;
388 /* Switch to next file to write */
389 p_sys->p_write_list = p_next;
390 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
392 p_sys->i_write_size = 0;
395 /*****************************************************************************
397 *****************************************************************************/
398 static void NextFileRead( access_t *p_access )
400 access_sys_t *p_sys = p_access->p_sys;
403 if( !p_sys->p_read_list ) return;
405 p_next = p_sys->p_read_list->p_next;
407 /* Put read file in write list */
408 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
409 *p_sys->pp_write_last = p_sys->p_read_list;
410 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
411 p_sys->p_read_list->p_next = 0;
413 /* Switch to next file to read */
414 p_sys->p_read_list = p_next;
415 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
418 /*****************************************************************************
420 *****************************************************************************/
421 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
423 access_sys_t *p_sys = p_access->p_sys;
424 int i_write, i_buffer;
426 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
428 /* Open new file if necessary */
429 if( !p_sys->p_write_list )
433 sprintf( p_sys->psz_filename, "%s%u.dat",
434 p_sys->psz_filename_base, p_sys->i_files );
435 file = utf8_fopen( p_sys->psz_filename, "w+b" );
437 if( !file && p_sys->i_files < 2 )
439 /* We just can't work with less than 2 buffer files */
440 msg_Err( p_access, "cannot open temporary file '%s' (%m)",
441 p_sys->psz_filename );
444 else if( !file ) return VLC_EGENERIC;
446 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
447 p_sys->p_write_list->p_next = 0;
448 p_sys->p_write_list->file = file;
449 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
455 i_buffer = __MIN( p_block->i_buffer,
456 p_sys->i_file_size - p_sys->i_write_size );
458 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
459 p_sys->p_write_list->file );
461 if( i_write > 0 ) p_sys->i_write_size += i_write;
463 //p_access->info.i_size += i_write;
464 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
466 if( i_write < i_buffer )
468 /* Looks like we're short of space */
470 if( !p_sys->p_write_list->p_next )
472 msg_Warn( p_access, "no more space, overwritting old data" );
473 NextFileRead( p_access );
474 NextFileRead( p_access );
477 /* Make sure we switch to next file in write list */
478 p_sys->i_write_size = p_sys->i_file_size;
481 p_block->p_buffer += i_write;
482 p_block->i_buffer -= i_write;
484 /* Check if we have some data left */
485 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
490 /*****************************************************************************
492 *****************************************************************************/
493 static block_t *ReadBlockFromFile( access_t *p_access )
495 access_sys_t *p_sys = p_access->p_sys;
498 if( !p_sys->p_read_list && p_sys->p_write_list )
500 /* Force switching to next write file, that should
501 * give us something to read */
502 NextFileWrite( p_access );
505 if( !p_sys->p_read_list ) return 0;
507 p_block = block_New( p_access, 4096 );
508 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
509 p_sys->p_read_list->file );
511 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
513 //p_access->info.i_size -= p_block->i_buffer;
514 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
519 /*****************************************************************************
520 * Seek: seek to a specific location in a file
521 *****************************************************************************/
522 static int Seek( access_t *p_access, int64_t i_pos )
524 //access_sys_t *p_sys = p_access->p_sys;
530 /*****************************************************************************
532 *****************************************************************************/
533 static int Control( access_t *p_access, int i_query, va_list args )
540 case ACCESS_CAN_SEEK:
541 case ACCESS_CAN_FASTSEEK:
542 pb_bool = (bool*)va_arg( args, bool* );
546 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
547 case ACCESS_CAN_PAUSE:
548 pb_bool = (bool*)va_arg( args, bool* );
553 pi_int = (int*)va_arg( args, int * );
557 case ACCESS_SET_PAUSE_STATE:
560 /* Forward everything else to the source access */
562 return access_vaControl( p_access->p_source, i_query, args );
567 /*****************************************************************************
569 *****************************************************************************/
571 #define getpid() (int)GetCurrentProcessId()
573 static char *GetTmpFilePath( access_t *p_access )
575 char *psz_dir = var_GetNonEmptyString( p_access, "timeshift-dir" );
576 char *psz_filename_base;
578 if( psz_dir == NULL )
581 DWORD ret = GetTempPathW (0, NULL);
582 wchar_t wdir[ret + 3]; // can at least old "C:" + nul
583 const wchar_t *pwdir = wdir;
584 wchar_t *pwdir_free = NULL;
586 if (GetTempPathW (ret + 1, wdir) == 0)
588 pwdir_free = pwdir = _wgetcwd (NULL, 0);
593 psz_dir = FromWide (pwdir);
594 if (pwdir_free != NULL)
597 /* remove trailing antislash if any */
598 if (psz_dir[strlen (psz_dir) - 1] == '\\')
599 psz_dir[strlen (psz_dir) - 1] = '\0';
601 psz_dir = strdup( "/tmp" );
605 if( asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
606 psz_dir, getpid(), p_access->i_object_id ) == -1 )
607 psz_filename_base = NULL;
610 return psz_filename_base;