]> git.sesse.net Git - vlc/blob - modules/access/avio.c
c6339ff0eb67b1eb8a22cb1e50594265f58f1740
[vlc] / modules / access / avio.c
1 /*****************************************************************************
2  * avio.c: access using libavformat library
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_access.h>
32 #include <vlc_sout.h>
33 #include <vlc_avcodec.h>
34
35 #include "avio.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 #ifndef MERGE_FFMPEG
41 vlc_module_begin()
42     AVIO_MODULE
43 vlc_module_end()
44 #endif
45
46 /*****************************************************************************
47  * Local prototypes
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);
55
56 static int     SetupAvio(vlc_object_t *);
57
58 struct access_sys_t {
59     URLContext *context;
60 };
61
62 struct sout_access_out_sys_t {
63     URLContext *context;
64 };
65
66 /* */
67 int OpenAvio(vlc_object_t *object)
68 {
69     access_t *access = (access_t*)object;
70     access_sys_t *sys;
71
72     /* */
73     access->p_sys = sys = malloc(sizeof(*sys));
74     if (!sys)
75         return VLC_ENOMEM;
76
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");
81         return VLC_EGENERIC;
82     }
83
84     /* */
85     vlc_avcodec_lock();
86     av_register_all();
87     vlc_avcodec_unlock();
88
89     /* We accept:
90      * - avio://full_url
91      * - url (only a subset of available protocols).
92      */
93     char *url;
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)
98         url = NULL;
99
100     if (!url)
101         goto error;
102
103     msg_Dbg(access, "Opening '%s'", url);
104     if (url_open(&sys->context, url, URL_RDONLY) < 0)
105         sys->context = NULL;
106     free(url);
107
108     if (!sys->context) {
109         msg_Err(access, "Failed to open url using libavformat");
110         goto error;
111     }
112     const int64_t size = url_filesize(sys->context);
113     msg_Dbg(access, "is_streamed=%d size=%"PRIi64, sys->context->is_streamed, size);
114
115     /* */
116     access_InitFields(access);
117     access->info.i_size = size > 0 ? size : 0;
118
119     access->pf_read = Read;
120     access->pf_block = NULL;
121     access->pf_control = Control;
122     access->pf_seek = Seek;
123     access->p_sys = sys;
124
125     return VLC_SUCCESS;
126
127 error:
128     SetupAvio(NULL);
129     free(sys);
130     return VLC_EGENERIC;
131 }
132
133 /* */
134 int OutOpenAvio(vlc_object_t *object)
135 {
136     sout_access_out_t *access = (sout_access_out_t*)object;
137     sout_access_out_sys_t *sys;
138
139     /* */
140     access->p_sys = sys = malloc(sizeof(*sys));
141     if (!sys)
142         return VLC_ENOMEM;
143
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");
148         free(sys);
149         return VLC_EGENERIC;
150     }
151
152     /* */
153     vlc_avcodec_lock();
154     av_register_all();
155     vlc_avcodec_unlock();
156
157     char *url = NULL;
158     if (access->psz_path)
159         url = strdup(access->psz_path);
160
161     if (!url)
162         goto error;
163
164     msg_Dbg(access, "avio_output Opening '%s'", url);
165     if (url_open(&sys->context, url, URL_WRONLY) < 0)
166         sys->context = NULL;
167     free(url);
168
169     if (!sys->context) {
170         msg_Err(access, "Failed to open url using libavformat");
171         goto error;
172     }
173
174     access->pf_write = Write;
175     access->pf_control = OutControl;
176     access->pf_seek = OutSeek;
177     access->p_sys = sys;
178
179     return VLC_SUCCESS;
180
181 error:
182     SetupAvio(NULL);
183     free(sys);
184     return VLC_EGENERIC;
185 }
186
187 void CloseAvio(vlc_object_t *object)
188 {
189     access_t *access = (access_t*)object;
190     access_sys_t *sys = access->p_sys;
191
192     url_close(sys->context);
193
194     SetupAvio(NULL);
195
196     free(sys);
197 }
198
199 void OutCloseAvio(vlc_object_t *object)
200 {
201     sout_access_out_t *access = (sout_access_out_t*)object;
202     sout_access_out_sys_t *sys = access->p_sys;
203
204     url_close(sys->context);
205
206     SetupAvio(NULL);
207
208     free(sys);
209 }
210
211 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
212 {
213     /* FIXME I am unsure of the meaning of the return value in case
214      * of error.
215      */
216     int r = url_read(access->p_sys->context, data, size);
217     access->info.b_eof = r <= 0;
218     if (r < 0)
219         return -1;
220     access->info.i_pos += r;
221     return r;
222 }
223
224 /*****************************************************************************
225  * Write:
226  *****************************************************************************/
227 static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
228 {
229     access_sys_t *p_sys = (access_sys_t*)p_access->p_sys;
230     size_t i_write = 0;
231
232     while (p_buffer != NULL) {
233         block_t *p_next = p_buffer->p_next;;
234
235         i_write += url_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
236         block_Release(p_buffer);
237
238         p_buffer = p_next;
239     }
240
241     return i_write;
242 }
243
244 static int Seek(access_t *access, uint64_t position)
245 {
246     access_sys_t *sys = access->p_sys;
247
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)
252             return VLC_EGENERIC;
253     }
254     access->info.i_pos = position;
255     access->info.b_eof = false;
256     return VLC_SUCCESS;
257 }
258
259 static int OutSeek(sout_access_out_t *p_access, off_t i_pos)
260 {
261     sout_access_out_sys_t *sys = p_access->p_sys;
262
263     if (url_seek(sys->context, i_pos, SEEK_SET) < 0)
264         return VLC_EGENERIC;
265     return VLC_SUCCESS;
266 }
267
268 static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
269 {
270     sout_access_out_sys_t *p_sys = p_access->p_sys;
271
272     VLC_UNUSED(p_sys);
273     switch (i_query) {
274     case ACCESS_OUT_CONTROLS_PACE: {
275         bool *pb = va_arg(args, bool *);
276         //*pb = strcmp(p_access->psz_access, "stream");
277         *pb = false;
278         break;
279     }
280
281     default:
282         return VLC_EGENERIC;
283     }
284     return VLC_SUCCESS;
285 }
286
287 static int Control(access_t *access, int query, va_list args)
288 {
289     access_sys_t *sys = access->p_sys;
290
291     switch (query) {
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;
296         return VLC_SUCCESS;
297     }
298     case ACCESS_CAN_PAUSE: {
299         bool *b = va_arg(args, bool *);
300         *b = sys->context->prot->url_read_pause != NULL; /* FIXME Unsure */
301         return VLC_SUCCESS;
302     }
303     case ACCESS_CAN_CONTROL_PACE: {
304         bool *b = va_arg(args, bool *);
305         *b = true; /* FIXME */
306         return VLC_SUCCESS;
307     }
308     case ACCESS_GET_PTS_DELAY: {
309         int64_t *delay = va_arg(args, int64_t *);
310         *delay = DEFAULT_PTS_DELAY; /* FIXME */
311     }
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)
315             return VLC_EGENERIC;
316         return VLC_SUCCESS;
317     }
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:
327     default:
328         return VLC_EGENERIC;
329     }
330 }
331
332 /* */
333 static vlc_mutex_t avio_lock = VLC_STATIC_MUTEX;
334 static vlc_object_t *current_access = NULL;
335
336
337 static int UrlInterruptCallback(void)
338 {
339     assert(current_access);
340     return !vlc_object_alive(current_access);
341 }
342
343
344 static int SetupAvio(vlc_object_t *access)
345 {
346     vlc_mutex_lock(&avio_lock);
347     assert(!access != !current_access);
348     if (access && current_access) {
349         vlc_mutex_unlock(&avio_lock);
350         return VLC_EGENERIC;
351     }
352     url_set_interrupt_cb(access ? UrlInterruptCallback : NULL);
353     current_access = access;
354     vlc_mutex_unlock(&avio_lock);
355
356     return VLC_SUCCESS;
357 }