From 66b02e61c59e2db55f14a85a21098843a4489935 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sat, 21 Sep 2019 15:17:24 +0200 Subject: [PATCH] Ref T730, directly use context from sApp * using sApp->getIContextOwnAircraft() * no need to pass context, avoid issues when shutting down * minor style changes --- src/blackcore/afv/clients/afvclient.cpp | 87 ++++++++++++---------- src/blackcore/afv/clients/afvclient.h | 45 +++++------ src/blackcore/context/contextaudioimpl.cpp | 5 +- 3 files changed, 73 insertions(+), 64 deletions(-) diff --git a/src/blackcore/afv/clients/afvclient.cpp b/src/blackcore/afv/clients/afvclient.cpp index 77c7503e1..3f4d67ad2 100644 --- a/src/blackcore/afv/clients/afvclient.cpp +++ b/src/blackcore/afv/clients/afvclient.cpp @@ -7,13 +7,17 @@ */ #include "afvclient.h" +#include "blackcore/context/contextownaircraft.h" +#include "blackcore/application.h" #include "blacksound/audioutilities.h" #include -using namespace BlackMisc::PhysicalQuantities; using namespace BlackCore::Context; using namespace BlackCore::Afv::Audio; using namespace BlackCore::Afv::Connection; +using namespace BlackMisc; +using namespace BlackMisc::PhysicalQuantities; +using namespace BlackMisc::Simulation; using namespace BlackSound::SampleProvider; namespace BlackCore @@ -28,7 +32,7 @@ namespace BlackCore m_connection = new CClientConnection(apiServer, this); m_connection->setReceiveAudio(false); - m_input = new CInput(c_sampleRate, this); + m_input = new CInput(SampleRate, this); connect(m_input, &CInput::opusDataAvailable, this, &CAfvClient::opusDataAvailable); connect(m_input, &CInput::inputVolumeStream, this, &CAfvClient::inputVolumeStream); @@ -45,34 +49,26 @@ namespace BlackCore { 1, 122800000, 48.5, 11.5, 1000.0, 1000.0 } }; - m_enabledTransceivers = - { - { 0 }, - { 1 } - }; - - m_transmittingTransceivers = - { - { 0 } - }; + m_enabledTransceivers = { 0, 1 }; + m_transmittingTransceivers = { { 0 } }; // TxTransceiverDto qDebug() << "UserClient instantiated"; } - void CAfvClient::setContextOwnAircraft(const IContextOwnAircraft *contextOwnAircraft) + void CAfvClient::initWithContext() { - m_contextOwnAircraft = contextOwnAircraft; - if (m_contextOwnAircraft) - { - connect(m_contextOwnAircraft, &IContextOwnAircraft::changedAircraftCockpit, this, &CAfvClient::updateTransceiversFromContext); - } + if (!hasContext()) { return; } + this->disconnect(sApp->getIContextOwnAircraft()); + sApp->getIContextOwnAircraft()->disconnect(this); + connect(sApp->getIContextOwnAircraft(), &IContextOwnAircraft::changedAircraftCockpit, this, &CAfvClient::updateTransceiversFromContext); } void CAfvClient::connectTo(const QString &cid, const QString &password, const QString &callsign) { + this->initWithContext(); m_callsign = callsign; m_connection->connectTo(cid, password, callsign); - updateTransceivers(); + this->updateTransceivers(); if (m_connection->isConnected()) { emit connectionStatusChanged(Connected); } else { emit connectionStatusChanged(Disconnected); } @@ -126,7 +122,7 @@ namespace BlackCore return; } - soundcardSampleProvider = new CSoundcardSampleProvider(c_sampleRate, transceiverIDs, this); + soundcardSampleProvider = new CSoundcardSampleProvider(SampleRate, transceiverIDs, this); connect(soundcardSampleProvider, &CSoundcardSampleProvider::receivingCallsignsChanged, this, &CAfvClient::receivingCallsignsChanged); outputSampleProvider = new CVolumeSampleProvider(soundcardSampleProvider, this); outputSampleProvider->setVolume(m_outputVolume); @@ -145,7 +141,7 @@ namespace BlackCore { if (m_isStarted) { return; } - soundcardSampleProvider = new CSoundcardSampleProvider(c_sampleRate, { 0, 1 }, this); + soundcardSampleProvider = new CSoundcardSampleProvider(SampleRate, { 0, 1 }, this); connect(soundcardSampleProvider, &CSoundcardSampleProvider::receivingCallsignsChanged, this, &CAfvClient::receivingCallsignsChanged); outputSampleProvider = new CVolumeSampleProvider(soundcardSampleProvider, this); outputSampleProvider->setVolume(m_outputVolume); @@ -205,46 +201,48 @@ namespace BlackCore updateTransceivers(); } - void CAfvClient::updateComFrequency(quint16 id, quint32 frequency) + void CAfvClient::updateComFrequency(quint16 id, quint32 frequencyHz) { if (id != 0 && id != 1) { return; } // Fix rounding issues like 128074999 Hz -> 128075000 Hz - quint32 roundedFrequency = qRound(frequency / 1000.0) * 1000; + quint32 roundedFrequencyHz = static_cast(qRound(frequencyHz / 1000.0)) * 1000; if (m_transceivers.size() >= id + 1) { - if (m_transceivers[id].frequency != roundedFrequency) + if (m_transceivers[id].frequency != roundedFrequencyHz) { - m_transceivers[id].frequency = roundedFrequency; + m_transceivers[id].frequency = roundedFrequencyHz; updateTransceivers(); } } } - void CAfvClient::updatePosition(double latitude, double longitude, double height) + void CAfvClient::updatePosition(double latitudeDeg, double longitudeDeg, double heightMeters) { for (TransceiverDto &transceiver : m_transceivers) { - transceiver.LatDeg = latitude; - transceiver.LonDeg = longitude; - transceiver.HeightAglM = height; - transceiver.HeightMslM = height; + transceiver.LatDeg = latitudeDeg; + transceiver.LonDeg = longitudeDeg; + transceiver.HeightAglM = heightMeters; + transceiver.HeightMslM = heightMeters; } } void CAfvClient::updateTransceivers() { - if (! m_connection->isConnected()) { return; } - - if (m_contextOwnAircraft) + if (!m_connection->isConnected()) { return; } + if (hasContext()) { - BlackMisc::Simulation::CSimulatedAircraft ownAircraft = m_contextOwnAircraft->getOwnAircraft(); + const CSimulatedAircraft ownAircraft = sApp->getIContextOwnAircraft()->getOwnAircraft(); updatePosition(ownAircraft.latitude().value(CAngleUnit::deg()), ownAircraft.longitude().value(CAngleUnit::deg()), ownAircraft.getAltitude().value(CLengthUnit::ft())); - updateComFrequency(0, ownAircraft.getCom1System().getFrequencyActive().value(CFrequencyUnit::Hz())); - updateComFrequency(1, ownAircraft.getCom2System().getFrequencyActive().value(CFrequencyUnit::Hz())); + + const quint16 com1Hz = static_cast(ownAircraft.getCom1System().getFrequencyActive().valueInteger(CFrequencyUnit::Hz())); + const quint16 com2Hz = static_cast(ownAircraft.getCom2System().getFrequencyActive().valueInteger(CFrequencyUnit::Hz())); + updateComFrequency(0, com1Hz); + updateComFrequency(1, com2Hz); } QVector enabledTransceivers; @@ -401,17 +399,26 @@ namespace BlackCore return {}; } - void CAfvClient::updateTransceiversFromContext(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, const BlackMisc::CIdentifier &originator) + void CAfvClient::updateTransceiversFromContext(const CSimulatedAircraft &aircraft, const CIdentifier &originator) { - Q_UNUSED(originator); + Q_UNUSED(originator) updatePosition(aircraft.latitude().value(CAngleUnit::deg()), aircraft.longitude().value(CAngleUnit::deg()), aircraft.getAltitude().value(CLengthUnit::ft())); - updateComFrequency(0, aircraft.getCom1System().getFrequencyActive().value(CFrequencyUnit::Hz())); - updateComFrequency(1, aircraft.getCom2System().getFrequencyActive().value(CFrequencyUnit::Hz())); + + const quint16 com1Hz = static_cast(aircraft.getCom1System().getFrequencyActive().valueInteger(CFrequencyUnit::Hz())); + const quint16 com2Hz = static_cast(aircraft.getCom2System().getFrequencyActive().valueInteger(CFrequencyUnit::Hz())); + + updateComFrequency(0, com1Hz); + updateComFrequency(1, com2Hz); updateTransceivers(); } + bool CAfvClient::hasContext() + { + return sApp && !sApp->isShuttingDown() && sApp->getIContextOwnAircraft(); + } + double CAfvClient::getOutputVolumeDb() const { return m_outputVolume; diff --git a/src/blackcore/afv/clients/afvclient.h b/src/blackcore/afv/clients/afvclient.h index 27c26f4d1..fed5915f0 100644 --- a/src/blackcore/afv/clients/afvclient.h +++ b/src/blackcore/afv/clients/afvclient.h @@ -39,8 +39,8 @@ namespace BlackCore class BLACKCORE_EXPORT CAfvClient final : public QObject { Q_OBJECT - Q_PROPERTY(float inputVolumePeakVU READ getInputVolumePeakVU NOTIFY inputVolumePeakVU) - Q_PROPERTY(float outputVolumePeakVU READ getOutputVolumePeakVU NOTIFY outputVolumePeakVU) + Q_PROPERTY(double inputVolumePeakVU READ getInputVolumePeakVU NOTIFY inputVolumePeakVU) + Q_PROPERTY(double outputVolumePeakVU READ getOutputVolumePeakVU NOTIFY outputVolumePeakVU) Q_PROPERTY(ConnectionStatus connectionStatus READ getConnectionStatus NOTIFY connectionStatusChanged) Q_PROPERTY(QString receivingCallsignsCom1 READ getReceivingCallsignsCom1 NOTIFY receivingCallsignsChanged) Q_PROPERTY(QString receivingCallsignsCom2 READ getReceivingCallsignsCom2 NOTIFY receivingCallsignsChanged) @@ -55,14 +55,15 @@ namespace BlackCore //! Dtor virtual ~CAfvClient() override { - stop(); + this->stop(); } - void setContextOwnAircraft(const BlackCore::Context::IContextOwnAircraft *contextOwnAircraft); + // void setContextOwnAircraft(const BlackCore::Context::IContextOwnAircraft *contextOwnAircraft); QString callsign() const { return m_callsign; } bool isConnected() const { return m_connection->isConnected(); } + ConnectionStatus getConnectionStatus() const; Q_INVOKABLE void connectTo(const QString &cid, const QString &password, const QString &callsign); Q_INVOKABLE void disconnectFrom(); @@ -80,8 +81,8 @@ namespace BlackCore void stop(); Q_INVOKABLE void enableTransceiver(quint16 id, bool enable); - Q_INVOKABLE void updateComFrequency(quint16 id, quint32 frequency); - Q_INVOKABLE void updatePosition(double latitude, double longitude, double height); + Q_INVOKABLE void updateComFrequency(quint16 id, quint32 frequencyHz); + Q_INVOKABLE void updatePosition(double latitudeDeg, double longitudeDeg, double heightMeters); void setTransmittingTransceivers(quint16 transceiverID); void setTransmittingTransceivers(const QVector &transceivers); @@ -90,25 +91,25 @@ namespace BlackCore Q_INVOKABLE void setLoopBack(bool on) { m_loopbackOn = on; } - double inputVolumeDb() const - { - return m_inputVolumeDb; - } - + //! Input volume in DB, +-18DB @{ + double getInputVolumeDb() const { return m_inputVolumeDb; } Q_INVOKABLE void setInputVolumeDb(double value); + //! @} + //! Output volume in DB, +-18DB @{ double getOutputVolumeDb() const; Q_INVOKABLE void setOutputVolumeDb(double outputVolume); + //! @} - float getInputVolumePeakVU() const { return m_inputVolumeStream.PeakVU; } - float getOutputVolumePeakVU() const { return m_outputVolumeStream.PeakVU; } - - ConnectionStatus getConnectionStatus() const; + //! VU values, 0..1 @{ + double getInputVolumePeakVU() const { return m_inputVolumeStream.PeakVU; } + double getOutputVolumePeakVU() const { return m_outputVolumeStream.PeakVU; } + //! @} signals: void receivingCallsignsChanged(const Audio::TransceiverReceivingCallsignsChangedArgs &args); - void inputVolumePeakVU(float value); - void outputVolumePeakVU(float value); + void inputVolumePeakVU(double value); + void outputVolumePeakVU(double value); void connectionStatusChanged(ConnectionStatus status); private: @@ -124,8 +125,8 @@ namespace BlackCore void updateTransceivers(); void updateTransceiversFromContext(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, const BlackMisc::CIdentifier &originator); - static constexpr int c_sampleRate = 48000; - static constexpr int frameSize = 960; // 20ms + static constexpr int SampleRate = 48000; + static constexpr int FrameSize = 960; // 20ms // Connection Connection::CClientConnection *m_connection = nullptr; @@ -133,7 +134,7 @@ namespace BlackCore // Properties QString m_callsign; - Audio::CInput *m_input = nullptr; + Audio::CInput *m_input = nullptr; Audio::Output *m_output = nullptr; Audio::CSoundcardSampleProvider *soundcardSampleProvider = nullptr; @@ -160,7 +161,9 @@ namespace BlackCore Audio::InputVolumeStreamArgs m_inputVolumeStream; Audio::OutputVolumeStreamArgs m_outputVolumeStream; - BlackCore::Context::IContextOwnAircraft const *m_contextOwnAircraft = nullptr; + void initWithContext(); + static bool hasContext(); + }; } // ns } // ns diff --git a/src/blackcore/context/contextaudioimpl.cpp b/src/blackcore/context/contextaudioimpl.cpp index e511bb969..55c2c9602 100644 --- a/src/blackcore/context/contextaudioimpl.cpp +++ b/src/blackcore/context/contextaudioimpl.cpp @@ -778,12 +778,11 @@ namespace BlackCore void CContextAudio::xCtxNetworkConnectionStatusChanged(const CConnectionStatus &from, const CConnectionStatus &to) { - Q_UNUSED(from); + Q_UNUSED(from) BLACK_VERIFY_X(this->getIContextNetwork(), Q_FUNC_INFO, "Missing network context"); if (to.isConnected() && this->getIContextNetwork()) { - CUser connectedUser = this->getIContextNetwork()->getConnectedServer().getUser(); - m_voiceClient.setContextOwnAircraft(getIContextOwnAircraft()); + const CUser connectedUser = this->getIContextNetwork()->getConnectedServer().getUser(); m_voiceClient.connectTo(connectedUser.getId(), connectedUser.getPassword(), connectedUser.getCallsign().asString()); m_voiceClient.start(QAudioDeviceInfo::defaultInputDevice(), QAudioDeviceInfo::defaultOutputDevice(), {0, 1}); }