48399d081f7df1d85b91815f0560e1e78b27b0b3
2 * Unbuffered io for ffmpeg system
3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 /* needed for usleep() */
23 #define _XOPEN_SOURCE 600
25 #include "libavutil/avstring.h"
26 #include "libavcodec/opt.h"
27 #include "os_support.h"
33 #if LIBAVFORMAT_VERSION_MAJOR >= 53
34 /** @name Logging context. */
36 static const char *urlcontext_to_name(void *ptr
)
38 URLContext
*h
= (URLContext
*)ptr
;
39 if(h
->prot
) return h
->prot
->name
;
42 static const AVOption options
[] = {{NULL
}};
43 static const AVClass urlcontext_class
=
44 { "URLContext", urlcontext_to_name
, options
, LIBAVUTIL_VERSION_INT
};
48 static int default_interrupt_cb(void);
50 URLProtocol
*first_protocol
= NULL
;
51 URLInterruptCB
*url_interrupt_cb
= default_interrupt_cb
;
53 URLProtocol
*av_protocol_next(URLProtocol
*p
)
56 else return first_protocol
;
59 int av_register_protocol(URLProtocol
*protocol
)
63 while (*p
!= NULL
) p
= &(*p
)->next
;
65 protocol
->next
= NULL
;
69 #if LIBAVFORMAT_VERSION_MAJOR < 53
70 int register_protocol(URLProtocol
*protocol
)
72 return av_register_protocol(protocol
);
76 int url_open_protocol (URLContext
**puc
, struct URLProtocol
*up
,
77 const char *filename
, int flags
)
83 if (!ff_network_init())
86 uc
= av_mallocz(sizeof(URLContext
) + strlen(filename
) + 1);
88 err
= AVERROR(ENOMEM
);
91 #if LIBAVFORMAT_VERSION_MAJOR >= 53
92 uc
->av_class
= &urlcontext_class
;
94 uc
->filename
= (char *) &uc
[1];
95 strcpy(uc
->filename
, filename
);
98 uc
->is_streamed
= 0; /* default = not streamed */
99 uc
->max_packet_size
= 0; /* default: stream file */
100 err
= up
->url_open(uc
, filename
, flags
);
106 //We must be careful here as url_seek() could be slow, for example for http
107 if( (flags
& (URL_WRONLY
| URL_RDWR
))
108 || !strcmp(up
->name
, "file"))
109 if(!uc
->is_streamed
&& url_seek(uc
, 0, SEEK_SET
) < 0)
121 int url_open(URLContext
**puc
, const char *filename
, int flags
)
125 char proto_str
[128], *q
;
129 while (*p
!= '\0' && *p
!= ':') {
130 /* protocols can only contain alphabetic chars */
133 if ((q
- proto_str
) < sizeof(proto_str
) - 1)
137 /* if the protocol has length 1, we consider it is a dos drive */
138 if (*p
== '\0' || is_dos_path(filename
)) {
140 strcpy(proto_str
, "file");
147 if (!strcmp(proto_str
, up
->name
))
148 return url_open_protocol (puc
, up
, filename
, flags
);
152 return AVERROR(ENOENT
);
155 int url_read(URLContext
*h
, unsigned char *buf
, int size
)
158 if (h
->flags
& URL_WRONLY
)
160 ret
= h
->prot
->url_read(h
, buf
, size
);
164 int url_read_complete(URLContext
*h
, unsigned char *buf
, int size
)
167 int fast_retries
= 5;
171 ret
= url_read(h
, buf
+len
, size
-len
);
172 if (ret
== AVERROR(EAGAIN
)) {
179 return ret
< 0 ? ret
: len
;
181 fast_retries
= FFMAX(fast_retries
, 2);
187 int url_write(URLContext
*h
, unsigned char *buf
, int size
)
190 if (!(h
->flags
& (URL_WRONLY
| URL_RDWR
)))
192 /* avoid sending too big packets */
193 if (h
->max_packet_size
&& size
> h
->max_packet_size
)
195 ret
= h
->prot
->url_write(h
, buf
, size
);
199 int64_t url_seek(URLContext
*h
, int64_t pos
, int whence
)
203 if (!h
->prot
->url_seek
)
204 return AVERROR(ENOSYS
);
205 ret
= h
->prot
->url_seek(h
, pos
, whence
& ~AVSEEK_FORCE
);
209 int url_close(URLContext
*h
)
212 if (!h
) return 0; /* can happen when url_open fails */
214 if (h
->prot
->url_close
)
215 ret
= h
->prot
->url_close(h
);
223 int url_exist(const char *filename
)
226 if (url_open(&h
, filename
, URL_RDONLY
) < 0)
232 int64_t url_filesize(URLContext
*h
)
236 size
= url_seek(h
, 0, AVSEEK_SIZE
);
238 pos
= url_seek(h
, 0, SEEK_CUR
);
239 if ((size
= url_seek(h
, -1, SEEK_END
)) < 0)
242 url_seek(h
, pos
, SEEK_SET
);
247 int url_get_file_handle(URLContext
*h
)
249 if (!h
->prot
->url_get_file_handle
)
251 return h
->prot
->url_get_file_handle(h
);
254 int url_get_max_packet_size(URLContext
*h
)
256 return h
->max_packet_size
;
259 void url_get_filename(URLContext
*h
, char *buf
, int buf_size
)
261 av_strlcpy(buf
, h
->filename
, buf_size
);
265 static int default_interrupt_cb(void)
270 void url_set_interrupt_cb(URLInterruptCB
*interrupt_cb
)
273 interrupt_cb
= default_interrupt_cb
;
274 url_interrupt_cb
= interrupt_cb
;
277 int av_url_read_pause(URLContext
*h
, int pause
)
279 if (!h
->prot
->url_read_pause
)
280 return AVERROR(ENOSYS
);
281 return h
->prot
->url_read_pause(h
, pause
);
284 int64_t av_url_read_seek(URLContext
*h
,
285 int stream_index
, int64_t timestamp
, int flags
)
287 if (!h
->prot
->url_read_seek
)
288 return AVERROR(ENOSYS
);
289 return h
->prot
->url_read_seek(h
, stream_index
, timestamp
, flags
);