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_description( _("Timeshift") );
43 set_category( CAT_INPUT );
44 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
45 set_capability( "access_filter", 0 );
46 add_shortcut( "timeshift" );
47 set_callbacks( Open, Close );
50 /*****************************************************************************
52 *****************************************************************************/
54 static block_t *Block ( access_t *p_access );
55 static int Control( access_t *, int i_query, va_list args );
56 static void Thread ( access_t * );
58 #define TIMESHIFT_FIFO_MAX (4*1024*1024)
59 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
83 access_t *p_access = (access_t*)p_this;
84 access_t *p_src = p_access->p_source;
92 /* Only work with not pace controled access */
93 if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) || b_bool )
95 msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE" );
98 /* Refuse access that can be paused */
99 if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
101 msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
106 p_access->pf_read = NULL;
107 p_access->pf_block = Block;
108 p_access->pf_seek = NULL;
109 p_access->pf_control = Control;
111 p_access->info = p_src->info;
113 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
116 p_sys->p_fifo = block_FifoNew( p_access );
117 p_sys->b_opened = VLC_FALSE;
118 p_sys->t1 = p_sys->t2 = NULL;
119 p_sys->w = p_sys->r = NULL;
123 i_size = GetTempPath( 4095, buffer );
124 if( i_size <= 0 || i_size >= 4095 )
126 if( getcwd( buffer, 4095 ) == NULL )
127 strcpy( buffer, "c:" );
129 /* remove last \\ if any */
130 if( buffer[strlen(buffer)-1] == '\\' )
131 buffer[strlen(buffer)-1] = '\0';
133 asprintf( &p_sys->psz_tmp1, "%s\\vlc-timeshift-%d-%d-1.dat",
134 buffer, GetCurrentProcessId(), p_access->i_object_id );
135 asprintf( &p_sys->psz_tmp2, "%s\\vlc-timeshift-%d-%d-2.dat",
136 buffer, GetCurrentProcessId(), p_access->i_object_id );
138 asprintf( &p_sys->psz_tmp1, "/tmp/vlc-timeshift-%d-%d-1.dat",
139 getpid(), p_access->i_object_id );
140 asprintf( &p_sys->psz_tmp2, "/tmp/vlc-timeshift-%d-%d-2.dat",
141 getpid(), p_access->i_object_id );
144 if( vlc_thread_create( p_access, "timeshift thread", Thread,
145 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
147 msg_Err( p_access, "cannot spawn timeshift access thread" );
154 /*****************************************************************************
156 *****************************************************************************/
157 static void Close( vlc_object_t *p_this )
159 access_t *p_access = (access_t*)p_this;
160 access_sys_t *p_sys = p_access->p_sys;
163 msg_Dbg( p_access, "timeshift close called" );
164 vlc_thread_join( p_access );
166 if( p_sys->b_opened )
168 if( p_sys->t1 ) fclose( p_sys->t1 );
169 if( p_sys->t2 ) fclose( p_sys->t2 );
170 unlink( p_sys->psz_tmp1 );
171 unlink( p_sys->psz_tmp2 );
174 free( p_sys->psz_tmp1 );
175 free( p_sys->psz_tmp2 );
177 block_FifoRelease( p_sys->p_fifo );
181 /*****************************************************************************
183 *****************************************************************************/
184 static block_t *Block( access_t *p_access )
186 access_sys_t *p_sys = p_access->p_sys;
188 if( p_access->b_die )
190 p_access->info.b_eof = VLC_TRUE;
194 return block_FifoGet( p_sys->p_fifo );
197 /*****************************************************************************
199 *****************************************************************************/
200 static int Control( access_t *p_access, int i_query, va_list args )
202 access_t *p_src = p_access->p_source;
211 case ACCESS_CAN_SEEK:
212 case ACCESS_CAN_FASTSEEK:
213 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
214 *pb_bool = VLC_FALSE;
217 case ACCESS_CAN_CONTROL_PACE: /* Not really true */
218 case ACCESS_CAN_PAUSE:
219 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
225 pi_int = (int*)va_arg( args, int * );
229 case ACCESS_GET_PTS_DELAY:
230 pi_64 = (int64_t*)va_arg( args, int64_t * );
231 return access2_Control( p_src, ACCESS_GET_PTS_DELAY, pi_64 );
233 case ACCESS_SET_PAUSE_STATE:
236 case ACCESS_GET_TITLE_INFO:
237 case ACCESS_SET_TITLE:
238 case ACCESS_SET_SEEKPOINT:
239 case ACCESS_GET_META:
242 case ACCESS_SET_PRIVATE_ID_STATE:
243 case ACCESS_GET_PRIVATE_ID_STATE:
244 case ACCESS_SET_PRIVATE_ID_CA:
245 return access2_vaControl( p_src, i_query, args );
248 msg_Warn( p_access, "unimplemented query in control" );
255 /*****************************************************************************
257 *****************************************************************************/
258 static void Thread( access_t *p_access )
260 access_sys_t *p_sys = p_access->p_sys;
261 access_t *p_src = p_access->p_source;
264 while( !p_access->b_die )
268 /* Get a new block */
269 if( p_src->pf_block )
271 p_block = p_src->pf_block( p_src );
273 if( p_block == NULL )
275 if( p_src->info.b_eof )
284 if( ( p_block = block_New( p_access, 2048 ) ) == NULL )
287 p_block->i_buffer = p_src->pf_read( p_src, p_block->p_buffer, 2048);
288 if( p_block->i_buffer < 0 )
290 block_Release( p_block );
291 if( p_block->i_buffer == 0 )
298 /* Open dump files if we need them */
299 if( p_sys->p_fifo->i_size >= TIMESHIFT_FIFO_MAX && !p_sys->b_opened )
301 msg_Dbg( p_access, "opening first temporary files (%s)",
304 p_sys->b_opened = VLC_TRUE;
305 p_sys->t1 = p_sys->t2 = NULL;
306 p_sys->w = p_sys->r = NULL;
308 p_sys->t1 = fopen( p_sys->psz_tmp1, "w+b" );
311 msg_Dbg( p_access, "opening second temporary files (%s)",
314 p_sys->t2 = fopen( p_sys->psz_tmp2, "w+b" );
317 p_sys->w = p_sys->t1;
320 msg_Dbg( p_access, "start writing into temporary file" );
324 msg_Err( p_access, "cannot open temporary file '%s' (%s)",
325 p_sys->psz_tmp2, strerror(errno) );
332 msg_Err( p_access, "cannot open temporary file '%s' (%s)",
333 p_sys->psz_tmp1, strerror(errno) );
342 i_write = fwrite( p_block->p_buffer, 1, p_block->i_buffer,
344 block_Release( p_block );
347 p_sys->i_w += i_write;
349 msg_Warn( p_access, "cannot write data" );
351 /* Start reading from a file if fifo isn't at 25% */
352 if( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN && !p_sys->r )
354 msg_Dbg( p_access, "start reading from temporary file (dumped=%d)", p_sys->i_w );
357 fseek( p_sys->r, 0, SEEK_SET );
359 p_sys->w = p_sys->t2;
365 while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN )
367 p_block = block_New( p_access, 4096 );
368 p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
371 if( p_block->i_buffer > 0 )
373 block_FifoPut( p_sys->p_fifo, p_block );
375 else if( p_sys->i_w > 32*1024)
378 block_Release( p_block );
380 msg_Dbg( p_access, "switching temporary files (dumped=%d)", p_sys->i_w );
382 /* Switch read/write */
386 fseek( p_sys->r, 0, SEEK_SET );
389 fseek( p_sys->w, 0, SEEK_SET );
390 ftruncate( fileno(p_sys->w), 0 );
395 msg_Dbg( p_access, "removing temporary files" );
397 /* We will remove the need of tmp files */
400 msg_Dbg( p_access, "loading temporary file" );
401 fseek( p_sys->w, 0, SEEK_SET );
404 p_block = block_New( p_access, 4096 );
405 p_block->i_buffer = fread( p_block->p_buffer,
408 if( p_block->i_buffer <= 0 )
410 block_Release( p_block );
413 block_FifoPut( p_sys->p_fifo, p_block );
417 p_sys->b_opened = VLC_FALSE;
422 p_sys->t1 = p_sys->t2 = NULL;
423 p_sys->w = p_sys->r = NULL;
425 unlink( p_sys->psz_tmp1 );
426 unlink( p_sys->psz_tmp2 );
432 else if( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
434 block_FifoPut( p_sys->p_fifo, p_block );
438 /* We failed to opened files so trash new data */
439 block_Release( p_block );
442 if( (i_loop % 400) == 0 )
443 msg_Dbg( p_access, "timeshift: buff=%d", p_sys->p_fifo->i_size );
448 msg_Warn( p_access, "timeshift: EOF" );
450 /* Send dummy packet to avoid deadlock in TShiftBlock */
451 for( i_loop = 0; i_loop < 2; i_loop++ )
453 block_t *p_dummy = block_New( p_access, 128 );
455 p_dummy->i_flags |= BLOCK_FLAG_DISCONTINUITY;
456 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
458 block_FifoPut( p_sys->p_fifo, p_dummy );