avio: lld => PRIi64.
[vlc/asuraparaju-public.git] / modules / access / avio.c
blob0da9278f2ee01485b43a0957c048b7b9bc107265
1 /*****************************************************************************
2 * avio.c: access using libavformat library
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
5 * $Id$
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <assert.h>
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_access.h>
32 #include <vlc_avcodec.h>
34 #include "avio.h"
36 /*****************************************************************************
37 * Module descriptor
38 *****************************************************************************/
39 #ifndef MERGE_FFMPEG
40 vlc_module_begin()
41 AVIO_MODULE
42 vlc_module_end()
43 #endif
45 /*****************************************************************************
46 * Local prototypes
47 *****************************************************************************/
48 static ssize_t Read (access_t *, uint8_t *, size_t);
49 static int Seek (access_t *, int64_t);
50 static int Control(access_t *, int, va_list);
52 static int SetupAvio(access_t *);
54 struct access_sys_t {
55 URLContext *context;
58 /* */
59 int OpenAvio(vlc_object_t *object)
61 access_t *access = (access_t*)object;
62 access_sys_t *sys;
64 /* */
65 access->p_sys = sys = malloc(sizeof(*sys));
66 if (!sys)
67 return VLC_ENOMEM;
69 /* We can either accept only one user (actually) or multiple ones
70 * with an exclusive lock */
71 if (SetupAvio(access)) {
72 msg_Err(access, "Module aready in use");
73 return VLC_EGENERIC;
76 /* */
77 vlc_avcodec_lock();
78 av_register_all();
79 vlc_avcodec_unlock();
81 /* We accept:
82 * - avio://full_url
83 * - url (only a subset of available protocols).
85 char *url;
86 if (!strcmp(access->psz_access, "avio"))
87 url = strdup(access->psz_path);
88 else if (asprintf(&url, "%s://%s", access->psz_access, access->psz_path) < 0)
89 url = NULL;
91 if (!url)
92 goto error;
94 msg_Dbg(access, "Opening '%s'", url);
95 if (url_open(&sys->context, url, URL_RDONLY) < 0 )
96 sys->context = NULL;
97 free(url);
99 if (!sys->context) {
100 msg_Err(access, "Failed to open url using libavformat");
101 goto error;
103 const int64_t size = url_filesize(sys->context);
104 msg_Dbg(access, "is_streamed=%d size=%"PRIi64, sys->context->is_streamed, size);
106 /* */
107 access_InitFields(access);
108 access->info.i_size = size > 0 ? size : 0;
110 access->pf_read = Read;
111 access->pf_block = NULL;
112 access->pf_control = Control;
113 access->pf_seek = Seek;
114 access->p_sys = sys;
116 return VLC_SUCCESS;
118 error:
119 SetupAvio(NULL);
120 free(sys);
121 return VLC_EGENERIC;
125 void CloseAvio(vlc_object_t *object)
127 access_t *access = (access_t*)object;
128 access_sys_t *sys = access->p_sys;
130 url_close(sys->context);
132 SetupAvio(NULL);
134 free(sys);
138 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
140 /* FIXME I am unsure of the meaning of the return value in case
141 * of error.
143 int r = url_read(access->p_sys->context, data, size);
144 access->info.b_eof = r <= 0;
145 if (r < 0)
146 return -1;
147 access->info.i_pos += r;
148 return r;
152 static int Seek(access_t *access, int64_t position)
154 access_sys_t *sys = access->p_sys;
156 if (url_seek(sys->context, position, SEEK_SET) < 0) {
157 msg_Err(access, "Seek to %"PRIi64" failed\n", position);
158 if (access->info.i_size <= 0 || position != access->info.i_size)
159 return VLC_EGENERIC;
161 access->info.i_pos = position;
162 access->info.b_eof = false;
163 return VLC_SUCCESS;
167 static int Control(access_t *access, int query, va_list args)
169 access_sys_t *sys = access->p_sys;
171 switch (query) {
172 case ACCESS_CAN_SEEK:
173 case ACCESS_CAN_FASTSEEK: { /* FIXME how to do that ? */
174 bool *b = va_arg(args, bool *);
175 *b = !sys->context->is_streamed;
176 return VLC_SUCCESS;
178 case ACCESS_CAN_PAUSE: {
179 bool *b = va_arg(args, bool *);
180 *b = sys->context->prot->url_read_pause != NULL; /* FIXME Unsure */
181 return VLC_SUCCESS;
183 case ACCESS_CAN_CONTROL_PACE: {
184 bool *b = va_arg(args, bool *);
185 *b = true; /* FIXME */
186 return VLC_SUCCESS;
188 case ACCESS_GET_PTS_DELAY: {
189 int64_t *delay = va_arg(args, int64_t *);
190 *delay = DEFAULT_PTS_DELAY; /* FIXME */
192 case ACCESS_SET_PAUSE_STATE: {
193 bool is_paused = va_arg(args, int);
194 if (av_url_read_pause(sys->context, is_paused)< 0)
195 return VLC_EGENERIC;
196 return VLC_SUCCESS;
198 case ACCESS_GET_TITLE_INFO:
199 case ACCESS_GET_META:
200 case ACCESS_GET_CONTENT_TYPE:
201 case ACCESS_GET_SIGNAL:
202 case ACCESS_SET_TITLE:
203 case ACCESS_SET_SEEKPOINT:
204 case ACCESS_SET_PRIVATE_ID_STATE:
205 case ACCESS_SET_PRIVATE_ID_CA:
206 case ACCESS_GET_PRIVATE_ID_STATE:
207 default:
208 return VLC_EGENERIC;
212 /* */
213 static vlc_mutex_t avio_lock = VLC_STATIC_MUTEX;
214 static access_t *current_access = NULL;
217 static int UrlInterruptCallback(void)
219 assert(current_access);
220 return !vlc_object_alive(current_access);
224 static int SetupAvio(access_t *access)
226 vlc_mutex_lock(&avio_lock);
227 assert(!access != !current_access);
228 if (access && current_access) {
229 vlc_mutex_unlock(&avio_lock);
230 return VLC_EGENERIC;
232 url_set_interrupt_cb(access ? UrlInterruptCallback : NULL);
233 current_access = access;
234 vlc_mutex_unlock(&avio_lock);
236 return VLC_SUCCESS;