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 *****************************************************************************/
36 #include <vlc_access.h>
37 #include <vlc_charset.h>
38 #include <vlc_input.h>
43 # include <direct.h> /* _wgetcwd */
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
52 #define GRANULARITY_TEXT N_("Timeshift granularity")
53 /// \bug [String] typo
54 #define GRANULARITY_LONGTEXT N_( "This is the size of the temporary files " \
55 "that will be used to store the timeshifted streams." )
56 #define DIR_TEXT N_("Timeshift directory")
57 #define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \
59 #define FORCE_TEXT N_("Force use of the timeshift module")
60 #define FORCE_LONGTEXT N_("Force use of the timeshift module even if the " \
61 "access declares that it can control pace or pause." )
64 set_shortname( _("Timeshift") );
65 set_description( _("Timeshift") );
66 set_category( CAT_INPUT );
67 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
68 set_capability( "access_filter", 0 );
69 add_shortcut( "timeshift" );
70 set_callbacks( Open, Close );
72 add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
73 GRANULARITY_LONGTEXT, true );
74 add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, false );
76 add_bool( "timeshift-force", false, NULL, FORCE_TEXT, FORCE_LONGTEXT,
80 /*****************************************************************************
82 *****************************************************************************/
84 static int Seek( access_t *, int64_t );
85 static block_t *Block ( access_t *p_access );
86 static int Control( access_t *, int i_query, va_list args );
87 static void Thread ( access_t *p_access );
88 static int WriteBlockToFile( access_t *p_access, block_t *p_block );
89 static block_t *ReadBlockFromFile( access_t *p_access );
90 static char *GetTmpFilePath( access_t *p_access );
92 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
93 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
94 #define TMP_FILE_MAX 256
96 typedef struct ts_entry_t
99 struct ts_entry_t *p_next;
105 block_fifo_t *p_fifo;
111 ts_entry_t *p_read_list;
112 ts_entry_t **pp_read_last;
113 ts_entry_t *p_write_list;
114 ts_entry_t **pp_write_last;
116 char *psz_filename_base;
122 /*****************************************************************************
124 *****************************************************************************/
125 static int Open( vlc_object_t *p_this )
127 access_t *p_access = (access_t*)p_this;
128 access_t *p_src = p_access->p_source;
132 var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
133 if( var_GetBool( p_access, "timeshift-force" ) )
135 msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
139 /* Only work with not pace controled access */
140 if( access_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
143 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
146 /* Refuse access that can be paused */
147 if( access_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
149 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
155 p_access->pf_read = NULL;
156 p_access->pf_block = Block;
157 p_access->pf_seek = Seek;
158 p_access->pf_control = Control;
159 p_access->info = p_src->info;
161 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
164 p_sys->p_fifo = block_FifoNew();
165 p_sys->i_write_size = 0;
169 p_sys->p_read_list = NULL;
170 p_sys->pp_read_last = &p_sys->p_read_list;
171 p_sys->p_write_list = NULL;
172 p_sys->pp_write_last = &p_sys->p_write_list;
174 var_Create( p_access, "timeshift-dir",
175 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
176 var_Create( p_access, "timeshift-granularity",
177 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
178 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
179 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
180 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
182 p_sys->psz_filename_base = GetTmpFilePath( p_access );
183 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
185 if( vlc_thread_create( p_access, "timeshift thread", Thread,
186 VLC_THREAD_PRIORITY_LOW, false ) )
188 msg_Err( p_access, "cannot spawn timeshift access thread" );
195 /*****************************************************************************
197 *****************************************************************************/
198 static void Close( vlc_object_t *p_this )
200 access_t *p_access = (access_t*)p_this;
201 access_sys_t *p_sys = p_access->p_sys;
205 msg_Dbg( p_access, "timeshift close called" );
206 vlc_thread_join( p_access );
208 for( p_entry = p_sys->p_write_list; p_entry; )
210 ts_entry_t *p_next = p_entry->p_next;
211 fclose( p_entry->file );
215 for( p_entry = p_sys->p_read_list; p_entry; )
217 ts_entry_t *p_next = p_entry->p_next;
218 fclose( p_entry->file );
222 for( i = 0; i < p_sys->i_files; i++ )
224 sprintf( p_sys->psz_filename, "%s%i.dat",
225 p_sys->psz_filename_base, i );
226 unlink( p_sys->psz_filename );
229 free( p_sys->psz_filename );
230 free( p_sys->psz_filename_base );
231 block_FifoRelease( p_sys->p_fifo );
235 /*****************************************************************************
237 *****************************************************************************/
238 static block_t *Block( access_t *p_access )
240 access_sys_t *p_sys = p_access->p_sys;
241 access_t *p_src = p_access->p_source;
242 block_t *p_block = NULL;
244 /* Update info (we probably ought to be time caching that as well) */
245 if( p_src->info.i_update & INPUT_UPDATE_META )
247 p_src->info.i_update &= ~INPUT_UPDATE_META;
248 p_access->info.i_update |= INPUT_UPDATE_META;
251 /* Get data from timeshift fifo */
252 if( !p_access->info.b_eof )
253 p_block = block_FifoGet( p_sys->p_fifo );
255 if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
256 { block_Release( p_block ); p_block = 0; }
260 p_sys->i_data -= p_block->i_buffer;
264 p_access->info.b_eof = p_src->info.b_eof;
268 /*****************************************************************************
270 *****************************************************************************/
271 static void Thread( access_t *p_access )
273 access_sys_t *p_sys = p_access->p_sys;
274 access_t *p_src = p_access->p_source;
277 while( !p_access->b_die )
279 /* Get a new block from the source */
280 if( p_src->pf_block )
282 p_block = p_src->pf_block( p_src );
286 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
289 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
291 if( p_block->i_buffer <= 0 )
293 block_Release( p_block );
298 if( p_block == NULL )
300 if( p_src->info.b_eof ) break;
305 p_sys->i_data += p_block->i_buffer;
308 if( !p_sys->p_write_list && !p_sys->p_read_list &&
309 block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MAX )
311 /* If there isn't too much timeshifted data,
312 * write directly to FIFO */
313 block_FifoPut( p_sys->p_fifo, p_block );
317 WriteBlockToFile( p_access, p_block );
318 block_Release( p_block );
320 /* Read from file to fill up the fifo */
321 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
324 p_block = ReadBlockFromFile( p_access );
325 if( !p_block ) break;
327 block_FifoPut( p_sys->p_fifo, p_block );
331 msg_Dbg( p_access, "timeshift: no more input data" );
333 while( !p_access->b_die &&
334 (p_sys->p_read_list || block_FifoSize( p_sys->p_fifo ) ) )
336 /* Read from file to fill up the fifo */
337 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
338 !p_access->b_die && p_sys->p_read_list )
340 p_block = ReadBlockFromFile( p_access );
341 if( !p_block ) break;
343 block_FifoPut( p_sys->p_fifo, p_block );
349 msg_Dbg( p_access, "timeshift: EOF" );
350 p_src->info.b_eof = true;
352 /* Send dummy packet to avoid deadlock in Block() */
353 block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
356 /*****************************************************************************
358 *****************************************************************************/
359 static void NextFileWrite( access_t *p_access )
361 access_sys_t *p_sys = p_access->p_sys;
364 if( !p_sys->p_write_list )
366 p_sys->i_write_size = 0;
370 p_next = p_sys->p_write_list->p_next;
372 /* Put written file in read list */
373 if( p_sys->i_write_size < p_sys->i_file_size )
374 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
376 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
377 *p_sys->pp_read_last = p_sys->p_write_list;
378 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
379 p_sys->p_write_list->p_next = 0;
381 /* Switch to next file to write */
382 p_sys->p_write_list = p_next;
383 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
385 p_sys->i_write_size = 0;
388 /*****************************************************************************
390 *****************************************************************************/
391 static void NextFileRead( access_t *p_access )
393 access_sys_t *p_sys = p_access->p_sys;
396 if( !p_sys->p_read_list ) return;
398 p_next = p_sys->p_read_list->p_next;
400 /* Put read file in write list */
401 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
402 *p_sys->pp_write_last = p_sys->p_read_list;
403 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
404 p_sys->p_read_list->p_next = 0;
406 /* Switch to next file to read */
407 p_sys->p_read_list = p_next;
408 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
411 /*****************************************************************************
413 *****************************************************************************/
414 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
416 access_sys_t *p_sys = p_access->p_sys;
417 int i_write, i_buffer;
419 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
421 /* Open new file if necessary */
422 if( !p_sys->p_write_list )
426 sprintf( p_sys->psz_filename, "%s%i.dat",
427 p_sys->psz_filename_base, p_sys->i_files );
428 file = utf8_fopen( p_sys->psz_filename, "w+b" );
430 if( !file && p_sys->i_files < 2 )
432 /* We just can't work with less than 2 buffer files */
433 msg_Err( p_access, "cannot open temporary file '%s' (%m)",
434 p_sys->psz_filename );
437 else if( !file ) return VLC_EGENERIC;
439 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
440 p_sys->p_write_list->p_next = 0;
441 p_sys->p_write_list->file = file;
442 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
448 i_buffer = __MIN( p_block->i_buffer,
449 p_sys->i_file_size - p_sys->i_write_size );
451 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
452 p_sys->p_write_list->file );
454 if( i_write > 0 ) p_sys->i_write_size += i_write;
456 //p_access->info.i_size += i_write;
457 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
459 if( i_write < i_buffer )
461 /* Looks like we're short of space */
463 if( !p_sys->p_write_list->p_next )
465 msg_Warn( p_access, "no more space, overwritting old data" );
466 NextFileRead( p_access );
467 NextFileRead( p_access );
470 /* Make sure we switch to next file in write list */
471 p_sys->i_write_size = p_sys->i_file_size;
474 p_block->p_buffer += i_write;
475 p_block->i_buffer -= i_write;
477 /* Check if we have some data left */
478 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
483 /*****************************************************************************
485 *****************************************************************************/
486 static block_t *ReadBlockFromFile( access_t *p_access )
488 access_sys_t *p_sys = p_access->p_sys;
491 if( !p_sys->p_read_list && p_sys->p_write_list )
493 /* Force switching to next write file, that should
494 * give us something to read */
495 NextFileWrite( p_access );
498 if( !p_sys->p_read_list ) return 0;
500 p_block = block_New( p_access, 4096 );
501 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
502 p_sys->p_read_list->file );
504 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
506 //p_access->info.i_size -= p_block->i_buffer;
507 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
512 /*****************************************************************************
513 * Seek: seek to a specific location in a file
514 *****************************************************************************/
515 static int Seek( access_t *p_access, int64_t i_pos )
517 //access_sys_t *p_sys = p_access->p_sys;
521 /*****************************************************************************
523 *****************************************************************************/
524 static int Control( access_t *p_access, int i_query, va_list args )
531 case ACCESS_CAN_SEEK:
532 case ACCESS_CAN_FASTSEEK:
533 pb_bool = (bool*)va_arg( args, bool* );
537 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
538 case ACCESS_CAN_PAUSE:
539 pb_bool = (bool*)va_arg( args, bool* );
544 pi_int = (int*)va_arg( args, int * );
548 case ACCESS_SET_PAUSE_STATE:
551 /* Forward everything else to the source access */
553 return access_vaControl( p_access->p_source, i_query, args );
558 /*****************************************************************************
560 *****************************************************************************/
562 #define getpid() (int)GetCurrentProcessId()
564 static char *GetTmpFilePath( access_t *p_access )
566 char *psz_dir = var_GetNonEmptyString( p_access, "timeshift-dir" );
567 char *psz_filename_base;
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;