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/input.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
42 #define GRANULARITY_TEXT N_("Timeshift granularity")
43 #define GRANULARITY_LONGTEXT N_( "Size of the temporary files use to store " \
44 "the timeshifted stream." )
45 #define DIR_TEXT N_("Timeshift directory")
46 #define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \
50 set_shortname( _("Timeshift") );
51 set_description( _("Timeshift") );
52 set_category( CAT_INPUT );
53 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
54 set_capability( "access_filter", 0 );
55 add_shortcut( "timeshift" );
56 set_callbacks( Open, Close );
58 add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
59 GRANULARITY_LONGTEXT, VLC_TRUE );
60 add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, VLC_FALSE );
63 /*****************************************************************************
65 *****************************************************************************/
67 static int Seek( access_t *, int64_t );
68 static block_t *Block ( access_t *p_access );
69 static int Control( access_t *, int i_query, va_list args );
70 static void Thread ( access_t *p_access );
71 static int WriteBlockToFile( access_t *p_access, block_t *p_block );
72 static block_t *ReadBlockFromFile( access_t *p_access );
73 static char *GetTmpFilePath( access_t *p_access );
75 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
76 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
77 #define TMP_FILE_MAX 256
79 typedef struct ts_entry_t
82 struct ts_entry_t *p_next;
94 ts_entry_t *p_read_list;
95 ts_entry_t **pp_read_last;
96 ts_entry_t *p_write_list;
97 ts_entry_t **pp_write_last;
99 char *psz_filename_base;
103 /*****************************************************************************
105 *****************************************************************************/
106 static int Open( vlc_object_t *p_this )
108 access_t *p_access = (access_t*)p_this;
109 access_t *p_src = p_access->p_source;
113 /* Only work with not pace controled access */
114 if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) || b_bool )
116 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE" );
119 /* Refuse access that can be paused */
120 if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
122 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
127 p_access->pf_read = NULL;
128 p_access->pf_block = Block;
129 p_access->pf_seek = Seek;
130 p_access->pf_control = Control;
131 p_access->info = p_src->info;
133 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
136 p_sys->p_fifo = block_FifoNew( p_access );
137 p_sys->i_write_size = 0;
140 p_sys->p_read_list = NULL;
141 p_sys->pp_read_last = &p_sys->p_read_list;
142 p_sys->p_write_list = NULL;
143 p_sys->pp_write_last = &p_sys->p_write_list;
145 var_Create( p_access, "timeshift-dir",
146 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
147 var_Create( p_access, "timeshift-granularity",
148 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
149 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
150 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
151 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
153 p_sys->psz_filename_base = GetTmpFilePath( p_access );
154 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
156 if( vlc_thread_create( p_access, "timeshift thread", Thread,
157 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
159 msg_Err( p_access, "cannot spawn timeshift access thread" );
166 /*****************************************************************************
168 *****************************************************************************/
169 static void Close( vlc_object_t *p_this )
171 access_t *p_access = (access_t*)p_this;
172 access_sys_t *p_sys = p_access->p_sys;
176 msg_Dbg( p_access, "timeshift close called" );
177 vlc_thread_join( p_access );
179 for( p_entry = p_sys->p_write_list; p_entry; )
181 ts_entry_t *p_next = p_entry->p_next;
182 fclose( p_entry->file );
186 for( p_entry = p_sys->p_read_list; p_entry; )
188 ts_entry_t *p_next = p_entry->p_next;
189 fclose( p_entry->file );
193 for( i = 0; i < p_sys->i_files; i++ )
195 sprintf( p_sys->psz_filename, "%s%i.dat",
196 p_sys->psz_filename_base, i );
197 unlink( p_sys->psz_filename );
200 free( p_sys->psz_filename );
201 free( p_sys->psz_filename_base );
202 block_FifoRelease( p_sys->p_fifo );
206 /*****************************************************************************
208 *****************************************************************************/
209 static block_t *Block( access_t *p_access )
211 access_sys_t *p_sys = p_access->p_sys;
214 if( p_access->b_die )
216 p_access->info.b_eof = VLC_TRUE;
220 p_block = block_FifoGet( p_sys->p_fifo );
221 //p_access->info.i_size -= p_block->i_buffer;
225 /*****************************************************************************
227 *****************************************************************************/
228 static void Thread( access_t *p_access )
230 access_sys_t *p_sys = p_access->p_sys;
231 access_t *p_src = p_access->p_source;
234 while( !p_access->b_die )
238 /* Get a new block from the source */
239 if( p_src->pf_block )
241 p_block = p_src->pf_block( p_src );
243 if( p_block == NULL )
245 if( p_src->info.b_eof ) break;
252 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
255 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
257 if( p_block->i_buffer < 0 )
259 block_Release( p_block );
260 if( p_block->i_buffer == 0 ) break;
267 if( !p_sys->p_write_list && !p_sys->p_read_list &&
268 p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
270 /* If there isn't too much timeshifted data,
271 * write directly to FIFO */
272 block_FifoPut( p_sys->p_fifo, p_block );
274 //p_access->info.i_size += p_block->i_buffer;
275 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
277 /* Nothing else to do */
281 WriteBlockToFile( p_access, p_block );
282 block_Release( p_block );
284 /* Read from file to fill up the fifo */
285 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
288 p_block = ReadBlockFromFile( p_access );
289 if( !p_block ) break;
290 block_FifoPut( p_sys->p_fifo, p_block );
294 msg_Dbg( p_access, "timeshift: EOF" );
296 /* Send dummy packet to avoid deadlock in TShiftBlock */
297 for( i = 0; i < 2; i++ )
299 block_t *p_dummy = block_New( p_access, 128 );
300 p_dummy->i_flags |= BLOCK_FLAG_DISCONTINUITY;
301 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
302 block_FifoPut( p_sys->p_fifo, p_dummy );
306 /*****************************************************************************
308 *****************************************************************************/
309 static void NextFileWrite( access_t *p_access )
311 access_sys_t *p_sys = p_access->p_sys;
314 if( !p_sys->p_write_list )
316 p_sys->i_write_size = 0;
320 p_next = p_sys->p_write_list->p_next;
322 /* Put written file in read list */
323 if( p_sys->i_write_size < p_sys->i_file_size )
324 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
326 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
327 *p_sys->pp_read_last = p_sys->p_write_list;
328 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
329 p_sys->p_write_list->p_next = 0;
331 /* Switch to next file to write */
332 p_sys->p_write_list = p_next;
333 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
335 p_sys->i_write_size = 0;
338 /*****************************************************************************
340 *****************************************************************************/
341 static void NextFileRead( access_t *p_access )
343 access_sys_t *p_sys = p_access->p_sys;
346 if( !p_sys->p_read_list ) return;
348 p_next = p_sys->p_read_list->p_next;
350 /* Put read file in write list */
351 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
352 *p_sys->pp_write_last = p_sys->p_read_list;
353 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
354 p_sys->p_read_list->p_next = 0;
356 /* Switch to next file to read */
357 p_sys->p_read_list = p_next;
358 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
361 /*****************************************************************************
363 *****************************************************************************/
364 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
366 access_sys_t *p_sys = p_access->p_sys;
367 int i_write, i_buffer;
369 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
371 /* Open new file if necessary */
372 if( !p_sys->p_write_list )
376 sprintf( p_sys->psz_filename, "%s%i.dat",
377 p_sys->psz_filename_base, p_sys->i_files );
378 file = utf8_fopen( p_sys->psz_filename, "w+b" );
380 if( !file && p_sys->i_files < 2 )
382 /* We just can't work with less than 2 buffer files */
383 msg_Err( p_access, "cannot open temporary file '%s' (%s)",
384 p_sys->psz_filename, strerror(errno) );
387 else if( !file ) return VLC_EGENERIC;
389 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
390 p_sys->p_write_list->p_next = 0;
391 p_sys->p_write_list->file = file;
392 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
398 i_buffer = __MIN( p_block->i_buffer,
399 p_sys->i_file_size - p_sys->i_write_size );
401 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
402 p_sys->p_write_list->file );
404 if( i_write > 0 ) p_sys->i_write_size += i_write;
406 //p_access->info.i_size += i_write;
407 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
409 if( i_write < i_buffer )
411 /* Looks like we're short of space */
413 if( !p_sys->p_write_list->p_next )
415 msg_Warn( p_access, "no more space, overwritting old data" );
416 NextFileRead( p_access );
417 NextFileRead( p_access );
420 /* Make sure we switch to next file in write list */
421 p_sys->i_write_size = p_sys->i_file_size;
424 p_block->p_buffer += i_write;
425 p_block->i_buffer -= i_write;
427 /* Check if we have some data left */
428 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
433 /*****************************************************************************
435 *****************************************************************************/
436 static block_t *ReadBlockFromFile( access_t *p_access )
438 access_sys_t *p_sys = p_access->p_sys;
441 if( !p_sys->p_read_list && p_sys->p_write_list )
443 /* Force switching to next write file, that should
444 * give us something to read */
445 NextFileWrite( p_access );
448 if( !p_sys->p_read_list ) return 0;
450 p_block = block_New( p_access, 4096 );
451 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
452 p_sys->p_read_list->file );
454 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
456 //p_access->info.i_size -= p_block->i_buffer;
457 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
462 /*****************************************************************************
463 * Seek: seek to a specific location in a file
464 *****************************************************************************/
465 static int Seek( access_t *p_access, int64_t i_pos )
467 //access_sys_t *p_sys = p_access->p_sys;
471 /*****************************************************************************
473 *****************************************************************************/
474 static int Control( access_t *p_access, int i_query, va_list args )
476 access_t *p_src = p_access->p_source;
484 case ACCESS_CAN_SEEK:
485 case ACCESS_CAN_FASTSEEK:
486 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
490 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
491 case ACCESS_CAN_PAUSE:
492 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
497 pi_int = (int*)va_arg( args, int * );
501 case ACCESS_GET_PTS_DELAY:
502 pi_64 = (int64_t*)va_arg( args, int64_t * );
503 return access2_Control( p_src, ACCESS_GET_PTS_DELAY, pi_64 );
505 case ACCESS_SET_PAUSE_STATE:
508 case ACCESS_GET_TITLE_INFO:
509 case ACCESS_SET_TITLE:
510 case ACCESS_SET_SEEKPOINT:
511 case ACCESS_GET_META:
514 case ACCESS_SET_PRIVATE_ID_STATE:
515 case ACCESS_GET_PRIVATE_ID_STATE:
516 case ACCESS_SET_PRIVATE_ID_CA:
517 return access2_vaControl( p_src, i_query, args );
520 msg_Warn( p_access, "unimplemented query in control" );
527 /*****************************************************************************
529 *****************************************************************************/
531 #define getpid() GetCurrentProcessId()
533 static char *GetTmpFilePath( access_t *p_access )
535 char *psz_dir = var_GetString( p_access, "timeshift-dir" );
536 char *psz_filename_base;
538 if( psz_dir && !*psz_dir )
549 psz_dir = malloc( MAX_PATH + 1 );
550 i_size = GetTempPath( MAX_PATH, psz_dir );
551 if( i_size <= 0 || i_size > MAX_PATH )
553 if( !getcwd( psz_dir, MAX_PATH ) ) strcpy( psz_dir, "c:" );
556 /* remove last \\ if any */
557 if( psz_dir[strlen(psz_dir)-1] == '\\' )
558 psz_dir[strlen(psz_dir)-1] = '\0';
561 psz_dir = strdup( "/tmp" );
565 asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
566 psz_dir, getpid(), p_access->i_object_id );
568 return psz_filename_base;