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 ( access_t *p_access );
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;
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 ) );
165 p_sys->p_fifo = block_FifoNew();
166 p_sys->i_write_size = 0;
170 p_sys->p_read_list = NULL;
171 p_sys->pp_read_last = &p_sys->p_read_list;
172 p_sys->p_write_list = NULL;
173 p_sys->pp_write_last = &p_sys->p_write_list;
175 var_Create( p_access, "timeshift-dir",
176 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
177 var_Create( p_access, "timeshift-granularity",
178 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
180 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
181 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
183 p_sys->psz_filename_base = GetTmpFilePath( p_access );
184 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
186 if( vlc_thread_create( p_access, "timeshift thread", Thread,
187 VLC_THREAD_PRIORITY_LOW, false ) )
189 msg_Err( p_access, "cannot spawn timeshift access thread" );
196 /*****************************************************************************
198 *****************************************************************************/
199 static void Close( vlc_object_t *p_this )
201 access_t *p_access = (access_t*)p_this;
202 access_sys_t *p_sys = p_access->p_sys;
206 msg_Dbg( p_access, "timeshift close called" );
207 vlc_thread_join( p_access );
209 for( p_entry = p_sys->p_write_list; p_entry; )
211 ts_entry_t *p_next = p_entry->p_next;
212 fclose( p_entry->file );
216 for( p_entry = p_sys->p_read_list; p_entry; )
218 ts_entry_t *p_next = p_entry->p_next;
219 fclose( p_entry->file );
223 for( i = 0; i < p_sys->i_files; i++ )
225 sprintf( p_sys->psz_filename, "%s%i.dat",
226 p_sys->psz_filename_base, i );
227 unlink( p_sys->psz_filename );
230 free( p_sys->psz_filename );
231 free( p_sys->psz_filename_base );
232 block_FifoRelease( p_sys->p_fifo );
236 /*****************************************************************************
238 *****************************************************************************/
239 static block_t *Block( access_t *p_access )
241 access_sys_t *p_sys = p_access->p_sys;
242 access_t *p_src = p_access->p_source;
243 block_t *p_block = NULL;
245 /* Update info (we probably ought to be time caching that as well) */
246 if( p_src->info.i_update & INPUT_UPDATE_META )
248 p_src->info.i_update &= ~INPUT_UPDATE_META;
249 p_access->info.i_update |= INPUT_UPDATE_META;
252 /* Get data from timeshift fifo */
253 if( !p_access->info.b_eof )
254 p_block = block_FifoGet( p_sys->p_fifo );
256 if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
257 { block_Release( p_block ); p_block = 0; }
261 p_sys->i_data -= p_block->i_buffer;
265 p_access->info.b_eof = p_src->info.b_eof;
269 /*****************************************************************************
271 *****************************************************************************/
272 static void Thread( access_t *p_access )
274 access_sys_t *p_sys = p_access->p_sys;
275 access_t *p_src = p_access->p_source;
278 while( vlc_object_alive (p_access) )
280 /* Get a new block from the source */
281 if( p_src->pf_block )
283 p_block = p_src->pf_block( p_src );
287 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
290 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
292 if( p_block->i_buffer <= 0 )
294 block_Release( p_block );
299 if( p_block == NULL )
301 if( p_src->info.b_eof ) break;
306 p_sys->i_data += p_block->i_buffer;
309 if( !p_sys->p_write_list && !p_sys->p_read_list &&
310 block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MAX )
312 /* If there isn't too much timeshifted data,
313 * write directly to FIFO */
314 block_FifoPut( p_sys->p_fifo, p_block );
318 WriteBlockToFile( p_access, p_block );
319 block_Release( p_block );
321 /* Read from file to fill up the fifo */
322 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
323 vlc_object_alive (p_access) )
325 p_block = ReadBlockFromFile( p_access );
326 if( !p_block ) break;
328 block_FifoPut( p_sys->p_fifo, p_block );
332 msg_Dbg( p_access, "timeshift: no more input data" );
334 while( vlc_object_alive (p_access) &&
335 (p_sys->p_read_list || block_FifoSize( p_sys->p_fifo ) ) )
337 /* Read from file to fill up the fifo */
338 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
339 vlc_object_alive (p_access) && p_sys->p_read_list )
341 p_block = ReadBlockFromFile( p_access );
342 if( !p_block ) break;
344 block_FifoPut( p_sys->p_fifo, p_block );
350 msg_Dbg( p_access, "timeshift: EOF" );
351 p_src->info.b_eof = true;
353 /* Send dummy packet to avoid deadlock in Block() */
354 block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
357 /*****************************************************************************
359 *****************************************************************************/
360 static void NextFileWrite( access_t *p_access )
362 access_sys_t *p_sys = p_access->p_sys;
365 if( !p_sys->p_write_list )
367 p_sys->i_write_size = 0;
371 p_next = p_sys->p_write_list->p_next;
373 /* Put written file in read list */
374 if( p_sys->i_write_size < p_sys->i_file_size )
375 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
377 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
378 *p_sys->pp_read_last = p_sys->p_write_list;
379 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
380 p_sys->p_write_list->p_next = 0;
382 /* Switch to next file to write */
383 p_sys->p_write_list = p_next;
384 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
386 p_sys->i_write_size = 0;
389 /*****************************************************************************
391 *****************************************************************************/
392 static void NextFileRead( access_t *p_access )
394 access_sys_t *p_sys = p_access->p_sys;
397 if( !p_sys->p_read_list ) return;
399 p_next = p_sys->p_read_list->p_next;
401 /* Put read file in write list */
402 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
403 *p_sys->pp_write_last = p_sys->p_read_list;
404 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
405 p_sys->p_read_list->p_next = 0;
407 /* Switch to next file to read */
408 p_sys->p_read_list = p_next;
409 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
412 /*****************************************************************************
414 *****************************************************************************/
415 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
417 access_sys_t *p_sys = p_access->p_sys;
418 int i_write, i_buffer;
420 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
422 /* Open new file if necessary */
423 if( !p_sys->p_write_list )
427 sprintf( p_sys->psz_filename, "%s%i.dat",
428 p_sys->psz_filename_base, p_sys->i_files );
429 file = utf8_fopen( p_sys->psz_filename, "w+b" );
431 if( !file && p_sys->i_files < 2 )
433 /* We just can't work with less than 2 buffer files */
434 msg_Err( p_access, "cannot open temporary file '%s' (%m)",
435 p_sys->psz_filename );
438 else if( !file ) return VLC_EGENERIC;
440 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
441 p_sys->p_write_list->p_next = 0;
442 p_sys->p_write_list->file = file;
443 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
449 i_buffer = __MIN( p_block->i_buffer,
450 p_sys->i_file_size - p_sys->i_write_size );
452 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
453 p_sys->p_write_list->file );
455 if( i_write > 0 ) p_sys->i_write_size += i_write;
457 //p_access->info.i_size += i_write;
458 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
460 if( i_write < i_buffer )
462 /* Looks like we're short of space */
464 if( !p_sys->p_write_list->p_next )
466 msg_Warn( p_access, "no more space, overwritting old data" );
467 NextFileRead( p_access );
468 NextFileRead( p_access );
471 /* Make sure we switch to next file in write list */
472 p_sys->i_write_size = p_sys->i_file_size;
475 p_block->p_buffer += i_write;
476 p_block->i_buffer -= i_write;
478 /* Check if we have some data left */
479 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
484 /*****************************************************************************
486 *****************************************************************************/
487 static block_t *ReadBlockFromFile( access_t *p_access )
489 access_sys_t *p_sys = p_access->p_sys;
492 if( !p_sys->p_read_list && p_sys->p_write_list )
494 /* Force switching to next write file, that should
495 * give us something to read */
496 NextFileWrite( p_access );
499 if( !p_sys->p_read_list ) return 0;
501 p_block = block_New( p_access, 4096 );
502 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
503 p_sys->p_read_list->file );
505 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
507 //p_access->info.i_size -= p_block->i_buffer;
508 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
513 /*****************************************************************************
514 * Seek: seek to a specific location in a file
515 *****************************************************************************/
516 static int Seek( access_t *p_access, int64_t i_pos )
518 //access_sys_t *p_sys = p_access->p_sys;
522 /*****************************************************************************
524 *****************************************************************************/
525 static int Control( access_t *p_access, int i_query, va_list args )
532 case ACCESS_CAN_SEEK:
533 case ACCESS_CAN_FASTSEEK:
534 pb_bool = (bool*)va_arg( args, bool* );
538 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
539 case ACCESS_CAN_PAUSE:
540 pb_bool = (bool*)va_arg( args, bool* );
545 pi_int = (int*)va_arg( args, int * );
549 case ACCESS_SET_PAUSE_STATE:
552 /* Forward everything else to the source access */
554 return access_vaControl( p_access->p_source, i_query, args );
559 /*****************************************************************************
561 *****************************************************************************/
563 #define getpid() (int)GetCurrentProcessId()
565 static char *GetTmpFilePath( access_t *p_access )
567 char *psz_dir = var_GetNonEmptyString( p_access, "timeshift-dir" );
568 char *psz_filename_base;
570 if( psz_dir == NULL )
573 DWORD ret = GetTempPathW (0, NULL);
574 wchar_t wdir[ret + 3]; // can at least old "C:" + nul
575 const wchar_t *pwdir = wdir;
576 wchar_t *pwdir_free = NULL;
578 if (GetTempPathW (ret + 1, wdir) == 0)
580 pwdir_free = pwdir = _wgetcwd (NULL, 0);
585 psz_dir = FromWide (pwdir);
586 if (pwdir_free != NULL)
589 /* remove trailing antislash if any */
590 if (psz_dir[strlen (psz_dir) - 1] == '\\')
591 psz_dir[strlen (psz_dir) - 1] = '\0';
593 psz_dir = strdup( "/tmp" );
597 asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
598 psz_dir, getpid(), p_access->i_object_id );
601 return psz_filename_base;