]> git.sesse.net Git - ffmpeg/blob - libavformat/os_support.c
lavfi: Add the af_channelmap audio channel mapping filter.
[ffmpeg] / libavformat / os_support.c
1 /*
2  * various OS-feature replacement utilities
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  * copyright (c) 2002 Francois Revol
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /* needed by inet_aton() */
24 #define _SVID_SOURCE
25
26 #include "config.h"
27 #include "avformat.h"
28 #include "os_support.h"
29
30 #if defined(_WIN32) && !defined(__MINGW32CE__)
31 #include <windows.h>
32
33 #undef open
34 int ff_win32_open(const char *filename_utf8, int oflag, int pmode)
35 {
36     int fd;
37     int num_chars;
38     wchar_t *filename_w;
39
40     /* convert UTF-8 to wide chars */
41     num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, NULL, 0);
42     if (num_chars <= 0)
43         return -1;
44     filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
45     MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
46
47     fd = _wopen(filename_w, oflag, pmode);
48     av_freep(&filename_w);
49
50     /* filename maybe be in CP_ACP */
51     if (fd == -1 && !(oflag & O_CREAT))
52         return open(filename_utf8, oflag, pmode);
53
54     return fd;
55 }
56 #endif
57
58 #if CONFIG_NETWORK
59 #include <fcntl.h>
60 #if !HAVE_POLL_H
61 #include <sys/time.h>
62 #if HAVE_WINSOCK2_H
63 #include <winsock2.h>
64 #elif HAVE_SYS_SELECT_H
65 #include <sys/select.h>
66 #endif
67 #endif
68
69 #include "network.h"
70
71 #if !HAVE_INET_ATON
72 #include <stdlib.h>
73
74 int ff_inet_aton (const char * str, struct in_addr * add)
75 {
76     unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
77
78     if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
79         return 0;
80
81     if (!add1 || (add1|add2|add3|add4) > 255) return 0;
82
83     add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
84
85     return 1;
86 }
87 #else
88 int ff_inet_aton (const char * str, struct in_addr * add)
89 {
90     return inet_aton(str, add);
91 }
92 #endif /* !HAVE_INET_ATON */
93
94 #if !HAVE_GETADDRINFO
95 int ff_getaddrinfo(const char *node, const char *service,
96                 const struct addrinfo *hints, struct addrinfo **res)
97 {
98     struct hostent *h = NULL;
99     struct addrinfo *ai;
100     struct sockaddr_in *sin;
101
102 #if HAVE_WINSOCK2_H
103     int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
104                                   const struct addrinfo *hints,
105                                   struct addrinfo **res);
106     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
107     win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
108     if (win_getaddrinfo)
109         return win_getaddrinfo(node, service, hints, res);
110 #endif
111
112     *res = NULL;
113     sin = av_mallocz(sizeof(struct sockaddr_in));
114     if (!sin)
115         return EAI_FAIL;
116     sin->sin_family = AF_INET;
117
118     if (node) {
119         if (!ff_inet_aton(node, &sin->sin_addr)) {
120             if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
121                 av_free(sin);
122                 return EAI_FAIL;
123             }
124             h = gethostbyname(node);
125             if (!h) {
126                 av_free(sin);
127                 return EAI_FAIL;
128             }
129             memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
130         }
131     } else {
132         if (hints && (hints->ai_flags & AI_PASSIVE)) {
133             sin->sin_addr.s_addr = INADDR_ANY;
134         } else
135             sin->sin_addr.s_addr = INADDR_LOOPBACK;
136     }
137
138     /* Note: getaddrinfo allows service to be a string, which
139      * should be looked up using getservbyname. */
140     if (service)
141         sin->sin_port = htons(atoi(service));
142
143     ai = av_mallocz(sizeof(struct addrinfo));
144     if (!ai) {
145         av_free(sin);
146         return EAI_FAIL;
147     }
148
149     *res = ai;
150     ai->ai_family = AF_INET;
151     ai->ai_socktype = hints ? hints->ai_socktype : 0;
152     switch (ai->ai_socktype) {
153     case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
154     case SOCK_DGRAM:  ai->ai_protocol = IPPROTO_UDP; break;
155     default:          ai->ai_protocol = 0;           break;
156     }
157
158     ai->ai_addr = (struct sockaddr *)sin;
159     ai->ai_addrlen = sizeof(struct sockaddr_in);
160     if (hints && (hints->ai_flags & AI_CANONNAME))
161         ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
162
163     ai->ai_next = NULL;
164     return 0;
165 }
166
167 void ff_freeaddrinfo(struct addrinfo *res)
168 {
169 #if HAVE_WINSOCK2_H
170     void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
171     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
172     win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
173                        GetProcAddress(ws2mod, "freeaddrinfo");
174     if (win_freeaddrinfo) {
175         win_freeaddrinfo(res);
176         return;
177     }
178 #endif
179
180     av_free(res->ai_canonname);
181     av_free(res->ai_addr);
182     av_free(res);
183 }
184
185 int ff_getnameinfo(const struct sockaddr *sa, int salen,
186                    char *host, int hostlen,
187                    char *serv, int servlen, int flags)
188 {
189     const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
190
191 #if HAVE_WINSOCK2_H
192     int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
193                                   char *host, DWORD hostlen,
194                                   char *serv, DWORD servlen, int flags);
195     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
196     win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
197     if (win_getnameinfo)
198         return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
199 #endif
200
201     if (sa->sa_family != AF_INET)
202         return EAI_FAMILY;
203     if (!host && !serv)
204         return EAI_NONAME;
205
206     if (host && hostlen > 0) {
207         struct hostent *ent = NULL;
208         uint32_t a;
209         if (!(flags & NI_NUMERICHOST))
210             ent = gethostbyaddr((const char *)&sin->sin_addr,
211                                 sizeof(sin->sin_addr), AF_INET);
212
213         if (ent) {
214             snprintf(host, hostlen, "%s", ent->h_name);
215         } else if (flags & NI_NAMERQD) {
216             return EAI_NONAME;
217         } else {
218             a = ntohl(sin->sin_addr.s_addr);
219             snprintf(host, hostlen, "%d.%d.%d.%d",
220                      ((a >> 24) & 0xff), ((a >> 16) & 0xff),
221                      ((a >>  8) & 0xff), ( a        & 0xff));
222         }
223     }
224
225     if (serv && servlen > 0) {
226         struct servent *ent = NULL;
227         if (!(flags & NI_NUMERICSERV))
228             ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
229
230         if (ent) {
231             snprintf(serv, servlen, "%s", ent->s_name);
232         } else
233             snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
234     }
235
236     return 0;
237 }
238
239 const char *ff_gai_strerror(int ecode)
240 {
241     switch(ecode) {
242     case EAI_FAIL   : return "A non-recoverable error occurred";
243     case EAI_FAMILY : return "The address family was not recognized or the address length was invalid for the specified family";
244     case EAI_NONAME : return "The name does not resolve for the supplied parameters";
245     }
246
247     return "Unknown error";
248 }
249 #endif
250
251 int ff_socket_nonblock(int socket, int enable)
252 {
253 #if HAVE_WINSOCK2_H
254    u_long param = enable;
255    return ioctlsocket(socket, FIONBIO, &param);
256 #else
257    if (enable)
258       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
259    else
260       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
261 #endif
262 }
263
264 #if !HAVE_POLL_H
265 int poll(struct pollfd *fds, nfds_t numfds, int timeout)
266 {
267     fd_set read_set;
268     fd_set write_set;
269     fd_set exception_set;
270     nfds_t i;
271     int n;
272     int rc;
273
274 #if HAVE_WINSOCK2_H
275     if (numfds >= FD_SETSIZE) {
276         errno = EINVAL;
277         return -1;
278     }
279 #endif
280
281     FD_ZERO(&read_set);
282     FD_ZERO(&write_set);
283     FD_ZERO(&exception_set);
284
285     n = -1;
286     for(i = 0; i < numfds; i++) {
287         if (fds[i].fd < 0)
288             continue;
289 #if !HAVE_WINSOCK2_H
290         if (fds[i].fd >= FD_SETSIZE) {
291             errno = EINVAL;
292             return -1;
293         }
294 #endif
295
296         if (fds[i].events & POLLIN)  FD_SET(fds[i].fd, &read_set);
297         if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
298         if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
299
300         if (fds[i].fd > n)
301             n = fds[i].fd;
302     };
303
304     if (n == -1)
305         /* Hey!? Nothing to poll, in fact!!! */
306         return 0;
307
308     if (timeout < 0)
309         rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
310     else {
311         struct timeval    tv;
312
313         tv.tv_sec = timeout / 1000;
314         tv.tv_usec = 1000 * (timeout % 1000);
315         rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
316     };
317
318     if (rc < 0)
319         return rc;
320
321     for(i = 0; i < numfds; i++) {
322         fds[i].revents = 0;
323
324         if (FD_ISSET(fds[i].fd, &read_set))      fds[i].revents |= POLLIN;
325         if (FD_ISSET(fds[i].fd, &write_set))     fds[i].revents |= POLLOUT;
326         if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
327     };
328
329     return rc;
330 }
331 #endif /* HAVE_POLL_H */
332 #endif /* CONFIG_NETWORK */