]> git.sesse.net Git - ffmpeg/blob - libavformat/os_support.c
23fc922e8444096d802fb23456d2d7f89e57f522
[ffmpeg] / libavformat / os_support.c
1 /*
2  * Various utilities for ffmpeg system
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  * copyright (c) 2002 Francois Revol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #include "config.h"
23 #include "avformat.h"
24 #if !defined(__MINGW32__)
25 #include <unistd.h>
26 #include <fcntl.h>
27 #endif
28 #include <sys/time.h>
29 #include <time.h>
30
31 #ifndef HAVE_SYS_POLL_H
32 #if defined(__MINGW32__)
33 #include <winsock2.h>
34 #else
35 #include <sys/select.h>
36 #endif
37 #endif
38
39 /**
40  * gets the current time in micro seconds.
41  */
42 int64_t av_gettime(void)
43 {
44     struct timeval tv;
45     gettimeofday(&tv,NULL);
46     return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
47 }
48
49 #ifdef CONFIG_NETWORK
50 #include "network.h"
51
52 #if !defined(HAVE_INET_ATON)
53 #include <stdlib.h>
54 #include <strings.h>
55
56 int inet_aton (const char * str, struct in_addr * add)
57 {
58     const char * pch = str;
59     unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
60
61     add1 = atoi(pch);
62     pch = strpbrk(pch,".");
63     if (pch == 0 || ++pch == 0) goto done;
64     add2 = atoi(pch);
65     pch = strpbrk(pch,".");
66     if (pch == 0 || ++pch == 0) goto done;
67     add3 = atoi(pch);
68     pch = strpbrk(pch,".");
69     if (pch == 0 || ++pch == 0) goto done;
70     add4 = atoi(pch);
71
72 done:
73     add->s_addr=(add4<<24)+(add3<<16)+(add2<<8)+add1;
74
75     return 1;
76 }
77 #endif /* !defined(HAVE_INET_ATON) */
78
79 /* resolve host with also IP address parsing */
80 int resolve_host(struct in_addr *sin_addr, const char *hostname)
81 {
82     struct hostent *hp;
83
84     if (!inet_aton(hostname, sin_addr)) {
85         hp = gethostbyname(hostname);
86         if (!hp)
87             return -1;
88         memcpy(sin_addr, hp->h_addr, sizeof(struct in_addr));
89     }
90     return 0;
91 }
92
93 int ff_socket_nonblock(int socket, int enable)
94 {
95 #ifdef __MINGW32__
96    return ioctlsocket(socket, FIONBIO, &enable);
97 #else
98    if (enable)
99       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
100    else
101       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
102 #endif
103 }
104 #endif /* CONFIG_NETWORK */
105
106 #ifdef CONFIG_FFSERVER
107 #ifndef HAVE_SYS_POLL_H
108 int poll(struct pollfd *fds, nfds_t numfds, int timeout)
109 {
110     fd_set read_set;
111     fd_set write_set;
112     fd_set exception_set;
113     nfds_t i;
114     int n;
115     int rc;
116
117 #ifdef __MINGW32__
118     if (numfds >= FD_SETSIZE) {
119         errno = EINVAL;
120         return -1;
121     }
122 #endif
123
124     FD_ZERO(&read_set);
125     FD_ZERO(&write_set);
126     FD_ZERO(&exception_set);
127
128     n = -1;
129     for(i = 0; i < numfds; i++) {
130         if (fds[i].fd < 0)
131             continue;
132 #ifndef __MINGW32__
133         if (fds[i].fd >= FD_SETSIZE) {
134             errno = EINVAL;
135             return -1;
136         }
137 #endif
138
139         if (fds[i].events & POLLIN)  FD_SET(fds[i].fd, &read_set);
140         if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
141         if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
142
143         if (fds[i].fd > n)
144             n = fds[i].fd;
145     };
146
147     if (n == -1)
148         /* Hey!? Nothing to poll, in fact!!! */
149         return 0;
150
151     if (timeout < 0)
152         rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
153     else {
154         struct timeval    tv;
155
156         tv.tv_sec = timeout / 1000;
157         tv.tv_usec = 1000 * (timeout % 1000);
158         rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
159     };
160
161     if (rc < 0)
162         return rc;
163
164     for(i = 0; i < (nfds_t) n; i++) {
165         fds[i].revents = 0;
166
167         if (FD_ISSET(fds[i].fd, &read_set))      fds[i].revents |= POLLIN;
168         if (FD_ISSET(fds[i].fd, &write_set))     fds[i].revents |= POLLOUT;
169         if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
170     };
171
172     return rc;
173 }
174 #endif /* HAVE_SYS_POLL_H */
175 #endif /* CONFIG_FFSERVER */
176