Mercurial > libavformat.hg
annotate avio.c @ 2469:335446056965 libavformat
Move writing Xiph-style sizes to its own function
author | conrad |
---|---|
date | Wed, 05 Sep 2007 00:24:06 +0000 |
parents | b21c2af60bc9 |
children | 00ffc2f02705 |
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" |
0 | 23 |
177 | 24 static int default_interrupt_cb(void); |
25 | |
0 | 26 URLProtocol *first_protocol = NULL; |
177 | 27 URLInterruptCB *url_interrupt_cb = default_interrupt_cb; |
0 | 28 |
29 int register_protocol(URLProtocol *protocol) | |
30 { | |
31 URLProtocol **p; | |
32 p = &first_protocol; | |
33 while (*p != NULL) p = &(*p)->next; | |
34 *p = protocol; | |
35 protocol->next = NULL; | |
36 return 0; | |
37 } | |
38 | |
39 int url_open(URLContext **puc, const char *filename, int flags) | |
40 { | |
41 URLContext *uc; | |
42 URLProtocol *up; | |
43 const char *p; | |
44 char proto_str[128], *q; | |
45 int err; | |
46 | |
47 p = filename; | |
48 q = proto_str; | |
49 while (*p != '\0' && *p != ':') { | |
67
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
50 /* protocols can only contain alphabetic chars */ |
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
51 if (!isalpha(*p)) |
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
52 goto file_proto; |
0 | 53 if ((q - proto_str) < sizeof(proto_str) - 1) |
54 *q++ = *p; | |
55 p++; | |
56 } | |
57 /* if the protocol has length 1, we consider it is a dos drive */ | |
58 if (*p == '\0' || (q - proto_str) <= 1) { | |
67
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
59 file_proto: |
0 | 60 strcpy(proto_str, "file"); |
61 } else { | |
62 *q = '\0'; | |
63 } | |
885 | 64 |
0 | 65 up = first_protocol; |
66 while (up != NULL) { | |
67 if (!strcmp(proto_str, up->name)) | |
68 goto found; | |
69 up = up->next; | |
70 } | |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1746
diff
changeset
|
71 err = AVERROR(ENOENT); |
0 | 72 goto fail; |
73 found: | |
1648
90987914ad57
makes the filename member of the URLContext a pointer, so that the
gpoirier
parents:
1613
diff
changeset
|
74 uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1); |
0 | 75 if (!uc) { |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1746
diff
changeset
|
76 err = AVERROR(ENOMEM); |
0 | 77 goto fail; |
78 } | |
1648
90987914ad57
makes the filename member of the URLContext a pointer, so that the
gpoirier
parents:
1613
diff
changeset
|
79 #if LIBAVFORMAT_VERSION_INT >= (52<<16) |
90987914ad57
makes the filename member of the URLContext a pointer, so that the
gpoirier
parents:
1613
diff
changeset
|
80 uc->filename = (char *) &uc[1]; |
90987914ad57
makes the filename member of the URLContext a pointer, so that the
gpoirier
parents:
1613
diff
changeset
|
81 #endif |
19 | 82 strcpy(uc->filename, filename); |
0 | 83 uc->prot = up; |
84 uc->flags = flags; | |
85 uc->is_streamed = 0; /* default = not streamed */ | |
86 uc->max_packet_size = 0; /* default: stream file */ | |
87 err = up->url_open(uc, filename, flags); | |
88 if (err < 0) { | |
89 av_free(uc); | |
90 *puc = NULL; | |
91 return err; | |
92 } | |
93 *puc = uc; | |
94 return 0; | |
95 fail: | |
96 *puc = NULL; | |
97 return err; | |
98 } | |
99 | |
100 int url_read(URLContext *h, unsigned char *buf, int size) | |
101 { | |
102 int ret; | |
103 if (h->flags & URL_WRONLY) | |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2189
diff
changeset
|
104 return AVERROR(EIO); |
0 | 105 ret = h->prot->url_read(h, buf, size); |
106 return ret; | |
107 } | |
108 | |
905
dbc0145bbf11
Add --disable-protocols option to configure to disable I/O protocol from
diego
parents:
896
diff
changeset
|
109 #if defined(CONFIG_MUXERS) || defined(CONFIG_PROTOCOLS) |
0 | 110 int url_write(URLContext *h, unsigned char *buf, int size) |
111 { | |
112 int ret; | |
113 if (!(h->flags & (URL_WRONLY | URL_RDWR))) | |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2189
diff
changeset
|
114 return AVERROR(EIO); |
0 | 115 /* avoid sending too big packets */ |
116 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
|
117 return AVERROR(EIO); |
0 | 118 ret = h->prot->url_write(h, buf, size); |
119 return ret; | |
120 } | |
905
dbc0145bbf11
Add --disable-protocols option to configure to disable I/O protocol from
diego
parents:
896
diff
changeset
|
121 #endif //CONFIG_MUXERS || CONFIG_PROTOCOLS |
0 | 122 |
123 offset_t url_seek(URLContext *h, offset_t pos, int whence) | |
124 { | |
125 offset_t ret; | |
126 | |
127 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
|
128 return AVERROR(EPIPE); |
0 | 129 ret = h->prot->url_seek(h, pos, whence); |
130 return ret; | |
131 } | |
132 | |
133 int url_close(URLContext *h) | |
134 { | |
135 int ret; | |
136 | |
137 ret = h->prot->url_close(h); | |
138 av_free(h); | |
139 return ret; | |
140 } | |
141 | |
142 int url_exist(const char *filename) | |
143 { | |
144 URLContext *h; | |
145 if (url_open(&h, filename, URL_RDONLY) < 0) | |
146 return 0; | |
147 url_close(h); | |
148 return 1; | |
149 } | |
150 | |
151 offset_t url_filesize(URLContext *h) | |
152 { | |
153 offset_t pos, size; | |
885 | 154 |
1612
a6eaa0762191
seekless filesize retrieving support in 7 lines of code, also doesnt break compatibility
michael
parents:
1358
diff
changeset
|
155 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
|
156 if(size<0){ |
1613 | 157 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
|
158 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
|
159 return size; |
2649c0a9c037
protect the size=seek(SEEK_END,-1)+1 results with an if (.. < 0), else the
gpoirier
parents:
1648
diff
changeset
|
160 size++; |
1613 | 161 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
|
162 } |
0 | 163 return size; |
164 } | |
165 | |
166 int url_get_max_packet_size(URLContext *h) | |
167 { | |
168 return h->max_packet_size; | |
169 } | |
19 | 170 |
171 void url_get_filename(URLContext *h, char *buf, int buf_size) | |
172 { | |
2189 | 173 av_strlcpy(buf, h->filename, buf_size); |
19 | 174 } |
177 | 175 |
176 | |
177 static int default_interrupt_cb(void) | |
178 { | |
179 return 0; | |
180 } | |
181 | |
182 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb) | |
183 { | |
184 if (!interrupt_cb) | |
185 interrupt_cb = default_interrupt_cb; | |
186 url_interrupt_cb = interrupt_cb; | |
187 } |