00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <unistd.h>
00023
00024 #include "libavutil/avstring.h"
00025 #include "libavutil/dict.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 #include "url.h"
00033
00034 static URLProtocol *first_protocol = NULL;
00035
00036 URLProtocol *ffurl_protocol_next(URLProtocol *prev)
00037 {
00038 return prev ? prev->next : first_protocol;
00039 }
00040
00043 static const char *urlcontext_to_name(void *ptr)
00044 {
00045 URLContext *h = (URLContext *)ptr;
00046 if(h->prot) return h->prot->name;
00047 else return "NULL";
00048 }
00049
00050 static void *urlcontext_child_next(void *obj, void *prev)
00051 {
00052 URLContext *h = obj;
00053 if (!prev && h->priv_data && h->prot->priv_data_class)
00054 return h->priv_data;
00055 return NULL;
00056 }
00057
00058 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
00059 {
00060 URLProtocol *p = NULL;
00061
00062
00063 while (prev && (p = ffurl_protocol_next(p)))
00064 if (p->priv_data_class == prev)
00065 break;
00066
00067
00068 while (p = ffurl_protocol_next(p))
00069 if (p->priv_data_class)
00070 return p->priv_data_class;
00071 return NULL;
00072
00073 }
00074
00075 static const AVOption options[] = {{NULL}};
00076 const AVClass ffurl_context_class = {
00077 .class_name = "URLContext",
00078 .item_name = urlcontext_to_name,
00079 .option = options,
00080 .version = LIBAVUTIL_VERSION_INT,
00081 .child_next = urlcontext_child_next,
00082 .child_class_next = urlcontext_child_class_next,
00083 };
00087 #if FF_API_OLD_INTERRUPT_CB
00088 static int default_interrupt_cb(void);
00089 int (*url_interrupt_cb)(void) = default_interrupt_cb;
00090 #endif
00091
00092 URLProtocol *av_protocol_next(URLProtocol *p)
00093 {
00094 return ffurl_protocol_next(p);
00095 }
00096
00097 const char *avio_enum_protocols(void **opaque, int output)
00098 {
00099 URLProtocol **p = opaque;
00100 *p = ffurl_protocol_next(*p);
00101 if (!*p) return NULL;
00102 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
00103 return (*p)->name;
00104 return avio_enum_protocols(opaque, output);
00105 }
00106
00107 int ffurl_register_protocol(URLProtocol *protocol, int size)
00108 {
00109 URLProtocol **p;
00110 if (size < sizeof(URLProtocol)) {
00111 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
00112 memcpy(temp, protocol, size);
00113 protocol = temp;
00114 }
00115 p = &first_protocol;
00116 while (*p != NULL) p = &(*p)->next;
00117 *p = protocol;
00118 protocol->next = NULL;
00119 return 0;
00120 }
00121
00122 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
00123 const char *filename, int flags,
00124 const AVIOInterruptCB *int_cb)
00125 {
00126 URLContext *uc;
00127 int err;
00128
00129 #if CONFIG_NETWORK
00130 if (!ff_network_init())
00131 return AVERROR(EIO);
00132 #endif
00133 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
00134 if (!uc) {
00135 err = AVERROR(ENOMEM);
00136 goto fail;
00137 }
00138 uc->av_class = &ffurl_context_class;
00139 uc->filename = (char *) &uc[1];
00140 strcpy(uc->filename, filename);
00141 uc->prot = up;
00142 uc->flags = flags;
00143 uc->is_streamed = 0;
00144 uc->max_packet_size = 0;
00145 if (up->priv_data_size) {
00146 uc->priv_data = av_mallocz(up->priv_data_size);
00147 if (up->priv_data_class) {
00148 int proto_len= strlen(up->name);
00149 char *start = strchr(uc->filename, ',');
00150 *(const AVClass**)uc->priv_data = up->priv_data_class;
00151 av_opt_set_defaults(uc->priv_data);
00152 if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
00153 int ret= 0;
00154 char *p= start;
00155 char sep= *++p;
00156 char *key, *val;
00157 p++;
00158 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
00159 *val= *key= 0;
00160 ret= av_opt_set(uc->priv_data, p, key+1, 0);
00161 if (ret == AVERROR_OPTION_NOT_FOUND)
00162 av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
00163 *val= *key= sep;
00164 p= val+1;
00165 }
00166 if(ret<0 || p!=key){
00167 av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
00168 av_freep(&uc->priv_data);
00169 av_freep(&uc);
00170 goto fail;
00171 }
00172 memmove(start, key+1, strlen(key));
00173 }
00174 }
00175 }
00176 if (int_cb)
00177 uc->interrupt_callback = *int_cb;
00178
00179 *puc = uc;
00180 return 0;
00181 fail:
00182 *puc = NULL;
00183 #if CONFIG_NETWORK
00184 ff_network_close();
00185 #endif
00186 return err;
00187 }
00188
00189 int ffurl_connect(URLContext* uc, AVDictionary **options)
00190 {
00191 int err =
00192 #if !FF_API_OLD_AVIO
00193 uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
00194 #endif
00195 uc->prot->url_open(uc, uc->filename, uc->flags);
00196 if (err)
00197 return err;
00198 uc->is_connected = 1;
00199
00200 if( (uc->flags & AVIO_FLAG_WRITE)
00201 || !strcmp(uc->prot->name, "file"))
00202 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
00203 uc->is_streamed= 1;
00204 return 0;
00205 }
00206
00207 #if FF_API_OLD_AVIO
00208 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
00209 const char *filename, int flags)
00210 {
00211 int ret;
00212
00213 ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
00214 if (ret)
00215 goto fail;
00216 ret = ffurl_connect(*puc, NULL);
00217 if (!ret)
00218 return 0;
00219 fail:
00220 ffurl_close(*puc);
00221 *puc = NULL;
00222 return ret;
00223 }
00224 int url_alloc(URLContext **puc, const char *filename, int flags)
00225 {
00226 return ffurl_alloc(puc, filename, flags, NULL);
00227 }
00228 int url_connect(URLContext* uc)
00229 {
00230 return ffurl_connect(uc, NULL);
00231 }
00232 int url_open(URLContext **puc, const char *filename, int flags)
00233 {
00234 return ffurl_open(puc, filename, flags, NULL, NULL);
00235 }
00236 int url_read(URLContext *h, unsigned char *buf, int size)
00237 {
00238 return ffurl_read(h, buf, size);
00239 }
00240 int url_read_complete(URLContext *h, unsigned char *buf, int size)
00241 {
00242 return ffurl_read_complete(h, buf, size);
00243 }
00244 int url_write(URLContext *h, const unsigned char *buf, int size)
00245 {
00246 return ffurl_write(h, buf, size);
00247 }
00248 int64_t url_seek(URLContext *h, int64_t pos, int whence)
00249 {
00250 return ffurl_seek(h, pos, whence);
00251 }
00252 int url_close(URLContext *h)
00253 {
00254 return ffurl_close(h);
00255 }
00256 int64_t url_filesize(URLContext *h)
00257 {
00258 return ffurl_size(h);
00259 }
00260 int url_get_file_handle(URLContext *h)
00261 {
00262 return ffurl_get_file_handle(h);
00263 }
00264 int url_get_max_packet_size(URLContext *h)
00265 {
00266 return h->max_packet_size;
00267 }
00268 void url_get_filename(URLContext *h, char *buf, int buf_size)
00269 {
00270 av_strlcpy(buf, h->filename, buf_size);
00271 }
00272 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
00273 {
00274 avio_set_interrupt_cb(interrupt_cb);
00275 }
00276 int av_register_protocol2(URLProtocol *protocol, int size)
00277 {
00278 return ffurl_register_protocol(protocol, size);
00279 }
00280 #endif
00281
00282 #define URL_SCHEME_CHARS \
00283 "abcdefghijklmnopqrstuvwxyz" \
00284 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
00285 "0123456789+-."
00286
00287 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
00288 const AVIOInterruptCB *int_cb)
00289 {
00290 URLProtocol *up = NULL;
00291 char proto_str[128], proto_nested[128], *ptr;
00292 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
00293
00294 if (!first_protocol) {
00295 av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
00296 "Missing call to av_register_all()?\n");
00297 }
00298
00299 if (filename[proto_len] != ':' && filename[proto_len] != ',' || is_dos_path(filename))
00300 strcpy(proto_str, "file");
00301 else
00302 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
00303
00304 if ((ptr = strchr(proto_str, ',')))
00305 *ptr = '\0';
00306 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
00307 if ((ptr = strchr(proto_nested, '+')))
00308 *ptr = '\0';
00309
00310 while (up = ffurl_protocol_next(up)) {
00311 if (!strcmp(proto_str, up->name))
00312 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
00313 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
00314 !strcmp(proto_nested, up->name))
00315 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
00316 }
00317 *puc = NULL;
00318 return AVERROR(ENOENT);
00319 }
00320
00321 int ffurl_open(URLContext **puc, const char *filename, int flags,
00322 const AVIOInterruptCB *int_cb, AVDictionary **options)
00323 {
00324 int ret = ffurl_alloc(puc, filename, flags, int_cb);
00325 if (ret)
00326 return ret;
00327 if (options && (*puc)->prot->priv_data_class &&
00328 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
00329 goto fail;
00330 ret = ffurl_connect(*puc, options);
00331 if (!ret)
00332 return 0;
00333 fail:
00334 ffurl_close(*puc);
00335 *puc = NULL;
00336 return ret;
00337 }
00338
00339 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
00340 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
00341 {
00342 int ret, len;
00343 int fast_retries = 5;
00344
00345 len = 0;
00346 while (len < size_min) {
00347 ret = transfer_func(h, buf+len, size-len);
00348 if (ret == AVERROR(EINTR))
00349 continue;
00350 if (h->flags & AVIO_FLAG_NONBLOCK)
00351 return ret;
00352 if (ret == AVERROR(EAGAIN)) {
00353 ret = 0;
00354 if (fast_retries)
00355 fast_retries--;
00356 else
00357 usleep(1000);
00358 } else if (ret < 1)
00359 return ret < 0 ? ret : len;
00360 if (ret)
00361 fast_retries = FFMAX(fast_retries, 2);
00362 len += ret;
00363 if (len < size && ff_check_interrupt(&h->interrupt_callback))
00364 return AVERROR_EXIT;
00365 }
00366 return len;
00367 }
00368
00369 int ffurl_read(URLContext *h, unsigned char *buf, int size)
00370 {
00371 if (!(h->flags & AVIO_FLAG_READ))
00372 return AVERROR(EIO);
00373 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
00374 }
00375
00376 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
00377 {
00378 if (!(h->flags & AVIO_FLAG_READ))
00379 return AVERROR(EIO);
00380 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
00381 }
00382
00383 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
00384 {
00385 if (!(h->flags & AVIO_FLAG_WRITE))
00386 return AVERROR(EIO);
00387
00388 if (h->max_packet_size && size > h->max_packet_size)
00389 return AVERROR(EIO);
00390
00391 return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
00392 }
00393
00394 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
00395 {
00396 int64_t ret;
00397
00398 if (!h->prot->url_seek)
00399 return AVERROR(ENOSYS);
00400 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
00401 return ret;
00402 }
00403
00404 int ffurl_close(URLContext *h)
00405 {
00406 int ret = 0;
00407 if (!h) return 0;
00408
00409 if (h->is_connected && h->prot->url_close)
00410 ret = h->prot->url_close(h);
00411 #if CONFIG_NETWORK
00412 ff_network_close();
00413 #endif
00414 if (h->prot->priv_data_size) {
00415 if (h->prot->priv_data_class)
00416 av_opt_free(h->priv_data);
00417 av_free(h->priv_data);
00418 }
00419 av_free(h);
00420 return ret;
00421 }
00422
00423 #if FF_API_OLD_AVIO
00424 int url_exist(const char *filename)
00425 {
00426 URLContext *h;
00427 if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
00428 return 0;
00429 ffurl_close(h);
00430 return 1;
00431 }
00432 #endif
00433
00434 int avio_check(const char *url, int flags)
00435 {
00436 URLContext *h;
00437 int ret = ffurl_alloc(&h, url, flags, NULL);
00438 if (ret)
00439 return ret;
00440
00441 if (h->prot->url_check) {
00442 ret = h->prot->url_check(h, flags);
00443 } else {
00444 ret = ffurl_connect(h, NULL);
00445 if (ret >= 0)
00446 ret = flags;
00447 }
00448
00449 ffurl_close(h);
00450 return ret;
00451 }
00452
00453 int64_t ffurl_size(URLContext *h)
00454 {
00455 int64_t pos, size;
00456
00457 size= ffurl_seek(h, 0, AVSEEK_SIZE);
00458 if(size<0){
00459 pos = ffurl_seek(h, 0, SEEK_CUR);
00460 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
00461 return size;
00462 size++;
00463 ffurl_seek(h, pos, SEEK_SET);
00464 }
00465 return size;
00466 }
00467
00468 int ffurl_get_file_handle(URLContext *h)
00469 {
00470 if (!h->prot->url_get_file_handle)
00471 return -1;
00472 return h->prot->url_get_file_handle(h);
00473 }
00474
00475 #if FF_API_OLD_INTERRUPT_CB
00476 static int default_interrupt_cb(void)
00477 {
00478 return 0;
00479 }
00480
00481 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
00482 {
00483 if (!interrupt_cb)
00484 interrupt_cb = default_interrupt_cb;
00485 url_interrupt_cb = interrupt_cb;
00486 }
00487 #endif
00488
00489 int ff_check_interrupt(AVIOInterruptCB *cb)
00490 {
00491 int ret;
00492 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
00493 return ret;
00494 #if FF_API_OLD_INTERRUPT_CB
00495 return url_interrupt_cb();
00496 #else
00497 return 0;
00498 #endif
00499 }
00500
00501 #if FF_API_OLD_AVIO
00502 int av_url_read_pause(URLContext *h, int pause)
00503 {
00504 if (!h->prot->url_read_pause)
00505 return AVERROR(ENOSYS);
00506 return h->prot->url_read_pause(h, pause);
00507 }
00508
00509 int64_t av_url_read_seek(URLContext *h,
00510 int stream_index, int64_t timestamp, int flags)
00511 {
00512 if (!h->prot->url_read_seek)
00513 return AVERROR(ENOSYS);
00514 return h->prot->url_read_seek(h, stream_index, timestamp, flags);
00515 }
00516 #endif