From dd1f02ec375673d72dc2394cfbe420a997fd8ada Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 1 Oct 2014 19:12:13 +0200 Subject: Potential pulse audio fix. --- src/audiobackend-pulse.cc | 48 +++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/audiobackend-pulse.cc b/src/audiobackend-pulse.cc index 5e3136e..9f84ebe 100644 --- a/src/audiobackend-pulse.cc +++ b/src/audiobackend-pulse.cc @@ -41,36 +41,38 @@ Output example: http://freedesktop.org/software/pulseaudio/doxygen/parec-simple_8c-example.html */ +static const pa_sample_spec ss = { + .format = PA_SAMPLE_S16LE, + .rate = SAMPLERATE, + .channels = 1 +}; + AudioBackendPulse::AudioBackendPulse(const char *device) { - // Initialize PulseAudio - static const pa_sample_spec ss = { - .format = PA_SAMPLE_S16LE, - .rate = SAMPLERATE, - .channels = 1 - }; - - int error; - - if (!(sIn = pa_simple_new(NULL, "Anything", PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) { - fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); - } - if (!(sOut = pa_simple_new(NULL, "Anything", PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) { - fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); - } + sIn = NULL; + sOut = NULL; } AudioBackendPulse::~AudioBackendPulse() { // Freeeeedoooooom! Set the PulseAudio instances free again, we're done - pa_simple_free(sIn); - pa_simple_free(sOut); + if(sIn) pa_simple_free(sIn); + if(sOut) pa_simple_free(sOut); } int AudioBackendPulse::read(char *pcm, size_t maxsize) { - int error; + if(!sIn) { + sIn = pa_simple_new(NULL, "Anything", PA_STREAM_RECORD, NULL, "record", + &ss, NULL, NULL, &error); + if(!sIn) { + fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", + pa_strerror(error)); + return 0; + } + } + int error; if (pa_simple_read(sIn, pcm, maxsize, &error) < 0) { fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); @@ -80,6 +82,16 @@ int AudioBackendPulse::read(char *pcm, size_t maxsize) int AudioBackendPulse::write(const char *pcm, size_t size) { + if(!sOut) { + sOut = pa_simple_new(NULL, "Anything", PA_STREAM_PLAYBACK, NULL, + "playback", &ss, NULL, NULL, &error); + if(!sOut) { + fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", + pa_strerror(error)); + return 0; + } + } + int error; if (pa_simple_write(sOut, pcm, size, &error) < 0) { -- cgit v1.2.3