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