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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
37 #include <vlc_access.h>
38 #include <vlc_charset.h>
39 #include <vlc_input.h>
44 # include <direct.h> /* _wgetcwd */
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
53 #define GRANULARITY_TEXT N_("Timeshift granularity")
54 /// \bug [String] typo
55 #define GRANULARITY_LONGTEXT N_( "This is the size of the temporary files " \
56 "that will be used to store the timeshifted streams." )
57 #define DIR_TEXT N_("Timeshift directory")
58 #define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \
60 #define FORCE_TEXT N_("Force use of the timeshift module")
61 #define FORCE_LONGTEXT N_("Force use of the timeshift module even if the " \
62 "access declares that it can control pace or pause." )
65 set_shortname( N_("Timeshift") );
66 set_description( N_("Timeshift") );
67 set_category( CAT_INPUT );
68 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
69 set_capability( "access_filter", 0 );
70 add_shortcut( "timeshift" );
71 set_callbacks( Open, Close );
73 add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
74 GRANULARITY_LONGTEXT, true );
75 add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, false );
77 add_bool( "timeshift-force", false, NULL, FORCE_TEXT, FORCE_LONGTEXT,
81 /*****************************************************************************
83 *****************************************************************************/
85 static int Seek( access_t *, int64_t );
86 static block_t *Block ( access_t *p_access );
87 static int Control( access_t *, int i_query, va_list args );
88 static void* Thread ( vlc_object_t *p_this );
89 static int WriteBlockToFile( access_t *p_access, block_t *p_block );
90 static block_t *ReadBlockFromFile( access_t *p_access );
91 static char *GetTmpFilePath( access_t *p_access );
93 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
94 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
95 #define TMP_FILE_MAX 256
97 typedef struct ts_entry_t
100 struct ts_entry_t *p_next;
106 block_fifo_t *p_fifo;
109 unsigned i_file_size;
110 unsigned i_write_size;
112 ts_entry_t *p_read_list;
113 ts_entry_t **pp_read_last;
114 ts_entry_t *p_write_list;
115 ts_entry_t **pp_write_last;
117 char *psz_filename_base;
123 /*****************************************************************************
125 *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
128 access_t *p_access = (access_t*)p_this;
129 access_t *p_src = p_access->p_source;
133 var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
134 if( var_GetBool( p_access, "timeshift-force" ) )
136 msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
140 /* Only work with not pace controled access */
141 if( access_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
144 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
147 /* Refuse access that can be paused */
148 if( access_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
150 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
156 p_access->pf_read = NULL;
157 p_access->pf_block = Block;
158 p_access->pf_seek = Seek;
159 p_access->pf_control = Control;
160 p_access->info = p_src->info;
162 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
167 p_sys->p_fifo = block_FifoNew();
168 p_sys->i_write_size = 0;
172 p_sys->p_read_list = NULL;
173 p_sys->pp_read_last = &p_sys->p_read_list;
174 p_sys->p_write_list = NULL;
175 p_sys->pp_write_last = &p_sys->p_write_list;
177 var_Create( p_access, "timeshift-dir",
178 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
179 var_Create( p_access, "timeshift-granularity",
180 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
181 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
182 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
183 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
185 p_sys->psz_filename_base = GetTmpFilePath( p_access );
186 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
188 if( vlc_thread_create( p_access, "timeshift thread", Thread,
189 VLC_THREAD_PRIORITY_LOW, false ) )
192 msg_Err( p_access, "cannot spawn timeshift access thread" );
199 /*****************************************************************************
201 *****************************************************************************/
202 static void Close( vlc_object_t *p_this )
204 access_t *p_access = (access_t*)p_this;
205 access_sys_t *p_sys = p_access->p_sys;
209 msg_Dbg( p_access, "timeshift close called" );
210 vlc_thread_join( p_access );
212 for( p_entry = p_sys->p_write_list; p_entry; )
214 ts_entry_t *p_next = p_entry->p_next;
215 fclose( p_entry->file );
219 for( p_entry = p_sys->p_read_list; p_entry; )
221 ts_entry_t *p_next = p_entry->p_next;
222 fclose( p_entry->file );
226 for( i = 0; i < p_sys->i_files; i++ )
228 sprintf( p_sys->psz_filename, "%s%i.dat",
229 p_sys->psz_filename_base, i );
230 unlink( p_sys->psz_filename );
233 free( p_sys->psz_filename );
234 free( p_sys->psz_filename_base );
235 block_FifoRelease( p_sys->p_fifo );
239 /*****************************************************************************
241 *****************************************************************************/
242 static block_t *Block( access_t *p_access )
244 access_sys_t *p_sys = p_access->p_sys;
245 access_t *p_src = p_access->p_source;
246 block_t *p_block = NULL;
248 /* Update info (we probably ought to be time caching that as well) */
249 if( p_src->info.i_update & INPUT_UPDATE_META )
251 p_src->info.i_update &= ~INPUT_UPDATE_META;
252 p_access->info.i_update |= INPUT_UPDATE_META;
255 /* Get data from timeshift fifo */
256 if( !p_access->info.b_eof )
257 p_block = block_FifoGet( p_sys->p_fifo );
259 if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
260 { block_Release( p_block ); p_block = 0; }
264 p_sys->i_data -= p_block->i_buffer;
268 p_access->info.b_eof = p_src->info.b_eof;
272 /*****************************************************************************
274 *****************************************************************************/
275 static void* Thread( vlc_object_t* p_this )
277 access_t *p_access = (access_t*)p_this;
278 access_sys_t *p_sys = p_access->p_sys;
279 access_t *p_src = p_access->p_source;
282 while( vlc_object_alive (p_access) )
284 /* Get a new block from the source */
285 if( p_src->pf_block )
287 p_block = p_src->pf_block( p_src );
293 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
295 i_read = p_src->pf_read( p_src, p_block->p_buffer, 2048 );
298 block_Release( p_block );
301 p_block->i_buffer = i_read;
304 if( p_block == NULL )
306 if( p_src->info.b_eof ) break;
311 p_sys->i_data += p_block->i_buffer;
314 if( !p_sys->p_write_list && !p_sys->p_read_list &&
315 block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MAX )
317 /* If there isn't too much timeshifted data,
318 * write directly to FIFO */
319 block_FifoPut( p_sys->p_fifo, p_block );
323 WriteBlockToFile( p_access, p_block );
324 block_Release( p_block );
326 /* Read from file to fill up the fifo */
327 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
328 vlc_object_alive (p_access) )
330 p_block = ReadBlockFromFile( p_access );
331 if( !p_block ) break;
333 block_FifoPut( p_sys->p_fifo, p_block );
337 msg_Dbg( p_access, "timeshift: no more input data" );
339 while( vlc_object_alive (p_access) &&
340 (p_sys->p_read_list || block_FifoSize( p_sys->p_fifo ) ) )
342 /* Read from file to fill up the fifo */
343 while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
344 vlc_object_alive (p_access) && p_sys->p_read_list )
346 p_block = ReadBlockFromFile( p_access );
347 if( !p_block ) break;
349 block_FifoPut( p_sys->p_fifo, p_block );
355 msg_Dbg( p_access, "timeshift: EOF" );
356 p_src->info.b_eof = true;
358 /* Send dummy packet to avoid deadlock in Block() */
359 block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
363 /*****************************************************************************
365 *****************************************************************************/
366 static void NextFileWrite( access_t *p_access )
368 access_sys_t *p_sys = p_access->p_sys;
371 if( !p_sys->p_write_list )
373 p_sys->i_write_size = 0;
377 p_next = p_sys->p_write_list->p_next;
379 /* Put written file in read list */
380 if( p_sys->i_write_size < p_sys->i_file_size )
381 if( ftruncate( fileno( p_sys->p_write_list->file ),
382 p_sys->i_write_size ) == -1 )
384 msg_Dbg( p_access, "unable to truncate file: %m" );
388 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
389 *p_sys->pp_read_last = p_sys->p_write_list;
390 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
391 p_sys->p_write_list->p_next = 0;
393 /* Switch to next file to write */
394 p_sys->p_write_list = p_next;
395 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
397 p_sys->i_write_size = 0;
400 /*****************************************************************************
402 *****************************************************************************/
403 static void NextFileRead( access_t *p_access )
405 access_sys_t *p_sys = p_access->p_sys;
408 if( !p_sys->p_read_list ) return;
410 p_next = p_sys->p_read_list->p_next;
412 /* Put read file in write list */
413 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
414 *p_sys->pp_write_last = p_sys->p_read_list;
415 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
416 p_sys->p_read_list->p_next = 0;
418 /* Switch to next file to read */
419 p_sys->p_read_list = p_next;
420 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
423 /*****************************************************************************
425 *****************************************************************************/
426 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
428 access_sys_t *p_sys = p_access->p_sys;
429 int i_write, i_buffer;
431 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
433 /* Open new file if necessary */
434 if( !p_sys->p_write_list )
438 sprintf( p_sys->psz_filename, "%s%u.dat",
439 p_sys->psz_filename_base, p_sys->i_files );
440 file = utf8_fopen( p_sys->psz_filename, "w+b" );
442 if( !file && p_sys->i_files < 2 )
444 /* We just can't work with less than 2 buffer files */
445 msg_Err( p_access, "cannot open temporary file '%s' (%m)",
446 p_sys->psz_filename );
449 else if( !file ) return VLC_EGENERIC;
451 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
452 p_sys->p_write_list->p_next = 0;
453 p_sys->p_write_list->file = file;
454 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
460 i_buffer = __MIN( p_block->i_buffer,
461 p_sys->i_file_size - p_sys->i_write_size );
463 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
464 p_sys->p_write_list->file );
466 if( i_write > 0 ) p_sys->i_write_size += i_write;
468 //p_access->info.i_size += i_write;
469 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
471 if( i_write < i_buffer )
473 /* Looks like we're short of space */
475 if( !p_sys->p_write_list->p_next )
477 msg_Warn( p_access, "no more space, overwritting old data" );
478 NextFileRead( p_access );
479 NextFileRead( p_access );
482 /* Make sure we switch to next file in write list */
483 p_sys->i_write_size = p_sys->i_file_size;
486 p_block->p_buffer += i_write;
487 p_block->i_buffer -= i_write;
489 /* Check if we have some data left */
490 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
495 /*****************************************************************************
497 *****************************************************************************/
498 static block_t *ReadBlockFromFile( access_t *p_access )
500 access_sys_t *p_sys = p_access->p_sys;
503 if( !p_sys->p_read_list && p_sys->p_write_list )
505 /* Force switching to next write file, that should
506 * give us something to read */
507 NextFileWrite( p_access );
510 if( !p_sys->p_read_list ) return 0;
512 p_block = block_New( p_access, 4096 );
513 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
514 p_sys->p_read_list->file );
516 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
518 //p_access->info.i_size -= p_block->i_buffer;
519 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
524 /*****************************************************************************
525 * Seek: seek to a specific location in a file
526 *****************************************************************************/
527 static int Seek( access_t *p_access, int64_t i_pos )
529 //access_sys_t *p_sys = p_access->p_sys;
535 /*****************************************************************************
537 *****************************************************************************/
538 static int Control( access_t *p_access, int i_query, va_list args )
545 case ACCESS_CAN_SEEK:
546 case ACCESS_CAN_FASTSEEK:
547 pb_bool = (bool*)va_arg( args, bool* );
551 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
552 case ACCESS_CAN_PAUSE:
553 pb_bool = (bool*)va_arg( args, bool* );
558 pi_int = (int*)va_arg( args, int * );
562 case ACCESS_SET_PAUSE_STATE:
565 /* Forward everything else to the source access */
567 return access_vaControl( p_access->p_source, i_query, args );
572 /*****************************************************************************
574 *****************************************************************************/
576 #define getpid() (int)GetCurrentProcessId()
578 static char *GetTmpFilePath( access_t *p_access )
580 char *psz_dir = var_GetNonEmptyString( p_access, "timeshift-dir" );
581 char *psz_filename_base;
583 if( psz_dir == NULL )
586 DWORD ret = GetTempPathW (0, NULL);
587 wchar_t wdir[ret + 3]; // can at least old "C:" + nul
588 const wchar_t *pwdir = wdir;
589 wchar_t *pwdir_free = NULL;
591 if (GetTempPathW (ret + 1, wdir) == 0)
593 pwdir_free = pwdir = _wgetcwd (NULL, 0);
598 psz_dir = FromWide (pwdir);
599 if (pwdir_free != NULL)
602 /* remove trailing antislash if any */
603 if (psz_dir[strlen (psz_dir) - 1] == '\\')
604 psz_dir[strlen (psz_dir) - 1] = '\0';
606 psz_dir = strdup( "/tmp" );
610 if( asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
611 psz_dir, getpid(), p_access->i_object_id ) == -1 )
612 psz_filename_base = NULL;
615 return psz_filename_base;