Mercurial > libavformat.hg
annotate avio.c @ 3148:f00aeedea66a libavformat
MSN TCP Webcam stream demuxer.
| author | ramiro |
|---|---|
| date | Tue, 18 Mar 2008 19:54:47 +0000 |
| parents | e38d5357f0d0 |
| children | ce8070648576 |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * Unbuffered io for ffmpeg system | |
| 3 * Copyright (c) 2001 Fabrice Bellard | |
| 4 * | |
|
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
905
diff
changeset
|
5 * This file is part of FFmpeg. |
|
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
905
diff
changeset
|
6 * |
|
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
905
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
| 0 | 8 * modify it under the terms of the GNU Lesser General Public |
| 9 * License as published by the Free Software Foundation; either | |
|
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
905
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
| 0 | 11 * |
|
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
905
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
| 0 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 * Lesser General Public License for more details. | |
| 16 * | |
| 17 * You should have received a copy of the GNU Lesser General Public | |
|
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
905
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
|
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 0 | 20 */ |
| 21 #include "avformat.h" | |
| 2189 | 22 #include "avstring.h" |
|
3136
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
23 #include "opt.h" |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
24 |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
25 #if LIBAVFORMAT_VERSION_MAJOR >= 53 |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
26 /** @name Logging context. */ |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
27 /*@{*/ |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
28 static const char *urlcontext_to_name(void *ptr) |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
29 { |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
30 URLContext *h = (URLContext *)ptr; |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
31 if(h->prot) return h->prot->name; |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
32 else return "NULL"; |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
33 } |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
34 static const AVOption options[] = {{NULL}}; |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
35 static const AVClass urlcontext_class = |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
36 { "URLContext", urlcontext_to_name, options }; |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
37 /*@}*/ |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
38 #endif |
| 0 | 39 |
| 177 | 40 static int default_interrupt_cb(void); |
| 41 | |
| 0 | 42 URLProtocol *first_protocol = NULL; |
| 177 | 43 URLInterruptCB *url_interrupt_cb = default_interrupt_cb; |
| 0 | 44 |
| 2812 | 45 URLProtocol *av_protocol_next(URLProtocol *p) |
| 46 { | |
| 47 if(p) return p->next; | |
| 48 else return first_protocol; | |
| 49 } | |
| 50 | |
| 0 | 51 int register_protocol(URLProtocol *protocol) |
| 52 { | |
| 53 URLProtocol **p; | |
| 54 p = &first_protocol; | |
| 55 while (*p != NULL) p = &(*p)->next; | |
| 56 *p = protocol; | |
| 57 protocol->next = NULL; | |
| 58 return 0; | |
| 59 } | |
| 60 | |
| 61 int url_open(URLContext **puc, const char *filename, int flags) | |
| 62 { | |
| 63 URLContext *uc; | |
| 64 URLProtocol *up; | |
| 65 const char *p; | |
| 66 char proto_str[128], *q; | |
| 67 int err; | |
| 68 | |
| 69 p = filename; | |
| 70 q = proto_str; | |
| 71 while (*p != '\0' && *p != ':') { | |
|
67
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
72 /* protocols can only contain alphabetic chars */ |
|
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
73 if (!isalpha(*p)) |
|
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
74 goto file_proto; |
| 0 | 75 if ((q - proto_str) < sizeof(proto_str) - 1) |
| 76 *q++ = *p; | |
| 77 p++; | |
| 78 } | |
| 79 /* if the protocol has length 1, we consider it is a dos drive */ | |
| 80 if (*p == '\0' || (q - proto_str) <= 1) { | |
|
67
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
81 file_proto: |
| 0 | 82 strcpy(proto_str, "file"); |
| 83 } else { | |
| 84 *q = '\0'; | |
| 85 } | |
| 885 | 86 |
| 0 | 87 up = first_protocol; |
| 88 while (up != NULL) { | |
| 89 if (!strcmp(proto_str, up->name)) | |
| 90 goto found; | |
| 91 up = up->next; | |
| 92 } | |
|
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1746
diff
changeset
|
93 err = AVERROR(ENOENT); |
| 0 | 94 goto fail; |
| 95 found: | |
|
1648
90987914ad57
makes the filename member of the URLContext a pointer, so that the
gpoirier
parents:
1613
diff
changeset
|
96 uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1); |
| 0 | 97 if (!uc) { |
|
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1746
diff
changeset
|
98 err = AVERROR(ENOMEM); |
| 0 | 99 goto fail; |
| 100 } | |
|
3136
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
101 #if LIBAVFORMAT_VERSION_MAJOR >= 53 |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
102 uc->av_class = &urlcontext_class; |
|
e38d5357f0d0
Add AVClass to URLContext at next major version bump
superdump
parents:
2914
diff
changeset
|
103 #endif |
|
1648
90987914ad57
makes the filename member of the URLContext a pointer, so that the
gpoirier
parents:
1613
diff
changeset
|
104 uc->filename = (char *) &uc[1]; |
| 19 | 105 strcpy(uc->filename, filename); |
| 0 | 106 uc->prot = up; |
| 107 uc->flags = flags; | |
| 108 uc->is_streamed = 0; /* default = not streamed */ | |
| 109 uc->max_packet_size = 0; /* default: stream file */ | |
| 110 err = up->url_open(uc, filename, flags); | |
| 111 if (err < 0) { | |
| 112 av_free(uc); | |
| 113 *puc = NULL; | |
| 114 return err; | |
| 115 } | |
| 116 *puc = uc; | |
| 117 return 0; | |
| 118 fail: | |
| 119 *puc = NULL; | |
| 120 return err; | |
| 121 } | |
| 122 | |
| 123 int url_read(URLContext *h, unsigned char *buf, int size) | |
| 124 { | |
| 125 int ret; | |
| 126 if (h->flags & URL_WRONLY) | |
|
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2189
diff
changeset
|
127 return AVERROR(EIO); |
| 0 | 128 ret = h->prot->url_read(h, buf, size); |
| 129 return ret; | |
| 130 } | |
| 131 | |
| 132 int url_write(URLContext *h, unsigned char *buf, int size) | |
| 133 { | |
| 134 int ret; | |
| 135 if (!(h->flags & (URL_WRONLY | URL_RDWR))) | |
|
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2189
diff
changeset
|
136 return AVERROR(EIO); |
| 0 | 137 /* avoid sending too big packets */ |
| 138 if (h->max_packet_size && size > h->max_packet_size) | |
|
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2189
diff
changeset
|
139 return AVERROR(EIO); |
| 0 | 140 ret = h->prot->url_write(h, buf, size); |
| 141 return ret; | |
| 142 } | |
| 143 | |
| 144 offset_t url_seek(URLContext *h, offset_t pos, int whence) | |
| 145 { | |
| 146 offset_t ret; | |
| 147 | |
| 148 if (!h->prot->url_seek) | |
|
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1746
diff
changeset
|
149 return AVERROR(EPIPE); |
| 0 | 150 ret = h->prot->url_seek(h, pos, whence); |
| 151 return ret; | |
| 152 } | |
| 153 | |
| 154 int url_close(URLContext *h) | |
| 155 { | |
| 2757 | 156 int ret = 0; |
| 2710 | 157 if (!h) return 0; /* can happen when url_open fails */ |
| 0 | 158 |
| 2757 | 159 if (h->prot->url_close) |
| 160 ret = h->prot->url_close(h); | |
| 0 | 161 av_free(h); |
| 162 return ret; | |
| 163 } | |
| 164 | |
| 165 int url_exist(const char *filename) | |
| 166 { | |
| 167 URLContext *h; | |
| 168 if (url_open(&h, filename, URL_RDONLY) < 0) | |
| 169 return 0; | |
| 170 url_close(h); | |
| 171 return 1; | |
| 172 } | |
| 173 | |
| 174 offset_t url_filesize(URLContext *h) | |
| 175 { | |
| 176 offset_t pos, size; | |
| 885 | 177 |
|
1612
a6eaa0762191
seekless filesize retrieving support in 7 lines of code, also doesnt break compatibility
michael
parents:
1358
diff
changeset
|
178 size= url_seek(h, 0, AVSEEK_SIZE); |
|
a6eaa0762191
seekless filesize retrieving support in 7 lines of code, also doesnt break compatibility
michael
parents:
1358
diff
changeset
|
179 if(size<0){ |
| 1613 | 180 pos = url_seek(h, 0, SEEK_CUR); |
|
1746
2649c0a9c037
protect the size=seek(SEEK_END,-1)+1 results with an if (.. < 0), else the
gpoirier
parents:
1648
diff
changeset
|
181 if ((size = url_seek(h, -1, SEEK_END)) < 0) |
|
2649c0a9c037
protect the size=seek(SEEK_END,-1)+1 results with an if (.. < 0), else the
gpoirier
parents:
1648
diff
changeset
|
182 return size; |
|
2649c0a9c037
protect the size=seek(SEEK_END,-1)+1 results with an if (.. < 0), else the
gpoirier
parents:
1648
diff
changeset
|
183 size++; |
| 1613 | 184 url_seek(h, pos, SEEK_SET); |
|
1612
a6eaa0762191
seekless filesize retrieving support in 7 lines of code, also doesnt break compatibility
michael
parents:
1358
diff
changeset
|
185 } |
| 0 | 186 return size; |
| 187 } | |
| 188 | |
| 189 int url_get_max_packet_size(URLContext *h) | |
| 190 { | |
| 191 return h->max_packet_size; | |
| 192 } | |
| 19 | 193 |
| 194 void url_get_filename(URLContext *h, char *buf, int buf_size) | |
| 195 { | |
| 2189 | 196 av_strlcpy(buf, h->filename, buf_size); |
| 19 | 197 } |
| 177 | 198 |
| 199 | |
| 200 static int default_interrupt_cb(void) | |
| 201 { | |
| 202 return 0; | |
| 203 } | |
| 204 | |
| 205 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb) | |
| 206 { | |
| 207 if (!interrupt_cb) | |
| 208 interrupt_cb = default_interrupt_cb; | |
| 209 url_interrupt_cb = interrupt_cb; | |
| 210 } | |
|
2778
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
211 |
|
2839
b51319dd86e5
Merge recently added and still unused play and pause functions.
michael
parents:
2834
diff
changeset
|
212 int av_url_read_pause(URLContext *h, int pause) |
|
2778
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
213 { |
|
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
214 if (!h->prot->url_read_pause) |
|
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
215 return AVERROR(ENOSYS); |
|
2839
b51319dd86e5
Merge recently added and still unused play and pause functions.
michael
parents:
2834
diff
changeset
|
216 return h->prot->url_read_pause(h, pause); |
|
2778
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
217 } |
|
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
218 |
|
2840
f51675f78402
Make recently added and still unused read_seek functions return offset_t.
michael
parents:
2839
diff
changeset
|
219 offset_t av_url_read_seek(URLContext *h, |
|
2778
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
220 int stream_index, int64_t timestamp, int flags) |
|
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
221 { |
|
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
222 if (!h->prot->url_read_seek) |
|
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
223 return AVERROR(ENOSYS); |
|
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
224 return h->prot->url_read_seek(h, stream_index, timestamp, flags); |
|
50e2307414ee
Extend URLProtocol with new function pointers and api functions for
andoma
parents:
2757
diff
changeset
|
225 } |
