]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
Split url_open and url_open_protocol into url_alloc and url_connect
[ffmpeg] / libavformat / avio.c
1 /*
2  * Unbuffered io for ffmpeg system
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /* needed for usleep() */
23 #define _XOPEN_SOURCE 600
24 #include <unistd.h>
25 #include "libavutil/avstring.h"
26 #include "libavcodec/opt.h"
27 #include "os_support.h"
28 #include "avformat.h"
29 #if CONFIG_NETWORK
30 #include "network.h"
31 #endif
32
33 #if LIBAVFORMAT_VERSION_MAJOR >= 53
34 /** @name Logging context. */
35 /*@{*/
36 static const char *urlcontext_to_name(void *ptr)
37 {
38     URLContext *h = (URLContext *)ptr;
39     if(h->prot) return h->prot->name;
40     else        return "NULL";
41 }
42 static const AVOption options[] = {{NULL}};
43 static const AVClass urlcontext_class =
44         { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
45 /*@}*/
46 #endif
47
48 static int default_interrupt_cb(void);
49
50 URLProtocol *first_protocol = NULL;
51 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
52
53 URLProtocol *av_protocol_next(URLProtocol *p)
54 {
55     if(p) return p->next;
56     else  return first_protocol;
57 }
58
59 int av_register_protocol2(URLProtocol *protocol, int size)
60 {
61     URLProtocol **p;
62     if (size < sizeof(URLProtocol)) {
63         URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
64         memcpy(temp, protocol, size);
65         protocol = temp;
66     }
67     p = &first_protocol;
68     while (*p != NULL) p = &(*p)->next;
69     *p = protocol;
70     protocol->next = NULL;
71     return 0;
72 }
73
74 #if LIBAVFORMAT_VERSION_MAJOR < 53
75 /* The layout of URLProtocol as of when major was bumped to 52 */
76 struct URLProtocol_compat {
77     const char *name;
78     int (*url_open)(URLContext *h, const char *filename, int flags);
79     int (*url_read)(URLContext *h, unsigned char *buf, int size);
80     int (*url_write)(URLContext *h, unsigned char *buf, int size);
81     int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
82     int (*url_close)(URLContext *h);
83     struct URLProtocol *next;
84 };
85
86 int av_register_protocol(URLProtocol *protocol)
87 {
88     return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
89 }
90
91 int register_protocol(URLProtocol *protocol)
92 {
93     return av_register_protocol(protocol);
94 }
95 #endif
96
97 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
98                        const char *filename, int flags)
99 {
100     URLContext *uc;
101     int err;
102
103 #if CONFIG_NETWORK
104     if (!ff_network_init())
105         return AVERROR(EIO);
106 #endif
107     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
108     if (!uc) {
109         err = AVERROR(ENOMEM);
110         goto fail;
111     }
112 #if LIBAVFORMAT_VERSION_MAJOR >= 53
113     uc->av_class = &urlcontext_class;
114 #endif
115     uc->filename = (char *) &uc[1];
116     strcpy(uc->filename, filename);
117     uc->prot = up;
118     uc->flags = flags;
119     uc->is_streamed = 0; /* default = not streamed */
120     uc->max_packet_size = 0; /* default: stream file */
121
122     *puc = uc;
123     return 0;
124  fail:
125     *puc = NULL;
126 #if CONFIG_NETWORK
127     ff_network_close();
128 #endif
129     return err;
130 }
131
132 int url_connect(URLContext* uc)
133 {
134     int err = uc->prot->url_open(uc, uc->filename, uc->flags);
135     if (err)
136         return err;
137     uc->is_connected = 1;
138     //We must be careful here as url_seek() could be slow, for example for http
139     if(   (uc->flags & (URL_WRONLY | URL_RDWR))
140        || !strcmp(uc->prot->name, "file"))
141         if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
142             uc->is_streamed= 1;
143     return 0;
144 }
145
146 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
147                        const char *filename, int flags)
148 {
149     int ret;
150
151     ret = url_alloc_for_protocol(puc, up, filename, flags);
152     if (ret)
153         goto fail;
154     ret = url_connect(*puc);
155     if (!ret)
156         return 0;
157  fail:
158     url_close(*puc);
159     *puc = NULL;
160     return ret;
161 }
162
163 int url_alloc(URLContext **puc, const char *filename, int flags)
164 {
165     URLProtocol *up;
166     const char *p;
167     char proto_str[128], *q;
168
169     p = filename;
170     q = proto_str;
171     while (*p != '\0' && *p != ':') {
172         /* protocols can only contain alphabetic chars */
173         if (!isalpha(*p))
174             goto file_proto;
175         if ((q - proto_str) < sizeof(proto_str) - 1)
176             *q++ = *p;
177         p++;
178     }
179     /* if the protocol has length 1, we consider it is a dos drive */
180     if (*p == '\0' || is_dos_path(filename)) {
181     file_proto:
182         strcpy(proto_str, "file");
183     } else {
184         *q = '\0';
185     }
186
187     up = first_protocol;
188     while (up != NULL) {
189         if (!strcmp(proto_str, up->name))
190             return url_alloc_for_protocol (puc, up, filename, flags);
191         up = up->next;
192     }
193     *puc = NULL;
194     return AVERROR(ENOENT);
195 }
196
197 int url_open(URLContext **puc, const char *filename, int flags)
198 {
199     int ret = url_alloc(puc, filename, flags);
200     if (ret)
201         return ret;
202     ret = url_connect(*puc);
203     if (!ret)
204         return 0;
205  fail:
206     url_close(*puc);
207     *puc = NULL;
208     return ret;
209 }
210
211 int url_read(URLContext *h, unsigned char *buf, int size)
212 {
213     int ret;
214     if (h->flags & URL_WRONLY)
215         return AVERROR(EIO);
216     ret = h->prot->url_read(h, buf, size);
217     return ret;
218 }
219
220 int url_read_complete(URLContext *h, unsigned char *buf, int size)
221 {
222     int ret, len;
223     int fast_retries = 5;
224
225     len = 0;
226     while (len < size) {
227         ret = url_read(h, buf+len, size-len);
228         if (ret == AVERROR(EAGAIN)) {
229             ret = 0;
230             if (fast_retries)
231                 fast_retries--;
232             else
233                 usleep(1000);
234         } else if (ret < 1)
235             return ret < 0 ? ret : len;
236         if (ret)
237            fast_retries = FFMAX(fast_retries, 2);
238         len += ret;
239     }
240     return len;
241 }
242
243 int url_write(URLContext *h, const unsigned char *buf, int size)
244 {
245     int ret;
246     if (!(h->flags & (URL_WRONLY | URL_RDWR)))
247         return AVERROR(EIO);
248     /* avoid sending too big packets */
249     if (h->max_packet_size && size > h->max_packet_size)
250         return AVERROR(EIO);
251     ret = h->prot->url_write(h, buf, size);
252     return ret;
253 }
254
255 int64_t url_seek(URLContext *h, int64_t pos, int whence)
256 {
257     int64_t ret;
258
259     if (!h->prot->url_seek)
260         return AVERROR(ENOSYS);
261     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
262     return ret;
263 }
264
265 int url_close(URLContext *h)
266 {
267     int ret = 0;
268     if (!h) return 0; /* can happen when url_open fails */
269
270     if (h->is_connected && h->prot->url_close)
271         ret = h->prot->url_close(h);
272 #if CONFIG_NETWORK
273     ff_network_close();
274 #endif
275     av_free(h);
276     return ret;
277 }
278
279 int url_exist(const char *filename)
280 {
281     URLContext *h;
282     if (url_open(&h, filename, URL_RDONLY) < 0)
283         return 0;
284     url_close(h);
285     return 1;
286 }
287
288 int64_t url_filesize(URLContext *h)
289 {
290     int64_t pos, size;
291
292     size= url_seek(h, 0, AVSEEK_SIZE);
293     if(size<0){
294         pos = url_seek(h, 0, SEEK_CUR);
295         if ((size = url_seek(h, -1, SEEK_END)) < 0)
296             return size;
297         size++;
298         url_seek(h, pos, SEEK_SET);
299     }
300     return size;
301 }
302
303 int url_get_file_handle(URLContext *h)
304 {
305     if (!h->prot->url_get_file_handle)
306         return -1;
307     return h->prot->url_get_file_handle(h);
308 }
309
310 int url_get_max_packet_size(URLContext *h)
311 {
312     return h->max_packet_size;
313 }
314
315 void url_get_filename(URLContext *h, char *buf, int buf_size)
316 {
317     av_strlcpy(buf, h->filename, buf_size);
318 }
319
320
321 static int default_interrupt_cb(void)
322 {
323     return 0;
324 }
325
326 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
327 {
328     if (!interrupt_cb)
329         interrupt_cb = default_interrupt_cb;
330     url_interrupt_cb = interrupt_cb;
331 }
332
333 int av_url_read_pause(URLContext *h, int pause)
334 {
335     if (!h->prot->url_read_pause)
336         return AVERROR(ENOSYS);
337     return h->prot->url_read_pause(h, pause);
338 }
339
340 int64_t av_url_read_seek(URLContext *h,
341         int stream_index, int64_t timestamp, int flags)
342 {
343     if (!h->prot->url_read_seek)
344         return AVERROR(ENOSYS);
345     return h->prot->url_read_seek(h, stream_index, timestamp, flags);
346 }