From 7ed2d97aadab8aa0acb39e54aafaea06c0ebe57c Mon Sep 17 00:00:00 2001 From: tox-user Date: Fri, 29 Sep 2017 12:33:30 +0200 Subject: [PATCH] feat(settings): make audio quality setting persistent --- src/core/coreav.cpp | 7 ++-- src/core/coreav.h | 1 - src/persistence/settings.cpp | 18 +++++++++++ src/persistence/settings.h | 6 ++++ src/widget/form/settings/avform.cpp | 50 +++++++++++++---------------- src/widget/form/settings/avform.ui | 2 +- 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/core/coreav.cpp b/src/core/coreav.cpp index 64715adfe..4edd8b7c3 100644 --- a/src/core/coreav.cpp +++ b/src/core/coreav.cpp @@ -48,9 +48,6 @@ * @brief Sent when a call was ended by the peer. * @param friendId Id of friend in call list. * - * @var CoreAV::audioBitrate - * @brief In kb/s. More than enough for Opus. - * * @var CoreAV::VIDEO_DEFAULT_BITRATE * @brief Picked at random by fair dice roll. */ @@ -236,7 +233,7 @@ bool CoreAV::answerCall(uint32_t friendNum) qDebug() << QString("answering call %1").arg(friendNum); assert(calls.contains(friendNum)); TOXAV_ERR_ANSWER err; - if (toxav_answer(toxav, friendNum, audioBitrate, VIDEO_DEFAULT_BITRATE, &err)) { + if (toxav_answer(toxav, friendNum, Settings::getInstance().getAudioBitrate(), VIDEO_DEFAULT_BITRATE, &err)) { calls[friendNum].inactive = false; return true; } else { @@ -271,7 +268,7 @@ bool CoreAV::startCall(uint32_t friendNum, bool video) } uint32_t videoBitrate = video ? VIDEO_DEFAULT_BITRATE : 0; - if (!toxav_call(toxav, friendNum, audioBitrate, videoBitrate, nullptr)) + if (!toxav_call(toxav, friendNum, Settings::getInstance().getAudioBitrate(), videoBitrate, nullptr)) return false; auto call = calls.insert({friendNum, video, *this}); diff --git a/src/core/coreav.h b/src/core/coreav.h index 0db54a632..e7e56ab17 100644 --- a/src/core/coreav.h +++ b/src/core/coreav.h @@ -81,7 +81,6 @@ public: uint8_t channels, unsigned sample_rate, void* core); static void invalidateGroupCallPeerSource(int group, int peer); - uint32_t audioBitrate = 64; public slots: bool startCall(uint32_t friendNum, bool video = false); bool answerCall(uint32_t friendNum); diff --git a/src/persistence/settings.cpp b/src/persistence/settings.cpp index 57d7f9142..57b5ba2ca 100644 --- a/src/persistence/settings.cpp +++ b/src/persistence/settings.cpp @@ -265,6 +265,7 @@ void Settings::loadGlobal() audioOutDevEnabled = s.value("audioOutDevEnabled", true).toBool(); audioInGainDecibel = s.value("inGain", 0).toReal(); outVolume = s.value("outVolume", 100).toInt(); + audioBitrate = s.value("audioBitrate", 64).toInt(); enableBackend2 = false; #ifdef USE_FILTERAUDIO enableBackend2 = s.value("enableBackend2", false).toBool(); @@ -566,6 +567,7 @@ void Settings::saveGlobal() s.setValue("audioOutDevEnabled", audioOutDevEnabled); s.setValue("inGain", audioInGainDecibel); s.setValue("outVolume", outVolume); + s.setValue("audioBitrate", audioBitrate); s.setValue("enableBackend2", enableBackend2); } s.endGroup(); @@ -1882,6 +1884,22 @@ void Settings::setOutVolume(int volume) } } +int Settings::getAudioBitrate() const +{ + QMutexLocker locker{&bigLock}; + return audioBitrate; +} + +void Settings::setAudioBitrate(int bitrate) +{ + QMutexLocker locker{&bigLock}; + + if (bitrate != audioBitrate) { + audioBitrate = bitrate; + emit audioBitrateChanged(audioBitrate); + } +} + bool Settings::getEnableBackend2() const { QMutexLocker locker{&bigLock}; diff --git a/src/persistence/settings.h b/src/persistence/settings.h index c3edc2d52..4c1408eb0 100644 --- a/src/persistence/settings.h +++ b/src/persistence/settings.h @@ -101,6 +101,7 @@ class Settings : public QObject Q_PROPERTY(bool audioOutDevEnabled READ getAudioOutDevEnabled WRITE setAudioOutDevEnabled NOTIFY audioOutDevEnabledChanged FINAL) Q_PROPERTY(int outVolume READ getOutVolume WRITE setOutVolume NOTIFY outVolumeChanged FINAL) + Q_PROPERTY(int audioBitrate READ getAudioBitrate WRITE setAudioBitrate NOTIFY audioBitrateChanged FINAL) Q_PROPERTY(bool enableBackend2 READ getEnableBackend2 WRITE setEnableBackend2 NOTIFY enableBackend2Changed FINAL) @@ -241,6 +242,7 @@ signals: void outDevChanged(const QString& name); void audioOutDevEnabledChanged(bool enabled); void outVolumeChanged(int volume); + void audioBitrateChanged(int bitrate); void enableTestSoundChanged(bool enabled); void enableBackend2Changed(bool enabled); @@ -369,6 +371,9 @@ public: int getOutVolume() const; void setOutVolume(int volume); + int getAudioBitrate() const; + void setAudioBitrate(int bitrate); + bool getEnableTestSound() const; void setEnableTestSound(bool newValue); @@ -631,6 +636,7 @@ private: QString outDev; bool audioOutDevEnabled; int outVolume; + int audioBitrate; bool enableTestSound; bool enableBackend2; diff --git a/src/widget/form/settings/avform.cpp b/src/widget/form/settings/avform.cpp index 3e6def05d..603979d61 100644 --- a/src/widget/form/settings/avform.cpp +++ b/src/widget/form/settings/avform.cpp @@ -83,6 +83,8 @@ AVForm::AVForm() microphoneSlider->setTracking(false); microphoneSlider->installEventFilter(this); + fillAudioQualityComboBox(); + eventsInit(); QDesktopWidget* desktop = QApplication::desktop(); @@ -121,7 +123,6 @@ void AVForm::showEvent(QShowEvent* event) getAudioInDevices(); createVideoSurface(); getVideoDevices(); - fillAudioQualityComboBox(); if (!subscribedToAudioIn) { // TODO: This should not be done in show/hide events @@ -337,18 +338,25 @@ void AVForm::fillScreenModesComboBox() void AVForm::fillAudioQualityComboBox() { bool previouslyBlocked = audioQualityComboBox->blockSignals(true); - audioQualityComboBox->clear(); - QString name; - name = tr("High (64 kbps)"); - audioQualityComboBox->addItem(name); - name = tr("Medium (32 kbps)"); - audioQualityComboBox->addItem(name); - name = tr("Low (16 kbps)"); - audioQualityComboBox->addItem(name); - name = tr("Very Low (8 kbps)"); - audioQualityComboBox->addItem(name); + QStringList names; + names << tr("High (64 kbps)") << tr("Medium (32 kbps)") << tr("Low (16 kbps") + << tr("Very low (8 kbps)"); + audioQualityComboBox->addItems(names); + int currentBitrate = Settings::getInstance().getAudioBitrate(); + QString currentBitrateString = QString::number(currentBitrate); + int index = 0; + + for (int i = 0; i < names.length(); i++) { + QString name = names.at(i); + if (name.contains(currentBitrateString)) { + index = i; + break; + } + } + + audioQualityComboBox->setCurrentIndex(index); audioQualityComboBox->blockSignals(previouslyBlocked); } @@ -429,23 +437,9 @@ void AVForm::on_videoDevCombobox_currentIndexChanged(int index) void AVForm::on_audioQualityComboBox_currentIndexChanged(int index) { - uint32_t bitrate; - switch (index) { - case 1: - bitrate = 32; - break; - case 2: - bitrate = 16; - break; - case 3: - bitrate = 8; - break; - default: - bitrate = 64; - break; - } - - Core::getInstance()->getAv()->audioBitrate = bitrate; + int bitrates[] = { 64, 32, 16, 8 }; + int bitrate = bitrates[index]; + Settings::getInstance().setAudioBitrate(bitrate); } void AVForm::getVideoDevices() diff --git a/src/widget/form/settings/avform.ui b/src/widget/form/settings/avform.ui index b7b77b512..a231f7b7a 100644 --- a/src/widget/form/settings/avform.ui +++ b/src/widget/form/settings/avform.ui @@ -111,7 +111,7 @@ - Audio Quality + Audio quality