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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_access.h>
32 #include <vlc_avcodec.h>
33 #include <vlc_interrupt.h>
36 #include "../codec/avcodec/avcommon.h"
38 /*****************************************************************************
40 *****************************************************************************/
47 /*****************************************************************************
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. */
76 } sout_access_out_sys_t
;
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
));
93 * - url (only a subset of available protocols).
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)
106 vlc_init_avformat(object
);
109 AVIOInterruptCB cb
= {
110 .callback
= UrlInterruptCallback
,
113 AVDictionary
*options
= NULL
;
114 char *psz_opts
= var_InheritString(access
, "avio-options");
116 vlc_av_get_options(psz_opts
, &options
);
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
);
125 msg_Err(access
, "Failed to open %s: %s", url
,
126 vlc_strerror_c(AVUNERROR(ret
)));
132 sys
->size
= avio_size(sys
->context
);
135 seekable
= sys
->context
->seekable
;
136 msg_Dbg(access
, "%sseekable, size=%"PRIi64
, seekable
? "" : "not ",
140 access
->pf_read
= Read
;
141 access
->pf_block
= NULL
;
142 access
->pf_control
= Control
;
143 access
->pf_seek
= Seek
;
151 static const char *const ppsz_sout_options
[] = {
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
));
168 vlc_init_avformat(object
);
170 if (!access
->psz_path
)
174 AVDictionary
*options
= NULL
;
175 char *psz_opts
= var_InheritString(access
, "sout-avio-options");
177 vlc_av_get_options(psz_opts
, &options
);
180 ret
= avio_open2(&sys
->context
, access
->psz_path
, AVIO_FLAG_WRITE
,
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
);
187 errno
= AVUNERROR(ret
);
188 msg_Err(access
, "Failed to open %s", access
->psz_path
);
192 access
->pf_write
= Write
;
193 access
->pf_control
= OutControl
;
194 access
->pf_seek
= OutSeek
;
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
);
226 /*****************************************************************************
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
;
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? */
244 i_write
+= p_buffer
->i_buffer
;
246 block_Release(p_buffer
);
254 msg_Err(p_access
, "Wrote only %zu bytes: %s", i_write
,
255 vlc_strerror_c(AVUNERROR(val
)));
256 block_ChainRelease( p_buffer
);
260 static int Seek(stream_t
*access
, uint64_t position
)
262 access_sys_t
*sys
= access
->p_sys
;
266 # define EOVERFLOW EFBIG
269 if (position
> INT64_MAX
)
270 ret
= AVERROR(EOVERFLOW
);
272 ret
= avio_seek(sys
->context
, position
, SEEK_SET
);
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
)
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)
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
;
297 case ACCESS_OUT_CONTROLS_PACE
: {
298 bool *pb
= va_arg(args
, bool *);
299 //*pb = strcmp(p_access->psz_name, "stream");
310 static int Control(stream_t
*access
, int query
, va_list args
)
312 access_sys_t
*sys
= access
->p_sys
;
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
;
321 case STREAM_CAN_PAUSE
:
322 b
= va_arg(args
, bool *);
323 *b
= sys
->context
->read_pause
!= NULL
;
325 case STREAM_CAN_CONTROL_PACE
:
326 b
= va_arg(args
, bool *);
327 *b
= true; /* FIXME */
329 case STREAM_GET_SIZE
:
332 *va_arg(args
, uint64_t *) = sys
->size
;
334 case STREAM_GET_PTS_DELAY
:
335 *va_arg(args
, vlc_tick_t
*) =
336 VLC_TICK_FROM_MS(var_InheritInteger(access
, "network-caching"));
339 case STREAM_SET_PAUSE_STATE
: {
340 bool is_paused
= va_arg(args
, int);
341 if (avio_pause(sys
->context
, is_paused
)< 0)