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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, 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);
80 struct sout_access_out_sys_t {
87 #if LIBAVFORMAT_VERSION_MAJOR < 54
88 static vlc_object_t *current_access = NULL;
90 static int UrlInterruptCallbackSingle(void)
92 return UrlInterruptCallback(current_access);
95 static int SetupAvioCb(vlc_object_t *access)
97 static vlc_mutex_t avio_lock = VLC_STATIC_MUTEX;
98 vlc_mutex_lock(&avio_lock);
99 assert(!access != !current_access);
100 if (access && current_access) {
101 vlc_mutex_unlock(&avio_lock);
104 url_set_interrupt_cb(access ? UrlInterruptCallbackSingle : NULL);
106 current_access = access;
108 vlc_mutex_unlock(&avio_lock);
116 int OpenAvio(vlc_object_t *object)
118 access_t *access = (access_t*)object;
119 access_sys_t *sys = malloc(sizeof(*sys));
126 * - url (only a subset of available protocols).
129 if (!strcmp(access->psz_access, "avio"))
130 url = strdup(access->psz_location);
131 else if (asprintf(&url, "%s://%s", access->psz_access,
132 access->psz_location) < 0)
144 #if LIBAVFORMAT_VERSION_MAJOR < 54
145 ret = avio_open(&sys->context, url, AVIO_FLAG_READ);
147 AVIOInterruptCB cb = {
148 .callback = UrlInterruptCallback,
151 ret = avio_open2(&sys->context, url, AVIO_FLAG_READ, &cb, NULL /* options */);
154 errno = AVUNERROR(ret);
155 msg_Err(access, "Failed to open %s: %m", url);
161 #if LIBAVFORMAT_VERSION_MAJOR < 54
162 /* We can accept only one active user at any time */
163 if (SetupAvioCb(VLC_OBJECT(access))) {
164 msg_Err(access, "Module aready in use");
165 avio_close(sys->context);
170 int64_t size = avio_size(sys->context);
172 #if LIBAVFORMAT_VERSION_MAJOR < 54
173 seekable = !sys->context->is_streamed;
175 seekable = sys->context->seekable;
177 msg_Dbg(access, "%sseekable, size=%"PRIi64, seekable ? "" : "not ", size);
180 access_InitFields(access);
181 access->info.i_size = size > 0 ? size : 0;
183 access->pf_read = Read;
184 access->pf_block = NULL;
185 access->pf_control = Control;
186 access->pf_seek = Seek;
197 int OutOpenAvio(vlc_object_t *object)
199 sout_access_out_t *access = (sout_access_out_t*)object;
200 sout_access_out_sys_t *sys = malloc(sizeof(*sys));
208 if (!access->psz_path)
212 #if LIBAVFORMAT_VERSION_MAJOR < 54
213 ret = avio_open(&sys->context, access->psz_path, AVIO_FLAG_WRITE);
215 AVIOInterruptCB cb = {
216 .callback = UrlInterruptCallback,
219 ret = avio_open2(&sys->context, access->psz_path, AVIO_FLAG_WRITE,
220 &cb, NULL /* options */);
223 errno = AVUNERROR(ret);
224 msg_Err(access, "Failed to open %s", access->psz_path);
228 #if LIBAVFORMAT_VERSION_MAJOR < 54
229 /* We can accept only one active user at any time */
230 if (SetupAvioCb(VLC_OBJECT(access))) {
231 msg_Err(access, "Module aready in use");
236 access->pf_write = Write;
237 access->pf_control = OutControl;
238 access->pf_seek = OutSeek;
248 void CloseAvio(vlc_object_t *object)
250 access_t *access = (access_t*)object;
251 access_sys_t *sys = access->p_sys;
253 #if LIBAVFORMAT_VERSION_MAJOR < 54
257 avio_close(sys->context);
261 void OutCloseAvio(vlc_object_t *object)
263 sout_access_out_t *access = (sout_access_out_t*)object;
264 sout_access_out_sys_t *sys = access->p_sys;
266 #if LIBAVFORMAT_VERSION_MAJOR < 54
270 avio_close(sys->context);
274 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
276 int r = avio_read(access->p_sys->context, data, size);
278 access->info.i_pos += r;
280 access->info.b_eof = true;
284 /*****************************************************************************
286 *****************************************************************************/
287 static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
289 access_sys_t *p_sys = (access_sys_t*)p_access->p_sys;
292 while (p_buffer != NULL) {
293 block_t *p_next = p_buffer->p_next;
295 #if LIBAVFORMAT_VERSION_MAJOR < 54
296 int written = url_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
298 errno = AVUNERROR(written);
303 avio_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
304 avio_flush(p_sys->context);
305 if (p_sys->context->error) {
306 errno = AVUNERROR(p_sys->context->error);
307 p_sys->context->error = 0; /* FIXME? */
310 i_write += p_buffer->i_buffer;
313 block_Release(p_buffer);
321 msg_Err(p_access, "Wrote only %zu bytes (%m)", i_write);
322 block_ChainRelease( p_buffer );
326 static int Seek(access_t *access, uint64_t position)
328 access_sys_t *sys = access->p_sys;
332 # define EOVERFLOW EFBIG
335 if (position > INT64_MAX)
336 ret = AVERROR(EOVERFLOW);
338 ret = avio_seek(sys->context, position, SEEK_SET);
340 errno = AVUNERROR(ret);
341 msg_Err(access, "Seek to %"PRIu64" failed: %m", position);
342 if (access->info.i_size <= 0 || position != access->info.i_size)
345 access->info.i_pos = position;
346 access->info.b_eof = false;
350 static int OutSeek(sout_access_out_t *p_access, off_t i_pos)
352 sout_access_out_sys_t *sys = p_access->p_sys;
354 if (avio_seek(sys->context, i_pos, SEEK_SET) < 0)
359 static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
361 sout_access_out_sys_t *p_sys = p_access->p_sys;
365 case ACCESS_OUT_CONTROLS_PACE: {
366 bool *pb = va_arg(args, bool *);
367 //*pb = strcmp(p_access->psz_access, "stream");
378 static int Control(access_t *access, int query, va_list args)
380 access_sys_t *sys = access->p_sys;
384 case ACCESS_CAN_SEEK:
385 case ACCESS_CAN_FASTSEEK: /* FIXME how to do that ? */
386 b = va_arg(args, bool *);
387 #if LIBAVFORMAT_VERSION_MAJOR < 54
388 *b = !sys->context->is_streamed;
390 *b = sys->context->seekable;
393 case ACCESS_CAN_PAUSE:
394 b = va_arg(args, bool *);
395 #if LIBAVFORMAT_VERSION_MAJOR < 54
396 *b = sys->context->prot->url_read_pause != NULL;
398 *b = sys->context->read_pause != NULL;
401 case ACCESS_CAN_CONTROL_PACE:
402 b = va_arg(args, bool *);
403 *b = true; /* FIXME */
405 case ACCESS_GET_PTS_DELAY: {
406 int64_t *delay = va_arg(args, int64_t *);
407 *delay = DEFAULT_PTS_DELAY; /* FIXME */
410 case ACCESS_SET_PAUSE_STATE: {
411 bool is_paused = va_arg(args, int);
412 if (avio_pause(sys->context, is_paused)< 0)
416 case ACCESS_GET_TITLE_INFO:
417 case ACCESS_GET_META:
418 case ACCESS_GET_CONTENT_TYPE:
419 case ACCESS_GET_SIGNAL:
420 case ACCESS_SET_TITLE:
421 case ACCESS_SET_SEEKPOINT:
422 case ACCESS_SET_PRIVATE_ID_STATE:
423 case ACCESS_SET_PRIVATE_ID_CA:
424 case ACCESS_GET_PRIVATE_ID_STATE: