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