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 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_access.h>
29 #include <vlc_plugin.h>
34 ssize_t (*read_cb
)(void *, unsigned char *, size_t);
35 int (*seek_cb
)(void *, uint64_t);
36 void (*close_cb
)(void *);
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
);
47 msg_Err(access
, "read error");
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)
65 static int Control(stream_t
*access
, int query
, va_list args
)
67 access_sys_t
*sys
= access
->p_sys
;
72 *va_arg(args
, bool *) = sys
->seek_cb
!= NULL
;
75 case STREAM_CAN_FASTSEEK
:
76 *va_arg(args
, bool *) = false;
79 case STREAM_CAN_PAUSE
:
80 case STREAM_CAN_CONTROL_PACE
:
81 *va_arg(args
, bool *) = sys
->seek_cb
!= NULL
;
85 if (sys
->size
== UINT64_MAX
)
87 *va_arg(args
, uint64_t *) = sys
->size
;
90 case STREAM_GET_PTS_DELAY
:
91 *va_arg(args
, vlc_tick_t
*) = DEFAULT_PTS_DELAY
;
94 case STREAM_SET_PAUSE_STATE
:
104 static int open_cb_default(void *opaque
, void **datap
, uint64_t *sizep
)
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
))
119 int (*open_cb
)(void *, void **, uint64_t *);
122 opaque
= var_InheritAddress(access
, "imem-data");
123 open_cb
= var_InheritAddress(access
, "imem-open");
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
;
131 open_cb
= open_cb_default
;
132 if (sys
->read_cb
== NULL
)
135 if (open_cb(opaque
, &sys
->opaque
, &sys
->size
)) {
136 msg_Err(access
, "open error");
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
;
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
);
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
)
165 set_capability("access", 0)
166 set_callbacks(Open
, Close
)