* #6887: audiopreference creates audio layer

This commit is contained in:
Tristan Matthews
2011-09-08 11:26:00 -04:00
parent 5f455af237
commit d5e607c189
6 changed files with 349 additions and 362 deletions

View File

@ -51,11 +51,11 @@ class AlsaThread : public ost::Thread
AlsaThread (const AlsaThread& at);
AlsaThread& operator= (const AlsaThread& at);
AlsaLayer* _alsa;
AlsaLayer* alsa_;
};
AlsaThread::AlsaThread (AlsaLayer *alsa)
: Thread(), _alsa (alsa)
: Thread(), alsa_(alsa)
{
setCancel (cancelDeferred);
}
@ -66,7 +66,7 @@ AlsaThread::AlsaThread (AlsaLayer *alsa)
void AlsaThread::run (void)
{
while (!testCancel()) {
_alsa->audioCallback();
alsa_->audioCallback();
Thread::sleep (20);
}
}
@ -481,12 +481,12 @@ AlsaLayer::getSoundCardsInfo (int stream)
bool
AlsaLayer::soundCardIndexExist (int card, int stream)
AlsaLayer::soundCardIndexExists(int card, int stream)
{
snd_ctl_t* handle;
snd_pcm_info_t *pcminfo;
snd_pcm_info_alloca(&pcminfo);
std::string name = "hw:";
std::string name("hw:");
std::stringstream ss;
ss << card;
name.append(ss.str());

View File

@ -102,7 +102,7 @@ class AlsaLayer : public AudioLayer
* @return bool True if it exists and supports the mode
* false otherwise
*/
static bool soundCardIndexExist (int card , int stream);
static bool soundCardIndexExists(int card, int stream);
/**
* An index is associated with its string description

View File

@ -1993,7 +1993,7 @@ void ManagerImpl::setAudioPlugin (const std::string& audioPlugin)
// Recreate audio driver with new settings
delete _audiodriver;
_audiodriver = preferences.createAudioLayer();
_audiodriver = audioPreference.createAudioLayer();
if (wasStarted)
_audiodriver->startStream();
@ -2032,7 +2032,7 @@ void ManagerImpl::setAudioDevice (const int index, int streamType)
// Recreate audio driver with new settings
delete _audiodriver;
_audiodriver = preferences.createAudioLayer();
_audiodriver = audioPreference.createAudioLayer();
if (wasStarted)
_audiodriver->startStream();
@ -2271,7 +2271,7 @@ void ManagerImpl::setAudioManager (const std::string &api)
return;
}
if (api == preferences.getAudioApi()) {
if (api == audioPreference.getAudioApi()) {
_debug ("Manager: Audio manager chosen already in use. No changes made. ");
audioLayerMutexUnlock();
return;
@ -2286,7 +2286,7 @@ void ManagerImpl::setAudioManager (const std::string &api)
std::string ManagerImpl::getAudioManager (void) const
{
return preferences.getAudioApi();
return audioPreference.getAudioApi();
}
@ -2364,31 +2364,7 @@ void ManagerImpl::setEchoCancelDelay(int delay)
void ManagerImpl::initAudioDriver (void)
{
audioLayerMutexLock();
/* Retrieve the global devices info from the user config */
int numCardIn = audioPreference.getCardin();
int numCardOut = audioPreference.getCardout();
int numCardRing = audioPreference.getCardring();
if (!AlsaLayer::soundCardIndexExist (numCardIn, SFL_PCM_CAPTURE)) {
_debug (" Card with index %d doesn't exist or cannot capture. Switch to 0.", numCardIn);
numCardIn = ALSA_DFT_CARD_ID;
audioPreference.setCardin (ALSA_DFT_CARD_ID);
}
if (!AlsaLayer::soundCardIndexExist (numCardOut, SFL_PCM_PLAYBACK)) {
_debug (" Card with index %d doesn't exist or cannot playback. Switch to 0.", numCardOut);
numCardOut = ALSA_DFT_CARD_ID;
audioPreference.setCardout (ALSA_DFT_CARD_ID);
}
if (!AlsaLayer::soundCardIndexExist (numCardRing, SFL_PCM_RINGTONE)) {
_debug (" Card with index %d doesn't exist or cannot ringtone. Switch to 0.", numCardRing);
numCardRing = ALSA_DFT_CARD_ID;
audioPreference.setCardring (ALSA_DFT_CARD_ID);
}
_audiodriver = preferences.createAudioLayer();
_audiodriver = audioPreference.createAudioLayer();
audioLayerMutexUnlock();
}
@ -2400,7 +2376,7 @@ void ManagerImpl::switchAudioManager (void)
bool wasStarted = _audiodriver->isStarted();
delete _audiodriver;
_audiodriver = preferences.switchAndCreateAudioLayer();
_audiodriver = audioPreference.switchAndCreateAudioLayer();
if (wasStarted)
_audiodriver->startStream();
@ -2433,7 +2409,7 @@ void ManagerImpl::audioSamplingRateChanged (int samplerate)
_mainBuffer.setInternalSamplingRate(samplerate);
delete _audiodriver;
_audiodriver = preferences.createAudioLayer();
_audiodriver = audioPreference.createAudioLayer();
unsigned int sampleRate = _audiodriver->getSampleRate();

View File

@ -46,7 +46,6 @@ namespace {
} // end anonymous namespace
Preferences::Preferences() : _accountOrder("")
, _audioApi (PULSEAUDIO_API_STR)
, _historyLimit(30)
, _historyMaxCalls(20)
, _notifyMails(false)
@ -57,26 +56,6 @@ Preferences::Preferences() : _accountOrder ("")
, _zeroConfenable(false)
, _md5Hash(false)
{
}
AudioLayer* Preferences::createAudioLayer()
{
if (_audioApi == PULSEAUDIO_API_STR and system("ps -C pulseaudio") == 0)
return new PulseLayer;
else {
_audioApi = ALSA_API_STR;
return new AlsaLayer;
}
}
AudioLayer* Preferences::switchAndCreateAudioLayer()
{
if (_audioApi == PULSEAUDIO_API_STR)
_audioApi = ALSA_API_STR;
else
_audioApi = PULSEAUDIO_API_STR;
return createAudioLayer();
}
void Preferences::serialize(Conf::YamlEmitter *emiter)
@ -84,7 +63,6 @@ void Preferences::serialize (Conf::YamlEmitter *emiter)
Conf::MappingNode preferencemap(NULL);
Conf::ScalarNode order(_accountOrder);
Conf::ScalarNode audioapi (_audioApi);
std::stringstream histlimitstr;
histlimitstr << _historyLimit;
Conf::ScalarNode historyLimit(histlimitstr.str());
@ -104,7 +82,6 @@ void Preferences::serialize (Conf::YamlEmitter *emiter)
Conf::ScalarNode md5Hash(_md5Hash);
preferencemap.setKeyValue(orderKey, &order);
preferencemap.setKeyValue (audioApiKey, &audioapi);
preferencemap.setKeyValue(historyLimitKey, &historyLimit);
preferencemap.setKeyValue(historyMaxCallsKey, &historyMaxCalls);
preferencemap.setKeyValue(notifyMailsKey, &notifyMails);
@ -126,8 +103,6 @@ void Preferences::unserialize (Conf::MappingNode *map)
}
map->getValue(orderKey, &_accountOrder);
map->getValue (audioApiKey, &_audioApi);
// 1 is pulseaudio, 0 is alsa
map->getValue(historyLimitKey, &_historyLimit);
map->getValue(historyMaxCallsKey, &_historyMaxCalls);
map->getValue(notifyMailsKey, &_notifyMails);
@ -284,7 +259,9 @@ void HookPreference::unserialize (Conf::MappingNode *map)
AudioPreference::AudioPreference() : _cardin (atoi (ALSA_DFT_CARD)) // ALSA_DFT_CARD
AudioPreference::AudioPreference() :
_audioApi(PULSEAUDIO_API_STR)
, _cardin(atoi(ALSA_DFT_CARD)) // ALSA_DFT_CARD
, _cardout(atoi(ALSA_DFT_CARD)) // ALSA_DFT_CARD
, _cardring(atoi(ALSA_DFT_CARD)) // ALSA_DFT_CARD
, _plugin("default") // PCM_DEFAULT
@ -302,6 +279,38 @@ AudioPreference::AudioPreference() : _cardin (atoi (ALSA_DFT_CARD)) // ALSA_DFT_
, _echoCancelDelay(0)
{}
namespace {
void checkSoundCard(int &card, int stream)
{
if (not AlsaLayer::soundCardIndexExists(card, stream)) {
_warn(" Card with index %d doesn't exist or is unusable.", card);
card = ALSA_DFT_CARD_ID;
}
}
}
AudioLayer* AudioPreference::createAudioLayer()
{
if (_audioApi == PULSEAUDIO_API_STR and system("ps -C pulseaudio") == 0)
return new PulseLayer;
else {
_audioApi = ALSA_API_STR;
checkSoundCard(_cardin, SFL_PCM_CAPTURE);
checkSoundCard(_cardout, SFL_PCM_PLAYBACK);
checkSoundCard(_cardring, SFL_PCM_RINGTONE);
return new AlsaLayer;
}
}
AudioLayer* AudioPreference::switchAndCreateAudioLayer()
{
if (_audioApi == PULSEAUDIO_API_STR)
_audioApi = ALSA_API_STR;
else
_audioApi = PULSEAUDIO_API_STR;
return createAudioLayer();
}
void AudioPreference::serialize(Conf::YamlEmitter *emitter)
{
Conf::MappingNode preferencemap(NULL);
@ -330,6 +339,7 @@ void AudioPreference::serialize (Conf::YamlEmitter *emitter)
Conf::ScalarNode deviceRingtone(_deviceRingtone); //:
// general preference
Conf::ScalarNode audioapi(_audioApi);
Conf::ScalarNode recordpath(_recordpath); //: /home/msavard/Bureau
Conf::ScalarNode alwaysRecording(_alwaysRecording);
std::stringstream micstr;
@ -349,6 +359,7 @@ void AudioPreference::serialize (Conf::YamlEmitter *emitter)
delaystr << _echoCancelDelay;
Conf::ScalarNode echodelay(delaystr.str());
preferencemap.setKeyValue(audioApiKey, &audioapi);
preferencemap.setKeyValue(recordpathKey, &recordpath);
preferencemap.setKeyValue(alwaysRecordingKey, &alwaysRecording);
preferencemap.setKeyValue(volumemicKey, &volumemic);
@ -372,13 +383,13 @@ void AudioPreference::serialize (Conf::YamlEmitter *emitter)
preferencemap.setKeyValue(echoDelayKey, &echodelay);
emitter->serializeAudioPreference(&preferencemap);
}
void AudioPreference::unserialize(Conf::MappingNode *map)
{
assert(map);
map->getValue(audioApiKey, &_audioApi);
map->getValue(recordpathKey, &_recordpath);
map->getValue(alwaysRecordingKey, &_alwaysRecording);
map->getValue(volumemicKey, &_volumemic);

View File

@ -108,9 +108,6 @@ class Preferences : public Serializable
Preferences();
AudioLayer *createAudioLayer();
AudioLayer *switchAndCreateAudioLayer();
virtual void serialize (Conf::YamlEmitter *emitter);
virtual void unserialize (Conf::MappingNode *map);
@ -123,13 +120,6 @@ class Preferences : public Serializable
_accountOrder = ord;
}
std::string getAudioApi (void) const {
return _audioApi;
}
void setAudioApi (const std::string &api) {
_audioApi = api;
}
int getHistoryLimit (void) const {
return _historyLimit;
}
@ -198,7 +188,6 @@ class Preferences : public Serializable
// account order
std::string _accountOrder;
std::string _audioApi;
int _historyLimit;
int _historyMaxCalls;
bool _notifyMails;
@ -402,6 +391,16 @@ class AudioPreference : public Serializable
{
public:
AudioPreference();
AudioLayer *createAudioLayer();
AudioLayer *switchAndCreateAudioLayer();
std::string getAudioApi (void) const {
return _audioApi;
}
void setAudioApi (const std::string &api) {
_audioApi = api;
}
virtual void serialize (Conf::YamlEmitter *emitter);
virtual void unserialize (Conf::MappingNode *map);
@ -530,6 +529,7 @@ class AudioPreference : public Serializable
}
private:
std::string _audioApi;
// alsa preference
int _cardin; // 0

View File

@ -81,9 +81,9 @@ void ConfigurationTest::testInitAudioDriver()
CPPUNIT_FAIL ("Error while loading audio layer");
// Check if it has been created with the right type
if (Manager::instance().preferences.getAudioApi() == "alsa")
if (Manager::instance().audioPreference.getAudioApi() == "alsa")
CPPUNIT_ASSERT (!dynamic_cast<PulseLayer*>(Manager::instance().getAudioDriver()));
else if (Manager::instance().preferences.getAudioApi() == "pulseaudio")
else if (Manager::instance().audioPreference.getAudioApi() == "pulseaudio")
CPPUNIT_ASSERT (!dynamic_cast<AlsaLayer*>(Manager::instance().getAudioDriver()));
else
CPPUNIT_FAIL ("Wrong audio layer type");