@@ -148,7 +148,19 @@ static void close_platform()
148148/*----------------------------------------------------------------------------*/
149149static void * CmdPipeReaderThread (void * args )
150150{
151- cmdPipeFd = open (cmdPipeName , O_RDONLY );
151+ // Open with O_NONBLOCK to avoid blocking if no writer is present
152+ cmdPipeFd = open (cmdPipeName , O_RDONLY | O_NONBLOCK );
153+ if (cmdPipeFd == -1 )
154+ {
155+ LOG_ERROR ("Failed to open command pipe: %s" , cmdPipeName );
156+ return NULL ;
157+ }
158+
159+ // Set back to blocking mode after opening
160+ int file_flags = fcntl (cmdPipeFd , F_GETFL );
161+ fcntl (cmdPipeFd , F_SETFL , file_flags & ~O_NONBLOCK );
162+ LOG_INFO ("Command pipe opened successfully" );
163+
152164 struct
153165 {
154166 char * title ;
@@ -164,7 +176,9 @@ static void *CmdPipeReaderThread(void *args)
164176 if (!glMainRunning )
165177 break ;
166178
167- if (read (cmdPipeFd , cmdPipeBuf , 512 ) > 0 )
179+ ssize_t bytes_read = read (cmdPipeFd , cmdPipeBuf , 512 );
180+
181+ if (bytes_read > 0 )
168182 {
169183 // read lines
170184 char * save_ptr1 , * save_ptr2 ;
@@ -296,8 +310,30 @@ static void *CmdPipeReaderThread(void *args)
296310 // clear cmdPipeBuf
297311 memset (cmdPipeBuf , 0 , sizeof cmdPipeBuf );
298312 }
313+ else if (bytes_read == 0 )
314+ {
315+ // EOF - writer closed the pipe, reopen to wait for next writer
316+ LOG_DEBUG ("Command pipe writer disconnected, waiting for reconnection..." );
317+ close (cmdPipeFd );
318+
319+ // Reopen with O_NONBLOCK to avoid blocking
320+ cmdPipeFd = open (cmdPipeName , O_RDONLY | O_NONBLOCK );
321+ if (cmdPipeFd == -1 )
322+ {
323+ LOG_ERROR ("Failed to reopen command pipe: %s" , cmdPipeName );
324+ break ;
325+ }
326+
327+ // Set back to blocking mode for reads
328+ int file_flags = fcntl (cmdPipeFd , F_GETFL );
329+ fcntl (cmdPipeFd , F_SETFL , file_flags & ~O_NONBLOCK );
330+
331+ // Small delay before trying to read again
332+ usleep (100 * 1000 ); // 100ms
333+ }
299334 else
300335 {
336+ // Error on read, sleep and retry
301337 usleep (250 * 1000 );
302338 }
303339 }
@@ -670,15 +706,13 @@ int main(int argc, char *argv[])
670706
671707 buf = malloc (DEFAULT_FRAMES_PER_CHUNK * 4 );
672708 uint32_t KeepAlive = 0 ;
709+ bool got_eof = false;
673710
674711 // keep reading audio from stdin until exit/EOF
675- while (n || raopcl_is_playing (raopcl ))
712+ while (status != STOPPED && (! got_eof || raopcl_is_playing (raopcl ) ))
676713 {
677714 uint64_t playtime , now ;
678715
679- if (status == STOPPED )
680- break ;
681-
682716 now = raopcl_get_ntp (NULL );
683717
684718 // execute every second
@@ -701,11 +735,30 @@ int main(int argc, char *argv[])
701735 {
702736 n = read (infile , buf , DEFAULT_FRAMES_PER_CHUNK * 4 );
703737
704- if (! n )
738+ if (n < 0 )
705739 {
706- // EOF or no writer on FIFO yet - sleep to avoid busy-waiting
707- usleep (10000 ); // 10ms
708- continue ;
740+ // Error on read
741+ LOG_ERROR ("Error reading from audio source" );
742+ break ;
743+ }
744+ else if (n == 0 )
745+ {
746+ // EOF or no writer on FIFO yet
747+ // Check if this is a regular file (true EOF) or FIFO (no writer yet)
748+ struct stat st ;
749+ if (fstat (infile , & st ) == 0 && S_ISFIFO (st .st_mode ))
750+ {
751+ // It's a FIFO - no writer yet, keep waiting
752+ usleep (10000 ); // 10ms
753+ continue ;
754+ }
755+ else
756+ {
757+ // Regular file EOF - mark as done and drain the buffer
758+ LOG_INFO ("End of audio file reached, draining buffer..." );
759+ got_eof = true;
760+ continue ;
761+ }
709762 }
710763
711764 raopcl_send_chunk (raopcl , buf , n / 4 , & playtime );
0 commit comments