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 *****************************************************************************/
35 #include <vlc_access.h>
36 #include <vlc_charset.h>
37 #include <vlc_input.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
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 " \
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." )
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 );
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,
74 /*****************************************************************************
76 *****************************************************************************/
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 );
86 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
87 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
88 #define TMP_FILE_MAX 256
90 typedef struct ts_entry_t
93 struct ts_entry_t *p_next;
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;
110 char *psz_filename_base;
116 /*****************************************************************************
118 *****************************************************************************/
119 static int Open( vlc_object_t *p_this )
121 access_t *p_access = (access_t*)p_this;
122 access_t *p_src = p_access->p_source;
126 var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
127 if( var_GetBool( p_access, "timeshift-force" ) )
129 msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
133 /* Only work with not pace controled access */
134 if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
137 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
140 /* Refuse access that can be paused */
141 if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
143 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
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;
155 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
158 p_sys->p_fifo = block_FifoNew( p_access );
159 p_sys->i_write_size = 0;
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;
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 */
176 p_sys->psz_filename_base = GetTmpFilePath( p_access );
177 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
179 if( vlc_thread_create( p_access, "timeshift thread", Thread,
180 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
182 msg_Err( p_access, "cannot spawn timeshift access thread" );
189 /*****************************************************************************
191 *****************************************************************************/
192 static void Close( vlc_object_t *p_this )
194 access_t *p_access = (access_t*)p_this;
195 access_sys_t *p_sys = p_access->p_sys;
199 msg_Dbg( p_access, "timeshift close called" );
200 vlc_thread_join( p_access );
202 for( p_entry = p_sys->p_write_list; p_entry; )
204 ts_entry_t *p_next = p_entry->p_next;
205 fclose( p_entry->file );
209 for( p_entry = p_sys->p_read_list; p_entry; )
211 ts_entry_t *p_next = p_entry->p_next;
212 fclose( p_entry->file );
216 for( i = 0; i < p_sys->i_files; i++ )
218 sprintf( p_sys->psz_filename, "%s%i.dat",
219 p_sys->psz_filename_base, i );
220 unlink( p_sys->psz_filename );
223 free( p_sys->psz_filename );
224 free( p_sys->psz_filename_base );
225 block_FifoRelease( p_sys->p_fifo );
229 /*****************************************************************************
231 *****************************************************************************/
232 static block_t *Block( access_t *p_access )
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;
238 /* Update info (we probably ought to be time caching that as well) */
239 if( p_src->info.i_update & INPUT_UPDATE_META )
241 p_src->info.i_update &= ~INPUT_UPDATE_META;
242 p_access->info.i_update |= INPUT_UPDATE_META;
245 /* Get data from timeshift fifo */
246 if( !p_access->info.b_eof )
247 p_block = block_FifoGet( p_sys->p_fifo );
249 if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
250 { block_Release( p_block ); p_block = 0; }
254 p_sys->i_data -= p_block->i_buffer;
258 p_access->info.b_eof = p_src->info.b_eof;
262 /*****************************************************************************
264 *****************************************************************************/
265 static void Thread( access_t *p_access )
267 access_sys_t *p_sys = p_access->p_sys;
268 access_t *p_src = p_access->p_source;
271 while( !p_access->b_die )
273 /* Get a new block from the source */
274 if( p_src->pf_block )
276 p_block = p_src->pf_block( p_src );
280 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
283 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
285 if( p_block->i_buffer <= 0 )
287 block_Release( p_block );
292 if( p_block == NULL )
294 if( p_src->info.b_eof ) break;
299 p_sys->i_data += p_block->i_buffer;
302 if( !p_sys->p_write_list && !p_sys->p_read_list &&
303 p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
305 /* If there isn't too much timeshifted data,
306 * write directly to FIFO */
307 block_FifoPut( p_sys->p_fifo, p_block );
311 WriteBlockToFile( p_access, p_block );
312 block_Release( p_block );
314 /* Read from file to fill up the fifo */
315 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
318 p_block = ReadBlockFromFile( p_access );
319 if( !p_block ) break;
321 block_FifoPut( p_sys->p_fifo, p_block );
325 msg_Dbg( p_access, "timeshift: no more input data" );
327 while( !p_access->b_die &&
328 (p_sys->p_read_list || p_sys->p_fifo->i_size) )
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 )
334 p_block = ReadBlockFromFile( p_access );
335 if( !p_block ) break;
337 block_FifoPut( p_sys->p_fifo, p_block );
343 msg_Dbg( p_access, "timeshift: EOF" );
344 p_src->info.b_eof = VLC_TRUE;
346 /* Send dummy packet to avoid deadlock in Block() */
347 block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
350 /*****************************************************************************
352 *****************************************************************************/
353 static void NextFileWrite( access_t *p_access )
355 access_sys_t *p_sys = p_access->p_sys;
358 if( !p_sys->p_write_list )
360 p_sys->i_write_size = 0;
364 p_next = p_sys->p_write_list->p_next;
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 );
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;
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;
379 p_sys->i_write_size = 0;
382 /*****************************************************************************
384 *****************************************************************************/
385 static void NextFileRead( access_t *p_access )
387 access_sys_t *p_sys = p_access->p_sys;
390 if( !p_sys->p_read_list ) return;
392 p_next = p_sys->p_read_list->p_next;
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;
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;
405 /*****************************************************************************
407 *****************************************************************************/
408 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
410 access_sys_t *p_sys = p_access->p_sys;
411 int i_write, i_buffer;
413 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
415 /* Open new file if necessary */
416 if( !p_sys->p_write_list )
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" );
424 if( !file && p_sys->i_files < 2 )
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) );
431 else if( !file ) return VLC_EGENERIC;
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;
442 i_buffer = __MIN( p_block->i_buffer,
443 p_sys->i_file_size - p_sys->i_write_size );
445 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
446 p_sys->p_write_list->file );
448 if( i_write > 0 ) p_sys->i_write_size += i_write;
450 //p_access->info.i_size += i_write;
451 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
453 if( i_write < i_buffer )
455 /* Looks like we're short of space */
457 if( !p_sys->p_write_list->p_next )
459 msg_Warn( p_access, "no more space, overwritting old data" );
460 NextFileRead( p_access );
461 NextFileRead( p_access );
464 /* Make sure we switch to next file in write list */
465 p_sys->i_write_size = p_sys->i_file_size;
468 p_block->p_buffer += i_write;
469 p_block->i_buffer -= i_write;
471 /* Check if we have some data left */
472 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
477 /*****************************************************************************
479 *****************************************************************************/
480 static block_t *ReadBlockFromFile( access_t *p_access )
482 access_sys_t *p_sys = p_access->p_sys;
485 if( !p_sys->p_read_list && p_sys->p_write_list )
487 /* Force switching to next write file, that should
488 * give us something to read */
489 NextFileWrite( p_access );
492 if( !p_sys->p_read_list ) return 0;
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 );
498 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
500 //p_access->info.i_size -= p_block->i_buffer;
501 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
506 /*****************************************************************************
507 * Seek: seek to a specific location in a file
508 *****************************************************************************/
509 static int Seek( access_t *p_access, int64_t i_pos )
511 //access_sys_t *p_sys = p_access->p_sys;
515 /*****************************************************************************
517 *****************************************************************************/
518 static int Control( access_t *p_access, int i_query, va_list args )
525 case ACCESS_CAN_SEEK:
526 case ACCESS_CAN_FASTSEEK:
527 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
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* );
538 pi_int = (int*)va_arg( args, int * );
542 case ACCESS_SET_PAUSE_STATE:
545 /* Forward everything else to the source access */
547 return access2_vaControl( p_access->p_source, i_query, args );
552 /*****************************************************************************
554 *****************************************************************************/
556 #define getpid() GetCurrentProcessId()
558 static char *GetTmpFilePath( access_t *p_access )
560 char *psz_dir = var_GetString( p_access, "timeshift-dir" );
561 char *psz_filename_base;
563 if( ( psz_dir != NULL ) && ( psz_dir[0] == '\0' ) )
569 if( psz_dir == NULL )
572 DWORD ret = GetTempPathW (0, NULL);
573 wchar_t wdir[ret + 3]; // can at least old "C:" + nul
574 const wchar_t *pwdir = wdir;
575 wchar_t *pwdir_free = NULL;
577 if (GetTempPathW (ret + 1, wdir) == 0)
579 pwdir_free = pwdir = _wgetcwd (NULL, 0);
584 psz_dir = FromWide (pwdir);
585 if (pwdir_free != NULL)
588 /* remove trailing antislash if any */
589 if (psz_dir[strlen (psz_dir) - 1] == '\\')
590 psz_dir[strlen (psz_dir) - 1] = '\0';
592 psz_dir = strdup( "/tmp" );
596 asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
597 psz_dir, getpid(), p_access->i_object_id );
600 return psz_filename_base;