From fcd3978f020be3348dcfc62fca76b0eb85e5b56f Mon Sep 17 00:00:00 2001 From: Alexandre Savard <alexandre.savard@savoirfairelinux.net> Date: Wed, 5 Aug 2009 14:01:01 -0400 Subject: [PATCH] [#1882] Use static audio buffer in Pulse and ALSA layer (instead of malloc) --- sflphone-common/src/audio/alsalayer.cpp | 75 ++++++++++++++++-------- sflphone-common/src/audio/audiolayer.h | 2 + sflphone-common/src/audio/mainbuffer.cpp | 9 ++- sflphone-common/src/audio/pulselayer.cpp | 43 ++++++++------ 4 files changed, 83 insertions(+), 46 deletions(-) diff --git a/sflphone-common/src/audio/alsalayer.cpp b/sflphone-common/src/audio/alsalayer.cpp index c1fc2dd2c6..3f5812c2cf 100644 --- a/sflphone-common/src/audio/alsalayer.cpp +++ b/sflphone-common/src/audio/alsalayer.cpp @@ -41,7 +41,7 @@ AlsaLayer::AlsaLayer (ManagerImpl* manager) _debug (" Constructor of AlsaLayer called\n"); /* Instanciate the audio thread */ _audioThread = new AudioThread (this); - + out_buffer = new SFLDataFormat[5000]; } // Destructor @@ -50,6 +50,8 @@ AlsaLayer::~AlsaLayer (void) _debug ("Destructor of AlsaLayer called\n"); closeLayer(); + delete out_buffer; + out_buffer = NULL; } bool @@ -721,7 +723,7 @@ void AlsaLayer::audioCallback (void) unsigned short spkrVolume, micVolume; AudioLoop *tone; - SFLDataFormat *out; + // SFLDataFormat *out; spkrVolume = _manager->getSpkrVolume(); micVolume = _manager->getMicVolume(); @@ -730,47 +732,68 @@ void AlsaLayer::audioCallback (void) // framePerBuffer are the number of data for one channel (left) urgentAvail = _urgentRingBuffer.AvailForGet(); + + + for(int k = 0; k < 5000; k++) + out_buffer[k] = 0; + if (urgentAvail > 0) { + // Urgent data (dtmf, incoming call signal) come first. toGet = (urgentAvail < (int) (framesPerBufferAlsa * sizeof (SFLDataFormat))) ? urgentAvail : framesPerBufferAlsa * sizeof (SFLDataFormat); - out = (SFLDataFormat*) malloc (toGet * sizeof (SFLDataFormat)); - _urgentRingBuffer.Get (out, toGet, spkrVolume); + // out = (SFLDataFormat*) malloc (toGet * sizeof (SFLDataFormat)); + _urgentRingBuffer.Get (out_buffer, toGet, spkrVolume); /* Play the sound */ - write (out , toGet); - free (out); - out=0; + write (out_buffer , toGet); + // free (out); + // out=0; // Consume the regular one as well (same amount of bytes) _mainBuffer.discard (toGet); - } else { + } + else + { + tone = _manager->getTelephoneTone(); toGet = 940 ; maxBytes = toGet * sizeof (SFLDataFormat) ; - if (tone != 0) { - out = (SFLDataFormat*) malloc (maxBytes * sizeof (SFLDataFormat)); - tone->getNext (out, toGet, spkrVolume); - write (out , maxBytes); - } else if ( (tone=_manager->getTelephoneFile()) != 0) { - out = (SFLDataFormat*) malloc (maxBytes * sizeof (SFLDataFormat)); - tone->getNext (out, toGet, spkrVolume); - write (out , maxBytes); - } else { + if (tone != 0) + { + + // out = (SFLDataFormat*) malloc (maxBytes * sizeof (SFLDataFormat)); + tone->getNext (out_buffer, toGet, spkrVolume); + write (out_buffer , maxBytes); + } + else if ( (tone=_manager->getTelephoneFile()) != 0) + { + + // out = (SFLDataFormat*) malloc (maxBytes * sizeof (SFLDataFormat)); + tone->getNext (out_buffer, toGet, spkrVolume); + write (out_buffer , maxBytes); + } + else + { + // If nothing urgent, play the regular sound samples normalAvail = _mainBuffer.availForGet(); toGet = (normalAvail < (int) (framesPerBufferAlsa * sizeof (SFLDataFormat))) ? normalAvail : framesPerBufferAlsa * sizeof (SFLDataFormat); - out = (SFLDataFormat*) malloc (framesPerBufferAlsa * sizeof (SFLDataFormat)); - - if (toGet) { - _mainBuffer.getData (out, toGet, spkrVolume); - write (out, toGet); - } else { - bzero (out, framesPerBufferAlsa * sizeof (SFLDataFormat)); + // out = (SFLDataFormat*) malloc (framesPerBufferAlsa * sizeof (SFLDataFormat)); + + if (toGet) + { + _mainBuffer.getData (out_buffer, toGet, spkrVolume); + write (out_buffer, toGet); + } + else + { + bzero (out_buffer, framesPerBufferAlsa * sizeof (SFLDataFormat)); } } - free (out); + // free (out); + - out=0; + // out=0; } // Additionally handle the mic's audio stream diff --git a/sflphone-common/src/audio/audiolayer.h b/sflphone-common/src/audio/audiolayer.h index 29505f76d3..1ec4b7c7c9 100644 --- a/sflphone-common/src/audio/audiolayer.h +++ b/sflphone-common/src/audio/audiolayer.h @@ -261,6 +261,8 @@ class AudioLayer { int _errorMessage; ost::Mutex _mutex; + + SFLDataFormat* out_buffer; }; #endif // _AUDIO_LAYER_H_ diff --git a/sflphone-common/src/audio/mainbuffer.cpp b/sflphone-common/src/audio/mainbuffer.cpp index bfb2e1a1e3..9af6b715d0 100644 --- a/sflphone-common/src/audio/mainbuffer.cpp +++ b/sflphone-common/src/audio/mainbuffer.cpp @@ -25,7 +25,7 @@ MainBuffer::MainBuffer() createRingBuffer(default_id); createCallIDSet(default_id); - mixBuffer = new SFLDataFormat[3000]; + mixBuffer = new SFLDataFormat[5000]; } @@ -33,6 +33,9 @@ MainBuffer::~MainBuffer() { removeRingBuffer(default_id); removeCallIDSet(default_id); + + delete mixBuffer; + mixBuffer = NULL; } CallIDSet* MainBuffer::getCallIDSet(CallID call_id) @@ -232,7 +235,7 @@ int MainBuffer::availForPut(CallID call_id) int MainBuffer::getData(void *buffer, int toCopy, unsigned short volume, CallID call_id) { - // _debug("MainBuffer::getData\n"); + // _debug("MainBuffer::getData \"%s\", toCopy %i\n",call_id.c_str(), toCopy); CallIDSet* callid_set = getCallIDSet(call_id); @@ -301,7 +304,7 @@ int MainBuffer::getDataByID(void *buffer, int toCopy, unsigned short volume, Cal int MainBuffer::availForGet(CallID call_id) { - _debug("MainBuffer::availForGet\n"); + // _debug("MainBuffer::availForGet\n"); CallIDSet* callid_set = getCallIDSet(call_id); diff --git a/sflphone-common/src/audio/pulselayer.cpp b/sflphone-common/src/audio/pulselayer.cpp index a02c1b29b7..81ac645eb5 100644 --- a/sflphone-common/src/audio/pulselayer.cpp +++ b/sflphone-common/src/audio/pulselayer.cpp @@ -38,6 +38,7 @@ PulseLayer::PulseLayer (ManagerImpl* manager) , record() { _debug ("PulseLayer::Pulse audio constructor: Create context\n"); + out_buffer = new SFLDataFormat[5000]; } @@ -45,6 +46,8 @@ PulseLayer::PulseLayer (ManagerImpl* manager) PulseLayer::~PulseLayer (void) { closeLayer (); + delete out_buffer; + out_buffer = NULL; } bool @@ -347,27 +350,33 @@ void PulseLayer::writeToSpeaker (void) int toGet; int toPlay; - SFLDataFormat* out;// = (SFLDataFormat*)pa_xmalloc(framesPerBuffer); + // SFLDataFormat* out;// = (SFLDataFormat*)pa_xmalloc(framesPerBuffer); urgentAvail = _urgentRingBuffer.AvailForGet(); + for(int k = 0; k < 5000; k++) + out_buffer[k] = 0; + if (urgentAvail > 0) { // Urgent data (dtmf, incoming call signal) come first. //_debug("Play urgent!: %i\e" , urgentAvail); toGet = (urgentAvail < (int) (framesPerBuffer * sizeof (SFLDataFormat))) ? urgentAvail : framesPerBuffer * sizeof (SFLDataFormat); - out = (SFLDataFormat*) pa_xmalloc (toGet * sizeof (SFLDataFormat)); - _urgentRingBuffer.Get (out, toGet, 100); - pa_stream_write (playback->pulseStream() , out , toGet , pa_xfree, 0 , PA_SEEK_RELATIVE); + // out = (SFLDataFormat*) pa_xmalloc (toGet * sizeof (SFLDataFormat)); + _urgentRingBuffer.Get (out_buffer, toGet, 100); + pa_stream_write (playback->pulseStream() , out_buffer , toGet , NULL, 0 , PA_SEEK_RELATIVE); // Consume the regular one as well (same amount of bytes) _mainBuffer.discard (toGet); - } else { + } + else + { AudioLoop* tone = _manager->getTelephoneTone(); - if (tone != 0) { + if (tone != 0) + { toGet = framesPerBuffer; - out = (SFLDataFormat*) pa_xmalloc (toGet * sizeof (SFLDataFormat)); - tone->getNext (out, toGet , 100); - pa_stream_write (playback->pulseStream() , out , toGet * sizeof (SFLDataFormat) , pa_xfree, 0 , PA_SEEK_RELATIVE); + // out = (SFLDataFormat*) pa_xmalloc (toGet * sizeof (SFLDataFormat)); + tone->getNext (out_buffer, toGet , 100); + pa_stream_write (playback->pulseStream() , out_buffer , toGet * sizeof (SFLDataFormat) , NULL, 0 , PA_SEEK_RELATIVE); } if ( (tone=_manager->getTelephoneFile()) != 0) { @@ -376,25 +385,25 @@ void PulseLayer::writeToSpeaker (void) toGet = framesPerBuffer; toPlay = ( (int) (toGet * sizeof (SFLDataFormat)) > framesPerBuffer) ? framesPerBuffer : toGet * sizeof (SFLDataFormat) ; - out = (SFLDataFormat*) pa_xmalloc (toPlay); - tone->getNext (out, toPlay/2 , 100); - pa_stream_write (playback->pulseStream() , out , toPlay , pa_xfree, 0 , PA_SEEK_RELATIVE) ; + // out = (SFLDataFormat*) pa_xmalloc (toPlay); + tone->getNext (out_buffer, toPlay/2 , 100); + pa_stream_write (playback->pulseStream() , out_buffer , toPlay , NULL, 0 , PA_SEEK_RELATIVE) ; } else { - out = (SFLDataFormat*) pa_xmalloc (framesPerBuffer * sizeof (SFLDataFormat)); + // out = (SFLDataFormat*) pa_xmalloc (framesPerBuffer * sizeof (SFLDataFormat)); normalAvail = _mainBuffer.availForGet(); toGet = (normalAvail < (int) (framesPerBuffer * sizeof (SFLDataFormat))) ? normalAvail : framesPerBuffer * sizeof (SFLDataFormat); if (toGet) { - _mainBuffer.getData (out, toGet, 100); + _mainBuffer.getData (out_buffer, toGet, 100); _mainBuffer.discard (toGet); } else { - bzero (out, framesPerBuffer * sizeof (SFLDataFormat)); + bzero (out_buffer, framesPerBuffer * sizeof (SFLDataFormat)); } - pa_stream_write (playback->pulseStream() , out , toGet , NULL, 0 , PA_SEEK_RELATIVE); + pa_stream_write (playback->pulseStream() , out_buffer , toGet , NULL, 0 , PA_SEEK_RELATIVE); - pa_xfree (out); + // pa_xfree (out); } } -- GitLab