]> git.sesse.net Git - vlc/blob - modules/access_filter/timeshift.c
Remove stdlib.h
[vlc] / modules / access_filter / timeshift.c
1 /*****************************************************************************
2  * timeshift.c: access filter implementing timeshifting capabilities
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29
30 #include <stdio.h>
31
32 #include <errno.h>
33
34 #include <vlc_access.h>
35 #include <vlc_charset.h>
36 #include <vlc_input.h>
37
38 #include <unistd.h>
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
45
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 " \
52   "files." )
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." )
56
57 vlc_module_begin();
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 );
65
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,
70               VLC_FALSE );
71 vlc_module_end();
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76
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 );
84
85 #define TIMESHIFT_FIFO_MAX (10*1024*1024)
86 #define TIMESHIFT_FIFO_MIN (TIMESHIFT_FIFO_MAX/4)
87 #define TMP_FILE_MAX 256
88
89 typedef struct ts_entry_t
90 {
91     FILE *file;
92     struct ts_entry_t *p_next;
93
94 } ts_entry_t;
95
96 struct access_sys_t
97 {
98     block_fifo_t *p_fifo;
99
100     int  i_files;
101     int  i_file_size;
102     int  i_write_size;
103
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;
108
109     char *psz_filename_base;
110     char *psz_filename;
111
112     int64_t i_data;
113 };
114
115 /*****************************************************************************
116  * Open:
117  *****************************************************************************/
118 static int Open( vlc_object_t *p_this )
119 {
120     access_t *p_access = (access_t*)p_this;
121     access_t *p_src = p_access->p_source;
122     access_sys_t *p_sys;
123     vlc_bool_t b_bool;
124
125     var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
126     if( var_GetBool( p_access, "timeshift-force" ) )
127     {
128         msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" );
129     }
130     else
131     {
132         /* Only work with not pace controled access */
133         if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
134             b_bool )
135         {
136             msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
137             return VLC_EGENERIC;
138         }
139         /* Refuse access that can be paused */
140         if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
141         {
142             msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
143             return VLC_EGENERIC;
144         }
145     }
146
147     /* */
148     p_access->pf_read = NULL;
149     p_access->pf_block = Block;
150     p_access->pf_seek = Seek;
151     p_access->pf_control = Control;
152     p_access->info = p_src->info;
153
154     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
155
156     /* */
157     p_sys->p_fifo = block_FifoNew( p_access );
158     p_sys->i_write_size = 0;
159     p_sys->i_files = 0;
160     p_sys->i_data = 0;
161
162     p_sys->p_read_list = NULL;
163     p_sys->pp_read_last = &p_sys->p_read_list;
164     p_sys->p_write_list = NULL;
165     p_sys->pp_write_last = &p_sys->p_write_list;
166
167     var_Create( p_access, "timeshift-dir",
168                 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
169     var_Create( p_access, "timeshift-granularity",
170                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
171     p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
172     if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
173     p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
174
175     p_sys->psz_filename_base = GetTmpFilePath( p_access );
176     p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
177
178     if( vlc_thread_create( p_access, "timeshift thread", Thread,
179                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
180     {
181         msg_Err( p_access, "cannot spawn timeshift access thread" );
182         return VLC_EGENERIC;
183     }
184
185     return VLC_SUCCESS;
186 }
187
188 /*****************************************************************************
189  * Close:
190  *****************************************************************************/
191 static void Close( vlc_object_t *p_this )
192 {
193     access_t     *p_access = (access_t*)p_this;
194     access_sys_t *p_sys = p_access->p_sys;
195     ts_entry_t *p_entry;
196     int i;
197
198     msg_Dbg( p_access, "timeshift close called" );
199     vlc_thread_join( p_access );
200
201     for( p_entry = p_sys->p_write_list; p_entry; )
202     {
203         ts_entry_t *p_next = p_entry->p_next;
204         fclose( p_entry->file );
205         free( p_entry );
206         p_entry = p_next;
207     }
208     for( p_entry = p_sys->p_read_list; p_entry; )
209     {
210         ts_entry_t *p_next = p_entry->p_next;
211         fclose( p_entry->file );
212         free( p_entry );
213         p_entry = p_next;
214     }
215     for( i = 0; i < p_sys->i_files; i++ )
216     {
217         sprintf( p_sys->psz_filename, "%s%i.dat",
218                  p_sys->psz_filename_base, i );
219         unlink( p_sys->psz_filename );
220     }
221
222     free( p_sys->psz_filename );
223     free( p_sys->psz_filename_base );
224     block_FifoRelease( p_sys->p_fifo );
225     free( p_sys );
226 }
227
228 /*****************************************************************************
229  *
230  *****************************************************************************/
231 static block_t *Block( access_t *p_access )
232 {
233     access_sys_t *p_sys = p_access->p_sys;
234     access_t *p_src = p_access->p_source;
235     block_t *p_block = NULL;
236
237     /* Update info (we probably ought to be time caching that as well) */
238     if( p_src->info.i_update & INPUT_UPDATE_META )
239     {
240         p_src->info.i_update &= ~INPUT_UPDATE_META;
241         p_access->info.i_update |= INPUT_UPDATE_META;
242     }
243
244     /* Get data from timeshift fifo */
245     if( !p_access->info.b_eof )
246         p_block = block_FifoGet( p_sys->p_fifo );
247
248     if( p_block && !p_block->i_buffer ) /* Used to signal EOF */
249     { block_Release( p_block ); p_block = 0; }
250
251     if( p_block )
252     {
253         p_sys->i_data -= p_block->i_buffer;
254         return p_block;
255     }
256
257     p_access->info.b_eof = p_src->info.b_eof;
258     return NULL;
259 }
260
261 /*****************************************************************************
262  *
263  *****************************************************************************/
264 static void Thread( access_t *p_access )
265 {
266     access_sys_t *p_sys = p_access->p_sys;
267     access_t     *p_src = p_access->p_source;
268     block_t      *p_block;
269
270     while( !p_access->b_die )
271     {
272         /* Get a new block from the source */
273         if( p_src->pf_block )
274         {
275             p_block = p_src->pf_block( p_src );
276         }
277         else
278         {
279             if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
280
281             p_block->i_buffer =
282                 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
283
284             if( p_block->i_buffer <= 0 )
285             {
286               block_Release( p_block );
287               p_block = NULL;
288             }
289         }
290
291         if( p_block == NULL )
292         {
293           if( p_src->info.b_eof ) break;
294           msleep( 10000 );
295           continue;
296         }
297
298         p_sys->i_data += p_block->i_buffer;
299
300         /* Write block */
301         if( !p_sys->p_write_list && !p_sys->p_read_list &&
302             p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
303         {
304             /* If there isn't too much timeshifted data,
305              * write directly to FIFO */
306             block_FifoPut( p_sys->p_fifo, p_block );
307             continue;
308         }
309
310         WriteBlockToFile( p_access, p_block );
311         block_Release( p_block );
312
313         /* Read from file to fill up the fifo */
314         while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
315                !p_access->b_die )
316         {
317             p_block = ReadBlockFromFile( p_access );
318             if( !p_block ) break;
319
320             block_FifoPut( p_sys->p_fifo, p_block );
321         }
322     }
323
324     msg_Dbg( p_access, "timeshift: no more input data" );
325
326     while( !p_access->b_die &&
327            (p_sys->p_read_list || p_sys->p_fifo->i_size) )
328     {
329         /* Read from file to fill up the fifo */
330         while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
331                !p_access->b_die && p_sys->p_read_list )
332         {
333             p_block = ReadBlockFromFile( p_access );
334             if( !p_block ) break;
335
336             block_FifoPut( p_sys->p_fifo, p_block );
337         }
338
339         msleep( 100000 );
340     }
341
342     msg_Dbg( p_access, "timeshift: EOF" );
343     p_src->info.b_eof = VLC_TRUE;
344
345     /* Send dummy packet to avoid deadlock in Block() */
346     block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
347 }
348
349 /*****************************************************************************
350  * NextFileWrite:
351  *****************************************************************************/
352 static void NextFileWrite( access_t *p_access )
353 {
354     access_sys_t *p_sys = p_access->p_sys;
355     ts_entry_t   *p_next;
356
357     if( !p_sys->p_write_list )
358     {
359         p_sys->i_write_size = 0;
360         return;
361     }
362
363     p_next = p_sys->p_write_list->p_next;
364
365     /* Put written file in read list */
366     if( p_sys->i_write_size < p_sys->i_file_size )
367         ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
368
369     fseek( p_sys->p_write_list->file, 0, SEEK_SET );
370     *p_sys->pp_read_last = p_sys->p_write_list;
371     p_sys->pp_read_last = &p_sys->p_write_list->p_next;
372     p_sys->p_write_list->p_next = 0;
373
374     /* Switch to next file to write */
375     p_sys->p_write_list = p_next;
376     if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
377
378     p_sys->i_write_size = 0;
379 }
380
381 /*****************************************************************************
382  * NextFileRead:
383  *****************************************************************************/
384 static void NextFileRead( access_t *p_access )
385 {
386     access_sys_t *p_sys = p_access->p_sys;
387     ts_entry_t   *p_next;
388
389     if( !p_sys->p_read_list ) return;
390
391     p_next = p_sys->p_read_list->p_next;
392
393     /* Put read file in write list */
394     fseek( p_sys->p_read_list->file, 0, SEEK_SET );
395     *p_sys->pp_write_last = p_sys->p_read_list;
396     p_sys->pp_write_last = &p_sys->p_read_list->p_next;
397     p_sys->p_read_list->p_next = 0;
398
399     /* Switch to next file to read */
400     p_sys->p_read_list = p_next;
401     if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
402 }
403
404 /*****************************************************************************
405  * WriteBlockToFile:
406  *****************************************************************************/
407 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
408 {
409     access_sys_t *p_sys = p_access->p_sys;
410     int i_write, i_buffer;
411
412     if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
413
414     /* Open new file if necessary */
415     if( !p_sys->p_write_list )
416     {
417         FILE *file;
418
419         sprintf( p_sys->psz_filename, "%s%i.dat",
420                  p_sys->psz_filename_base, p_sys->i_files );
421         file = utf8_fopen( p_sys->psz_filename, "w+b" );
422
423         if( !file && p_sys->i_files < 2 )
424         {
425             /* We just can't work with less than 2 buffer files */
426             msg_Err( p_access, "cannot open temporary file '%s' (%s)",
427                      p_sys->psz_filename, strerror(errno) );
428             return VLC_EGENERIC;
429         }
430         else if( !file ) return VLC_EGENERIC;
431
432         p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
433         p_sys->p_write_list->p_next = 0;
434         p_sys->p_write_list->file = file;
435         p_sys->pp_write_last = &p_sys->p_write_list->p_next;
436
437         p_sys->i_files++;
438     }
439
440     /* Write to file */
441     i_buffer = __MIN( p_block->i_buffer,
442                       p_sys->i_file_size - p_sys->i_write_size );
443
444     i_write = fwrite( p_block->p_buffer, 1, i_buffer,
445                       p_sys->p_write_list->file );
446
447     if( i_write > 0 ) p_sys->i_write_size += i_write;
448
449     //p_access->info.i_size += i_write;
450     //p_access->info.i_update |= INPUT_UPDATE_SIZE;
451
452     if( i_write < i_buffer )
453     {
454         /* Looks like we're short of space */
455
456         if( !p_sys->p_write_list->p_next )
457         {
458             msg_Warn( p_access, "no more space, overwritting old data" );
459             NextFileRead( p_access );
460             NextFileRead( p_access );
461         }
462
463         /* Make sure we switch to next file in write list */
464         p_sys->i_write_size = p_sys->i_file_size;
465     }
466
467     p_block->p_buffer += i_write;
468     p_block->i_buffer -= i_write;
469
470     /* Check if we have some data left */
471     if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
472
473     return VLC_SUCCESS;
474 }
475
476 /*****************************************************************************
477  * ReadBlockFromFile:
478  *****************************************************************************/
479 static block_t *ReadBlockFromFile( access_t *p_access )
480 {
481     access_sys_t *p_sys = p_access->p_sys;
482     block_t *p_block;
483
484     if( !p_sys->p_read_list && p_sys->p_write_list )
485     {
486         /* Force switching to next write file, that should
487          * give us something to read */
488         NextFileWrite( p_access );
489     }
490
491     if( !p_sys->p_read_list ) return 0;
492
493     p_block = block_New( p_access, 4096 );
494     p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
495                                p_sys->p_read_list->file );
496
497     if( p_block->i_buffer == 0 ) NextFileRead( p_access );
498
499     //p_access->info.i_size -= p_block->i_buffer;
500     //p_access->info.i_update |= INPUT_UPDATE_SIZE;
501
502     return p_block;
503 }
504
505 /*****************************************************************************
506  * Seek: seek to a specific location in a file
507  *****************************************************************************/
508 static int Seek( access_t *p_access, int64_t i_pos )
509 {
510     //access_sys_t *p_sys = p_access->p_sys;
511     return VLC_SUCCESS;
512 }
513
514 /*****************************************************************************
515  *
516  *****************************************************************************/
517 static int Control( access_t *p_access, int i_query, va_list args )
518 {
519     vlc_bool_t   *pb_bool;
520     int          *pi_int;
521
522     switch( i_query )
523     {
524     case ACCESS_CAN_SEEK:
525     case ACCESS_CAN_FASTSEEK:
526         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
527         *pb_bool = VLC_TRUE;
528         break;
529
530     case ACCESS_CAN_CONTROL_PACE:   /* Not really true */
531     case ACCESS_CAN_PAUSE:
532         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
533         *pb_bool = VLC_TRUE;
534         break;
535
536     case ACCESS_GET_MTU:
537         pi_int = (int*)va_arg( args, int * );
538         *pi_int = 0;
539         break;
540
541     case ACCESS_SET_PAUSE_STATE: 
542         break;
543
544     /* Forward everything else to the source access */
545     default:
546         return access2_vaControl( p_access->p_source, i_query, args );
547     }
548     return VLC_SUCCESS;
549 }
550
551 /*****************************************************************************
552  * GetTmpFilePath:
553  *****************************************************************************/
554 #ifdef WIN32
555 #define getpid() GetCurrentProcessId()
556 #endif
557 static char *GetTmpFilePath( access_t *p_access )
558 {
559     char *psz_dir = var_GetString( p_access, "timeshift-dir" );
560     char *psz_filename_base;
561
562     if( ( psz_dir != NULL ) && ( psz_dir[0] == '\0' ) )
563     {
564         free( psz_dir );
565         psz_dir = NULL;
566     }
567
568     if( psz_dir == NULL )
569     {
570 #ifdef WIN32
571         DWORD ret = GetTempPathW (0, NULL);
572         wchar_t wdir[ret + 3]; // can at least old "C:" + nul
573         const wchar_t *pwdir = wdir;
574         wchar_t *pwdir_free = NULL;
575
576         if (GetTempPathW (ret + 1, wdir) == 0)
577         {
578             pwdir_free = pwdir = _wgetcwd (NULL, 0);
579             if (pwdir == NULL)
580                 pwdir = L"C:";
581         }
582
583         psz_dir = FromWide (pwdir);
584         if (pwdir_free != NULL)
585             free (pwdir_free);
586
587         /* remove trailing antislash if any */
588         if (psz_dir[strlen (psz_dir) - 1] == '\\')
589             psz_dir[strlen (psz_dir) - 1] = '\0';
590 #else
591         psz_dir = strdup( "/tmp" );
592 #endif
593     }
594
595     asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
596               psz_dir, getpid(), p_access->i_object_id );
597     free( psz_dir );
598
599     return psz_filename_base;
600 }