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