[alsa-devel] [PATCH 07/10] alsabat: add interrupt handler for shutdown

han.lu at intel.com han.lu at intel.com
Wed Mar 2 09:53:17 CET 2016


From: "Lu, Han" <han.lu at intel.com>

Add ctrl-c capturing for playback thread and capture thread to
shutdown cleanly.

Signed-off-by: Lu, Han <han.lu at intel.com>

diff --git a/bat/alsa.c b/bat/alsa.c
index 905be6e..443ede8 100644
--- a/bat/alsa.c
+++ b/bat/alsa.c
@@ -18,6 +18,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <pthread.h>
+#include <signal.h>
 
 #include <alsa/asoundlib.h>
 
@@ -256,7 +257,7 @@ static int write_to_pcm_loop(struct pcm_container *sndpcm, struct bat *bat)
 		}
 	}
 
-	while (1) {
+	do {
 		err = generate_input_data(bat, sndpcm->buffer, bytes, frames);
 		if (err != 0)
 			break;
@@ -278,7 +279,7 @@ static int write_to_pcm_loop(struct pcm_container *sndpcm, struct bat *bat)
 		err = write_to_pcm(sndpcm, frames, bat);
 		if (err != 0)
 			return err;
-	}
+	} while (is_playing);
 
 	if (bat->debugplay) {
 		err = update_wav_header(bat, fp, bytes_total);
@@ -342,6 +343,9 @@ void *playback_alsa(struct bat *bat)
 		}
 	}
 
+	/* catch ctrl-c to shutdown cleanly */
+	signal(SIGINT, stream_close);
+
 	err = write_to_pcm_loop(&sndpcm, bat);
 	if (err < 0) {
 		retval_play = 1;
@@ -412,7 +416,7 @@ static int read_from_pcm_loop(struct pcm_container *sndpcm, struct bat *bat)
 		return -errno;
 	}
 
-	while (remain > 0) {
+	while (remain > 0 && is_capturing) {
 		size = (remain <= sndpcm->period_bytes) ?
 			remain : sndpcm->period_bytes;
 		frames = size * 8 / sndpcm->frame_bits;
@@ -477,6 +481,9 @@ void *record_alsa(struct bat *bat)
 		goto exit2;
 	}
 
+	/* install signal handler and begin capturing Ctrl-C */
+	signal(SIGINT, sigint_handler);
+
 	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
 	pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
 	pthread_cleanup_push(pcm_cleanup, sndpcm.handle);
diff --git a/bat/alsa.h b/bat/alsa.h
index d5c9972..19945ed 100644
--- a/bat/alsa.h
+++ b/bat/alsa.h
@@ -16,5 +16,8 @@
 extern int retval_play;
 extern int retval_record;
 
+extern int is_capturing;
+extern int is_playing;
+
 void *playback_alsa(struct bat *);
 void *record_alsa(struct bat *);
-- 
2.5.0



More information about the Alsa-devel mailing list