Get transmission working

This commit is contained in:
Kae 2023-07-15 14:01:44 +10:00
parent 91cd6182d8
commit 4e44a4ed75
5 changed files with 46 additions and 14 deletions

View File

@ -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();

View File

@ -241,6 +241,7 @@ void Voice::mix(int16_t* buffer, size_t frameCount, unsigned channels) {
if (!audio->samples.empty()) {
std::vector<int16_t> 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;

View File

@ -18,6 +18,8 @@ typedef std::unique_ptr<OpusEncoder, void(*)(OpusEncoder*)> OpusEncoderPtr;
namespace Star {
String const VoiceBroadcastPrefix = "Voice\0"s;
STAR_EXCEPTION(VoiceException, StarException);
enum class VoiceInputMode : uint8_t { VoiceActivity, PushToTalk };

View File

@ -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;
}

View File

@ -166,6 +166,8 @@ public:
void waitForLighting();
typedef std::function<bool(PlayerPtr, StringView)> BroadcastCallback;
BroadcastCallback& broadcastCallback();
private:
static const float DropDist;
@ -345,6 +347,8 @@ private:
HashMap<Uuid, RpcPromiseKeeper<InteractAction>> m_entityInteractionResponses;
List<PhysicsForceRegion> m_forceRegions;
BroadcastCallback m_broadcastCallback;
};
}