]> git.sesse.net Git - vlc/blob - modules/access/avio.c
access: http: only warn on deflate errors
[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 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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
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 #include "../codec/avcodec/avcommon.h"
37
38 #if LIBAVFORMAT_VERSION_MAJOR < 54
39 # define AVIOContext URLContext
40
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
46
47 # define AVIO_FLAG_READ URL_RDONLY
48 # define AVIO_FLAG_WRITE URL_WRONLY
49 # define avio_size url_filesize
50 #endif
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 #ifndef MERGE_FFMPEG
56 vlc_module_begin()
57     AVIO_MODULE
58 vlc_module_end()
59 #endif
60
61 /*****************************************************************************
62  * Local prototypes
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);
70
71 static int UrlInterruptCallback(void *access)
72 {
73     return !vlc_object_alive((vlc_object_t*)access);
74 }
75
76 struct access_sys_t {
77     AVIOContext *context;
78 };
79
80 struct sout_access_out_sys_t {
81     AVIOContext *context;
82 };
83
84
85 /* */
86
87 #if LIBAVFORMAT_VERSION_MAJOR < 54
88 static vlc_object_t *current_access = NULL;
89
90 static int UrlInterruptCallbackSingle(void)
91 {
92     return UrlInterruptCallback(current_access);
93 }
94
95 static int SetupAvioCb(vlc_object_t *access)
96 {
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);
102         return VLC_EGENERIC;
103     }
104     url_set_interrupt_cb(access ? UrlInterruptCallbackSingle : NULL);
105
106     current_access = access;
107
108     vlc_mutex_unlock(&avio_lock);
109
110     return VLC_SUCCESS;
111 }
112 #endif
113
114 /* */
115
116 int OpenAvio(vlc_object_t *object)
117 {
118     access_t *access = (access_t*)object;
119     access_sys_t *sys = malloc(sizeof(*sys));
120     if (!sys)
121         return VLC_ENOMEM;
122     sys->context = NULL;
123
124     /* We accept:
125      * - avio://full_url
126      * - url (only a subset of available protocols).
127      */
128     char *url;
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)
133         url = NULL;
134
135     if (!url) {
136         free(sys);
137         return VLC_ENOMEM;
138     }
139
140     /* */
141     vlc_init_avformat();
142
143     int ret;
144 #if LIBAVFORMAT_VERSION_MAJOR < 54
145     ret = avio_open(&sys->context, url, AVIO_FLAG_READ);
146 #else
147     AVIOInterruptCB cb = {
148         .callback = UrlInterruptCallback,
149         .opaque = access,
150     };
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);
155         free(psz_opts);
156     }
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);
162 #endif
163     if (ret < 0) {
164         errno = AVUNERROR(ret);
165         msg_Err(access, "Failed to open %s: %m", url);
166         free(url);
167         goto error;
168     }
169     free(url);
170
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);
176         goto error;
177     }
178 #endif
179
180     int64_t size = avio_size(sys->context);
181     bool seekable;
182 #if LIBAVFORMAT_VERSION_MAJOR < 54
183     seekable = !sys->context->is_streamed;
184 #else
185     seekable = sys->context->seekable;
186 #endif
187     msg_Dbg(access, "%sseekable, size=%"PRIi64, seekable ? "" : "not ", size);
188
189     /* */
190     access_InitFields(access);
191     access->info.i_size = size > 0 ? size : 0;
192
193     access->pf_read = Read;
194     access->pf_block = NULL;
195     access->pf_control = Control;
196     access->pf_seek = Seek;
197     access->p_sys = sys;
198
199     return VLC_SUCCESS;
200
201 error:
202     free(sys);
203     return VLC_EGENERIC;
204 }
205
206 /* */
207
208 static const char *const ppsz_sout_options[] = {
209     "options",
210     NULL,
211 };
212
213 int OutOpenAvio(vlc_object_t *object)
214 {
215     sout_access_out_t *access = (sout_access_out_t*)object;
216
217     config_ChainParse( access, "sout-avio-", ppsz_sout_options, access->p_cfg );
218
219     sout_access_out_sys_t *sys = malloc(sizeof(*sys));
220     if (!sys)
221         return VLC_ENOMEM;
222     sys->context = NULL;
223
224     /* */
225     vlc_init_avformat();
226
227     if (!access->psz_path)
228         goto error;
229
230     int ret;
231 #if LIBAVFORMAT_VERSION_MAJOR < 54
232     ret = avio_open(&sys->context, access->psz_path, AVIO_FLAG_WRITE);
233 #else
234     AVIOInterruptCB cb = {
235         .callback = UrlInterruptCallback,
236         .opaque = access,
237     };
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);
242         free(psz_opts);
243     }
244     ret = avio_open2(&sys->context, access->psz_path, AVIO_FLAG_WRITE,
245                      &cb, &options);
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);
250 #endif
251     if (ret < 0) {
252         errno = AVUNERROR(ret);
253         msg_Err(access, "Failed to open %s", access->psz_path);
254         goto error;
255     }
256
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");
261         goto error;
262     }
263 #endif
264
265     access->pf_write = Write;
266     access->pf_control = OutControl;
267     access->pf_seek = OutSeek;
268     access->p_sys = sys;
269
270     return VLC_SUCCESS;
271
272 error:
273     free(sys);
274     return VLC_EGENERIC;
275 }
276
277 void CloseAvio(vlc_object_t *object)
278 {
279     access_t *access = (access_t*)object;
280     access_sys_t *sys = access->p_sys;
281
282 #if LIBAVFORMAT_VERSION_MAJOR < 54
283     SetupAvioCb(NULL);
284 #endif
285
286     avio_close(sys->context);
287     free(sys);
288 }
289
290 void OutCloseAvio(vlc_object_t *object)
291 {
292     sout_access_out_t *access = (sout_access_out_t*)object;
293     sout_access_out_sys_t *sys = access->p_sys;
294
295 #if LIBAVFORMAT_VERSION_MAJOR < 54
296     SetupAvioCb(NULL);
297 #endif
298
299     avio_close(sys->context);
300     free(sys);
301 }
302
303 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
304 {
305     int r = avio_read(access->p_sys->context, data, size);
306     if (r > 0)
307         access->info.i_pos += r;
308     else
309         access->info.b_eof = true;
310     return r;
311 }
312
313 /*****************************************************************************
314  * Write:
315  *****************************************************************************/
316 static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
317 {
318     access_sys_t *p_sys = (access_sys_t*)p_access->p_sys;
319     size_t i_write = 0;
320
321     while (p_buffer != NULL) {
322         block_t *p_next = p_buffer->p_next;
323
324 #if LIBAVFORMAT_VERSION_MAJOR < 54
325         int written = url_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
326         if (written < 0) {
327             errno = AVUNERROR(written);
328             goto error;
329         }
330         i_write += written;
331 #else
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? */
337             goto error;
338         }
339         i_write += p_buffer->i_buffer;
340 #endif
341
342         block_Release(p_buffer);
343
344         p_buffer = p_next;
345     }
346
347     return i_write;
348
349 error:
350     msg_Err(p_access, "Wrote only %zu bytes (%m)", i_write);
351     block_ChainRelease( p_buffer );
352     return i_write;
353 }
354
355 static int Seek(access_t *access, uint64_t position)
356 {
357     access_sys_t *sys = access->p_sys;
358     int ret;
359
360 #ifndef EOVERFLOW
361 # define EOVERFLOW EFBIG
362 #endif
363
364     if (position > INT64_MAX)
365         ret = AVERROR(EOVERFLOW);
366     else
367         ret = avio_seek(sys->context, position, SEEK_SET);
368     if (ret < 0) {
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)
372             return VLC_EGENERIC;
373     }
374     access->info.i_pos = position;
375     access->info.b_eof = false;
376     return VLC_SUCCESS;
377 }
378
379 static int OutSeek(sout_access_out_t *p_access, off_t i_pos)
380 {
381     sout_access_out_sys_t *sys = p_access->p_sys;
382
383     if (avio_seek(sys->context, i_pos, SEEK_SET) < 0)
384         return VLC_EGENERIC;
385     return VLC_SUCCESS;
386 }
387
388 static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
389 {
390     sout_access_out_sys_t *p_sys = p_access->p_sys;
391
392     VLC_UNUSED(p_sys);
393     switch (i_query) {
394     case ACCESS_OUT_CONTROLS_PACE: {
395         bool *pb = va_arg(args, bool *);
396         //*pb = strcmp(p_access->psz_access, "stream");
397         *pb = false;
398         break;
399     }
400
401     default:
402         return VLC_EGENERIC;
403     }
404     return VLC_SUCCESS;
405 }
406
407 static int Control(access_t *access, int query, va_list args)
408 {
409     access_sys_t *sys = access->p_sys;
410     bool *b;
411
412     switch (query) {
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;
418 #else
419         *b = sys->context->seekable;
420 #endif
421         return VLC_SUCCESS;
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;
426 #else
427         *b = sys->context->read_pause != NULL;
428 #endif
429         return VLC_SUCCESS;
430     case ACCESS_CAN_CONTROL_PACE:
431         b = va_arg(args, bool *);
432         *b = true; /* FIXME */
433         return VLC_SUCCESS;
434     case ACCESS_GET_PTS_DELAY: {
435         int64_t *delay = va_arg(args, int64_t *);
436         *delay = DEFAULT_PTS_DELAY; /* FIXME */
437         return VLC_SUCCESS;
438     }
439     case ACCESS_SET_PAUSE_STATE: {
440         bool is_paused = va_arg(args, int);
441         if (avio_pause(sys->context, is_paused)< 0)
442             return VLC_EGENERIC;
443         return VLC_SUCCESS;
444     }
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:
454     default:
455         return VLC_EGENERIC;
456     }
457 }