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 );
67 add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, VLC_FALSE );
68 add_bool( "timeshift-force", VLC_FALSE, NULL, FORCE_TEXT, FORCE_LONGTEXT,
72 /*****************************************************************************
74 *****************************************************************************/
76 static int Seek( access_t *, int64_t );
77 static block_t *Block ( access_t *p_access );
78 static int Control( access_t *, int i_query, va_list args );
79 static void Thread ( access_t *p_access );
80 static int WriteBlockToFile( access_t *p_access, block_t *p_block );
81 static block_t *ReadBlockFromFile( access_t *p_access );
82 static char *GetTmpFilePath( access_t *p_access );
84 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
85 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
86 #define TMP_FILE_MAX 256
88 typedef struct ts_entry_t
91 struct ts_entry_t *p_next;
103 ts_entry_t *p_read_list;
104 ts_entry_t **pp_read_last;
105 ts_entry_t *p_write_list;
106 ts_entry_t **pp_write_last;
108 char *psz_filename_base;
114 /*****************************************************************************
116 *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
119 access_t *p_access = (access_t*)p_this;
120 access_t *p_src = p_access->p_source;
124 var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
125 if( var_GetBool( p_access, "timeshift-force" ) )
127 msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
131 /* Only work with not pace controled access */
132 if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
135 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
138 /* Refuse access that can be paused */
139 if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
141 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
147 p_access->pf_read = NULL;
148 p_access->pf_block = Block;
149 p_access->pf_seek = Seek;
150 p_access->pf_control = Control;
151 p_access->info = p_src->info;
153 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
156 p_sys->p_fifo = block_FifoNew( p_access );
157 p_sys->i_write_size = 0;
161 p_sys->p_read_list = NULL;
162 p_sys->pp_read_last = &p_sys->p_read_list;
163 p_sys->p_write_list = NULL;
164 p_sys->pp_write_last = &p_sys->p_write_list;
166 var_Create( p_access, "timeshift-dir",
167 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
168 var_Create( p_access, "timeshift-granularity",
169 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
170 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
171 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
172 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
174 p_sys->psz_filename_base = GetTmpFilePath( p_access );
175 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
177 if( vlc_thread_create( p_access, "timeshift thread", Thread,
178 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
180 msg_Err( p_access, "cannot spawn timeshift access thread" );
187 /*****************************************************************************
189 *****************************************************************************/
190 static void Close( vlc_object_t *p_this )
192 access_t *p_access = (access_t*)p_this;
193 access_sys_t *p_sys = p_access->p_sys;
197 msg_Dbg( p_access, "timeshift close called" );
198 vlc_thread_join( p_access );
200 for( p_entry = p_sys->p_write_list; p_entry; )
202 ts_entry_t *p_next = p_entry->p_next;
203 fclose( p_entry->file );
207 for( p_entry = p_sys->p_read_list; p_entry; )
209 ts_entry_t *p_next = p_entry->p_next;
210 fclose( p_entry->file );
214 for( i = 0; i < p_sys->i_files; i++ )
216 sprintf( p_sys->psz_filename, "%s%i.dat",
217 p_sys->psz_filename_base, i );
218 unlink( p_sys->psz_filename );
221 free( p_sys->psz_filename );
222 free( p_sys->psz_filename_base );
223 block_FifoRelease( p_sys->p_fifo );
227 /*****************************************************************************
229 *****************************************************************************/
230 static block_t *Block( access_t *p_access )
232 access_sys_t *p_sys = p_access->p_sys;
233 access_t *p_src = p_access->p_source;
234 block_t *p_block = NULL;
236 /* Update info (we probably ought to be time caching that as well) */
237 if( p_src->info.i_update & INPUT_UPDATE_META )
239 p_src->info.i_update &= ~INPUT_UPDATE_META;
240 p_access->info.i_update |= INPUT_UPDATE_META;
243 /* Get data from timeshift fifo */
244 if( !p_access->info.b_eof )
245 p_block = block_FifoGet( p_sys->p_fifo );
247 if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
248 { block_Release( p_block ); p_block = 0; }
252 p_sys->i_data -= p_block->i_buffer;
256 p_access->info.b_eof = p_src->info.b_eof;
260 /*****************************************************************************
262 *****************************************************************************/
263 static void Thread( access_t *p_access )
265 access_sys_t *p_sys = p_access->p_sys;
266 access_t *p_src = p_access->p_source;
269 while( !p_access->b_die )
271 /* Get a new block from the source */
272 if( p_src->pf_block )
274 p_block = p_src->pf_block( p_src );
278 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
281 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
283 if( p_block->i_buffer <= 0 )
285 block_Release( p_block );
290 if( p_block == NULL )
292 if( p_src->info.b_eof ) break;
297 p_sys->i_data += p_block->i_buffer;
300 if( !p_sys->p_write_list && !p_sys->p_read_list &&
301 p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
303 /* If there isn't too much timeshifted data,
304 * write directly to FIFO */
305 block_FifoPut( p_sys->p_fifo, p_block );
309 WriteBlockToFile( p_access, p_block );
310 block_Release( p_block );
312 /* Read from file to fill up the fifo */
313 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
316 p_block = ReadBlockFromFile( p_access );
317 if( !p_block ) break;
319 block_FifoPut( p_sys->p_fifo, p_block );
323 msg_Dbg( p_access, "timeshift: no more input data" );
325 while( !p_access->b_die &&
326 (p_sys->p_read_list || p_sys->p_fifo->i_size) )
328 /* Read from file to fill up the fifo */
329 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
330 !p_access->b_die && p_sys->p_read_list )
332 p_block = ReadBlockFromFile( p_access );
333 if( !p_block ) break;
335 block_FifoPut( p_sys->p_fifo, p_block );
341 msg_Dbg( p_access, "timeshift: EOF" );
342 p_src->info.b_eof = VLC_TRUE;
344 /* Send dummy packet to avoid deadlock in Block() */
345 block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
348 /*****************************************************************************
350 *****************************************************************************/
351 static void NextFileWrite( access_t *p_access )
353 access_sys_t *p_sys = p_access->p_sys;
356 if( !p_sys->p_write_list )
358 p_sys->i_write_size = 0;
362 p_next = p_sys->p_write_list->p_next;
364 /* Put written file in read list */
365 if( p_sys->i_write_size < p_sys->i_file_size )
366 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
368 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
369 *p_sys->pp_read_last = p_sys->p_write_list;
370 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
371 p_sys->p_write_list->p_next = 0;
373 /* Switch to next file to write */
374 p_sys->p_write_list = p_next;
375 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
377 p_sys->i_write_size = 0;
380 /*****************************************************************************
382 *****************************************************************************/
383 static void NextFileRead( access_t *p_access )
385 access_sys_t *p_sys = p_access->p_sys;
388 if( !p_sys->p_read_list ) return;
390 p_next = p_sys->p_read_list->p_next;
392 /* Put read file in write list */
393 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
394 *p_sys->pp_write_last = p_sys->p_read_list;
395 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
396 p_sys->p_read_list->p_next = 0;
398 /* Switch to next file to read */
399 p_sys->p_read_list = p_next;
400 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
403 /*****************************************************************************
405 *****************************************************************************/
406 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
408 access_sys_t *p_sys = p_access->p_sys;
409 int i_write, i_buffer;
411 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
413 /* Open new file if necessary */
414 if( !p_sys->p_write_list )
418 sprintf( p_sys->psz_filename, "%s%i.dat",
419 p_sys->psz_filename_base, p_sys->i_files );
420 file = utf8_fopen( p_sys->psz_filename, "w+b" );
422 if( !file && p_sys->i_files < 2 )
424 /* We just can't work with less than 2 buffer files */
425 msg_Err( p_access, "cannot open temporary file '%s' (%s)",
426 p_sys->psz_filename, strerror(errno) );
429 else if( !file ) return VLC_EGENERIC;
431 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
432 p_sys->p_write_list->p_next = 0;
433 p_sys->p_write_list->file = file;
434 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
440 i_buffer = __MIN( p_block->i_buffer,
441 p_sys->i_file_size - p_sys->i_write_size );
443 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
444 p_sys->p_write_list->file );
446 if( i_write > 0 ) p_sys->i_write_size += i_write;
448 //p_access->info.i_size += i_write;
449 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
451 if( i_write < i_buffer )
453 /* Looks like we're short of space */
455 if( !p_sys->p_write_list->p_next )
457 msg_Warn( p_access, "no more space, overwritting old data" );
458 NextFileRead( p_access );
459 NextFileRead( p_access );
462 /* Make sure we switch to next file in write list */
463 p_sys->i_write_size = p_sys->i_file_size;
466 p_block->p_buffer += i_write;
467 p_block->i_buffer -= i_write;
469 /* Check if we have some data left */
470 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
475 /*****************************************************************************
477 *****************************************************************************/
478 static block_t *ReadBlockFromFile( access_t *p_access )
480 access_sys_t *p_sys = p_access->p_sys;
483 if( !p_sys->p_read_list && p_sys->p_write_list )
485 /* Force switching to next write file, that should
486 * give us something to read */
487 NextFileWrite( p_access );
490 if( !p_sys->p_read_list ) return 0;
492 p_block = block_New( p_access, 4096 );
493 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
494 p_sys->p_read_list->file );
496 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
498 //p_access->info.i_size -= p_block->i_buffer;
499 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
504 /*****************************************************************************
505 * Seek: seek to a specific location in a file
506 *****************************************************************************/
507 static int Seek( access_t *p_access, int64_t i_pos )
509 //access_sys_t *p_sys = p_access->p_sys;
513 /*****************************************************************************
515 *****************************************************************************/
516 static int Control( access_t *p_access, int i_query, va_list args )
523 case ACCESS_CAN_SEEK:
524 case ACCESS_CAN_FASTSEEK:
525 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
529 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
530 case ACCESS_CAN_PAUSE:
531 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
536 pi_int = (int*)va_arg( args, int * );
540 case ACCESS_SET_PAUSE_STATE:
543 /* Forward everything else to the source access */
545 return access2_vaControl( p_access->p_source, i_query, args );
550 /*****************************************************************************
552 *****************************************************************************/
554 #define getpid() GetCurrentProcessId()
556 static char *GetTmpFilePath( access_t *p_access )
558 char *psz_dir = var_GetString( p_access, "timeshift-dir" );
559 char *psz_filename_base;
561 if( ( psz_dir != NULL ) && ( psz_dir[0] == '\0' ) )
567 if( psz_dir == NULL )
570 DWORD ret = GetTempPathW (0, NULL);
571 wchar_t wdir[ret + 3]; // can at least old "C:" + nul
572 const wchar_t *pwdir = wdir;
573 wchar_t *pwdir_free = NULL;
575 if (GetTempPathW (ret + 1, wdir) == 0)
577 pwdir_free = pwdir = _wgetcwd (NULL, 0);
582 psz_dir = FromWide (pwdir);
583 if (pwdir_free != NULL)
586 /* remove trailing antislash if any */
587 if (psz_dir[strlen (psz_dir) - 1] == '\\')
588 psz_dir[strlen (psz_dir) - 1] = '\0';
590 psz_dir = strdup( "/tmp" );
594 asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
595 psz_dir, getpid(), p_access->i_object_id );
598 return psz_filename_base;