2c25c8a0594ae9d593aeab27829bfb26854c1771
[libav.git] / libavformat / avio.c
1 /*
2 * Unbuffered io for ffmpeg system
3 * Copyright (c) 2001 Fabrice Bellard
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avformat.h"
20 #include <ctype.h>
21
22 static int default_interrupt_cb(void);
23
24 URLProtocol *first_protocol = NULL;
25 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
26
27 int register_protocol(URLProtocol *protocol)
28 {
29 URLProtocol **p;
30 p = &first_protocol;
31 while (*p != NULL) p = &(*p)->next;
32 *p = protocol;
33 protocol->next = NULL;
34 return 0;
35 }
36
37 int url_open(URLContext **puc, const char *filename, int flags)
38 {
39 URLContext *uc;
40 URLProtocol *up;
41 const char *p;
42 char proto_str[128], *q;
43 int err;
44
45 p = filename;
46 q = proto_str;
47 while (*p != '\0' && *p != ':') {
48 /* protocols can only contain alphabetic chars */
49 if (!isalpha(*p))
50 goto file_proto;
51 if ((q - proto_str) < sizeof(proto_str) - 1)
52 *q++ = *p;
53 p++;
54 }
55 /* if the protocol has length 1, we consider it is a dos drive */
56 if (*p == '\0' || (q - proto_str) <= 1) {
57 file_proto:
58 strcpy(proto_str, "file");
59 } else {
60 *q = '\0';
61 }
62
63 up = first_protocol;
64 while (up != NULL) {
65 if (!strcmp(proto_str, up->name))
66 goto found;
67 up = up->next;
68 }
69 err = -ENOENT;
70 goto fail;
71 found:
72 uc = av_malloc(sizeof(URLContext) + strlen(filename));
73 if (!uc) {
74 err = -ENOMEM;
75 goto fail;
76 }
77 strcpy(uc->filename, filename);
78 uc->prot = up;
79 uc->flags = flags;
80 uc->is_streamed = 0; /* default = not streamed */
81 uc->max_packet_size = 0; /* default: stream file */
82 err = up->url_open(uc, filename, flags);
83 if (err < 0) {
84 av_free(uc);
85 *puc = NULL;
86 return err;
87 }
88 *puc = uc;
89 return 0;
90 fail:
91 *puc = NULL;
92 return err;
93 }
94
95 int url_read(URLContext *h, unsigned char *buf, int size)
96 {
97 int ret;
98 if (h->flags & URL_WRONLY)
99 return -EIO;
100 ret = h->prot->url_read(h, buf, size);
101 return ret;
102 }
103
104 int url_write(URLContext *h, unsigned char *buf, int size)
105 {
106 int ret;
107 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
108 return -EIO;
109 /* avoid sending too big packets */
110 if (h->max_packet_size && size > h->max_packet_size)
111 return -EIO;
112 ret = h->prot->url_write(h, buf, size);
113 return ret;
114 }
115
116 offset_t url_seek(URLContext *h, offset_t pos, int whence)
117 {
118 offset_t ret;
119
120 if (!h->prot->url_seek)
121 return -EPIPE;
122 ret = h->prot->url_seek(h, pos, whence);
123 return ret;
124 }
125
126 int url_close(URLContext *h)
127 {
128 int ret;
129
130 ret = h->prot->url_close(h);
131 av_free(h);
132 return ret;
133 }
134
135 int url_exist(const char *filename)
136 {
137 URLContext *h;
138 if (url_open(&h, filename, URL_RDONLY) < 0)
139 return 0;
140 url_close(h);
141 return 1;
142 }
143
144 offset_t url_filesize(URLContext *h)
145 {
146 offset_t pos, size;
147
148 pos = url_seek(h, 0, SEEK_CUR);
149 size = url_seek(h, 0, SEEK_END);
150 url_seek(h, pos, SEEK_SET);
151 return size;
152 }
153
154 /*
155 * Return the maximum packet size associated to packetized file
156 * handle. If the file is not packetized (stream like http or file on
157 * disk), then 0 is returned.
158 *
159 * @param h file handle
160 * @return maximum packet size in bytes
161 */
162 int url_get_max_packet_size(URLContext *h)
163 {
164 return h->max_packet_size;
165 }
166
167 void url_get_filename(URLContext *h, char *buf, int buf_size)
168 {
169 pstrcpy(buf, buf_size, h->filename);
170 }
171
172
173 static int default_interrupt_cb(void)
174 {
175 return 0;
176 }
177
178 /**
179 * The callback is called in blocking functions to test regulary if
180 * asynchronous interruption is needed. -EINTR is returned in this
181 * case by the interrupted function. 'NULL' means no interrupt
182 * callback is given.
183 */
184 void 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 }