playlist demuxer: remove tabs
[vlc.git] / modules / access / imem-access.c
blob3e902822ac8326ca3c468c6bf04597d087fe1aca
1 /*****************************************************************************
2 * imem-access.c: In-memory bit stream input for VLC
3 *****************************************************************************
4 * Copyright (C) 2015 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 #include <assert.h>
25 #include <stdint.h>
27 #include <vlc_common.h>
28 #include <vlc_access.h>
29 #include <vlc_plugin.h>
31 typedef struct
33 void *opaque;
34 ssize_t (*read_cb)(void *, unsigned char *, size_t);
35 int (*seek_cb)(void *, uint64_t);
36 void (*close_cb)(void *);
37 uint64_t size;
38 } access_sys_t;
40 static ssize_t Read(stream_t *access, void *buf, size_t len)
42 access_sys_t *sys = access->p_sys;
44 ssize_t val = sys->read_cb(sys->opaque, buf, len);
46 if (val < 0) {
47 msg_Err(access, "read error");
48 val = 0;
51 return val;
54 static int Seek(stream_t *access, uint64_t offset)
56 access_sys_t *sys = access->p_sys;
58 assert(sys->seek_cb != NULL);
60 if (sys->seek_cb(sys->opaque, offset) != 0)
61 return VLC_EGENERIC;
62 return VLC_SUCCESS;
65 static int Control(stream_t *access, int query, va_list args)
67 access_sys_t *sys = access->p_sys;
69 switch (query)
71 case STREAM_CAN_SEEK:
72 *va_arg(args, bool *) = sys->seek_cb != NULL;
73 break;
75 case STREAM_CAN_FASTSEEK:
76 *va_arg(args, bool *) = false;
77 break;
79 case STREAM_CAN_PAUSE:
80 case STREAM_CAN_CONTROL_PACE:
81 *va_arg(args, bool *) = sys->seek_cb != NULL;
82 break;
84 case STREAM_GET_SIZE:
85 if (sys->size == UINT64_MAX)
86 return VLC_EGENERIC;
87 *va_arg(args, uint64_t *) = sys->size;
88 break;
90 case STREAM_GET_PTS_DELAY:
91 *va_arg(args, vlc_tick_t *) = DEFAULT_PTS_DELAY;
92 break;
94 case STREAM_SET_PAUSE_STATE:
95 break;
97 default:
98 return VLC_EGENERIC;
100 (void) access;
101 return VLC_SUCCESS;
104 static int open_cb_default(void *opaque, void **datap, uint64_t *sizep)
106 *datap = opaque;
107 (void) sizep;
108 return 0;
111 static int Open(vlc_object_t *object)
113 stream_t *access = (stream_t *)object;
115 access_sys_t *sys = vlc_obj_malloc(object, sizeof (*sys));
116 if (unlikely(sys == NULL))
117 return VLC_ENOMEM;
119 int (*open_cb)(void *, void **, uint64_t *);
120 void *opaque;
122 opaque = var_InheritAddress(access, "imem-data");
123 open_cb = var_InheritAddress(access, "imem-open");
124 sys->opaque = NULL;
125 sys->read_cb = var_InheritAddress(access, "imem-read");
126 sys->seek_cb = var_InheritAddress(access, "imem-seek");
127 sys->close_cb = var_InheritAddress(access, "imem-close");
128 sys->size = UINT64_MAX;
130 if (open_cb == NULL)
131 open_cb = open_cb_default;
132 if (sys->read_cb == NULL)
133 return VLC_EGENERIC;
135 if (open_cb(opaque, &sys->opaque, &sys->size)) {
136 msg_Err(access, "open error");
137 return VLC_EGENERIC;
140 access->pf_read = Read;
141 access->pf_block = NULL;
142 access->pf_seek = (sys->seek_cb != NULL) ? Seek : NULL;
143 access->pf_control = Control;
145 access->p_sys = sys;
146 return VLC_SUCCESS;
149 static void Close(vlc_object_t *object)
151 stream_t *access = (stream_t *)object;
152 access_sys_t *sys = access->p_sys;
154 if (sys->close_cb != NULL)
155 sys->close_cb(sys->opaque);
158 vlc_module_begin()
159 set_shortname(N_("Memory stream"))
160 set_description(N_("In-memory stream input"))
161 set_category(CAT_INPUT)
162 set_subcategory(SUBCAT_INPUT_ACCESS)
164 add_shortcut("imem")
165 set_capability("access", 0)
166 set_callbacks(Open, Close)
167 vlc_module_end()