contrib: cargo: use the 0.6.13 cargo-c version
[vlc.git] / modules / access / avio.c
blob8302ca7de9b95ca40342fdcb91327a05bf952e76
1 /*****************************************************************************
2 * avio.c: access using libavformat library
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
6 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 #include <assert.h>
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_access.h>
31 #include <vlc_sout.h>
32 #include <vlc_avcodec.h>
33 #include <vlc_interrupt.h>
35 #include "avio.h"
36 #include "../codec/avcodec/avcommon.h"
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 #ifndef MERGE_FFMPEG
42 vlc_module_begin()
43 AVIO_MODULE
44 vlc_module_end()
45 #endif
47 /*****************************************************************************
48 * Local prototypes
49 *****************************************************************************/
50 static ssize_t Read (stream_t *, void *, size_t);
51 static int Seek (stream_t *, uint64_t);
52 static int Control(stream_t *, int, va_list);
53 static ssize_t Write(sout_access_out_t *, block_t *);
54 static int OutControl(sout_access_out_t *, int, va_list);
55 static int OutSeek (sout_access_out_t *, off_t);
57 static int UrlInterruptCallback(void *access)
59 /* NOTE: This works so long as libavformat invokes the callback from the
60 * same thread that invokes libavformat. Currently libavformat does not
61 * create internal threads at all. This is not proper event handling in any
62 * case; libavformat needs fixing. */
63 (void) access;
64 return vlc_killed();
67 typedef struct
69 AVIOContext *context;
70 int64_t size;
71 } access_sys_t;
73 typedef struct
75 AVIOContext *context;
76 } sout_access_out_sys_t;
79 /* */
81 /* */
83 int OpenAvio(vlc_object_t *object)
85 stream_t *access = (stream_t*)object;
86 access_sys_t *sys = vlc_obj_malloc(object, sizeof(*sys));
87 if (!sys)
88 return VLC_ENOMEM;
89 sys->context = NULL;
91 /* We accept:
92 * - avio://full_url
93 * - url (only a subset of available protocols).
95 char *url;
96 if (!strcmp(access->psz_name, "avio"))
97 url = strdup(access->psz_location);
98 else if (asprintf(&url, "%s://%s", access->psz_name,
99 access->psz_location) < 0)
100 url = NULL;
102 if (!url)
103 return VLC_ENOMEM;
105 /* */
106 vlc_init_avformat(object);
108 int ret;
109 AVIOInterruptCB cb = {
110 .callback = UrlInterruptCallback,
111 .opaque = access,
113 AVDictionary *options = NULL;
114 char *psz_opts = var_InheritString(access, "avio-options");
115 if (psz_opts) {
116 vlc_av_get_options(psz_opts, &options);
117 free(psz_opts);
119 ret = avio_open2(&sys->context, url, AVIO_FLAG_READ, &cb, &options);
120 AVDictionaryEntry *t = NULL;
121 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
122 msg_Err( access, "unknown option \"%s\"", t->key );
123 av_dict_free(&options);
124 if (ret < 0) {
125 msg_Err(access, "Failed to open %s: %s", url,
126 vlc_strerror_c(AVUNERROR(ret)));
127 free(url);
128 return VLC_EGENERIC;
130 free(url);
132 sys->size = avio_size(sys->context);
134 bool seekable;
135 seekable = sys->context->seekable;
136 msg_Dbg(access, "%sseekable, size=%"PRIi64, seekable ? "" : "not ",
137 sys->size);
139 /* */
140 access->pf_read = Read;
141 access->pf_block = NULL;
142 access->pf_control = Control;
143 access->pf_seek = Seek;
144 access->p_sys = sys;
146 return VLC_SUCCESS;
149 /* */
151 static const char *const ppsz_sout_options[] = {
152 "options",
153 NULL,
156 int OutOpenAvio(vlc_object_t *object)
158 sout_access_out_t *access = (sout_access_out_t*)object;
160 config_ChainParse( access, "sout-avio-", ppsz_sout_options, access->p_cfg );
162 sout_access_out_sys_t *sys = vlc_obj_malloc(object, sizeof(*sys));
163 if (!sys)
164 return VLC_ENOMEM;
165 sys->context = NULL;
167 /* */
168 vlc_init_avformat(object);
170 if (!access->psz_path)
171 return VLC_EGENERIC;
173 int ret;
174 AVDictionary *options = NULL;
175 char *psz_opts = var_InheritString(access, "sout-avio-options");
176 if (psz_opts) {
177 vlc_av_get_options(psz_opts, &options);
178 free(psz_opts);
180 ret = avio_open2(&sys->context, access->psz_path, AVIO_FLAG_WRITE,
181 NULL, &options);
182 AVDictionaryEntry *t = NULL;
183 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
184 msg_Err( access, "unknown option \"%s\"", t->key );
185 av_dict_free(&options);
186 if (ret < 0) {
187 errno = AVUNERROR(ret);
188 msg_Err(access, "Failed to open %s", access->psz_path);
189 return VLC_EGENERIC;
192 access->pf_write = Write;
193 access->pf_control = OutControl;
194 access->pf_seek = OutSeek;
195 access->p_sys = sys;
197 return VLC_SUCCESS;
200 void CloseAvio(vlc_object_t *object)
202 stream_t *access = (stream_t*)object;
203 access_sys_t *sys = access->p_sys;
205 avio_close(sys->context);
208 void OutCloseAvio(vlc_object_t *object)
210 sout_access_out_t *access = (sout_access_out_t*)object;
211 sout_access_out_sys_t *sys = access->p_sys;
213 avio_close(sys->context);
216 static ssize_t Read(stream_t *access, void *data, size_t size)
218 access_sys_t *sys = access->p_sys;
220 int r = avio_read(sys->context, data, size);
221 if (r < 0)
222 r = 0;
223 return r;
226 /*****************************************************************************
227 * Write:
228 *****************************************************************************/
229 static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
231 sout_access_out_sys_t *p_sys = (sout_access_out_sys_t*)p_access->p_sys;
232 size_t i_write = 0;
233 int val;
235 while (p_buffer != NULL) {
236 block_t *p_next = p_buffer->p_next;
238 avio_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
239 avio_flush(p_sys->context);
240 if ((val = p_sys->context->error) != 0) {
241 p_sys->context->error = 0; /* FIXME? */
242 goto error;
244 i_write += p_buffer->i_buffer;
246 block_Release(p_buffer);
248 p_buffer = p_next;
251 return i_write;
253 error:
254 msg_Err(p_access, "Wrote only %zu bytes: %s", i_write,
255 vlc_strerror_c(AVUNERROR(val)));
256 block_ChainRelease( p_buffer );
257 return i_write;
260 static int Seek(stream_t *access, uint64_t position)
262 access_sys_t *sys = access->p_sys;
263 int ret;
265 #ifndef EOVERFLOW
266 # define EOVERFLOW EFBIG
267 #endif
269 if (position > INT64_MAX)
270 ret = AVERROR(EOVERFLOW);
271 else
272 ret = avio_seek(sys->context, position, SEEK_SET);
273 if (ret < 0) {
274 msg_Err(access, "Seek to %"PRIu64" failed: %s", position,
275 vlc_strerror_c(AVUNERROR(ret)));
276 if (sys->size < 0 || position != (uint64_t)sys->size)
277 return VLC_EGENERIC;
279 return VLC_SUCCESS;
282 static int OutSeek(sout_access_out_t *p_access, off_t i_pos)
284 sout_access_out_sys_t *sys = p_access->p_sys;
286 if (avio_seek(sys->context, i_pos, SEEK_SET) < 0)
287 return VLC_EGENERIC;
288 return VLC_SUCCESS;
291 static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
293 sout_access_out_sys_t *p_sys = p_access->p_sys;
295 VLC_UNUSED(p_sys);
296 switch (i_query) {
297 case ACCESS_OUT_CONTROLS_PACE: {
298 bool *pb = va_arg(args, bool *);
299 //*pb = strcmp(p_access->psz_name, "stream");
300 *pb = false;
301 break;
304 default:
305 return VLC_EGENERIC;
307 return VLC_SUCCESS;
310 static int Control(stream_t *access, int query, va_list args)
312 access_sys_t *sys = access->p_sys;
313 bool *b;
315 switch (query) {
316 case STREAM_CAN_SEEK:
317 case STREAM_CAN_FASTSEEK: /* FIXME how to do that ? */
318 b = va_arg(args, bool *);
319 *b = sys->context->seekable;
320 return VLC_SUCCESS;
321 case STREAM_CAN_PAUSE:
322 b = va_arg(args, bool *);
323 *b = sys->context->read_pause != NULL;
324 return VLC_SUCCESS;
325 case STREAM_CAN_CONTROL_PACE:
326 b = va_arg(args, bool *);
327 *b = true; /* FIXME */
328 return VLC_SUCCESS;
329 case STREAM_GET_SIZE:
330 if (sys->size < 0)
331 return VLC_EGENERIC;
332 *va_arg(args, uint64_t *) = sys->size;
333 return VLC_SUCCESS;
334 case STREAM_GET_PTS_DELAY:
335 *va_arg(args, vlc_tick_t *) =
336 VLC_TICK_FROM_MS(var_InheritInteger(access, "network-caching"));
337 return VLC_SUCCESS;
339 case STREAM_SET_PAUSE_STATE: {
340 bool is_paused = va_arg(args, int);
341 if (avio_pause(sys->context, is_paused)< 0)
342 return VLC_EGENERIC;
343 return VLC_SUCCESS;
345 default:
346 return VLC_EGENERIC;