cache.c 3.46 KB
Newer Older
Michael Niedermayer's avatar
Michael Niedermayer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Input cache protocol.
 * Copyright (c) 2011 Michael Niedermayer
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg 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.
 *
 * FFmpeg 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 FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Based on file.c by Fabrice Bellard
 */

24 25 26 27 28 29 30
/**
 * @TODO
 *      support non continuous caching
 *      support keeping files
 *      support filling with a background thread
 */

Michael Niedermayer's avatar
Michael Niedermayer committed
31 32 33 34 35
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/file.h"
#include "avformat.h"
#include <fcntl.h>
36
#if HAVE_IO_H
Michael Niedermayer's avatar
Michael Niedermayer committed
37 38
#include <io.h>
#endif
39
#if HAVE_UNISTD_H
Michael Niedermayer's avatar
Michael Niedermayer committed
40
#include <unistd.h>
41
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <sys/stat.h>
#include <stdlib.h>
#include "os_support.h"
#include "url.h"

typedef struct Context {
    int fd;
    int64_t end;
    int64_t pos;
    URLContext *inner;
} Context;

static int cache_open(URLContext *h, const char *arg, int flags)
{
56
    char *buffername;
57
    Context *c= h->priv_data;
Michael Niedermayer's avatar
Michael Niedermayer committed
58 59 60

    av_strstart(arg, "cache:", &arg);

61
    c->fd = av_tempfile("ffcache", &buffername, 0, h);
Michael Niedermayer's avatar
Michael Niedermayer committed
62 63 64 65 66 67
    if (c->fd < 0){
        av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
        return c->fd;
    }

    unlink(buffername);
68
    av_freep(&buffername);
Michael Niedermayer's avatar
Michael Niedermayer committed
69

70
    return ffurl_open(&c->inner, arg, flags, &h->interrupt_callback, NULL);
Michael Niedermayer's avatar
Michael Niedermayer committed
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
}

static int cache_read(URLContext *h, unsigned char *buf, int size)
{
    Context *c= h->priv_data;
    int r;

    if(c->pos<c->end){
        r = read(c->fd, buf, FFMIN(size, c->end - c->pos));
        if(r>0)
            c->pos += r;
        return (-1 == r)?AVERROR(errno):r;
    }else{
        r = ffurl_read(c->inner, buf, size);
        if(r > 0){
            int r2= write(c->fd, buf, r);
            av_assert0(r2==r); // FIXME handle cache failure
            c->pos += r;
            c->end += r;
        }
        return r;
    }
}

static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
{
    Context *c= h->priv_data;

    if (whence == AVSEEK_SIZE) {
100 101 102 103 104 105 106 107
        pos= ffurl_seek(c->inner, pos, whence);
        if(pos <= 0){
            pos= ffurl_seek(c->inner, -1, SEEK_END);
            ffurl_seek(c->inner, c->end, SEEK_SET);
            if(pos <= 0)
                return c->end;
        }
        return pos;
Michael Niedermayer's avatar
Michael Niedermayer committed
108 109 110 111 112 113 114 115 116
    }

    pos= lseek(c->fd, pos, whence);
    if(pos<0){
        return pos;
    }else if(pos <= c->end){
        c->pos= pos;
        return pos;
    }else{
117 118 119
        if(lseek(c->fd, c->pos, SEEK_SET) < 0) {
            av_log(h, AV_LOG_ERROR, "Failure to seek in cache\n");
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        return AVERROR(EPIPE);
    }
}

static int cache_close(URLContext *h)
{
    Context *c= h->priv_data;
    close(c->fd);
    ffurl_close(c->inner);

    return 0;
}

URLProtocol ff_cache_protocol = {
    .name                = "cache",
    .url_open            = cache_open,
    .url_read            = cache_read,
    .url_seek            = cache_seek,
    .url_close           = cache_close,
139
    .priv_data_size      = sizeof(Context),
Michael Niedermayer's avatar
Michael Niedermayer committed
140
};