1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Get rid of static methods and bugs

This commit is contained in:
Nils Fenner 2015-10-20 03:46:44 +02:00
parent 54547af7cf
commit b7cf4df13f
No known key found for this signature in database
GPG Key ID: 9591A163FF9BE04C
7 changed files with 66 additions and 156 deletions

View File

@ -92,29 +92,19 @@ void Audio::startAudioThread()
moveToThread(audioThread); moveToThread(audioThread);
} }
float Audio::getOutputVolume()
{
return getInstance().GetOutputVolume();
}
/** /**
Returns the current output volume, between 0 and 1 Returns the current output volume, between 0 and 1
*/ */
qreal Audio::GetOutputVolume() qreal Audio::getOutputVolume()
{ {
QMutexLocker locker(&audioOutLock); QMutexLocker locker(&audioOutLock);
return outputVolume; return outputVolume;
} }
void Audio::setOutputVolume(float volume)
{
getInstance().SetOutputVolume(volume);
}
/** /**
The volume must be between 0 and 1 The volume must be between 0 and 1
*/ */
void Audio::SetOutputVolume(qreal volume) void Audio::setOutputVolume(qreal volume)
{ {
QMutexLocker locker(&audioOutLock); QMutexLocker locker(&audioOutLock);
outputVolume = volume; outputVolume = volume;
@ -139,33 +129,24 @@ void Audio::SetOutputVolume(qreal volume)
/** /**
The volume must be between 0 and 2 The volume must be between 0 and 2
*/ */
void Audio::setInputVolume(float volume) void Audio::setInputVolume(qreal volume)
{
getInstance().SetInputVolume(volume);
}
void Audio::SetInputVolume(qreal volume)
{ {
QMutexLocker locker(&audioInLock); QMutexLocker locker(&audioInLock);
inputVolume = volume; inputVolume = volume;
} }
void Audio::suscribeInput()
{
getInstance().SubscribeInput();
}
/** /**
@brief Subscribe to capture sound from the opened input device. @brief Subscribe to capture sound from the opened input device.
If the input device is not open, it will be opened before capturing. If the input device is not open, it will be opened before capturing.
*/ */
void Audio::SubscribeInput() void Audio::subscribeInput()
{ {
qDebug() << "subscribing input" << inputSubscriptions; qDebug() << "subscribing input" << inputSubscriptions;
if (!inputSubscriptions++) if (!inputSubscriptions++)
{ {
OpenInput(Settings::getInstance().getInDev()); openInput(Settings::getInstance().getInDev());
openOutput(Settings::getInstance().getOutDev());
#if (!FIX_SND_PCM_PREPARE_BUG) #if (!FIX_SND_PCM_PREPARE_BUG)
if (alInDev) if (alInDev)
@ -175,16 +156,6 @@ void Audio::SubscribeInput()
} }
#endif #endif
} }
QTimer::singleShot(1, [=]()
{
OpenOutput(Settings::getInstance().getOutDev());
});
}
void Audio::unsuscribeInput()
{
getInstance().UnsubscribeInput();
} }
/** /**
@ -192,7 +163,7 @@ void Audio::unsuscribeInput()
If the input device has no more subscriptions, it will be closed. If the input device has no more subscriptions, it will be closed.
*/ */
void Audio::UnsubscribeInput() void Audio::unsubscribeInput()
{ {
qDebug() << "unsubscribing input" << inputSubscriptions; qDebug() << "unsubscribing input" << inputSubscriptions;
if (inputSubscriptions > 0) if (inputSubscriptions > 0)
@ -200,29 +171,16 @@ void Audio::UnsubscribeInput()
else if(inputSubscriptions < 0) else if(inputSubscriptions < 0)
inputSubscriptions = 0; inputSubscriptions = 0;
if (!inputSubscriptions) if (!inputSubscriptions) {
{
closeOutput(); closeOutput();
#if (!FIX_SND_PCM_PREPARE_BUG)
if (alInDev)
{
qDebug() << "stopping capture";
alcCaptureStop(alInDev);
}
#endif
closeInput(); closeInput();
} }
} }
void Audio::openInput(const QString& inDevDescr)
{
getInstance().OpenInput(inDevDescr);
}
/** /**
Open an input device, use before suscribing Open an input device, use before suscribing
*/ */
void Audio::OpenInput(const QString& inDevDescr) void Audio::openInput(const QString& inDevDescr)
{ {
QMutexLocker lock(&audioInLock); QMutexLocker lock(&audioInLock);
auto* tmp = alInDev; auto* tmp = alInDev;
@ -261,15 +219,10 @@ void Audio::OpenInput(const QString& inDevDescr)
} }
} }
bool Audio::openOutput(const QString& outDevDescr)
{
return getInstance().OpenOutput(outDevDescr);
}
/** /**
Open an output device Open an output device
*/ */
bool Audio::OpenOutput(const QString &outDevDescr) bool Audio::openOutput(const QString &outDevDescr)
{ {
QMutexLocker lock(&audioOutLock); QMutexLocker lock(&audioOutLock);
auto* tmp = alOutDev; auto* tmp = alOutDev;
@ -314,20 +267,20 @@ bool Audio::OpenOutput(const QString &outDevDescr)
return true; return true;
} }
void Audio::closeInput()
{
getInstance().CloseInput();
}
/** /**
Close an input device, please don't use unless everyone's unsuscribed Close an input device, please don't use unless everyone's unsuscribed
*/ */
void Audio::CloseInput() void Audio::closeInput()
{ {
qDebug() << "Closing input"; qDebug() << "Closing input";
QMutexLocker locker(&audioInLock); QMutexLocker locker(&audioInLock);
if (alInDev) if (alInDev)
{ {
#if (!FIX_SND_PCM_PREPARE_BUG)
qDebug() << "stopping capture";
alcCaptureStop(alInDev);
#endif
if (alcCaptureCloseDevice(alInDev) == ALC_TRUE) if (alcCaptureCloseDevice(alInDev) == ALC_TRUE)
{ {
alInDev = nullptr; alInDev = nullptr;
@ -340,15 +293,10 @@ void Audio::CloseInput()
} }
} }
void Audio::closeOutput()
{
getInstance().CloseOutput();
}
/** /**
Close an output device Close an output device
*/ */
void Audio::CloseOutput() void Audio::closeOutput()
{ {
qDebug() << "Closing output"; qDebug() << "Closing output";
QMutexLocker locker(&audioOutLock); QMutexLocker locker(&audioOutLock);
@ -371,15 +319,10 @@ void Audio::CloseOutput()
} }
} }
void Audio::playMono16Sound(const QByteArray& data)
{
getInstance().PlayMono16Sound(data);
}
/** /**
Play a 44100Hz mono 16bit PCM sound Play a 44100Hz mono 16bit PCM sound
*/ */
void Audio::PlayMono16Sound(const QByteArray& data) void Audio::playMono16Sound(const QByteArray& data)
{ {
QMutexLocker lock(&audioOutLock); QMutexLocker lock(&audioOutLock);
@ -430,20 +373,15 @@ void Audio::playGroupAudioQueued(Tox*,int group, int peer, const int16_t* data,
emit static_cast<Core*>(core)->groupPeerAudioPlaying(group, peer); emit static_cast<Core*>(core)->groupPeerAudioPlaying(group, peer);
} }
void Audio::playGroupAudio(int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate)
{
getInstance().PlayGroupAudio(group, peer, data, samples, channels, sample_rate);
}
/** /**
Must be called from the audio thread, plays a group call's received audio Must be called from the audio thread, plays a group call's received audio
*/ */
void Audio::PlayGroupAudio(int group, int peer, const int16_t* data, void Audio::playGroupAudio(int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate) unsigned samples, uint8_t channels, unsigned sample_rate)
{ {
assert(QThread::currentThread() == audioThread); assert(QThread::currentThread() == audioThread);
QMutexLocker lock(&audioOutLock); QMutexLocker lock(&audioOutLock);
ToxGroupCall& call = Core::groupCalls[group]; ToxGroupCall& call = Core::groupCalls[group];
@ -507,43 +445,28 @@ void Audio::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, u
alSourcePlay(alSource); alSourcePlay(alSource);
} }
bool Audio::isInputReady()
{
return getInstance().IsInputReady();
}
/** /**
Returns true if the input device is open and suscribed to Returns true if the input device is open and suscribed to
*/ */
bool Audio::IsInputReady() bool Audio::isInputReady()
{ {
QMutexLocker locker(&audioInLock); QMutexLocker locker(&audioInLock);
return alInDev && inputSubscriptions; return alInDev && inputSubscriptions;
} }
bool Audio::isOutputClosed()
{
return getInstance().IsOutputClosed();
}
/** /**
Returns true if the output device is open Returns true if the output device is open
*/ */
bool Audio::IsOutputClosed() bool Audio::isOutputClosed()
{ {
QMutexLocker locker(&audioOutLock); QMutexLocker locker(&audioOutLock);
return alOutDev; return alOutDev;
} }
bool Audio::tryCaptureSamples(uint8_t* buf, int framesize)
{
return getInstance().TryCaptureSamples(buf, framesize);
}
/** /**
Does nothing and return false on failure Does nothing and return false on failure
*/ */
bool Audio::TryCaptureSamples(uint8_t* buf, int framesize) bool Audio::tryCaptureSamples(uint8_t* buf, int framesize)
{ {
QMutexLocker lock(&audioInLock); QMutexLocker lock(&audioInLock);

View File

@ -52,42 +52,24 @@ public:
public: public:
void startAudioThread(); void startAudioThread();
static float getOutputVolume(); qreal getOutputVolume();
qreal GetOutputVolume(); void setOutputVolume(qreal volume);
static void setOutputVolume(float volume);
void SetOutputVolume(qreal volume);
static void setInputVolume(float volume); void setInputVolume(qreal volume);
void SetInputVolume(qreal volume);
static void suscribeInput(); void subscribeInput();
void SubscribeInput(); void unsubscribeInput();
static void unsuscribeInput(); void openInput(const QString& inDevDescr);
void UnsubscribeInput(); bool openOutput(const QString& outDevDescr);
static void openInput(const QString& inDevDescr); bool isInputReady();
void OpenInput(const QString& inDevDescr); bool isOutputClosed();
static bool openOutput(const QString& outDevDescr);
bool OpenOutput(const QString& outDevDescr);
static void closeInput();
void CloseInput();
static void closeOutput();
void CloseOutput();
static bool isInputReady(); void playMono16Sound(const QByteArray& data);
bool IsInputReady(); bool tryCaptureSamples(uint8_t* buf, int framesize);
static bool isOutputClosed();
bool IsOutputClosed();
static void playMono16Sound(const QByteArray& data);
void PlayMono16Sound(const QByteArray& data);
static bool tryCaptureSamples(uint8_t* buf, int framesize);
bool TryCaptureSamples(uint8_t* buf, int framesize);
static void playGroupAudioQueued(Tox*, int group, int peer, const int16_t* data, static void playGroupAudioQueued(Tox*, int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate, void*); unsigned samples, uint8_t channels, unsigned sample_rate, void*);
void PlayGroupAudio(int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate);
#ifdef QTOX_FILTER_AUDIO #ifdef QTOX_FILTER_AUDIO
static void getEchoesToFilter(AudioFilterer* filter, int framesize); static void getEchoesToFilter(AudioFilterer* filter, int framesize);
@ -95,6 +77,8 @@ public:
#endif #endif
public slots: public slots:
void closeInput();
void closeOutput();
void playGroupAudio(int group, int peer, const int16_t* data, void playGroupAudio(int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate); unsigned samples, uint8_t channels, unsigned sample_rate);

View File

@ -125,8 +125,9 @@ Core::~Core()
delete[] videobuf; delete[] videobuf;
videobuf=nullptr; videobuf=nullptr;
Audio::closeInput(); Audio& audio = Audio::getInstance();
Audio::closeOutput(); audio.closeInput();
audio.closeOutput();
} }
Core* Core::getInstance() Core* Core::getInstance()

View File

@ -70,7 +70,7 @@ void Core::prepareCall(uint32_t friendId, int32_t callId, ToxAv* toxav, bool vid
qWarning() << QString("Error starting call %1: toxav_prepare_transmission failed with %2").arg(callId).arg(r); qWarning() << QString("Error starting call %1: toxav_prepare_transmission failed with %2").arg(callId).arg(r);
// Audio // Audio
Audio::suscribeInput(); Audio::getInstance().subscribeInput();
// Go // Go
calls[callId].active = true; calls[callId].active = true;
@ -248,7 +248,7 @@ void Core::cleanupCall(int32_t callId)
} }
} }
Audio::unsuscribeInput(); Audio::getInstance().unsubscribeInput();
toxav_kill_transmission(Core::getInstance()->toxav, callId); toxav_kill_transmission(Core::getInstance()->toxav, callId);
if (!anyActiveCalls()) if (!anyActiveCalls())
@ -278,7 +278,7 @@ void Core::sendCallAudio(int32_t callId, ToxAv* toxav)
if (!calls[callId].active) if (!calls[callId].active)
return; return;
if (calls[callId].muteMic || !Audio::isInputReady()) if (calls[callId].muteMic || !Audio::getInstance().isInputReady())
{ {
calls[callId].sendAudioTimer->start(); calls[callId].sendAudioTimer->start();
return; return;
@ -288,7 +288,7 @@ void Core::sendCallAudio(int32_t callId, ToxAv* toxav)
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels; const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
uint8_t buf[bufsize]; uint8_t buf[bufsize];
if (Audio::tryCaptureSamples(buf, framesize)) if (Audio::getInstance().tryCaptureSamples(buf, framesize))
{ {
#ifdef QTOX_FILTER_AUDIO #ifdef QTOX_FILTER_AUDIO
if (Settings::getInstance().getFilterAudio()) if (Settings::getInstance().getFilterAudio())
@ -602,7 +602,7 @@ void Core::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, un
ALint state; ALint state;
alGetSourcei(alSource, AL_SOURCE_STATE, &state); alGetSourcei(alSource, AL_SOURCE_STATE, &state);
alSourcef(alSource, AL_GAIN, Audio::getOutputVolume()); alSourcef(alSource, AL_GAIN, Audio::getInstance().getOutputVolume());
if (state != AL_PLAYING) if (state != AL_PLAYING)
{ {
alSourcePlay(alSource); alSourcePlay(alSource);
@ -628,7 +628,7 @@ void Core::joinGroupCall(int groupId)
groupCalls[groupId].codecSettings.max_video_height = TOXAV_MAX_VIDEO_HEIGHT; groupCalls[groupId].codecSettings.max_video_height = TOXAV_MAX_VIDEO_HEIGHT;
// Audio // Audio
Audio::suscribeInput(); Audio::getInstance().subscribeInput();
// Go // Go
Core* core = Core::getInstance(); Core* core = Core::getInstance();
@ -651,7 +651,7 @@ void Core::leaveGroupCall(int groupId)
for (ALuint source : groupCalls[groupId].alSources) for (ALuint source : groupCalls[groupId].alSources)
alDeleteSources(1, &source); alDeleteSources(1, &source);
groupCalls[groupId].alSources.clear(); groupCalls[groupId].alSources.clear();
Audio::unsuscribeInput(); Audio::getInstance().unsubscribeInput();
delete groupCalls[groupId].sendAudioTimer; delete groupCalls[groupId].sendAudioTimer;
} }
@ -660,7 +660,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav)
if (!groupCalls[groupId].active) if (!groupCalls[groupId].active)
return; return;
if (groupCalls[groupId].muteMic || !Audio::isInputReady()) if (groupCalls[groupId].muteMic || !Audio::getInstance().isInputReady())
{ {
groupCalls[groupId].sendAudioTimer->start(); groupCalls[groupId].sendAudioTimer->start();
return; return;
@ -670,7 +670,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav)
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels; const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
uint8_t buf[bufsize]; uint8_t buf[bufsize];
if (Audio::tryCaptureSamples(buf, framesize)) if (Audio::getInstance().tryCaptureSamples(buf, framesize))
{ {
if (toxav_group_send_audio(toxav_get_tox(toxav), groupId, (int16_t*)buf, if (toxav_group_send_audio(toxav_get_tox(toxav), groupId, (int16_t*)buf,
framesize, av_DefaultSettings.audio_channels, av_DefaultSettings.audio_sample_rate) < 0) framesize, av_DefaultSettings.audio_channels, av_DefaultSettings.audio_sample_rate) < 0)

View File

@ -89,7 +89,7 @@ void AVForm::showEvent(QShowEvent*)
getAudioInDevices(); getAudioInDevices();
createVideoSurface(); createVideoSurface();
getVideoDevices(); getVideoDevices();
Audio::suscribeInput(); Audio::getInstance().subscribeInput();
} }
void AVForm::onVideoModesIndexChanged(int index) void AVForm::onVideoModesIndexChanged(int index)
@ -220,7 +220,7 @@ void AVForm::hideEvent(QHideEvent *)
killVideoSurface(); killVideoSurface();
} }
videoDeviceList.clear(); videoDeviceList.clear();
Audio::unsuscribeInput(); Audio::getInstance().unsubscribeInput();
} }
void AVForm::getVideoDevices() void AVForm::getVideoDevices()
@ -314,19 +314,21 @@ void AVForm::getAudioOutDevices()
void AVForm::onInDevChanged(const QString &deviceDescriptor) void AVForm::onInDevChanged(const QString &deviceDescriptor)
{ {
Settings::getInstance().setInDev(deviceDescriptor); Settings::getInstance().setInDev(deviceDescriptor);
Audio::openInput(deviceDescriptor);
Audio::unsuscribeInput(); Audio& audio = Audio::getInstance();
Audio::suscribeInput(); audio.openInput(deviceDescriptor);
audio.unsubscribeInput();
audio.subscribeInput();
} }
void AVForm::onOutDevChanged(const QString& deviceDescriptor) void AVForm::onOutDevChanged(const QString& deviceDescriptor)
{ {
Settings::getInstance().setInDev(deviceDescriptor); Settings::getInstance().setOutDev(deviceDescriptor);
Audio::openOutput(deviceDescriptor);
Audio::unsuscribeInput(); Audio& audio = Audio::getInstance();
Audio::suscribeInput(); audio.openOutput(deviceDescriptor);
audio.unsubscribeInput();
audio.subscribeInput();
} }
void AVForm::onFilterAudioToggled(bool filterAudio) void AVForm::onFilterAudioToggled(bool filterAudio)
@ -336,13 +338,13 @@ void AVForm::onFilterAudioToggled(bool filterAudio)
void AVForm::on_playbackSlider_valueChanged(int value) void AVForm::on_playbackSlider_valueChanged(int value)
{ {
Audio::setOutputVolume(value / 100.0); Audio::getInstance().setOutputVolume(value / 100.0);
bodyUI->playbackMax->setText(QString::number(value)); bodyUI->playbackMax->setText(QString::number(value));
} }
void AVForm::on_microphoneSlider_valueChanged(int value) void AVForm::on_microphoneSlider_valueChanged(int value)
{ {
Audio::setInputVolume(value / 100.0); Audio::getInstance().setInputVolume(value / 100.0);
bodyUI->microphoneMax->setText(QString::number(value)); bodyUI->microphoneMax->setText(QString::number(value));
} }

View File

@ -65,7 +65,7 @@ void MicFeedbackWidget::timerEvent(QTimerEvent*)
uint8_t buff[bufsize]; uint8_t buff[bufsize];
memset(buff, 0, bufsize); memset(buff, 0, bufsize);
if (Audio::tryCaptureSamples(buff, framesize)) if (Audio::getInstance().tryCaptureSamples(buff, framesize))
{ {
double max = 0; double max = 0;
int16_t* buffReal = reinterpret_cast<int16_t*>(&buff[0]); int16_t* buffReal = reinterpret_cast<int16_t*>(&buff[0]);
@ -88,13 +88,13 @@ void MicFeedbackWidget::timerEvent(QTimerEvent*)
void MicFeedbackWidget::showEvent(QShowEvent*) void MicFeedbackWidget::showEvent(QShowEvent*)
{ {
Audio::suscribeInput(); Audio::getInstance().subscribeInput();
timerId = startTimer(60); timerId = startTimer(60);
} }
void MicFeedbackWidget::hideEvent(QHideEvent*) void MicFeedbackWidget::hideEvent(QHideEvent*)
{ {
Audio::unsuscribeInput(); Audio::getInstance().unsubscribeInput();
if (timerId != 0) if (timerId != 0)
{ {

View File

@ -1249,7 +1249,7 @@ bool Widget::newMessageAlert(QWidget* currentWindow, bool isActive, bool notify)
sndFile.close(); sndFile.close();
} }
Audio::playMono16Sound(sndData); Audio::getInstance().playMono16Sound(sndData);
} }
} }
@ -1271,7 +1271,7 @@ void Widget::playRingtone()
sndFile1.close(); sndFile1.close();
} }
Audio::playMono16Sound(sndData1); Audio::getInstance().playMono16Sound(sndData1);
} }
void Widget::onFriendRequestReceived(const QString& userId, const QString& message) void Widget::onFriendRequestReceived(const QString& userId, const QString& message)