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);
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 AVDictionary *options = NULL;
152 char *psz_opts = var_InheritString(access, "avio-options");
153 if (psz_opts && *psz_opts) {
154 options = vlc_av_get_options(psz_opts);
157 ret = avio_open2(&sys->context, url, AVIO_FLAG_READ, &cb, &options);
158 AVDictionaryEntry *t = NULL;
159 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
160 msg_Err( access, "unknown option \"%s\"", t->key );
161 av_dict_free(&options);
164 errno = AVUNERROR(ret);
165 msg_Err(access, "Failed to open %s: %m", url);
171 #if LIBAVFORMAT_VERSION_MAJOR < 54
172 /* We can accept only one active user at any time */
173 if (SetupAvioCb(VLC_OBJECT(access))) {
174 msg_Err(access, "Module aready in use");
175 avio_close(sys->context);
180 int64_t size = avio_size(sys->context);
182 #if LIBAVFORMAT_VERSION_MAJOR < 54
183 seekable = !sys->context->is_streamed;
185 seekable = sys->context->seekable;
187 msg_Dbg(access, "%sseekable, size=%"PRIi64, seekable ? "" : "not ", size);
190 access_InitFields(access);
191 access->info.i_size = size > 0 ? size : 0;
193 access->pf_read = Read;
194 access->pf_block = NULL;
195 access->pf_control = Control;
196 access->pf_seek = Seek;
208 static const char *const ppsz_sout_options[] = {
213 int OutOpenAvio(vlc_object_t *object)
215 sout_access_out_t *access = (sout_access_out_t*)object;
217 config_ChainParse( access, "sout-avio-", ppsz_sout_options, access->p_cfg );
219 sout_access_out_sys_t *sys = malloc(sizeof(*sys));
227 if (!access->psz_path)
231 #if LIBAVFORMAT_VERSION_MAJOR < 54
232 ret = avio_open(&sys->context, access->psz_path, AVIO_FLAG_WRITE);
234 AVIOInterruptCB cb = {
235 .callback = UrlInterruptCallback,
238 AVDictionary *options = NULL;
239 char *psz_opts = var_InheritString(access, "sout-avio-options");
240 if (psz_opts && *psz_opts) {
241 options = vlc_av_get_options(psz_opts);
244 ret = avio_open2(&sys->context, access->psz_path, AVIO_FLAG_WRITE,
246 AVDictionaryEntry *t = NULL;
247 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
248 msg_Err( access, "unknown option \"%s\"", t->key );
249 av_dict_free(&options);
252 errno = AVUNERROR(ret);
253 msg_Err(access, "Failed to open %s", access->psz_path);
257 #if LIBAVFORMAT_VERSION_MAJOR < 54
258 /* We can accept only one active user at any time */
259 if (SetupAvioCb(VLC_OBJECT(access))) {
260 msg_Err(access, "Module aready in use");
265 access->pf_write = Write;
266 access->pf_control = OutControl;
267 access->pf_seek = OutSeek;
277 void CloseAvio(vlc_object_t *object)
279 access_t *access = (access_t*)object;
280 access_sys_t *sys = access->p_sys;
282 #if LIBAVFORMAT_VERSION_MAJOR < 54
286 avio_close(sys->context);
290 void OutCloseAvio(vlc_object_t *object)
292 sout_access_out_t *access = (sout_access_out_t*)object;
293 sout_access_out_sys_t *sys = access->p_sys;
295 #if LIBAVFORMAT_VERSION_MAJOR < 54
299 avio_close(sys->context);
303 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
305 int r = avio_read(access->p_sys->context, data, size);
307 access->info.i_pos += r;
309 access->info.b_eof = true;
313 /*****************************************************************************
315 *****************************************************************************/
316 static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
318 access_sys_t *p_sys = (access_sys_t*)p_access->p_sys;
321 while (p_buffer != NULL) {
322 block_t *p_next = p_buffer->p_next;
324 #if LIBAVFORMAT_VERSION_MAJOR < 54
325 int written = url_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
327 errno = AVUNERROR(written);
332 avio_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
333 avio_flush(p_sys->context);
334 if (p_sys->context->error) {
335 errno = AVUNERROR(p_sys->context->error);
336 p_sys->context->error = 0; /* FIXME? */
339 i_write += p_buffer->i_buffer;
342 block_Release(p_buffer);
350 msg_Err(p_access, "Wrote only %zu bytes (%m)", i_write);
351 block_ChainRelease( p_buffer );
355 static int Seek(access_t *access, uint64_t position)
357 access_sys_t *sys = access->p_sys;
361 # define EOVERFLOW EFBIG
364 if (position > INT64_MAX)
365 ret = AVERROR(EOVERFLOW);
367 ret = avio_seek(sys->context, position, SEEK_SET);
369 errno = AVUNERROR(ret);
370 msg_Err(access, "Seek to %"PRIu64" failed: %m", position);
371 if (access->info.i_size <= 0 || position != access->info.i_size)
374 access->info.i_pos = position;
375 access->info.b_eof = false;
379 static int OutSeek(sout_access_out_t *p_access, off_t i_pos)
381 sout_access_out_sys_t *sys = p_access->p_sys;
383 if (avio_seek(sys->context, i_pos, SEEK_SET) < 0)
388 static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
390 sout_access_out_sys_t *p_sys = p_access->p_sys;
394 case ACCESS_OUT_CONTROLS_PACE: {
395 bool *pb = va_arg(args, bool *);
396 //*pb = strcmp(p_access->psz_access, "stream");
407 static int Control(access_t *access, int query, va_list args)
409 access_sys_t *sys = access->p_sys;
413 case ACCESS_CAN_SEEK:
414 case ACCESS_CAN_FASTSEEK: /* FIXME how to do that ? */
415 b = va_arg(args, bool *);
416 #if LIBAVFORMAT_VERSION_MAJOR < 54
417 *b = !sys->context->is_streamed;
419 *b = sys->context->seekable;
422 case ACCESS_CAN_PAUSE:
423 b = va_arg(args, bool *);
424 #if LIBAVFORMAT_VERSION_MAJOR < 54
425 *b = sys->context->prot->url_read_pause != NULL;
427 *b = sys->context->read_pause != NULL;
430 case ACCESS_CAN_CONTROL_PACE:
431 b = va_arg(args, bool *);
432 *b = true; /* FIXME */
434 case ACCESS_GET_PTS_DELAY: {
435 int64_t *delay = va_arg(args, int64_t *);
436 *delay = DEFAULT_PTS_DELAY; /* FIXME */
439 case ACCESS_SET_PAUSE_STATE: {
440 bool is_paused = va_arg(args, int);
441 if (avio_pause(sys->context, is_paused)< 0)
445 case ACCESS_GET_TITLE_INFO:
446 case ACCESS_GET_META:
447 case ACCESS_GET_CONTENT_TYPE:
448 case ACCESS_GET_SIGNAL:
449 case ACCESS_SET_TITLE:
450 case ACCESS_SET_SEEKPOINT:
451 case ACCESS_SET_PRIVATE_ID_STATE:
452 case ACCESS_SET_PRIVATE_ID_CA:
453 case ACCESS_GET_PRIVATE_ID_STATE: