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