1 /*****************************************************************************
2 * avio.c: access using libavformat library
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_access.h>
33 #include <vlc_avcodec.h>
36 #include "../codec/avcodec/avcommon.h"
38 #if LIBAVFORMAT_VERSION_MAJOR < 54
39 # define AVIOContext URLContext
41 # define avio_open url_open
42 # define avio_close url_close
43 # define avio_read url_read
44 # define avio_seek url_seek
45 # define avio_pause av_url_read_pause
47 # define AVIO_FLAG_READ URL_RDONLY
48 # define AVIO_FLAG_WRITE URL_WRONLY
49 # define avio_size url_filesize
52 /*****************************************************************************
54 *****************************************************************************/
61 /*****************************************************************************
63 *****************************************************************************/
64 static ssize_t Read (access_t *, uint8_t *, size_t);
65 static int Seek (access_t *, uint64_t);
66 static int Control(access_t *, int, va_list);
67 static ssize_t Write(sout_access_out_t *, block_t *);
68 static int OutControl(sout_access_out_t *, int, va_list);
69 static int OutSeek (sout_access_out_t *, off_t);
71 static int UrlInterruptCallback(void *access)
73 return !vlc_object_alive((vlc_object_t*)access);
82 struct sout_access_out_sys_t {
89 #if LIBAVFORMAT_VERSION_MAJOR < 54
90 static vlc_object_t *current_access = NULL;
92 static int UrlInterruptCallbackSingle(void)
94 return UrlInterruptCallback(current_access);
97 static int SetupAvioCb(vlc_object_t *access)
99 static vlc_mutex_t avio_lock = VLC_STATIC_MUTEX;
100 vlc_mutex_lock(&avio_lock);
101 assert(!access != !current_access);
102 if (access && current_access) {
103 vlc_mutex_unlock(&avio_lock);
106 url_set_interrupt_cb(access ? UrlInterruptCallbackSingle : NULL);
108 current_access = access;
110 vlc_mutex_unlock(&avio_lock);
118 int OpenAvio(vlc_object_t *object)
120 access_t *access = (access_t*)object;
121 access_sys_t *sys = malloc(sizeof(*sys));
128 * - url (only a subset of available protocols).
131 if (!strcmp(access->psz_access, "avio"))
132 url = strdup(access->psz_location);
133 else if (asprintf(&url, "%s://%s", access->psz_access,
134 access->psz_location) < 0)
146 #if LIBAVFORMAT_VERSION_MAJOR < 54
147 ret = avio_open(&sys->context, url, AVIO_FLAG_READ);
149 AVIOInterruptCB cb = {
150 .callback = UrlInterruptCallback,
153 AVDictionary *options = NULL;
154 char *psz_opts = var_InheritString(access, "avio-options");
155 if (psz_opts && *psz_opts) {
156 options = vlc_av_get_options(psz_opts);
159 ret = avio_open2(&sys->context, url, AVIO_FLAG_READ, &cb, &options);
160 AVDictionaryEntry *t = NULL;
161 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
162 msg_Err( access, "unknown option \"%s\"", t->key );
163 av_dict_free(&options);
166 errno = AVUNERROR(ret);
167 msg_Err(access, "Failed to open %s: %m", url);
173 #if LIBAVFORMAT_VERSION_MAJOR < 54
174 /* We can accept only one active user at any time */
175 if (SetupAvioCb(VLC_OBJECT(access))) {
176 msg_Err(access, "Module aready in use");
177 avio_close(sys->context);
182 int64_t size = avio_size(sys->context);
184 #if LIBAVFORMAT_VERSION_MAJOR < 54
185 seekable = !sys->context->is_streamed;
187 seekable = sys->context->seekable;
189 msg_Dbg(access, "%sseekable, size=%"PRIi64, seekable ? "" : "not ", size);
190 sys->size = size > 0 ? size : 0;
193 access_InitFields(access);
195 access->pf_read = Read;
196 access->pf_block = NULL;
197 access->pf_control = Control;
198 access->pf_seek = Seek;
210 static const char *const ppsz_sout_options[] = {
215 int OutOpenAvio(vlc_object_t *object)
217 sout_access_out_t *access = (sout_access_out_t*)object;
219 config_ChainParse( access, "sout-avio-", ppsz_sout_options, access->p_cfg );
221 sout_access_out_sys_t *sys = malloc(sizeof(*sys));
229 if (!access->psz_path)
233 #if LIBAVFORMAT_VERSION_MAJOR < 54
234 ret = avio_open(&sys->context, access->psz_path, AVIO_FLAG_WRITE);
236 AVIOInterruptCB cb = {
237 .callback = UrlInterruptCallback,
240 AVDictionary *options = NULL;
241 char *psz_opts = var_InheritString(access, "sout-avio-options");
242 if (psz_opts && *psz_opts) {
243 options = vlc_av_get_options(psz_opts);
246 ret = avio_open2(&sys->context, access->psz_path, AVIO_FLAG_WRITE,
248 AVDictionaryEntry *t = NULL;
249 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
250 msg_Err( access, "unknown option \"%s\"", t->key );
251 av_dict_free(&options);
254 errno = AVUNERROR(ret);
255 msg_Err(access, "Failed to open %s", access->psz_path);
259 #if LIBAVFORMAT_VERSION_MAJOR < 54
260 /* We can accept only one active user at any time */
261 if (SetupAvioCb(VLC_OBJECT(access))) {
262 msg_Err(access, "Module aready in use");
267 access->pf_write = Write;
268 access->pf_control = OutControl;
269 access->pf_seek = OutSeek;
279 void CloseAvio(vlc_object_t *object)
281 access_t *access = (access_t*)object;
282 access_sys_t *sys = access->p_sys;
284 #if LIBAVFORMAT_VERSION_MAJOR < 54
288 avio_close(sys->context);
292 void OutCloseAvio(vlc_object_t *object)
294 sout_access_out_t *access = (sout_access_out_t*)object;
295 sout_access_out_sys_t *sys = access->p_sys;
297 #if LIBAVFORMAT_VERSION_MAJOR < 54
301 avio_close(sys->context);
305 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
307 int r = avio_read(access->p_sys->context, data, size);
309 access->info.i_pos += r;
311 access->info.b_eof = true;
315 /*****************************************************************************
317 *****************************************************************************/
318 static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
320 access_sys_t *p_sys = (access_sys_t*)p_access->p_sys;
323 while (p_buffer != NULL) {
324 block_t *p_next = p_buffer->p_next;
326 #if LIBAVFORMAT_VERSION_MAJOR < 54
327 int written = url_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
329 errno = AVUNERROR(written);
334 avio_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
335 avio_flush(p_sys->context);
336 if (p_sys->context->error) {
337 errno = AVUNERROR(p_sys->context->error);
338 p_sys->context->error = 0; /* FIXME? */
341 i_write += p_buffer->i_buffer;
344 block_Release(p_buffer);
352 msg_Err(p_access, "Wrote only %zu bytes (%m)", i_write);
353 block_ChainRelease( p_buffer );
357 static int Seek(access_t *access, uint64_t position)
359 access_sys_t *sys = access->p_sys;
363 # define EOVERFLOW EFBIG
366 if (position > INT64_MAX)
367 ret = AVERROR(EOVERFLOW);
369 ret = avio_seek(sys->context, position, SEEK_SET);
371 errno = AVUNERROR(ret);
372 msg_Err(access, "Seek to %"PRIu64" failed: %m", position);
373 if (sys->size == 0 || position != sys->size)
376 access->info.i_pos = position;
377 access->info.b_eof = false;
381 static int OutSeek(sout_access_out_t *p_access, off_t i_pos)
383 sout_access_out_sys_t *sys = p_access->p_sys;
385 if (avio_seek(sys->context, i_pos, SEEK_SET) < 0)
390 static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
392 sout_access_out_sys_t *p_sys = p_access->p_sys;
396 case ACCESS_OUT_CONTROLS_PACE: {
397 bool *pb = va_arg(args, bool *);
398 //*pb = strcmp(p_access->psz_access, "stream");
409 static int Control(access_t *access, int query, va_list args)
411 access_sys_t *sys = access->p_sys;
415 case ACCESS_CAN_SEEK:
416 case ACCESS_CAN_FASTSEEK: /* FIXME how to do that ? */
417 b = va_arg(args, bool *);
418 #if LIBAVFORMAT_VERSION_MAJOR < 54
419 *b = !sys->context->is_streamed;
421 *b = sys->context->seekable;
424 case ACCESS_CAN_PAUSE:
425 b = va_arg(args, bool *);
426 #if LIBAVFORMAT_VERSION_MAJOR < 54
427 *b = sys->context->prot->url_read_pause != NULL;
429 *b = sys->context->read_pause != NULL;
432 case ACCESS_CAN_CONTROL_PACE:
433 b = va_arg(args, bool *);
434 *b = true; /* FIXME */
436 case ACCESS_GET_SIZE:
437 *va_arg(args, uint64_t *) = sys->size;
439 case ACCESS_GET_PTS_DELAY: {
440 int64_t *delay = va_arg(args, int64_t *);
441 *delay = DEFAULT_PTS_DELAY; /* FIXME */
444 case ACCESS_SET_PAUSE_STATE: {
445 bool is_paused = va_arg(args, int);
446 if (avio_pause(sys->context, is_paused)< 0)