878
|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 */
|
|
19 #include "avformat.h"
|
|
20 #include "cutils.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_vopen(URLContext **puc, VFSFile *fd)
|
|
38 {
|
|
39 URLContext *uc;
|
|
40 URLProtocol *up;
|
|
41 int err = 0;
|
|
42
|
|
43 up = first_protocol;
|
|
44 uc = av_malloc(sizeof(URLContext) + strlen(fd->uri ? fd->uri : ""));
|
|
45 if (!uc) {
|
|
46 err = -ENOMEM;
|
|
47 goto fail;
|
|
48 }
|
|
49 strcpy(uc->filename, fd->uri ? fd->uri : "");
|
|
50 uc->prot = up;
|
|
51 uc->flags = URL_RDONLY;
|
|
52 uc->is_streamed = 0; /* default = not streamed */
|
|
53 uc->max_packet_size = 0; /* default: stream file */
|
|
54 uc->priv_data = fd;
|
|
55 if (err < 0) {
|
|
56 free(uc);
|
|
57 *puc = NULL;
|
|
58 return err;
|
|
59 }
|
|
60 *puc = uc;
|
|
61 return 0;
|
|
62 fail:
|
|
63 *puc = NULL;
|
|
64 return err;
|
|
65 }
|
|
66
|
|
67 int url_open(URLContext **puc, const char *filename, int flags)
|
|
68 {
|
|
69 URLContext *uc;
|
|
70 URLProtocol *up;
|
|
71 int err;
|
|
72
|
|
73 up = first_protocol;
|
|
74 uc = av_malloc(sizeof(URLContext) + strlen(filename));
|
|
75 if (!uc) {
|
|
76 err = -ENOMEM;
|
|
77 goto fail;
|
|
78 }
|
|
79 strcpy(uc->filename, filename);
|
|
80 uc->prot = up;
|
|
81 uc->flags = flags;
|
|
82 uc->is_streamed = 0; /* default = not streamed */
|
|
83 uc->max_packet_size = 0; /* default: stream file */
|
|
84 err = up->url_open(uc, filename, flags);
|
|
85 if (err < 0) {
|
|
86 free(uc);
|
|
87 *puc = NULL;
|
|
88 return err;
|
|
89 }
|
|
90 *puc = uc;
|
|
91 return 0;
|
|
92 fail:
|
|
93 *puc = NULL;
|
|
94 return err;
|
|
95 }
|
|
96
|
|
97 int url_read(URLContext *h, unsigned char *buf, int size)
|
|
98 {
|
|
99 int ret;
|
|
100 if (h->flags & URL_WRONLY)
|
|
101 return -EIO;
|
|
102 ret = h->prot->url_read(h, buf, size);
|
|
103 return ret;
|
|
104 }
|
|
105
|
|
106 offset_t url_seek(URLContext *h, offset_t pos, int whence)
|
|
107 {
|
|
108 offset_t ret;
|
|
109
|
|
110 if (!h->prot->url_seek)
|
|
111 return -EPIPE;
|
|
112 ret = h->prot->url_seek(h, pos, whence);
|
|
113 return ret;
|
|
114 }
|
|
115
|
|
116 int url_close(URLContext *h)
|
|
117 {
|
|
118 int ret;
|
|
119
|
|
120 ret = h->prot->url_close(h);
|
|
121 free(h);
|
|
122
|
|
123 return ret;
|
|
124 }
|
|
125
|
|
126 int url_exist(const char *filename)
|
|
127 {
|
|
128 URLContext *h;
|
|
129 if (url_open(&h, filename, URL_RDONLY) < 0)
|
|
130 return 0;
|
|
131 url_close(h);
|
|
132 return 1;
|
|
133 }
|
|
134
|
|
135 offset_t url_filesize(URLContext *h)
|
|
136 {
|
|
137 offset_t pos, size;
|
|
138
|
|
139 pos = url_seek(h, 0, SEEK_CUR);
|
|
140 size = url_seek(h, 0, SEEK_END);
|
|
141 url_seek(h, pos, SEEK_SET);
|
|
142 return size;
|
|
143 }
|
|
144
|
|
145 /*
|
|
146 * Return the maximum packet size associated to packetized file
|
|
147 * handle. If the file is not packetized (stream like http or file on
|
|
148 * disk), then 0 is returned.
|
|
149 *
|
|
150 * @param h file handle
|
|
151 * @return maximum packet size in bytes
|
|
152 */
|
|
153 int url_get_max_packet_size(URLContext *h)
|
|
154 {
|
|
155 return h->max_packet_size;
|
|
156 }
|
|
157
|
|
158 void url_get_filename(URLContext *h, char *buf, int buf_size)
|
|
159 {
|
|
160 pstrcpy(buf, buf_size, h->filename);
|
|
161 }
|
|
162
|
|
163
|
|
164 static int default_interrupt_cb(void)
|
|
165 {
|
|
166 return 0;
|
|
167 }
|
|
168
|
|
169 /**
|
|
170 * The callback is called in blocking functions to test regulary if
|
|
171 * asynchronous interruption is needed. -EINTR is returned in this
|
|
172 * case by the interrupted function. 'NULL' means no interrupt
|
|
173 * callback is given.
|
|
174 */
|
|
175 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
|
|
176 {
|
|
177 if (!interrupt_cb)
|
|
178 interrupt_cb = default_interrupt_cb;
|
|
179 url_interrupt_cb = interrupt_cb;
|
|
180 }
|