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 );
76 add_bool( "timeshift-force", false, NULL, FORCE_TEXT, FORCE_LONGTEXT,
80 /*****************************************************************************
82 *****************************************************************************/
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 ( vlc_object_t *p_this );
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 );
92 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
93 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
94 #define TMP_FILE_MAX 256
96 typedef struct ts_entry_t
99 struct ts_entry_t *p_next;
105 block_fifo_t *p_fifo;
108 unsigned i_file_size;
109 unsigned i_write_size;
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;
116 char *psz_filename_base;
122 /*****************************************************************************
124 *****************************************************************************/
125 static int Open( vlc_object_t *p_this )
127 access_t *p_access = (access_t*)p_this;
128 access_t *p_src = p_access->p_source;
132 var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
133 if( var_GetBool( p_access, "timeshift-force" ) )
135 msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
139 /* Only work with not pace controled access */
140 if( access_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
143 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
146 /* Refuse access that can be paused */
147 if( access_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
149 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
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;
161 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
166 p_sys->p_fifo = block_FifoNew();
167 p_sys->i_write_size = 0;
171 p_sys->p_read_list = NULL;
172 p_sys->pp_read_last = &p_sys->p_read_list;
173 p_sys->p_write_list = NULL;
174 p_sys->pp_write_last = &p_sys->p_write_list;
176 var_Create( p_access, "timeshift-dir",
177 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
178 var_Create( p_access, "timeshift-granularity",
179 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
180 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
181 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
182 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
184 p_sys->psz_filename_base = GetTmpFilePath( p_access );
185 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
187 if( vlc_thread_create( p_access, "timeshift thread", Thread,
188 VLC_THREAD_PRIORITY_LOW, false ) )
191 msg_Err( p_access, "cannot spawn timeshift access thread" );
198 /*****************************************************************************
200 *****************************************************************************/
201 static void Close( vlc_object_t *p_this )
203 access_t *p_access = (access_t*)p_this;
204 access_sys_t *p_sys = p_access->p_sys;
208 msg_Dbg( p_access, "timeshift close called" );
209 vlc_thread_join( p_access );
211 for( p_entry = p_sys->p_write_list; p_entry; )
213 ts_entry_t *p_next = p_entry->p_next;
214 fclose( p_entry->file );
218 for( p_entry = p_sys->p_read_list; p_entry; )
220 ts_entry_t *p_next = p_entry->p_next;
221 fclose( p_entry->file );
225 for( i = 0; i < p_sys->i_files; i++ )
227 sprintf( p_sys->psz_filename, "%s%i.dat",
228 p_sys->psz_filename_base, i );
229 unlink( p_sys->psz_filename );
232 free( p_sys->psz_filename );
233 free( p_sys->psz_filename_base );
234 block_FifoRelease( p_sys->p_fifo );
238 /*****************************************************************************
240 *****************************************************************************/
241 static block_t *Block( access_t *p_access )
243 access_sys_t *p_sys = p_access->p_sys;
244 access_t *p_src = p_access->p_source;
245 block_t *p_block = NULL;
247 /* Update info (we probably ought to be time caching that as well) */
248 if( p_src->info.i_update & INPUT_UPDATE_META )
250 p_src->info.i_update &= ~INPUT_UPDATE_META;
251 p_access->info.i_update |= INPUT_UPDATE_META;
254 /* Get data from timeshift fifo */
255 if( !p_access->info.b_eof )
256 p_block = block_FifoGet( p_sys->p_fifo );
258 if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
259 { block_Release( p_block ); p_block = 0; }
263 p_sys->i_data -= p_block->i_buffer;
267 p_access->info.b_eof = p_src->info.b_eof;
271 /*****************************************************************************
273 *****************************************************************************/
274 static void* Thread( vlc_object_t* p_this )
276 access_t *p_access = (access_t*)p_this;
277 access_sys_t *p_sys = p_access->p_sys;
278 access_t *p_src = p_access->p_source;
281 int canc = vlc_savecancel ();
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 ) );
360 vlc_restorecancel (canc);
364 /*****************************************************************************
366 *****************************************************************************/
367 static void NextFileWrite( access_t *p_access )
369 access_sys_t *p_sys = p_access->p_sys;
372 if( !p_sys->p_write_list )
374 p_sys->i_write_size = 0;
378 p_next = p_sys->p_write_list->p_next;
380 /* Put written file in read list */
381 if( p_sys->i_write_size < p_sys->i_file_size )
382 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
384 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
385 *p_sys->pp_read_last = p_sys->p_write_list;
386 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
387 p_sys->p_write_list->p_next = 0;
389 /* Switch to next file to write */
390 p_sys->p_write_list = p_next;
391 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
393 p_sys->i_write_size = 0;
396 /*****************************************************************************
398 *****************************************************************************/
399 static void NextFileRead( access_t *p_access )
401 access_sys_t *p_sys = p_access->p_sys;
404 if( !p_sys->p_read_list ) return;
406 p_next = p_sys->p_read_list->p_next;
408 /* Put read file in write list */
409 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
410 *p_sys->pp_write_last = p_sys->p_read_list;
411 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
412 p_sys->p_read_list->p_next = 0;
414 /* Switch to next file to read */
415 p_sys->p_read_list = p_next;
416 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
419 /*****************************************************************************
421 *****************************************************************************/
422 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
424 access_sys_t *p_sys = p_access->p_sys;
425 int i_write, i_buffer;
427 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
429 /* Open new file if necessary */
430 if( !p_sys->p_write_list )
434 sprintf( p_sys->psz_filename, "%s%u.dat",
435 p_sys->psz_filename_base, p_sys->i_files );
436 file = utf8_fopen( p_sys->psz_filename, "w+b" );
438 if( !file && p_sys->i_files < 2 )
440 /* We just can't work with less than 2 buffer files */
441 msg_Err( p_access, "cannot open temporary file '%s' (%m)",
442 p_sys->psz_filename );
445 else if( !file ) return VLC_EGENERIC;
447 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
448 p_sys->p_write_list->p_next = 0;
449 p_sys->p_write_list->file = file;
450 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
456 i_buffer = __MIN( p_block->i_buffer,
457 p_sys->i_file_size - p_sys->i_write_size );
459 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
460 p_sys->p_write_list->file );
462 if( i_write > 0 ) p_sys->i_write_size += i_write;
464 //p_access->info.i_size += i_write;
465 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
467 if( i_write < i_buffer )
469 /* Looks like we're short of space */
471 if( !p_sys->p_write_list->p_next )
473 msg_Warn( p_access, "no more space, overwritting old data" );
474 NextFileRead( p_access );
475 NextFileRead( p_access );
478 /* Make sure we switch to next file in write list */
479 p_sys->i_write_size = p_sys->i_file_size;
482 p_block->p_buffer += i_write;
483 p_block->i_buffer -= i_write;
485 /* Check if we have some data left */
486 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
491 /*****************************************************************************
493 *****************************************************************************/
494 static block_t *ReadBlockFromFile( access_t *p_access )
496 access_sys_t *p_sys = p_access->p_sys;
499 if( !p_sys->p_read_list && p_sys->p_write_list )
501 /* Force switching to next write file, that should
502 * give us something to read */
503 NextFileWrite( p_access );
506 if( !p_sys->p_read_list ) return 0;
508 p_block = block_New( p_access, 4096 );
509 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
510 p_sys->p_read_list->file );
512 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
514 //p_access->info.i_size -= p_block->i_buffer;
515 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
520 /*****************************************************************************
521 * Seek: seek to a specific location in a file
522 *****************************************************************************/
523 static int Seek( access_t *p_access, int64_t i_pos )
525 //access_sys_t *p_sys = p_access->p_sys;
531 /*****************************************************************************
533 *****************************************************************************/
534 static int Control( access_t *p_access, int i_query, va_list args )
541 case ACCESS_CAN_SEEK:
542 case ACCESS_CAN_FASTSEEK:
543 pb_bool = (bool*)va_arg( args, bool* );
547 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
548 case ACCESS_CAN_PAUSE:
549 pb_bool = (bool*)va_arg( args, bool* );
554 pi_int = (int*)va_arg( args, int * );
558 case ACCESS_SET_PAUSE_STATE:
561 /* Forward everything else to the source access */
563 return access_vaControl( p_access->p_source, i_query, args );
568 /*****************************************************************************
570 *****************************************************************************/
572 #define getpid() (int)GetCurrentProcessId()
574 static char *GetTmpFilePath( access_t *p_access )
576 char *psz_dir = var_GetNonEmptyString( p_access, "timeshift-dir" );
577 char *psz_filename_base;
579 if( psz_dir == NULL )
582 DWORD ret = GetTempPathW (0, NULL);
583 wchar_t wdir[ret + 3]; // can at least old "C:" + nul
584 const wchar_t *pwdir = wdir;
585 wchar_t *pwdir_free = NULL;
587 if (GetTempPathW (ret + 1, wdir) == 0)
589 pwdir_free = pwdir = _wgetcwd (NULL, 0);
594 psz_dir = FromWide (pwdir);
595 if (pwdir_free != NULL)
598 /* remove trailing antislash if any */
599 if (psz_dir[strlen (psz_dir) - 1] == '\\')
600 psz_dir[strlen (psz_dir) - 1] = '\0';
602 psz_dir = strdup( "/tmp" );
606 if( asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%p-",
607 psz_dir, getpid(), p_access ) == -1 )
608 psz_filename_base = NULL;
611 return psz_filename_base;