|
808
|
1 /*
|
|
|
2 * Unbuffered io for ffmpeg system
|
|
|
3 * Copyright (c) 2001 Fabrice Bellard
|
|
|
4 *
|
|
823
|
5 * This library is free software; you can redistribute it and/or
|
|
808
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
|
7 * License as published by the Free Software Foundation; either
|
|
823
|
8 * version 2 of the License, or (at your option) any later version.
|
|
808
|
9 *
|
|
823
|
10 * This library is distributed in the hope that it will be useful,
|
|
808
|
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
|
|
823
|
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
|
|
808
|
18 */
|
|
|
19 #include "avformat.h"
|
|
|
20
|
|
|
21 static int default_interrupt_cb(void);
|
|
|
22
|
|
|
23 URLProtocol *first_protocol = NULL;
|
|
|
24 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
|
|
|
25
|
|
|
26 int 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
|
|
823
|
36 int url_vopen(URLContext **puc, VFSFile *fd)
|
|
|
37 {
|
|
|
38 URLContext *uc;
|
|
|
39 URLProtocol *up;
|
|
|
40 const char *p;
|
|
|
41 char proto_str[128], *q;
|
|
|
42 int err = 0;
|
|
|
43
|
|
|
44 up = first_protocol;
|
|
|
45 uc = av_malloc(sizeof(URLContext) + strlen(fd->uri ? fd->uri : ""));
|
|
|
46 if (!uc) {
|
|
|
47 err = -ENOMEM;
|
|
|
48 goto fail;
|
|
|
49 }
|
|
|
50 strcpy(uc->filename, fd->uri ? fd->uri : "");
|
|
|
51 uc->prot = up;
|
|
|
52 uc->flags = URL_RDONLY;
|
|
|
53 uc->is_streamed = 0; /* default = not streamed */
|
|
|
54 uc->max_packet_size = 0; /* default: stream file */
|
|
|
55 uc->priv_data = fd;
|
|
|
56 if (err < 0) {
|
|
|
57 free(uc);
|
|
|
58 *puc = NULL;
|
|
|
59 return err;
|
|
|
60 }
|
|
|
61 *puc = uc;
|
|
|
62 return 0;
|
|
|
63 fail:
|
|
|
64 *puc = NULL;
|
|
|
65 return err;
|
|
|
66 }
|
|
|
67
|
|
808
|
68 int url_open(URLContext **puc, const char *filename, int flags)
|
|
|
69 {
|
|
|
70 URLContext *uc;
|
|
|
71 URLProtocol *up;
|
|
|
72 const char *p;
|
|
|
73 char proto_str[128], *q;
|
|
|
74 int err;
|
|
|
75
|
|
|
76 up = first_protocol;
|
|
|
77 uc = av_malloc(sizeof(URLContext) + strlen(filename));
|
|
|
78 if (!uc) {
|
|
|
79 err = -ENOMEM;
|
|
|
80 goto fail;
|
|
|
81 }
|
|
|
82 strcpy(uc->filename, filename);
|
|
|
83 uc->prot = up;
|
|
|
84 uc->flags = flags;
|
|
|
85 uc->is_streamed = 0; /* default = not streamed */
|
|
|
86 uc->max_packet_size = 0; /* default: stream file */
|
|
|
87 err = up->url_open(uc, filename, flags);
|
|
|
88 if (err < 0) {
|
|
823
|
89 free(uc);
|
|
808
|
90 *puc = NULL;
|
|
|
91 return err;
|
|
|
92 }
|
|
|
93 *puc = uc;
|
|
|
94 return 0;
|
|
|
95 fail:
|
|
|
96 *puc = NULL;
|
|
|
97 return err;
|
|
|
98 }
|
|
|
99
|
|
|
100 int url_read(URLContext *h, unsigned char *buf, int size)
|
|
|
101 {
|
|
|
102 int ret;
|
|
|
103 if (h->flags & URL_WRONLY)
|
|
823
|
104 return -EIO;
|
|
808
|
105 ret = h->prot->url_read(h, buf, size);
|
|
|
106 return ret;
|
|
|
107 }
|
|
|
108
|
|
|
109 offset_t url_seek(URLContext *h, offset_t pos, int whence)
|
|
|
110 {
|
|
|
111 offset_t ret;
|
|
|
112
|
|
|
113 if (!h->prot->url_seek)
|
|
|
114 return -EPIPE;
|
|
|
115 ret = h->prot->url_seek(h, pos, whence);
|
|
|
116 return ret;
|
|
|
117 }
|
|
|
118
|
|
|
119 int url_close(URLContext *h)
|
|
|
120 {
|
|
823
|
121 int ret;
|
|
808
|
122
|
|
823
|
123 ret = h->prot->url_close(h);
|
|
|
124 free(h);
|
|
|
125
|
|
|
126 return ret;
|
|
808
|
127 }
|
|
|
128
|
|
|
129 int url_exist(const char *filename)
|
|
|
130 {
|
|
|
131 URLContext *h;
|
|
|
132 if (url_open(&h, filename, URL_RDONLY) < 0)
|
|
|
133 return 0;
|
|
|
134 url_close(h);
|
|
|
135 return 1;
|
|
|
136 }
|
|
|
137
|
|
|
138 offset_t url_filesize(URLContext *h)
|
|
|
139 {
|
|
|
140 offset_t pos, size;
|
|
823
|
141
|
|
808
|
142 pos = url_seek(h, 0, SEEK_CUR);
|
|
823
|
143 size = url_seek(h, 0, SEEK_END);
|
|
808
|
144 url_seek(h, pos, SEEK_SET);
|
|
|
145 return size;
|
|
|
146 }
|
|
|
147
|
|
823
|
148 /*
|
|
808
|
149 * Return the maximum packet size associated to packetized file
|
|
|
150 * handle. If the file is not packetized (stream like http or file on
|
|
|
151 * disk), then 0 is returned.
|
|
823
|
152 *
|
|
808
|
153 * @param h file handle
|
|
|
154 * @return maximum packet size in bytes
|
|
|
155 */
|
|
|
156 int url_get_max_packet_size(URLContext *h)
|
|
|
157 {
|
|
|
158 return h->max_packet_size;
|
|
|
159 }
|
|
|
160
|
|
|
161 void url_get_filename(URLContext *h, char *buf, int buf_size)
|
|
|
162 {
|
|
|
163 pstrcpy(buf, buf_size, h->filename);
|
|
|
164 }
|
|
|
165
|
|
|
166
|
|
|
167 static int default_interrupt_cb(void)
|
|
|
168 {
|
|
|
169 return 0;
|
|
|
170 }
|
|
|
171
|
|
823
|
172 /**
|
|
808
|
173 * The callback is called in blocking functions to test regulary if
|
|
|
174 * asynchronous interruption is needed. -EINTR is returned in this
|
|
|
175 * case by the interrupted function. 'NULL' means no interrupt
|
|
823
|
176 * callback is given.
|
|
808
|
177 */
|
|
|
178 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
|
|
|
179 {
|
|
|
180 if (!interrupt_cb)
|
|
|
181 interrupt_cb = default_interrupt_cb;
|
|
|
182 url_interrupt_cb = interrupt_cb;
|
|
|
183 }
|