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];
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 up = first_protocol;
00187 while (up != NULL) {
00188 if (!strcmp(proto_str, up->name))
00189 return url_alloc_for_protocol (puc, up, filename, flags);
00190 up = up->next;
00191 }
00192 *puc = NULL;
00193 return AVERROR(ENOENT);
00194 }
00195
00196 int url_open(URLContext **puc, const char *filename, int flags)
00197 {
00198 int ret = url_alloc(puc, filename, flags);
00199 if (ret)
00200 return ret;
00201 ret = url_connect(*puc);
00202 if (!ret)
00203 return 0;
00204 url_close(*puc);
00205 *puc = NULL;
00206 return ret;
00207 }
00208
00209 int url_read(URLContext *h, unsigned char *buf, int size)
00210 {
00211 int ret;
00212 if (h->flags & URL_WRONLY)
00213 return AVERROR(EIO);
00214 ret = h->prot->url_read(h, buf, size);
00215 return ret;
00216 }
00217
00218 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size,
00219 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
00220 {
00221 int ret, len;
00222 int fast_retries = 5;
00223
00224 len = 0;
00225 while (len < size) {
00226 ret = transfer_func(h, buf+len, size-len);
00227 if (ret == AVERROR(EAGAIN)) {
00228 ret = 0;
00229 if (fast_retries)
00230 fast_retries--;
00231 else
00232 usleep(1000);
00233 } else if (ret < 1)
00234 return ret < 0 ? ret : len;
00235 if (ret)
00236 fast_retries = FFMAX(fast_retries, 2);
00237 len += ret;
00238 }
00239 return len;
00240 }
00241
00242 int url_read_complete(URLContext *h, unsigned char *buf, int size)
00243 {
00244 return retry_transfer_wrapper(h, buf, size, url_read);
00245 }
00246
00247 int url_write(URLContext *h, const unsigned char *buf, int size)
00248 {
00249 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
00250 return AVERROR(EIO);
00251
00252 if (h->max_packet_size && size > h->max_packet_size)
00253 return AVERROR(EIO);
00254
00255 return retry_transfer_wrapper(h, buf, size, h->prot->url_write);
00256 }
00257
00258 int64_t url_seek(URLContext *h, int64_t pos, int whence)
00259 {
00260 int64_t ret;
00261
00262 if (!h->prot->url_seek)
00263 return AVERROR(ENOSYS);
00264 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
00265 return ret;
00266 }
00267
00268 int url_close(URLContext *h)
00269 {
00270 int ret = 0;
00271 if (!h) return 0;
00272
00273 if (h->is_connected && h->prot->url_close)
00274 ret = h->prot->url_close(h);
00275 #if CONFIG_NETWORK
00276 ff_network_close();
00277 #endif
00278 if (h->prot->priv_data_size)
00279 av_free(h->priv_data);
00280 av_free(h);
00281 return ret;
00282 }
00283
00284 int url_exist(const char *filename)
00285 {
00286 URLContext *h;
00287 if (url_open(&h, filename, URL_RDONLY) < 0)
00288 return 0;
00289 url_close(h);
00290 return 1;
00291 }
00292
00293 int64_t url_filesize(URLContext *h)
00294 {
00295 int64_t pos, size;
00296
00297 size= url_seek(h, 0, AVSEEK_SIZE);
00298 if(size<0){
00299 pos = url_seek(h, 0, SEEK_CUR);
00300 if ((size = url_seek(h, -1, SEEK_END)) < 0)
00301 return size;
00302 size++;
00303 url_seek(h, pos, SEEK_SET);
00304 }
00305 return size;
00306 }
00307
00308 int url_get_file_handle(URLContext *h)
00309 {
00310 if (!h->prot->url_get_file_handle)
00311 return -1;
00312 return h->prot->url_get_file_handle(h);
00313 }
00314
00315 int url_get_max_packet_size(URLContext *h)
00316 {
00317 return h->max_packet_size;
00318 }
00319
00320 void url_get_filename(URLContext *h, char *buf, int buf_size)
00321 {
00322 av_strlcpy(buf, h->filename, buf_size);
00323 }
00324
00325
00326 static int default_interrupt_cb(void)
00327 {
00328 return 0;
00329 }
00330
00331 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
00332 {
00333 if (!interrupt_cb)
00334 interrupt_cb = default_interrupt_cb;
00335 url_interrupt_cb = interrupt_cb;
00336 }
00337
00338 int av_url_read_pause(URLContext *h, int pause)
00339 {
00340 if (!h->prot->url_read_pause)
00341 return AVERROR(ENOSYS);
00342 return h->prot->url_read_pause(h, pause);
00343 }
00344
00345 int64_t av_url_read_seek(URLContext *h,
00346 int stream_index, int64_t timestamp, int flags)
00347 {
00348 if (!h->prot->url_read_seek)
00349 return AVERROR(ENOSYS);
00350 return h->prot->url_read_seek(h, stream_index, timestamp, flags);
00351 }