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/input.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 #define GRANULARITY_TEXT N_("Timeshift granularity")
47 /// \bug [String] typo
48 #define GRANULARITY_LONGTEXT N_( "This is the size of the temporary files " \
49 "that will be used to store the timeshifted streams." )
50 #define DIR_TEXT N_("Timeshift directory")
51 #define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \
53 #define FORCE_TEXT N_("Force use of the timeshift module")
54 #define FORCE_LONGTEXT N_("Force use of the timeshift module even if the " \
55 "access declares that it can control pace or pause." )
58 set_shortname( _("Timeshift") );
59 set_description( _("Timeshift") );
60 set_category( CAT_INPUT );
61 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
62 set_capability( "access_filter", 0 );
63 add_shortcut( "timeshift" );
64 set_callbacks( Open, Close );
66 add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
67 GRANULARITY_LONGTEXT, VLC_TRUE );
68 add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, VLC_FALSE );
69 add_bool( "timeshift-force", VLC_FALSE, NULL, FORCE_TEXT, FORCE_LONGTEXT,
73 /*****************************************************************************
75 *****************************************************************************/
77 static int Seek( access_t *, int64_t );
78 static block_t *Block ( access_t *p_access );
79 static int Control( access_t *, int i_query, va_list args );
80 static void Thread ( access_t *p_access );
81 static int WriteBlockToFile( access_t *p_access, block_t *p_block );
82 static block_t *ReadBlockFromFile( access_t *p_access );
83 static char *GetTmpFilePath( access_t *p_access );
85 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
86 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
87 #define TMP_FILE_MAX 256
89 typedef struct ts_entry_t
92 struct ts_entry_t *p_next;
104 ts_entry_t *p_read_list;
105 ts_entry_t **pp_read_last;
106 ts_entry_t *p_write_list;
107 ts_entry_t **pp_write_last;
109 char *psz_filename_base;
113 /*****************************************************************************
115 *****************************************************************************/
116 static int Open( vlc_object_t *p_this )
118 access_t *p_access = (access_t*)p_this;
119 access_t *p_src = p_access->p_source;
123 var_Create( p_access, "timeshift-force",
124 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
125 if( var_GetBool( p_access, "timeshift-force" ) == VLC_TRUE )
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;
160 p_sys->p_read_list = NULL;
161 p_sys->pp_read_last = &p_sys->p_read_list;
162 p_sys->p_write_list = NULL;
163 p_sys->pp_write_last = &p_sys->p_write_list;
165 var_Create( p_access, "timeshift-dir",
166 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
167 var_Create( p_access, "timeshift-granularity",
168 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
169 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
170 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
171 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
173 p_sys->psz_filename_base = GetTmpFilePath( p_access );
174 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
176 if( vlc_thread_create( p_access, "timeshift thread", Thread,
177 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
179 msg_Err( p_access, "cannot spawn timeshift access thread" );
186 /*****************************************************************************
188 *****************************************************************************/
189 static void Close( vlc_object_t *p_this )
191 access_t *p_access = (access_t*)p_this;
192 access_sys_t *p_sys = p_access->p_sys;
196 msg_Dbg( p_access, "timeshift close called" );
197 vlc_thread_join( p_access );
199 for( p_entry = p_sys->p_write_list; p_entry; )
201 ts_entry_t *p_next = p_entry->p_next;
202 fclose( p_entry->file );
206 for( p_entry = p_sys->p_read_list; p_entry; )
208 ts_entry_t *p_next = p_entry->p_next;
209 fclose( p_entry->file );
213 for( i = 0; i < p_sys->i_files; i++ )
215 sprintf( p_sys->psz_filename, "%s%i.dat",
216 p_sys->psz_filename_base, i );
217 unlink( p_sys->psz_filename );
220 free( p_sys->psz_filename );
221 free( p_sys->psz_filename_base );
222 block_FifoRelease( p_sys->p_fifo );
226 /*****************************************************************************
228 *****************************************************************************/
229 static block_t *Block( access_t *p_access )
231 access_sys_t *p_sys = p_access->p_sys;
234 if( p_access->b_die )
236 p_access->info.b_eof = VLC_TRUE;
240 p_block = block_FifoGet( p_sys->p_fifo );
241 //p_access->info.i_size -= p_block->i_buffer;
245 /*****************************************************************************
247 *****************************************************************************/
248 static void Thread( access_t *p_access )
250 access_sys_t *p_sys = p_access->p_sys;
251 access_t *p_src = p_access->p_source;
254 while( !p_access->b_die )
258 /* Get a new block from the source */
259 if( p_src->pf_block )
261 p_block = p_src->pf_block( p_src );
263 if( p_block == NULL )
265 if( p_src->info.b_eof ) break;
272 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
275 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
277 if( p_block->i_buffer < 0 )
279 block_Release( p_block );
280 if( p_block->i_buffer == 0 ) break;
287 if( !p_sys->p_write_list && !p_sys->p_read_list &&
288 p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
290 /* If there isn't too much timeshifted data,
291 * write directly to FIFO */
292 block_FifoPut( p_sys->p_fifo, p_block );
294 //p_access->info.i_size += p_block->i_buffer;
295 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
297 /* Nothing else to do */
301 WriteBlockToFile( p_access, p_block );
302 block_Release( p_block );
304 /* Read from file to fill up the fifo */
305 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
308 p_block = ReadBlockFromFile( p_access );
309 if( !p_block ) break;
310 block_FifoPut( p_sys->p_fifo, p_block );
314 msg_Dbg( p_access, "timeshift: EOF" );
316 /* Send dummy packet to avoid deadlock in TShiftBlock */
317 for( i = 0; i < 2; i++ )
319 block_t *p_dummy = block_New( p_access, 128 );
320 p_dummy->i_flags |= BLOCK_FLAG_DISCONTINUITY;
321 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
322 block_FifoPut( p_sys->p_fifo, p_dummy );
326 /*****************************************************************************
328 *****************************************************************************/
329 static void NextFileWrite( access_t *p_access )
331 access_sys_t *p_sys = p_access->p_sys;
334 if( !p_sys->p_write_list )
336 p_sys->i_write_size = 0;
340 p_next = p_sys->p_write_list->p_next;
342 /* Put written file in read list */
343 if( p_sys->i_write_size < p_sys->i_file_size )
344 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
346 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
347 *p_sys->pp_read_last = p_sys->p_write_list;
348 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
349 p_sys->p_write_list->p_next = 0;
351 /* Switch to next file to write */
352 p_sys->p_write_list = p_next;
353 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
355 p_sys->i_write_size = 0;
358 /*****************************************************************************
360 *****************************************************************************/
361 static void NextFileRead( access_t *p_access )
363 access_sys_t *p_sys = p_access->p_sys;
366 if( !p_sys->p_read_list ) return;
368 p_next = p_sys->p_read_list->p_next;
370 /* Put read file in write list */
371 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
372 *p_sys->pp_write_last = p_sys->p_read_list;
373 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
374 p_sys->p_read_list->p_next = 0;
376 /* Switch to next file to read */
377 p_sys->p_read_list = p_next;
378 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
381 /*****************************************************************************
383 *****************************************************************************/
384 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
386 access_sys_t *p_sys = p_access->p_sys;
387 int i_write, i_buffer;
389 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
391 /* Open new file if necessary */
392 if( !p_sys->p_write_list )
396 sprintf( p_sys->psz_filename, "%s%i.dat",
397 p_sys->psz_filename_base, p_sys->i_files );
398 file = utf8_fopen( p_sys->psz_filename, "w+b" );
400 if( !file && p_sys->i_files < 2 )
402 /* We just can't work with less than 2 buffer files */
403 msg_Err( p_access, "cannot open temporary file '%s' (%s)",
404 p_sys->psz_filename, strerror(errno) );
407 else if( !file ) return VLC_EGENERIC;
409 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
410 p_sys->p_write_list->p_next = 0;
411 p_sys->p_write_list->file = file;
412 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
418 i_buffer = __MIN( p_block->i_buffer,
419 p_sys->i_file_size - p_sys->i_write_size );
421 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
422 p_sys->p_write_list->file );
424 if( i_write > 0 ) p_sys->i_write_size += i_write;
426 //p_access->info.i_size += i_write;
427 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
429 if( i_write < i_buffer )
431 /* Looks like we're short of space */
433 if( !p_sys->p_write_list->p_next )
435 msg_Warn( p_access, "no more space, overwritting old data" );
436 NextFileRead( p_access );
437 NextFileRead( p_access );
440 /* Make sure we switch to next file in write list */
441 p_sys->i_write_size = p_sys->i_file_size;
444 p_block->p_buffer += i_write;
445 p_block->i_buffer -= i_write;
447 /* Check if we have some data left */
448 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
453 /*****************************************************************************
455 *****************************************************************************/
456 static block_t *ReadBlockFromFile( access_t *p_access )
458 access_sys_t *p_sys = p_access->p_sys;
461 if( !p_sys->p_read_list && p_sys->p_write_list )
463 /* Force switching to next write file, that should
464 * give us something to read */
465 NextFileWrite( p_access );
468 if( !p_sys->p_read_list ) return 0;
470 p_block = block_New( p_access, 4096 );
471 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
472 p_sys->p_read_list->file );
474 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
476 //p_access->info.i_size -= p_block->i_buffer;
477 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
482 /*****************************************************************************
483 * Seek: seek to a specific location in a file
484 *****************************************************************************/
485 static int Seek( access_t *p_access, int64_t i_pos )
487 //access_sys_t *p_sys = p_access->p_sys;
491 /*****************************************************************************
493 *****************************************************************************/
494 static int Control( access_t *p_access, int i_query, va_list args )
496 access_t *p_src = p_access->p_source;
504 case ACCESS_CAN_SEEK:
505 case ACCESS_CAN_FASTSEEK:
506 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
510 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
511 case ACCESS_CAN_PAUSE:
512 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
517 pi_int = (int*)va_arg( args, int * );
521 case ACCESS_GET_PTS_DELAY:
522 pi_64 = (int64_t*)va_arg( args, int64_t * );
523 return access2_Control( p_src, ACCESS_GET_PTS_DELAY, pi_64 );
525 case ACCESS_SET_PAUSE_STATE:
528 case ACCESS_GET_TITLE_INFO:
529 case ACCESS_SET_TITLE:
530 case ACCESS_SET_SEEKPOINT:
531 case ACCESS_GET_META:
534 case ACCESS_SET_PRIVATE_ID_STATE:
535 case ACCESS_GET_PRIVATE_ID_STATE:
536 case ACCESS_SET_PRIVATE_ID_CA:
537 return access2_vaControl( p_src, i_query, args );
540 msg_Warn( p_access, "unimplemented query in control" );
547 /*****************************************************************************
549 *****************************************************************************/
551 #define getpid() GetCurrentProcessId()
553 static char *GetTmpFilePath( access_t *p_access )
555 char *psz_dir = var_GetString( p_access, "timeshift-dir" );
556 char *psz_filename_base;
558 if( ( psz_dir != NULL ) && ( psz_dir[0] == '\0' ) )
564 if( psz_dir == NULL )
567 DWORD ret = GetTempPathW (0, NULL);
568 wchar_t wdir[ret + 3]; // can at least old "C:" + nul
569 wchar_t *pwdir = wdir, pwdir_free = NULL;
571 if (GetTempPathW (ret + 1, wdir) == 0)
573 pwdir_free = pwdir = _wgetcwd (NULL, 0);
575 wcscpy (wdir, L"C:");
578 ret = WideCharToMultiByte (CP_UTF8, 0, pwdir, -1, NULL, 0);
580 WideCharToMultiByte (CP_UTF8, 0, pwdir, -1, mbdir, ret);
581 if (pwdir_free != NULL)
584 /* remove last \\ if any */
585 if (mbdir[strlen (mbdir) - 1] == '\\')
586 mbdir[strlen (mbdit) - 1] = '\0';
588 psz_dir = strdup (mbdir);
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;