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>
37 #if LIBAVFORMAT_VERSION_MAJOR < 54
38 # define AVIOContext URLContext
40 # define avio_open url_open
41 # define avio_close url_close
42 # define avio_read url_read
43 # define avio_seek url_seek
44 # define avio_pause av_url_read_pause
46 # define AVIO_FLAG_READ URL_RDONLY
47 # define AVIO_FLAG_WRITE URL_WRONLY
48 # define avio_size url_filesize
51 /*****************************************************************************
53 *****************************************************************************/
60 /*****************************************************************************
62 *****************************************************************************/
63 static ssize_t Read (access_t *, uint8_t *, size_t);
64 static int Seek (access_t *, uint64_t);
65 static int Control(access_t *, int, va_list);
66 static ssize_t Write(sout_access_out_t *, block_t *);
67 static int OutControl(sout_access_out_t *, int, va_list);
68 static int OutSeek (sout_access_out_t *, off_t);
70 static int UrlInterruptCallback(void *access)
72 return !vlc_object_alive((vlc_object_t*)access);
79 struct sout_access_out_sys_t {
86 #if LIBAVFORMAT_VERSION_MAJOR < 54
87 static vlc_object_t *current_access = NULL;
89 static int UrlInterruptCallbackSingle(void)
91 return UrlInterruptCallback(current_access);
94 static int SetupAvioCb(vlc_object_t *access)
96 static vlc_mutex_t avio_lock = VLC_STATIC_MUTEX;
97 vlc_mutex_lock(&avio_lock);
98 assert(!access != !current_access);
99 if (access && current_access) {
100 vlc_mutex_unlock(&avio_lock);
103 url_set_interrupt_cb(access ? UrlInterruptCallbackSingle : NULL);
105 current_access = access;
107 vlc_mutex_unlock(&avio_lock);
115 int OpenAvio(vlc_object_t *object)
117 access_t *access = (access_t*)object;
118 access_sys_t *sys = malloc(sizeof(*sys));
125 * - url (only a subset of available protocols).
128 if (!strcmp(access->psz_access, "avio"))
129 url = strdup(access->psz_location);
130 else if (asprintf(&url, "%s://%s", access->psz_access,
131 access->psz_location) < 0)
142 vlc_avcodec_unlock();
145 #if LIBAVFORMAT_VERSION_MAJOR < 54
146 ret = avio_open(&sys->context, url, AVIO_FLAG_READ);
148 AVIOInterruptCB cb = {
149 .callback = UrlInterruptCallback,
152 ret = avio_open2(&sys->context, url, AVIO_FLAG_READ, &cb, NULL /* options */);
155 errno = AVUNERROR(ret);
156 msg_Err(access, "Failed to open %s: %m", url);
162 #if LIBAVFORMAT_VERSION_MAJOR < 54
163 /* We can accept only one active user at any time */
164 if (SetupAvioCb(VLC_OBJECT(access))) {
165 msg_Err(access, "Module aready in use");
166 avio_close(sys->context);
171 int64_t size = avio_size(sys->context);
173 #if LIBAVFORMAT_VERSION_MAJOR < 54
174 seekable = !sys->context->is_streamed;
176 seekable = sys->context->seekable;
178 msg_Dbg(access, "%sseekable, size=%"PRIi64, seekable ? "" : "not ", size);
181 access_InitFields(access);
182 access->info.i_size = size > 0 ? size : 0;
184 access->pf_read = Read;
185 access->pf_block = NULL;
186 access->pf_control = Control;
187 access->pf_seek = Seek;
198 int OutOpenAvio(vlc_object_t *object)
200 sout_access_out_t *access = (sout_access_out_t*)object;
201 sout_access_out_sys_t *sys = malloc(sizeof(*sys));
209 vlc_avcodec_unlock();
211 if (!access->psz_path)
215 #if LIBAVFORMAT_VERSION_MAJOR < 54
216 ret = avio_open(&sys->context, access->psz_path, AVIO_FLAG_WRITE);
218 AVIOInterruptCB cb = {
219 .callback = UrlInterruptCallback,
222 ret = avio_open2(&sys->context, access->psz_path, AVIO_FLAG_WRITE,
223 &cb, NULL /* options */);
226 errno = AVUNERROR(ret);
227 msg_Err(access, "Failed to open %s", access->psz_path);
231 #if LIBAVFORMAT_VERSION_MAJOR < 54
232 /* We can accept only one active user at any time */
233 if (SetupAvioCb(VLC_OBJECT(access))) {
234 msg_Err(access, "Module aready in use");
239 access->pf_write = Write;
240 access->pf_control = OutControl;
241 access->pf_seek = OutSeek;
251 void CloseAvio(vlc_object_t *object)
253 access_t *access = (access_t*)object;
254 access_sys_t *sys = access->p_sys;
256 #if LIBAVFORMAT_VERSION_MAJOR < 54
260 avio_close(sys->context);
264 void OutCloseAvio(vlc_object_t *object)
266 sout_access_out_t *access = (sout_access_out_t*)object;
267 sout_access_out_sys_t *sys = access->p_sys;
269 #if LIBAVFORMAT_VERSION_MAJOR < 54
273 avio_close(sys->context);
277 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
279 int r = avio_read(access->p_sys->context, data, size);
281 access->info.i_pos += r;
283 access->info.b_eof = true;
287 /*****************************************************************************
289 *****************************************************************************/
290 static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
292 access_sys_t *p_sys = (access_sys_t*)p_access->p_sys;
295 while (p_buffer != NULL) {
296 block_t *p_next = p_buffer->p_next;
298 #if LIBAVFORMAT_VERSION_MAJOR < 54
299 int written = url_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
301 errno = AVUNERROR(written);
306 avio_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
307 avio_flush(p_sys->context);
308 if (p_sys->context->error) {
309 errno = AVUNERROR(p_sys->context->error);
310 p_sys->context->error = 0; /* FIXME? */
313 i_write += p_buffer->i_buffer;
316 block_Release(p_buffer);
324 msg_Err(p_access, "Wrote only %zu bytes (%m)", i_write);
325 block_ChainRelease( p_buffer );
329 static int Seek(access_t *access, uint64_t position)
331 access_sys_t *sys = access->p_sys;
335 # define EOVERFLOW EFBIG
338 if (position > INT64_MAX)
339 ret = AVERROR(EOVERFLOW);
341 ret = avio_seek(sys->context, position, SEEK_SET);
343 errno = AVUNERROR(ret);
344 msg_Err(access, "Seek to %"PRIu64" failed: %m", position);
345 if (access->info.i_size <= 0 || position != access->info.i_size)
348 access->info.i_pos = position;
349 access->info.b_eof = false;
353 static int OutSeek(sout_access_out_t *p_access, off_t i_pos)
355 sout_access_out_sys_t *sys = p_access->p_sys;
357 if (avio_seek(sys->context, i_pos, SEEK_SET) < 0)
362 static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
364 sout_access_out_sys_t *p_sys = p_access->p_sys;
368 case ACCESS_OUT_CONTROLS_PACE: {
369 bool *pb = va_arg(args, bool *);
370 //*pb = strcmp(p_access->psz_access, "stream");
381 static int Control(access_t *access, int query, va_list args)
383 access_sys_t *sys = access->p_sys;
387 case ACCESS_CAN_SEEK:
388 case ACCESS_CAN_FASTSEEK: /* FIXME how to do that ? */
389 b = va_arg(args, bool *);
390 #if LIBAVFORMAT_VERSION_MAJOR < 54
391 *b = !sys->context->is_streamed;
393 *b = sys->context->seekable;
396 case ACCESS_CAN_PAUSE:
397 b = va_arg(args, bool *);
398 #if LIBAVFORMAT_VERSION_MAJOR < 54
399 *b = sys->context->prot->url_read_pause != NULL;
401 *b = sys->context->read_pause != NULL;
404 case ACCESS_CAN_CONTROL_PACE:
405 b = va_arg(args, bool *);
406 *b = true; /* FIXME */
408 case ACCESS_GET_PTS_DELAY: {
409 int64_t *delay = va_arg(args, int64_t *);
410 *delay = DEFAULT_PTS_DELAY; /* FIXME */
413 case ACCESS_SET_PAUSE_STATE: {
414 bool is_paused = va_arg(args, int);
415 if (avio_pause(sys->context, is_paused)< 0)
419 case ACCESS_GET_TITLE_INFO:
420 case ACCESS_GET_META:
421 case ACCESS_GET_CONTENT_TYPE:
422 case ACCESS_GET_SIGNAL:
423 case ACCESS_SET_TITLE:
424 case ACCESS_SET_SEEKPOINT:
425 case ACCESS_SET_PRIVATE_ID_STATE:
426 case ACCESS_SET_PRIVATE_ID_CA:
427 case ACCESS_GET_PRIVATE_ID_STATE: