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 *****************************************************************************/
33 #include <vlc_access.h>
34 #include <vlc_charset.h>
35 #include <vlc_input.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
45 #define GRANULARITY_TEXT N_("Timeshift granularity")
46 /// \bug [String] typo
47 #define GRANULARITY_LONGTEXT N_( "This is the size of the temporary files " \
48 "that will be used to store the timeshifted streams." )
49 #define DIR_TEXT N_("Timeshift directory")
50 #define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \
52 #define FORCE_TEXT N_("Force use of the timeshift module")
53 #define FORCE_LONGTEXT N_("Force use of the timeshift module even if the " \
54 "access declares that it can control pace or pause." )
57 set_shortname( _("Timeshift") );
58 set_description( _("Timeshift") );
59 set_category( CAT_INPUT );
60 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
61 set_capability( "access_filter", 0 );
62 add_shortcut( "timeshift" );
63 set_callbacks( Open, Close );
65 add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
66 GRANULARITY_LONGTEXT, VLC_TRUE );
69 add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, VLC_FALSE );
71 add_bool( "timeshift-force", VLC_FALSE, NULL, FORCE_TEXT, FORCE_LONGTEXT,
77 /*****************************************************************************
79 *****************************************************************************/
81 static int Seek( access_t *, int64_t );
82 static block_t *Block ( access_t *p_access );
83 static int Control( access_t *, int i_query, va_list args );
84 static void Thread ( access_t *p_access );
85 static int WriteBlockToFile( access_t *p_access, block_t *p_block );
86 static block_t *ReadBlockFromFile( access_t *p_access );
87 static char *GetTmpFilePath( access_t *p_access );
89 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
90 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
91 #define TMP_FILE_MAX 256
93 typedef struct ts_entry_t
96 struct ts_entry_t *p_next;
102 block_fifo_t *p_fifo;
108 ts_entry_t *p_read_list;
109 ts_entry_t **pp_read_last;
110 ts_entry_t *p_write_list;
111 ts_entry_t **pp_write_last;
113 char *psz_filename_base;
119 /*****************************************************************************
121 *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
124 access_t *p_access = (access_t*)p_this;
125 access_t *p_src = p_access->p_source;
129 var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
130 if( var_GetBool( p_access, "timeshift-force" ) )
132 msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
136 /* Only work with not pace controled access */
137 if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
140 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
143 /* Refuse access that can be paused */
144 if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
146 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
152 p_access->pf_read = NULL;
153 p_access->pf_block = Block;
154 p_access->pf_seek = Seek;
155 p_access->pf_control = Control;
156 p_access->info = p_src->info;
158 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
161 p_sys->p_fifo = block_FifoNew( p_access );
162 p_sys->i_write_size = 0;
166 p_sys->p_read_list = NULL;
167 p_sys->pp_read_last = &p_sys->p_read_list;
168 p_sys->p_write_list = NULL;
169 p_sys->pp_write_last = &p_sys->p_write_list;
171 var_Create( p_access, "timeshift-dir",
172 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
173 var_Create( p_access, "timeshift-granularity",
174 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
175 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
176 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
177 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
179 p_sys->psz_filename_base = GetTmpFilePath( p_access );
180 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
182 if( vlc_thread_create( p_access, "timeshift thread", Thread,
183 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
185 msg_Err( p_access, "cannot spawn timeshift access thread" );
192 /*****************************************************************************
194 *****************************************************************************/
195 static void Close( vlc_object_t *p_this )
197 access_t *p_access = (access_t*)p_this;
198 access_sys_t *p_sys = p_access->p_sys;
202 msg_Dbg( p_access, "timeshift close called" );
203 vlc_thread_join( p_access );
205 for( p_entry = p_sys->p_write_list; p_entry; )
207 ts_entry_t *p_next = p_entry->p_next;
208 fclose( p_entry->file );
212 for( p_entry = p_sys->p_read_list; p_entry; )
214 ts_entry_t *p_next = p_entry->p_next;
215 fclose( p_entry->file );
219 for( i = 0; i < p_sys->i_files; i++ )
221 sprintf( p_sys->psz_filename, "%s%i.dat",
222 p_sys->psz_filename_base, i );
223 unlink( p_sys->psz_filename );
226 free( p_sys->psz_filename );
227 free( p_sys->psz_filename_base );
228 block_FifoRelease( p_sys->p_fifo );
232 /*****************************************************************************
234 *****************************************************************************/
235 static block_t *Block( access_t *p_access )
237 access_sys_t *p_sys = p_access->p_sys;
238 access_t *p_src = p_access->p_source;
239 block_t *p_block = NULL;
241 /* Update info (we probably ought to be time caching that as well) */
242 if( p_src->info.i_update & INPUT_UPDATE_META )
244 p_src->info.i_update &= ~INPUT_UPDATE_META;
245 p_access->info.i_update |= INPUT_UPDATE_META;
248 /* Get data from timeshift fifo */
249 if( !p_access->info.b_eof )
250 p_block = block_FifoGet( p_sys->p_fifo );
252 if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
253 { block_Release( p_block ); p_block = 0; }
257 p_sys->i_data -= p_block->i_buffer;
261 p_access->info.b_eof = p_src->info.b_eof;
265 /*****************************************************************************
267 *****************************************************************************/
268 static void Thread( access_t *p_access )
270 access_sys_t *p_sys = p_access->p_sys;
271 access_t *p_src = p_access->p_source;
274 while( !p_access->b_die )
276 /* Get a new block from the source */
277 if( p_src->pf_block )
279 p_block = p_src->pf_block( p_src );
283 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
286 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
288 if( p_block->i_buffer <= 0 )
290 block_Release( p_block );
295 if( p_block == NULL )
297 if( p_src->info.b_eof ) break;
302 p_sys->i_data += p_block->i_buffer;
305 if( !p_sys->p_write_list && !p_sys->p_read_list &&
306 block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MAX )
308 /* If there isn't too much timeshifted data,
309 * write directly to FIFO */
310 block_FifoPut( p_sys->p_fifo, p_block );
314 WriteBlockToFile( p_access, p_block );
315 block_Release( p_block );
317 /* Read from file to fill up the fifo */
318 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
321 p_block = ReadBlockFromFile( p_access );
322 if( !p_block ) break;
324 block_FifoPut( p_sys->p_fifo, p_block );
328 msg_Dbg( p_access, "timeshift: no more input data" );
330 while( !p_access->b_die &&
331 (p_sys->p_read_list || block_FifoSize( p_sys->p_fifo ) ) )
333 /* Read from file to fill up the fifo */
334 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
335 !p_access->b_die && p_sys->p_read_list )
337 p_block = ReadBlockFromFile( p_access );
338 if( !p_block ) break;
340 block_FifoPut( p_sys->p_fifo, p_block );
346 msg_Dbg( p_access, "timeshift: EOF" );
347 p_src->info.b_eof = VLC_TRUE;
349 /* Send dummy packet to avoid deadlock in Block() */
350 block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
353 /*****************************************************************************
355 *****************************************************************************/
356 static void NextFileWrite( access_t *p_access )
358 access_sys_t *p_sys = p_access->p_sys;
361 if( !p_sys->p_write_list )
363 p_sys->i_write_size = 0;
367 p_next = p_sys->p_write_list->p_next;
369 /* Put written file in read list */
370 if( p_sys->i_write_size < p_sys->i_file_size )
371 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
373 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
374 *p_sys->pp_read_last = p_sys->p_write_list;
375 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
376 p_sys->p_write_list->p_next = 0;
378 /* Switch to next file to write */
379 p_sys->p_write_list = p_next;
380 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
382 p_sys->i_write_size = 0;
385 /*****************************************************************************
387 *****************************************************************************/
388 static void NextFileRead( access_t *p_access )
390 access_sys_t *p_sys = p_access->p_sys;
393 if( !p_sys->p_read_list ) return;
395 p_next = p_sys->p_read_list->p_next;
397 /* Put read file in write list */
398 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
399 *p_sys->pp_write_last = p_sys->p_read_list;
400 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
401 p_sys->p_read_list->p_next = 0;
403 /* Switch to next file to read */
404 p_sys->p_read_list = p_next;
405 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
408 /*****************************************************************************
410 *****************************************************************************/
411 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
413 access_sys_t *p_sys = p_access->p_sys;
414 int i_write, i_buffer;
416 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
418 /* Open new file if necessary */
419 if( !p_sys->p_write_list )
423 sprintf( p_sys->psz_filename, "%s%i.dat",
424 p_sys->psz_filename_base, p_sys->i_files );
425 file = utf8_fopen( p_sys->psz_filename, "w+b" );
427 if( !file && p_sys->i_files < 2 )
429 /* We just can't work with less than 2 buffer files */
430 msg_Err( p_access, "cannot open temporary file '%s' (%m)",
431 p_sys->psz_filename );
434 else if( !file ) return VLC_EGENERIC;
436 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
437 p_sys->p_write_list->p_next = 0;
438 p_sys->p_write_list->file = file;
439 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
445 i_buffer = __MIN( p_block->i_buffer,
446 p_sys->i_file_size - p_sys->i_write_size );
448 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
449 p_sys->p_write_list->file );
451 if( i_write > 0 ) p_sys->i_write_size += i_write;
453 //p_access->info.i_size += i_write;
454 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
456 if( i_write < i_buffer )
458 /* Looks like we're short of space */
460 if( !p_sys->p_write_list->p_next )
462 msg_Warn( p_access, "no more space, overwritting old data" );
463 NextFileRead( p_access );
464 NextFileRead( p_access );
467 /* Make sure we switch to next file in write list */
468 p_sys->i_write_size = p_sys->i_file_size;
471 p_block->p_buffer += i_write;
472 p_block->i_buffer -= i_write;
474 /* Check if we have some data left */
475 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
480 /*****************************************************************************
482 *****************************************************************************/
483 static block_t *ReadBlockFromFile( access_t *p_access )
485 access_sys_t *p_sys = p_access->p_sys;
488 if( !p_sys->p_read_list && p_sys->p_write_list )
490 /* Force switching to next write file, that should
491 * give us something to read */
492 NextFileWrite( p_access );
495 if( !p_sys->p_read_list ) return 0;
497 p_block = block_New( p_access, 4096 );
498 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
499 p_sys->p_read_list->file );
501 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
503 //p_access->info.i_size -= p_block->i_buffer;
504 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
509 /*****************************************************************************
510 * Seek: seek to a specific location in a file
511 *****************************************************************************/
512 static int Seek( access_t *p_access, int64_t i_pos )
514 //access_sys_t *p_sys = p_access->p_sys;
518 /*****************************************************************************
520 *****************************************************************************/
521 static int Control( access_t *p_access, int i_query, va_list args )
528 case ACCESS_CAN_SEEK:
529 case ACCESS_CAN_FASTSEEK:
530 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
534 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
535 case ACCESS_CAN_PAUSE:
536 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
541 pi_int = (int*)va_arg( args, int * );
545 case ACCESS_SET_PAUSE_STATE:
548 /* Forward everything else to the source access */
550 return access2_vaControl( p_access->p_source, i_query, args );
555 /*****************************************************************************
557 *****************************************************************************/
559 #define getpid() GetCurrentProcessId()
561 static char *GetTmpFilePath( access_t *p_access )
563 char *psz_dir = var_GetNonEmptyString( p_access, "timeshift-dir" );
564 char *psz_filename_base;
566 if( psz_dir == NULL )
569 DWORD ret = GetTempPathW (0, NULL);
570 wchar_t wdir[ret + 3]; // can at least old "C:" + nul
571 const wchar_t *pwdir = wdir;
572 wchar_t *pwdir_free = NULL;
574 if (GetTempPathW (ret + 1, wdir) == 0)
576 pwdir_free = pwdir = _wgetcwd (NULL, 0);
581 psz_dir = FromWide (pwdir);
582 if (pwdir_free != NULL)
585 /* remove trailing antislash if any */
586 if (psz_dir[strlen (psz_dir) - 1] == '\\')
587 psz_dir[strlen (psz_dir) - 1] = '\0';
589 psz_dir = strdup( "/tmp" );
593 asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
594 psz_dir, getpid(), p_access->i_object_id );
597 return psz_filename_base;