timeshift.c 18.2 KB
Newer Older
1
/*****************************************************************************
2
 * timeshift.c: access filter implementing timeshifting capabilities
3
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5
 * $Id$
6
 *
7 8
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Gildas Bazin <gbazin@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
Rémi Denis-Courmont's avatar
More  
Rémi Denis-Courmont committed
28 29 30
#include <vlc/vlc.h>

#include <stdio.h>
31

32 33
#include <errno.h>

zorglub's avatar
zorglub committed
34 35
#include <vlc_access.h>
#include <vlc_charset.h>
36
#include <vlc_input.h>
37

38 39 40 41 42 43 44 45
#include <unistd.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

46
#define GRANULARITY_TEXT N_("Timeshift granularity")
zorglub's avatar
Strings  
zorglub committed
47
/// \bug [String] typo
zorglub's avatar
zorglub committed
48
#define GRANULARITY_LONGTEXT N_( "This is the size of the temporary files " \
Jean-Paul Saman's avatar
Jean-Paul Saman committed
49
  "that will be used to store the timeshifted streams." )
50 51 52
#define DIR_TEXT N_("Timeshift directory")
#define DIR_LONGTEXT N_( "Directory used to store the timeshift temporary " \
  "files." )
53 54 55
#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." )
56

57
vlc_module_begin();
Laurent Aimar's avatar
Laurent Aimar committed
58
    set_shortname( _("Timeshift") );
59 60 61 62 63 64
    set_description( _("Timeshift") );
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
    set_capability( "access_filter", 0 );
    add_shortcut( "timeshift" );
    set_callbacks( Open, Close );
65 66 67 68

    add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
                 GRANULARITY_LONGTEXT, VLC_TRUE );
    add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, VLC_FALSE );
69 70
    add_bool( "timeshift-force", VLC_FALSE, NULL, FORCE_TEXT, FORCE_LONGTEXT,
              VLC_FALSE );
71 72 73 74 75 76
vlc_module_end();

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

77
static int      Seek( access_t *, int64_t );
78 79
static block_t *Block  ( access_t *p_access );
static int      Control( access_t *, int i_query, va_list args );
80 81 82 83
static void     Thread ( access_t *p_access );
static int      WriteBlockToFile( access_t *p_access, block_t *p_block );
static block_t *ReadBlockFromFile( access_t *p_access );
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 92
    FILE *file;
    struct ts_entry_t *p_next;
93

94
} ts_entry_t;
95

96 97 98
struct access_sys_t
{
    block_fifo_t *p_fifo;
99

100 101 102
    int  i_files;
    int  i_file_size;
    int  i_write_size;
103

104 105 106 107
    ts_entry_t *p_read_list;
    ts_entry_t **pp_read_last;
    ts_entry_t *p_write_list;
    ts_entry_t **pp_write_last;
108

109 110
    char *psz_filename_base;
    char *psz_filename;
111 112

    int64_t i_data;
113 114 115 116 117 118 119 120 121 122 123 124
};

/*****************************************************************************
 * Open:
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    access_t *p_access = (access_t*)p_this;
    access_t *p_src = p_access->p_source;
    access_sys_t *p_sys;
    vlc_bool_t b_bool;

125 126
    var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
    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 134
        if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
            b_bool )
135 136 137 138 139 140 141 142 143 144
        {
            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;
        }
145 146 147 148 149
    }

    /* */
    p_access->pf_read = NULL;
    p_access->pf_block = Block;
150
    p_access->pf_seek = Seek;
151 152 153 154 155 156 157
    p_access->pf_control = Control;
    p_access->info = p_src->info;

    p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );

    /* */
    p_sys->p_fifo = block_FifoNew( p_access );
158 159
    p_sys->i_write_size = 0;
    p_sys->i_files = 0;
160
    p_sys->i_data = 0;
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    p_sys->p_read_list = NULL;
    p_sys->pp_read_last = &p_sys->p_read_list;
    p_sys->p_write_list = NULL;
    p_sys->pp_write_last = &p_sys->p_write_list;

    var_Create( p_access, "timeshift-dir",
                VLC_VAR_DIRECTORY | VLC_VAR_DOINHERIT );
    var_Create( p_access, "timeshift-granularity",
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    p_sys->i_file_size = var_GetInteger( p_access, "timeshift-granularity" );
    if( p_sys->i_file_size < 1 ) p_sys->i_file_size = 1;
    p_sys->i_file_size *= 1024 * 1024; /* In MBytes */

    p_sys->psz_filename_base = GetTmpFilePath( p_access );
    p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

    if( vlc_thread_create( p_access, "timeshift thread", Thread,
                           VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
    {
        msg_Err( p_access, "cannot spawn timeshift access thread" );
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
195 196
    ts_entry_t *p_entry;
    int i;
197 198 199 200

    msg_Dbg( p_access, "timeshift close called" );
    vlc_thread_join( p_access );

201
    for( p_entry = p_sys->p_write_list; p_entry; )
202
    {
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        ts_entry_t *p_next = p_entry->p_next;
        fclose( p_entry->file );
        free( p_entry );
        p_entry = p_next;
    }
    for( p_entry = p_sys->p_read_list; p_entry; )
    {
        ts_entry_t *p_next = p_entry->p_next;
        fclose( p_entry->file );
        free( p_entry );
        p_entry = p_next;
    }
    for( i = 0; i < p_sys->i_files; i++ )
    {
        sprintf( p_sys->psz_filename, "%s%i.dat",
                 p_sys->psz_filename_base, i );
        unlink( p_sys->psz_filename );
220 221
    }

222 223
    free( p_sys->psz_filename );
    free( p_sys->psz_filename_base );
224 225 226 227 228 229 230 231 232 233
    block_FifoRelease( p_sys->p_fifo );
    free( p_sys );
}

/*****************************************************************************
 *
 *****************************************************************************/
static block_t *Block( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
234 235
    access_t *p_src = p_access->p_source;
    block_t *p_block = NULL;
236

237 238
    /* Update info (we probably ought to be time caching that as well) */
    if( p_src->info.i_update & INPUT_UPDATE_META )
239
    {
240 241
        p_src->info.i_update &= ~INPUT_UPDATE_META;
        p_access->info.i_update |= INPUT_UPDATE_META;
242 243
    }

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    /* 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;
259 260 261 262 263 264 265 266 267
}

/*****************************************************************************
 *
 *****************************************************************************/
static void Thread( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    access_t     *p_src = p_access->p_source;
268
    block_t      *p_block;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

    while( !p_access->b_die )
    {
        /* Get a new block from the source */
        if( p_src->pf_block )
        {
            p_block = p_src->pf_block( p_src );
        }
        else
        {
            if( ( p_block = block_New( p_access, 2048 ) ) == NULL ) break;

            p_block->i_buffer =
                p_src->pf_read( p_src, p_block->p_buffer, 2048 );

284
            if( p_block->i_buffer <= 0 )
285
            {
286 287
              block_Release( p_block );
              p_block = NULL;
288 289 290
            }
        }

291 292 293 294 295 296 297 298 299
        if( p_block == NULL )
        {
          if( p_src->info.b_eof ) break;
          msleep( 10000 );
          continue;
        }

        p_sys->i_data += p_block->i_buffer;

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
        /* Write block */
        if( !p_sys->p_write_list && !p_sys->p_read_list &&
            p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
        {
            /* If there isn't too much timeshifted data,
             * write directly to FIFO */
            block_FifoPut( p_sys->p_fifo, p_block );
            continue;
        }

        WriteBlockToFile( p_access, p_block );
        block_Release( p_block );

        /* Read from file to fill up the fifo */
        while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
               !p_access->b_die )
        {
            p_block = ReadBlockFromFile( p_access );
            if( !p_block ) break;
319

320 321 322 323
            block_FifoPut( p_sys->p_fifo, p_block );
        }
    }

324
    msg_Dbg( p_access, "timeshift: no more input data" );
325

326 327
    while( !p_access->b_die &&
           (p_sys->p_read_list || p_sys->p_fifo->i_size) )
328
    {
329 330 331 332 333 334 335 336 337 338 339
        /* Read from file to fill up the fifo */
        while( p_sys->p_fifo->i_size < 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 );
340
    }
341 342 343 344 345 346

    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 ) );
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
}

/*****************************************************************************
 * NextFileWrite:
 *****************************************************************************/
static void NextFileWrite( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    ts_entry_t   *p_next;

    if( !p_sys->p_write_list )
    {
        p_sys->i_write_size = 0;
        return;
    }

    p_next = p_sys->p_write_list->p_next;

    /* Put written file in read list */
    if( p_sys->i_write_size < p_sys->i_file_size )
        ftruncate( fileno( p_sys->p_write_list->file ), p_sys->i_write_size );

    fseek( p_sys->p_write_list->file, 0, SEEK_SET );
    *p_sys->pp_read_last = p_sys->p_write_list;
    p_sys->pp_read_last = &p_sys->p_write_list->p_next;
    p_sys->p_write_list->p_next = 0;

    /* Switch to next file to write */
    p_sys->p_write_list = p_next;
    if( !p_sys->p_write_list ) p_sys->pp_write_last = &p_sys->p_write_list;

    p_sys->i_write_size = 0;
}

/*****************************************************************************
 * NextFileRead:
 *****************************************************************************/
static void NextFileRead( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    ts_entry_t   *p_next;

    if( !p_sys->p_read_list ) return;

    p_next = p_sys->p_read_list->p_next;

    /* Put read file in write list */
    fseek( p_sys->p_read_list->file, 0, SEEK_SET );
    *p_sys->pp_write_last = p_sys->p_read_list;
    p_sys->pp_write_last = &p_sys->p_read_list->p_next;
    p_sys->p_read_list->p_next = 0;

    /* Switch to next file to read */
    p_sys->p_read_list = p_next;
    if( !p_sys->p_read_list ) p_sys->pp_read_last = &p_sys->p_read_list;
}

/*****************************************************************************
 * WriteBlockToFile:
 *****************************************************************************/
static int WriteBlockToFile( access_t *p_access, block_t *p_block )
{
    access_sys_t *p_sys = p_access->p_sys;
    int i_write, i_buffer;

    if( p_sys->i_write_size == p_sys->i_file_size ) NextFileWrite( p_access );

    /* Open new file if necessary */
    if( !p_sys->p_write_list )
    {
        FILE *file;

        sprintf( p_sys->psz_filename, "%s%i.dat",
                 p_sys->psz_filename_base, p_sys->i_files );
421
        file = utf8_fopen( p_sys->psz_filename, "w+b" );
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

        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) );
            return VLC_EGENERIC;
        }
        else if( !file ) return VLC_EGENERIC;

        p_sys->p_write_list = malloc( sizeof(ts_entry_t) );
        p_sys->p_write_list->p_next = 0;
        p_sys->p_write_list->file = file;
        p_sys->pp_write_last = &p_sys->p_write_list->p_next;

        p_sys->i_files++;
    }

    /* Write to file */
    i_buffer = __MIN( p_block->i_buffer,
                      p_sys->i_file_size - p_sys->i_write_size );

    i_write = fwrite( p_block->p_buffer, 1, i_buffer,
                      p_sys->p_write_list->file );

    if( i_write > 0 ) p_sys->i_write_size += i_write;

    //p_access->info.i_size += i_write;
    //p_access->info.i_update |= INPUT_UPDATE_SIZE;

    if( i_write < i_buffer )
    {
        /* Looks like we're short of space */

        if( !p_sys->p_write_list->p_next )
        {
            msg_Warn( p_access, "no more space, overwritting old data" );
            NextFileRead( p_access );
            NextFileRead( p_access );
        }

        /* Make sure we switch to next file in write list */
        p_sys->i_write_size = p_sys->i_file_size;
    }

    p_block->p_buffer += i_write;
    p_block->i_buffer -= i_write;

    /* Check if we have some data left */
    if( p_block->i_buffer ) return WriteBlockToFile( p_access, p_block );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * ReadBlockFromFile:
 *****************************************************************************/
static block_t *ReadBlockFromFile( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    block_t *p_block;

    if( !p_sys->p_read_list && p_sys->p_write_list )
    {
        /* Force switching to next write file, that should
         * give us something to read */
        NextFileWrite( p_access );
    }

    if( !p_sys->p_read_list ) return 0;

    p_block = block_New( p_access, 4096 );
    p_block->i_buffer = fread( p_block->p_buffer, 1, 4096,
                               p_sys->p_read_list->file );

    if( p_block->i_buffer == 0 ) NextFileRead( p_access );

    //p_access->info.i_size -= p_block->i_buffer;
    //p_access->info.i_update |= INPUT_UPDATE_SIZE;

    return p_block;
}

/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
static int Seek( access_t *p_access, int64_t i_pos )
{
    //access_sys_t *p_sys = p_access->p_sys;
    return VLC_SUCCESS;
512 513 514 515 516 517 518 519 520 521 522 523
}

/*****************************************************************************
 *
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    vlc_bool_t   *pb_bool;
    int          *pi_int;

    switch( i_query )
    {
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    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 );
547 548 549 550 551
    }
    return VLC_SUCCESS;
}

/*****************************************************************************
552
 * GetTmpFilePath:
553
 *****************************************************************************/
554 555 556 557
#ifdef WIN32
#define getpid() GetCurrentProcessId()
#endif
static char *GetTmpFilePath( access_t *p_access )
558
{
559 560
    char *psz_dir = var_GetString( p_access, "timeshift-dir" );
    char *psz_filename_base;
561

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
562
    if( ( psz_dir != NULL ) && ( psz_dir[0] == '\0' ) )
563
    {
564
        free( psz_dir );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
565
        psz_dir = NULL;
566
    }
567

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
568
    if( psz_dir == NULL )
569 570
    {
#ifdef WIN32
571 572
        DWORD ret = GetTempPathW (0, NULL);
        wchar_t wdir[ret + 3]; // can at least old "C:" + nul
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
573 574
        const wchar_t *pwdir = wdir;
        wchar_t *pwdir_free = NULL;
575

576
        if (GetTempPathW (ret + 1, wdir) == 0)
577
        {
578 579
            pwdir_free = pwdir = _wgetcwd (NULL, 0);
            if (pwdir == NULL)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
580
                pwdir = L"C:";
581 582
        }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
583
        psz_dir = FromWide (pwdir);
584 585
        if (pwdir_free != NULL)
            free (pwdir_free);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
586

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
587
        /* remove trailing antislash if any */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
588 589
        if (psz_dir[strlen (psz_dir) - 1] == '\\')
            psz_dir[strlen (psz_dir) - 1] = '\0';
590 591
#else
        psz_dir = strdup( "/tmp" );
592 593 594
#endif
    }

595 596
    asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
              psz_dir, getpid(), p_access->i_object_id );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
597
    free( psz_dir );
598

599
    return psz_filename_base;
600
}