From b7cf4df13f741a0eb812531ab39a8c0b7195610a Mon Sep 17 00:00:00 2001 From: Nils Fenner Date: Tue, 20 Oct 2015 03:46:44 +0200 Subject: [PATCH] Get rid of static methods and bugs --- src/audio/audio.cpp | 123 +++++--------------------- src/audio/audio.h | 42 +++------ src/core/core.cpp | 5 +- src/core/coreav.cpp | 18 ++-- src/widget/form/settings/avform.cpp | 24 ++--- src/widget/tool/micfeedbackwidget.cpp | 6 +- src/widget/widget.cpp | 4 +- 7 files changed, 66 insertions(+), 156 deletions(-) diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index b46c2f7fb..9a8eeb205 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -92,29 +92,19 @@ void Audio::startAudioThread() moveToThread(audioThread); } -float Audio::getOutputVolume() -{ - return getInstance().GetOutputVolume(); -} - /** Returns the current output volume, between 0 and 1 */ -qreal Audio::GetOutputVolume() +qreal Audio::getOutputVolume() { QMutexLocker locker(&audioOutLock); return outputVolume; } -void Audio::setOutputVolume(float volume) -{ - getInstance().SetOutputVolume(volume); -} - /** The volume must be between 0 and 1 */ -void Audio::SetOutputVolume(qreal volume) +void Audio::setOutputVolume(qreal volume) { QMutexLocker locker(&audioOutLock); outputVolume = volume; @@ -139,33 +129,24 @@ void Audio::SetOutputVolume(qreal volume) /** The volume must be between 0 and 2 */ -void Audio::setInputVolume(float volume) -{ - getInstance().SetInputVolume(volume); -} - -void Audio::SetInputVolume(qreal volume) +void Audio::setInputVolume(qreal volume) { QMutexLocker locker(&audioInLock); inputVolume = volume; } -void Audio::suscribeInput() -{ - getInstance().SubscribeInput(); -} - /** @brief Subscribe to capture sound from the opened input device. If the input device is not open, it will be opened before capturing. */ -void Audio::SubscribeInput() +void Audio::subscribeInput() { qDebug() << "subscribing input" << inputSubscriptions; if (!inputSubscriptions++) { - OpenInput(Settings::getInstance().getInDev()); + openInput(Settings::getInstance().getInDev()); + openOutput(Settings::getInstance().getOutDev()); #if (!FIX_SND_PCM_PREPARE_BUG) if (alInDev) @@ -175,16 +156,6 @@ void Audio::SubscribeInput() } #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. */ -void Audio::UnsubscribeInput() +void Audio::unsubscribeInput() { qDebug() << "unsubscribing input" << inputSubscriptions; if (inputSubscriptions > 0) @@ -200,29 +171,16 @@ void Audio::UnsubscribeInput() else if(inputSubscriptions < 0) inputSubscriptions = 0; - if (!inputSubscriptions) - { + if (!inputSubscriptions) { closeOutput(); -#if (!FIX_SND_PCM_PREPARE_BUG) - if (alInDev) - { - qDebug() << "stopping capture"; - alcCaptureStop(alInDev); - } -#endif closeInput(); } } -void Audio::openInput(const QString& inDevDescr) -{ - getInstance().OpenInput(inDevDescr); -} - /** Open an input device, use before suscribing */ -void Audio::OpenInput(const QString& inDevDescr) +void Audio::openInput(const QString& inDevDescr) { QMutexLocker lock(&audioInLock); 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 */ -bool Audio::OpenOutput(const QString &outDevDescr) +bool Audio::openOutput(const QString &outDevDescr) { QMutexLocker lock(&audioOutLock); auto* tmp = alOutDev; @@ -314,20 +267,20 @@ bool Audio::OpenOutput(const QString &outDevDescr) return true; } -void Audio::closeInput() -{ - getInstance().CloseInput(); -} - /** Close an input device, please don't use unless everyone's unsuscribed */ -void Audio::CloseInput() +void Audio::closeInput() { qDebug() << "Closing input"; QMutexLocker locker(&audioInLock); if (alInDev) { +#if (!FIX_SND_PCM_PREPARE_BUG) + qDebug() << "stopping capture"; + alcCaptureStop(alInDev); +#endif + if (alcCaptureCloseDevice(alInDev) == ALC_TRUE) { alInDev = nullptr; @@ -340,15 +293,10 @@ void Audio::CloseInput() } } -void Audio::closeOutput() -{ - getInstance().CloseOutput(); -} - /** Close an output device */ -void Audio::CloseOutput() +void Audio::closeOutput() { qDebug() << "Closing output"; 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 */ -void Audio::PlayMono16Sound(const QByteArray& data) +void Audio::playMono16Sound(const QByteArray& data) { QMutexLocker lock(&audioOutLock); @@ -430,20 +373,15 @@ void Audio::playGroupAudioQueued(Tox*,int group, int peer, const int16_t* data, emit static_cast(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 */ -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) { assert(QThread::currentThread() == audioThread); - QMutexLocker lock(&audioOutLock); ToxGroupCall& call = Core::groupCalls[group]; @@ -507,43 +445,28 @@ void Audio::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, u alSourcePlay(alSource); } -bool Audio::isInputReady() -{ - return getInstance().IsInputReady(); -} - /** Returns true if the input device is open and suscribed to */ -bool Audio::IsInputReady() +bool Audio::isInputReady() { QMutexLocker locker(&audioInLock); return alInDev && inputSubscriptions; } -bool Audio::isOutputClosed() -{ - return getInstance().IsOutputClosed(); -} - /** Returns true if the output device is open */ -bool Audio::IsOutputClosed() +bool Audio::isOutputClosed() { QMutexLocker locker(&audioOutLock); return alOutDev; } -bool Audio::tryCaptureSamples(uint8_t* buf, int framesize) -{ - return getInstance().TryCaptureSamples(buf, framesize); -} - /** 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); diff --git a/src/audio/audio.h b/src/audio/audio.h index fea900e43..d508b1304 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -52,42 +52,24 @@ public: public: void startAudioThread(); - static float getOutputVolume(); - qreal GetOutputVolume(); - static void setOutputVolume(float volume); - void SetOutputVolume(qreal volume); + qreal getOutputVolume(); + void setOutputVolume(qreal volume); - static void setInputVolume(float volume); - void SetInputVolume(qreal volume); + void setInputVolume(qreal volume); - static void suscribeInput(); - void SubscribeInput(); - static void unsuscribeInput(); - void UnsubscribeInput(); + void subscribeInput(); + void unsubscribeInput(); + void openInput(const QString& inDevDescr); + bool openOutput(const QString& outDevDescr); - static void openInput(const QString& inDevDescr); - void OpenInput(const QString& inDevDescr); - static bool openOutput(const QString& outDevDescr); - bool OpenOutput(const QString& outDevDescr); - static void closeInput(); - void CloseInput(); - static void closeOutput(); - void CloseOutput(); + bool isInputReady(); + bool isOutputClosed(); - static bool isInputReady(); - bool IsInputReady(); - 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); + void playMono16Sound(const QByteArray& data); + bool tryCaptureSamples(uint8_t* buf, int framesize); static void playGroupAudioQueued(Tox*, int group, int peer, const int16_t* data, 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 static void getEchoesToFilter(AudioFilterer* filter, int framesize); @@ -95,6 +77,8 @@ public: #endif public slots: + void closeInput(); + void closeOutput(); void playGroupAudio(int group, int peer, const int16_t* data, unsigned samples, uint8_t channels, unsigned sample_rate); diff --git a/src/core/core.cpp b/src/core/core.cpp index 760ce7267..04ec8ac08 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -125,8 +125,9 @@ Core::~Core() delete[] videobuf; videobuf=nullptr; - Audio::closeInput(); - Audio::closeOutput(); + Audio& audio = Audio::getInstance(); + audio.closeInput(); + audio.closeOutput(); } Core* Core::getInstance() diff --git a/src/core/coreav.cpp b/src/core/coreav.cpp index 578afee6d..70bf505b0 100644 --- a/src/core/coreav.cpp +++ b/src/core/coreav.cpp @@ -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); // Audio - Audio::suscribeInput(); + Audio::getInstance().subscribeInput(); // Go 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); if (!anyActiveCalls()) @@ -278,7 +278,7 @@ void Core::sendCallAudio(int32_t callId, ToxAv* toxav) if (!calls[callId].active) return; - if (calls[callId].muteMic || !Audio::isInputReady()) + if (calls[callId].muteMic || !Audio::getInstance().isInputReady()) { calls[callId].sendAudioTimer->start(); return; @@ -288,7 +288,7 @@ void Core::sendCallAudio(int32_t callId, ToxAv* toxav) const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels; uint8_t buf[bufsize]; - if (Audio::tryCaptureSamples(buf, framesize)) + if (Audio::getInstance().tryCaptureSamples(buf, framesize)) { #ifdef QTOX_FILTER_AUDIO if (Settings::getInstance().getFilterAudio()) @@ -602,7 +602,7 @@ void Core::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, un ALint state; alGetSourcei(alSource, AL_SOURCE_STATE, &state); - alSourcef(alSource, AL_GAIN, Audio::getOutputVolume()); + alSourcef(alSource, AL_GAIN, Audio::getInstance().getOutputVolume()); if (state != AL_PLAYING) { alSourcePlay(alSource); @@ -628,7 +628,7 @@ void Core::joinGroupCall(int groupId) groupCalls[groupId].codecSettings.max_video_height = TOXAV_MAX_VIDEO_HEIGHT; // Audio - Audio::suscribeInput(); + Audio::getInstance().subscribeInput(); // Go Core* core = Core::getInstance(); @@ -651,7 +651,7 @@ void Core::leaveGroupCall(int groupId) for (ALuint source : groupCalls[groupId].alSources) alDeleteSources(1, &source); groupCalls[groupId].alSources.clear(); - Audio::unsuscribeInput(); + Audio::getInstance().unsubscribeInput(); delete groupCalls[groupId].sendAudioTimer; } @@ -660,7 +660,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav) if (!groupCalls[groupId].active) return; - if (groupCalls[groupId].muteMic || !Audio::isInputReady()) + if (groupCalls[groupId].muteMic || !Audio::getInstance().isInputReady()) { groupCalls[groupId].sendAudioTimer->start(); return; @@ -670,7 +670,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav) const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels; 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, framesize, av_DefaultSettings.audio_channels, av_DefaultSettings.audio_sample_rate) < 0) diff --git a/src/widget/form/settings/avform.cpp b/src/widget/form/settings/avform.cpp index 61e7ad9ec..a2111e44c 100644 --- a/src/widget/form/settings/avform.cpp +++ b/src/widget/form/settings/avform.cpp @@ -89,7 +89,7 @@ void AVForm::showEvent(QShowEvent*) getAudioInDevices(); createVideoSurface(); getVideoDevices(); - Audio::suscribeInput(); + Audio::getInstance().subscribeInput(); } void AVForm::onVideoModesIndexChanged(int index) @@ -220,7 +220,7 @@ void AVForm::hideEvent(QHideEvent *) killVideoSurface(); } videoDeviceList.clear(); - Audio::unsuscribeInput(); + Audio::getInstance().unsubscribeInput(); } void AVForm::getVideoDevices() @@ -314,19 +314,21 @@ void AVForm::getAudioOutDevices() void AVForm::onInDevChanged(const QString &deviceDescriptor) { Settings::getInstance().setInDev(deviceDescriptor); - Audio::openInput(deviceDescriptor); - Audio::unsuscribeInput(); - Audio::suscribeInput(); + Audio& audio = Audio::getInstance(); + audio.openInput(deviceDescriptor); + audio.unsubscribeInput(); + audio.subscribeInput(); } void AVForm::onOutDevChanged(const QString& deviceDescriptor) { - Settings::getInstance().setInDev(deviceDescriptor); - Audio::openOutput(deviceDescriptor); + Settings::getInstance().setOutDev(deviceDescriptor); - Audio::unsuscribeInput(); - Audio::suscribeInput(); + Audio& audio = Audio::getInstance(); + audio.openOutput(deviceDescriptor); + audio.unsubscribeInput(); + audio.subscribeInput(); } void AVForm::onFilterAudioToggled(bool filterAudio) @@ -336,13 +338,13 @@ void AVForm::onFilterAudioToggled(bool filterAudio) void AVForm::on_playbackSlider_valueChanged(int value) { - Audio::setOutputVolume(value / 100.0); + Audio::getInstance().setOutputVolume(value / 100.0); bodyUI->playbackMax->setText(QString::number(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)); } diff --git a/src/widget/tool/micfeedbackwidget.cpp b/src/widget/tool/micfeedbackwidget.cpp index 358614d6b..3568ab07c 100644 --- a/src/widget/tool/micfeedbackwidget.cpp +++ b/src/widget/tool/micfeedbackwidget.cpp @@ -65,7 +65,7 @@ void MicFeedbackWidget::timerEvent(QTimerEvent*) uint8_t buff[bufsize]; memset(buff, 0, bufsize); - if (Audio::tryCaptureSamples(buff, framesize)) + if (Audio::getInstance().tryCaptureSamples(buff, framesize)) { double max = 0; int16_t* buffReal = reinterpret_cast(&buff[0]); @@ -88,13 +88,13 @@ void MicFeedbackWidget::timerEvent(QTimerEvent*) void MicFeedbackWidget::showEvent(QShowEvent*) { - Audio::suscribeInput(); + Audio::getInstance().subscribeInput(); timerId = startTimer(60); } void MicFeedbackWidget::hideEvent(QHideEvent*) { - Audio::unsuscribeInput(); + Audio::getInstance().unsubscribeInput(); if (timerId != 0) { diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 410d3fd70..db1c1eafd 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -1249,7 +1249,7 @@ bool Widget::newMessageAlert(QWidget* currentWindow, bool isActive, bool notify) sndFile.close(); } - Audio::playMono16Sound(sndData); + Audio::getInstance().playMono16Sound(sndData); } } @@ -1271,7 +1271,7 @@ void Widget::playRingtone() sndFile1.close(); } - Audio::playMono16Sound(sndData1); + Audio::getInstance().playMono16Sound(sndData1); } void Widget::onFriendRequestReceived(const QString& userId, const QString& message)