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>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
44 #define GRANULARITY_TEXT N_("Timeshift granularity")
45 #define GRANULARITY_LONGTEXT N_( "This is the size of the temporary files " \
46 "tha will be used to store the timeshifted streams." )
47 #define DIR_TEXT N_("Timeshift directory")
48 #define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \
52 set_shortname( _("Timeshift") );
53 set_description( _("Timeshift") );
54 set_category( CAT_INPUT );
55 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
56 set_capability( "access_filter", 0 );
57 add_shortcut( "timeshift" );
58 set_callbacks( Open, Close );
60 add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
61 GRANULARITY_LONGTEXT, VLC_TRUE );
62 add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, VLC_FALSE );
65 /*****************************************************************************
67 *****************************************************************************/
69 static int Seek( access_t *, int64_t );
70 static block_t *Block ( access_t *p_access );
71 static int Control( access_t *, int i_query, va_list args );
72 static void Thread ( access_t *p_access );
73 static int WriteBlockToFile( access_t *p_access, block_t *p_block );
74 static block_t *ReadBlockFromFile( access_t *p_access );
75 static char *GetTmpFilePath( access_t *p_access );
77 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
78 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
79 #define TMP_FILE_MAX 256
81 typedef struct ts_entry_t
84 struct ts_entry_t *p_next;
96 ts_entry_t *p_read_list;
97 ts_entry_t **pp_read_last;
98 ts_entry_t *p_write_list;
99 ts_entry_t **pp_write_last;
101 char *psz_filename_base;
105 /*****************************************************************************
107 *****************************************************************************/
108 static int Open( vlc_object_t *p_this )
110 access_t *p_access = (access_t*)p_this;
111 access_t *p_src = p_access->p_source;
115 /* Only work with not pace controled access */
116 if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) || b_bool )
118 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE" );
121 /* Refuse access that can be paused */
122 if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
124 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
129 p_access->pf_read = NULL;
130 p_access->pf_block = Block;
131 p_access->pf_seek = Seek;
132 p_access->pf_control = Control;
133 p_access->info = p_src->info;
135 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
138 p_sys->p_fifo = block_FifoNew( p_access );
139 p_sys->i_write_size = 0;
142 p_sys->p_read_list = NULL;
143 p_sys->pp_read_last = &p_sys->p_read_list;
144 p_sys->p_write_list = NULL;
145 p_sys->pp_write_last = &p_sys->p_write_list;
147 var_Create( p_access, "timeshift-dir",
148 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
149 var_Create( p_access, "timeshift-granularity",
150 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
151 p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
152 if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
153 p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
155 p_sys->psz_filename_base = GetTmpFilePath( p_access );
156 p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
158 if( vlc_thread_create( p_access, "timeshift thread", Thread,
159 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
161 msg_Err( p_access, "cannot spawn timeshift access thread" );
168 /*****************************************************************************
170 *****************************************************************************/
171 static void Close( vlc_object_t *p_this )
173 access_t *p_access = (access_t*)p_this;
174 access_sys_t *p_sys = p_access->p_sys;
178 msg_Dbg( p_access, "timeshift close called" );
179 vlc_thread_join( p_access );
181 for( p_entry = p_sys->p_write_list; p_entry; )
183 ts_entry_t *p_next = p_entry->p_next;
184 fclose( p_entry->file );
188 for( p_entry = p_sys->p_read_list; p_entry; )
190 ts_entry_t *p_next = p_entry->p_next;
191 fclose( p_entry->file );
195 for( i = 0; i < p_sys->i_files; i++ )
197 sprintf( p_sys->psz_filename, "%s%i.dat",
198 p_sys->psz_filename_base, i );
199 unlink( p_sys->psz_filename );
202 free( p_sys->psz_filename );
203 free( p_sys->psz_filename_base );
204 block_FifoRelease( p_sys->p_fifo );
208 /*****************************************************************************
210 *****************************************************************************/
211 static block_t *Block( access_t *p_access )
213 access_sys_t *p_sys = p_access->p_sys;
216 if( p_access->b_die )
218 p_access->info.b_eof = VLC_TRUE;
222 p_block = block_FifoGet( p_sys->p_fifo );
223 //p_access->info.i_size -= p_block->i_buffer;
227 /*****************************************************************************
229 *****************************************************************************/
230 static void Thread( access_t *p_access )
232 access_sys_t *p_sys = p_access->p_sys;
233 access_t *p_src = p_access->p_source;
236 while( !p_access->b_die )
240 /* Get a new block from the source */
241 if( p_src->pf_block )
243 p_block = p_src->pf_block( p_src );
245 if( p_block == NULL )
247 if( p_src->info.b_eof ) break;
254 if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
257 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
259 if( p_block->i_buffer < 0 )
261 block_Release( p_block );
262 if( p_block->i_buffer == 0 ) break;
269 if( !p_sys->p_write_list && !p_sys->p_read_list &&
270 p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
272 /* If there isn't too much timeshifted data,
273 * write directly to FIFO */
274 block_FifoPut( p_sys->p_fifo, p_block );
276 //p_access->info.i_size += p_block->i_buffer;
277 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
279 /* Nothing else to do */
283 WriteBlockToFile( p_access, p_block );
284 block_Release( p_block );
286 /* Read from file to fill up the fifo */
287 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
290 p_block = ReadBlockFromFile( p_access );
291 if( !p_block ) break;
292 block_FifoPut( p_sys->p_fifo, p_block );
296 msg_Dbg( p_access, "timeshift: EOF" );
298 /* Send dummy packet to avoid deadlock in TShiftBlock */
299 for( i = 0; i < 2; i++ )
301 block_t *p_dummy = block_New( p_access, 128 );
302 p_dummy->i_flags |= BLOCK_FLAG_DISCONTINUITY;
303 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
304 block_FifoPut( p_sys->p_fifo, p_dummy );
308 /*****************************************************************************
310 *****************************************************************************/
311 static void NextFileWrite( access_t *p_access )
313 access_sys_t *p_sys = p_access->p_sys;
316 if( !p_sys->p_write_list )
318 p_sys->i_write_size = 0;
322 p_next = p_sys->p_write_list->p_next;
324 /* Put written file in read list */
325 if( p_sys->i_write_size < p_sys->i_file_size )
326 ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
328 fseek( p_sys->p_write_list->file, 0, SEEK_SET );
329 *p_sys->pp_read_last = p_sys->p_write_list;
330 p_sys->pp_read_last = &p_sys->p_write_list->p_next;
331 p_sys->p_write_list->p_next = 0;
333 /* Switch to next file to write */
334 p_sys->p_write_list = p_next;
335 if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
337 p_sys->i_write_size = 0;
340 /*****************************************************************************
342 *****************************************************************************/
343 static void NextFileRead( access_t *p_access )
345 access_sys_t *p_sys = p_access->p_sys;
348 if( !p_sys->p_read_list ) return;
350 p_next = p_sys->p_read_list->p_next;
352 /* Put read file in write list */
353 fseek( p_sys->p_read_list->file, 0, SEEK_SET );
354 *p_sys->pp_write_last = p_sys->p_read_list;
355 p_sys->pp_write_last = &p_sys->p_read_list->p_next;
356 p_sys->p_read_list->p_next = 0;
358 /* Switch to next file to read */
359 p_sys->p_read_list = p_next;
360 if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
363 /*****************************************************************************
365 *****************************************************************************/
366 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
368 access_sys_t *p_sys = p_access->p_sys;
369 int i_write, i_buffer;
371 if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
373 /* Open new file if necessary */
374 if( !p_sys->p_write_list )
378 sprintf( p_sys->psz_filename, "%s%i.dat",
379 p_sys->psz_filename_base, p_sys->i_files );
380 file = utf8_fopen( p_sys->psz_filename, "w+b" );
382 if( !file && p_sys->i_files < 2 )
384 /* We just can't work with less than 2 buffer files */
385 msg_Err( p_access, "cannot open temporary file '%s' (%s)",
386 p_sys->psz_filename, strerror(errno) );
389 else if( !file ) return VLC_EGENERIC;
391 p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
392 p_sys->p_write_list->p_next = 0;
393 p_sys->p_write_list->file = file;
394 p_sys->pp_write_last = &p_sys->p_write_list->p_next;
400 i_buffer = __MIN( p_block->i_buffer,
401 p_sys->i_file_size - p_sys->i_write_size );
403 i_write = fwrite( p_block->p_buffer, 1, i_buffer,
404 p_sys->p_write_list->file );
406 if( i_write > 0 ) p_sys->i_write_size += i_write;
408 //p_access->info.i_size += i_write;
409 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
411 if( i_write < i_buffer )
413 /* Looks like we're short of space */
415 if( !p_sys->p_write_list->p_next )
417 msg_Warn( p_access, "no more space, overwritting old data" );
418 NextFileRead( p_access );
419 NextFileRead( p_access );
422 /* Make sure we switch to next file in write list */
423 p_sys->i_write_size = p_sys->i_file_size;
426 p_block->p_buffer += i_write;
427 p_block->i_buffer -= i_write;
429 /* Check if we have some data left */
430 if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
435 /*****************************************************************************
437 *****************************************************************************/
438 static block_t *ReadBlockFromFile( access_t *p_access )
440 access_sys_t *p_sys = p_access->p_sys;
443 if( !p_sys->p_read_list && p_sys->p_write_list )
445 /* Force switching to next write file, that should
446 * give us something to read */
447 NextFileWrite( p_access );
450 if( !p_sys->p_read_list ) return 0;
452 p_block = block_New( p_access, 4096 );
453 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
454 p_sys->p_read_list->file );
456 if( p_block->i_buffer == 0 ) NextFileRead( p_access );
458 //p_access->info.i_size -= p_block->i_buffer;
459 //p_access->info.i_update |= INPUT_UPDATE_SIZE;
464 /*****************************************************************************
465 * Seek: seek to a specific location in a file
466 *****************************************************************************/
467 static int Seek( access_t *p_access, int64_t i_pos )
469 //access_sys_t *p_sys = p_access->p_sys;
473 /*****************************************************************************
475 *****************************************************************************/
476 static int Control( access_t *p_access, int i_query, va_list args )
478 access_t *p_src = p_access->p_source;
486 case ACCESS_CAN_SEEK:
487 case ACCESS_CAN_FASTSEEK:
488 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
492 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
493 case ACCESS_CAN_PAUSE:
494 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
499 pi_int = (int*)va_arg( args, int * );
503 case ACCESS_GET_PTS_DELAY:
504 pi_64 = (int64_t*)va_arg( args, int64_t * );
505 return access2_Control( p_src, ACCESS_GET_PTS_DELAY, pi_64 );
507 case ACCESS_SET_PAUSE_STATE:
510 case ACCESS_GET_TITLE_INFO:
511 case ACCESS_SET_TITLE:
512 case ACCESS_SET_SEEKPOINT:
513 case ACCESS_GET_META:
516 case ACCESS_SET_PRIVATE_ID_STATE:
517 case ACCESS_GET_PRIVATE_ID_STATE:
518 case ACCESS_SET_PRIVATE_ID_CA:
519 return access2_vaControl( p_src, i_query, args );
522 msg_Warn( p_access, "unimplemented query in control" );
529 /*****************************************************************************
531 *****************************************************************************/
533 #define getpid() GetCurrentProcessId()
535 static char *GetTmpFilePath( access_t *p_access )
537 char *psz_dir = var_GetString( p_access, "timeshift-dir" );
538 char *psz_filename_base;
540 if( ( psz_dir != NULL ) && ( psz_dir[0] == '\0' ) )
546 if( psz_dir == NULL )
549 char psz_local_dir[MAX_PATH];
552 i_size = GetTempPath( MAX_PATH, psz_local_dir );
553 if( i_size <= 0 || i_size > MAX_PATH )
555 if( !getcwd( psz_local_dir, MAX_PATH ) )
556 strcpy( psz_local_dir, "C:" );
559 psz_dir = FromLocaleDup( psz_local_dir );
561 /* remove last \\ if any */
562 if( psz_dir[strlen(psz_dir)-1] == '\\' )
563 psz_dir[strlen(psz_dir)-1] = '\0';
565 psz_dir = strdup( "/tmp" );
569 asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
570 psz_dir, getpid(), p_access->i_object_id );
573 return psz_filename_base;