Force blocking stdout

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2022-07-19 21:45:36 +02:00
parent 8c47f2726c
commit 770deae6cd
2 changed files with 41 additions and 0 deletions

View file

@ -48,6 +48,39 @@ static void convert_for_ffmpeg(const uint8_t *in, uint8_t *out,
} }
} }
static int wait_fd_event(int fd, int events)
{
struct pollfd pfd;
int ret;
for (;;) {
pfd.fd = fd;
pfd.events = events;
pfd.revents = 0;
ret = poll(&pfd, 1, -1);
if (ret > 0) {
if (pfd.revents & events)
break;
if (pfd.revents & (POLLERR | POLLHUP)) {
fputs("poll reported error\n", stderr);
return -1;
}
}
if (ret < 0) {
if (errno == EINTR)
continue;
perror("poll");
return -1;
}
}
return 0;
}
static int write_retry(int fd, const void *buffer, size_t size) static int write_retry(int fd, const void *buffer, size_t size)
{ {
while (size > 0) { while (size > 0) {
@ -56,6 +89,13 @@ static int write_retry(int fd, const void *buffer, size_t size)
if (ret < 0) { if (ret < 0) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
if (errno == EAGAIN) {
if (wait_fd_event(fd, POLLOUT))
return -1;
continue;
}
perror("write"); perror("write");
return -1; return -1;
} }

View file

@ -23,6 +23,7 @@
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <poll.h>
#include <time.h> #include <time.h>
#define AUDIO_SAMPLES (512) #define AUDIO_SAMPLES (512)