imem-access.c 4.42 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
/*****************************************************************************
 * imem-access.c: In-memory bit stream input for VLC
 *****************************************************************************
 * Copyright (C) 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>
25
#include <stdint.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39

#include <vlc_common.h>
#include <vlc_access.h>
#include <vlc_plugin.h>

struct access_sys_t
{
    void *opaque;
    ssize_t (*read_cb)(void *, unsigned char *, size_t);
    int (*seek_cb)(void *, uint64_t);
    void (*close_cb)(void *);
    uint64_t size;
};

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
40
static ssize_t Read(stream_t *access, void *buf, size_t len)
41 42 43 44 45 46 47 48 49 50 51 52 53
{
    access_sys_t *sys = access->p_sys;

    ssize_t val = sys->read_cb(sys->opaque, buf, len);

    if (val < 0) {
        msg_Err(access, "read error");
        val = 0;
    }

    return val;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
54
static int Seek(stream_t *access, uint64_t offset)
55 56 57 58 59 60 61 62 63 64
{
    access_sys_t *sys = access->p_sys;

    assert(sys->seek_cb != NULL);

    if (sys->seek_cb(sys->opaque, offset) != 0)
        return VLC_EGENERIC;
   return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
65
static int Control(stream_t *access, int query, va_list args)
66 67 68 69 70
{
    access_sys_t *sys = access->p_sys;

    switch (query)
    {
71
        case STREAM_CAN_SEEK:
72 73 74
            *va_arg(args, bool *) = sys->seek_cb != NULL;
            break;

75
        case STREAM_CAN_FASTSEEK:
76 77 78
            *va_arg(args, bool *) = false;
            break;

79 80
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
81 82 83
            *va_arg(args, bool *) = sys->seek_cb != NULL;
            break;

84
        case STREAM_GET_SIZE:
85 86
            if (sys->size == UINT64_MAX)
                return VLC_EGENERIC;
87 88 89
            *va_arg(args, uint64_t *) = sys->size;
            break;

90
        case STREAM_GET_PTS_DELAY:
91 92 93
            *va_arg(args, int64_t *) = DEFAULT_PTS_DELAY;
            break;

94
        case STREAM_SET_PAUSE_STATE:
95 96 97 98 99 100 101 102 103
            break;

        default:
            return VLC_EGENERIC;
    }
    (void) access;
    return VLC_SUCCESS;
}

104
static int open_cb_default(void *opaque, void **datap, uint64_t *sizep)
105 106 107 108 109 110 111 112
{
    *datap = opaque;
    (void) sizep;
    return 0;
}

static int Open(vlc_object_t *object)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
113
    stream_t *access = (stream_t *)object;
114

115
    access_sys_t *sys = vlc_obj_malloc(object, sizeof (*sys));
116 117 118 119 120 121 122 123 124 125 126 127
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

    int (*open_cb)(void *, void **, uint64_t *);
    void *opaque;

    opaque = var_InheritAddress(access, "imem-data");
    open_cb = var_InheritAddress(access, "imem-open");
    sys->opaque = NULL;
    sys->read_cb = var_InheritAddress(access, "imem-read");
    sys->seek_cb = var_InheritAddress(access, "imem-seek");
    sys->close_cb = var_InheritAddress(access, "imem-close");
128
    sys->size = UINT64_MAX;
129 130 131 132

    if (open_cb == NULL)
        open_cb = open_cb_default;
    if (sys->read_cb == NULL)
133
        return VLC_EGENERIC;
134 135 136

    if (open_cb(opaque, &sys->opaque, &sys->size)) {
        msg_Err(access, "open error");
137
        return VLC_EGENERIC;
138 139 140 141 142 143 144 145 146 147 148 149 150
    }

    access->pf_read = Read;
    access->pf_block = NULL;
    access->pf_seek = (sys->seek_cb != NULL) ? Seek : NULL;
    access->pf_control = Control;

    access->p_sys = sys;
    return VLC_SUCCESS;
}

static void Close(vlc_object_t *object)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
151
    stream_t *access = (stream_t *)object;
152 153 154 155 156 157 158
    access_sys_t *sys = access->p_sys;

    if (sys->close_cb != NULL)
        sys->close_cb(sys->opaque);
}

vlc_module_begin()
159
    set_shortname(N_("Memory stream"))
160 161 162 163 164 165 166 167
    set_description(N_("In-memory stream input"))
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_ACCESS)

    add_shortcut("imem")
    set_capability("access", 0)
    set_callbacks(Open, Close)
vlc_module_end()