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 *****************************************************************************/
34 #include <vlc_access.h>
35 #include <vlc_charset.h>
36 #include <vlc_input.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 #define GRANULARITY_TEXT N_("Timeshift granularity")
47 /// \bug [String] typo
48 #define GRANULARITY_LONGTEXT N_( "This is the size of the temporary files " \
49 "that will be used to store the timeshifted streams." )
50 #define DIR_TEXT N_("Timeshift directory")
51 #define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \
53 #define FORCE_TEXT N_("Force use of the timeshift module")
54 #define FORCE_LONGTEXT N_("Force use of the timeshift module even if the " \
55 "access declares that it can control pace or pause." )
58 set_shortname( _("Timeshift") );
59 set_description( _("Timeshift") );
60 set_category( CAT_INPUT );
61 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
62 set_capability( "access_filter", 0 );
63 add_shortcut( "timeshift" );
64 set_callbacks( Open, Close );
66 add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
67 GRANULARITY_LONGTEXT, VLC_TRUE );
68 add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, VLC_FALSE );
69 add_bool( "timeshift-force", VLC_FALSE, NULL, FORCE_TEXT, FORCE_LONGTEXT,
73 /*****************************************************************************
75 *****************************************************************************/
77 static int Seek( access_t *, int64_t );
78 static block_t *Block ( access_t *p_access );
79 static int Control( access_t *, int i_query, va_list args );
80 static void Thread ( access_t *p_access );
81 static int WriteBlockToFile( access_t *p_access, block_t *p_block );
82 static block_t *ReadBlockFromFile( access_t *p_access );
83 static char *GetTmpFilePath( access_t *p_access );
85 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
86 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
87 #define TMP_FILE_MAX 256
89 typedef struct ts_entry_t
92 struct ts_entry_t *p_next;
104 ts_entry_t *p_read_list;
105 ts_entry_t **pp_read_last;
106 ts_entry_t *p_write_list;
107 ts_entry_t **pp_write_last;
109 char *psz_filename_base;
115 /*****************************************************************************
117 *****************************************************************************/
118 static int Open( vlc_object_t *p_this )
120 access_t *p_access = (access_t*)p_this;
121 access_t *p_src = p_access->p_source;
125 var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
126 if( var_GetBool( p_access, "timeshift-force" ) )
128 msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
132 /* Only work with not pace controled access */
133 if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
136 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
139 /* Refuse access that can be paused */
140 if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
142 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
148 p_access->pf_read = NULL;
149 p_access->pf_block = Block;
150 p_access->pf_seek = Seek;
151 p_access->pf_control = Control;
152 p_access->info = p_src->info;
154 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
157 p_sys->p_fifo = block_FifoNew( p_access );
158 p_sys->i_write_size = 0;
162 p_sys->p_read_list = NULL;
163 p_sys->pp_read_last = &p_sys->p_read_list;
164 p_sys->p_write_list = NULL;
165 p_sys->pp_write_last = &p_sys->p_write_list;
167 var_Create( p_access, "timeshift-dir",
168 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
169 var_Create( p_access, "timeshift-granularity",
170 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
171 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
172 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
173 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
175 p_sys->psz_filename_base = GetTmpFilePath( p_access );
176 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
178 if( vlc_thread_create( p_access, "timeshift thread", Thread,
179 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
181 msg_Err( p_access, "cannot spawn timeshift access thread" );
188 /*****************************************************************************
190 *****************************************************************************/
191 static void Close( vlc_object_t *p_this )
193 access_t *p_access = (access_t*)p_this;
194 access_sys_t *p_sys = p_access->p_sys;
198 msg_Dbg( p_access, "timeshift close called" );
199 vlc_thread_join( p_access );
201 for( p_entry = p_sys->p_write_list; p_entry; )
203 ts_entry_t *p_next = p_entry->p_next;
204 fclose( p_entry->file );
208 for( p_entry = p_sys->p_read_list; p_entry; )
210 ts_entry_t *p_next = p_entry->p_next;
211 fclose( p_entry->file );
215 for( i = 0; i < p_sys->i_files; i++ )
217 sprintf( p_sys->psz_filename, "%s%i.dat",
218 p_sys->psz_filename_base, i );
219 unlink( p_sys->psz_filename );
222 free( p_sys->psz_filename );
223 free( p_sys->psz_filename_base );
224 block_FifoRelease( p_sys->p_fifo );
228 /*****************************************************************************
230 *****************************************************************************/
231 static block_t *Block( access_t *p_access )
233 access_sys_t *p_sys = p_access->p_sys;
234 access_t *p_src = p_access->p_source;
235 block_t *p_block = NULL;
237 /* Update info (we probably ought to be time caching that as well) */
238 if( p_src->info.i_update & INPUT_UPDATE_META )
240 p_src->info.i_update &= ~INPUT_UPDATE_META;
241 p_access->info.i_update |= INPUT_UPDATE_META;
244 /* Get data from timeshift fifo */
245 if( !p_access->info.b_eof )
246 p_block = block_FifoGet( p_sys->p_fifo );
248 if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
249 { block_Release( p_block ); p_block = 0; }
253 p_sys->i_data -= p_block->i_buffer;
257 p_access->info.b_eof = p_src->info.b_eof;
261 /*****************************************************************************
263 *****************************************************************************/
264 static void Thread( access_t *p_access )
266 access_sys_t *p_sys = p_access->p_sys;
267 access_t *p_src = p_access->p_source;
270 while( !p_access->b_die )
272 /* Get a new block from the source */
273 if( p_src->pf_block )
275 p_block = p_src->pf_block( p_src );
279 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
282 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
284 if( p_block->i_buffer <= 0 )
286 block_Release( p_block );
291 if( p_block == NULL )
293 if( p_src->info.b_eof ) break;
298 p_sys->i_data += p_block->i_buffer;
301 if( !p_sys->p_write_list && !p_sys->p_read_list &&
302 p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
304 /* If there isn't too much timeshifted data,
305 * write directly to FIFO */
306 block_FifoPut( p_sys->p_fifo, p_block );
310 WriteBlockToFile( p_access, p_block );
311 block_Release( p_block );
313 /* Read from file to fill up the fifo */
314 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
317 p_block = ReadBlockFromFile( p_access );
318 if( !p_block ) break;
320 block_FifoPut( p_sys->p_fifo, p_block );
324 msg_Dbg( p_access, "timeshift: no more input data" );
326 while( !p_access->b_die &&
327 (p_sys->p_read_list || p_sys->p_fifo->i_size) )
329 /* Read from file to fill up the fifo */
330 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
331 !p_access->b_die && p_sys->p_read_list )
333 p_block = ReadBlockFromFile( p_access );
334 if( !p_block ) break;
336 block_FifoPut( p_sys->p_fifo, p_block );
342 msg_Dbg( p_access, "timeshift: EOF" );
343 p_src->info.b_eof = VLC_TRUE;
345 /* Send dummy packet to avoid deadlock in Block() */
346 block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
349 /*****************************************************************************
351 *****************************************************************************/
352 static void NextFileWrite( access_t *p_access )
354 access_sys_t *p_sys = p_access->p_sys;
357 if( !p_sys->p_write_list )
359 p_sys->i_write_size = 0;
363 p_next = p_sys->p_write_list->p_next;
365 /* Put written file in read list */
366 if( p_sys->i_write_size < p_sys->i_file_size )
367 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
369 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
370 *p_sys->pp_read_last = p_sys->p_write_list;
371 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
372 p_sys->p_write_list->p_next = 0;
374 /* Switch to next file to write */
375 p_sys->p_write_list = p_next;
376 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
378 p_sys->i_write_size = 0;
381 /*****************************************************************************
383 *****************************************************************************/
384 static void NextFileRead( access_t *p_access )
386 access_sys_t *p_sys = p_access->p_sys;
389 if( !p_sys->p_read_list ) return;
391 p_next = p_sys->p_read_list->p_next;
393 /* Put read file in write list */
394 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
395 *p_sys->pp_write_last = p_sys->p_read_list;
396 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
397 p_sys->p_read_list->p_next = 0;
399 /* Switch to next file to read */
400 p_sys->p_read_list = p_next;
401 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
404 /*****************************************************************************
406 *****************************************************************************/
407 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
409 access_sys_t *p_sys = p_access->p_sys;
410 int i_write, i_buffer;
412 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
414 /* Open new file if necessary */
415 if( !p_sys->p_write_list )
419 sprintf( p_sys->psz_filename, "%s%i.dat",
420 p_sys->psz_filename_base, p_sys->i_files );
421 file = utf8_fopen( p_sys->psz_filename, "w+b" );
423 if( !file && p_sys->i_files < 2 )
425 /* We just can't work with less than 2 buffer files */
426 msg_Err( p_access, "cannot open temporary file '%s' (%s)",
427 p_sys->psz_filename, strerror(errno) );
430 else if( !file ) return VLC_EGENERIC;
432 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
433 p_sys->p_write_list->p_next = 0;
434 p_sys->p_write_list->file = file;
435 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
441 i_buffer = __MIN( p_block->i_buffer,
442 p_sys->i_file_size - p_sys->i_write_size );
444 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
445 p_sys->p_write_list->file );
447 if( i_write > 0 ) p_sys->i_write_size += i_write;
449 //p_access->info.i_size += i_write;
450 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
452 if( i_write < i_buffer )
454 /* Looks like we're short of space */
456 if( !p_sys->p_write_list->p_next )
458 msg_Warn( p_access, "no more space, overwritting old data" );
459 NextFileRead( p_access );
460 NextFileRead( p_access );
463 /* Make sure we switch to next file in write list */
464 p_sys->i_write_size = p_sys->i_file_size;
467 p_block->p_buffer += i_write;
468 p_block->i_buffer -= i_write;
470 /* Check if we have some data left */
471 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
476 /*****************************************************************************
478 *****************************************************************************/
479 static block_t *ReadBlockFromFile( access_t *p_access )
481 access_sys_t *p_sys = p_access->p_sys;
484 if( !p_sys->p_read_list && p_sys->p_write_list )
486 /* Force switching to next write file, that should
487 * give us something to read */
488 NextFileWrite( p_access );
491 if( !p_sys->p_read_list ) return 0;
493 p_block = block_New( p_access, 4096 );
494 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
495 p_sys->p_read_list->file );
497 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
499 //p_access->info.i_size -= p_block->i_buffer;
500 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
505 /*****************************************************************************
506 * Seek: seek to a specific location in a file
507 *****************************************************************************/
508 static int Seek( access_t *p_access, int64_t i_pos )
510 //access_sys_t *p_sys = p_access->p_sys;
514 /*****************************************************************************
516 *****************************************************************************/
517 static int Control( access_t *p_access, int i_query, va_list args )
524 case ACCESS_CAN_SEEK:
525 case ACCESS_CAN_FASTSEEK:
526 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
530 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
531 case ACCESS_CAN_PAUSE:
532 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
537 pi_int = (int*)va_arg( args, int * );
541 case ACCESS_SET_PAUSE_STATE:
544 /* Forward everything else to the source access */
546 return access2_vaControl( p_access->p_source, i_query, args );
551 /*****************************************************************************
553 *****************************************************************************/
555 #define getpid() GetCurrentProcessId()
557 static char *GetTmpFilePath( access_t *p_access )
559 char *psz_dir = var_GetString( p_access, "timeshift-dir" );
560 char *psz_filename_base;
562 if( ( psz_dir != NULL ) && ( psz_dir[0] == '\0' ) )
568 if( psz_dir == NULL )
571 DWORD ret = GetTempPathW (0, NULL);
572 wchar_t wdir[ret + 3]; // can at least old "C:" + nul
573 const wchar_t *pwdir = wdir;
574 wchar_t *pwdir_free = NULL;
576 if (GetTempPathW (ret + 1, wdir) == 0)
578 pwdir_free = pwdir = _wgetcwd (NULL, 0);
583 psz_dir = FromWide (pwdir);
584 if (pwdir_free != NULL)
587 /* remove trailing antislash if any */
588 if (psz_dir[strlen (psz_dir) - 1] == '\\')
589 psz_dir[strlen (psz_dir) - 1] = '\0';
591 psz_dir = strdup( "/tmp" );
595 asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
596 psz_dir, getpid(), p_access->i_object_id );
599 return psz_filename_base;