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 #if FF_API_OLD_AVIO
00093 URLProtocol *av_protocol_next(URLProtocol *p)
00094 {
00095 return ffurl_protocol_next(p);
00096 }
00097 #endif
00098
00099 const char *avio_enum_protocols(void **opaque, int output)
00100 {
00101 URLProtocol **p = opaque;
00102 *p = ffurl_protocol_next(*p);
00103 if (!*p) return NULL;
00104 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
00105 return (*p)->name;
00106 return avio_enum_protocols(opaque, output);
00107 }
00108
00109 int ffurl_register_protocol(URLProtocol *protocol, int size)
00110 {
00111 URLProtocol **p;
00112 if (size < sizeof(URLProtocol)) {
00113 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
00114 memcpy(temp, protocol, size);
00115 protocol = temp;
00116 }
00117 p = &first_protocol;
00118 while (*p != NULL) p = &(*p)->next;
00119 *p = protocol;
00120 protocol->next = NULL;
00121 return 0;
00122 }
00123
00124 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
00125 const char *filename, int flags,
00126 const AVIOInterruptCB *int_cb)
00127 {
00128 URLContext *uc;
00129 int err;
00130
00131 #if CONFIG_NETWORK
00132 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
00133 return AVERROR(EIO);
00134 #endif
00135 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
00136 if (!uc) {
00137 err = AVERROR(ENOMEM);
00138 goto fail;
00139 }
00140 uc->av_class = &ffurl_context_class;
00141 uc->filename = (char *) &uc[1];
00142 strcpy(uc->filename, filename);
00143 uc->prot = up;
00144 uc->flags = flags;
00145 uc->is_streamed = 0;
00146 uc->max_packet_size = 0;
00147 if (up->priv_data_size) {
00148 uc->priv_data = av_mallocz(up->priv_data_size);
00149 if (up->priv_data_class) {
00150 *(const AVClass**)uc->priv_data = up->priv_data_class;
00151 av_opt_set_defaults(uc->priv_data);
00152 }
00153 }
00154 if (int_cb)
00155 uc->interrupt_callback = *int_cb;
00156
00157 *puc = uc;
00158 return 0;
00159 fail:
00160 *puc = NULL;
00161 #if CONFIG_NETWORK
00162 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
00163 ff_network_close();
00164 #endif
00165 return err;
00166 }
00167
00168 int ffurl_connect(URLContext* uc, AVDictionary **options)
00169 {
00170 int err =
00171 #if !FF_API_OLD_AVIO
00172 uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
00173 #endif
00174 uc->prot->url_open(uc, uc->filename, uc->flags);
00175 if (err)
00176 return err;
00177 uc->is_connected = 1;
00178
00179 if( (uc->flags & AVIO_FLAG_WRITE)
00180 || !strcmp(uc->prot->name, "file"))
00181 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
00182 uc->is_streamed= 1;
00183 return 0;
00184 }
00185
00186 #if FF_API_OLD_AVIO
00187 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
00188 const char *filename, int flags)
00189 {
00190 int ret;
00191
00192 ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
00193 if (ret)
00194 goto fail;
00195 ret = ffurl_connect(*puc, NULL);
00196 if (!ret)
00197 return 0;
00198 fail:
00199 ffurl_close(*puc);
00200 *puc = NULL;
00201 return ret;
00202 }
00203 int url_alloc(URLContext **puc, const char *filename, int flags)
00204 {
00205 return ffurl_alloc(puc, filename, flags, NULL);
00206 }
00207 int url_connect(URLContext* uc)
00208 {
00209 return ffurl_connect(uc, NULL);
00210 }
00211 int url_open(URLContext **puc, const char *filename, int flags)
00212 {
00213 return ffurl_open(puc, filename, flags, NULL, NULL);
00214 }
00215 int url_read(URLContext *h, unsigned char *buf, int size)
00216 {
00217 return ffurl_read(h, buf, size);
00218 }
00219 int url_read_complete(URLContext *h, unsigned char *buf, int size)
00220 {
00221 return ffurl_read_complete(h, buf, size);
00222 }
00223 int url_write(URLContext *h, const unsigned char *buf, int size)
00224 {
00225 return ffurl_write(h, buf, size);
00226 }
00227 int64_t url_seek(URLContext *h, int64_t pos, int whence)
00228 {
00229 return ffurl_seek(h, pos, whence);
00230 }
00231 int url_close(URLContext *h)
00232 {
00233 return ffurl_close(h);
00234 }
00235 int64_t url_filesize(URLContext *h)
00236 {
00237 return ffurl_size(h);
00238 }
00239 int url_get_file_handle(URLContext *h)
00240 {
00241 return ffurl_get_file_handle(h);
00242 }
00243 int url_get_max_packet_size(URLContext *h)
00244 {
00245 return h->max_packet_size;
00246 }
00247 void url_get_filename(URLContext *h, char *buf, int buf_size)
00248 {
00249 av_strlcpy(buf, h->filename, buf_size);
00250 }
00251 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
00252 {
00253 avio_set_interrupt_cb(interrupt_cb);
00254 }
00255 int av_register_protocol2(URLProtocol *protocol, int size)
00256 {
00257 return ffurl_register_protocol(protocol, size);
00258 }
00259 #endif
00260
00261 #define URL_SCHEME_CHARS \
00262 "abcdefghijklmnopqrstuvwxyz" \
00263 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
00264 "0123456789+-."
00265
00266 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
00267 const AVIOInterruptCB *int_cb)
00268 {
00269 URLProtocol *up = NULL;
00270 char proto_str[128], proto_nested[128], *ptr;
00271 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
00272
00273 if (filename[proto_len] != ':' || is_dos_path(filename))
00274 strcpy(proto_str, "file");
00275 else
00276 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
00277
00278 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
00279 if ((ptr = strchr(proto_nested, '+')))
00280 *ptr = '\0';
00281
00282 while (up = ffurl_protocol_next(up)) {
00283 if (!strcmp(proto_str, up->name))
00284 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
00285 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
00286 !strcmp(proto_nested, up->name))
00287 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
00288 }
00289 *puc = NULL;
00290 return AVERROR(ENOENT);
00291 }
00292
00293 int ffurl_open(URLContext **puc, const char *filename, int flags,
00294 const AVIOInterruptCB *int_cb, AVDictionary **options)
00295 {
00296 int ret = ffurl_alloc(puc, filename, flags, int_cb);
00297 if (ret)
00298 return ret;
00299 if (options && (*puc)->prot->priv_data_class &&
00300 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
00301 goto fail;
00302 ret = ffurl_connect(*puc, options);
00303 if (!ret)
00304 return 0;
00305 fail:
00306 ffurl_close(*puc);
00307 *puc = NULL;
00308 return ret;
00309 }
00310
00311 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
00312 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
00313 {
00314 int ret, len;
00315 int fast_retries = 5;
00316
00317 len = 0;
00318 while (len < size_min) {
00319 ret = transfer_func(h, buf+len, size-len);
00320 if (ret == AVERROR(EINTR))
00321 continue;
00322 if (h->flags & AVIO_FLAG_NONBLOCK)
00323 return ret;
00324 if (ret == AVERROR(EAGAIN)) {
00325 ret = 0;
00326 if (fast_retries)
00327 fast_retries--;
00328 else
00329 usleep(1000);
00330 } else if (ret < 1)
00331 return ret < 0 ? ret : len;
00332 if (ret)
00333 fast_retries = FFMAX(fast_retries, 2);
00334 len += ret;
00335 if (ff_check_interrupt(&h->interrupt_callback))
00336 return AVERROR_EXIT;
00337 }
00338 return len;
00339 }
00340
00341 int ffurl_read(URLContext *h, unsigned char *buf, int size)
00342 {
00343 if (!(h->flags & AVIO_FLAG_READ))
00344 return AVERROR(EIO);
00345 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
00346 }
00347
00348 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
00349 {
00350 if (!(h->flags & AVIO_FLAG_READ))
00351 return AVERROR(EIO);
00352 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
00353 }
00354
00355 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
00356 {
00357 if (!(h->flags & AVIO_FLAG_WRITE))
00358 return AVERROR(EIO);
00359
00360 if (h->max_packet_size && size > h->max_packet_size)
00361 return AVERROR(EIO);
00362
00363 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
00364 }
00365
00366 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
00367 {
00368 int64_t ret;
00369
00370 if (!h->prot->url_seek)
00371 return AVERROR(ENOSYS);
00372 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
00373 return ret;
00374 }
00375
00376 int ffurl_close(URLContext *h)
00377 {
00378 int ret = 0;
00379 if (!h) return 0;
00380
00381 if (h->is_connected && h->prot->url_close)
00382 ret = h->prot->url_close(h);
00383 #if CONFIG_NETWORK
00384 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
00385 ff_network_close();
00386 #endif
00387 if (h->prot->priv_data_size) {
00388 if (h->prot->priv_data_class)
00389 av_opt_free(h->priv_data);
00390 av_free(h->priv_data);
00391 }
00392 av_free(h);
00393 return ret;
00394 }
00395
00396 #if FF_API_OLD_AVIO
00397 int url_exist(const char *filename)
00398 {
00399 URLContext *h;
00400 if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
00401 return 0;
00402 ffurl_close(h);
00403 return 1;
00404 }
00405 #endif
00406
00407 int avio_check(const char *url, int flags)
00408 {
00409 URLContext *h;
00410 int ret = ffurl_alloc(&h, url, flags, NULL);
00411 if (ret)
00412 return ret;
00413
00414 if (h->prot->url_check) {
00415 ret = h->prot->url_check(h, flags);
00416 } else {
00417 ret = ffurl_connect(h, NULL);
00418 if (ret >= 0)
00419 ret = flags;
00420 }
00421
00422 ffurl_close(h);
00423 return ret;
00424 }
00425
00426 int64_t ffurl_size(URLContext *h)
00427 {
00428 int64_t pos, size;
00429
00430 size= ffurl_seek(h, 0, AVSEEK_SIZE);
00431 if(size<0){
00432 pos = ffurl_seek(h, 0, SEEK_CUR);
00433 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
00434 return size;
00435 size++;
00436 ffurl_seek(h, pos, SEEK_SET);
00437 }
00438 return size;
00439 }
00440
00441 int ffurl_get_file_handle(URLContext *h)
00442 {
00443 if (!h->prot->url_get_file_handle)
00444 return -1;
00445 return h->prot->url_get_file_handle(h);
00446 }
00447
00448 #if FF_API_OLD_INTERRUPT_CB
00449 static int default_interrupt_cb(void)
00450 {
00451 return 0;
00452 }
00453
00454 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
00455 {
00456 if (!interrupt_cb)
00457 interrupt_cb = default_interrupt_cb;
00458 url_interrupt_cb = interrupt_cb;
00459 }
00460 #endif
00461
00462 int ff_check_interrupt(AVIOInterruptCB *cb)
00463 {
00464 int ret;
00465 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
00466 return ret;
00467 #if FF_API_OLD_INTERRUPT_CB
00468 return url_interrupt_cb();
00469 #else
00470 return 0;
00471 #endif
00472 }
00473
00474 #if FF_API_OLD_AVIO
00475 int av_url_read_pause(URLContext *h, int pause)
00476 {
00477 if (!h->prot->url_read_pause)
00478 return AVERROR(ENOSYS);
00479 return h->prot->url_read_pause(h, pause);
00480 }
00481
00482 int64_t av_url_read_seek(URLContext *h,
00483 int stream_index, int64_t timestamp, int flags)
00484 {
00485 if (!h->prot->url_read_seek)
00486 return AVERROR(ENOSYS);
00487 return h->prot->url_read_seek(h, stream_index, timestamp, flags);
00488 }
00489 #endif