forked from DeYangLiu/ffmpeg-streaming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompact.c
More file actions
216 lines (186 loc) · 4.59 KB
/
compact.c
File metadata and controls
216 lines (186 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "config.h"
#include "compact.h"
#include <stdarg.h>
#include <stdlib.h>
#if HAVE_GETTIMEOFDAY
#include <sys/time.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_WINDOWS_H
#include <windows.h>
#else
#include <fcntl.h> //F_SETFL
#endif
#if HAVE_WINSOCK2_H
int ff_neterrno(void)
{
int err = WSAGetLastError();
switch (err) {
case WSAEWOULDBLOCK:
return AVERROR(EAGAIN);
case WSAEINTR:
return AVERROR(EINTR);
case WSAEPROTONOSUPPORT:
return AVERROR(EPROTONOSUPPORT);
case WSAETIMEDOUT:
return AVERROR(ETIMEDOUT);
case WSAECONNREFUSED:
return AVERROR(ECONNREFUSED);
case WSAEINPROGRESS:
return AVERROR(EINPROGRESS);
}
return -err;
}
#endif
int ff_socket_nonblock(int socket, int enable)
{
#if HAVE_WINSOCK2_H
u_long param = enable;
return ioctlsocket(socket, FIONBIO, ¶m);
#else
if (enable)
return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
else
return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
#endif /* HAVE_WINSOCK2_H */
}
#if !HAVE_POLL_H
int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
{
fd_set read_set;
fd_set write_set;
fd_set exception_set;
nfds_t i;
int n;
int rc;
#if HAVE_WINSOCK2_H
if (numfds >= FD_SETSIZE) {
errno = EINVAL;
return -1;
}
#endif /* HAVE_WINSOCK2_H */
FD_ZERO(&read_set);
FD_ZERO(&write_set);
FD_ZERO(&exception_set);
n = 0;
for (i = 0; i < numfds; i++) {
if (fds[i].fd < 0)
continue;
#if !HAVE_WINSOCK2_H
if (fds[i].fd >= FD_SETSIZE) {
errno = EINVAL;
return -1;
}
#endif /* !HAVE_WINSOCK2_H */
if (fds[i].events & POLLIN)
FD_SET(fds[i].fd, &read_set);
if (fds[i].events & POLLOUT)
FD_SET(fds[i].fd, &write_set);
if (fds[i].events & POLLERR)
FD_SET(fds[i].fd, &exception_set);
if (fds[i].fd >= n)
n = fds[i].fd + 1;
}
if (n == 0)
/* Hey!? Nothing to poll, in fact!!! */
return 0;
if (timeout < 0) {
rc = select(n, &read_set, &write_set, &exception_set, NULL);
} else {
struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = 1000 * (timeout % 1000);
rc = select(n, &read_set, &write_set, &exception_set, &tv);
}
if (rc < 0)
return rc;
for (i = 0; i < numfds; i++) {
fds[i].revents = 0;
if (FD_ISSET(fds[i].fd, &read_set))
fds[i].revents |= POLLIN;
if (FD_ISSET(fds[i].fd, &write_set))
fds[i].revents |= POLLOUT;
if (FD_ISSET(fds[i].fd, &exception_set))
fds[i].revents |= POLLERR;
}
return rc;
}
#endif /* !HAVE_POLL_H */
void *av_malloc(size_t size)
{
return malloc(size);
}
void *av_mallocz(size_t size)
{
void *ptr = av_malloc(size);
if (ptr)
memset(ptr, 0, size);
return ptr;
}
void *av_realloc(void *ptr, size_t size)
{
return realloc(ptr, size + !size);
}
void av_freep(void *arg)
{
void *val;
memcpy(&val, arg, sizeof(val));
memcpy(arg, &(void *){ NULL }, sizeof(val));
av_free(val);
}
void av_free(void *ptr)
{
if(ptr)
free(ptr);
}
int64_t av_gettime(void)
{
#if HAVE_GETTIMEOFDAY
struct timeval tv;
gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
#elif HAVE_GETSYSTEMTIMEASFILETIME
FILETIME ft;
int64_t t;
GetSystemTimeAsFileTime(&ft);
t = (int64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime;
return t / 10 - 11644473600000000; /* Jan 1, 1601 */
#else
return -1;
#endif
}
int64_t av_gettime_relative(void)
{
#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
#else
return av_gettime() + 42 * 60 * 60 * INT64_C(1000000);
#endif
}
int av_gettime_relative_is_monotonic(void)
{
#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
return 1;
#else
return 0;
#endif
}
int av_usleep(unsigned usec)
{
#if HAVE_NANOSLEEP
struct timespec ts = { usec / 1000000, usec % 1000000 * 1000 };
while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
return 0;
#elif HAVE_USLEEP
return usleep(usec);
#elif HAVE_SLEEP
Sleep(usec / 1000);
return 0;
#else
return AVERROR(ENOSYS);
#endif
}