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 URLProtocol *av_protocol_next(URLProtocol *p)
00030 {
00031 if(p) return p->next;
00032 else return first_protocol;
00033 }
00034
00035 int register_protocol(URLProtocol *protocol)
00036 {
00037 URLProtocol **p;
00038 p = &first_protocol;
00039 while (*p != NULL) p = &(*p)->next;
00040 *p = protocol;
00041 protocol->next = NULL;
00042 return 0;
00043 }
00044
00045 int url_open(URLContext **puc, const char *filename, int flags)
00046 {
00047 URLContext *uc;
00048 URLProtocol *up;
00049 const char *p;
00050 char proto_str[128], *q;
00051 int err;
00052
00053 p = filename;
00054 q = proto_str;
00055 while (*p != '\0' && *p != ':') {
00056
00057 if (!isalpha(*p))
00058 goto file_proto;
00059 if ((q - proto_str) < sizeof(proto_str) - 1)
00060 *q++ = *p;
00061 p++;
00062 }
00063
00064 if (*p == '\0' || (q - proto_str) <= 1) {
00065 file_proto:
00066 strcpy(proto_str, "file");
00067 } else {
00068 *q = '\0';
00069 }
00070
00071 up = first_protocol;
00072 while (up != NULL) {
00073 if (!strcmp(proto_str, up->name))
00074 goto found;
00075 up = up->next;
00076 }
00077 err = AVERROR(ENOENT);
00078 goto fail;
00079 found:
00080 uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);
00081 if (!uc) {
00082 err = AVERROR(ENOMEM);
00083 goto fail;
00084 }
00085 uc->filename = (char *) &uc[1];
00086 strcpy(uc->filename, filename);
00087 uc->prot = up;
00088 uc->flags = flags;
00089 uc->is_streamed = 0;
00090 uc->max_packet_size = 0;
00091 err = up->url_open(uc, filename, flags);
00092 if (err < 0) {
00093 av_free(uc);
00094 *puc = NULL;
00095 return err;
00096 }
00097 *puc = uc;
00098 return 0;
00099 fail:
00100 *puc = NULL;
00101 return err;
00102 }
00103
00104 int url_read(URLContext *h, unsigned char *buf, int size)
00105 {
00106 int ret;
00107 if (h->flags & URL_WRONLY)
00108 return AVERROR(EIO);
00109 ret = h->prot->url_read(h, buf, size);
00110 return ret;
00111 }
00112
00113 int url_write(URLContext *h, unsigned char *buf, int size)
00114 {
00115 int ret;
00116 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
00117 return AVERROR(EIO);
00118
00119 if (h->max_packet_size && size > h->max_packet_size)
00120 return AVERROR(EIO);
00121 ret = h->prot->url_write(h, buf, size);
00122 return ret;
00123 }
00124
00125 offset_t url_seek(URLContext *h, offset_t pos, int whence)
00126 {
00127 offset_t ret;
00128
00129 if (!h->prot->url_seek)
00130 return AVERROR(EPIPE);
00131 ret = h->prot->url_seek(h, pos, whence);
00132 return ret;
00133 }
00134
00135 int url_close(URLContext *h)
00136 {
00137 int ret = 0;
00138 if (!h) return 0;
00139
00140 if (h->prot->url_close)
00141 ret = h->prot->url_close(h);
00142 av_free(h);
00143 return ret;
00144 }
00145
00146 int url_exist(const char *filename)
00147 {
00148 URLContext *h;
00149 if (url_open(&h, filename, URL_RDONLY) < 0)
00150 return 0;
00151 url_close(h);
00152 return 1;
00153 }
00154
00155 offset_t url_filesize(URLContext *h)
00156 {
00157 offset_t pos, size;
00158
00159 size= url_seek(h, 0, AVSEEK_SIZE);
00160 if(size<0){
00161 pos = url_seek(h, 0, SEEK_CUR);
00162 if ((size = url_seek(h, -1, SEEK_END)) < 0)
00163 return size;
00164 size++;
00165 url_seek(h, pos, SEEK_SET);
00166 }
00167 return size;
00168 }
00169
00170 int url_get_max_packet_size(URLContext *h)
00171 {
00172 return h->max_packet_size;
00173 }
00174
00175 void url_get_filename(URLContext *h, char *buf, int buf_size)
00176 {
00177 av_strlcpy(buf, h->filename, buf_size);
00178 }
00179
00180
00181 static int default_interrupt_cb(void)
00182 {
00183 return 0;
00184 }
00185
00186 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
00187 {
00188 if (!interrupt_cb)
00189 interrupt_cb = default_interrupt_cb;
00190 url_interrupt_cb = interrupt_cb;
00191 }
00192
00193 int av_url_read_pause(URLContext *h, int pause)
00194 {
00195 if (!h->prot->url_read_pause)
00196 return AVERROR(ENOSYS);
00197 return h->prot->url_read_pause(h, pause);
00198 }
00199
00200 offset_t av_url_read_seek(URLContext *h,
00201 int stream_index, int64_t timestamp, int flags)
00202 {
00203 if (!h->prot->url_read_seek)
00204 return AVERROR(ENOSYS);
00205 return h->prot->url_read_seek(h, stream_index, timestamp, flags);
00206 }