X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=modules%2Faccess_filter%2Ftimeshift.c;h=13511dc0e54b63e24b65acf72d668190122dd103;hb=fa9abba62f600b1c019da926c5ed1c6752f80fb5;hp=e7beaff2472edb68dd7342277782357f17a228e9;hpb=ad69969da07b08850d7bec945f7e194897549d38;p=vlc diff --git a/modules/access_filter/timeshift.c b/modules/access_filter/timeshift.c index e7beaff247..13511dc0e5 100644 --- a/modules/access_filter/timeshift.c +++ b/modules/access_filter/timeshift.c @@ -19,20 +19,30 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** * Preamble *****************************************************************************/ -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include #include -#include -#include +#include +#include +#include + #include +#ifdef WIN32 +# include /* _wgetcwd */ +#endif + /***************************************************************************** * Module descriptor *****************************************************************************/ @@ -40,11 +50,15 @@ static int Open ( vlc_object_t * ); static void Close( vlc_object_t * ); #define GRANULARITY_TEXT N_("Timeshift granularity") -#define GRANULARITY_LONGTEXT N_( "Size of the temporary files use to store " \ - "the timeshifted stream." ) +/// \bug [String] typo +#define GRANULARITY_LONGTEXT N_( "This is the size of the temporary files " \ + "that will be used to store the timeshifted streams." ) #define DIR_TEXT N_("Timeshift directory") #define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \ "files." ) +#define FORCE_TEXT N_("Force use of the timeshift module") +#define FORCE_LONGTEXT N_("Force use of the timeshift module even if the " \ + "access declares that it can control pace or pause." ) vlc_module_begin(); set_shortname( _("Timeshift") ); @@ -58,6 +72,9 @@ vlc_module_begin(); add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT, GRANULARITY_LONGTEXT, VLC_TRUE ); add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, VLC_FALSE ); + change_unsafe(); + add_bool( "timeshift-force", VLC_FALSE, NULL, FORCE_TEXT, FORCE_LONGTEXT, + VLC_FALSE ); vlc_module_end(); /***************************************************************************** @@ -98,6 +115,8 @@ struct access_sys_t char *psz_filename_base; char *psz_filename; + + int64_t i_data; }; /***************************************************************************** @@ -110,17 +129,26 @@ static int Open( vlc_object_t *p_this ) access_sys_t *p_sys; vlc_bool_t b_bool; - /* Only work with not pace controled access */ - if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) || b_bool ) + var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT ); + if( var_GetBool( p_access, "timeshift-force" ) ) { - msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE" ); - return VLC_EGENERIC; + msg_Dbg( p_access, "Forcing use of timeshift even if access can control pace or pause" ); } - /* Refuse access that can be paused */ - if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool ) + else { - msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" ); - return VLC_EGENERIC; + /* Only work with not pace controled access */ + if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) || + b_bool ) + { + msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" ); + return VLC_EGENERIC; + } + /* Refuse access that can be paused */ + if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool ) + { + msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" ); + return VLC_EGENERIC; + } } /* */ @@ -136,6 +164,7 @@ static int Open( vlc_object_t *p_this ) p_sys->p_fifo = block_FifoNew( p_access ); p_sys->i_write_size = 0; p_sys->i_files = 0; + p_sys->i_data = 0; p_sys->p_read_list = NULL; p_sys->pp_read_last = &p_sys->p_read_list; @@ -209,17 +238,31 @@ static void Close( vlc_object_t *p_this ) static block_t *Block( access_t *p_access ) { access_sys_t *p_sys = p_access->p_sys; - block_t *p_block; + access_t *p_src = p_access->p_source; + block_t *p_block = NULL; - if( p_access->b_die ) + /* Update info (we probably ought to be time caching that as well) */ + if( p_src->info.i_update & INPUT_UPDATE_META ) { - p_access->info.b_eof = VLC_TRUE; - return NULL; + p_src->info.i_update &= ~INPUT_UPDATE_META; + p_access->info.i_update |= INPUT_UPDATE_META; } - p_block = block_FifoGet( p_sys->p_fifo ); - //p_access->info.i_size -= p_block->i_buffer; - return p_block; + /* Get data from timeshift fifo */ + if( !p_access->info.b_eof ) + p_block = block_FifoGet( p_sys->p_fifo ); + + if( p_block && !p_block->i_buffer ) /* Used to signal EOF */ + { block_Release( p_block ); p_block = 0; } + + if( p_block ) + { + p_sys->i_data -= p_block->i_buffer; + return p_block; + } + + p_access->info.b_eof = p_src->info.b_eof; + return NULL; } /***************************************************************************** @@ -229,23 +272,14 @@ static void Thread( access_t *p_access ) { access_sys_t *p_sys = p_access->p_sys; access_t *p_src = p_access->p_source; - int i; + block_t *p_block; while( !p_access->b_die ) { - block_t *p_block; - /* Get a new block from the source */ if( p_src->pf_block ) { p_block = p_src->pf_block( p_src ); - - if( p_block == NULL ) - { - if( p_src->info.b_eof ) break; - msleep( 1000 ); - continue; - } } else { @@ -254,27 +288,29 @@ static void Thread( access_t *p_access ) p_block->i_buffer = p_src->pf_read( p_src, p_block->p_buffer, 2048 ); - if( p_block->i_buffer < 0 ) + if( p_block->i_buffer <= 0 ) { - block_Release( p_block ); - if( p_block->i_buffer == 0 ) break; - msleep( 1000 ); - continue; + block_Release( p_block ); + p_block = NULL; } } + if( p_block == NULL ) + { + if( p_src->info.b_eof ) break; + msleep( 10000 ); + continue; + } + + p_sys->i_data += p_block->i_buffer; + /* Write block */ if( !p_sys->p_write_list && !p_sys->p_read_list && - p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX ) + block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MAX ) { /* If there isn't too much timeshifted data, * write directly to FIFO */ block_FifoPut( p_sys->p_fifo, p_block ); - - //p_access->info.i_size += p_block->i_buffer; - //p_access->info.i_update |= INPUT_UPDATE_SIZE; - - /* Nothing else to do */ continue; } @@ -282,25 +318,39 @@ static void Thread( access_t *p_access ) block_Release( p_block ); /* Read from file to fill up the fifo */ - while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN && + while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN && !p_access->b_die ) { p_block = ReadBlockFromFile( p_access ); if( !p_block ) break; + block_FifoPut( p_sys->p_fifo, p_block ); } } - msg_Dbg( p_access, "timeshift: EOF" ); + msg_Dbg( p_access, "timeshift: no more input data" ); - /* Send dummy packet to avoid deadlock in TShiftBlock */ - for( i = 0; i < 2; i++ ) + while( !p_access->b_die && + (p_sys->p_read_list || block_FifoSize( p_sys->p_fifo ) ) ) { - block_t *p_dummy = block_New( p_access, 128 ); - p_dummy->i_flags |= BLOCK_FLAG_DISCONTINUITY; - memset( p_dummy->p_buffer, 0, p_dummy->i_buffer ); - block_FifoPut( p_sys->p_fifo, p_dummy ); + /* Read from file to fill up the fifo */ + while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN && + !p_access->b_die && p_sys->p_read_list ) + { + p_block = ReadBlockFromFile( p_access ); + if( !p_block ) break; + + block_FifoPut( p_sys->p_fifo, p_block ); + } + + msleep( 100000 ); } + + msg_Dbg( p_access, "timeshift: EOF" ); + p_src->info.b_eof = VLC_TRUE; + + /* Send dummy packet to avoid deadlock in Block() */ + block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) ); } /***************************************************************************** @@ -375,13 +425,13 @@ static int WriteBlockToFile( access_t *p_access, block_t *p_block ) sprintf( p_sys->psz_filename, "%s%i.dat", p_sys->psz_filename_base, p_sys->i_files ); - file = fopen( p_sys->psz_filename, "w+b" ); + file = utf8_fopen( p_sys->psz_filename, "w+b" ); if( !file && p_sys->i_files < 2 ) { /* We just can't work with less than 2 buffer files */ - msg_Err( p_access, "cannot open temporary file '%s' (%s)", - p_sys->psz_filename, strerror(errno) ); + msg_Err( p_access, "cannot open temporary file '%s' (%m)", + p_sys->psz_filename ); return VLC_EGENERIC; } else if( !file ) return VLC_EGENERIC; @@ -473,53 +523,34 @@ static int Seek( access_t *p_access, int64_t i_pos ) *****************************************************************************/ static int Control( access_t *p_access, int i_query, va_list args ) { - access_t *p_src = p_access->p_source; - vlc_bool_t *pb_bool; int *pi_int; - int64_t *pi_64; switch( i_query ) { - case ACCESS_CAN_SEEK: - case ACCESS_CAN_FASTSEEK: - pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* ); - *pb_bool = VLC_TRUE; - break; - - case ACCESS_CAN_CONTROL_PACE: /* Not really true */ - case ACCESS_CAN_PAUSE: - pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* ); - *pb_bool = VLC_TRUE; - break; - - case ACCESS_GET_MTU: - pi_int = (int*)va_arg( args, int * ); - *pi_int = 0; - break; - - case ACCESS_GET_PTS_DELAY: - pi_64 = (int64_t*)va_arg( args, int64_t * ); - return access2_Control( p_src, ACCESS_GET_PTS_DELAY, pi_64 ); - - case ACCESS_SET_PAUSE_STATE: - return VLC_SUCCESS; - - case ACCESS_GET_TITLE_INFO: - case ACCESS_SET_TITLE: - case ACCESS_SET_SEEKPOINT: - case ACCESS_GET_META: - return VLC_EGENERIC; - - case ACCESS_SET_PRIVATE_ID_STATE: - case ACCESS_GET_PRIVATE_ID_STATE: - case ACCESS_SET_PRIVATE_ID_CA: - return access2_vaControl( p_src, i_query, args ); - - default: - msg_Warn( p_access, "unimplemented query in control" ); - return VLC_EGENERIC; - + case ACCESS_CAN_SEEK: + case ACCESS_CAN_FASTSEEK: + pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* ); + *pb_bool = VLC_TRUE; + break; + + case ACCESS_CAN_CONTROL_PACE: /* Not really true */ + case ACCESS_CAN_PAUSE: + pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* ); + *pb_bool = VLC_TRUE; + break; + + case ACCESS_GET_MTU: + pi_int = (int*)va_arg( args, int * ); + *pi_int = 0; + break; + + case ACCESS_SET_PAUSE_STATE: + break; + + /* Forward everything else to the source access */ + default: + return access2_vaControl( p_access->p_source, i_query, args ); } return VLC_SUCCESS; } @@ -528,42 +559,43 @@ static int Control( access_t *p_access, int i_query, va_list args ) * GetTmpFilePath: *****************************************************************************/ #ifdef WIN32 -#define getpid() GetCurrentProcessId() +#define getpid() (int)GetCurrentProcessId() #endif static char *GetTmpFilePath( access_t *p_access ) { - char *psz_dir = var_GetString( p_access, "timeshift-dir" ); + char *psz_dir = var_GetNonEmptyString( p_access, "timeshift-dir" ); char *psz_filename_base; - if( psz_dir && !*psz_dir ) - { - free( psz_dir ); - psz_dir = 0; - } - - if( !psz_dir ) + if( psz_dir == NULL ) { #ifdef WIN32 - int i_size; + DWORD ret = GetTempPathW (0, NULL); + wchar_t wdir[ret + 3]; // can at least old "C:" + nul + const wchar_t *pwdir = wdir; + wchar_t *pwdir_free = NULL; - psz_dir = malloc( MAX_PATH + 1 ); - i_size = GetTempPath( MAX_PATH, psz_dir ); - if( i_size <= 0 || i_size > MAX_PATH ) + if (GetTempPathW (ret + 1, wdir) == 0) { - if( !getcwd( psz_dir, MAX_PATH ) ) strcpy( psz_dir, "c:" ); + pwdir_free = pwdir = _wgetcwd (NULL, 0); + if (pwdir == NULL) + pwdir = L"C:"; } - /* remove last \\ if any */ - if( psz_dir[strlen(psz_dir)-1] == '\\' ) - psz_dir[strlen(psz_dir)-1] = '\0'; -#else + psz_dir = FromWide (pwdir); + if (pwdir_free != NULL) + free (pwdir_free); + /* remove trailing antislash if any */ + if (psz_dir[strlen (psz_dir) - 1] == '\\') + psz_dir[strlen (psz_dir) - 1] = '\0'; +#else psz_dir = strdup( "/tmp" ); #endif } asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-", psz_dir, getpid(), p_access->i_object_id ); + free( psz_dir ); return psz_filename_base; }