stream.c 35.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * stream.c
 *****************************************************************************
Clément Stenac's avatar
Clément Stenac committed
4
 * Copyright (C) 1999-2004 VideoLAN
Gildas Bazin's avatar
Gildas Bazin committed
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <stdlib.h>
#include <vlc/vlc.h>
#include <vlc/input.h>

Laurent Aimar's avatar
Laurent Aimar committed
28
#include "input_internal.h"
29

Laurent Aimar's avatar
Laurent Aimar committed
30 31 32 33 34
/* TODO:
 *  - tune the 2 methods
 *  - compute cost for seek
 *  - improve stream mode seeking with closest segments
 *  - ...
35
 */
Laurent Aimar's avatar
Laurent Aimar committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49

/* Two methods:
 *  - using pf_block
 *      One linked list of data read
 *  - using pf_read
 *      More complex scheme using mutliple track to avoid seeking
 */

/* How many track we have, currently only used for stream mode */
#define STREAM_CACHE_TRACK 3
/* Max size of our cache 4Mo per track */
#define STREAM_CACHE_SIZE  (4*STREAM_CACHE_TRACK*1024*1024)
  /* How many data we try to prebuffer */
#define STREAM_CACHE_PREBUFFER_SIZE (32767)
50 51
/* Maximum time we take to pre-buffer */
#define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
Laurent Aimar's avatar
Laurent Aimar committed
52 53 54 55 56 57


/* 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
 */
58
#define STREAM_DATA_WAIT 40000       /* Time between before a pf_block retry */
Laurent Aimar's avatar
Laurent Aimar committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

/* 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
 *        - ?
 */
#define STREAM_READ_ATONCE 32767
#define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)

typedef struct
77
{
Laurent Aimar's avatar
Laurent Aimar committed
78
    int64_t i_date;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
79

Laurent Aimar's avatar
Laurent Aimar committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    int64_t i_start;
    int64_t i_end;

    uint8_t *p_buffer;
} stream_track_t;

struct stream_sys_t
{
    access_t    *p_access;

    vlc_bool_t  b_block;    /* Block method (1) or stream */

    int64_t     i_pos;      /* Current reading offset */

    /* Method 1: pf_block */
    struct
96
    {
Laurent Aimar's avatar
Laurent Aimar committed
97 98 99 100 101 102 103 104 105 106 107
        int64_t i_start;        /* Offset of block for p_first */
        int     i_offset;       /* Offset for data in p_current */
        block_t *p_current;     /* Current block */

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

    /* Method 2: for pf_read */
    struct
108
    {
109
        int i_offset;   /* Buffer offset in the current track */
Laurent Aimar's avatar
Laurent Aimar committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        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;
    } stream;

    /* Peek temporary buffer */
    int     i_peek;
    uint8_t *p_peek;

    /* Stat for both method */
    struct
127
    {
Laurent Aimar's avatar
Laurent Aimar committed
128
        vlc_bool_t b_fastseek;  /* From access */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
129

Laurent Aimar's avatar
Laurent Aimar committed
130 131 132 133
        /* Stat about reading data */
        int64_t i_read_count;
        int64_t i_bytes;
        int64_t i_read_time;
134

Laurent Aimar's avatar
Laurent Aimar committed
135 136 137 138 139
        /* Stat about seek */
        int     i_seek_count;
        int64_t i_seek_time;
    } stat;
};
140

Laurent Aimar's avatar
Laurent Aimar committed
141 142 143 144 145
/* Method 1: */
static int  AStreamReadBlock( stream_t *, void *p_read, int i_read );
static int  AStreamPeekBlock( stream_t *, uint8_t **p_peek, int i_read );
static int  AStreamSeekBlock( stream_t *s, int64_t i_pos );
static void AStreamPrebufferBlock( stream_t * );
146

Laurent Aimar's avatar
Laurent Aimar committed
147 148 149 150 151
/* Method 2 */
static int  AStreamReadStream( stream_t *, void *p_read, int i_read );
static int  AStreamPeekStream( stream_t *, uint8_t **pp_peek, int i_read );
static int  AStreamSeekStream( stream_t *s, int64_t i_pos );
static void AStreamPrebufferStream( stream_t * );
152

Laurent Aimar's avatar
Laurent Aimar committed
153 154
/* Common */
static int AStreamControl( stream_t *, int i_query, va_list );
155

156

157
/****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
158
 * stream_AccessNew: create a stream from a access
159
 ****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
160
stream_t *stream_AccessNew( access_t *p_access )
161
{
Laurent Aimar's avatar
Laurent Aimar committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    stream_t *s = vlc_object_create( p_access, VLC_OBJECT_STREAM );
    stream_sys_t *p_sys;

    if( !s )
        return NULL;

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

    s->pf_block  = NULL;
    s->pf_read   = NULL;    /* Set up later */
    s->pf_peek   = NULL;
    s->pf_control= AStreamControl;

    s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );

    /* Common field */
    p_sys->p_access = p_access;
    p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
    p_sys->i_pos = p_access->info.i_pos;

    /* Stats */
    access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
    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;

    /* Peek */
    p_sys->i_peek = 0;
    p_sys->p_peek = NULL;

    if( p_sys->b_block )
    {
        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;
        }
    }
    else
218
    {
Laurent Aimar's avatar
Laurent Aimar committed
219 220 221 222 223 224 225 226 227 228
        int i;

        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 );
        p_sys->stream.i_used   = 0;
229 230
        access2_Control( p_access, ACCESS_GET_MTU,
                         &p_sys->stream.i_read_size );
Laurent Aimar's avatar
Laurent Aimar committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        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 );
247

Laurent Aimar's avatar
Laurent Aimar committed
248 249 250 251 252
        if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
        {
            msg_Err( s, "cannot pre fill buffer" );
            goto error;
        }
253
    }
Laurent Aimar's avatar
Laurent Aimar committed
254

255
    return s;
Laurent Aimar's avatar
Laurent Aimar committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269

error:
    if( p_sys->b_block )
    {
        /* Nothing yet */
    }
    else
    {
        free( p_sys->stream.p_buffer );
    }
    free( s->p_sys );
    vlc_object_detach( s );
    vlc_object_destroy( s );
    return NULL;
270 271
}

272
/****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
273
 * stream_AccessDelete:
274
 ****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
275
void stream_AccessDelete( stream_t *s )
276
{
Laurent Aimar's avatar
Laurent Aimar committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    stream_sys_t *p_sys = s->p_sys;

    vlc_object_detach( s );

    if( p_sys->b_block )
    {
        block_ChainRelease( p_sys->block.p_first );
    }
    else
    {
        free( p_sys->stream.p_buffer );
    }

    if( p_sys->p_peek )
        free( p_sys->p_peek );

293
    free( s->p_sys );
294 295 296
    vlc_object_destroy( s );
}

Laurent Aimar's avatar
Laurent Aimar committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
/****************************************************************************
 * 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;

    if( p_sys->b_block )
    {
        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 );
    }
    else
    {
        int i;

        /* 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 );
    }
}
341

342 343 344 345 346 347 348 349 350
/****************************************************************************
 * stream_AccessUpdate:
 ****************************************************************************/
void stream_AccessUpdate( stream_t *s )
{
    stream_sys_t *p_sys = s->p_sys;
    p_sys->i_pos = p_sys->p_access->info.i_pos;
}

351
/****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
352
 * AStreamControl:
353
 ****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
354
static int AStreamControl( stream_t *s, int i_query, va_list args )
355
{
Laurent Aimar's avatar
Laurent Aimar committed
356 357
    stream_sys_t *p_sys = s->p_sys;
    access_t     *p_access = p_sys->p_access;
358

Laurent Aimar's avatar
Laurent Aimar committed
359 360 361
    vlc_bool_t *p_bool;
    int64_t    *pi_64, i_64;
    int        i_int;
362 363 364 365

    switch( i_query )
    {
        case STREAM_GET_SIZE:
Laurent Aimar's avatar
Laurent Aimar committed
366 367 368
            pi_64 = (int64_t*)va_arg( args, int64_t * );
            *pi_64 = p_access->info.i_size;
            break;
369 370

        case STREAM_CAN_SEEK:
Laurent Aimar's avatar
Laurent Aimar committed
371 372 373
            p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
            access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
            break;
374 375

        case STREAM_CAN_FASTSEEK:
Laurent Aimar's avatar
Laurent Aimar committed
376 377 378
            p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
            access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
            break;
379 380

        case STREAM_GET_POSITION:
Laurent Aimar's avatar
Laurent Aimar committed
381 382 383
            pi_64 = (int64_t*)va_arg( args, int64_t * );
            *pi_64 = p_sys->i_pos;
            break;
384 385

        case STREAM_SET_POSITION:
Laurent Aimar's avatar
Laurent Aimar committed
386 387 388 389 390
            i_64 = (int64_t)va_arg( args, int64_t );
            if( p_sys->b_block )
                return AStreamSeekBlock( s, i_64 );
            else
                return AStreamSeekStream( s, i_64 );
391

392
        case STREAM_GET_MTU:
Laurent Aimar's avatar
Laurent Aimar committed
393
            return VLC_EGENERIC;
394

395
        case STREAM_CONTROL_ACCESS:
Laurent Aimar's avatar
Laurent Aimar committed
396
            i_int = (int) va_arg( args, int );
397 398
            if( i_int != ACCESS_SET_PRIVATE_ID_STATE
                 && i_int != ACCESS_SET_PRIVATE_ID_CA )
399 400 401 402 403
            {
                msg_Err( s, "Hey, what are you thinking ?"
                            "DON'T USE STREAM_CONTROL_ACCESS !!!" );
                return VLC_EGENERIC;
            }
404
            return access2_vaControl( p_access, i_int, args );
405

406 407 408 409
        default:
            msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
            return VLC_EGENERIC;
    }
Laurent Aimar's avatar
Laurent Aimar committed
410
    return VLC_SUCCESS;
411 412
}

Laurent Aimar's avatar
Laurent Aimar committed
413 414


415
/****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
416
 * Method 1:
417
 ****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
418
static void AStreamPrebufferBlock( stream_t *s )
419
{
Laurent Aimar's avatar
Laurent Aimar committed
420 421
    stream_sys_t *p_sys = s->p_sys;
    access_t     *p_access = p_sys->p_access;
422

Laurent Aimar's avatar
Laurent Aimar committed
423 424
    int64_t i_first = 0;
    int64_t i_start;
425

Laurent Aimar's avatar
Laurent Aimar committed
426 427 428
    msg_Dbg( s, "pre buffering" );
    i_start = mdate();
    for( ;; )
429
    {
Laurent Aimar's avatar
Laurent Aimar committed
430 431
        int64_t i_date = mdate();
        block_t *b;
432

433
        if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
Laurent Aimar's avatar
Laurent Aimar committed
434 435 436 437 438 439 440 441
            ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
        {
            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;
            i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
442
                         (p_sys->stat.i_read_time + 1);
Laurent Aimar's avatar
Laurent Aimar committed
443

444 445
            msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
                     I64Fd" kbytes/s",
Laurent Aimar's avatar
Laurent Aimar committed
446 447 448 449 450
                     p_sys->stat.i_bytes,
                     p_sys->stat.i_read_time / I64C(1000000),
                     i_byterate / 1024 );
            break;
        }
451

Laurent Aimar's avatar
Laurent Aimar committed
452 453
        /* Fetch a block */
        if( ( b = p_access->pf_block( p_access ) ) == NULL )
454
        {
Laurent Aimar's avatar
Laurent Aimar committed
455 456 457 458 459
            if( p_access->info.b_eof )
                break;

            msleep( STREAM_DATA_WAIT );
            continue;
460
        }
Laurent Aimar's avatar
Laurent Aimar committed
461 462

        if( i_first == 0 )
463
        {
Laurent Aimar's avatar
Laurent Aimar committed
464
            i_first = mdate();
465 466
            msg_Dbg( s, "received first data for our buffer");
        }
Laurent Aimar's avatar
Laurent Aimar committed
467 468 469 470 471 472 473

        /* 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;

        p_sys->stat.i_read_count++;
474 475
    }

Laurent Aimar's avatar
Laurent Aimar committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    p_sys->block.p_current = p_sys->block.p_first;
}

static int AStreamRefillBlock( stream_t *s );

static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
{
    stream_sys_t *p_sys = s->p_sys;

    uint8_t *p_data= (uint8_t*)p_read;
    int     i_data = 0;

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

492
    if( p_read == NULL )
Damien Fouilleul's avatar
 
Damien Fouilleul committed
493 494 495 496 497 498 499 500 501
    {
	/* seek within this stream if possible, else use plain old read and discard */
        stream_sys_t *p_sys = s->p_sys;
        access_t     *p_access = p_sys->p_access;
        vlc_bool_t   b_aseek;
        access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
        if( b_aseek )
            return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
    }
502

Laurent Aimar's avatar
Laurent Aimar committed
503
    while( i_data < i_read )
504
    {
505 506
        int i_current =
            p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
Laurent Aimar's avatar
Laurent Aimar committed
507
        int i_copy = __MIN( i_current, i_read - i_data);
Gildas Bazin's avatar
 
Gildas Bazin committed
508

Laurent Aimar's avatar
Laurent Aimar committed
509 510
        /* Copy data */
        if( p_data )
511
        {
512 513 514
            memcpy( p_data,
                    &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
                    i_copy );
Laurent Aimar's avatar
Laurent Aimar committed
515 516 517
            p_data += i_copy;
        }
        i_data += i_copy;
518

Laurent Aimar's avatar
Laurent Aimar committed
519 520 521 522 523 524 525 526 527 528 529
        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;
            }
            /*Get a new block */
            if( AStreamRefillBlock( s ) )
Gildas Bazin's avatar
Gildas Bazin committed
530 531 532
            {
                break;
            }
533 534 535
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
536 537
    p_sys->i_pos += i_data;
    return i_data;
538
}
Gildas Bazin's avatar
Gildas Bazin committed
539

Laurent Aimar's avatar
Laurent Aimar committed
540
static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
541
{
Laurent Aimar's avatar
Laurent Aimar committed
542 543 544 545 546 547
    stream_sys_t *p_sys = s->p_sys;
    uint8_t *p_data;
    int      i_data = 0;
    block_t *b;
    int      i_offset;

548
    if( p_sys->block.p_current == NULL ) return 0; /* EOF */
Laurent Aimar's avatar
Laurent Aimar committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

    /* 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 )
    {
        if( p_sys->p_peek )
            free( p_sys->p_peek );
        p_sys->i_peek = i_read;
        p_sys->p_peek = malloc( p_sys->i_peek );
    }

    /* Fill enough data */
567 568
    while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
           < i_read )
Laurent Aimar's avatar
Laurent Aimar committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    {
        block_t **pp_last = p_sys->block.pp_last;

        if( AStreamRefillBlock( s ) )
            break;

        /* Our buffer are probably filled enough, don't try anymore */
        if( pp_last == p_sys->block.pp_last )
            break;
    }

    /* 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 )
    {
        int i_current = b->i_buffer - i_offset;
        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
604
}
605

Laurent Aimar's avatar
Laurent Aimar committed
606
static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
607
{
Laurent Aimar's avatar
Laurent Aimar committed
608 609 610 611
    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;
    vlc_bool_t b_seek;
612

Laurent Aimar's avatar
Laurent Aimar committed
613 614 615 616 617
    /* 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;
618

Laurent Aimar's avatar
Laurent Aimar committed
619 620 621 622 623
        while( i_current + b->i_buffer < i_offset )
        {
            i_current += b->i_buffer;
            b = b->p_next;
        }
624

Laurent Aimar's avatar
Laurent Aimar committed
625 626
        p_sys->block.p_current = b;
        p_sys->block.i_offset = i_offset - i_current;
627

Laurent Aimar's avatar
Laurent Aimar committed
628
        p_sys->i_pos = i_pos;
629

Laurent Aimar's avatar
Laurent Aimar committed
630 631
        return VLC_SUCCESS;
    }
632

Laurent Aimar's avatar
Laurent Aimar committed
633 634
    /* We may need to seek or to read data */
    if( i_offset < 0 )
635
    {
Laurent Aimar's avatar
Laurent Aimar committed
636 637 638 639 640 641 642 643 644 645
        vlc_bool_t b_aseek;
        access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );

        if( !b_aseek )
        {
            msg_Err( s, "backward seek impossible (access non seekable)" );
            return VLC_EGENERIC;
        }

        b_seek = VLC_TRUE;
646
    }
Laurent Aimar's avatar
Laurent Aimar committed
647 648 649
    else
    {
        vlc_bool_t b_aseek, b_aseekfast;
650

Laurent Aimar's avatar
Laurent Aimar committed
651 652 653 654 655 656
        access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
        access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );

        if( !b_aseek )
        {
            b_seek = VLC_FALSE;
657 658
            msg_Warn( s, I64Fd" bytes need to be skipped "
                      "(access non seekable)",
Laurent Aimar's avatar
Laurent Aimar committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
                      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 )
                b_seek = VLC_FALSE;
            else
                b_seek = VLC_TRUE;

676
            msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
Laurent Aimar's avatar
Laurent Aimar committed
677 678
                     b_seek, i_th*i_avg, i_skip );
        }
679 680
    }

Laurent Aimar's avatar
Laurent Aimar committed
681 682 683 684 685 686 687 688
    if( b_seek )
    {
        int64_t i_start, i_end;
        /* Do the access seek */
        i_start = mdate();
        if( p_access->pf_seek( p_access, i_pos ) )
            return VLC_EGENERIC;
        i_end = mdate();
689

Laurent Aimar's avatar
Laurent Aimar committed
690 691 692 693 694 695 696 697 698 699
        /* 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;
700

Laurent Aimar's avatar
Laurent Aimar committed
701 702 703 704 705 706 707 708 709 710 711 712
        /* Refill a block */
        if( AStreamRefillBlock( s ) )
        {
            msg_Err( s, "cannot re fill buffer" );
            return VLC_EGENERIC;
        }
        /* Update stat */
        p_sys->stat.i_seek_time += i_end - i_start;
        p_sys->stat.i_seek_count++;
        return VLC_SUCCESS;
    }
    else
713
    {
Laurent Aimar's avatar
Laurent Aimar committed
714 715
        /* Read enought data */
        while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
716
        {
Laurent Aimar's avatar
Laurent Aimar committed
717 718 719 720 721 722 723
            if( AStreamRefillBlock( s ) )
            {
                msg_Err( s, "can't read enough data in seek" );
                return VLC_EGENERIC;
            }
            while( p_sys->block.p_current &&
                   p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
724
            {
Laurent Aimar's avatar
Laurent Aimar committed
725 726
                p_sys->i_pos += p_sys->block.p_current->i_buffer;
                p_sys->block.p_current = p_sys->block.p_current->p_next;
727 728 729
            }
        }

Laurent Aimar's avatar
Laurent Aimar committed
730 731
        p_sys->block.i_offset = i_pos - p_sys->i_pos;
        p_sys->i_pos = i_pos;
732

Laurent Aimar's avatar
Laurent Aimar committed
733 734
        /* TODO read data */
        return VLC_SUCCESS;
735 736
    }

Laurent Aimar's avatar
Laurent Aimar committed
737
    return VLC_EGENERIC;
738 739
}

Laurent Aimar's avatar
Laurent Aimar committed
740
static int AStreamRefillBlock( stream_t *s )
741
{
Laurent Aimar's avatar
Laurent Aimar committed
742 743 744 745 746 747 748 749 750 751
    stream_sys_t *p_sys = s->p_sys;
    access_t     *p_access = p_sys->p_access;
    int64_t      i_start, i_stop;
    block_t      *b;

    /* Release data */
    while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
           p_sys->block.p_first != p_sys->block.p_current )
    {
        block_t *b = p_sys->block.p_first;
752

Laurent Aimar's avatar
Laurent Aimar committed
753 754 755
        p_sys->block.i_start += b->i_buffer;
        p_sys->block.i_size  -= b->i_buffer;
        p_sys->block.p_first  = b->p_next;
756

Laurent Aimar's avatar
Laurent Aimar committed
757 758 759 760 761
        block_Release( b );
    }
    if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
        p_sys->block.p_current == p_sys->block.p_first &&
        p_sys->block.p_current->p_next )    /* At least 2 packets */
762
    {
Laurent Aimar's avatar
Laurent Aimar committed
763 764
        /* Enough data, don't read more */
        return VLC_SUCCESS;
765 766
    }

Laurent Aimar's avatar
Laurent Aimar committed
767 768 769
    /* Now read a new block */
    i_start = mdate();
    for( ;; )
770
    {
Laurent Aimar's avatar
Laurent Aimar committed
771 772 773 774 775 776 777 778 779 780 781 782
        if( s->b_die )
            return VLC_EGENERIC;


        /* Fetch a block */
        if( ( b = p_access->pf_block( p_access ) ) )
            break;

        if( p_access->info.b_eof )
            return VLC_EGENERIC;

        msleep( STREAM_DATA_WAIT );
783
    }
Laurent Aimar's avatar
Laurent Aimar committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
    i_stop = mdate();

    /* 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;

    /* Fix p_current */
    if( p_sys->block.p_current == NULL )
        p_sys->block.p_current = b;

    /* Update stat */
    p_sys->stat.i_bytes += b->i_buffer;
    p_sys->stat.i_read_time += i_stop - i_start;
    p_sys->stat.i_read_count++;

    return VLC_SUCCESS;
801 802 803
}


Laurent Aimar's avatar
Laurent Aimar committed
804 805 806 807 808 809
/****************************************************************************
 * Method 2:
 ****************************************************************************/
static int AStreamRefillStream( stream_t *s );

static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
810
{
Laurent Aimar's avatar
Laurent Aimar committed
811 812
    stream_sys_t *p_sys = s->p_sys;
    stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
813

814
    uint8_t *p_data = (uint8_t *)p_read;
Laurent Aimar's avatar
Laurent Aimar committed
815
    int      i_data = 0;
816

817
    if( tk->i_start >= tk->i_end ) return 0; /* EOF */
818

819
    if( p_read == NULL )
Damien Fouilleul's avatar
 
Damien Fouilleul committed
820 821 822 823 824 825 826 827 828
    {
	/* seek within this stream if possible, else use plain old read and discard */
        stream_sys_t *p_sys = s->p_sys;
        access_t     *p_access = p_sys->p_access;
        vlc_bool_t   b_aseek;
        access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
        if( b_aseek )
            return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
    }
829

830 831 832 833 834 835
#if 0
    msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
             " offset=%d end="I64Fd,
             i_read, p_sys->i_pos, p_sys->stream.i_tk,
             tk->i_start, p_sys->stream.i_offset, tk->i_end );
#endif
Laurent Aimar's avatar
Laurent Aimar committed
836 837

    while( i_data < i_read )
838
    {
839 840 841 842 843
        int i_off = (tk->i_start + p_sys->stream.i_offset) %
                    STREAM_CACHE_TRACK_SIZE;
        int i_current =
            __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
                   STREAM_CACHE_TRACK_SIZE - i_off );
Laurent Aimar's avatar
Laurent Aimar committed
844 845
        int i_copy = __MIN( i_current, i_read - i_data );

846 847
        if( i_copy <= 0 ) break; /* EOF */

Laurent Aimar's avatar
Laurent Aimar committed
848
        /* Copy data */
849
        /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
Laurent Aimar's avatar
Laurent Aimar committed
850
        if( p_data )
851
        {
Laurent Aimar's avatar
Laurent Aimar committed
852 853
            memcpy( p_data, &tk->p_buffer[i_off], i_copy );
            p_data += i_copy;
854
        }
Laurent Aimar's avatar
Laurent Aimar committed
855 856 857 858
        i_data += i_copy;
        p_sys->stream.i_offset += i_copy;

        /* Update pos now */
859 860
        p_sys->i_pos += i_copy;

Laurent Aimar's avatar
Laurent Aimar committed
861 862 863 864
        /* */
        p_sys->stream.i_used += i_copy;
        if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
            p_sys->stream.i_used >= p_sys->stream.i_read_size )
865
        {
Laurent Aimar's avatar
Laurent Aimar committed
866 867
            if( AStreamRefillStream( s ) )
            {
868 869
                /* EOF */
                if( tk->i_start >= tk->i_end ) break;
Laurent Aimar's avatar
Laurent Aimar committed
870
            }
871 872 873
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
874
    return i_data;
875
}
Laurent Aimar's avatar
Laurent Aimar committed
876 877

static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
878
{
Laurent Aimar's avatar
Laurent Aimar committed
879 880 881
    stream_sys_t *p_sys = s->p_sys;
    stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
    int64_t i_off;
882

883
    if( tk->i_start >= tk->i_end ) return 0; /* EOF */
Laurent Aimar's avatar
Laurent Aimar committed
884

885 886 887 888 889 890
#if 0
    msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
             "start="I64Fd" offset=%d end="I64Fd,
             i_read, p_sys->i_pos, p_sys->stream.i_tk,
             tk->i_start, p_sys->stream.i_offset, tk->i_end );
#endif
Laurent Aimar's avatar
Laurent Aimar committed
891 892 893 894 895 896

    /* Avoid problem, but that should *never* happen */
    if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
        i_read = STREAM_CACHE_TRACK_SIZE / 2;

    while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
897
    {
898 899 900 901 902 903 904
        if( p_sys->stream.i_used <= 1 )
        {
            /* Be sure we will read something */
            p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
        }
        if( AStreamRefillStream( s ) )
            break;
905 906
    }

Laurent Aimar's avatar
Laurent Aimar committed
907 908
    if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
        i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
909

Laurent Aimar's avatar
Laurent Aimar committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
    /* Now, direct pointer or a copy ? */
    i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
    if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
    {
        *pp_peek = &tk->p_buffer[i_off];
        return i_read;
    }

    if( p_sys->i_peek < i_read )
    {
        if( p_sys->p_peek ) free( p_sys->p_peek );
        p_sys->i_peek = i_read;
        p_sys->p_peek = malloc( i_read );
    }

925 926 927 928
    memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
            STREAM_CACHE_TRACK_SIZE - i_off );
    memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
            &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
Laurent Aimar's avatar
Laurent Aimar committed
929 930 931

    *pp_peek = p_sys->p_peek;
    return i_read;
932 933
}

Laurent Aimar's avatar
Laurent Aimar committed
934
static int AStreamSeekStream( stream_t *s, int64_t i_pos )
935
{
Laurent Aimar's avatar
Laurent Aimar committed
936 937 938 939 940 941 942 943
    stream_sys_t *p_sys = s->p_sys;
    access_t     *p_access = p_sys->p_access;
    vlc_bool_t   b_aseek;
    vlc_bool_t   b_afastseek;
    int i_maxth;
    int i_new;
    int i;

944 945 946 947 948 949 950 951
#if 0
    msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
             "tk=%d start="I64Fd" offset=%d end="I64Fd,
             i_pos, p_sys->i_pos, p_sys->stream.i_tk,
             p_sys->stream.tk[p_sys->stream.i_tk].i_start,
             p_sys->stream.i_offset,
             p_sys->stream.tk[p_sys->stream.i_tk].i_end );
#endif
Laurent Aimar's avatar
Laurent Aimar committed
952 953 954 955 956


    /* Seek in our current track ? */
    if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
        i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
957
    {
Laurent Aimar's avatar
Laurent Aimar committed
958 959 960 961 962
        //msg_Dbg( s, "AStreamSeekStream: current track" );
        p_sys->i_pos = i_pos;
        p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
        return VLC_SUCCESS;
    }
963

Laurent Aimar's avatar
Laurent Aimar committed
964 965 966 967
    access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
    if( !b_aseek )
    {
        /* We can't do nothing */
Damien Fouilleul's avatar
 
Damien Fouilleul committed
968
        msg_Dbg( s, "AStreamSeekStream: can't seek" );
Laurent Aimar's avatar
Laurent Aimar committed
969 970
        return VLC_EGENERIC;
    }
971

Laurent Aimar's avatar
Laurent Aimar committed
972 973
    /* Date the current track */
    p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
974

Laurent Aimar's avatar
Laurent Aimar committed
975 976 977 978
    /* Try to reuse already read data */
    for( i = 0; i < STREAM_CACHE_TRACK; i++ )
    {
        stream_track_t *tk = &p_sys->stream.tk[i];
979

Laurent Aimar's avatar
Laurent Aimar committed
980 981
        if( i_pos >= tk->i_start && i_pos <= tk->i_end )
        {
982 983 984 985
#if 0
            msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
                     " end="I64Fd, i, tk->i_start, tk->i_end );
#endif
Laurent Aimar's avatar
Laurent Aimar committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999
            /* Seek at the end of the buffer */
            if( p_access->pf_seek( p_access, tk->i_end ) )
                return VLC_EGENERIC;

            /* That's it */
            p_sys->i_pos = i_pos;
            p_sys->stream.i_tk = i;
            p_sys->stream.i_offset = i_pos - tk->i_start;

            if( p_sys->stream.i_used < 1024 )
                p_sys->stream.i_used = 1024;

            if( AStreamRefillStream( s ) )
                return VLC_EGENERIC;
1000 1001

            return VLC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
1002 1003
        }
    }
1004

Laurent Aimar's avatar
Laurent Aimar committed
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
    access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
    /* FIXME compute seek cost (instead of static 'stupid' value) */
    i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
    if( !b_afastseek )
        i_maxth *= 3;

    /* FIXME TODO */
#if 0
    /* Search closest segment TODO */
    for( i = 0; i < STREAM_CACHE_TRACK; i++ )
    {
        stream_track_t *tk = &p_sys->stream.tk[i];

        if( i_pos + i_maxth >= tk->i_start )
        {
            msg_Dbg( s, "good segment before current pos, TODO" );
        }
        if( i_pos - i_maxth <= tk->i_end )
        {
            msg_Dbg( s, "good segment after current pos, TODO" );
        }
1026
    }
Laurent Aimar's avatar
Laurent Aimar committed
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
#endif

    /* Nothing good, seek and choose oldest segment */
    if( p_access->pf_seek( p_access, i_pos ) )
        return VLC_EGENERIC;
    p_sys->i_pos = i_pos;

    i_new = 0;
    for( i = 1; i < STREAM_CACHE_TRACK; i++ )
    {
        if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
            i_new = i;
    }

    /* Reset the segment */
    p_sys->stream.i_tk     = i_new;
    p_sys->stream.i_offset =  0;
    p_sys->stream.tk[i_new].i_start = i_pos;
    p_sys->stream.tk[i_new].i_end   = i_pos;

    /* Read data */
    if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
        p_sys->stream.i_used = STREAM_READ_ATONCE / 2;

    if( AStreamRefillStream( s ) )
        return VLC_EGENERIC;

    return VLC_SUCCESS;
1055 1056
}

Laurent Aimar's avatar
Laurent Aimar committed
1057
static int AStreamRefillStream( stream_t *s )
1058
{
Laurent Aimar's avatar
Laurent Aimar committed
1059 1060 1061
    stream_sys_t *p_sys = s->p_sys;
    access_t     *p_access = p_sys->p_access;
    stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1062 1063

    /* We read but won't increase i_start after initial start + offset */
1064 1065 1066
    int i_toread =
        __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
               (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1067
    vlc_bool_t b_read = VLC_FALSE;
Laurent Aimar's avatar
Laurent Aimar committed
1068
    int64_t i_start, i_stop;
1069

1070 1071 1072
    if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */

    /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1073

Laurent Aimar's avatar
Laurent Aimar committed
1074 1075
    i_start = mdate();
    while( i_toread > 0 )
1076
    {
Laurent Aimar's avatar
Laurent Aimar committed
1077 1078 1079 1080 1081 1082 1083 1084
        int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
        int i_read;

        if( s->b_die )
            return VLC_EGENERIC;

        i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
        i_read = p_access->pf_read( p_access, &tk->p_buffer[i_off], i_read );
1085 1086

        /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
Laurent Aimar's avatar
Laurent Aimar committed
1087 1088 1089 1090 1091 1092 1093
        if( i_read <  0 )
        {
            msleep( STREAM_DATA_WAIT );
            continue;
        }
        else if( i_read == 0 )
        {
1094 1095 1096
            if( !b_read )
                return VLC_EGENERIC;
            return VLC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
1097
        }
1098
        b_read = VLC_TRUE;
Laurent Aimar's avatar
Laurent Aimar committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116

        /* Update end */
        tk->i_end += i_read;

        /* Windows of STREAM_CACHE_TRACK_SIZE */
        if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
        {
            int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;

            tk->i_start += i_invalid;
            p_sys->stream.i_offset -= i_invalid;
        }

        i_toread -= i_read;
        p_sys->stream.i_used -= i_read;

        p_sys->stat.i_bytes += i_read;
        p_sys->stat.i_read_count++;
1117
    }
Laurent Aimar's avatar
Laurent Aimar committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    i_stop = mdate();

    p_sys->stat.i_read_time += i_stop - i_start;

    return VLC_SUCCESS;
}

static void AStreamPrebufferStream( stream_t *s )
{
    stream_sys_t *p_sys = s->p_sys;
    access_t     *p_access = p_sys->p_access;
1129

Laurent Aimar's avatar
Laurent Aimar committed
1130 1131
    int64_t i_first = 0;
    int64_t i_start;
1132 1133
    int64_t i_prebuffer = (s->p_sys->p_access->info.i_title > 1 ||
                           s->p_sys->p_access->info.i_seekpoint > 1) ? STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3;
1134

Laurent Aimar's avatar
Laurent Aimar committed
1135 1136 1137
    msg_Dbg( s, "pre buffering" );
    i_start = mdate();
    for( ;; )
1138
    {
Laurent Aimar's avatar
Laurent Aimar committed
1139 1140 1141 1142 1143
        stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];

        int64_t i_date = mdate();
        int i_read;

1144
        if( s->b_die || tk->i_end >= i_prebuffer ||
1145
            (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1146
        {
Laurent Aimar's avatar
Laurent Aimar committed
1147 1148 1149 1150 1151 1152 1153 1154
            int64_t i_byterate;

            /* Update stat */
            p_sys->stat.i_bytes = tk->i_end - tk->i_start;
            p_sys->stat.i_read_time = i_date - i_start;
            i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
                         (p_sys->stat.i_read_time+1);

1155 1156
            msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
                     I64Fd" kbytes/s",
Laurent Aimar's avatar
Laurent Aimar committed
1157 1158 1159
                     p_sys->stat.i_bytes,
                     p_sys->stat.i_read_time / I64C(1000000),
                     i_byterate / 1024 );
1160 1161
            break;
        }
Laurent Aimar's avatar
Laurent Aimar committed
1162 1163

        /* */
1164 1165
        i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
        i_read = __MIN( p_sys->stream.i_read_size, i_read );
1166 1167
        i_read = p_access->pf_read( p_access, &tk->p_buffer[tk->i_end],
                                    i_read );
Laurent Aimar's avatar
Laurent Aimar committed
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
        if( i_read <  0 )
        {
            msleep( STREAM_DATA_WAIT );
            continue;
        }
        else if( i_read == 0 )
        {
            /* EOF */
            break;
        }

        if( i_first == 0 )
1180
        {
Laurent Aimar's avatar
Laurent Aimar committed
1181
            i_first = mdate();
1182 1183
            msg_Dbg( s, "received first data for our buffer");
        }
Laurent Aimar's avatar
Laurent Aimar committed
1184 1185 1186 1187

        tk->i_end += i_read;

        p_sys->stat.i_read_count++;
1188 1189 1190 1191
    }
}


Laurent Aimar's avatar
Laurent Aimar committed
1192 1193 1194 1195 1196 1197 1198 1199
/****************************************************************************
 * stream_ReadLine:
 ****************************************************************************/
/**
 * Read from the stream untill first newline.
 * \param s Stream handle to read from
 * \return A null-terminated string. This must be freed,
 */
1200 1201
#define STREAM_PROBE_LINE 2048
#define STREAM_LINE_MAX (2048*100)
Laurent Aimar's avatar
Laurent Aimar committed
1202 1203
char *stream_ReadLine( stream_t *s )
{
1204 1205
    char *p_line = NULL;
    int i_line = 0, i_read = 0;
1206

1207
    while( i_read < STREAM_LINE_MAX )
Laurent Aimar's avatar
Laurent Aimar committed
1208
    {
1209
        char *psz_eol;
Laurent Aimar's avatar
 
Laurent Aimar committed
1210
        uint8_t *p_data;
1211
        int i_data;
Laurent Aimar's avatar
 
Laurent Aimar committed
1212 1213 1214

        /* Probe new data */
        i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1215
        if( i_data <= 0 ) break; /* No more data */
Laurent Aimar's avatar
 
Laurent Aimar committed
1216

1217 1218
        /* Check if there is an EOL */
        if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
Laurent Aimar's avatar
Laurent Aimar committed
1219
        {
1220 1221 1222 1223 1224 1225 1226 1227 1228
            i_data = (psz_eol - (char *)p_data) + 1;
            p_line = realloc( p_line, i_line + i_data + 1 );
            i_data = stream_Read( s, &p_line[i_line], i_data );
            if( i_data <= 0 ) break; /* Hmmm */
            i_line += (i_data - 1);
            i_read += i_data;

            /* We have our line */
            break;
Laurent Aimar's avatar
 
Laurent Aimar committed
1229 1230 1231 1232
        }

        /* Read data (+1 for easy \0 append) */
        p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
1233 1234 1235 1236
        i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
        if( i_data <= 0 ) break; /* Hmmm */
        i_line += i_data;
        i_read += i_data;
Laurent Aimar's avatar
Laurent Aimar committed
1237
    }
Laurent Aimar's avatar
 
Laurent Aimar committed
1238

1239
    /* Remove trailing LF/CR */
1240 1241
    while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
           p_line[i_line-1] == '\n') ) i_line--;
1242

1243
    if( i_read > 0 )
1244
    {
Laurent Aimar's avatar
 
Laurent Aimar committed
1245
        p_line[i_line] = '\0';
1246 1247
        return p_line;
    }
Laurent Aimar's avatar
 
Laurent Aimar committed
1248

1249
    /* We failed to read any data, probably EOF */
1250 1251
    if( p_line ) free( p_line );
    return NULL;
Laurent Aimar's avatar
Laurent Aimar committed
1252
}