stream.c 59.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * stream.c
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
Gildas Bazin's avatar
Gildas Bazin committed
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <dirent.h>
29
#include <assert.h>
30

31
#include <vlc_common.h>
32 33
#include <vlc_strings.h>
#include <vlc_osd.h>
34
#include <vlc_charset.h>
35

36
#include <libvlc.h>
37

38
#include "access.h"
39 40 41
#include "stream.h"

#include "input_internal.h"
42

43 44
#undef STREAM_DEBUG

45
/* TODO:
46
 *  - tune the 2 methods (block/stream)
47 48 49
 *  - compute cost for seek
 *  - improve stream mode seeking with closest segments
 *  - ...
50
 */
51 52 53 54 55 56

/* Two methods:
 *  - using pf_block
 *      One linked list of data read
 *  - using pf_read
 *      More complex scheme using mutliple track to avoid seeking
57 58 59
 *  - using directly the access (only indirection for peeking).
 *      This method is known to introduce much less latency.
 *      It should probably defaulted (instead of the stream method (2)).
60 61
 */

62 63
/* How many tracks we have, currently only used for stream mode */
#ifdef OPTIMIZE_MEMORY
64 65 66 67 68 69 70 71 72
#   define STREAM_CACHE_TRACK 1
    /* Max size of our cache 128Ko per track */
#   define STREAM_CACHE_SIZE  (STREAM_CACHE_TRACK*1024*128)
#else
#   define STREAM_CACHE_TRACK 3
    /* Max size of our cache 4Mo per track */
#   define STREAM_CACHE_SIZE  (4*STREAM_CACHE_TRACK*1024*1024)
#endif

73 74 75 76
/* How many data we try to prebuffer
 * XXX it should be small to avoid useless latency but big enough for
 * efficient demux probing */
#define STREAM_CACHE_PREBUFFER_SIZE (128)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

/* Method1: Simple, for pf_block.
 *  We get blocks and put them in the linked list.
 *  We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
 */

/* Method2: A bit more complex, for pf_read
 *  - We use ring buffers, only one if unseekable, all if seekable
 *  - Upon seek date current ring, then search if one ring match the pos,
 *      yes: switch to it, seek the access to match the end of the ring
 *      no: search the ring with i_end the closer to i_pos,
 *          if close enough, read data and use this ring
 *          else use the oldest ring, seek and use it.
 *
 *  TODO: - with access non seekable: use all space available for only one ring, but
 *          we have to support seekable/non-seekable switch on the fly.
 *        - compute a good value for i_read_size
 *        - ?
 */
96
#define STREAM_READ_ATONCE 1024
97 98 99
#define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)

typedef struct
100
{
101
    int64_t i_date;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
102

103 104 105 106
    int64_t i_start;
    int64_t i_end;

    uint8_t *p_buffer;
107

108 109
} stream_track_t;

110 111 112 113 114 115 116
typedef struct
{
    char     *psz_path;
    int64_t  i_size;

} access_entry_t;

Laurent Aimar's avatar
Laurent Aimar committed
117
typedef enum
118
{
119 120
    STREAM_METHOD_BLOCK,
    STREAM_METHOD_STREAM
121 122
} stream_read_method_t;

123 124 125 126
struct stream_sys_t
{
    access_t    *p_access;

127
    stream_read_method_t   method;    /* method to use */
128 129 130 131 132

    int64_t     i_pos;      /* Current reading offset */

    /* Method 1: pf_block */
    struct
133
    {
134
        int64_t i_start;        /* Offset of block for p_first */
135
        int64_t i_offset;       /* Offset for data in p_current */
136 137 138 139 140
        block_t *p_current;     /* Current block */

        int     i_size;         /* Total amount of data in the list */
        block_t *p_first;
        block_t **pp_last;
141

142 143 144 145
    } block;

    /* Method 2: for pf_read */
    struct
146
    {
147
        int i_offset;   /* Buffer offset in the current track */
148 149 150 151 152 153 154 155 156
        int i_tk;       /* Current track */
        stream_track_t tk[STREAM_CACHE_TRACK];

        /* Global buffer */
        uint8_t *p_buffer;

        /* */
        int i_used; /* Used since last read */
        int i_read_size;
157

158 159 160
    } stream;

    /* Peek temporary buffer */
161
    unsigned int i_peek;
162 163 164 165
    uint8_t *p_peek;

    /* Stat for both method */
    struct
166
    {
167
        bool b_fastseek;  /* From access */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
168

169 170 171 172
        /* Stat about reading data */
        int64_t i_read_count;
        int64_t i_bytes;
        int64_t i_read_time;
173

174 175 176
        /* Stat about seek */
        int     i_seek_count;
        int64_t i_seek_time;
177

178
    } stat;
179

180 181 182 183 184 185
    /* Streams list */
    int            i_list;
    access_entry_t **list;
    int            i_list_index;
    access_t       *p_list_access;

186
    /* Preparse mode ? */
187
    bool      b_quick;
188 189 190 191 192 193

    /* */
    struct
    {
        bool b_active;

194 195
        FILE *f;        /* TODO it could be replaced by access_output_t one day */
        bool b_error;
196
    } record;
197
};
198

199
/* Method 1: */
200 201
static int  AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read );
static int  AStreamPeekBlock( stream_t *s, const uint8_t **p_peek, unsigned int i_read );
202
static int  AStreamSeekBlock( stream_t *s, int64_t i_pos );
203
static void AStreamPrebufferBlock( stream_t *s );
204
static block_t *AReadBlock( stream_t *s, bool *pb_eof );
205

206
/* Method 2 */
207 208
static int  AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read );
static int  AStreamPeekStream( stream_t *s, const uint8_t **pp_peek, unsigned int i_read );
209
static int  AStreamSeekStream( stream_t *s, int64_t i_pos );
210
static void AStreamPrebufferStream( stream_t *s );
211
static int  AReadStream( stream_t *s, void *p_read, unsigned int i_read );
212

213
/* Common */
214
static int AStreamControl( stream_t *s, int i_query, va_list );
215 216
static void AStreamDestroy( stream_t *s );
static void UStreamDestroy( stream_t *s );
217
static int  ASeek( stream_t *s, int64_t i_pos );
218 219
static int  ARecordSetState( stream_t *s, bool b_record, const char *psz_extension );
static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer );
220

Laurent Aimar's avatar
Laurent Aimar committed
221 222 223 224 225 226 227 228 229
/****************************************************************************
 * stream_CommonNew: create an empty stream structure
 ****************************************************************************/
stream_t *stream_CommonNew( vlc_object_t *p_obj )
{
    return (stream_t *)vlc_custom_create( p_obj, sizeof(stream_t),
                                          VLC_OBJECT_GENERIC, "stream" );
}

230
/****************************************************************************
231
 * stream_UrlNew: create a stream from a access
232
 ****************************************************************************/
233
stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
234
{
235 236
    const char *psz_access, *psz_demux;
    char *psz_path;
237 238
    access_t *p_access;
    stream_t *p_res;
239

240 241
    if( !psz_url )
        return NULL;
242

243 244 245
    char psz_dup[strlen( psz_url ) + 1];
    strcpy( psz_dup, psz_url );
    input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
246

247
    /* Now try a real access */
248
    p_access = access_New( p_parent, psz_access, psz_demux, psz_path );
249 250 251 252 253 254

    if( p_access == NULL )
    {
        msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
        return NULL;
    }
255

256
    if( !( p_res = stream_AccessNew( p_access, true ) ) )
257
    {
258
        access_Delete( p_access );
259
        return NULL;
260
    }
261 262 263

    p_res->pf_destroy = UStreamDestroy;
    return p_res;
264 265
}

266
stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
267
{
Laurent Aimar's avatar
Laurent Aimar committed
268
    stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
269
    stream_sys_t *p_sys;
270
    char *psz_list = NULL;
271

272 273 274 275 276 277 278 279 280
    if( !s )
        return NULL;

    s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
    if( !p_sys )
    {
        vlc_object_release( s );
        return NULL;
    }
281 282 283 284 285 286

    /* Attach it now, needed for b_die */
    vlc_object_attach( s, p_access );

    s->pf_read   = NULL;    /* Set up later */
    s->pf_peek   = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
287
    s->pf_control = AStreamControl;
288
    s->pf_destroy = AStreamDestroy;
289

290 291
    /* UTF16 and UTF32 text file conversion */
    s->i_char_width = 1;
292
    s->b_little_endian = false;
293
    s->conv = (vlc_iconv_t)(-1);
294

295 296
    /* Common field */
    p_sys->p_access = p_access;
297
    if( p_access->pf_block )
298
        p_sys->method = STREAM_METHOD_BLOCK;
299
    else
300
        p_sys->method = STREAM_METHOD_STREAM;
301

302 303
    p_sys->record.b_active = false;

304 305 306
    p_sys->i_pos = p_access->info.i_pos;

    /* Stats */
307
    access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
308 309 310 311 312 313
    p_sys->stat.i_bytes = 0;
    p_sys->stat.i_read_time = 0;
    p_sys->stat.i_read_count = 0;
    p_sys->stat.i_seek_count = 0;
    p_sys->stat.i_seek_time = 0;

314 315 316 317 318
    p_sys->i_list = 0;
    p_sys->list = 0;
    p_sys->i_list_index = 0;
    p_sys->p_list_access = 0;

319 320
    p_sys->b_quick = b_quick;

321 322 323 324
    /* Get the additional list of inputs if any (for concatenation) */
    if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
    {
        access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
325 326
        if( p_entry == NULL )
            goto error;
327 328 329 330 331
        char *psz_name, *psz_parser = psz_name = psz_list;

        p_sys->p_list_access = p_access;
        p_entry->i_size = p_access->info.i_size;
        p_entry->psz_path = strdup( p_access->psz_path );
332 333 334 335 336
        if( p_entry->psz_path == NULL )
        {
            free( p_entry );
            goto error;
        }
337
        TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
338
        msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
339 340 341 342 343 344 345 346 347 348
                 p_entry->psz_path, p_access->info.i_size );

        while( psz_name && *psz_name )
        {
            psz_parser = strchr( psz_name, ',' );
            if( psz_parser ) *psz_parser = 0;

            psz_name = strdup( psz_name );
            if( psz_name )
            {
349
                access_t *p_tmp = access_New( p_access, p_access->psz_access,
350
                                               "", psz_name );
351 352 353 354 355 356 357 358

                if( !p_tmp )
                {
                    psz_name = psz_parser;
                    if( psz_name ) psz_name++;
                    continue;
                }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
359
                msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
360 361 362
                         psz_name, p_tmp->info.i_size );

                p_entry = malloc( sizeof(access_entry_t) );
363 364
                if( p_entry == NULL )
                    goto error;
365 366 367 368
                p_entry->i_size = p_tmp->info.i_size;
                p_entry->psz_path = psz_name;
                TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );

369
                access_Delete( p_tmp );
370 371 372 373 374 375
            }

            psz_name = psz_parser;
            if( psz_name ) psz_name++;
        }
    }
376
    FREENULL( psz_list );
377

378 379 380 381
    /* Peek */
    p_sys->i_peek = 0;
    p_sys->p_peek = NULL;

382
    if( p_sys->method == STREAM_METHOD_BLOCK )
383
    {
384
        msg_Dbg( s, "Using AStream*Block" );
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
        s->pf_read = AStreamReadBlock;
        s->pf_peek = AStreamPeekBlock;

        /* Init all fields of p_sys->block */
        p_sys->block.i_start = p_sys->i_pos;
        p_sys->block.i_offset = 0;
        p_sys->block.p_current = NULL;
        p_sys->block.i_size = 0;
        p_sys->block.p_first = NULL;
        p_sys->block.pp_last = &p_sys->block.p_first;

        /* Do the prebuffering */
        AStreamPrebufferBlock( s );

        if( p_sys->block.i_size <= 0 )
        {
            msg_Err( s, "cannot pre fill buffer" );
            goto error;
        }
    }
405
    else
406
    {
407 408
        int i;

409 410
        assert( p_sys->method == STREAM_METHOD_STREAM );

411 412
        msg_Dbg( s, "Using AStream*Stream" );

413 414 415 416 417 418 419
        s->pf_read = AStreamReadStream;
        s->pf_peek = AStreamPeekStream;

        /* Allocate/Setup our tracks */
        p_sys->stream.i_offset = 0;
        p_sys->stream.i_tk     = 0;
        p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
420 421
        if( p_sys->stream.p_buffer == NULL )
            goto error;
422
        p_sys->stream.i_used   = 0;
423
        access_Control( p_access, ACCESS_GET_MTU,
424
                         &p_sys->stream.i_read_size );
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
        if( p_sys->stream.i_read_size <= 0 )
            p_sys->stream.i_read_size = STREAM_READ_ATONCE;
        else if( p_sys->stream.i_read_size <= 256 )
            p_sys->stream.i_read_size = 256;

        for( i = 0; i < STREAM_CACHE_TRACK; i++ )
        {
            p_sys->stream.tk[i].i_date  = 0;
            p_sys->stream.tk[i].i_start = p_sys->i_pos;
            p_sys->stream.tk[i].i_end   = p_sys->i_pos;
            p_sys->stream.tk[i].p_buffer=
                &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
        }

        /* Do the prebuffering */
        AStreamPrebufferStream( s );
441

442 443 444 445 446
        if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
        {
            msg_Err( s, "cannot pre fill buffer" );
            goto error;
        }
447
    }
448

449
    return s;
450 451

error:
452
    if( p_sys->method == STREAM_METHOD_BLOCK )
453 454 455 456 457 458 459
    {
        /* Nothing yet */
    }
    else
    {
        free( p_sys->stream.p_buffer );
    }
460 461 462 463
    while( p_sys->i_list > 0 )
        free( p_sys->list[--(p_sys->i_list)] );
    free( p_sys->list );
    free( psz_list );
464 465
    free( s->p_sys );
    vlc_object_detach( s );
466
    vlc_object_release( s );
467
    return NULL;
468 469
}

470
/****************************************************************************
471
 * AStreamDestroy:
472
 ****************************************************************************/
473
static void AStreamDestroy( stream_t *s )
474
{
475 476 477 478
    stream_sys_t *p_sys = s->p_sys;

    vlc_object_detach( s );

479 480 481
    if( p_sys->record.b_active )
        ARecordSetState( s, false, NULL );

482
    if( p_sys->method == STREAM_METHOD_BLOCK )
483 484 485
        block_ChainRelease( p_sys->block.p_first );
    else
        free( p_sys->stream.p_buffer );
486

487
    free( p_sys->p_peek );
488 489

    if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
490
        access_Delete( p_sys->p_list_access );
491 492

    while( p_sys->i_list-- )
493
    {
494 495
        free( p_sys->list[p_sys->i_list]->psz_path );
        free( p_sys->list[p_sys->i_list] );
496 497
    }

498 499 500
    free( p_sys->list );
    free( p_sys );

501
    vlc_object_release( s );
502 503
}

504 505
static void UStreamDestroy( stream_t *s )
{
506
    access_t *p_access = (access_t *)s->p_parent;
507
    AStreamDestroy( s );
508
    access_Delete( p_access );
509 510
}

511 512 513 514 515 516 517 518 519
/****************************************************************************
 * stream_AccessReset:
 ****************************************************************************/
void stream_AccessReset( stream_t *s )
{
    stream_sys_t *p_sys = s->p_sys;

    p_sys->i_pos = p_sys->p_access->info.i_pos;

520
    if( p_sys->method == STREAM_METHOD_BLOCK )
521 522 523 524 525 526 527 528 529 530 531 532 533 534
    {
        block_ChainRelease( p_sys->block.p_first );

        /* Init all fields of p_sys->block */
        p_sys->block.i_start = p_sys->i_pos;
        p_sys->block.i_offset = 0;
        p_sys->block.p_current = NULL;
        p_sys->block.i_size = 0;
        p_sys->block.p_first = NULL;
        p_sys->block.pp_last = &p_sys->block.p_first;

        /* Do the prebuffering */
        AStreamPrebufferBlock( s );
    }
535
    else
536 537 538
    {
        int i;

539 540
        assert( p_sys->method == STREAM_METHOD_STREAM );

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
        /* Setup our tracks */
        p_sys->stream.i_offset = 0;
        p_sys->stream.i_tk     = 0;
        p_sys->stream.i_used   = 0;

        for( i = 0; i < STREAM_CACHE_TRACK; i++ )
        {
            p_sys->stream.tk[i].i_date  = 0;
            p_sys->stream.tk[i].i_start = p_sys->i_pos;
            p_sys->stream.tk[i].i_end   = p_sys->i_pos;
        }

        /* Do the prebuffering */
        AStreamPrebufferStream( s );
    }
}
557

558 559 560 561 562 563
/****************************************************************************
 * stream_AccessUpdate:
 ****************************************************************************/
void stream_AccessUpdate( stream_t *s )
{
    stream_sys_t *p_sys = s->p_sys;
564

565
    p_sys->i_pos = p_sys->p_access->info.i_pos;
566 567 568 569 570 571 572 573 574

    if( p_sys->i_list )
    {
        int i;
        for( i = 0; i < p_sys->i_list_index; i++ )
        {
            p_sys->i_pos += p_sys->list[i]->i_size;
        }
    }
575 576
}

577
/****************************************************************************
578
 * AStreamControl:
579
 ****************************************************************************/
580
static int AStreamControl( stream_t *s, int i_query, va_list args )
581
{
582 583
    stream_sys_t *p_sys = s->p_sys;
    access_t     *p_access = p_sys->p_access;
584

585
    bool *p_bool;
586 587
    bool b_bool;
    const char *psz_string;
588 589
    int64_t    *pi_64, i_64;
    int        i_int;
590 591 592 593

    switch( i_query )
    {
        case STREAM_GET_SIZE:
594
            pi_64 = (int64_t*)va_arg( args, int64_t * );
595 596 597 598 599 600 601 602
            if( s->p_sys->i_list )
            {
                int i;
                *pi_64 = 0;
                for( i = 0; i < s->p_sys->i_list; i++ )
                    *pi_64 += s->p_sys->list[i]->i_size;
                break;
            }
603 604
            *pi_64 = p_access->info.i_size;
            break;
605 606

        case STREAM_CAN_SEEK:
607
            p_bool = (bool*)va_arg( args, bool * );
608
            access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
609
            break;
610 611

        case STREAM_CAN_FASTSEEK:
612
            p_bool = (bool*)va_arg( args, bool * );
613
            access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
614
            break;
615 616

        case STREAM_GET_POSITION:
617 618 619
            pi_64 = (int64_t*)va_arg( args, int64_t * );
            *pi_64 = p_sys->i_pos;
            break;
620 621

        case STREAM_SET_POSITION:
622
            i_64 = (int64_t)va_arg( args, int64_t );
623 624 625
            switch( p_sys->method )
            {
            case STREAM_METHOD_BLOCK:
626
                return AStreamSeekBlock( s, i_64 );
627
            case STREAM_METHOD_STREAM:
628
                return AStreamSeekStream( s, i_64 );
629 630 631 632
            default:
                assert(0);
                return VLC_EGENERIC;
            }
633

634
        case STREAM_GET_MTU:
635
            return VLC_EGENERIC;
636

637
        case STREAM_CONTROL_ACCESS:
638
            i_int = (int) va_arg( args, int );
639 640 641
            if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
                i_int != ACCESS_SET_PRIVATE_ID_CA &&
                i_int != ACCESS_GET_PRIVATE_ID_STATE )
642 643 644 645 646
            {
                msg_Err( s, "Hey, what are you thinking ?"
                            "DON'T USE STREAM_CONTROL_ACCESS !!!" );
                return VLC_EGENERIC;
            }
647
            return access_vaControl( p_access, i_int, args );
648

649
        case STREAM_GET_CONTENT_TYPE:
650
            return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
651
                                    va_arg( args, char ** ) );
652 653 654 655 656 657
        case STREAM_SET_RECORD_STATE:
            b_bool = (bool)va_arg( args, int );
            psz_string = NULL;
            if( b_bool )
                psz_string = (const char*)va_arg( args, const char* );
            return ARecordSetState( s, b_bool, psz_string );
658

659 660 661 662
        default:
            msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
            return VLC_EGENERIC;
    }
663
    return VLC_SUCCESS;
664 665
}

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
/****************************************************************************
 * ARecord*: record stream functions
 ****************************************************************************/
static int  ARecordStart( stream_t *s, const char *psz_extension )
{
    stream_sys_t *p_sys = s->p_sys;

    char *psz_file;
    FILE *f;

    /* */
    if( !psz_extension )
        psz_extension = "dat";

    /* Retreive path */
    char *psz_path = var_CreateGetString( s, "input-record-path" );
    if( !psz_path || *psz_path == '\0' )
    {
        free( psz_path );
        psz_path = strdup( config_GetHomeDir() );
    }

    if( !psz_path )
        return VLC_ENOMEM;

    /* Create file name
     * TODO allow prefix configuration */
693
    psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

    free( psz_path );

    if( !psz_file )
        return VLC_ENOMEM;

    f = utf8_fopen( psz_file, "wb" );
    if( !f )
    {
        free( psz_file );
        return VLC_EGENERIC;
    }
    msg_Dbg( s, "Recording into %s", psz_file );
    free( psz_file );

    /* */
    p_sys->record.f = f;
    p_sys->record.b_active = true;
712
    p_sys->record.b_error = false;
713 714 715 716 717 718 719
    return VLC_SUCCESS;
}
static int  ARecordStop( stream_t *s )
{
    stream_sys_t *p_sys = s->p_sys;

    assert( p_sys->record.b_active );
720

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
    msg_Dbg( s, "Recording completed" );
    fclose( p_sys->record.f );
    p_sys->record.b_active = false;
    return VLC_SUCCESS;
}

static int  ARecordSetState( stream_t *s, bool b_record, const char *psz_extension )
{
    stream_sys_t *p_sys = s->p_sys;

    if( !!p_sys->record.b_active == !!b_record )
        return VLC_SUCCESS;

    if( b_record )
        return ARecordStart( s, psz_extension );
    else
        return ARecordStop( s );
}
static void ARecordWrite( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
{
    stream_sys_t *p_sys = s->p_sys;

    assert( p_sys->record.b_active );

745 746 747 748 749 750 751 752 753 754 755 756 757
    if( i_buffer > 0 )
    {
        const bool b_previous_error = p_sys->record.b_error;
        const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->record.f );

        p_sys->record.b_error = i_written != i_buffer;

        /* TODO maybe a intf_UserError or something like that ? */
        if( p_sys->record.b_error && !b_previous_error )
            msg_Err( s, "Failed to record data (begin)" );
        else if( !p_sys->record.b_error && b_previous_error )
            msg_Err( s, "Failed to record data (end)" );
    }
758
}
759

760
/****************************************************************************
761
 * Method 1:
762
 ****************************************************************************/
763
static void AStreamPrebufferBlock( stream_t *s )
764
{
765
    stream_sys_t *p_sys = s->p_sys;
766
    access_t     *p_access = p_sys->p_access;
767

768 769
    int64_t i_first = 0;
    int64_t i_start;
770

771 772 773
    msg_Dbg( s, "pre buffering" );
    i_start = mdate();
    for( ;; )
774
    {
775
        const int64_t i_date = mdate();
776
        bool b_eof;
777
        block_t *b;
778

779
        if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
780 781 782 783 784 785
        {
            int64_t i_byterate;

            /* Update stat */
            p_sys->stat.i_bytes = p_sys->block.i_size;
            p_sys->stat.i_read_time = i_date - i_start;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
786
            i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
787
                         (p_sys->stat.i_read_time + 1);
788

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
789
            msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
790
                     "%"PRId64" kbytes/s",
791
                     p_sys->stat.i_bytes,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
792
                     p_sys->stat.i_read_time / INT64_C(1000000),
793 794 795
                     i_byterate / 1024 );
            break;
        }
796

797
        /* Fetch a block */
798
        if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
799
        {
800 801
            if( b_eof )
                break;
802
            continue;
803
        }
804

805 806 807 808 809 810
        while( b )
        {
            /* Append the block */
            p_sys->block.i_size += b->i_buffer;
            *p_sys->block.pp_last = b;
            p_sys->block.pp_last = &b->p_next;
811

812 813 814
            p_sys->stat.i_read_count++;
            b = b->p_next;
        }
815 816 817 818

        if( i_first == 0 )
        {
            i_first = mdate();
819 820
            msg_Dbg( s, "received first data after %d ms",
                     (int)((i_first-i_start)/1000) );
821
        }
822 823
    }

824 825 826 827 828
    p_sys->block.p_current = p_sys->block.p_first;
}

static int AStreamRefillBlock( stream_t *s );

829
static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
830 831 832
{
    stream_sys_t *p_sys = s->p_sys;

833 834
    uint8_t *p_data = p_read;
    uint8_t *p_record = p_data;
835
    unsigned int i_data = 0;
836 837 838 839 840

    /* It means EOF */
    if( p_sys->block.p_current == NULL )
        return 0;

841 842 843 844
    if( p_sys->record.b_active && !p_data )
        p_record = p_data = malloc( i_read );

    if( p_data == NULL )
Damien Fouilleul's avatar
 
Damien Fouilleul committed
845
    {
846
        /* seek within this stream if possible, else use plain old read and discard */
Damien Fouilleul's avatar
 
Damien Fouilleul committed
847 848
        stream_sys_t *p_sys = s->p_sys;
        access_t     *p_access = p_sys->p_access;
849
        bool   b_aseek;
850
        access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
Damien Fouilleul's avatar
 
Damien Fouilleul committed
851 852 853
        if( b_aseek )
            return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
    }
854

855
    while( i_data < i_read )
856
    {
857 858
        int i_current =
            p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
859
        unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
Gildas Bazin's avatar
 
Gildas Bazin committed
860

861
        /* Copy data */
862 863 864
        if( p_data )
        {
            memcpy( p_data,
865 866
                    &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
                    i_copy );
867 868
            p_data += i_copy;
        }
869
        i_data += i_copy;
870

871 872 873 874 875 876 877 878 879
        p_sys->block.i_offset += i_copy;
        if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
        {
            /* Current block is now empty, switch to next */
            if( p_sys->block.p_current )
            {
                p_sys->block.i_offset = 0;
                p_sys->block.p_current = p_sys->block.p_current->p_next;
            }
Rafaël Carré's avatar
Rafaël Carré committed
880 881
            /*Get a new block if needed */
            if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
Gildas Bazin's avatar
Gildas Bazin committed
882 883 884
            {
                break;
            }
885 886 887
        }
    }

888 889 890 891 892 893 894
    if( p_sys->record.b_active )
    {
        if( i_data > 0 && p_record != NULL)
            ARecordWrite( s, p_record, i_data );
        if( !p_read )
            free( p_record );
    }
895

896 897
    p_sys->i_pos += i_data;
    return i_data;
898
}
Gildas Bazin's avatar
Gildas Bazin committed
899

900
static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
901
{
902 903
    stream_sys_t *p_sys = s->p_sys;
    uint8_t *p_data;
904
    unsigned int i_data = 0;
905
    block_t *b;
906
    unsigned int i_offset;
907

908
    if( p_sys->block.p_current == NULL ) return 0; /* EOF */
909 910 911 912 913 914 915 916 917 918 919

    /* We can directly give a pointer over our buffer */
    if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
    {
        *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
        return i_read;
    }

    /* We need to create a local copy */
    if( p_sys->i_peek < i_read )
    {
920 921 922 923 924 925
        p_sys->p_peek = realloc( p_sys->p_peek, i_read );
        if( !p_sys->p_peek )
        {
            p_sys->i_peek = 0;
            return 0;
        }
926 927 928 929
        p_sys->i_peek = i_read;
    }

    /* Fill enough data */
930 931
    while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
           < i_read )
932 933 934
    {
        block_t **pp_last = p_sys->block.pp_last;

935
        if( AStreamRefillBlock( s ) ) break;
936 937

        /* Our buffer are probably filled enough, don't try anymore */
938
        if( pp_last == p_sys->block.pp_last ) break;
939 940 941 942 943 944 945 946 947
    }

    /* Copy what we have */
    b = p_sys->block.p_current;
    i_offset = p_sys->block.i_offset;
    p_data = p_sys->p_peek;

    while( b && i_data < i_read )
    {
948
        unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
        int i_copy = __MIN( i_current, i_read - i_data );

        memcpy( p_data, &b->p_buffer[i_offset], i_copy );
        i_data += i_copy;
        p_data += i_copy;
        i_offset += i_copy;

        if( i_offset >= b->i_buffer )
        {
            i_offset = 0;
            b = b->p_next;
        }
    }

    *pp_peek = p_sys->p_peek;
    return i_data;
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
965
}
966

967
static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
968
{
969 970 971
    stream_sys_t *p_sys = s->p_sys;
    access_t   *p_access = p_sys->p_access;
    int64_t    i_offset = i_pos - p_sys->block.i_start;
972
    bool b_seek;
973

974 975 976 977 978
    /* We already have thoses data, just update p_current/i_offset */
    if( i_offset >= 0 && i_offset < p_sys->block.i_size )
    {
        block_t *b = p_sys->block.p_first;
        int i_current = 0;
979

980 981 982 983 984
        while( i_current + b->i_buffer < i_offset )
        {
            i_current += b->i_buffer;
            b = b->p_next;
        }
985

986 987
        p_sys->block.p_current = b;
        p_sys->block.i_offset = i_offset - i_current;
988

989
        p_sys->i_pos = i_pos;
990

991 992
        return VLC_SUCCESS;
    }
993

994 995
    /* We may need to seek or to read data */
    if( i_offset < 0 )
996
    {
997
        bool b_aseek;
998
        access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
999 1000 1001

        if( !b_aseek )
        {
1002
            msg_Err( s, "backward seeking impossible (access not seekable)" );
1003 1004 1005
            return VLC_EGENERIC;
        }

1006
        b_seek = true;
1007
    }
1008 1009
    else
    {
1010
        bool b_aseek, b_aseekfast;
1011

1012 1013
        access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
        access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
1014 1015 1016

        if( !b_aseek )
        {
1017
            b_seek = false;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1018
            msg_Warn( s, "%"PRId64" bytes need to be skipped "
1019
                      "(access non seekable)",
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
                      i_offset - p_sys->block.i_size );
        }
        else
        {
            int64_t i_skip = i_offset - p_sys->block.i_size;

            /* Avg bytes per packets */
            int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
            /* TODO compute a seek cost instead of fixed threshold */
            int i_th = b_aseekfast ? 1 : 5;

            if( i_skip <= i_th * i_avg &&
                i_skip < STREAM_CACHE_SIZE )
1033
                b_seek = false;
1034
            else
1035
                b_seek = true;
1036

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1037
            msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
1038 1039
                     b_seek, i_th*i_avg, i_skip );
        }
1040 1041
    }

1042 1043 1044 1045 1046
    if( b_seek )
    {
        int64_t i_start, i_end;
        /* Do the access seek */
        i_start = mdate();
1047
        if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1048
        i_end = mdate();
1049

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
        /* Release data */
        block_ChainRelease( p_sys->block.p_first );

        /* Reinit */
        p_sys->block.i_start = p_sys->i_pos = i_pos;
        p_sys->block.i_offset = 0;
        p_sys->block.p_current = NULL;
        p_sys->block.i_size = 0;
        p_sys->block.p_first = NULL;
        p_sys->block.pp_last = &p_sys->block.p_first;
1060

1061 1062 1063
        /* Refill a block */
        if( AStreamRefillBlock( s ) )
            return VLC_EGENERIC;
1064

1065 1066 1067 1068 1069 1070
        /* Update stat */
        p_sys->stat.i_seek_time += i_end - i_start;
        p_sys->stat.i_seek_count++;
        return VLC_SUCCESS;
    }
    else
1071
    {
1072
        do
1073
        {
1074
            /* Read and skip enough data */
1075 1076
            if( AStreamRefillBlock( s ) )
                return VLC_EGENERIC;
1077

1078
            while( p_sys->block.p_current &&
1079
                   p_sys->i_pos + p_sys->block.p_current->i_buffer - p_sys->block.i_offset < i_pos )
1080
            {
1081
                p_sys->i_pos += p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
1082
                p_sys->block.p_current = p_sys->block.p_current->p_next;
1083
                p_sys->block.i_offset = 0;
1084 1085
            }
        }
1086
        while( p_sys->block.i_start + p_sys->block.i_size < i_pos );
1087

1088 1089
        p_sys->block.i_offset = i_pos - p_sys->i_pos;
        p_sys->i_pos = i_pos;
1090

1091
        return VLC_SUCCESS;
1092 1093
    }

1094
    return VLC_EGENERIC;
1095 1096
}

1097
static int AStreamRefillBlock( stream_t *s )
1098
{
1099 1100 1101 1102