From efa7b7cbc09bd661078065e9fb8d07114aba63f0 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 23 Jul 2015 21:07:40 -0700 Subject: [PATCH] ALSA input stream: add missing clean up --- example/microphone.c | 12 +++++++++--- src/alsa.cpp | 12 +++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/example/microphone.c b/example/microphone.c index 580fecd..a287f2c 100644 --- a/example/microphone.c +++ b/example/microphone.c @@ -142,6 +142,11 @@ static void write_callback(struct SoundIoOutStream *outstream, int requested_fra soundio_ring_buffer_advance_read_ptr(ring_buffer, total_read_count * outstream->bytes_per_frame); } +static void underflow_callback(struct SoundIoOutStream *outstream) { + static int count = 0; + fprintf(stderr, "underflow %d\n", count++); +} + static int usage(char *exe) { fprintf(stderr, "Usage: %s [--dummy] [--alsa] [--pulseaudio] [--in-device name] [--out-device name]\n", exe); return 1; @@ -280,14 +285,15 @@ int main(int argc, char **argv) { outstream->format = *fmt; outstream->sample_rate = sample_rate; outstream->layout = *layout; - outstream->buffer_duration = 0.2; - outstream->period_duration = 0.1; + outstream->buffer_duration = 0.1; + outstream->period_duration = out_device->period_duration_min; outstream->write_callback = write_callback; + outstream->underflow_callback = underflow_callback; if ((err = soundio_outstream_open(outstream))) panic("unable to open output stream: %s", soundio_strerror(err)); - int capacity = 0.4 * instream->sample_rate * instream->bytes_per_frame; + int capacity = outstream->buffer_duration * 2 * instream->sample_rate * instream->bytes_per_frame; ring_buffer = soundio_ring_buffer_create(soundio, capacity); if (!ring_buffer) panic("unable to create ring buffer: out of memory"); diff --git a/src/alsa.cpp b/src/alsa.cpp index 0689722..df19b0e 100644 --- a/src/alsa.cpp +++ b/src/alsa.cpp @@ -1414,8 +1414,18 @@ static void instream_destroy_alsa(SoundIoPrivate *si, SoundIoInStreamPrivate *is if (!isa) return; + if (isa->thread) { + isa->thread_exit_flag.clear(); + // TODO wake up poll + soundio_os_thread_destroy(isa->thread); + } + + if (isa->handle) + snd_pcm_close(isa->handle); + + deallocate(isa->poll_fds, isa->poll_fd_count); deallocate(isa->chmap, isa->chmap_size); - // TODO + deallocate(isa->sample_buffer, isa->sample_buffer_size); destroy(isa); is->backend_data = nullptr;