00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "avstring.h"
00023
00024 static int default_interrupt_cb(void);
00025
00026 URLProtocol *first_protocol = NULL;
00027 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
00028
00029 int register_protocol(URLProtocol *protocol)
00030 {
00031 URLProtocol **p;
00032 p = &first_protocol;
00033 while (*p != NULL) p = &(*p)->next;
00034 *p = protocol;
00035 protocol->next = NULL;
00036 return 0;
00037 }
00038
00039 int url_open(URLContext **puc, const char *filename, int flags)
00040 {
00041 URLContext *uc;
00042 URLProtocol *up;
00043 const char *p;
00044 char proto_str[128], *q;
00045 int err;
00046
00047 p = filename;
00048 q = proto_str;
00049 while (*p != '\0' && *p != ':') {
00050
00051 if (!isalpha(*p))
00052 goto file_proto;
00053 if ((q - proto_str) < sizeof(proto_str) - 1)
00054 *q++ = *p;
00055 p++;
00056 }
00057
00058 if (*p == '\0' || (q - proto_str) <= 1) {
00059 file_proto:
00060 strcpy(proto_str, "file");
00061 } else {
00062 *q = '\0';
00063 }
00064
00065 up = first_protocol;
00066 while (up != NULL) {
00067 if (!strcmp(proto_str, up->name))
00068 goto found;
00069 up = up->next;
00070 }
00071 err = AVERROR(ENOENT);
00072 goto fail;
00073 found:
00074 uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);
00075 if (!uc) {
00076 err = AVERROR(ENOMEM);
00077 goto fail;
00078 }
00079 #if LIBAVFORMAT_VERSION_INT >= (52<<16)
00080 uc->filename = (char *) &uc[1];
00081 #endif
00082 strcpy(uc->filename, filename);
00083 uc->prot = up;
00084 uc->flags = flags;
00085 uc->is_streamed = 0;
00086 uc->max_packet_size = 0;
00087 err = up->url_open(uc, filename, flags);
00088 if (err < 0) {
00089 av_free(uc);
00090 *puc = NULL;
00091 return err;
00092 }
00093 *puc = uc;
00094 return 0;
00095 fail:
00096 *puc = NULL;
00097 return err;
00098 }
00099
00100 int url_read(URLContext *h, unsigned char *buf, int size)
00101 {
00102 int ret;
00103 if (h->flags & URL_WRONLY)
00104 return AVERROR(EIO);
00105 ret = h->prot->url_read(h, buf, size);
00106 return ret;
00107 }
00108
00109 #if defined(CONFIG_MUXERS) || defined(CONFIG_PROTOCOLS)
00110 int url_write(URLContext *h, unsigned char *buf, int size)
00111 {
00112 int ret;
00113 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
00114 return AVERROR(EIO);
00115
00116 if (h->max_packet_size && size > h->max_packet_size)
00117 return AVERROR(EIO);
00118 ret = h->prot->url_write(h, buf, size);
00119 return ret;
00120 }
00121 #endif //CONFIG_MUXERS || CONFIG_PROTOCOLS
00122
00123 offset_t url_seek(URLContext *h, offset_t pos, int whence)
00124 {
00125 offset_t ret;
00126
00127 if (!h->prot->url_seek)
00128 return AVERROR(EPIPE);
00129 ret = h->prot->url_seek(h, pos, whence);
00130 return ret;
00131 }
00132
00133 int url_close(URLContext *h)
00134 {
00135 int ret = 0;
00136 if (!h) return 0;
00137
00138 if (h->prot->url_close)
00139 ret = h->prot->url_close(h);
00140 av_free(h);
00141 return ret;
00142 }
00143
00144 int url_exist(const char *filename)
00145 {
00146 URLContext *h;
00147 if (url_open(&h, filename, URL_RDONLY) < 0)
00148 return 0;
00149 url_close(h);
00150 return 1;
00151 }
00152
00153 offset_t url_filesize(URLContext *h)
00154 {
00155 offset_t pos, size;
00156
00157 size= url_seek(h, 0, AVSEEK_SIZE);
00158 if(size<0){
00159 pos = url_seek(h, 0, SEEK_CUR);
00160 if ((size = url_seek(h, -1, SEEK_END)) < 0)
00161 return size;
00162 size++;
00163 url_seek(h, pos, SEEK_SET);
00164 }
00165 return size;
00166 }
00167
00168 int url_get_max_packet_size(URLContext *h)
00169 {
00170 return h->max_packet_size;
00171 }
00172
00173 void url_get_filename(URLContext *h, char *buf, int buf_size)
00174 {
00175 av_strlcpy(buf, h->filename, buf_size);
00176 }
00177
00178
00179 static int default_interrupt_cb(void)
00180 {
00181 return 0;
00182 }
00183
00184 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
00185 {
00186 if (!interrupt_cb)
00187 interrupt_cb = default_interrupt_cb;
00188 url_interrupt_cb = interrupt_cb;
00189 }