summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2014-10-01 19:12:13 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2014-10-01 19:12:13 +0200
commitdd1f02ec375673d72dc2394cfbe420a997fd8ada (patch)
tree88e3cf756e612f6dd4ef0a8df556e27c87263f7b
parent6d1bc935a6982f045298dc074f0867c2431c3d24 (diff)
Potential pulse audio fix.
-rw-r--r--src/audiobackend-pulse.cc48
1 files 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) {