Commit 11dec4cb authored by Tristan Matthews's avatar Tristan Matthews

* #6692: more audiolayer cleanup

parent f18b84c6
......@@ -92,11 +92,11 @@ AlsaLayer::AlsaLayer (ManagerImpl* manager)
{
_debug ("Audio: Build ALSA layer");
_urgentRingBuffer.createReadPointer();
urgentRingBuffer_.createReadPointer();
audioPlugin_ = AudioLayer::_manager->audioPreference.getPlugin();
audioPlugin_ = manager_->audioPreference.getPlugin();
AudioLayer::_noisesuppressstate = true;
noiseSuppressState_ = true;
}
// Destructor
......@@ -146,26 +146,26 @@ AlsaLayer::openDevice (int indexIn, int indexOut, int indexRing, int sampleRate,
else if ( (stream == SFL_PCM_PLAYBACK or stream == SFL_PCM_BOTH) and is_playback_open_)
closePlaybackStream ();
_indexIn = indexIn;
_indexOut = indexOut;
_indexRing = indexRing;
indexIn_ = indexIn;
indexOut_ = indexOut;
indexRing_ = indexRing;
_audioSampleRate = sampleRate;
_frameSize = frameSize;
audioSampleRate_ = sampleRate;
frameSize_ = frameSize;
audioPlugin_ = plugin;
_debugAlsa (" Setting AlsaLayer: device in=%2d, out=%2d, ring=%2d", _indexIn, _indexOut, _indexRing);
_debugAlsa (" Setting AlsaLayer: device in=%2d, out=%2d, ring=%2d", indexIn_, indexOut_, indexRing_);
_debugAlsa (" : alsa plugin=%s", audioPlugin_.c_str());
_debugAlsa (" : nb channel in=%2d, out=%2d", _inChannel, _outChannel);
_debugAlsa (" : sample rate=%5d, format=%s", _audioSampleRate, SFLDataFormatString);
_debugAlsa (" : nb channel in=%2d, out=%2d", inChannel_, outChannel_);
_debugAlsa (" : sample rate=%5d, format=%s", audioSampleRate_, SFLDataFormatString);
audioThread_ = NULL;
// use 1 sec buffer for resampling
converter_ = new SamplerateConverter (_audioSampleRate);
_dcblocker = new DcBlocker;
_audiofilter = new AudioProcessing (_dcblocker);
converter_ = new SamplerateConverter (audioSampleRate_);
dcblocker_ = new DcBlocker;
audiofilter_ = new AudioProcessing (dcblocker_);
}
void
......@@ -173,8 +173,8 @@ AlsaLayer::startStream (void)
{
_debug ("Audio: Start stream");
if (_audiofilter)
_audiofilter->resetAlgorithm();
if (audiofilter_)
audiofilter_->resetAlgorithm();
if (is_playback_running_ and is_capture_running_)
return;
......@@ -184,18 +184,18 @@ AlsaLayer::startStream (void)
std::string pcmc;
if (audioPlugin_ == PCM_DMIX_DSNOOP) {
pcmp = buildDeviceTopo (PCM_DMIX, _indexOut, 0);
pcmr = buildDeviceTopo (PCM_DMIX, _indexRing, 0);
pcmc = buildDeviceTopo (PCM_DSNOOP, _indexIn, 0);
pcmp = buildDeviceTopo (PCM_DMIX, indexOut_, 0);
pcmr = buildDeviceTopo (PCM_DMIX, indexRing_, 0);
pcmc = buildDeviceTopo (PCM_DSNOOP, indexIn_, 0);
} else {
pcmp = buildDeviceTopo (audioPlugin_, _indexOut, 0);
pcmr = buildDeviceTopo (audioPlugin_, _indexRing, 0);
pcmc = buildDeviceTopo (audioPlugin_, _indexIn, 0);
pcmp = buildDeviceTopo (audioPlugin_, indexOut_, 0);
pcmr = buildDeviceTopo (audioPlugin_, indexRing_, 0);
pcmc = buildDeviceTopo (audioPlugin_, indexIn_, 0);
}
_debug ("pcmp: %s, index %d", pcmp.c_str(), _indexOut);
_debug ("pcmr: %s, index %d", pcmr.c_str(), _indexRing);
_debug ("pcmc: %s, index %d", pcmc.c_str(), _indexIn);
_debug ("pcmp: %s, index %d", pcmp.c_str(), indexOut_);
_debug ("pcmr: %s, index %d", pcmr.c_str(), indexRing_);
_debug ("pcmc: %s, index %d", pcmc.c_str(), indexIn_);
if (not is_capture_open_)
open_device (pcmp, pcmc, pcmr, SFL_PCM_CAPTURE);
......@@ -222,7 +222,7 @@ AlsaLayer::startStream (void)
}
}
_isStarted = true;
isStarted_ = true;
}
void
......@@ -230,7 +230,7 @@ AlsaLayer::stopStream (void)
{
_debug ("Audio: Stop stream");
_isStarted = false;
isStarted_ = false;
try {
/* Stop the audio thread first */
......@@ -256,12 +256,6 @@ AlsaLayer::stopStream (void)
flushMain ();
}
void AlsaLayer::setNoiseSuppressState (bool state)
{
// if a stream already opened
AudioLayer::_noisesuppressstate = state;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///////////////// ALSA PRIVATE FUNCTIONS ////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
......@@ -430,17 +424,17 @@ bool AlsaLayer::alsa_set_params (snd_pcm_t *pcm_handle, int type)
/* Set sample rate. If we can't set to the desired exact value, we set to the nearest acceptable */
int dir = 0;
unsigned int exact_ivalue = _audioSampleRate;
unsigned int exact_ivalue = audioSampleRate_;
if ((err = snd_pcm_hw_params_set_rate_near (pcm_handle, hwparams, &exact_ivalue, &dir) < 0)) {
_debugAlsa ("Audio: Error: Cannot set sample rate (%s)", snd_strerror (err));
return false;
} else
_debug ("Audio: Set audio rate to %d", _audioSampleRate);
_debug ("Audio: Set audio rate to %d", audioSampleRate_);
if (dir != 0) {
_debugAlsa ("Audio: Error: (%i) The chosen rate %d Hz is not supported by your hardware.Using %d Hz instead. ", type , _audioSampleRate, exact_ivalue);
_audioSampleRate = exact_ivalue;
_debugAlsa ("Audio: Error: (%i) The chosen rate %d Hz is not supported by your hardware.Using %d Hz instead. ", type , audioSampleRate_, exact_ivalue);
audioSampleRate_ = exact_ivalue;
}
/* Set the number of channels */
......@@ -808,19 +802,19 @@ namespace
void AlsaLayer::audioCallback (void)
{
unsigned int _mainBufferSampleRate = getMainBuffer()->getInternalSamplingRate();
bool resample = _audioSampleRate != _mainBufferSampleRate;
unsigned int mainBufferSampleRate = getMainBuffer()->getInternalSamplingRate();
bool resample = audioSampleRate_ != mainBufferSampleRate;
notifyincomingCall();
unsigned short spkrVolume = _manager->getSpkrVolume();
unsigned short spkrVolume = manager_->getSpkrVolume();
AudioLoop *tone = _manager->getTelephoneTone();
AudioLoop *file_tone = _manager->getTelephoneFile();
AudioLoop *tone = manager_->getTelephoneTone();
AudioLoop *file_tone = manager_->getTelephoneFile();
// AvailForGet tell the number of chars inside the buffer
// framePerBuffer are the number of data for one channel (left)
int urgentAvailBytes = _urgentRingBuffer.AvailForGet();
int urgentAvailBytes = urgentRingBuffer_.AvailForGet();
if (!playbackHandle_ or !captureHandle_)
return;
......@@ -836,7 +830,7 @@ void AlsaLayer::audioCallback (void)
if (toGet > playbackAvailBytes)
toGet = playbackAvailBytes;
SFLDataFormat *out = (SFLDataFormat*) malloc (toGet);
_urgentRingBuffer.Get (out, toGet);
urgentRingBuffer_.Get (out, toGet);
adjustVolume(out, toGet / sizeof(SFLDataFormat), spkrVolume);
write (out, toGet, playbackHandle_);
......@@ -867,7 +861,7 @@ void AlsaLayer::audioCallback (void)
// Compute maximal value to get from the ring buffer
double resampleFactor = 1.0;
if (resample) {
resampleFactor = (double) _audioSampleRate / _mainBufferSampleRate;
resampleFactor = (double) audioSampleRate_ / mainBufferSampleRate;
maxNbBytesToGet = (double) toGet / resampleFactor;
}
......@@ -882,7 +876,7 @@ void AlsaLayer::audioCallback (void)
int inSamples = toGet / sizeof(SFLDataFormat);
int outSamples = inSamples * resampleFactor;
SFLDataFormat *rsmpl_out = (SFLDataFormat*) malloc (outSamples * sizeof(SFLDataFormat));
converter_->resample (out, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, inSamples);
converter_->resample (out, rsmpl_out, mainBufferSampleRate, audioSampleRate_, inSamples);
write (rsmpl_out, outSamples * sizeof(SFLDataFormat), playbackHandle_);
free (rsmpl_out);
} else {
......@@ -932,19 +926,19 @@ void AlsaLayer::audioCallback (void)
free(in);
return;
}
adjustVolume (in, toPutSamples, _manager->getSpkrVolume());
adjustVolume (in, toPutSamples, manager_->getSpkrVolume());
if (resample) {
int outSamples = toPutSamples * ((double) _audioSampleRate / _mainBufferSampleRate);
int outSamples = toPutSamples * ((double) audioSampleRate_ / mainBufferSampleRate);
int outBytes = outSamples * sizeof (SFLDataFormat);
SFLDataFormat* rsmpl_out = (SFLDataFormat*) malloc (outBytes);
converter_->resample ( (SFLDataFormat*) in, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, toPutSamples);
_audiofilter->processAudio (rsmpl_out, outBytes);
converter_->resample ( (SFLDataFormat*) in, rsmpl_out, mainBufferSampleRate, audioSampleRate_, toPutSamples);
audiofilter_->processAudio (rsmpl_out, outBytes);
getMainBuffer()->putData (rsmpl_out, outBytes);
free (rsmpl_out);
} else {
SFLDataFormat* filter_out = (SFLDataFormat*) malloc (toPutBytes);
_audiofilter->processAudio (in, filter_out, toPutBytes);
audiofilter_->processAudio (in, filter_out, toPutBytes);
getMainBuffer()->putData (filter_out, toPutBytes);
free (filter_out);
}
......
......@@ -144,19 +144,6 @@ class AlsaLayer : public AudioLayer
void audioCallback (void);
/**
* Get the noise suppressor state
* @return true if noise suppressor activated
*/
virtual bool getNoiseSuppressState (void) const {
return AudioLayer::_noisesuppressstate;
}
/**
* Set the noise suppressor state
* @param state true if noise suppressor active, false elsewhere
*/
virtual void setNoiseSuppressState (bool state);
private:
void closeLayer (void);
......
......@@ -36,66 +36,66 @@
#include <cc++/numbers.h>
AudioLayer::AudioLayer (ManagerImpl* manager , int type)
: _layerType (type)
, _isStarted(false)
, _manager (manager)
, _urgentRingBuffer (SIZEBUF, Call::DEFAULT_ID)
, _mainBuffer(0)
, _recorder(0)
, _indexIn (0)
, _indexOut (0)
, _indexRing(0)
, _audioSampleRate (0)
, _frameSize (0)
, _inChannel (1)
, _outChannel (1)
, _errorMessage (0)
, _mutex ()
, _dcblocker(0)
, _audiofilter(0)
, _noisesuppressstate(false)
, _countNotificationTime(0)
, _time (new ost::Time)
: layerType_ (type)
, isStarted_ (false)
, manager_ (manager)
, urgentRingBuffer_ (SIZEBUF, Call::DEFAULT_ID)
, mainBuffer_ (0)
, recorder_ (0)
, indexIn_ (0)
, indexOut_ (0)
, indexRing_ (0)
, audioSampleRate_ (0)
, frameSize_ (0)
, inChannel_ (1)
, outChannel_ (1)
, errorMessage_ (0)
, mutex_ ()
, dcblocker_ (0)
, audiofilter_ (0)
, noiseSuppressState_ (false)
, countNotificationTime_ (0)
, time_ (new ost::Time)
{}
AudioLayer::~AudioLayer ()
{
delete _time;
delete _audiofilter;
delete _dcblocker;
delete time_;
delete audiofilter_;
delete dcblocker_;
}
void AudioLayer::flushMain (void)
{
ost::MutexLock guard (_mutex);
ost::MutexLock guard (mutex_);
// should pass call id
getMainBuffer()->flushAllBuffers();
}
void AudioLayer::flushUrgent (void)
{
ost::MutexLock guard (_mutex);
_urgentRingBuffer.flushAll();
ost::MutexLock guard (mutex_);
urgentRingBuffer_.flushAll();
}
void AudioLayer::putUrgent (void* buffer, int toCopy)
{
ost::MutexLock guard (_mutex);
_urgentRingBuffer.Put (buffer, toCopy);
ost::MutexLock guard (mutex_);
urgentRingBuffer_.Put (buffer, toCopy);
}
void AudioLayer::notifyincomingCall()
{
// Notify (with a beep) an incoming call when there is already a call
if (Manager::instance().incomingCallWaiting()) {
_countNotificationTime += _time->getSecond();
int countTimeModulo = _countNotificationTime % 5000;
countNotificationTime_ += time_->getSecond();
int countTimeModulo = countNotificationTime_ % 5000;
if ((countTimeModulo - _countNotificationTime) < 0)
if ((countTimeModulo - countNotificationTime_) < 0)
Manager::instance().notificationIncomingCall();
_countNotificationTime = countTimeModulo;
countNotificationTime_ = countTimeModulo;
}
}
......@@ -101,7 +101,7 @@ class AudioLayer
*/
virtual void stopStream (void) = 0;
bool isStarted(void) { return _isStarted; }
bool isStarted(void) const { return isStarted_; }
/**
* Send a chunk of data to the hardware buffer to start the playback
......@@ -121,8 +121,8 @@ class AudioLayer
* Could be: ALSA_PLAYBACK_DEVICE
* ALSA_CAPTURE_DEVICE
*/
void setErrorMessage (const int& error) {
_errorMessage = error;
void setErrorMessage (int error) {
errorMessage_ = error;
}
/**
......@@ -130,7 +130,7 @@ class AudioLayer
* @return int The error code
*/
int getErrorMessage() const {
return _errorMessage;
return errorMessage_;
}
/**
......@@ -139,7 +139,7 @@ class AudioLayer
* 0 for the first available card on the system, 1 ...
*/
int getIndexIn() const {
return _indexIn;
return indexIn_;
}
/**
......@@ -148,7 +148,7 @@ class AudioLayer
* 0 for the first available card on the system, 1 ...
*/
int getIndexOut() const {
return _indexOut;
return indexOut_;
}
/**
......@@ -157,7 +157,7 @@ class AudioLayer
* 0 for the first available card on the system, 1 ...
*/
int getIndexRing() const {
return _indexRing;
return indexRing_;
}
/**
......@@ -166,7 +166,7 @@ class AudioLayer
* default: 44100 HZ
*/
unsigned int getSampleRate() const {
return _audioSampleRate;
return audioSampleRate_;
}
/**
......@@ -175,7 +175,7 @@ class AudioLayer
* default: 20 ms
*/
unsigned int getFrameSize() const {
return _frameSize;
return frameSize_;
}
/**
......@@ -184,7 +184,7 @@ class AudioLayer
*
*/
int getLayerType (void) const {
return _layerType;
return layerType_;
}
/**
......@@ -196,54 +196,56 @@ class AudioLayer
* @return MainBuffer* a pointer to the MainBuffer instance
*/
MainBuffer* getMainBuffer (void) const {
return _mainBuffer;
return mainBuffer_;
}
/**
* Set the mainbuffer once the audiolayer is created
*/
void setMainBuffer (MainBuffer* mainbuffer) {
_mainBuffer = mainbuffer;
mainBuffer_ = mainbuffer;
}
/**
* Set the audio recorder
*/
void setRecorderInstance (Recordable* rec) {
_recorder = rec;
recorder_ = rec;
}
/**
* Get the audio recorder
*/
Recordable* getRecorderInstance (void) const {
return _recorder;
return recorder_;
}
/**
* Get the noise suppressor state
* @return true if noise suppressor activated
*/
virtual bool getNoiseSuppressState (void) const = 0;
/**
* Set the noise suppressor state
* @param state true if noise suppressor active, false elsewhere
*/
virtual void setNoiseSuppressState (bool state) = 0;
void setNoiseSuppressState (bool state) { noiseSuppressState_ = state; }
/**
* Get the noise suppressor state
* @return true if noise suppressor activated
*/
bool getNoiseSuppressState (void) const {
return noiseSuppressState_;
}
/**
* Get the mutex lock for the entire audio layer
*/
ost::Mutex* getMutexLock (void) {
return &_mutex;
return &mutex_;
}
void notifyincomingCall (void);
protected:
int _layerType;
int layerType_;
/**
* Drop the pending frames and close the capture device
......@@ -258,15 +260,15 @@ class AudioLayer
/**
* Wether or not the audio layer stream is started
*/
bool _isStarted;
bool isStarted_;
/** Augment coupling, reduce indirect access */
ManagerImpl* _manager;
ManagerImpl* manager_;
/**
* Urgent ring buffer used for ringtones
*/
RingBuffer _urgentRingBuffer;
RingBuffer urgentRingBuffer_;
/**
* Instance of the MainBuffer for the whole application
......@@ -275,72 +277,72 @@ class AudioLayer
* Audio instances must be registered into the MainBuffer and bound together via the ManagerImpl.
*
*/
MainBuffer* _mainBuffer;
MainBuffer* mainBuffer_;
/**
* A pointer to the recordable instance (may be a call or a conference)
*/
Recordable* _recorder;
Recordable* recorder_;
/**
* Number of audio cards on which capture stream has been opened
*/
int _indexIn;
int indexIn_;
/**
* Number of audio cards on which playback stream has been opened
*/
int _indexOut;
int indexOut_;
/**
* Number of audio cards on which ringtone stream has been opened
*/
int _indexRing;
int indexRing_;
/**
* Sample Rate SFLphone should send sound data to the sound card
* The value can be set in the user config file- now: 44100HZ
*/
unsigned int _audioSampleRate;
unsigned int audioSampleRate_;
/**
* Length of the sound frame we capture or read in ms
* The value can be set in the user config file - now: 20ms
*/
unsigned int _frameSize;
unsigned int frameSize_;
/**
* Input channel (mic) should be 1 mono
*/
unsigned int _inChannel;
unsigned int inChannel_;
/**
* Output channel (stereo) should be 1 mono
*/
unsigned int _outChannel;
unsigned int outChannel_;
/** Contains the current error code */
int _errorMessage;
int errorMessage_;
/**
* Lock for the entire audio layer
*/
ost::Mutex _mutex;
ost::Mutex mutex_;
DcBlocker *_dcblocker;
AudioProcessing *_audiofilter;
DcBlocker *dcblocker_;
AudioProcessing *audiofilter_;
bool _noisesuppressstate;
bool noiseSuppressState_;
/**
* Time counter used to trigger incoming call notification
*/
int _countNotificationTime;
int countNotificationTime_;
/**
* Used to get formated system time in order to compute incoming call notification
*/
ost::Time * _time;
ost::Time * time_;
};
#endif // _AUDIO_LAYER_H_
......@@ -227,9 +227,9 @@ PulseLayer::PulseLayer (ManagerImpl* manager)
, ringtone_(0)
, converter_(0)
{
_urgentRingBuffer.createReadPointer();
urgentRingBuffer_.createReadPointer();
AudioLayer::_noisesuppressstate = true;
noiseSuppressState_ = true;
openLayer();
}
......@@ -244,14 +244,14 @@ PulseLayer::~PulseLayer (void)
void
PulseLayer::openLayer (void)
{
if(_isStarted)
if (isStarted_)
return;
_info ("Audio: Open Pulseaudio layer");
connectPulseAudioServer();
_isStarted = true;
isStarted_ = true;
}
void
......@@ -259,7 +259,7 @@ PulseLayer::closeLayer (void)
{