1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
5 * $Id: demux.c 7546 2004-04-29 13:53:29Z gbazin $
7 * Author: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
42 set_shortname( _("Timeshift") );
43 set_description( _("Timeshift") );
44 set_category( CAT_INPUT );
45 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
46 set_capability( "access_filter", 0 );
47 add_shortcut( "timeshift" );
48 set_callbacks( Open, Close );
51 /*****************************************************************************
53 *****************************************************************************/
55 static block_t *Block ( access_t *p_access );
56 static int Control( access_t *, int i_query, va_list args );
57 static void Thread ( access_t * );
59 #define TIMESHIFT_FIFO_MAX (4*1024*1024)
60 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
79 /*****************************************************************************
81 *****************************************************************************/
82 static int Open( vlc_object_t *p_this )
84 access_t *p_access = (access_t*)p_this;
85 access_t *p_src = p_access->p_source;
93 /* Only work with not pace controled access */
94 if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) || b_bool )
96 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE" );
99 /* Refuse access that can be paused */
100 if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
102 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
107 p_access->pf_read = NULL;
108 p_access->pf_block = Block;
109 p_access->pf_seek = NULL;
110 p_access->pf_control = Control;
112 p_access->info = p_src->info;
114 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
117 p_sys->p_fifo = block_FifoNew( p_access );
118 p_sys->b_opened = VLC_FALSE;
119 p_sys->t1 = p_sys->t2 = NULL;
120 p_sys->w = p_sys->r = NULL;
124 i_size = GetTempPath( 4095, buffer );
125 if( i_size <= 0 || i_size >= 4095 )
127 if( getcwd( buffer, 4095 ) == NULL )
128 strcpy( buffer, "c:" );
130 /* remove last \\ if any */
131 if( buffer[strlen(buffer)-1] == '\\' )
132 buffer[strlen(buffer)-1] = '\0';
134 asprintf( &p_sys->psz_tmp1, "%s\\vlc-timeshift-%d-%d-1.dat",
135 buffer, GetCurrentProcessId(), p_access->i_object_id );
136 asprintf( &p_sys->psz_tmp2, "%s\\vlc-timeshift-%d-%d-2.dat",
137 buffer, GetCurrentProcessId(), p_access->i_object_id );
139 asprintf( &p_sys->psz_tmp1, "/tmp/vlc-timeshift-%d-%d-1.dat",
140 getpid(), p_access->i_object_id );
141 asprintf( &p_sys->psz_tmp2, "/tmp/vlc-timeshift-%d-%d-2.dat",
142 getpid(), p_access->i_object_id );
145 if( vlc_thread_create( p_access, "timeshift thread", Thread,
146 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
148 msg_Err( p_access, "cannot spawn timeshift access thread" );
155 /*****************************************************************************
157 *****************************************************************************/
158 static void Close( vlc_object_t *p_this )
160 access_t *p_access = (access_t*)p_this;
161 access_sys_t *p_sys = p_access->p_sys;
164 msg_Dbg( p_access, "timeshift close called" );
165 vlc_thread_join( p_access );
167 if( p_sys->b_opened )
169 if( p_sys->t1 ) fclose( p_sys->t1 );
170 if( p_sys->t2 ) fclose( p_sys->t2 );
171 unlink( p_sys->psz_tmp1 );
172 unlink( p_sys->psz_tmp2 );
175 free( p_sys->psz_tmp1 );
176 free( p_sys->psz_tmp2 );
178 block_FifoRelease( p_sys->p_fifo );
182 /*****************************************************************************
184 *****************************************************************************/
185 static block_t *Block( access_t *p_access )
187 access_sys_t *p_sys = p_access->p_sys;
189 if( p_access->b_die )
191 p_access->info.b_eof = VLC_TRUE;
195 return block_FifoGet( p_sys->p_fifo );
198 /*****************************************************************************
200 *****************************************************************************/
201 static int Control( access_t *p_access, int i_query, va_list args )
203 access_t *p_src = p_access->p_source;
212 case ACCESS_CAN_SEEK:
213 case ACCESS_CAN_FASTSEEK:
214 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
215 *pb_bool = VLC_FALSE;
218 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
219 case ACCESS_CAN_PAUSE:
220 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
226 pi_int = (int*)va_arg( args, int * );
230 case ACCESS_GET_PTS_DELAY:
231 pi_64 = (int64_t*)va_arg( args, int64_t * );
232 return access2_Control( p_src, ACCESS_GET_PTS_DELAY, pi_64 );
234 case ACCESS_SET_PAUSE_STATE:
237 case ACCESS_GET_TITLE_INFO:
238 case ACCESS_SET_TITLE:
239 case ACCESS_SET_SEEKPOINT:
240 case ACCESS_GET_META:
243 case ACCESS_SET_PRIVATE_ID_STATE:
244 case ACCESS_GET_PRIVATE_ID_STATE:
245 case ACCESS_SET_PRIVATE_ID_CA:
246 return access2_vaControl( p_src, i_query, args );
249 msg_Warn( p_access, "unimplemented query in control" );
256 /*****************************************************************************
258 *****************************************************************************/
259 static void Thread( access_t *p_access )
261 access_sys_t *p_sys = p_access->p_sys;
262 access_t *p_src = p_access->p_source;
265 while( !p_access->b_die )
269 /* Get a new block */
270 if( p_src->pf_block )
272 p_block = p_src->pf_block( p_src );
274 if( p_block == NULL )
276 if( p_src->info.b_eof )
285 if( ( p_block = block_New( p_access, 2048 ) ) == NULL )
288 p_block->i_buffer = p_src->pf_read( p_src, p_block->p_buffer, 2048);
289 if( p_block->i_buffer < 0 )
291 block_Release( p_block );
292 if( p_block->i_buffer == 0 )
299 /* Open dump files if we need them */
300 if( p_sys->p_fifo->i_size >= TIMESHIFT_FIFO_MAX && !p_sys->b_opened )
302 msg_Dbg( p_access, "opening first temporary files (%s)",
305 p_sys->b_opened = VLC_TRUE;
306 p_sys->t1 = p_sys->t2 = NULL;
307 p_sys->w = p_sys->r = NULL;
309 p_sys->t1 = fopen( p_sys->psz_tmp1, "w+b" );
312 msg_Dbg( p_access, "opening second temporary files (%s)",
315 p_sys->t2 = fopen( p_sys->psz_tmp2, "w+b" );
318 p_sys->w = p_sys->t1;
321 msg_Dbg( p_access, "start writing into temporary file" );
325 msg_Err( p_access, "cannot open temporary file '%s' (%s)",
326 p_sys->psz_tmp2, strerror(errno) );
333 msg_Err( p_access, "cannot open temporary file '%s' (%s)",
334 p_sys->psz_tmp1, strerror(errno) );
343 i_write = fwrite( p_block->p_buffer, 1, p_block->i_buffer,
345 block_Release( p_block );
348 p_sys->i_w += i_write;
350 msg_Warn( p_access, "cannot write data" );
352 /* Start reading from a file if fifo isn't at 25% */
353 if( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN && !p_sys->r )
355 msg_Dbg( p_access, "start reading from temporary file (dumped=%d)", p_sys->i_w );
358 fseek( p_sys->r, 0, SEEK_SET );
360 p_sys->w = p_sys->t2;
366 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN )
368 p_block = block_New( p_access, 4096 );
369 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
372 if( p_block->i_buffer > 0 )
374 block_FifoPut( p_sys->p_fifo, p_block );
376 else if( p_sys->i_w > 32*1024)
379 block_Release( p_block );
381 msg_Dbg( p_access, "switching temporary files (dumped=%d)", p_sys->i_w );
383 /* Switch read/write */
387 fseek( p_sys->r, 0, SEEK_SET );
390 fseek( p_sys->w, 0, SEEK_SET );
391 ftruncate( fileno(p_sys->w), 0 );
396 msg_Dbg( p_access, "removing temporary files" );
398 /* We will remove the need of tmp files */
401 msg_Dbg( p_access, "loading temporary file" );
402 fseek( p_sys->w, 0, SEEK_SET );
405 p_block = block_New( p_access, 4096 );
406 p_block->i_buffer = fread( p_block->p_buffer,
409 if( p_block->i_buffer <= 0 )
411 block_Release( p_block );
414 block_FifoPut( p_sys->p_fifo, p_block );
418 p_sys->b_opened = VLC_FALSE;
423 p_sys->t1 = p_sys->t2 = NULL;
424 p_sys->w = p_sys->r = NULL;
426 unlink( p_sys->psz_tmp1 );
427 unlink( p_sys->psz_tmp2 );
433 else if( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
435 block_FifoPut( p_sys->p_fifo, p_block );
439 /* We failed to opened files so trash new data */
440 block_Release( p_block );
443 if( (i_loop % 400) == 0 )
444 msg_Dbg( p_access, "timeshift: buff=%d", p_sys->p_fifo->i_size );
449 msg_Warn( p_access, "timeshift: EOF" );
451 /* Send dummy packet to avoid deadlock in TShiftBlock */
452 for( i_loop = 0; i_loop < 2; i_loop++ )
454 block_t *p_dummy = block_New( p_access, 128 );
456 p_dummy->i_flags |= BLOCK_FLAG_DISCONTINUITY;
457 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
459 block_FifoPut( p_sys->p_fifo, p_dummy );