diff --git a/main.py b/main.py index d3bd04e..678edf9 100644 --- a/main.py +++ b/main.py @@ -75,19 +75,24 @@ def playback_thread(file_path, state): graph = build_graph(frame) except Exception as e: print(f"Filter graph error: {e}", file=sys.stderr) - break + graph = "FAILED" - try: - graph.push(frame) - except av.AVError: - continue - - while True: + if graph == "FAILED": + frames_to_process = [frame] + else: try: - filtered_frame = graph.pull() - except (av.AVError, BlockingIOError): - break + graph.push(frame) + except av.AVError: + continue + frames_to_process = [] + while True: + try: + frames_to_process.append(graph.pull()) + except (av.AVError, BlockingIOError): + break + + for filtered_frame in frames_to_process: resampled_frames = resampler.resample(filtered_frame) for r_frame in resampled_frames: arr = r_frame.to_ndarray() @@ -98,7 +103,11 @@ def playback_thread(file_path, state): arr = arr.astype(np.float32) * multiplier arr = np.clip(arr, -32768, 32767).astype(np.int16) - fifo.write(arr.tobytes()) + try: + fifo.write(arr.tobytes()) + except BrokenPipeError: + state.running = False + return def handle_ipc_client(conn, state): buffer = "" @@ -199,6 +208,9 @@ def main(): try: playback_thread(args.file, state) + # Keep the main thread alive until Navidrome explicitly sends 'quit' + while state.running: + time.sleep(0.1) except KeyboardInterrupt: pass finally: