]> git.sesse.net Git - vlc/blob - modules/access_filter/timeshift.c
A bit of headers cleanup
[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 #include <stdlib.h>
32
33 #include <errno.h>
34
35 #include <vlc_access.h>
36 #include <vlc_charset.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
113 /*****************************************************************************
114  * Open:
115  *****************************************************************************/
116 static int Open( vlc_object_t *p_this )
117 {
118     access_t *p_access = (access_t*)p_this;
119     access_t *p_src = p_access->p_source;
120     access_sys_t *p_sys;
121     vlc_bool_t b_bool;
122
123     var_Create( p_access, "timeshift-force",
124                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
125     if( var_GetBool( p_access, "timeshift-force" ) == VLC_TRUE )
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
160     p_sys->p_read_list = NULL;
161     p_sys->pp_read_last = &p_sys->p_read_list;
162     p_sys->p_write_list = NULL;
163     p_sys->pp_write_last = &p_sys->p_write_list;
164
165     var_Create( p_access, "timeshift-dir",
166                 VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
167     var_Create( p_access, "timeshift-granularity",
168                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
169     p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
170     if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
171     p_sys->i_file_size *= 1024 * 1024; /* In MBytes */
172
173     p_sys->psz_filename_base = GetTmpFilePath( p_access );
174     p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
175
176     if( vlc_thread_create( p_access, "timeshift thread", Thread,
177                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
178     {
179         msg_Err( p_access, "cannot spawn timeshift access thread" );
180         return VLC_EGENERIC;
181     }
182
183     return VLC_SUCCESS;
184 }
185
186 /*****************************************************************************
187  * Close:
188  *****************************************************************************/
189 static void Close( vlc_object_t *p_this )
190 {
191     access_t     *p_access = (access_t*)p_this;
192     access_sys_t *p_sys = p_access->p_sys;
193     ts_entry_t *p_entry;
194     int i;
195
196     msg_Dbg( p_access, "timeshift close called" );
197     vlc_thread_join( p_access );
198
199     for( p_entry = p_sys->p_write_list; p_entry; )
200     {
201         ts_entry_t *p_next = p_entry->p_next;
202         fclose( p_entry->file );
203         free( p_entry );
204         p_entry = p_next;
205     }
206     for( p_entry = p_sys->p_read_list; p_entry; )
207     {
208         ts_entry_t *p_next = p_entry->p_next;
209         fclose( p_entry->file );
210         free( p_entry );
211         p_entry = p_next;
212     }
213     for( i = 0; i < p_sys->i_files; i++ )
214     {
215         sprintf( p_sys->psz_filename, "%s%i.dat",
216                  p_sys->psz_filename_base, i );
217         unlink( p_sys->psz_filename );
218     }
219
220     free( p_sys->psz_filename );
221     free( p_sys->psz_filename_base );
222     block_FifoRelease( p_sys->p_fifo );
223     free( p_sys );
224 }
225
226 /*****************************************************************************
227  *
228  *****************************************************************************/
229 static block_t *Block( access_t *p_access )
230 {
231     access_sys_t *p_sys = p_access->p_sys;
232     block_t *p_block;
233
234     if( p_access->b_die )
235     {
236         p_access->info.b_eof = VLC_TRUE;
237         return NULL;
238     }
239
240     p_block = block_FifoGet( p_sys->p_fifo );
241     //p_access->info.i_size -= p_block->i_buffer;
242     return p_block;
243 }
244
245 /*****************************************************************************
246  *
247  *****************************************************************************/
248 static void Thread( access_t *p_access )
249 {
250     access_sys_t *p_sys = p_access->p_sys;
251     access_t     *p_src = p_access->p_source;
252     int i;
253
254     while( !p_access->b_die )
255     {
256         block_t *p_block;
257
258         /* Get a new block from the source */
259         if( p_src->pf_block )
260         {
261             p_block = p_src->pf_block( p_src );
262
263             if( p_block == NULL )
264             {
265                 if( p_src->info.b_eof ) break;
266                 msleep( 1000 );
267                 continue;
268             }
269         }
270         else
271         {
272             if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;
273
274             p_block->i_buffer =
275                 p_src->pf_read( p_src, p_block->p_buffer, 2048 );
276
277             if( p_block->i_buffer < 0 )
278             {
279                 block_Release( p_block );
280                 if( p_block->i_buffer == 0 ) break;
281                 msleep( 1000 );
282                 continue;
283             }
284         }
285
286         /* Write block */
287         if( !p_sys->p_write_list && !p_sys->p_read_list &&
288             p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
289         {
290             /* If there isn't too much timeshifted data,
291              * write directly to FIFO */
292             block_FifoPut( p_sys->p_fifo, p_block );
293
294             //p_access->info.i_size += p_block->i_buffer;
295             //p_access->info.i_update |= INPUT_UPDATE_SIZE;
296
297             /* Nothing else to do */
298             continue;
299         }
300
301         WriteBlockToFile( p_access, p_block );
302         block_Release( p_block );
303
304         /* Read from file to fill up the fifo */
305         while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
306                !p_access->b_die )
307         {
308             p_block = ReadBlockFromFile( p_access );
309             if( !p_block ) break;
310             block_FifoPut( p_sys->p_fifo, p_block );
311         }
312     }
313
314     msg_Dbg( p_access, "timeshift: EOF" );
315
316     /* Send dummy packet to avoid deadlock in TShiftBlock */
317     for( i = 0; i < 2; i++ )
318     {
319         block_t *p_dummy = block_New( p_access, 128 );
320         p_dummy->i_flags |= BLOCK_FLAG_DISCONTINUITY;
321         memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
322         block_FifoPut( p_sys->p_fifo, p_dummy );
323     }
324 }
325
326 /*****************************************************************************
327  * NextFileWrite:
328  *****************************************************************************/
329 static void NextFileWrite( access_t *p_access )
330 {
331     access_sys_t *p_sys = p_access->p_sys;
332     ts_entry_t   *p_next;
333
334     if( !p_sys->p_write_list )
335     {
336         p_sys->i_write_size = 0;
337         return;
338     }
339
340     p_next = p_sys->p_write_list->p_next;
341
342     /* Put written file in read list */
343     if( p_sys->i_write_size < p_sys->i_file_size )
344         ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );
345
346     fseek( p_sys->p_write_list->file, 0, SEEK_SET );
347     *p_sys->pp_read_last = p_sys->p_write_list;
348     p_sys->pp_read_last = &p_sys->p_write_list->p_next;
349     p_sys->p_write_list->p_next = 0;
350
351     /* Switch to next file to write */
352     p_sys->p_write_list = p_next;
353     if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;
354
355     p_sys->i_write_size = 0;
356 }
357
358 /*****************************************************************************
359  * NextFileRead:
360  *****************************************************************************/
361 static void NextFileRead( access_t *p_access )
362 {
363     access_sys_t *p_sys = p_access->p_sys;
364     ts_entry_t   *p_next;
365
366     if( !p_sys->p_read_list ) return;
367
368     p_next = p_sys->p_read_list->p_next;
369
370     /* Put read file in write list */
371     fseek( p_sys->p_read_list->file, 0, SEEK_SET );
372     *p_sys->pp_write_last = p_sys->p_read_list;
373     p_sys->pp_write_last = &p_sys->p_read_list->p_next;
374     p_sys->p_read_list->p_next = 0;
375
376     /* Switch to next file to read */
377     p_sys->p_read_list = p_next;
378     if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
379 }
380
381 /*****************************************************************************
382  * WriteBlockToFile:
383  *****************************************************************************/
384 static int WriteBlockToFile( access_t *p_access, block_t *p_block )
385 {
386     access_sys_t *p_sys = p_access->p_sys;
387     int i_write, i_buffer;
388
389     if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );
390
391     /* Open new file if necessary */
392     if( !p_sys->p_write_list )
393     {
394         FILE *file;
395
396         sprintf( p_sys->psz_filename, "%s%i.dat",
397                  p_sys->psz_filename_base, p_sys->i_files );
398         file = utf8_fopen( p_sys->psz_filename, "w+b" );
399
400         if( !file && p_sys->i_files < 2 )
401         {
402             /* We just can't work with less than 2 buffer files */
403             msg_Err( p_access, "cannot open temporary file '%s' (%s)",
404                      p_sys->psz_filename, strerror(errno) );
405             return VLC_EGENERIC;
406         }
407         else if( !file ) return VLC_EGENERIC;
408
409         p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
410         p_sys->p_write_list->p_next = 0;
411         p_sys->p_write_list->file = file;
412         p_sys->pp_write_last = &p_sys->p_write_list->p_next;
413
414         p_sys->i_files++;
415     }
416
417     /* Write to file */
418     i_buffer = __MIN( p_block->i_buffer,
419                       p_sys->i_file_size - p_sys->i_write_size );
420
421     i_write = fwrite( p_block->p_buffer, 1, i_buffer,
422                       p_sys->p_write_list->file );
423
424     if( i_write > 0 ) p_sys->i_write_size += i_write;
425
426     //p_access->info.i_size += i_write;
427     //p_access->info.i_update |= INPUT_UPDATE_SIZE;
428
429     if( i_write < i_buffer )
430     {
431         /* Looks like we're short of space */
432
433         if( !p_sys->p_write_list->p_next )
434         {
435             msg_Warn( p_access, "no more space, overwritting old data" );
436             NextFileRead( p_access );
437             NextFileRead( p_access );
438         }
439
440         /* Make sure we switch to next file in write list */
441         p_sys->i_write_size = p_sys->i_file_size;
442     }
443
444     p_block->p_buffer += i_write;
445     p_block->i_buffer -= i_write;
446
447     /* Check if we have some data left */
448     if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );
449
450     return VLC_SUCCESS;
451 }
452
453 /*****************************************************************************
454  * ReadBlockFromFile:
455  *****************************************************************************/
456 static block_t *ReadBlockFromFile( access_t *p_access )
457 {
458     access_sys_t *p_sys = p_access->p_sys;
459     block_t *p_block;
460
461     if( !p_sys->p_read_list && p_sys->p_write_list )
462     {
463         /* Force switching to next write file, that should
464          * give us something to read */
465         NextFileWrite( p_access );
466     }
467
468     if( !p_sys->p_read_list ) return 0;
469
470     p_block = block_New( p_access, 4096 );
471     p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
472                                p_sys->p_read_list->file );
473
474     if( p_block->i_buffer == 0 ) NextFileRead( p_access );
475
476     //p_access->info.i_size -= p_block->i_buffer;
477     //p_access->info.i_update |= INPUT_UPDATE_SIZE;
478
479     return p_block;
480 }
481
482 /*****************************************************************************
483  * Seek: seek to a specific location in a file
484  *****************************************************************************/
485 static int Seek( access_t *p_access, int64_t i_pos )
486 {
487     //access_sys_t *p_sys = p_access->p_sys;
488     return VLC_SUCCESS;
489 }
490
491 /*****************************************************************************
492  *
493  *****************************************************************************/
494 static int Control( access_t *p_access, int i_query, va_list args )
495 {
496     access_t     *p_src = p_access->p_source;
497
498     vlc_bool_t   *pb_bool;
499     int          *pi_int;
500     int64_t      *pi_64;
501
502     switch( i_query )
503     {
504         case ACCESS_CAN_SEEK:
505         case ACCESS_CAN_FASTSEEK:
506             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
507             *pb_bool = VLC_TRUE;
508             break;
509
510         case ACCESS_CAN_CONTROL_PACE:   /* Not really true */
511         case ACCESS_CAN_PAUSE:
512             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
513             *pb_bool = VLC_TRUE;
514             break;
515
516         case ACCESS_GET_MTU:
517             pi_int = (int*)va_arg( args, int * );
518             *pi_int = 0;
519             break;
520
521         case ACCESS_GET_PTS_DELAY:
522             pi_64 = (int64_t*)va_arg( args, int64_t * );
523             return access2_Control( p_src, ACCESS_GET_PTS_DELAY, pi_64 );
524
525         case ACCESS_SET_PAUSE_STATE:
526             return VLC_SUCCESS;
527
528         case ACCESS_GET_TITLE_INFO:
529         case ACCESS_SET_TITLE:
530         case ACCESS_SET_SEEKPOINT:
531         case ACCESS_GET_META:
532             return VLC_EGENERIC;
533
534         case ACCESS_SET_PRIVATE_ID_STATE:
535         case ACCESS_GET_PRIVATE_ID_STATE:
536         case ACCESS_SET_PRIVATE_ID_CA:
537             return access2_vaControl( p_src, i_query, args );
538
539         default:
540             msg_Warn( p_access, "unimplemented query in control" );
541             return VLC_EGENERIC;
542
543     }
544     return VLC_SUCCESS;
545 }
546
547 /*****************************************************************************
548  * GetTmpFilePath:
549  *****************************************************************************/
550 #ifdef WIN32
551 #define getpid() GetCurrentProcessId()
552 #endif
553 static char *GetTmpFilePath( access_t *p_access )
554 {
555     char *psz_dir = var_GetString( p_access, "timeshift-dir" );
556     char *psz_filename_base;
557
558     if( ( psz_dir != NULL ) && ( psz_dir[0] == '\0' ) )
559     {
560         free( psz_dir );
561         psz_dir = NULL;
562     }
563
564     if( psz_dir == NULL )
565     {
566 #ifdef WIN32
567         DWORD ret = GetTempPathW (0, NULL);
568         wchar_t wdir[ret + 3]; // can at least old "C:" + nul
569         const wchar_t *pwdir = wdir;
570         wchar_t *pwdir_free = NULL;
571
572         if (GetTempPathW (ret + 1, wdir) == 0)
573         {
574             pwdir_free = pwdir = _wgetcwd (NULL, 0);
575             if (pwdir == NULL)
576                 pwdir = L"C:";
577         }
578
579         psz_dir = FromWide (pwdir);
580         if (pwdir_free != NULL)
581             free (pwdir_free);
582
583         /* remove trailing antislash if any */
584         if (psz_dir[strlen (psz_dir) - 1] == '\\')
585             psz_dir[strlen (psz_dir) - 1] = '\0';
586 #else
587         psz_dir = strdup( "/tmp" );
588 #endif
589     }
590
591     asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
592               psz_dir, getpid(), p_access->i_object_id );
593     free( psz_dir );
594
595     return psz_filename_base;
596 }