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 /*****************************************************************************
39 *****************************************************************************/
46 /*****************************************************************************
48 *****************************************************************************/
49 static ssize_t Read (access_t *, uint8_t *, size_t);
50 static int Seek (access_t *, uint64_t);
51 static int Control(access_t *, int, va_list);
52 static ssize_t Write(sout_access_out_t *, block_t *);
53 static int OutControl(sout_access_out_t *, int, va_list);
54 static int OutSeek (sout_access_out_t *, off_t);
56 static int SetupAvio(vlc_object_t *);
62 struct sout_access_out_sys_t {
67 int OpenAvio(vlc_object_t *object)
69 access_t *access = (access_t*)object;
73 access->p_sys = sys = malloc(sizeof(*sys));
77 /* We can either accept only one user (actually) or multiple ones
78 * with an exclusive lock */
79 if (SetupAvio(VLC_OBJECT(access))) {
80 msg_Err(access, "Module aready in use");
91 * - url (only a subset of available protocols).
94 if (!strcmp(access->psz_access, "avio"))
95 url = strdup(access->psz_location);
96 else if (asprintf(&url, "%s://%s", access->psz_access,
97 access->psz_location) < 0)
103 msg_Dbg(access, "Opening '%s'", url);
104 if (url_open(&sys->context, url, URL_RDONLY) < 0)
109 msg_Err(access, "Failed to open url using libavformat");
112 const int64_t size = url_filesize(sys->context);
113 msg_Dbg(access, "is_streamed=%d size=%"PRIi64, sys->context->is_streamed, size);
116 access_InitFields(access);
117 access->info.i_size = size > 0 ? size : 0;
119 access->pf_read = Read;
120 access->pf_block = NULL;
121 access->pf_control = Control;
122 access->pf_seek = Seek;
134 int OutOpenAvio(vlc_object_t *object)
136 sout_access_out_t *access = (sout_access_out_t*)object;
137 sout_access_out_sys_t *sys;
140 access->p_sys = sys = malloc(sizeof(*sys));
144 /* We can either accept only one user (actually) or multiple ones
145 * with an exclusive lock */
146 if (SetupAvio(VLC_OBJECT(access))) {
147 msg_Err(access, "Module aready in use");
155 vlc_avcodec_unlock();
158 if (access->psz_path)
159 url = strdup(access->psz_path);
164 msg_Dbg(access, "avio_output Opening '%s'", url);
165 if (url_open(&sys->context, url, URL_WRONLY) < 0)
170 msg_Err(access, "Failed to open url using libavformat");
174 access->pf_write = Write;
175 access->pf_control = OutControl;
176 access->pf_seek = OutSeek;
187 void CloseAvio(vlc_object_t *object)
189 access_t *access = (access_t*)object;
190 access_sys_t *sys = access->p_sys;
192 url_close(sys->context);
199 void OutCloseAvio(vlc_object_t *object)
201 sout_access_out_t *access = (sout_access_out_t*)object;
202 sout_access_out_sys_t *sys = access->p_sys;
204 url_close(sys->context);
211 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
213 /* FIXME I am unsure of the meaning of the return value in case
216 int r = url_read(access->p_sys->context, data, size);
217 access->info.b_eof = r <= 0;
220 access->info.i_pos += r;
224 /*****************************************************************************
226 *****************************************************************************/
227 static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
229 access_sys_t *p_sys = (access_sys_t*)p_access->p_sys;
232 while (p_buffer != NULL) {
233 block_t *p_next = p_buffer->p_next;;
235 i_write += url_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
236 block_Release(p_buffer);
244 static int Seek(access_t *access, uint64_t position)
246 access_sys_t *sys = access->p_sys;
248 if (position > INT64_MAX ||
249 url_seek(sys->context, position, SEEK_SET) < 0) {
250 msg_Err(access, "Seek to %"PRIu64" failed\n", position);
251 if (access->info.i_size <= 0 || position != access->info.i_size)
254 access->info.i_pos = position;
255 access->info.b_eof = false;
259 static int OutSeek(sout_access_out_t *p_access, off_t i_pos)
261 sout_access_out_sys_t *sys = p_access->p_sys;
263 if (url_seek(sys->context, i_pos, SEEK_SET) < 0)
268 static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
270 sout_access_out_sys_t *p_sys = p_access->p_sys;
274 case ACCESS_OUT_CONTROLS_PACE: {
275 bool *pb = va_arg(args, bool *);
276 //*pb = strcmp(p_access->psz_access, "stream");
287 static int Control(access_t *access, int query, va_list args)
289 access_sys_t *sys = access->p_sys;
292 case ACCESS_CAN_SEEK:
293 case ACCESS_CAN_FASTSEEK: { /* FIXME how to do that ? */
294 bool *b = va_arg(args, bool *);
295 *b = !sys->context->is_streamed;
298 case ACCESS_CAN_PAUSE: {
299 bool *b = va_arg(args, bool *);
300 *b = sys->context->prot->url_read_pause != NULL; /* FIXME Unsure */
303 case ACCESS_CAN_CONTROL_PACE: {
304 bool *b = va_arg(args, bool *);
305 *b = true; /* FIXME */
308 case ACCESS_GET_PTS_DELAY: {
309 int64_t *delay = va_arg(args, int64_t *);
310 *delay = DEFAULT_PTS_DELAY; /* FIXME */
312 case ACCESS_SET_PAUSE_STATE: {
313 bool is_paused = va_arg(args, int);
314 if (av_url_read_pause(sys->context, is_paused)< 0)
318 case ACCESS_GET_TITLE_INFO:
319 case ACCESS_GET_META:
320 case ACCESS_GET_CONTENT_TYPE:
321 case ACCESS_GET_SIGNAL:
322 case ACCESS_SET_TITLE:
323 case ACCESS_SET_SEEKPOINT:
324 case ACCESS_SET_PRIVATE_ID_STATE:
325 case ACCESS_SET_PRIVATE_ID_CA:
326 case ACCESS_GET_PRIVATE_ID_STATE:
333 static vlc_mutex_t avio_lock = VLC_STATIC_MUTEX;
334 static vlc_object_t *current_access = NULL;
337 static int UrlInterruptCallback(void)
339 assert(current_access);
340 return !vlc_object_alive(current_access);
344 static int SetupAvio(vlc_object_t *access)
346 vlc_mutex_lock(&avio_lock);
347 assert(!access != !current_access);
348 if (access && current_access) {
349 vlc_mutex_unlock(&avio_lock);
352 url_set_interrupt_cb(access ? UrlInterruptCallback : NULL);
353 current_access = access;
354 vlc_mutex_unlock(&avio_lock);