Implement url_open_protocol(), which is basiclly the former url_open()
[libav.git] / libavformat / avio.h
CommitLineData
04d7f601
DB
1/*
2 * unbuffered io for ffmpeg system
3 * copyright (c) 2001 Fabrice Bellard
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
04d7f601
DB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
04d7f601 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
04d7f601
DB
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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
04d7f601
DB
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
5b21bdab
DB
21#ifndef FFMPEG_AVIO_H
22#define FFMPEG_AVIO_H
f031df23 23
99545457
MR
24#include <stdint.h>
25
de6d9b64
FB
26/* output byte stream handling */
27
0c1a9eda 28typedef int64_t offset_t;
de6d9b64
FB
29
30/* unbuffered I/O */
31
1308f273
MN
32/**
33 * URL Context.
34 * New fields can be added to the end with minor version bumps.
8bfb108b 35 * Removal, reordering and changes to existing fields require a major
1308f273 36 * version bump.
8bfb108b 37 * sizeof(URLContext) must not be used outside libav*.
1308f273 38 */
de6d9b64 39struct URLContext {
5acef35f
BA
40#if LIBAVFORMAT_VERSION_MAJOR >= 53
41 const AVClass *av_class; ///< information for av_log(). Set by url_open().
42#endif
de6d9b64 43 struct URLProtocol *prot;
115329f1 44 int flags;
c3316802
PI
45 int is_streamed; /**< true if streamed (no seek possible), default = false */
46 int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
de6d9b64 47 void *priv_data;
c3316802 48 char *filename; /**< specified filename */
de6d9b64
FB
49};
50
de6d9b64
FB
51typedef struct URLContext URLContext;
52
53typedef struct URLPollEntry {
54 URLContext *handle;
55 int events;
56 int revents;
57} URLPollEntry;
58
59#define URL_RDONLY 0
60#define URL_WRONLY 1
f031df23
FB
61#define URL_RDWR 2
62
019ac05a
FB
63typedef int URLInterruptCB(void);
64
ba99cfc2
RB
65int url_open_protocol (URLContext **puc, struct URLProtocol *up,
66 const char *filename, int flags);
de6d9b64
FB
67int url_open(URLContext **h, const char *filename, int flags);
68int url_read(URLContext *h, unsigned char *buf, int size);
69int url_write(URLContext *h, unsigned char *buf, int size);
70offset_t url_seek(URLContext *h, offset_t pos, int whence);
de6d9b64
FB
71int url_close(URLContext *h);
72int url_exist(const char *filename);
73offset_t url_filesize(URLContext *h);
c306748c 74
c3316802 75/**
c306748c 76 * Return the maximum packet size associated to packetized file
8bfb108b 77 * handle. If the file is not packetized (stream like HTTP or file on
c306748c
PI
78 * disk), then 0 is returned.
79 *
80 * @param h file handle
81 * @return maximum packet size in bytes
82 */
f031df23 83int url_get_max_packet_size(URLContext *h);
f746a046
FB
84void url_get_filename(URLContext *h, char *buf, int buf_size);
85
c3316802 86/**
8bfb108b 87 * The callback is called in blocking functions to test regulary if
c3316802
PI
88 * asynchronous interruption is needed. AVERROR(EINTR) is returned
89 * in this case by the interrupted function. 'NULL' means no interrupt
8bfb108b 90 * callback is given.
c3316802 91 */
019ac05a
FB
92void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
93
de6d9b64
FB
94/* not implemented */
95int url_poll(URLPollEntry *poll_table, int n, int timeout);
96
502bdf68
MN
97/**
98 * Pause and resume playing - only meaningful if using a network streaming
99 * protocol (e.g. MMS).
100 * @param pause 1 for pause, 0 for resume
101 */
102int av_url_read_pause(URLContext *h, int pause);
8bfb108b 103
536333a0
BA
104/**
105 * Seek to a given timestamp relative to some component stream.
8bfb108b 106 * Only meaningful if using a network streaming protocol (e.g. MMS.).
536333a0
BA
107 * @param stream_index The stream index that the timestamp is relative to.
108 * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
109 * units from the beginning of the presentation.
110 * If a stream_index >= 0 is used and the protocol does not support
111 * seeking based on component streams, the call will fail with ENOTSUP.
00ef4f58 112 * @param timestamp timestamp in AVStream.time_base units
536333a0
BA
113 * or if there is no stream specified then in AV_TIME_BASE units.
114 * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
115 * and AVSEEK_FLAG_ANY. The protocol may silently ignore
116 * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
117 * fail with ENOTSUP if used and not supported.
118 * @return >= 0 on success
119 * @see AVInputFormat::read_seek
120 */
b075e520 121offset_t av_url_read_seek(URLContext *h,
536333a0
BA
122 int stream_index, int64_t timestamp, int flags);
123
aa38b097 124/**
90b5b51e
DB
125 * Passing this as the "whence" parameter to a seek function causes it to
126 * return the filesize without seeking anywhere. Supporting this is optional.
127 * If it is not supported then the seek function will return <0.
aa38b097 128 */
8e287af0
MN
129#define AVSEEK_SIZE 0x10000
130
de6d9b64
FB
131typedef struct URLProtocol {
132 const char *name;
133 int (*url_open)(URLContext *h, const char *filename, int flags);
134 int (*url_read)(URLContext *h, unsigned char *buf, int size);
135 int (*url_write)(URLContext *h, unsigned char *buf, int size);
136 offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
137 int (*url_close)(URLContext *h);
de6d9b64 138 struct URLProtocol *next;
502bdf68 139 int (*url_read_pause)(URLContext *h, int pause);
b075e520 140 offset_t (*url_read_seek)(URLContext *h,
536333a0 141 int stream_index, int64_t timestamp, int flags);
de6d9b64
FB
142} URLProtocol;
143
144extern URLProtocol *first_protocol;
019ac05a 145extern URLInterruptCB *url_interrupt_cb;
de6d9b64 146
84be6e72
MN
147URLProtocol *av_protocol_next(URLProtocol *p);
148
de6d9b64
FB
149int register_protocol(URLProtocol *protocol);
150
1308f273
MN
151/**
152 * Bytestream IO Context.
153 * New fields can be added to the end with minor version bumps.
8bfb108b 154 * Removal, reordering and changes to existing fields require a major
1308f273 155 * version bump.
8bfb108b 156 * sizeof(ByteIOContext) must not be used outside libav*.
1308f273 157 */
de6d9b64
FB
158typedef struct {
159 unsigned char *buffer;
160 int buffer_size;
161 unsigned char *buf_ptr, *buf_end;
162 void *opaque;
0c1a9eda 163 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
576ae256 164 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
536ffa59 165 offset_t (*seek)(void *opaque, offset_t offset, int whence);
c3316802
PI
166 offset_t pos; /**< position in the file of the current buffer */
167 int must_flush; /**< true if the next seek should flush */
168 int eof_reached; /**< true if eof reached */
169 int write_flag; /**< true if open for writing */
de6d9b64 170 int is_streamed;
f031df23 171 int max_packet_size;
ee9f36a8
MN
172 unsigned long checksum;
173 unsigned char *checksum_ptr;
174 unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
576ae256 175 int error; ///< contains the error code or 0 if no error happened
502bdf68 176 int (*read_pause)(void *opaque, int pause);
b075e520 177 offset_t (*read_seek)(void *opaque,
e7e4810a 178 int stream_index, int64_t timestamp, int flags);
de6d9b64
FB
179} ByteIOContext;
180
181int init_put_byte(ByteIOContext *s,
182 unsigned char *buffer,
183 int buffer_size,
184 int write_flag,
185 void *opaque,
1e0f3468
RD
186 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
187 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
188 offset_t (*seek)(void *opaque, offset_t offset, int whence));
189ByteIOContext *av_alloc_put_byte(
190 unsigned char *buffer,
191 int buffer_size,
192 int write_flag,
193 void *opaque,
0c1a9eda 194 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
576ae256 195 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
536ffa59 196 offset_t (*seek)(void *opaque, offset_t offset, int whence));
de6d9b64
FB
197
198void put_byte(ByteIOContext *s, int b);
75bdb984 199void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
0c1a9eda
ZK
200void put_le64(ByteIOContext *s, uint64_t val);
201void put_be64(ByteIOContext *s, uint64_t val);
de6d9b64
FB
202void put_le32(ByteIOContext *s, unsigned int val);
203void put_be32(ByteIOContext *s, unsigned int val);
ea395e8c 204void put_le24(ByteIOContext *s, unsigned int val);
a254c574 205void put_be24(ByteIOContext *s, unsigned int val);
de6d9b64
FB
206void put_le16(ByteIOContext *s, unsigned int val);
207void put_be16(ByteIOContext *s, unsigned int val);
0570bf06 208void put_tag(ByteIOContext *s, const char *tag);
de6d9b64 209
3b4b29dc 210void put_strz(ByteIOContext *s, const char *buf);
75bdb984 211
67e21020
MN
212/**
213 * fseek() equivalent for ByteIOContext.
214 * @return new position or AVERROR.
215 */
de6d9b64 216offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
67e21020
MN
217
218/**
219 * Skip given number of bytes forward.
220 * @param offset number of bytes
221 */
de6d9b64 222void url_fskip(ByteIOContext *s, offset_t offset);
67e21020
MN
223
224/**
225 * ftell() equivalent for ByteIOContext.
226 * @return position or AVERROR.
227 */
de6d9b64 228offset_t url_ftell(ByteIOContext *s);
67e21020
MN
229
230/**
231 * Gets the filesize.
232 * @return filesize or AVERROR
233 */
a965c478 234offset_t url_fsize(ByteIOContext *s);
67e21020
MN
235
236/**
237 * feof() equivalent for ByteIOContext.
238 * @return non zero if and only if end of file
239 */
de6d9b64 240int url_feof(ByteIOContext *s);
67e21020 241
576ae256 242int url_ferror(ByteIOContext *s);
de6d9b64 243
502bdf68 244int av_url_read_fpause(ByteIOContext *h, int pause);
b075e520 245offset_t av_url_read_fseek(ByteIOContext *h,
e7e4810a
BA
246 int stream_index, int64_t timestamp, int flags);
247
f031df23 248#define URL_EOF (-1)
c3316802 249/** @note return URL_EOF (-1) if EOF */
f031df23 250int url_fgetc(ByteIOContext *s);
a8c5ab27 251
c3316802 252/** @warning currently size is limited */
572f992e 253#ifdef __GNUC__
08f29f82 254int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
572f992e
MN
255#else
256int url_fprintf(ByteIOContext *s, const char *fmt, ...);
257#endif
a8c5ab27 258
c3316802 259/** @note unlike fgets, the EOL character is not returned and a whole
a8c5ab27 260 line is parsed. return NULL if first char read was EOF */
f031df23
FB
261char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
262
de6d9b64
FB
263void put_flush_packet(ByteIOContext *s);
264
4edfcecc
MN
265
266/**
267 * Reads size bytes from ByteIOContext into buf.
268 * @returns number of bytes read or AVERROR
269 */
de6d9b64 270int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
4edfcecc
MN
271
272/**
273 * Reads size bytes from ByteIOContext into buf.
4477dedc 274 * This reads at most 1 packet. If that is not enough fewer bytes will be
4edfcecc
MN
275 * returned.
276 * @returns number of bytes read or AVERROR
277 */
e15dec10 278int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
a8c5ab27 279
c3316802 280/** @note return 0 if EOF, so you cannot use it if EOF handling is
a8c5ab27 281 necessary */
de6d9b64 282int get_byte(ByteIOContext *s);
ea395e8c 283unsigned int get_le24(ByteIOContext *s);
de6d9b64 284unsigned int get_le32(ByteIOContext *s);
0c1a9eda 285uint64_t get_le64(ByteIOContext *s);
de6d9b64
FB
286unsigned int get_le16(ByteIOContext *s);
287
3b4b29dc 288char *get_strz(ByteIOContext *s, char *buf, int maxlen);
de6d9b64 289unsigned int get_be16(ByteIOContext *s);
a254c574 290unsigned int get_be24(ByteIOContext *s);
de6d9b64 291unsigned int get_be32(ByteIOContext *s);
0c1a9eda 292uint64_t get_be64(ByteIOContext *s);
de6d9b64 293
7798b42d 294uint64_t ff_get_v(ByteIOContext *bc);
897d3eef 295
7ac13f0c 296static inline int url_is_streamed(ByteIOContext *s)
de6d9b64
FB
297{
298 return s->is_streamed;
299}
de6d9b64 300
3576f9c6
BA
301/** @note when opened as read/write, the buffers are only used for
302 writing */
899681cd 303int url_fdopen(ByteIOContext **s, URLContext *h);
a8c5ab27 304
c3316802 305/** @warning must be called before any I/O */
de6d9b64 306int url_setbufsize(ByteIOContext *s, int buf_size);
770d9daf
BA
307/** Reset the buffer for reading or writing.
308 * @note Will drop any data currently in the buffer without transmitting it.
309 * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
310 * to set up the buffer for writing. */
311int url_resetbuf(ByteIOContext *s, int flags);
a8c5ab27 312
c3316802 313/** @note when opened as read/write, the buffers are only used for
3576f9c6 314 writing */
899681cd 315int url_fopen(ByteIOContext **s, const char *filename, int flags);
de6d9b64
FB
316int url_fclose(ByteIOContext *s);
317URLContext *url_fileno(ByteIOContext *s);
a8c5ab27 318
c3316802 319/**
a8c5ab27
PI
320 * Return the maximum packet size associated to packetized buffered file
321 * handle. If the file is not packetized (stream like http or file on
322 * disk), then 0 is returned.
323 *
abfe87d4 324 * @param s buffered file handle
a8c5ab27
PI
325 * @return maximum packet size in bytes
326 */
f031df23 327int url_fget_max_packet_size(ByteIOContext *s);
de6d9b64 328
899681cd 329int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
a8c5ab27 330
c3316802 331/** return the written or read size */
de6d9b64
FB
332int url_close_buf(ByteIOContext *s);
333
c3316802 334/**
a8c5ab27
PI
335 * Open a write only memory stream.
336 *
337 * @param s new IO context
338 * @return zero if no error.
339 */
899681cd 340int url_open_dyn_buf(ByteIOContext **s);
a8c5ab27 341
c3316802 342/**
a8c5ab27
PI
343 * Open a write only packetized memory stream with a maximum packet
344 * size of 'max_packet_size'. The stream is stored in a memory buffer
345 * with a big endian 4 byte header giving the packet size in bytes.
346 *
347 * @param s new IO context
348 * @param max_packet_size maximum packet size (must be > 0)
349 * @return zero if no error.
350 */
899681cd 351int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
a8c5ab27 352
c3316802 353/**
a8c5ab27
PI
354 * Return the written size and a pointer to the buffer. The buffer
355 * must be freed with av_free().
356 * @param s IO context
e8b7c70f 357 * @param pbuffer pointer to a byte buffer
a8c5ab27
PI
358 * @return the length of the byte buffer
359 */
0c1a9eda 360int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
f031df23 361
992e78f5 362unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len);
ee9f36a8
MN
363unsigned long get_checksum(ByteIOContext *s);
364void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
ee9f36a8 365
de6d9b64 366/* udp.c */
f031df23
FB
367int udp_set_remote_url(URLContext *h, const char *uri);
368int udp_get_local_port(URLContext *h);
369int udp_get_file_handle(URLContext *h);
370
5b21bdab 371#endif /* FFMPEG_AVIO_H */