From ff33b596836bea8182399e3fedc2e95013a2c578 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 9 Oct 2019 14:56:04 +0200 Subject: [PATCH] Ref T739, make member of CAfvClient a "pointer" * we can use forward declaration for m_voiceClient * make IContextAudio -> BlackMisc::CIdentifiable * removed delayedInitMicrophone, RR will move it to another place --- src/blackcore/context/contextaudio.cpp | 98 ++++++++++++---------- src/blackcore/context/contextaudio.h | 29 +++---- src/blackcore/context/contextaudioimpl.cpp | 3 +- src/blackcore/context/contextaudioimpl.h | 4 +- src/blackgui/components/afvmapdialog.cpp | 37 ++++---- src/blackgui/components/afvmapdialog.h | 2 +- 6 files changed, 89 insertions(+), 84 deletions(-) diff --git a/src/blackcore/context/contextaudio.cpp b/src/blackcore/context/contextaudio.cpp index 9ea8f25cc..2022973db 100644 --- a/src/blackcore/context/contextaudio.cpp +++ b/src/blackcore/context/contextaudio.cpp @@ -8,6 +8,7 @@ #include "contextaudio.h" +#include "blackcore/afv/clients/afvclient.h" #include "blackcore/context/contextaudio.h" #include "blackcore/context/contextnetwork.h" // for user login #include "blackcore/context/contextownaircraft.h" // for COM integration @@ -33,42 +34,33 @@ namespace BlackCore namespace Context { IContextAudio::IContextAudio(CCoreFacadeConfig::ContextMode mode, CCoreFacade *runtime) : - IContext(mode, runtime), m_voiceClient(CVoiceSetup().getAfvVoiceServerUrl(), this) + IContext(mode, runtime), + CIdentifiable(this), + m_voiceClient(new CAfvClient(CVoiceSetup().getAfvVoiceServerUrl(), this)) { const CVoiceSetup vs = m_voiceSettings.getThreadLocal(); - m_voiceClient.updateVoiceServerUrl(vs.getAfvVoiceServerUrl()); + m_voiceClient->updateVoiceServerUrl(vs.getAfvVoiceServerUrl()); - Q_ASSERT_X(CThreadUtils::isApplicationThread(m_voiceClient.thread()), Q_FUNC_INFO, "Should be in main thread"); - m_voiceClient.start(); - Q_ASSERT_X(m_voiceClient.owner() == this, Q_FUNC_INFO, "Wrong owner"); - Q_ASSERT_X(!CThreadUtils::isApplicationThread(m_voiceClient.thread()), Q_FUNC_INFO, "Must NOT be in main thread"); + Q_ASSERT_X(CThreadUtils::isApplicationThread(m_voiceClient->thread()), Q_FUNC_INFO, "Should be in main thread"); + m_voiceClient->start(); + Q_ASSERT_X(m_voiceClient->owner() == this, Q_FUNC_INFO, "Wrong owner"); + Q_ASSERT_X(!CThreadUtils::isApplicationThread(m_voiceClient->thread()), Q_FUNC_INFO, "Must NOT be in main thread"); const CSettings as = m_audioSettings.getThreadLocal(); this->setVoiceOutputVolume(as.getOutVolume()); m_selcalPlayer = new CSelcalPlayer(CAudioDeviceInfo::getDefaultOutputDevice(), this); - connect(&m_voiceClient, &CAfvClient::ptt, this, &IContextAudio::ptt, Qt::QueuedConnection); + connect(m_voiceClient.data(), &CAfvClient::ptt, this, &IContextAudio::ptt, Qt::QueuedConnection); this->changeDeviceSettings(); QPointer myself(this); QTimer::singleShot(5000, this, [ = ] { - if (!myself) { return; } - if (!sApp || sApp->isShuttingDown()) { return; } + if (!myself || !sApp || sApp->isShuttingDown()) { return; } myself->onChangedAudioSettings(); - myself->delayedInitMicrophone(); }); } - void IContextAudio::delayedInitMicrophone() - { -#ifdef Q_OS_MAC - // m_voiceInputDevice = m_voice->createInputDevice(); - // m_voice->connectVoice(m_voiceInputDevice.get(), m_audioMixer.get(), IAudioMixer::InputMicrophone); - CLogMessage(this).info(u"MacOS delayed input device init"); -#endif - } - const QString &IContextAudio::InterfaceName() { static const QString s(BLACKCORE_CONTEXTAUDIO_INTERFACENAME); @@ -107,9 +99,12 @@ namespace BlackCore void IContextAudio::gracefulShutdown() { - m_voiceClient.stopAudio(); - m_voiceClient.quitAndWait(); - Q_ASSERT_X(CThreadUtils::isCurrentThreadObjectThread(&m_voiceClient), Q_FUNC_INFO, "Needs to be back in current thread"); + if (m_voiceClient) + { + m_voiceClient->stopAudio(); + m_voiceClient->quitAndWait(); + Q_ASSERT_X(CThreadUtils::isCurrentThreadObjectThread(m_voiceClient.data()), Q_FUNC_INFO, "Needs to be back in current thread"); + } QObject::disconnect(this); } @@ -163,11 +158,13 @@ namespace BlackCore CAudioDeviceInfoList IContextAudio::getCurrentAudioDevices() const { + if (!m_voiceClient) { return {}; } + // either the devices really used, or settings - CAudioDeviceInfo inputDevice = m_voiceClient.getInputDevice(); + CAudioDeviceInfo inputDevice = m_voiceClient->getInputDevice(); if (!inputDevice.isValid()) { inputDevice = CAudioDeviceInfo::getDefaultInputDevice(); } - CAudioDeviceInfo outputDevice = m_voiceClient.getOutputDevice(); + CAudioDeviceInfo outputDevice = m_voiceClient->getOutputDevice(); if (!outputDevice.isValid()) { outputDevice = CAudioDeviceInfo::getDefaultOutputDevice(); } CAudioDeviceInfoList devices; @@ -178,13 +175,13 @@ namespace BlackCore void IContextAudio::setCurrentAudioDevices(const CAudioDeviceInfo &inputDevice, const CAudioDeviceInfo &outputDevice) { + if (!m_voiceClient) { return; } if (!inputDevice.getName().isEmpty()) { m_inputDeviceSetting.setAndSave(inputDevice.getName()); } if (!outputDevice.getName().isEmpty()) { m_outputDeviceSetting.setAndSave(outputDevice.getName()); } - const bool changed = m_voiceClient.restartWithNewDevices(inputDevice, outputDevice); + const bool changed = m_voiceClient->restartWithNewDevices(inputDevice, outputDevice); const CVoiceSetup vs = m_voiceSettings.getThreadLocal(); - m_voiceClient.updateVoiceServerUrl(vs.getAfvVoiceServerUrl()); - + m_voiceClient->updateVoiceServerUrl(vs.getAfvVoiceServerUrl()); if (changed) { emit this->changedSelectedAudioDevices(this->getCurrentAudioDevices()); @@ -193,14 +190,16 @@ namespace BlackCore void IContextAudio::setVoiceOutputVolume(int volume) { + if (!m_voiceClient) { return; } + const bool wasMuted = this->isMuted(); volume = CSettings::fixOutVolume(volume); - const int currentVolume = m_voiceClient.getNormalizedOutputVolume(); + const int currentVolume = m_voiceClient->getNormalizedOutputVolume(); const bool changedVoiceOutput = (currentVolume != volume); if (changedVoiceOutput) { - m_voiceClient.setNormalizedOutputVolume(volume); + m_voiceClient->setNormalizedOutputVolume(volume); m_outVolumeBeforeMute = currentVolume; emit this->changedAudioVolume(volume); @@ -221,15 +220,17 @@ namespace BlackCore int IContextAudio::getVoiceOutputVolume() const { - return m_voiceClient.getNormalizedOutputVolume(); + if (!m_voiceClient) { return 0; } + return m_voiceClient->getNormalizedOutputVolume(); } void IContextAudio::setMute(bool muted) { + if (!m_voiceClient) { return; } if (this->isMuted() == muted) { return; } // avoid roundtrips / unnecessary signals - if (m_voiceClient.isMuted() == muted) { return; } - m_voiceClient.setMuted(muted); + if (m_voiceClient->isMuted() == muted) { return; } + m_voiceClient->setMuted(muted); // signal emit this->changedMute(muted); @@ -237,7 +238,8 @@ namespace BlackCore bool IContextAudio::isMuted() const { - return m_voiceClient.isMuted(); + if (!m_voiceClient) { return false; } + return m_voiceClient->isMuted(); } void IContextAudio::playSelcalTone(const CSelcal &selcal) @@ -284,12 +286,14 @@ namespace BlackCore void IContextAudio::enableAudioLoopback(bool enable) { - m_voiceClient.setLoopBack(enable); + if (!m_voiceClient) { return; } + m_voiceClient->setLoopBack(enable); } bool IContextAudio::isAudioLoopbackEnabled() const { - return m_voiceClient.isLoopback(); + if (!m_voiceClient) { return false; } + return m_voiceClient->isLoopback(); } void IContextAudio::setVoiceSetup(const CVoiceSetup &setup) @@ -305,7 +309,8 @@ namespace BlackCore void IContextAudio::setVoiceTransmission(bool enable, PTTCOM com) { - m_voiceClient.setPttForCom(enable, com); + if (!m_voiceClient) { return; } + m_voiceClient->setPttForCom(enable, com); } void IContextAudio::setVoiceTransmissionCom1(bool enabled) @@ -371,7 +376,7 @@ namespace BlackCore this->setVoiceOutputVolume(v); } - CComSystem IContextAudio::getOwnComSystem(CComSystem::ComUnit unit) const + CComSystem IContextAudio::xCtxGetOwnComSystem(CComSystem::ComUnit unit) const { if (!this->getIContextOwnAircraft()) { @@ -388,7 +393,7 @@ namespace BlackCore return this->getIContextOwnAircraft()->getOwnComSystem(unit); } - bool IContextAudio::isComIntegratedWithSimulator() const + bool IContextAudio::xCtxIsComIntegratedWithSimulator() const { if (!this->getIContextSimulator()) { return false; } return this->getIContextSimulator()->getSimulatorSettings().isComIntegrated(); @@ -399,9 +404,9 @@ namespace BlackCore Q_UNUSED(aircraft) Q_UNUSED(originator) - /** + /** NOT NEEDED as CAfvClient is directly "tracking changes" if (CIdentifiable::isMyIdentifier(originator)) { return; } - const bool integrated = this->isComIntegratedWithSimulator(); + const bool integrated = this->xCtxisComIntegratedWithSimulator(); if (integrated) { // set as in cockpit @@ -415,12 +420,15 @@ namespace BlackCore { Q_UNUSED(from) BLACK_VERIFY_X(this->getIContextNetwork(), Q_FUNC_INFO, "Missing network context"); + if (!m_voiceClient) { return; } + if (to.isConnected() && this->getIContextNetwork()) { const CVoiceSetup vs = m_voiceSettings.getThreadLocal(); - m_voiceClient.updateVoiceServerUrl(vs.getAfvVoiceServerUrl()); + m_voiceClient->updateVoiceServerUrl(vs.getAfvVoiceServerUrl()); const CUser connectedUser = this->getIContextNetwork()->getConnectedServer().getUser(); + const QString inputDeviceName = m_inputDeviceSetting.get(); CAudioDeviceInfo input = CAudioDeviceInfo::getDefaultInputDevice(); if (!inputDeviceName.isEmpty()) @@ -436,13 +444,13 @@ namespace BlackCore const CAudioDeviceInfoList outputDevs = this->getAudioOutputDevices(); output = outputDevs.findByName(outputDeviceName); } - m_voiceClient.connectTo(connectedUser.getId(), connectedUser.getPassword(), connectedUser.getCallsign().asString()); - m_voiceClient.startAudio(input, output, {0, 1}); + m_voiceClient->connectTo(connectedUser.getId(), connectedUser.getPassword(), connectedUser.getCallsign().asString()); + m_voiceClient->startAudio(input, output, {0, 1}); } else if (to.isDisconnected()) { - m_voiceClient.stopAudio(); - m_voiceClient.disconnectFrom(); + m_voiceClient->stopAudio(); + m_voiceClient->disconnectFrom(); } } } // ns diff --git a/src/blackcore/context/contextaudio.h b/src/blackcore/context/contextaudio.h index 1a6c50181..22fa68765 100644 --- a/src/blackcore/context/contextaudio.h +++ b/src/blackcore/context/contextaudio.h @@ -11,7 +11,6 @@ #ifndef BLACKCORE_CONTEXT_CONTEXTAUDIO_H #define BLACKCORE_CONTEXT_CONTEXTAUDIO_H -#include "blackcore/afv/clients/afvclient.h" #include "blackcore/audio/audiosettings.h" #include "blackcore/context/context.h" #include "blackcore/actionbind.h" @@ -21,10 +20,10 @@ #include "blacksound/selcalplayer.h" #include "blacksound/notificationplayer.h" #include "blackmisc/macos/microphoneaccess.h" +#include "blackmisc/audio/settings/voicesettings.h" #include "blackmisc/audio/audiodeviceinfolist.h" #include "blackmisc/audio/notificationsounds.h" #include "blackmisc/audio/audiosettings.h" -#include "blackmisc/audio/settings/voicesettings.h" #include "blackmisc/audio/ptt.h" #include "blackmisc/aviation/callsignset.h" #include "blackmisc/aviation/comsystem.h" @@ -32,6 +31,7 @@ #include "blackmisc/network/connectionstatus.h" #include "blackmisc/network/userlist.h" #include "blackmisc/input/actionhotkeydefs.h" +#include "blackmisc/identifiable.h" #include "blackmisc/identifier.h" #include @@ -56,10 +56,13 @@ namespace BlackMisc { class CDBusServer; } namespace BlackCore { + namespace Afv { namespace Clients { class CAfvClient; }} namespace Context { //! Audio context interface - class BLACKCORE_EXPORT IContextAudio : public IContext + class BLACKCORE_EXPORT IContextAudio : + public IContext, + public BlackMisc::CIdentifiable { Q_OBJECT Q_CLASSINFO("D-Bus Interface", BLACKCORE_CONTEXTAUDIO_INTERFACENAME) @@ -92,7 +95,7 @@ namespace BlackCore // -------- parts which can run in core and GUI, referring to local voice client ------------ //! Reference to voice client - BlackCore::Afv::Clients::CAfvClient &voiceClient() { return m_voiceClient; } + BlackCore::Afv::Clients::CAfvClient *voiceClient() { return m_voiceClient.data(); } //! Audio devices //! @{ @@ -205,8 +208,8 @@ namespace BlackCore //! Get current COM unit from cockpit //! \remark cross context //! @{ - BlackMisc::Aviation::CComSystem getOwnComSystem(BlackMisc::Aviation::CComSystem::ComUnit unit) const; - bool isComIntegratedWithSimulator() const; + BlackMisc::Aviation::CComSystem xCtxGetOwnComSystem(BlackMisc::Aviation::CComSystem::ComUnit unit) const; + bool xCtxIsComIntegratedWithSimulator() const; //! @} //! Changed cockpit @@ -226,24 +229,18 @@ namespace BlackCore static constexpr int MinUnmuteVolume = 20; //!< minimum volume when unmuted // settings - BlackMisc::CSetting m_audioSettings { this, &IContextAudio::onChangedAudioSettings }; + BlackMisc::CSetting m_audioSettings { this, &IContextAudio::onChangedAudioSettings }; BlackMisc::CSetting m_voiceSettings { this, &IContextAudio::onChangedVoiceSettings }; - BlackMisc::CSetting m_inputDeviceSetting { this, &IContextAudio::changeDeviceSettings }; - BlackMisc::CSetting m_outputDeviceSetting { this, &IContextAudio::changeDeviceSettings }; + BlackMisc::CSetting m_inputDeviceSetting { this, &IContextAudio::changeDeviceSettings }; + BlackMisc::CSetting m_outputDeviceSetting { this, &IContextAudio::changeDeviceSettings }; // AFV - Afv::Clients::CAfvClient m_voiceClient; + QScopedPointer m_voiceClient; // Players BlackSound::CSelcalPlayer *m_selcalPlayer = nullptr; BlackSound::CNotificationPlayer m_notificationPlayer; - -#ifdef Q_OS_MAC - BlackMisc::CMacOSMicrophoneAccess m_micAccess; -#endif - //! Init microphone - void delayedInitMicrophone(); }; } // ns } // ns diff --git a/src/blackcore/context/contextaudioimpl.cpp b/src/blackcore/context/contextaudioimpl.cpp index c273ef651..e12937df9 100644 --- a/src/blackcore/context/contextaudioimpl.cpp +++ b/src/blackcore/context/contextaudioimpl.cpp @@ -21,8 +21,7 @@ namespace BlackCore namespace Context { CContextAudio::CContextAudio(CCoreFacadeConfig::ContextMode mode, CCoreFacade *runtime) : - IContextAudio(mode, runtime), - CIdentifiable(this) + IContextAudio(mode, runtime) { } CContextAudio *CContextAudio::registerWithDBus(CDBusServer *server) diff --git a/src/blackcore/context/contextaudioimpl.h b/src/blackcore/context/contextaudioimpl.h index 5dfe9b87e..ce689b4e2 100644 --- a/src/blackcore/context/contextaudioimpl.h +++ b/src/blackcore/context/contextaudioimpl.h @@ -15,7 +15,6 @@ #include "blackcore/corefacadeconfig.h" #include "blackcore/blackcoreexport.h" #include "blackmisc/network/userlist.h" -#include "blackmisc/identifiable.h" #include #include @@ -34,8 +33,7 @@ namespace BlackCore { //! Audio context implementation class BLACKCORE_EXPORT CContextAudio : - public IContextAudio, - public BlackMisc::CIdentifiable + public IContextAudio { Q_CLASSINFO("D-Bus Interface", BLACKCORE_CONTEXTAUDIO_INTERFACENAME) Q_OBJECT diff --git a/src/blackgui/components/afvmapdialog.cpp b/src/blackgui/components/afvmapdialog.cpp index f40814358..65255d16f 100644 --- a/src/blackgui/components/afvmapdialog.cpp +++ b/src/blackgui/components/afvmapdialog.cpp @@ -13,8 +13,8 @@ #include "afvmapdialog.h" #include "ui_afvmapdialog.h" -//#include -//#include +// #include +// #include using namespace BlackCore::Afv::Model; using namespace BlackCore::Afv::Clients; @@ -27,28 +27,31 @@ namespace BlackGui QDialog(parent), ui(new Ui::CAfvMapDialog) { + /** m_afvMapReader = new CAfvMapReader(this); m_afvMapReader->updateFromMap(); -// if (sGui && !sGui->isShuttingDown() && sGui->getIContextAudio()) -// { -// if (sGui->getIContextAudio()->isUsingImplementingObject()) -// { -// m_afvClient = &sGui->getCoreFacade()->getCContextAudio()->voiceClient(); -// } -// } + CAfvClient *afvClient = nullptr; + if (sGui && !sGui->isShuttingDown() && sGui->getIContextAudio()) + { + if (sGui->getIContextAudio()->isUsingImplementingObject()) + { + afvClient = sGui->getCoreFacade()->getCContextAudio()->voiceClient(); + } + } ui->setupUi(this); -// QQmlContext *ctxt = ui->qw_AfvMap->rootContext(); -// ctxt->setContextProperty("afvMapReader", m_afvMapReader); + QQmlContext *ctxt = ui->qw_AfvMap->rootContext(); + ctxt->setContextProperty("afvMapReader", m_afvMapReader); -// if (m_afvClient) -// { -// ctxt->setContextProperty("voiceClient", m_afvClient); -// } + if (m_afvClient) + { + ctxt->setContextProperty("voiceClient", afvClient); + } -// // ui->qw_AfvMap->engine()->setBaseUrl(":/blackgui/qml"); -// ui->qw_AfvMap->setSource(QUrl("qrc:/blackgui/qml/AFVMap.qml")); + // ui->qw_AfvMap->engine()->setBaseUrl(":/blackgui/qml"); + ui->qw_AfvMap->setSource(QUrl("qrc:/blackgui/qml/AFVMap.qml")); + **/ } CAfvMapDialog::~CAfvMapDialog() { } diff --git a/src/blackgui/components/afvmapdialog.h b/src/blackgui/components/afvmapdialog.h index 797f45448..b7834ad23 100644 --- a/src/blackgui/components/afvmapdialog.h +++ b/src/blackgui/components/afvmapdialog.h @@ -43,7 +43,7 @@ namespace BlackGui private: QScopedPointer ui; BlackCore::Afv::Model::CAfvMapReader *m_afvMapReader = nullptr; - BlackCore::Afv::Clients::CAfvClient *m_afvClient = nullptr; + BlackCore::Afv::Clients::CAfvClient *m_afvClient = nullptr; }; } // ns } // ns