2 * Unbuffered io for ffmpeg system
3 * Copyright (c) 2001 Fabrice Bellard
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.
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.
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
21 static int default_interrupt_cb(void);
23 URLProtocol *first_protocol = NULL;
24 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
26 int register_protocol(URLProtocol *protocol)
30 while (*p != NULL) p = &(*p)->next;
32 protocol->next = NULL;
36 int url_open(URLContext **puc, const char *filename, int flags)
41 char proto_str[128], *q;
46 while (*p != '\0' && *p != ':') {
47 /* protocols can only contain alphabetic chars */
50 if ((q - proto_str) < sizeof(proto_str) - 1)
54 /* if the protocol has length 1, we consider it is a dos drive */
55 if (*p == '\0' || (q - proto_str) <= 1) {
57 strcpy(proto_str, "file");
64 if (!strcmp(proto_str, up->name))
71 uc = av_malloc(sizeof(URLContext) + strlen(filename));
76 strcpy(uc->filename, filename);
79 uc->is_streamed = 0; /* default = not streamed */
80 uc->max_packet_size = 0; /* default: stream file */
81 err = up->url_open(uc, filename, flags);
94 int url_read(URLContext *h, unsigned char *buf, int size)
97 if (h->flags & URL_WRONLY)
99 ret = h->prot->url_read(h, buf, size);
103 #ifdef CONFIG_ENCODERS
104 int url_write(URLContext *h, unsigned char *buf, int size)
107 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
109 /* avoid sending too big packets */
110 if (h->max_packet_size && size > h->max_packet_size)
112 ret = h->prot->url_write(h, buf, size);
115 #endif //CONFIG_ENCODERS
117 offset_t url_seek(URLContext *h, offset_t pos, int whence)
121 if (!h->prot->url_seek)
123 ret = h->prot->url_seek(h, pos, whence);
127 int url_close(URLContext *h)
131 ret = h->prot->url_close(h);
136 int url_exist(const char *filename)
139 if (url_open(&h, filename, URL_RDONLY) < 0)
145 offset_t url_filesize(URLContext *h)
149 pos = url_seek(h, 0, SEEK_CUR);
150 size = url_seek(h, 0, SEEK_END);
151 url_seek(h, pos, SEEK_SET);
156 * Return the maximum packet size associated to packetized file
157 * handle. If the file is not packetized (stream like http or file on
158 * disk), then 0 is returned.
160 * @param h file handle
161 * @return maximum packet size in bytes
163 int url_get_max_packet_size(URLContext *h)
165 return h->max_packet_size;
168 void url_get_filename(URLContext *h, char *buf, int buf_size)
170 pstrcpy(buf, buf_size, h->filename);
174 static int default_interrupt_cb(void)
180 * The callback is called in blocking functions to test regulary if
181 * asynchronous interruption is needed. -EINTR is returned in this
182 * case by the interrupted function. 'NULL' means no interrupt
185 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
188 interrupt_cb = default_interrupt_cb;
189 url_interrupt_cb = interrupt_cb;