prefetch.c 20.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*****************************************************************************
 * prefetch.c: prefetchinging module for VLC
 *****************************************************************************
 * Copyright © 2015 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <unistd.h>
31

32
#ifdef HAVE_MMAP
33 34 35 36
# include <sys/mman.h>
# ifndef MAP_ANONYMOUS
#  define MAP_ANONYMOUS MAP_ANON
# endif
37
#elif !defined(__OS2__)
38 39 40 41 42 43 44
# include <errno.h>
# define MAP_FAILED ((void *)-1)
# define mmap(a,l,p,f,d,o) \
     ((void)(a), (void)(l), (void)(d), (void)(o), errno = ENOMEM, MAP_FAILED)
# define munmap(a,l) \
     ((void)(a), (void)(l), errno = EINVAL, -1)
# define sysconf(a) 1
45
#endif
46

47
#if defined (_WIN32)
48
# include <windows.h>
49
#endif
50 51 52 53 54

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_stream.h>
#include <vlc_fs.h>
55
#include <vlc_interrupt.h>
56 57 58 59 60 61 62

struct stream_sys_t
{
    vlc_mutex_t  lock;
    vlc_cond_t   wait_data;
    vlc_cond_t   wait_space;
    vlc_thread_t thread;
63
    vlc_interrupt_t *interrupt;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    bool         eof;
    bool         error;
    bool         paused;

    bool         can_seek;
    bool         can_pace;
    bool         can_pause;
    uint64_t     size;
    int64_t      pts_delay;
    char        *content_type;

    uint64_t     buffer_offset;
    uint64_t     stream_offset;
    size_t       buffer_length;
    size_t       buffer_size;
    char        *buffer;
    size_t       read_size;
    size_t       seek_threshold;
};

static int ThreadRead(stream_t *stream, size_t length)
{
    stream_sys_t *sys = stream->p_sys;
    int canc = vlc_savecancel();

    vlc_mutex_unlock(&sys->lock);
    assert(length > 0);

    char *p = sys->buffer + (sys->buffer_offset % sys->buffer_size)
                          + sys->buffer_length;
    ssize_t val = stream_Read(stream->p_source, p, length);

    if (val < 0)
        msg_Err(stream, "cannot read data (at offset %"PRIu64")",
                sys->buffer_offset + sys->buffer_length);
    if (val == 0)
        msg_Dbg(stream, "end of stream");

    vlc_mutex_lock(&sys->lock);
    vlc_restorecancel(canc);

    if (val < 0)
        return -1;

    if (val == 0)
        sys->eof = true;

    assert((size_t)val <= length);
    sys->buffer_length += val;
    assert(sys->buffer_length <= sys->buffer_size);
115
    //msg_Dbg(stream, "buffer: %zu/%zu", sys->buffer_length, sys->buffer_size);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    return 0;
}

static int ThreadSeek(stream_t *stream, uint64_t seek_offset)
{
    stream_sys_t *sys = stream->p_sys;
    int canc = vlc_savecancel();

    vlc_mutex_unlock(&sys->lock);

    int val = stream_Seek(stream->p_source, seek_offset);
    if (val != VLC_SUCCESS)
        msg_Err(stream, "cannot seek (to offset %"PRIu64")", seek_offset);

    vlc_mutex_lock(&sys->lock);
    vlc_restorecancel(canc);

    if (val != VLC_SUCCESS)
        return -1;

    sys->buffer_offset = seek_offset;
    sys->buffer_length = 0;
    sys->eof = false;
    return 0;
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static int ThreadControl(stream_t *stream, int query, ...)
{
    stream_sys_t *sys = stream->p_sys;
    int canc = vlc_savecancel();

    vlc_mutex_unlock(&sys->lock);

    va_list ap;
    int ret;

    va_start(ap, query);
    ret = stream_vaControl(stream->p_source, query, ap);
    va_end(ap);

    vlc_mutex_lock(&sys->lock);
    vlc_restorecancel(canc);
    return ret;
}

161 162 163 164 165 166 167 168 169
#define MAX_READ 65536
#define SEEK_THRESHOLD MAX_READ

static void *Thread(void *data)
{
    stream_t *stream = data;
    stream_sys_t *sys = stream->p_sys;
    bool paused = false;

170 171
    vlc_interrupt_set(sys->interrupt);

172 173 174 175 176
    vlc_mutex_lock(&sys->lock);
    mutex_cleanup_push(&sys->lock);
    for (;;)
    {
        if (paused)
177 178 179 180 181 182 183 184 185 186 187
        {
            if (sys->paused)
            {   /* Wait for resumption */
                vlc_cond_wait(&sys->wait_space, &sys->lock);
                continue;
            }

            /* Resume the underlying stream */
            msg_Dbg(stream, "resuming");
            ThreadControl(stream, STREAM_SET_PAUSE_STATE, false);
            paused = false;
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 218 219 220 221 222 223 224
            continue;
        }

        if (sys->stream_offset < sys->buffer_offset)
        {   /* Need to seek backward */
            if (ThreadSeek(stream, sys->stream_offset))
                break;
            continue;
        }

        if (sys->eof)
        {   /* At EOF, wait for backward seek */
            vlc_cond_wait(&sys->wait_space, &sys->lock);
            continue;
        }

        assert(sys->stream_offset >= sys->buffer_offset);

        /* As long as there is space, the buffer will retain already read
         * ("historical") data. The data can be used if/when seeking backward.
         * Unread data is however given precedence if the buffer is full. */
        uint64_t history = sys->stream_offset - sys->buffer_offset;

        if (sys->can_seek
         && history >= (sys->buffer_length + sys->seek_threshold))
        {   /* Large skip: seek forward */
            if (ThreadSeek(stream, sys->stream_offset))
                break;
            continue;
        }

        assert(sys->buffer_size >= sys->buffer_length);

        size_t unused = sys->buffer_size - sys->buffer_length;
        if (unused == 0)
        {   /* Buffer is full */
            if (history == 0)
225 226 227 228 229 230 231 232 233 234 235
            {
                if (sys->paused)
                {   /* Pause the stream once the buffer is full
                     * (and assuming pause was actually requested) */
                    msg_Dbg(stream, "pausing");
                    ThreadControl(stream, STREAM_SET_PAUSE_STATE, true);
                    paused = true;
                    continue;
                }

                /* Wait for data to be read */
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
                vlc_cond_wait(&sys->wait_space, &sys->lock);
                continue;
            }

            /* Discard some historical data to make room. */
            size_t discard = sys->read_size;
            if (discard > history)
                discard = history;

            /* discard <= sys->read_size <= sys->buffer_size = ...
             * ... unused + sys->buffer_length = 0 + sys->buffer_length */
            assert(discard <= sys->buffer_length);
            sys->buffer_offset += discard;
            sys->buffer_length -= discard;
            history -= discard;
            unused = discard;
        }

        /* Some streams cannot return a short data count and just wait for all
         * requested data to become available (e.g. regular files). So we have
         * to limit the data read in a single operation to avoid blocking for
         * too long. */
        if (unused > sys->read_size)
            unused = sys->read_size;

        if (ThreadRead(stream, unused))
            break;

        vlc_cond_signal(&sys->wait_data);
    }
    vlc_cleanup_pop();

    sys->error = true;
    vlc_cond_signal(&sys->wait_data);
    vlc_mutex_unlock(&sys->lock);
    return NULL;
}

static int Seek(stream_t *stream, uint64_t offset)
{
    stream_sys_t *sys = stream->p_sys;

    vlc_mutex_lock(&sys->lock);
    if (sys->stream_offset != offset)
    {
        sys->stream_offset = offset;
        vlc_cond_signal(&sys->wait_space);
    }
    vlc_mutex_unlock(&sys->lock);
    return 0;
}

static size_t BufferLevel(const stream_t *stream, bool *eof)
{
    stream_sys_t *sys = stream->p_sys;

    *eof = false;

    if (sys->stream_offset < sys->buffer_offset)
        return 0;
    if ((sys->stream_offset - sys->buffer_offset) >= sys->buffer_length)
    {
        *eof = sys->eof;
        return 0;
    }
    return sys->buffer_offset + sys->buffer_length - sys->stream_offset;
}

static ssize_t Read(stream_t *stream, void *buf, size_t buflen)
{
    stream_sys_t *sys = stream->p_sys;
    size_t copy;
    bool eof;

310 311
    if (buflen == 0)
        return buflen;
312 313 314 315 316 317 318
    if (buf == NULL)
    {
        Seek(stream, sys->stream_offset + buflen);
        return buflen;
    }

    vlc_mutex_lock(&sys->lock);
319 320 321 322 323 324 325
    if (sys->paused)
    {
        msg_Err(stream, "reading while paused (buggy demux?)");
        sys->paused = false;
        vlc_cond_signal(&sys->wait_space);
    }

326 327
    while ((copy = BufferLevel(stream, &eof)) == 0 && !eof)
    {
328 329
        void *data[2];

330
        if (sys->error)
331 332 333 334
        {
            vlc_mutex_unlock(&sys->lock);
            return -1;
        }
335

336
        vlc_interrupt_forward_start(sys->interrupt, data);
337
        vlc_cond_wait(&sys->wait_data, &sys->lock);
338
        vlc_interrupt_forward_stop(data);
339 340 341 342 343
    }

    char *p = sys->buffer + (sys->stream_offset % sys->buffer_size);
    if (copy > buflen)
        copy = buflen;
344 345 346
    memcpy(buf, p, copy);
    sys->stream_offset += copy;
    vlc_cond_signal(&sys->wait_space);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    vlc_mutex_unlock(&sys->lock);
    return copy;
}

static input_item_t *ReadDir(stream_t *stream)
{
    (void) stream;
    return NULL;
}

static int Control(stream_t *stream, int query, va_list args)
{
    stream_sys_t *sys = stream->p_sys;

    switch (query)
    {
        case STREAM_CAN_SEEK:
            *va_arg(args, bool *) = sys->can_seek;
            break;
        case STREAM_CAN_FASTSEEK:
            *va_arg(args, bool *) = false;
            break;
        case STREAM_CAN_PAUSE:
             *va_arg(args, bool *) = sys->can_pause;
            break;
        case STREAM_CAN_CONTROL_PACE:
            *va_arg (args, bool *) = sys->can_pace;
            break;
        case STREAM_IS_DIRECTORY:
            return VLC_EGENERIC;
        case STREAM_GET_SIZE:
378 379
            if (sys->size == (uint64_t)-1)
                return VLC_EGENERIC;
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
            *va_arg(args, uint64_t *) = sys->size;
            break;
        case STREAM_GET_PTS_DELAY:
            *va_arg(args, int64_t *) = sys->pts_delay;
            break;
        case STREAM_GET_TITLE_INFO:
        case STREAM_GET_TITLE:
        case STREAM_GET_SEEKPOINT:
        case STREAM_GET_META:
            return VLC_EGENERIC;
        case STREAM_GET_CONTENT_TYPE:
            if (sys->content_type == NULL)
                return VLC_EGENERIC;
            *va_arg(args, char **) = strdup(sys->content_type);
            return VLC_SUCCESS;
        case STREAM_GET_SIGNAL:
            return VLC_EGENERIC;
        case STREAM_SET_PAUSE_STATE:
        {
            bool paused = va_arg(args, unsigned);

            vlc_mutex_lock(&sys->lock);
            sys->paused = paused;
            vlc_cond_signal(&sys->wait_space);
            vlc_mutex_unlock (&sys->lock);
            break;
        }
        case STREAM_SET_TITLE:
        case STREAM_SET_SEEKPOINT:
        case STREAM_SET_PRIVATE_ID_STATE:
        case STREAM_SET_PRIVATE_ID_CA:
        case STREAM_GET_PRIVATE_ID_STATE:
            return VLC_EGENERIC;
        default:
            msg_Err(stream, "unimplemented query (%d) in control", query);
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}

static int Open(vlc_object_t *obj)
{
    stream_t *stream = (stream_t *)obj;

    bool fast_seek;
    /* For local files, the operating system is likely to do a better work at
     * caching/prefetching. Also, prefetching with this module could cause
     * undesirable high load at start-up. Lastly, local files may require
     * support for title/seekpoint and meta control requests. */
    stream_Control(stream->p_source, STREAM_CAN_FASTSEEK, &fast_seek);
    if (fast_seek)
        return VLC_EGENERIC;

    /* PID-filtered streams are not suitable for prefetching, as they would
     * suffer excessive latency to enable a PID. DVB would also require support
     * for the signal level and Conditional Access controls.
     * TODO? For seekable streams, a forced could work around the problem. */
    if (stream_Control(stream->p_source, STREAM_GET_PRIVATE_ID_STATE, 0,
                       &(bool){ false }) == VLC_SUCCESS)
        return VLC_EGENERIC;

    stream_sys_t *sys = malloc(sizeof (*sys));
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

    stream->pf_read = Read;
446
    stream->pf_seek = Seek;
447 448 449 450 451
    stream->pf_control = Control;

    stream_Control(stream->p_source, STREAM_CAN_SEEK, &sys->can_seek);
    stream_Control(stream->p_source, STREAM_CAN_PAUSE, &sys->can_pause);
    stream_Control(stream->p_source, STREAM_CAN_CONTROL_PACE, &sys->can_pace);
452 453
    if (stream_Control(stream->p_source, STREAM_GET_SIZE, &sys->size))
        sys->size = -1;
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    stream_Control(stream->p_source, STREAM_GET_PTS_DELAY, &sys->pts_delay);
    if (stream_Control(stream->p_source, STREAM_GET_CONTENT_TYPE,
                       &sys->content_type))
        sys->content_type = NULL;

    sys->eof = false;
    sys->error = false;
    sys->paused = false;
    sys->buffer_offset = 0;
    sys->stream_offset = 0;
    sys->buffer_length = 0;
    sys->buffer_size = var_InheritInteger(obj, "prefetch-buffer-size") << 10u;
    sys->read_size = var_InheritInteger(obj, "prefetch-read-size");
    sys->seek_threshold = var_InheritInteger(obj, "prefetch-seek-threshold");

    uint64_t size = stream_Size(stream->p_source);
    if (size > 0)
    {   /* No point allocating a buffer larger than the source stream */
        if (sys->buffer_size > size)
            sys->buffer_size = size;
        if (sys->read_size > size)
            sys->read_size = size;
    }
    if (sys->buffer_size < sys->read_size)
        sys->buffer_size = sys->read_size;

480
#if !defined(_WIN32) && !defined(__OS2__)
481 482
    /* Round up to a multiple of the page size */
    long page_size = sysconf(_SC_PAGESIZE);
483

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    sys->buffer_size += page_size - 1;
    sys->buffer_size &= ~(page_size - 1);

    sys->buffer = mmap(NULL, 2 * sys->buffer_size, PROT_NONE,
                       MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    if (sys->buffer == MAP_FAILED)
        goto error;

    int fd = vlc_memfd();
    if (fd == -1)
        goto error;

    if (ftruncate(fd, sys->buffer_size)
     || mmap(sys->buffer, sys->buffer_size,
             PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, 0) == MAP_FAILED
     || mmap(sys->buffer + sys->buffer_size, sys->buffer_size,
             PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, 0) == MAP_FAILED)
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
502
        vlc_close(fd);
503 504
        goto error;
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
505
    vlc_close(fd);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 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 604 605 606 607 608 609
#elif defined(__OS2__)
    /* On OS/2 Warp, page size is 4K, but the smallest chunk size is 64K */
    int page_size = 64 * 1024;

    sys->buffer_size += page_size - 1;
    sys->buffer_size &= ~(page_size - 1);
    sys->buffer = NULL;

    char *buffer;

    if (DosAllocMem(&buffer, 2 * sys->buffer_size, fALLOC))
        goto error;

    struct buffer_list
    {
        char *buffer;
        struct buffer_list *next;
    } *buffer_list_start = NULL;

    for (;;)
    {
        char *buf;
        if (DosAllocMem(&buf, sys->buffer_size, fPERM | OBJ_TILE))
            break;

        struct buffer_list *new_buffer = calloc(1, sizeof(*new_buffer));

        if (!new_buffer)
        {
            DosFreeMem(buf);
            break;
        }

        new_buffer->buffer = buf;
        new_buffer->next = buffer_list_start;

        buffer_list_start = new_buffer;

        if (buf > buffer)
        {
            /* Disable thread switching */
            DosEnterCritSec();

            char *addr = buffer;

            DosFreeMem(buffer);
            buffer = NULL;

            if (DosAllocMem(&buf, sys->buffer_size, fALLOC))
                goto exitcritsec;

            /* Was hole ? */
            if (buf < addr)
            {
                DosFreeMem(buf);
                buf = NULL;

                char *tmp;

                /* Try to fill the hole out */
                if (DosAllocMem(&tmp, addr - buf, fALLOC))
                    goto exitcritsec;

                DosAllocMem(&buf, sys->buffer_size, fALLOC);

                DosFreeMem(tmp);
            }

            if (buf != addr)
            {
                DosFreeMem(buf);
                goto exitcritsec;
            }

            char *alias = NULL;

            if (!DosAliasMem(buf, sys->buffer_size, &alias, 0)
             && alias == buf + sys->buffer_size)
                sys->buffer = addr;
            else
            {
                DosFreeMem(buf);
                DosFreeMem(alias);
            }

exitcritsec:
            /* Enable thread switching */
            DosExitCritSec();
            break;
        }
    }

    DosFreeMem(buffer);

    for (struct buffer_list *l = buffer_list_start, *next; l; l = next)
    {
        next = l->next;

        DosFreeMem(l->buffer);
        free(l);
    }

    if (sys->buffer == NULL)
        goto error;
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
#else
    SYSTEM_INFO info;
    GetSystemInfo(&info);

    sys->buffer_size += info.dwPageSize - 1;
    sys->buffer_size &= ~(info.dwPageSize - 1);
    sys->buffer = NULL;

    HANDLE map = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
                                   0, sys->buffer_size, NULL);
    if (map == NULL)
        goto error;

    for (;;)
    {
        char *buffer = VirtualAlloc(NULL, 2 * sys->buffer_size, MEM_RESERVE,
                                    PAGE_NOACCESS);
        if (buffer == NULL)
            break;

        VirtualFree(buffer, 2 * sys->buffer_size, MEM_RELEASE);

        char *a = MapViewOfFileEx(map, FILE_MAP_ALL_ACCESS, 0, 0,
                                  sys->buffer_size, buffer);
        char *b = MapViewOfFileEx(map, FILE_MAP_ALL_ACCESS, 0, 0,
                                  sys->buffer_size, buffer + sys->buffer_size);

        if (a == buffer && b == buffer + sys->buffer_size)
        {
            sys->buffer = buffer;
            break;
        }
        if (b != NULL)
            UnmapViewOfFile(b);
        if (a != NULL)
            UnmapViewOfFile(a);
        if (a == NULL || b == NULL)
            break; /* ENOMEM */
    }

    CloseHandle(map);
    if (sys->buffer == NULL)
        goto error;
#endif /* _WIN32 */
654

655 656 657 658
    sys->interrupt = vlc_interrupt_create();
    if (unlikely(sys->interrupt == NULL))
        goto error;

659 660 661 662 663 664 665 666 667 668 669
    vlc_mutex_init(&sys->lock);
    vlc_cond_init(&sys->wait_data);
    vlc_cond_init(&sys->wait_space);

    stream->p_sys = sys;

    if (vlc_clone(&sys->thread, Thread, stream, VLC_THREAD_PRIORITY_LOW))
    {
        vlc_cond_destroy(&sys->wait_space);
        vlc_cond_destroy(&sys->wait_data);
        vlc_mutex_destroy(&sys->lock);
670
        vlc_interrupt_destroy(sys->interrupt);
671 672 673 674 675 676 677 678 679 680 681
        goto error;
    }

    msg_Dbg(stream, "using %zu bytes buffer, %zu bytes read",
            sys->buffer_size, sys->read_size);
    stream->pf_read = Read;
    stream->pf_readdir = ReadDir;
    stream->pf_control = Control;
    return VLC_SUCCESS;

error:
682
#if !defined(_WIN32) && !defined(__OS2__)
683 684
    if (sys->buffer != MAP_FAILED)
        munmap(sys->buffer, 2 * sys->buffer_size);
685 686 687 688 689 690
#elif defined(__OS2__)
    if (sys->buffer != NULL)
    {
        DosFreeMem(sys->buffer + sys->buffer_size);
        DosFreeMem(sys->buffer);
    }
691 692 693 694 695 696 697
#else
    if (sys->buffer != NULL)
    {
        UnmapViewOfFile(sys->buffer + sys->buffer_size);
        UnmapViewOfFile(sys->buffer);
    }
#endif
698 699 700 701 702 703 704 705 706 707 708 709 710 711
    free(sys);
    return VLC_ENOMEM;
}


/**
 * Releases allocate resources.
 */
static void Close (vlc_object_t *obj)
{
    stream_t *stream = (stream_t *)obj;
    stream_sys_t *sys = stream->p_sys;

    vlc_cancel(sys->thread);
712
    vlc_interrupt_kill(sys->interrupt);
713
    vlc_join(sys->thread, NULL);
714
    vlc_interrupt_destroy(sys->interrupt);
715 716 717 718
    vlc_cond_destroy(&sys->wait_space);
    vlc_cond_destroy(&sys->wait_data);
    vlc_mutex_destroy(&sys->lock);

719
#if !defined(_WIN32) && !defined(__OS2__)
720
    munmap(sys->buffer, 2 * sys->buffer_size);
721 722 723
#elif defined(__OS2__)
    DosFreeMem(sys->buffer + sys->buffer_size);
    DosFreeMem(sys->buffer);
724 725 726 727
#else
    UnmapViewOfFile(sys->buffer + sys->buffer_size);
    UnmapViewOfFile(sys->buffer);
#endif
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    free(sys->content_type);
    free(sys);
}

vlc_module_begin()
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_STREAM_FILTER)
    set_capability("stream_filter", 0)

    set_description(N_("Stream prefetch filter"))
    set_callbacks(Open, Close)

    add_integer("prefetch-buffer-size", 1 << 14, N_("Buffer size"),
                N_("Prefetch buffer size (KiB)"), false)
        change_integer_range(4, 1 << 20)
    add_integer("prefetch-read-size", 1 << 14, N_("Read size"),
                N_("Prefetch background read size (bytes)"), true)
        change_integer_range(1, 1 << 29)
    add_integer("prefetch-seek-threshold", 1 << 14, N_("Seek threshold"),
                N_("Prefetch forward seek threshold (bytes)"), true)
        change_integer_range(0, UINT64_C(1) << 60)
vlc_module_end()