Reopen FIFO if closed or deleted

master
Adrian Rossiter 8 years ago
parent a332c6a781
commit ad82c867b9
  1. 17
      input/fifo.c

@ -15,6 +15,14 @@ struct audio_data {
char error_message[1024]; char error_message[1024];
}; };
int open_fifo(const char *path)
{
int fd = open(path, O_RDONLY);
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
return fd;
}
//input: FIFO //input: FIFO
void* input_fifo(void* data) void* input_fifo(void* data)
@ -28,27 +36,26 @@ void* input_fifo(void* data)
int t = 0; int t = 0;
//int size = 1024; //int size = 1024;
int bytes = 0; int bytes = 0;
int flags;
int16_t buf[BUFSIZE / 2]; int16_t buf[BUFSIZE / 2];
struct timespec req = { .tv_sec = 0, .tv_nsec = 10000000 }; struct timespec req = { .tv_sec = 0, .tv_nsec = 10000000 };
fd = open(audio->source, O_RDONLY); fd = open_fifo(audio->source);
flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
while (1) { while (1) {
bytes = read(fd, buf, sizeof(buf)); bytes = read(fd, buf, sizeof(buf));
if (bytes == -1) { //if no bytes read sleep 10ms and zero shared buffer if (bytes < 1) { //if no bytes read sleep 10ms and zero shared buffer
nanosleep (&req, NULL); nanosleep (&req, NULL);
t++; t++;
if (t > 10) { if (t > 10) {
for (i = 0; i < 2048; i++)audio->audio_out_l[i] = 0; for (i = 0; i < 2048; i++)audio->audio_out_l[i] = 0;
for (i = 0; i < 2048; i++)audio->audio_out_r[i] = 0; for (i = 0; i < 2048; i++)audio->audio_out_r[i] = 0;
close(fd);
fd = open_fifo(audio->source);
t = 0; t = 0;
} }
} else { //if bytes read go ahead } else { //if bytes read go ahead

Loading…
Cancel
Save