Skip to content
Snippets Groups Projects
Commit fcd3978f authored by Alexandre Savard's avatar Alexandre Savard
Browse files

[#1882] Use static audio buffer in Pulse and ALSA layer (instead of malloc)

parent a87b54fd
No related branches found
No related tags found
No related merge requests found
...@@ -41,7 +41,7 @@ AlsaLayer::AlsaLayer (ManagerImpl* manager) ...@@ -41,7 +41,7 @@ AlsaLayer::AlsaLayer (ManagerImpl* manager)
_debug (" Constructor of AlsaLayer called\n"); _debug (" Constructor of AlsaLayer called\n");
/* Instanciate the audio thread */ /* Instanciate the audio thread */
_audioThread = new AudioThread (this); _audioThread = new AudioThread (this);
out_buffer = new SFLDataFormat[5000];
} }
// Destructor // Destructor
...@@ -50,6 +50,8 @@ AlsaLayer::~AlsaLayer (void) ...@@ -50,6 +50,8 @@ AlsaLayer::~AlsaLayer (void)
_debug ("Destructor of AlsaLayer called\n"); _debug ("Destructor of AlsaLayer called\n");
closeLayer(); closeLayer();
delete out_buffer;
out_buffer = NULL;
} }
bool bool
...@@ -721,7 +723,7 @@ void AlsaLayer::audioCallback (void) ...@@ -721,7 +723,7 @@ void AlsaLayer::audioCallback (void)
unsigned short spkrVolume, micVolume; unsigned short spkrVolume, micVolume;
AudioLoop *tone; AudioLoop *tone;
SFLDataFormat *out; // SFLDataFormat *out;
spkrVolume = _manager->getSpkrVolume(); spkrVolume = _manager->getSpkrVolume();
micVolume = _manager->getMicVolume(); micVolume = _manager->getMicVolume();
...@@ -730,47 +732,68 @@ void AlsaLayer::audioCallback (void) ...@@ -730,47 +732,68 @@ void AlsaLayer::audioCallback (void)
// framePerBuffer are the number of data for one channel (left) // framePerBuffer are the number of data for one channel (left)
urgentAvail = _urgentRingBuffer.AvailForGet(); urgentAvail = _urgentRingBuffer.AvailForGet();
for(int k = 0; k < 5000; k++)
out_buffer[k] = 0;
if (urgentAvail > 0) { if (urgentAvail > 0) {
// Urgent data (dtmf, incoming call signal) come first. // Urgent data (dtmf, incoming call signal) come first.
toGet = (urgentAvail < (int) (framesPerBufferAlsa * sizeof (SFLDataFormat))) ? urgentAvail : framesPerBufferAlsa * sizeof (SFLDataFormat); toGet = (urgentAvail < (int) (framesPerBufferAlsa * sizeof (SFLDataFormat))) ? urgentAvail : framesPerBufferAlsa * sizeof (SFLDataFormat);
out = (SFLDataFormat*) malloc (toGet * sizeof (SFLDataFormat)); // out = (SFLDataFormat*) malloc (toGet * sizeof (SFLDataFormat));
_urgentRingBuffer.Get (out, toGet, spkrVolume); _urgentRingBuffer.Get (out_buffer, toGet, spkrVolume);
/* Play the sound */ /* Play the sound */
write (out , toGet); write (out_buffer , toGet);
free (out); // free (out);
out=0; // out=0;
// Consume the regular one as well (same amount of bytes) // Consume the regular one as well (same amount of bytes)
_mainBuffer.discard (toGet); _mainBuffer.discard (toGet);
} else { }
else
{
tone = _manager->getTelephoneTone(); tone = _manager->getTelephoneTone();
toGet = 940 ; toGet = 940 ;
maxBytes = toGet * sizeof (SFLDataFormat) ; maxBytes = toGet * sizeof (SFLDataFormat) ;
if (tone != 0) { if (tone != 0)
out = (SFLDataFormat*) malloc (maxBytes * sizeof (SFLDataFormat)); {
tone->getNext (out, toGet, spkrVolume);
write (out , maxBytes); // out = (SFLDataFormat*) malloc (maxBytes * sizeof (SFLDataFormat));
} else if ( (tone=_manager->getTelephoneFile()) != 0) { tone->getNext (out_buffer, toGet, spkrVolume);
out = (SFLDataFormat*) malloc (maxBytes * sizeof (SFLDataFormat)); write (out_buffer , maxBytes);
tone->getNext (out, toGet, spkrVolume); }
write (out , maxBytes); else if ( (tone=_manager->getTelephoneFile()) != 0)
} else { {
// 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 // If nothing urgent, play the regular sound samples
normalAvail = _mainBuffer.availForGet(); normalAvail = _mainBuffer.availForGet();
toGet = (normalAvail < (int) (framesPerBufferAlsa * sizeof (SFLDataFormat))) ? normalAvail : framesPerBufferAlsa * sizeof (SFLDataFormat); toGet = (normalAvail < (int) (framesPerBufferAlsa * sizeof (SFLDataFormat))) ? normalAvail : framesPerBufferAlsa * sizeof (SFLDataFormat);
out = (SFLDataFormat*) malloc (framesPerBufferAlsa * sizeof (SFLDataFormat)); // out = (SFLDataFormat*) malloc (framesPerBufferAlsa * sizeof (SFLDataFormat));
if (toGet) { if (toGet)
_mainBuffer.getData (out, toGet, spkrVolume); {
write (out, toGet); _mainBuffer.getData (out_buffer, toGet, spkrVolume);
} else { write (out_buffer, toGet);
bzero (out, framesPerBufferAlsa * sizeof (SFLDataFormat));
} }
else
{
bzero (out_buffer, framesPerBufferAlsa * sizeof (SFLDataFormat));
} }
}
// free (out);
free (out);
out=0; // out=0;
} }
// Additionally handle the mic's audio stream // Additionally handle the mic's audio stream
......
...@@ -261,6 +261,8 @@ class AudioLayer { ...@@ -261,6 +261,8 @@ class AudioLayer {
int _errorMessage; int _errorMessage;
ost::Mutex _mutex; ost::Mutex _mutex;
SFLDataFormat* out_buffer;
}; };
#endif // _AUDIO_LAYER_H_ #endif // _AUDIO_LAYER_H_
...@@ -25,7 +25,7 @@ MainBuffer::MainBuffer() ...@@ -25,7 +25,7 @@ MainBuffer::MainBuffer()
createRingBuffer(default_id); createRingBuffer(default_id);
createCallIDSet(default_id); createCallIDSet(default_id);
mixBuffer = new SFLDataFormat[3000]; mixBuffer = new SFLDataFormat[5000];
} }
...@@ -33,6 +33,9 @@ MainBuffer::~MainBuffer() ...@@ -33,6 +33,9 @@ MainBuffer::~MainBuffer()
{ {
removeRingBuffer(default_id); removeRingBuffer(default_id);
removeCallIDSet(default_id); removeCallIDSet(default_id);
delete mixBuffer;
mixBuffer = NULL;
} }
CallIDSet* MainBuffer::getCallIDSet(CallID call_id) CallIDSet* MainBuffer::getCallIDSet(CallID call_id)
...@@ -232,7 +235,7 @@ int MainBuffer::availForPut(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) 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); CallIDSet* callid_set = getCallIDSet(call_id);
...@@ -301,7 +304,7 @@ int MainBuffer::getDataByID(void *buffer, int toCopy, unsigned short volume, Cal ...@@ -301,7 +304,7 @@ int MainBuffer::getDataByID(void *buffer, int toCopy, unsigned short volume, Cal
int MainBuffer::availForGet(CallID call_id) int MainBuffer::availForGet(CallID call_id)
{ {
_debug("MainBuffer::availForGet\n"); // _debug("MainBuffer::availForGet\n");
CallIDSet* callid_set = getCallIDSet(call_id); CallIDSet* callid_set = getCallIDSet(call_id);
......
...@@ -38,6 +38,7 @@ PulseLayer::PulseLayer (ManagerImpl* manager) ...@@ -38,6 +38,7 @@ PulseLayer::PulseLayer (ManagerImpl* manager)
, record() , record()
{ {
_debug ("PulseLayer::Pulse audio constructor: Create context\n"); _debug ("PulseLayer::Pulse audio constructor: Create context\n");
out_buffer = new SFLDataFormat[5000];
} }
...@@ -45,6 +46,8 @@ PulseLayer::PulseLayer (ManagerImpl* manager) ...@@ -45,6 +46,8 @@ PulseLayer::PulseLayer (ManagerImpl* manager)
PulseLayer::~PulseLayer (void) PulseLayer::~PulseLayer (void)
{ {
closeLayer (); closeLayer ();
delete out_buffer;
out_buffer = NULL;
} }
bool bool
...@@ -347,27 +350,33 @@ void PulseLayer::writeToSpeaker (void) ...@@ -347,27 +350,33 @@ void PulseLayer::writeToSpeaker (void)
int toGet; int toGet;
int toPlay; int toPlay;
SFLDataFormat* out;// = (SFLDataFormat*)pa_xmalloc(framesPerBuffer); // SFLDataFormat* out;// = (SFLDataFormat*)pa_xmalloc(framesPerBuffer);
urgentAvail = _urgentRingBuffer.AvailForGet(); urgentAvail = _urgentRingBuffer.AvailForGet();
for(int k = 0; k < 5000; k++)
out_buffer[k] = 0;
if (urgentAvail > 0) { if (urgentAvail > 0) {
// Urgent data (dtmf, incoming call signal) come first. // Urgent data (dtmf, incoming call signal) come first.
//_debug("Play urgent!: %i\e" , urgentAvail); //_debug("Play urgent!: %i\e" , urgentAvail);
toGet = (urgentAvail < (int) (framesPerBuffer * sizeof (SFLDataFormat))) ? urgentAvail : framesPerBuffer * sizeof (SFLDataFormat); toGet = (urgentAvail < (int) (framesPerBuffer * sizeof (SFLDataFormat))) ? urgentAvail : framesPerBuffer * sizeof (SFLDataFormat);
out = (SFLDataFormat*) pa_xmalloc (toGet * sizeof (SFLDataFormat)); // out = (SFLDataFormat*) pa_xmalloc (toGet * sizeof (SFLDataFormat));
_urgentRingBuffer.Get (out, toGet, 100); _urgentRingBuffer.Get (out_buffer, toGet, 100);
pa_stream_write (playback->pulseStream() , out , toGet , pa_xfree, 0 , PA_SEEK_RELATIVE); pa_stream_write (playback->pulseStream() , out_buffer , toGet , NULL, 0 , PA_SEEK_RELATIVE);
// Consume the regular one as well (same amount of bytes) // Consume the regular one as well (same amount of bytes)
_mainBuffer.discard (toGet); _mainBuffer.discard (toGet);
} else { }
else
{
AudioLoop* tone = _manager->getTelephoneTone(); AudioLoop* tone = _manager->getTelephoneTone();
if (tone != 0) { if (tone != 0)
{
toGet = framesPerBuffer; toGet = framesPerBuffer;
out = (SFLDataFormat*) pa_xmalloc (toGet * sizeof (SFLDataFormat)); // out = (SFLDataFormat*) pa_xmalloc (toGet * sizeof (SFLDataFormat));
tone->getNext (out, toGet , 100); tone->getNext (out_buffer, toGet , 100);
pa_stream_write (playback->pulseStream() , out , toGet * sizeof (SFLDataFormat) , pa_xfree, 0 , PA_SEEK_RELATIVE); pa_stream_write (playback->pulseStream() , out_buffer , toGet * sizeof (SFLDataFormat) , NULL, 0 , PA_SEEK_RELATIVE);
} }
if ( (tone=_manager->getTelephoneFile()) != 0) { if ( (tone=_manager->getTelephoneFile()) != 0) {
...@@ -376,25 +385,25 @@ void PulseLayer::writeToSpeaker (void) ...@@ -376,25 +385,25 @@ void PulseLayer::writeToSpeaker (void)
toGet = framesPerBuffer; toGet = framesPerBuffer;
toPlay = ( (int) (toGet * sizeof (SFLDataFormat)) > framesPerBuffer) ? framesPerBuffer : toGet * sizeof (SFLDataFormat) ; toPlay = ( (int) (toGet * sizeof (SFLDataFormat)) > framesPerBuffer) ? framesPerBuffer : toGet * sizeof (SFLDataFormat) ;
out = (SFLDataFormat*) pa_xmalloc (toPlay); // out = (SFLDataFormat*) pa_xmalloc (toPlay);
tone->getNext (out, toPlay/2 , 100); tone->getNext (out_buffer, toPlay/2 , 100);
pa_stream_write (playback->pulseStream() , out , toPlay , pa_xfree, 0 , PA_SEEK_RELATIVE) ; pa_stream_write (playback->pulseStream() , out_buffer , toPlay , NULL, 0 , PA_SEEK_RELATIVE) ;
} else { } else {
out = (SFLDataFormat*) pa_xmalloc (framesPerBuffer * sizeof (SFLDataFormat)); // out = (SFLDataFormat*) pa_xmalloc (framesPerBuffer * sizeof (SFLDataFormat));
normalAvail = _mainBuffer.availForGet(); normalAvail = _mainBuffer.availForGet();
toGet = (normalAvail < (int) (framesPerBuffer * sizeof (SFLDataFormat))) ? normalAvail : framesPerBuffer * sizeof (SFLDataFormat); toGet = (normalAvail < (int) (framesPerBuffer * sizeof (SFLDataFormat))) ? normalAvail : framesPerBuffer * sizeof (SFLDataFormat);
if (toGet) { if (toGet) {
_mainBuffer.getData (out, toGet, 100); _mainBuffer.getData (out_buffer, toGet, 100);
_mainBuffer.discard (toGet); _mainBuffer.discard (toGet);
} else { } 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);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment