Link libavdevice against libavutil, libavcodec, and libavformat
[libav.git] / libavformat / avio.c
CommitLineData
de6d9b64
FB
1/*
2 * Unbuffered io for ffmpeg system
19720f15 3 * Copyright (c) 2001 Fabrice Bellard
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
de6d9b64 21#include "avformat.h"
75e61b0e 22#include "avstring.h"
de6d9b64 23
019ac05a
FB
24static int default_interrupt_cb(void);
25
de6d9b64 26URLProtocol *first_protocol = NULL;
019ac05a 27URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
de6d9b64
FB
28
29int register_protocol(URLProtocol *protocol)
30{
31 URLProtocol **p;
32 p = &first_protocol;
33 while (*p != NULL) p = &(*p)->next;
34 *p = protocol;
35 protocol->next = NULL;
36 return 0;
37}
38
39int url_open(URLContext **puc, const char *filename, int flags)
40{
41 URLContext *uc;
42 URLProtocol *up;
43 const char *p;
44 char proto_str[128], *q;
45 int err;
46
47 p = filename;
48 q = proto_str;
49 while (*p != '\0' && *p != ':') {
5d5fef0a
FB
50 /* protocols can only contain alphabetic chars */
51 if (!isalpha(*p))
52 goto file_proto;
de6d9b64
FB
53 if ((q - proto_str) < sizeof(proto_str) - 1)
54 *q++ = *p;
55 p++;
56 }
a74127c0
FB
57 /* if the protocol has length 1, we consider it is a dos drive */
58 if (*p == '\0' || (q - proto_str) <= 1) {
5d5fef0a 59 file_proto:
de6d9b64
FB
60 strcpy(proto_str, "file");
61 } else {
62 *q = '\0';
63 }
115329f1 64
de6d9b64
FB
65 up = first_protocol;
66 while (up != NULL) {
a74127c0 67 if (!strcmp(proto_str, up->name))
de6d9b64
FB
68 goto found;
69 up = up->next;
70 }
8fa36ae0 71 err = AVERROR(ENOENT);
8a9488b5 72 goto fail;
de6d9b64 73 found:
62181004 74 uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);
8a9488b5 75 if (!uc) {
8fa36ae0 76 err = AVERROR(ENOMEM);
8a9488b5
FB
77 goto fail;
78 }
62181004
RB
79#if LIBAVFORMAT_VERSION_INT >= (52<<16)
80 uc->filename = (char *) &uc[1];
81#endif
f746a046 82 strcpy(uc->filename, filename);
de6d9b64
FB
83 uc->prot = up;
84 uc->flags = flags;
85 uc->is_streamed = 0; /* default = not streamed */
8a9488b5 86 uc->max_packet_size = 0; /* default: stream file */
de6d9b64
FB
87 err = up->url_open(uc, filename, flags);
88 if (err < 0) {
1ea4f593 89 av_free(uc);
de6d9b64
FB
90 *puc = NULL;
91 return err;
92 }
93 *puc = uc;
94 return 0;
8a9488b5
FB
95 fail:
96 *puc = NULL;
97 return err;
de6d9b64
FB
98}
99
100int url_read(URLContext *h, unsigned char *buf, int size)
101{
102 int ret;
103 if (h->flags & URL_WRONLY)
6f3e0b21 104 return AVERROR(EIO);
de6d9b64
FB
105 ret = h->prot->url_read(h, buf, size);
106 return ret;
107}
108
04f46ced 109#if defined(CONFIG_MUXERS) || defined(CONFIG_PROTOCOLS)
de6d9b64
FB
110int url_write(URLContext *h, unsigned char *buf, int size)
111{
112 int ret;
8a9488b5 113 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
6f3e0b21 114 return AVERROR(EIO);
8a9488b5
FB
115 /* avoid sending too big packets */
116 if (h->max_packet_size && size > h->max_packet_size)
6f3e0b21 117 return AVERROR(EIO);
de6d9b64
FB
118 ret = h->prot->url_write(h, buf, size);
119 return ret;
120}
04f46ced 121#endif //CONFIG_MUXERS || CONFIG_PROTOCOLS
de6d9b64
FB
122
123offset_t url_seek(URLContext *h, offset_t pos, int whence)
124{
125 offset_t ret;
126
127 if (!h->prot->url_seek)
8fa36ae0 128 return AVERROR(EPIPE);
de6d9b64
FB
129 ret = h->prot->url_seek(h, pos, whence);
130 return ret;
131}
132
de6d9b64
FB
133int url_close(URLContext *h)
134{
7a813b36 135 int ret = 0;
346db900 136 if (!h) return 0; /* can happen when url_open fails */
de6d9b64 137
7a813b36
AB
138 if (h->prot->url_close)
139 ret = h->prot->url_close(h);
1ea4f593 140 av_free(h);
de6d9b64
FB
141 return ret;
142}
143
144int url_exist(const char *filename)
145{
146 URLContext *h;
147 if (url_open(&h, filename, URL_RDONLY) < 0)
148 return 0;
149 url_close(h);
150 return 1;
151}
152
153offset_t url_filesize(URLContext *h)
154{
155 offset_t pos, size;
115329f1 156
8e287af0
MN
157 size= url_seek(h, 0, AVSEEK_SIZE);
158 if(size<0){
1ae2c5f2 159 pos = url_seek(h, 0, SEEK_CUR);
eabbae73
RB
160 if ((size = url_seek(h, -1, SEEK_END)) < 0)
161 return size;
162 size++;
1ae2c5f2 163 url_seek(h, pos, SEEK_SET);
8e287af0 164 }
de6d9b64
FB
165 return size;
166}
8a9488b5 167
8a9488b5
FB
168int url_get_max_packet_size(URLContext *h)
169{
170 return h->max_packet_size;
171}
f746a046
FB
172
173void url_get_filename(URLContext *h, char *buf, int buf_size)
174{
75e61b0e 175 av_strlcpy(buf, h->filename, buf_size);
f746a046 176}
019ac05a
FB
177
178
179static int default_interrupt_cb(void)
180{
181 return 0;
182}
183
019ac05a
FB
184void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
185{
186 if (!interrupt_cb)
187 interrupt_cb = default_interrupt_cb;
188 url_interrupt_cb = interrupt_cb;
189}