stream.c 56.2 KB
Newer Older
1
2
3
/*****************************************************************************
 * stream.c
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
gbazin's avatar
gbazin 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
dionoea's avatar
dionoea 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
#include <vlc_strings.h>
33
#include <vlc_memory.h>
34

35
#include <libvlc.h>
36

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

#include "input_internal.h"
41

42
// #define STREAM_DEBUG 1
43

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

/* Two methods:
 *  - using pf_block
 *      One linked list of data read
 *  - using pf_read
 *      More complex scheme using mutliple track to avoid seeking
56
57
58
 *  - 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)).
Laurent Aimar's avatar
Laurent Aimar committed
59
60
 */

61
62
/* How many tracks we have, currently only used for stream mode */
#ifdef OPTIMIZE_MEMORY
63
64
65
66
67
68
69
70
71
#   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

72
73
74
75
/* 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)
Laurent Aimar's avatar
Laurent Aimar committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

/* 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
 *        - ?
 */
95
#define STREAM_READ_ATONCE 1024
Laurent Aimar's avatar
Laurent Aimar committed
96
97
98
#define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)

typedef struct
99
{
Laurent Aimar's avatar
Laurent Aimar committed
100
    int64_t i_date;
hartman's avatar
hartman committed
101

102
103
    uint64_t i_start;
    uint64_t i_end;
Laurent Aimar's avatar
Laurent Aimar committed
104
105

    uint8_t *p_buffer;
106

Laurent Aimar's avatar
Laurent Aimar committed
107
108
} stream_track_t;

109
110
111
typedef struct
{
    char     *psz_path;
112
    uint64_t  i_size;
113
114
115

} access_entry_t;

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

Laurent Aimar's avatar
Laurent Aimar committed
122
123
124
125
struct stream_sys_t
{
    access_t    *p_access;

126
    stream_read_method_t   method;    /* method to use */
Laurent Aimar's avatar
Laurent Aimar committed
127

128
    uint64_t     i_pos;      /* Current reading offset */
Laurent Aimar's avatar
Laurent Aimar committed
129
130
131

    /* Method 1: pf_block */
    struct
132
    {
133
134
        uint64_t i_start;        /* Offset of block for p_first */
        uint64_t i_offset;       /* Offset for data in p_current */
Laurent Aimar's avatar
Laurent Aimar committed
135
136
        block_t *p_current;     /* Current block */

137
        uint64_t i_size;         /* Total amount of data in the list */
Laurent Aimar's avatar
Laurent Aimar committed
138
139
        block_t *p_first;
        block_t **pp_last;
140

Laurent Aimar's avatar
Laurent Aimar committed
141
142
143
144
    } block;

    /* Method 2: for pf_read */
    struct
145
    {
146
147
        unsigned i_offset;   /* Buffer offset in the current track */
        int      i_tk;       /* Current track */
Laurent Aimar's avatar
Laurent Aimar committed
148
149
150
151
152
153
        stream_track_t tk[STREAM_CACHE_TRACK];

        /* Global buffer */
        uint8_t *p_buffer;

        /* */
154
155
        unsigned i_used; /* Used since last read */
        unsigned i_read_size;
156

Laurent Aimar's avatar
Laurent Aimar committed
157
158
159
    } stream;

    /* Peek temporary buffer */
160
    unsigned int i_peek;
Laurent Aimar's avatar
Laurent Aimar committed
161
162
163
164
    uint8_t *p_peek;

    /* Stat for both method */
    struct
165
    {
166
        bool b_fastseek;  /* From access */
hartman's avatar
hartman committed
167

Laurent Aimar's avatar
Laurent Aimar committed
168
        /* Stat about reading data */
169
170
171
        uint64_t i_read_count;
        uint64_t i_bytes;
        uint64_t i_read_time;
172

Laurent Aimar's avatar
Laurent Aimar committed
173
        /* Stat about seek */
174
175
        unsigned i_seek_count;
        uint64_t i_seek_time;
176

Laurent Aimar's avatar
Laurent Aimar committed
177
    } stat;
178

179
180
181
182
183
    /* Streams list */
    int            i_list;
    access_entry_t **list;
    int            i_list_index;
    access_t       *p_list_access;
Laurent Aimar's avatar
Laurent Aimar committed
184
};
185

Laurent Aimar's avatar
Laurent Aimar committed
186
/* Method 1: */
187
188
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 );
189
static int  AStreamSeekBlock( stream_t *s, uint64_t i_pos );
190
static void AStreamPrebufferBlock( stream_t *s );
191
static block_t *AReadBlock( stream_t *s, bool *pb_eof );
192

Laurent Aimar's avatar
Laurent Aimar committed
193
/* Method 2 */
194
195
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 );
196
static int  AStreamSeekStream( stream_t *s, uint64_t i_pos );
197
static void AStreamPrebufferStream( stream_t *s );
198
static int  AReadStream( stream_t *s, void *p_read, unsigned int i_read );
199

Laurent Aimar's avatar
Laurent Aimar committed
200
/* Common */
201
static int AStreamControl( stream_t *s, int i_query, va_list );
202
203
static void AStreamDestroy( stream_t *s );
static void UStreamDestroy( stream_t *s );
204
static int  ASeek( stream_t *s, uint64_t i_pos );
205

Laurent Aimar's avatar
Laurent Aimar committed
206
207
208
209
210
/****************************************************************************
 * stream_CommonNew: create an empty stream structure
 ****************************************************************************/
stream_t *stream_CommonNew( vlc_object_t *p_obj )
{
Laurent Aimar's avatar
Laurent Aimar committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    stream_t *s = (stream_t *)vlc_custom_create( p_obj, sizeof(*s),
                                                 VLC_OBJECT_GENERIC, "stream" );

    if( !s )
        return NULL;

    s->p_text = malloc( sizeof(*s->p_text) );
    if( !s->p_text )
    {
        vlc_object_release( s );
        return NULL;
    }

    /* UTF16 and UTF32 text file conversion */
    s->p_text->conv = (vlc_iconv_t)(-1);
    s->p_text->i_char_width = 1;
    s->p_text->b_little_endian = false;

    return s;
}
231

Laurent Aimar's avatar
Laurent Aimar committed
232
233
234
235
236
237
238
239
void stream_CommonDelete( stream_t *s )
{
    if( s->p_text )
    {
        if( s->p_text->conv != (vlc_iconv_t)(-1) )
            vlc_iconv_close( s->p_text->conv );
        free( s->p_text );
    }
240
    free( s->psz_access );
241
    free( s->psz_path );
Laurent Aimar's avatar
Laurent Aimar committed
242
    vlc_object_release( s );
Laurent Aimar's avatar
Laurent Aimar committed
243
244
}

245
#undef stream_UrlNew
246
/****************************************************************************
247
 * stream_UrlNew: create a stream from a access
248
 ****************************************************************************/
249
stream_t *stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
250
{
251
252
    const char *psz_access, *psz_demux;
    char *psz_path;
253
254
    access_t *p_access;
    stream_t *p_res;
255

Laurent Aimar's avatar
Laurent Aimar committed
256
257
    if( !psz_url )
        return NULL;
258

Laurent Aimar's avatar
Laurent Aimar committed
259
260
261
    char psz_dup[strlen( psz_url ) + 1];
    strcpy( psz_dup, psz_url );
    input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
262

263
    /* Now try a real access */
264
    p_access = access_New( p_parent, NULL, psz_access, psz_demux, psz_path );
265
266
267
268
269
    if( p_access == NULL )
    {
        msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
        return NULL;
    }
270

271
    if( !( p_res = stream_AccessNew( p_access, NULL ) ) )
272
    {
273
        access_Delete( p_access );
274
        return NULL;
275
    }
276
277
278

    p_res->pf_destroy = UStreamDestroy;
    return p_res;
279
280
}

281
stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
282
{
Laurent Aimar's avatar
Laurent Aimar committed
283
    stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
Laurent Aimar's avatar
Laurent Aimar committed
284
285
    stream_sys_t *p_sys;

hartman's avatar
hartman committed
286
287
288
    if( !s )
        return NULL;

289
    s->p_input = p_access->p_input;
290
    s->psz_access = strdup( p_access->psz_access );
291
    s->psz_path = strdup( p_access->psz_location );
292
    s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
293
    if( !s->psz_access || !s->psz_path || !s->p_sys )
hartman's avatar
hartman committed
294
    {
Laurent Aimar's avatar
Laurent Aimar committed
295
        stream_CommonDelete( s );
hartman's avatar
hartman committed
296
297
        return NULL;
    }
Laurent Aimar's avatar
Laurent Aimar committed
298
299
300

    s->pf_read   = NULL;    /* Set up later */
    s->pf_peek   = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
301
    s->pf_control = AStreamControl;
302
    s->pf_destroy = AStreamDestroy;
Laurent Aimar's avatar
Laurent Aimar committed
303
304
305

    /* Common field */
    p_sys->p_access = p_access;
306
    if( p_access->pf_block )
307
        p_sys->method = STREAM_METHOD_BLOCK;
308
    else
309
        p_sys->method = STREAM_METHOD_STREAM;
310

Laurent Aimar's avatar
Laurent Aimar committed
311
312
313
    p_sys->i_pos = p_access->info.i_pos;

    /* Stats */
314
    access_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
Laurent Aimar's avatar
Laurent Aimar committed
315
316
317
318
319
320
    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;

321
    TAB_INIT( p_sys->i_list, p_sys->list );
322
    p_sys->i_list_index = 0;
323
    p_sys->p_list_access = NULL;
324
325

    /* Get the additional list of inputs if any (for concatenation) */
326
    if( ppsz_list && ppsz_list[0] )
327
    {
328
329
        access_entry_t *p_entry = malloc( sizeof(*p_entry) );
        if( !p_entry )
330
            goto error;
331
332

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

344
        for( int i = 0; ppsz_list[i] != NULL; i++ )
345
        {
346
347
348
349
350
            char *psz_name = strdup( ppsz_list[i] );

            if( !psz_name )
                break;

351
            access_t *p_tmp = access_New( p_access, p_access->p_input,
352
353
354
                                          p_access->psz_access, "", psz_name );
            if( !p_tmp )
                continue;
355

356
357
358
359
360
            msg_Dbg( p_access, "adding file `%s', (%"PRId64" bytes)",
                     psz_name, p_tmp->info.i_size );

            p_entry = malloc( sizeof(*p_entry) );
            if( p_entry )
361
362
363
364
365
            {
                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 );
            }
366
            access_Delete( p_tmp );
367
368
369
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
370
371
372
373
    /* Peek */
    p_sys->i_peek = 0;
    p_sys->p_peek = NULL;

374
    if( p_sys->method == STREAM_METHOD_BLOCK )
Laurent Aimar's avatar
Laurent Aimar committed
375
    {
376
        msg_Dbg( s, "Using block method for AStream*" );
Laurent Aimar's avatar
Laurent Aimar committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
        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;
        }
    }
397
    else
398
    {
Laurent Aimar's avatar
Laurent Aimar committed
399
400
        int i;

401
402
        assert( p_sys->method == STREAM_METHOD_STREAM );

403
        msg_Dbg( s, "Using stream method for AStream*" );
404

Laurent Aimar's avatar
Laurent Aimar committed
405
406
407
408
409
410
411
        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 );
412
413
        if( p_sys->stream.p_buffer == NULL )
            goto error;
Laurent Aimar's avatar
Laurent Aimar committed
414
        p_sys->stream.i_used   = 0;
415
416
417
418
        p_sys->stream.i_read_size = STREAM_READ_ATONCE;
#if STREAM_READ_ATONCE < 256
#   error "Invalid STREAM_READ_ATONCE value"
#endif
Laurent Aimar's avatar
Laurent Aimar committed
419
420
421
422
423
424
425
426
427
428
429
430

        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 );
431

Laurent Aimar's avatar
Laurent Aimar committed
432
433
434
435
436
        if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
        {
            msg_Err( s, "cannot pre fill buffer" );
            goto error;
        }
437
    }
Laurent Aimar's avatar
Laurent Aimar committed
438

439
    return s;
Laurent Aimar's avatar
Laurent Aimar committed
440
441

error:
442
    if( p_sys->method == STREAM_METHOD_BLOCK )
Laurent Aimar's avatar
Laurent Aimar committed
443
444
445
446
447
448
449
    {
        /* Nothing yet */
    }
    else
    {
        free( p_sys->stream.p_buffer );
    }
450
451
452
    while( p_sys->i_list > 0 )
        free( p_sys->list[--(p_sys->i_list)] );
    free( p_sys->list );
Laurent Aimar's avatar
Laurent Aimar committed
453
    free( s->p_sys );
Laurent Aimar's avatar
Laurent Aimar committed
454
    stream_CommonDelete( s );
Laurent Aimar's avatar
Laurent Aimar committed
455
    return NULL;
456
457
}

458
/****************************************************************************
459
 * AStreamDestroy:
460
 ****************************************************************************/
461
static void AStreamDestroy( stream_t *s )
462
{
Laurent Aimar's avatar
Laurent Aimar committed
463
464
    stream_sys_t *p_sys = s->p_sys;

465
    if( p_sys->method == STREAM_METHOD_BLOCK )
466
467
468
        block_ChainRelease( p_sys->block.p_first );
    else
        free( p_sys->stream.p_buffer );
469

470
    free( p_sys->p_peek );
471
472

    if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
473
        access_Delete( p_sys->p_list_access );
474
475

    while( p_sys->i_list-- )
Laurent Aimar's avatar
Laurent Aimar committed
476
    {
477
478
        free( p_sys->list[p_sys->i_list]->psz_path );
        free( p_sys->list[p_sys->i_list] );
Laurent Aimar's avatar
Laurent Aimar committed
479
480
    }

481
482
483
    free( p_sys->list );
    free( p_sys );

Laurent Aimar's avatar
Laurent Aimar committed
484
    stream_CommonDelete( s );
485
486
}

487
488
static void UStreamDestroy( stream_t *s )
{
489
    access_t *p_access = (access_t *)s->p_parent;
490
    AStreamDestroy( s );
491
    access_Delete( p_access );
492
493
}

Laurent Aimar's avatar
Laurent Aimar committed
494
/****************************************************************************
495
 * AStreamControlReset:
Laurent Aimar's avatar
Laurent Aimar committed
496
 ****************************************************************************/
497
static void AStreamControlReset( stream_t *s )
Laurent Aimar's avatar
Laurent Aimar committed
498
499
500
501
502
{
    stream_sys_t *p_sys = s->p_sys;

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

503
    if( p_sys->method == STREAM_METHOD_BLOCK )
Laurent Aimar's avatar
Laurent Aimar committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
    {
        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 );
    }
518
    else
Laurent Aimar's avatar
Laurent Aimar committed
519
520
521
    {
        int i;

522
523
        assert( p_sys->method == STREAM_METHOD_STREAM );

Laurent Aimar's avatar
Laurent Aimar committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
        /* 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 );
    }
}
540

541
/****************************************************************************
542
 * AStreamControlUpdate:
543
 ****************************************************************************/
544
static void AStreamControlUpdate( stream_t *s )
545
546
{
    stream_sys_t *p_sys = s->p_sys;
547

548
    p_sys->i_pos = p_sys->p_access->info.i_pos;
549
550
551
552
553
554
555
556
557

    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;
        }
    }
558
559
}

560
/****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
561
 * AStreamControl:
562
 ****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
563
static int AStreamControl( stream_t *s, int i_query, va_list args )
564
{
Laurent Aimar's avatar
Laurent Aimar committed
565
566
    stream_sys_t *p_sys = s->p_sys;
    access_t     *p_access = p_sys->p_access;
567

568
569
570
    bool     *p_bool;
    uint64_t *pi_64, i_64;
    int      i_int;
571
572
573
574

    switch( i_query )
    {
        case STREAM_GET_SIZE:
575
            pi_64 = va_arg( args, uint64_t * );
576
577
578
579
580
581
582
583
            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;
            }
Laurent Aimar's avatar
Laurent Aimar committed
584
585
            *pi_64 = p_access->info.i_size;
            break;
586
587

        case STREAM_CAN_SEEK:
588
            p_bool = (bool*)va_arg( args, bool * );
589
            access_Control( p_access, ACCESS_CAN_SEEK, p_bool );
Laurent Aimar's avatar
Laurent Aimar committed
590
            break;
591
592

        case STREAM_CAN_FASTSEEK:
593
            p_bool = (bool*)va_arg( args, bool * );
594
            access_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
Laurent Aimar's avatar
Laurent Aimar committed
595
            break;
596
597

        case STREAM_GET_POSITION:
598
            pi_64 = va_arg( args, uint64_t * );
Laurent Aimar's avatar
Laurent Aimar committed
599
600
            *pi_64 = p_sys->i_pos;
            break;
601
602

        case STREAM_SET_POSITION:
603
            i_64 = va_arg( args, uint64_t );
604
605
606
            switch( p_sys->method )
            {
            case STREAM_METHOD_BLOCK:
Laurent Aimar's avatar
Laurent Aimar committed
607
                return AStreamSeekBlock( s, i_64 );
608
            case STREAM_METHOD_STREAM:
Laurent Aimar's avatar
Laurent Aimar committed
609
                return AStreamSeekStream( s, i_64 );
610
611
612
613
            default:
                assert(0);
                return VLC_EGENERIC;
            }
614

615
        case STREAM_CONTROL_ACCESS:
616
        {
Laurent Aimar's avatar
Laurent Aimar committed
617
            i_int = (int) va_arg( args, int );
618
619
            if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
                i_int != ACCESS_SET_PRIVATE_ID_CA &&
620
621
622
                i_int != ACCESS_GET_PRIVATE_ID_STATE &&
                i_int != ACCESS_SET_TITLE &&
                i_int != ACCESS_SET_SEEKPOINT )
623
624
625
626
627
            {
                msg_Err( s, "Hey, what are you thinking ?"
                            "DON'T USE STREAM_CONTROL_ACCESS !!!" );
                return VLC_EGENERIC;
            }
628
629
630
631
632
633
634
635
636
            int i_ret = access_vaControl( p_access, i_int, args );
            if( i_int == ACCESS_SET_TITLE || i_int == ACCESS_SET_SEEKPOINT )
                AStreamControlReset( s );
            return i_ret;
        }

        case STREAM_UPDATE_SIZE:
            AStreamControlUpdate( s );
            return VLC_SUCCESS;
637

638
        case STREAM_GET_CONTENT_TYPE:
639
            return access_Control( p_access, ACCESS_GET_CONTENT_TYPE,
640
                                    va_arg( args, char ** ) );
641
        case STREAM_SET_RECORD_STATE:
642
643
644
645
        default:
            msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
            return VLC_EGENERIC;
    }
Laurent Aimar's avatar
Laurent Aimar committed
646
    return VLC_SUCCESS;
647
648
}

649
/****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
650
 * Method 1:
651
 ****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
652
static void AStreamPrebufferBlock( stream_t *s )
653
{
Laurent Aimar's avatar
Laurent Aimar committed
654
    stream_sys_t *p_sys = s->p_sys;
655

Laurent Aimar's avatar
Laurent Aimar committed
656
657
    int64_t i_first = 0;
    int64_t i_start;
658

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
659
    msg_Dbg( s, "starting pre-buffering" );
Laurent Aimar's avatar
Laurent Aimar committed
660
661
    i_start = mdate();
    for( ;; )
662
    {
663
        const int64_t i_date = mdate();
664
        bool b_eof;
Laurent Aimar's avatar
Laurent Aimar committed
665
        block_t *b;
666

667
        if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
Laurent Aimar's avatar
Laurent Aimar committed
668
669
670
671
672
673
        {
            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
674
            i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
675
                         (p_sys->stat.i_read_time + 1);
Laurent Aimar's avatar
Laurent Aimar committed
676

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
677
            msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
678
                     "%"PRId64" KiB/s",
Laurent Aimar's avatar
Laurent Aimar committed
679
                     p_sys->stat.i_bytes,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
680
                     p_sys->stat.i_read_time / INT64_C(1000000),
Laurent Aimar's avatar
Laurent Aimar committed
681
682
683
                     i_byterate / 1024 );
            break;
        }
684

Laurent Aimar's avatar
Laurent Aimar committed
685
        /* Fetch a block */
686
        if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
687
        {
688
689
            if( b_eof )
                break;
Laurent Aimar's avatar
Laurent Aimar committed
690
            continue;
691
        }
Laurent Aimar's avatar
Laurent Aimar committed
692

693
694
695
696
697
698
        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;
Laurent Aimar's avatar
Laurent Aimar committed
699

700
701
702
            p_sys->stat.i_read_count++;
            b = b->p_next;
        }
703
704
705
706

        if( i_first == 0 )
        {
            i_first = mdate();
707
708
            msg_Dbg( s, "received first data after %d ms",
                     (int)((i_first-i_start)/1000) );
709
        }
710
711
    }

Laurent Aimar's avatar
Laurent Aimar committed
712
713
714
715
716
    p_sys->block.p_current = p_sys->block.p_first;
}

static int AStreamRefillBlock( stream_t *s );

717
static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
Laurent Aimar's avatar
Laurent Aimar committed
718
719
720
{
    stream_sys_t *p_sys = s->p_sys;

721
    uint8_t *p_data = p_read;
722
    unsigned int i_data = 0;
Laurent Aimar's avatar
Laurent Aimar committed
723
724
725
726
727

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

728
    if( p_data == NULL )
damienf's avatar
   
damienf committed
729
    {
730
        /* seek within this stream if possible, else use plain old read and discard */
damienf's avatar
   
damienf committed
731
732
        stream_sys_t *p_sys = s->p_sys;
        access_t     *p_access = p_sys->p_access;
733
        bool   b_aseek;
734
        access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
damienf's avatar
   
damienf committed
735
736
737
        if( b_aseek )
            return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
    }
738

Laurent Aimar's avatar
Laurent Aimar committed
739
    while( i_data < i_read )
740
    {
741
742
        int i_current =
            p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
743
        unsigned int i_copy = __MIN( (unsigned int)__MAX(i_current,0), i_read - i_data);
gbazin's avatar
   
gbazin committed
744

Laurent Aimar's avatar
Laurent Aimar committed
745
        /* Copy data */
746
747
748
        if( p_data )
        {
            memcpy( p_data,
749
750
                    &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
                    i_copy );
751
752
            p_data += i_copy;
        }
Laurent Aimar's avatar
Laurent Aimar committed
753
        i_data += i_copy;
754

Laurent Aimar's avatar
Laurent Aimar committed
755
756
757
758
759
760
761
762
763
        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
764
765
            /*Get a new block if needed */
            if( !p_sys->block.p_current && AStreamRefillBlock( s ) )
gbazin's avatar
gbazin committed
766
767
768
            {
                break;
            }
769
770
771
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
772
773
    p_sys->i_pos += i_data;
    return i_data;
774
}
gbazin's avatar
gbazin committed
775

776
static int AStreamPeekBlock( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
sigmunau's avatar
sigmunau committed
777
{
Laurent Aimar's avatar
Laurent Aimar committed
778
779
    stream_sys_t *p_sys = s->p_sys;
    uint8_t *p_data;
780
    unsigned int i_data = 0;
Laurent Aimar's avatar
Laurent Aimar committed
781
    block_t *b;
782
    unsigned int i_offset;
Laurent Aimar's avatar
Laurent Aimar committed
783

784
    if( p_sys->block.p_current == NULL ) return 0; /* EOF */
Laurent Aimar's avatar
Laurent Aimar committed
785
786
787
788
789
790
791
792
793
794
795

    /* 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 )
    {
796
        p_sys->p_peek = realloc_or_free( p_sys->p_peek, i_read );
797
798
799
800
801
        if( !p_sys->p_peek )
        {
            p_sys->i_peek = 0;
            return 0;
        }
Laurent Aimar's avatar
Laurent Aimar committed
802
803
804
805
        p_sys->i_peek = i_read;
    }

    /* Fill enough data */
806
807
    while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
           < i_read )
Laurent Aimar's avatar
Laurent Aimar committed
808
809
810
    {
        block_t **pp_last = p_sys->block.pp_last;

811
        if( AStreamRefillBlock( s ) ) break;
Laurent Aimar's avatar
Laurent Aimar committed
812
813

        /* Our buffer are probably filled enough, don't try anymore */
814
        if( pp_last == p_sys->block.pp_last ) break;
Laurent Aimar's avatar
Laurent Aimar committed
815
816
817
818
819
820
821
822
823
    }

    /* 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 )
    {
824
        unsigned int i_current = __MAX(b->i_buffer - i_offset,0);
Laurent Aimar's avatar
Laurent Aimar committed
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
        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;
sigmunau's avatar
sigmunau committed
841
}
842

843
static int AStreamSeekBlock( stream_t *s, uint64_t i_pos )
844
{
Laurent Aimar's avatar
Laurent Aimar committed
845
846
847
    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;
848
    bool b_seek;
849

Laurent Aimar's avatar
Laurent Aimar committed
850
    /* We already have thoses data, just update p_current/i_offset */
851
    if( i_offset >= 0 && (uint64_t)i_offset < p_sys->block.i_size )
Laurent Aimar's avatar
Laurent Aimar committed
852
853
854
    {
        block_t *b = p_sys->block.p_first;
        int i_current = 0;
855

856
        while( i_current + b->i_buffer < (uint64_t)i_offset )
Laurent Aimar's avatar
Laurent Aimar committed
857
858
859
860
        {
            i_current += b->i_buffer;
            b = b->p_next;
        }
861

Laurent Aimar's avatar
Laurent Aimar committed
862
863
        p_sys->block.p_current = b;
        p_sys->block.i_offset = i_offset - i_current;
864

Laurent Aimar's avatar
Laurent Aimar committed
865
        p_sys->i_pos = i_pos;
866

Laurent Aimar's avatar
Laurent Aimar committed
867
868
        return VLC_SUCCESS;
    }
869

Laurent Aimar's avatar
Laurent Aimar committed
870
871
    /* We may need to seek or to read data */
    if( i_offset < 0 )
872
    {
873
        bool b_aseek;
874
        access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
Laurent Aimar's avatar
Laurent Aimar committed
875
876
877

        if( !b_aseek )
        {
878
            msg_Err( s, "backward seeking impossible (access not seekable)" );
Laurent Aimar's avatar
Laurent Aimar committed
879
880
881
            return VLC_EGENERIC;
        }

882
        b_seek = true;
883
    }
Laurent Aimar's avatar
Laurent Aimar committed
884
885
    else
    {
886
        bool b_aseek, b_aseekfast;
887

888
889
        access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
        access_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
Laurent Aimar's avatar
Laurent Aimar committed
890
891
892

        if( !b_aseek )
        {
893
            b_seek = false;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
894
            msg_Warn( s, "%"PRId64" bytes need to be skipped "
895
                      "(access non seekable)",
Laurent Aimar's avatar
Laurent Aimar committed
896
897
898
899
900
901
902
903
904
905
906
907
908
                      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 )
909
                b_seek = false;
Laurent Aimar's avatar
Laurent Aimar committed
910
            else
911
                b_seek = true;
Laurent Aimar's avatar
Laurent Aimar committed
912

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
913
            msg_Dbg( s, "b_seek=%d th*avg=%d skip=%"PRId64,
Laurent Aimar's avatar
Laurent Aimar committed
914
915
                     b_seek, i_th*i_avg, i_skip );
        }
916
917
    }

Laurent Aimar's avatar
Laurent Aimar committed
918
919
920
921
922
    if( b_seek )
    {
        int64_t i_start, i_end;
        /* Do the access seek */
        i_start = mdate();
923
        if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
924
        i_end = mdate();
925

Laurent Aimar's avatar
Laurent Aimar committed
926
927
928
929
930
931
932
933
934
935
        /* 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;
936

Laurent Aimar's avatar
Laurent Aimar committed
937
938
939
        /* Refill a block */
        if( AStreamRefillBlock( s ) )
            return VLC_EGENERIC;
940

Laurent Aimar's avatar
Laurent Aimar committed
941
942
943
944
945
946
        /* Update stat */
        p_sys->stat.i_seek_time += i_end - i_start;
        p_sys->stat.i_seek_count++;
        return VLC_SUCCESS;
    }
    else