$OpenBSD: patch-src_core_c,v 1.6 2019/02/03 03:58:35 bentley Exp $

Reinit sndio if there is a audio(4) error.  Without this, aqualung is
unusable after an audio(4) error until it is closed and reopened.

Index: src/core.c
--- src/core.c.orig
+++ src/core.c
@@ -119,6 +119,10 @@ GSList * saved_pconns_L = NULL;
 GSList * saved_pconns_R = NULL;
 #endif /* HAVE_JACK */
 
+#ifdef HAVE_SNDIO
+int sndio_reinit(thread_info_t * info, int verbose);
+#endif /* HAVE_SNDIO */
+
 const size_t sample_size = sizeof(float);
 
 gint playlist_state, browser_state;
@@ -802,8 +806,15 @@ sndio_thread(void * arg) {
 
 		/* write data to audio device */
 		bytes_written = sio_write(sndio_hdl, sndio_short_buf, 2*n_avail * sizeof(short));
-		if (bytes_written != 2*n_avail * sizeof(short))
+		if (bytes_written != 2*n_avail * sizeof(short)) {
 			fprintf(stderr, "sndio_thread: Error writing to audio device\n");
+			if (sndio_reinit(info, 1) == 0) {
+				fprintf(stderr, "sndio_thread: audio device reinit successful\n");
+				sio_close(sndio_hdl);
+				sndio_hdl = info->sndio_hdl;
+				goto sndio_wake;
+			}
+		}
 	}
  sndio_finish:
 	return 0;
@@ -1235,7 +1246,7 @@ jack_info_shutdown(jack_status_t code, const char * re
  * -N : unable to start with given params
  */
 int
-sndio_init(thread_info_t * info, int verbose, gboolean realtime, int priority) {
+sndio_reinit(thread_info_t * info, int verbose) {
 
 	struct sio_hdl * sndio_hdl;
 	struct sio_par sndio_par;
@@ -1300,6 +1311,15 @@ sndio_init(thread_info_t * info, int verbose, gboolean
 	}
 
 	info->sndio_hdl = sndio_hdl;
+	return 0;
+}
+
+int
+sndio_init(thread_info_t * info, int verbose, gboolean realtime, int priority) {
+	int ret;
+	if ((ret = sndio_reinit(info, verbose)) != 0) {
+		return ret;
+	}
 	AQUALUNG_THREAD_CREATE(info->sndio_thread_id, NULL, sndio_thread, info)
 	set_thread_priority(info->sndio_thread_id, "sndio output", realtime, priority);
 
