From 4e44a4ed7566fbbc7248796423b822430205ad98 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Sat, 15 Jul 2023 14:01:44 +1000 Subject: [PATCH] Get transmission working --- source/client/StarClientApplication.cpp | 38 +++++++++++++++++++------ source/frontend/StarVoice.cpp | 6 ++-- source/frontend/StarVoice.hpp | 2 ++ source/game/StarWorldClient.cpp | 10 +++++-- source/game/StarWorldClient.hpp | 4 +++ 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/source/client/StarClientApplication.cpp b/source/client/StarClientApplication.cpp index 8093582..8dfd110 100644 --- a/source/client/StarClientApplication.cpp +++ b/source/client/StarClientApplication.cpp @@ -375,14 +375,6 @@ void ClientApplication::update() { else if (m_state > MainAppState::Title) updateRunning(); - { // testing - m_voice->setLocalSpeaker(0); - m_voice->setInput(m_input->bindHeld("opensb", "pushToTalk")); - DataStreamBuffer data; - if (m_voice->send(data, 5000)) - m_voice->receive(m_voice->speaker(0), std::string_view(data.ptr(), data.size())); - } - m_guiContext->cleanup(); m_edgeKeyEvents.clear(); m_input->reset(); @@ -860,13 +852,41 @@ void ClientApplication::updateRunning() { if (checkDisconnection()) return; + m_voice->setInput(m_input->bindHeld("opensb", "pushToTalk")); + DataStreamBuffer voiceData; + voiceData.setByteOrder(ByteOrder::LittleEndian); + voiceData.writeBytes(VoiceBroadcastPrefix.utf8Bytes()); + bool needstoSendVoice = m_voice->send(voiceData, 5000); + m_universeClient->update(); if (checkDisconnection()) return; - if (auto worldClient = m_universeClient->worldClient()) + if (auto worldClient = m_universeClient->worldClient()) { + auto& broadcastCallback = worldClient->broadcastCallback(); + if (!broadcastCallback) { + broadcastCallback = [&](PlayerPtr player, StringView broadcast) -> bool { + auto& view = broadcast.utf8(); + if (view.rfind(VoiceBroadcastPrefix.utf8(), 0) != NPos) { + auto entityId = player->entityId(); + auto speaker = m_voice->speaker(connectionForEntity(entityId)); + speaker->entityId = entityId; + speaker->name = player->name(); + speaker->position = player->mouthPosition(); + m_voice->receive(speaker, view.substr(VoiceBroadcastPrefix.utf8Size())); + } + return true; + }; + } + + if (worldClient->inWorld()) { + if (needstoSendVoice) + worldClient->sendSecretBroadcast(StringView(voiceData.ptr(), voiceData.size())); + m_voice->setLocalSpeaker(worldClient->connection()); + } worldClient->setInteractiveHighlightMode(isActionTaken(InterfaceAction::ShowLabels)); + } updateCamera(); diff --git a/source/frontend/StarVoice.cpp b/source/frontend/StarVoice.cpp index f2bcbe2..c424d30 100644 --- a/source/frontend/StarVoice.cpp +++ b/source/frontend/StarVoice.cpp @@ -241,6 +241,7 @@ void Voice::mix(int16_t* buffer, size_t frameCount, unsigned channels) { if (!audio->samples.empty()) { std::vector samples = move(audio->samples); audioLock.unlock(); + speaker->decibelLevel = getAudioLoudness(samples.data(), samples.size()); if (!speaker->muted) { mix = true; if (voiceBuffer.size() < samples.size()) @@ -286,8 +287,8 @@ void Voice::update(PositionalAttenuationFunction positionalAttenuationFunction) for (auto& entry : m_speakers) { if (SpeakerPtr& speaker = entry.second) { speaker->channelVolumes = { - positionalAttenuationFunction(0, speaker->position, 1.0f), - positionalAttenuationFunction(1, speaker->position, 1.0f) + 1.0f - positionalAttenuationFunction(0, speaker->position, 1.0f), + 1.0f - positionalAttenuationFunction(1, speaker->position, 1.0f) }; } } @@ -376,7 +377,6 @@ bool Voice::receive(SpeakerPtr speaker, std::string_view view) { decodedSamples *= channels; //Logger::info("Voice: decoded Opus chunk {} bytes -> {} samples", opusLength, decodedSamples); - speaker->decibelLevel = getAudioLoudness(decodeBuffer, decodedSamples); { MutexLocker lock(speaker->audioStream->mutex); auto& samples = speaker->audioStream->samples; diff --git a/source/frontend/StarVoice.hpp b/source/frontend/StarVoice.hpp index 8f39246..94ef8ac 100644 --- a/source/frontend/StarVoice.hpp +++ b/source/frontend/StarVoice.hpp @@ -18,6 +18,8 @@ typedef std::unique_ptr OpusEncoderPtr; namespace Star { +String const VoiceBroadcastPrefix = "Voice\0"s; + STAR_EXCEPTION(VoiceException, StarException); enum class VoiceInputMode : uint8_t { VoiceActivity, PushToTalk }; diff --git a/source/game/StarWorldClient.cpp b/source/game/StarWorldClient.cpp index 2b4af00..272b5df 100644 --- a/source/game/StarWorldClient.cpp +++ b/source/game/StarWorldClient.cpp @@ -1212,6 +1212,10 @@ void WorldClient::waitForLighting() { MutexLocker lock(m_lightingMutex); } +WorldClient::BroadcastCallback& WorldClient::broadcastCallback() { + return m_broadcastCallback; +} + bool WorldClient::isTileProtected(Vec2I const& pos) const { if (!inWorld()) return true; @@ -1905,8 +1909,10 @@ bool WorldClient::sendSecretBroadcast(StringView broadcast, bool raw) { } bool WorldClient::handleSecretBroadcast(PlayerPtr player, StringView broadcast) { - Logger::info("Received broadcast '{}'", broadcast); - return true; + if (m_broadcastCallback) + return m_broadcastCallback(player, broadcast); + else + return false; } diff --git a/source/game/StarWorldClient.hpp b/source/game/StarWorldClient.hpp index 0a48a31..436c2df 100644 --- a/source/game/StarWorldClient.hpp +++ b/source/game/StarWorldClient.hpp @@ -166,6 +166,8 @@ public: void waitForLighting(); + typedef std::function BroadcastCallback; + BroadcastCallback& broadcastCallback(); private: static const float DropDist; @@ -345,6 +347,8 @@ private: HashMap> m_entityInteractionResponses; List m_forceRegions; + + BroadcastCallback m_broadcastCallback; }; }