FFmpeg  3.4.9
network.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 The FFmpeg Project
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef AVFORMAT_NETWORK_H
22 #define AVFORMAT_NETWORK_H
23 
24 #include <errno.h>
25 #include <stdint.h>
26 
27 #include "config.h"
28 #include "libavutil/error.h"
29 #include "os_support.h"
30 #include "avio.h"
31 #include "url.h"
32 
33 #if HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 
37 #if HAVE_WINSOCK2_H
38 #include <winsock2.h>
39 #include <ws2tcpip.h>
40 
41 #ifndef EPROTONOSUPPORT
42 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
43 #endif
44 #ifndef ETIMEDOUT
45 #define ETIMEDOUT WSAETIMEDOUT
46 #endif
47 #ifndef ECONNREFUSED
48 #define ECONNREFUSED WSAECONNREFUSED
49 #endif
50 #ifndef EINPROGRESS
51 #define EINPROGRESS WSAEINPROGRESS
52 #endif
53 #ifndef ENOTCONN
54 #define ENOTCONN WSAENOTCONN
55 #endif
56 
57 #define getsockopt(a, b, c, d, e) getsockopt(a, b, c, (char*) d, e)
58 #define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char*) d, e)
59 
60 int ff_neterrno(void);
61 #else
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <netdb.h>
66 
67 #define ff_neterrno() AVERROR(errno)
68 #endif /* HAVE_WINSOCK2_H */
69 
70 #if HAVE_ARPA_INET_H
71 #include <arpa/inet.h>
72 #endif
73 
74 #if HAVE_POLL_H
75 #include <poll.h>
76 #endif
77 
78 int ff_socket_nonblock(int socket, int enable);
79 
81 int ff_network_init(void);
82 void ff_network_close(void);
83 
84 int ff_tls_init(void);
85 void ff_tls_deinit(void);
86 
87 int ff_network_wait_fd(int fd, int write);
88 
89 /**
90  * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
91  * Uses ff_network_wait_fd in a loop
92  *
93  * @fd Socket descriptor
94  * @write Set 1 to wait for socket able to be read, 0 to be written
95  * @timeout Timeout interval, in microseconds. Actual precision is 100000 mcs, due to ff_network_wait_fd usage
96  * @param int_cb Interrupt callback, is checked before each ff_network_wait_fd call
97  * @return 0 if data can be read/written, AVERROR(ETIMEDOUT) if timeout expired, or negative error code
98  */
99 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
100 
101 int ff_inet_aton (const char * str, struct in_addr * add);
102 
103 #if !HAVE_STRUCT_SOCKADDR_STORAGE
105 #if HAVE_STRUCT_SOCKADDR_SA_LEN
106  uint8_t ss_len;
108 #else
109  uint16_t ss_family;
110 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
111  char ss_pad1[6];
112  int64_t ss_align;
113  char ss_pad2[112];
114 };
115 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
116 
117 typedef union sockaddr_union {
118  struct sockaddr_storage storage;
119  struct sockaddr_in in;
120 #if HAVE_STRUCT_SOCKADDR_IN6
121  struct sockaddr_in6 in6;
122 #endif
124 
125 #ifndef MSG_NOSIGNAL
126 #define MSG_NOSIGNAL 0
127 #endif
128 
129 #if !HAVE_STRUCT_ADDRINFO
130 struct addrinfo {
131  int ai_flags;
136  struct sockaddr *ai_addr;
138  struct addrinfo *ai_next;
139 };
140 #endif /* !HAVE_STRUCT_ADDRINFO */
141 
142 /* getaddrinfo constants */
143 #ifndef EAI_AGAIN
144 #define EAI_AGAIN 2
145 #endif
146 #ifndef EAI_BADFLAGS
147 #define EAI_BADFLAGS 3
148 #endif
149 #ifndef EAI_FAIL
150 #define EAI_FAIL 4
151 #endif
152 #ifndef EAI_FAMILY
153 #define EAI_FAMILY 5
154 #endif
155 #ifndef EAI_MEMORY
156 #define EAI_MEMORY 6
157 #endif
158 #ifndef EAI_NODATA
159 #define EAI_NODATA 7
160 #endif
161 #ifndef EAI_NONAME
162 #define EAI_NONAME 8
163 #endif
164 #ifndef EAI_SERVICE
165 #define EAI_SERVICE 9
166 #endif
167 #ifndef EAI_SOCKTYPE
168 #define EAI_SOCKTYPE 10
169 #endif
170 
171 #ifndef AI_PASSIVE
172 #define AI_PASSIVE 1
173 #endif
174 
175 #ifndef AI_CANONNAME
176 #define AI_CANONNAME 2
177 #endif
178 
179 #ifndef AI_NUMERICHOST
180 #define AI_NUMERICHOST 4
181 #endif
182 
183 #ifndef NI_NOFQDN
184 #define NI_NOFQDN 1
185 #endif
186 
187 #ifndef NI_NUMERICHOST
188 #define NI_NUMERICHOST 2
189 #endif
190 
191 #ifndef NI_NAMERQD
192 #define NI_NAMERQD 4
193 #endif
194 
195 #ifndef NI_NUMERICSERV
196 #define NI_NUMERICSERV 8
197 #endif
198 
199 #ifndef NI_DGRAM
200 #define NI_DGRAM 16
201 #endif
202 
203 #if !HAVE_GETADDRINFO
204 int ff_getaddrinfo(const char *node, const char *service,
205  const struct addrinfo *hints, struct addrinfo **res);
206 void ff_freeaddrinfo(struct addrinfo *res);
207 int ff_getnameinfo(const struct sockaddr *sa, int salen,
208  char *host, int hostlen,
209  char *serv, int servlen, int flags);
210 #define getaddrinfo ff_getaddrinfo
211 #define freeaddrinfo ff_freeaddrinfo
212 #define getnameinfo ff_getnameinfo
213 #endif /* !HAVE_GETADDRINFO */
214 
215 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
216 const char *ff_gai_strerror(int ecode);
217 #undef gai_strerror
218 #define gai_strerror ff_gai_strerror
219 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
220 
221 #ifndef INADDR_LOOPBACK
222 #define INADDR_LOOPBACK 0x7f000001
223 #endif
224 
225 #ifndef INET_ADDRSTRLEN
226 #define INET_ADDRSTRLEN 16
227 #endif
228 
229 #ifndef INET6_ADDRSTRLEN
230 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
231 #endif
232 
233 #ifndef IN_MULTICAST
234 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
235 #endif
236 #ifndef IN6_IS_ADDR_MULTICAST
237 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
238 #endif
239 
240 int ff_is_multicast_address(struct sockaddr *addr);
241 
242 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
243 
244 /**
245  * Bind to a file descriptor and poll for a connection.
246  *
247  * @param fd First argument of bind().
248  * @param addr Second argument of bind().
249  * @param addrlen Third argument of bind().
250  * @param timeout Polling timeout in milliseconds.
251  * @param h URLContext providing interrupt check
252  * callback and logging context.
253  * @return A non-blocking file descriptor on success
254  * or an AVERROR on failure.
255  */
256 int ff_listen_bind(int fd, const struct sockaddr *addr,
257  socklen_t addrlen, int timeout,
258  URLContext *h);
259 
260 /**
261  * Bind to a file descriptor to an address without accepting connections.
262  * @param fd First argument of bind().
263  * @param addr Second argument of bind().
264  * @param addrlen Third argument of bind().
265  * @return 0 on success or an AVERROR on failure.
266  */
267 int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen);
268 
269 /**
270  * Poll for a single connection on the passed file descriptor.
271  * @param fd The listening socket file descriptor.
272  * @param timeout Polling timeout in milliseconds.
273  * @param h URLContext providing interrupt check
274  * callback and logging context.
275  * @return A non-blocking file descriptor on success
276  * or an AVERROR on failure.
277  */
278 int ff_accept(int fd, int timeout, URLContext *h);
279 
280 /**
281  * Connect to a file descriptor and poll for result.
282  *
283  * @param fd First argument of connect(),
284  * will be set as non-blocking.
285  * @param addr Second argument of connect().
286  * @param addrlen Third argument of connect().
287  * @param timeout Polling timeout in milliseconds.
288  * @param h URLContext providing interrupt check
289  * callback and logging context.
290  * @param will_try_next Whether the caller will try to connect to another
291  * address for the same host name, affecting the form of
292  * logged errors.
293  * @return 0 on success, AVERROR on failure.
294  */
295 int ff_listen_connect(int fd, const struct sockaddr *addr,
296  socklen_t addrlen, int timeout,
297  URLContext *h, int will_try_next);
298 
299 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
300 
301 int ff_socket(int domain, int type, int protocol);
302 
303 #endif /* AVFORMAT_NETWORK_H */
Buffered I/O operations.
char ss_pad1[6]
Definition: network.h:111
int ff_network_wait_fd(int fd, int write)
Definition: network.c:73
int ff_getnameinfo(const struct sockaddr *sa, int salen, char *host, int hostlen, char *serv, int servlen, int flags)
int ff_is_multicast_address(struct sockaddr *addr)
Definition: network.c:131
int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
This works similarly to ff_network_wait_fd, but waits up to &#39;timeout&#39; microseconds Uses ff_network_wa...
Definition: network.c:82
uint8_t
miscellaneous OS support macros and functions.
uint16_t ss_family
Definition: network.h:109
static int flags
Definition: log.c:57
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:307
void ff_network_close(void)
Definition: network.c:102
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
int ff_tls_init(void)
Definition: network.c:30
error code definitions
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:464
const char * ff_gai_strerror(int ecode)
char * ai_canonname
Definition: network.h:137
void ff_freeaddrinfo(struct addrinfo *res)
int ai_addrlen
Definition: network.h:135
int ff_inet_aton(const char *str, struct in_addr *add)
#define ff_neterrno()
Definition: network.h:67
int ff_listen_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next)
Connect to a file descriptor and poll for result.
Definition: network.c:238
int ff_socket_nonblock(int socket, int enable)
int64_t ss_align
Definition: network.h:112
int ff_accept(int fd, int timeout, URLContext *h)
Poll for a single connection on the passed file descriptor.
Definition: network.c:208
int ff_listen_bind(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h)
Bind to a file descriptor and poll for a connection.
Definition: network.c:226
Definition: url.h:38
int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen)
Bind to a file descriptor to an address without accepting connections.
Definition: network.c:190
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
int ai_protocol
Definition: network.h:134
void ff_tls_deinit(void)
Definition: network.c:43
int ai_socktype
Definition: network.h:133
int ff_network_inited_globally
Definition: network.c:53
struct addrinfo * ai_next
Definition: network.h:138
int ff_network_init(void)
Definition: network.c:55
char ss_pad2[112]
Definition: network.h:113
int ff_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
int ff_socket(int domain, int type, int protocol)
Definition: network.c:166
int ai_flags
Definition: network.h:131
unbuffered private I/O API
struct sockaddr * ai_addr
Definition: network.h:136
int ai_family
Definition: network.h:132