00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #define _XOPEN_SOURCE 600
00024 #include <unistd.h>
00025 #include "libavutil/avstring.h"
00026 #include "libavutil/opt.h"
00027 #include "os_support.h"
00028 #include "avformat.h"
00029 #if CONFIG_NETWORK
00030 #include "network.h"
00031 #endif
00032
00033 #if FF_API_URL_CLASS
00034
00036 static const char *urlcontext_to_name(void *ptr)
00037 {
00038 URLContext *h = (URLContext *)ptr;
00039 if(h->prot) return h->prot->name;
00040 else return "NULL";
00041 }
00042 static const AVOption options[] = {{NULL}};
00043 static const AVClass urlcontext_class =
00044 { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
00046 #endif
00047
00048 static int default_interrupt_cb(void);
00049
00050 URLProtocol *first_protocol = NULL;
00051 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
00052
00053 URLProtocol *av_protocol_next(URLProtocol *p)
00054 {
00055 if(p) return p->next;
00056 else return first_protocol;
00057 }
00058
00059 int av_register_protocol2(URLProtocol *protocol, int size)
00060 {
00061 URLProtocol **p;
00062 if (size < sizeof(URLProtocol)) {
00063 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
00064 memcpy(temp, protocol, size);
00065 protocol = temp;
00066 }
00067 p = &first_protocol;
00068 while (*p != NULL) p = &(*p)->next;
00069 *p = protocol;
00070 protocol->next = NULL;
00071 return 0;
00072 }
00073
00074 #if FF_API_REGISTER_PROTOCOL
00075
00076 struct URLProtocol_compat {
00077 const char *name;
00078 int (*url_open)(URLContext *h, const char *filename, int flags);
00079 int (*url_read)(URLContext *h, unsigned char *buf, int size);
00080 int (*url_write)(URLContext *h, unsigned char *buf, int size);
00081 int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
00082 int (*url_close)(URLContext *h);
00083 struct URLProtocol *next;
00084 };
00085
00086 int av_register_protocol(URLProtocol *protocol)
00087 {
00088 return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
00089 }
00090
00091 int register_protocol(URLProtocol *protocol)
00092 {
00093 return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
00094 }
00095 #endif
00096
00097 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
00098 const char *filename, int flags)
00099 {
00100 URLContext *uc;
00101 int err;
00102
00103 #if CONFIG_NETWORK
00104 if (!ff_network_init())
00105 return AVERROR(EIO);
00106 #endif
00107 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
00108 if (!uc) {
00109 err = AVERROR(ENOMEM);
00110 goto fail;
00111 }
00112 #if FF_API_URL_CLASS
00113 uc->av_class = &urlcontext_class;
00114 #endif
00115 uc->filename = (char *) &uc[1];
00116 strcpy(uc->filename, filename);
00117 uc->prot = up;
00118 uc->flags = flags;
00119 uc->is_streamed = 0;
00120 uc->max_packet_size = 0;
00121 if (up->priv_data_size) {
00122 uc->priv_data = av_mallocz(up->priv_data_size);
00123 if (up->priv_data_class) {
00124 *(const AVClass**)uc->priv_data = up->priv_data_class;
00125 av_opt_set_defaults(uc->priv_data);
00126 }
00127 }
00128
00129 *puc = uc;
00130 return 0;
00131 fail:
00132 *puc = NULL;
00133 #if CONFIG_NETWORK
00134 ff_network_close();
00135 #endif
00136 return err;
00137 }
00138
00139 int url_connect(URLContext* uc)
00140 {
00141 int err = uc->prot->url_open(uc, uc->filename, uc->flags);
00142 if (err)
00143 return err;
00144 uc->is_connected = 1;
00145
00146 if( (uc->flags & (URL_WRONLY | URL_RDWR))
00147 || !strcmp(uc->prot->name, "file"))
00148 if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
00149 uc->is_streamed= 1;
00150 return 0;
00151 }
00152
00153 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
00154 const char *filename, int flags)
00155 {
00156 int ret;
00157
00158 ret = url_alloc_for_protocol(puc, up, filename, flags);
00159 if (ret)
00160 goto fail;
00161 ret = url_connect(*puc);
00162 if (!ret)
00163 return 0;
00164 fail:
00165 url_close(*puc);
00166 *puc = NULL;
00167 return ret;
00168 }
00169
00170 #define URL_SCHEME_CHARS \
00171 "abcdefghijklmnopqrstuvwxyz" \
00172 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
00173 "0123456789+-."
00174
00175 int url_alloc(URLContext **puc, const char *filename, int flags)
00176 {
00177 URLProtocol *up;
00178 char proto_str[128], proto_nested[128], *ptr;
00179 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
00180
00181 if (filename[proto_len] != ':' || is_dos_path(filename))
00182 strcpy(proto_str, "file");
00183 else
00184 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
00185
00186 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
00187 if ((ptr = strchr(proto_nested, '+')))
00188 *ptr = '\0';
00189
00190 up = first_protocol;
00191 while (up != NULL) {
00192 if (!strcmp(proto_str, up->name))
00193 return url_alloc_for_protocol (puc, up, filename, flags);
00194 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
00195 !strcmp(proto_nested, up->name))
00196 return url_alloc_for_protocol (puc, up, filename, flags);
00197 up = up->next;
00198 }
00199 *puc = NULL;
00200 return AVERROR(ENOENT);
00201 }
00202
00203 int url_open(URLContext **puc, const char *filename, int flags)
00204 {
00205 int ret = url_alloc(puc, filename, flags);
00206 if (ret)
00207 return ret;
00208 ret = url_connect(*puc);
00209 if (!ret)
00210 return 0;
00211 url_close(*puc);
00212 *puc = NULL;
00213 return ret;
00214 }
00215
00216 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
00217 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
00218 {
00219 int ret, len;
00220 int fast_retries = 5;
00221
00222 len = 0;
00223 while (len < size_min) {
00224 ret = transfer_func(h, buf+len, size-len);
00225 if (ret == AVERROR(EINTR))
00226 continue;
00227 if (h->flags & URL_FLAG_NONBLOCK)
00228 return ret;
00229 if (ret == AVERROR(EAGAIN)) {
00230 ret = 0;
00231 if (fast_retries)
00232 fast_retries--;
00233 else
00234 usleep(1000);
00235 } else if (ret < 1)
00236 return ret < 0 ? ret : len;
00237 if (ret)
00238 fast_retries = FFMAX(fast_retries, 2);
00239 len += ret;
00240 if (url_interrupt_cb())
00241 return AVERROR(EINTR);
00242 }
00243 return len;
00244 }
00245
00246 int url_read(URLContext *h, unsigned char *buf, int size)
00247 {
00248 if (h->flags & URL_WRONLY)
00249 return AVERROR(EIO);
00250 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
00251 }
00252
00253 int url_read_complete(URLContext *h, unsigned char *buf, int size)
00254 {
00255 if (h->flags & URL_WRONLY)
00256 return AVERROR(EIO);
00257 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
00258 }
00259
00260 int url_write(URLContext *h, const unsigned char *buf, int size)
00261 {
00262 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
00263 return AVERROR(EIO);
00264
00265 if (h->max_packet_size && size > h->max_packet_size)
00266 return AVERROR(EIO);
00267
00268 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
00269 }
00270
00271 int64_t url_seek(URLContext *h, int64_t pos, int whence)
00272 {
00273 int64_t ret;
00274
00275 if (!h->prot->url_seek)
00276 return AVERROR(ENOSYS);
00277 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
00278 return ret;
00279 }
00280
00281 int url_close(URLContext *h)
00282 {
00283 int ret = 0;
00284 if (!h) return 0;
00285
00286 if (h->is_connected && h->prot->url_close)
00287 ret = h->prot->url_close(h);
00288 #if CONFIG_NETWORK
00289 ff_network_close();
00290 #endif
00291 if (h->prot->priv_data_size)
00292 av_free(h->priv_data);
00293 av_free(h);
00294 return ret;
00295 }
00296
00297 int url_exist(const char *filename)
00298 {
00299 URLContext *h;
00300 if (url_open(&h, filename, URL_RDONLY) < 0)
00301 return 0;
00302 url_close(h);
00303 return 1;
00304 }
00305
00306 int64_t url_filesize(URLContext *h)
00307 {
00308 int64_t pos, size;
00309
00310 size= url_seek(h, 0, AVSEEK_SIZE);
00311 if(size<0){
00312 pos = url_seek(h, 0, SEEK_CUR);
00313 if ((size = url_seek(h, -1, SEEK_END)) < 0)
00314 return size;
00315 size++;
00316 url_seek(h, pos, SEEK_SET);
00317 }
00318 return size;
00319 }
00320
00321 int url_get_file_handle(URLContext *h)
00322 {
00323 if (!h->prot->url_get_file_handle)
00324 return -1;
00325 return h->prot->url_get_file_handle(h);
00326 }
00327
00328 int url_get_max_packet_size(URLContext *h)
00329 {
00330 return h->max_packet_size;
00331 }
00332
00333 void url_get_filename(URLContext *h, char *buf, int buf_size)
00334 {
00335 av_strlcpy(buf, h->filename, buf_size);
00336 }
00337
00338
00339 static int default_interrupt_cb(void)
00340 {
00341 return 0;
00342 }
00343
00344 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
00345 {
00346 if (!interrupt_cb)
00347 interrupt_cb = default_interrupt_cb;
00348 url_interrupt_cb = interrupt_cb;
00349 }
00350
00351 int av_url_read_pause(URLContext *h, int pause)
00352 {
00353 if (!h->prot->url_read_pause)
00354 return AVERROR(ENOSYS);
00355 return h->prot->url_read_pause(h, pause);
00356 }
00357
00358 int64_t av_url_read_seek(URLContext *h,
00359 int stream_index, int64_t timestamp, int flags)
00360 {
00361 if (!h->prot->url_read_seek)
00362 return AVERROR(ENOSYS);
00363 return h->prot->url_read_seek(h, stream_index, timestamp, flags);
00364 }