]> git.sesse.net Git - vlc/blob - modules/access/avio.c
Update LGPL license blurb, choosing v2.1+.
[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_avcodec.h>
33
34 #include "avio.h"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 #ifndef MERGE_FFMPEG
40 vlc_module_begin()
41     AVIO_MODULE
42 vlc_module_end()
43 #endif
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static ssize_t Read   (access_t *, uint8_t *, size_t);
49 static int     Seek   (access_t *, uint64_t);
50 static int     Control(access_t *, int, va_list);
51
52 static int     SetupAvio(access_t *);
53
54 struct access_sys_t {
55     URLContext *context;
56 };
57
58 /* */
59 int OpenAvio(vlc_object_t *object)
60 {
61     access_t *access = (access_t*)object;
62     access_sys_t *sys;
63
64     /* */
65     access->p_sys = sys = malloc(sizeof(*sys));
66     if (!sys)
67         return VLC_ENOMEM;
68
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;
74     }
75
76     /* */
77     vlc_avcodec_lock();
78     av_register_all();
79     vlc_avcodec_unlock();
80
81     /* We accept:
82      * - avio://full_url
83      * - url (only a subset of available protocols).
84      */
85     char *url;
86     if (!strcmp(access->psz_access, "avio"))
87         url = strdup(access->psz_location);
88     else if (asprintf(&url, "%s://%s", access->psz_access,
89                       access->psz_location) < 0)
90         url = NULL;
91
92     if (!url)
93         goto error;
94
95     msg_Dbg(access, "Opening '%s'", url);
96     if (url_open(&sys->context, url, URL_RDONLY) < 0 )
97         sys->context = NULL;
98     free(url);
99
100     if (!sys->context) {
101         msg_Err(access, "Failed to open url using libavformat");
102         goto error;
103     }
104     const int64_t size = url_filesize(sys->context);
105     msg_Dbg(access, "is_streamed=%d size=%"PRIi64, sys->context->is_streamed, size);
106
107     /* */
108     access_InitFields(access);
109     access->info.i_size = size > 0 ? size : 0;
110
111     access->pf_read = Read;
112     access->pf_block = NULL;
113     access->pf_control = Control;
114     access->pf_seek = Seek;
115     access->p_sys = sys;
116
117     return VLC_SUCCESS;
118
119 error:
120     SetupAvio(NULL);
121     free(sys);
122     return VLC_EGENERIC;
123 }
124
125
126 void CloseAvio(vlc_object_t *object)
127 {
128     access_t *access = (access_t*)object;
129     access_sys_t *sys = access->p_sys;
130
131     url_close(sys->context);
132
133     SetupAvio(NULL);
134
135     free(sys);
136 }
137
138
139 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
140 {
141     /* FIXME I am unsure of the meaning of the return value in case
142      * of error.
143      */
144     int r = url_read(access->p_sys->context, data, size);
145     access->info.b_eof = r <= 0;
146     if (r < 0)
147         return -1;
148     access->info.i_pos += r;
149     return r;
150 }
151
152
153 static int Seek(access_t *access, uint64_t position)
154 {
155     access_sys_t *sys = access->p_sys;
156
157     if (position > INT64_MAX ||
158         url_seek(sys->context, position, SEEK_SET) < 0) {
159         msg_Err(access, "Seek to %"PRIu64" failed\n", position);
160         if (access->info.i_size <= 0 || position != access->info.i_size)
161             return VLC_EGENERIC;
162     }
163     access->info.i_pos = position;
164     access->info.b_eof = false;
165     return VLC_SUCCESS;
166 }
167
168
169 static int Control(access_t *access, int query, va_list args)
170 {
171     access_sys_t *sys = access->p_sys;
172
173     switch (query) {
174     case ACCESS_CAN_SEEK:
175     case ACCESS_CAN_FASTSEEK: { /* FIXME how to do that ? */
176         bool *b = va_arg(args, bool *);
177         *b = !sys->context->is_streamed;
178         return VLC_SUCCESS;
179     }
180     case ACCESS_CAN_PAUSE: {
181         bool *b = va_arg(args, bool *);
182         *b = sys->context->prot->url_read_pause != NULL; /* FIXME Unsure */
183         return VLC_SUCCESS;
184     }
185     case ACCESS_CAN_CONTROL_PACE: {
186         bool *b = va_arg(args, bool *);
187         *b = true; /* FIXME */
188         return VLC_SUCCESS;
189     }
190     case ACCESS_GET_PTS_DELAY: {
191         int64_t *delay = va_arg(args, int64_t *);
192         *delay = DEFAULT_PTS_DELAY; /* FIXME */
193     }
194     case ACCESS_SET_PAUSE_STATE: {
195         bool is_paused = va_arg(args, int);
196         if (av_url_read_pause(sys->context, is_paused)< 0)
197             return VLC_EGENERIC;
198         return VLC_SUCCESS;
199     }
200     case ACCESS_GET_TITLE_INFO:
201     case ACCESS_GET_META:
202     case ACCESS_GET_CONTENT_TYPE:
203     case ACCESS_GET_SIGNAL:
204     case ACCESS_SET_TITLE:
205     case ACCESS_SET_SEEKPOINT:
206     case ACCESS_SET_PRIVATE_ID_STATE:
207     case ACCESS_SET_PRIVATE_ID_CA:
208     case ACCESS_GET_PRIVATE_ID_STATE:
209     default:
210         return VLC_EGENERIC;
211     }
212 }
213
214 /* */
215 static vlc_mutex_t avio_lock = VLC_STATIC_MUTEX;
216 static access_t *current_access = NULL;
217
218
219 static int UrlInterruptCallback(void)
220 {
221     assert(current_access);
222     return !vlc_object_alive(current_access);
223 }
224
225
226 static int SetupAvio(access_t *access)
227 {
228     vlc_mutex_lock(&avio_lock);
229     assert(!access != !current_access);
230     if (access && current_access) {
231         vlc_mutex_unlock(&avio_lock);
232         return VLC_EGENERIC;
233     }
234     url_set_interrupt_cb(access ? UrlInterruptCallback : NULL);
235     current_access = access;
236     vlc_mutex_unlock(&avio_lock);
237
238     return VLC_SUCCESS;
239 }
240