1 parent
09a3a4fee3
commit
7b8dcd9f31
7 files changed
+133
-7
No files matched your search
@@ -30,10 +30,11 @@ auto connected = client.connect({
|
||||
});
|
||||
```
|
||||
|
||||
Each `Client` owns one connection, packet sequence, and transmitted audio
|
||||
timeline. To send different audio to different audiences simultaneously,
|
||||
create one `Client` per timeline and give each client independent
|
||||
`WhisperTargets`.
|
||||
Each `Client` owns one connection and encrypted packet sequence. For different
|
||||
audio programs on that same visible identity, create logical `VoiceStreamId`
|
||||
handles and call `sendWhisper(stream, frame, targets)`. Each handle keeps an
|
||||
independent inner voice sequence while the connection retains its one global
|
||||
outer packet sequence.
|
||||
|
||||
## Build
|
||||
|
||||
|
||||
@@ -128,6 +128,14 @@ namespace Void::TeamSpeak
|
||||
[[nodiscard]] Void::Result::Result<void, Error>
|
||||
sendWhisper(std::span<const std::uint8_t> opusFrame,
|
||||
const WhisperTargets& targets);
|
||||
[[nodiscard]] Void::Result::Result<VoiceStreamId, Error>
|
||||
createVoiceStream();
|
||||
[[nodiscard]] Void::Result::Result<void, Error>
|
||||
destroyVoiceStream(VoiceStreamId stream);
|
||||
[[nodiscard]] Void::Result::Result<void, Error>
|
||||
sendWhisper(VoiceStreamId stream,
|
||||
std::span<const std::uint8_t> opusFrame,
|
||||
const WhisperTargets& targets);
|
||||
void stopVoice() noexcept;
|
||||
|
||||
private:
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
@@ -107,6 +108,12 @@ namespace Void::TeamSpeak::Protocol
|
||||
void sendWhisper(std::span<const uint8_t> opus,
|
||||
const std::vector<uint64_t>& channelIds,
|
||||
const std::vector<uint16_t>& clientIds);
|
||||
[[nodiscard]] uint32_t createVoiceStream();
|
||||
[[nodiscard]] bool destroyVoiceStream(uint32_t streamId);
|
||||
[[nodiscard]] bool sendWhisper(
|
||||
uint32_t streamId, std::span<const uint8_t> opus,
|
||||
const std::vector<uint64_t>& channelIds,
|
||||
const std::vector<uint16_t>& clientIds);
|
||||
// End-of-voice marker (empty frame).
|
||||
void sendVoiceStop();
|
||||
|
||||
@@ -134,7 +141,8 @@ namespace Void::TeamSpeak::Protocol
|
||||
void loopThread();
|
||||
void handleIncomingPacket(std::span<const uint8_t> raw);
|
||||
void sendOutgoing(std::span<const uint8_t> data, PacketType type,
|
||||
uint8_t extraFlags = 0);
|
||||
uint8_t extraFlags = 0,
|
||||
std::optional<uint16_t> voiceId = std::nullopt);
|
||||
std::vector<uint8_t> buildRawPacket(std::span<const uint8_t> data,
|
||||
PacketType type,
|
||||
uint16_t packetId,
|
||||
@@ -192,6 +200,8 @@ namespace Void::TeamSpeak::Protocol
|
||||
|
||||
std::array<uint16_t, 9> packetCounter_{};
|
||||
std::array<uint32_t, 9> generationCounter_{};
|
||||
uint32_t nextVoiceStreamId_{1};
|
||||
std::map<uint32_t, uint16_t> voiceStreamCounters_;
|
||||
|
||||
bool cryptoInitComplete_ = false;
|
||||
std::vector<uint8_t> ivStruct_;
|
||||
|
||||
@@ -16,6 +16,7 @@ namespace Void::TeamSpeak
|
||||
};
|
||||
|
||||
using ClientId = Id<struct ClientIdTag, std::uint16_t>;
|
||||
using VoiceStreamId = Id<struct VoiceStreamIdTag, std::uint32_t>;
|
||||
using ClientDatabaseId = Id<struct ClientDatabaseIdTag, std::uint64_t>;
|
||||
using ChannelId = Id<struct ChannelIdTag, std::uint64_t>;
|
||||
using ServerConnectionId = Id<struct ServerConnectionIdTag, std::uint64_t>;
|
||||
|
||||
@@ -490,5 +490,41 @@ Client::removeFromServerGroup(ServerGroupId group, ClientDatabaseId client)
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<VoiceStreamId, Error> Client::createVoiceStream()
|
||||
{
|
||||
return VoiceStreamId{impl_->client.createVoiceStream()};
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Error>
|
||||
Client::destroyVoiceStream(VoiceStreamId stream)
|
||||
{
|
||||
if (!stream) return Void::Result::fail(
|
||||
Error{Error::Code::InvalidArgument, "invalid voice stream"});
|
||||
if (!impl_->client.destroyVoiceStream(stream.value)) {
|
||||
return Void::Result::fail(
|
||||
Error{Error::Code::InvalidArgument, "voice stream not found"});
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Error> Client::sendWhisper(
|
||||
VoiceStreamId stream, std::span<const std::uint8_t> frame,
|
||||
const WhisperTargets& targets)
|
||||
{
|
||||
if (state() != ConnectionState::Connected) return Void::Result::fail(
|
||||
Error{Error::Code::NotConnected, "not connected"});
|
||||
std::vector<std::uint64_t> channels;
|
||||
std::vector<std::uint16_t> clients;
|
||||
channels.reserve(targets.channels.size());
|
||||
clients.reserve(targets.clients.size());
|
||||
for (auto value : targets.channels) channels.push_back(value.value);
|
||||
for (auto value : targets.clients) clients.push_back(value.value);
|
||||
if (!impl_->client.sendWhisper(stream.value, frame, channels, clients)) {
|
||||
return Void::Result::fail(
|
||||
Error{Error::Code::InvalidArgument, "voice stream not found"});
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void Client::stopVoice() noexcept { impl_->client.sendVoiceStop(); }
|
||||
}
|
||||
+53
-2
@@ -270,7 +270,8 @@ namespace Void::TeamSpeak::Protocol
|
||||
}
|
||||
|
||||
void TsClient::sendOutgoing(std::span<const uint8_t> dataIn, PacketType type,
|
||||
uint8_t extraFlags)
|
||||
uint8_t extraFlags,
|
||||
std::optional<uint16_t> voiceId)
|
||||
{
|
||||
auto ctr = getCounter(type);
|
||||
incCounter(type);
|
||||
@@ -281,7 +282,7 @@ namespace Void::TeamSpeak::Protocol
|
||||
case PacketType::Voice:
|
||||
case PacketType::VoiceWhisper:
|
||||
if (data.size() >= 2) {
|
||||
putU16BE(data, 0, ctr.id);
|
||||
putU16BE(data, 0, voiceId.value_or(ctr.id));
|
||||
}
|
||||
break;
|
||||
case PacketType::Command:
|
||||
@@ -646,6 +647,56 @@ namespace Void::TeamSpeak::Protocol
|
||||
sendOutgoing(buf, PacketType::VoiceWhisper);
|
||||
}
|
||||
|
||||
uint32_t TsClient::createVoiceStream()
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mu_);
|
||||
while (nextVoiceStreamId_ == 0 ||
|
||||
voiceStreamCounters_.contains(nextVoiceStreamId_)) {
|
||||
++nextVoiceStreamId_;
|
||||
}
|
||||
const auto id = nextVoiceStreamId_++;
|
||||
voiceStreamCounters_.emplace(id, 0);
|
||||
return id;
|
||||
}
|
||||
|
||||
bool TsClient::destroyVoiceStream(uint32_t streamId)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mu_);
|
||||
return voiceStreamCounters_.erase(streamId) != 0;
|
||||
}
|
||||
|
||||
bool TsClient::sendWhisper(uint32_t streamId, std::span<const uint8_t> opus,
|
||||
const std::vector<uint64_t>& channelIds,
|
||||
const std::vector<uint16_t>& clientIds)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mu_);
|
||||
if (state_.load() != ClientState::Connected) return false;
|
||||
auto stream = voiceStreamCounters_.find(streamId);
|
||||
if (stream == voiceStreamCounters_.end()) return false;
|
||||
if (channelIds.empty() && clientIds.empty()) return true;
|
||||
|
||||
std::vector<uint8_t> buf;
|
||||
buf.resize(5 + channelIds.size() * 8 + clientIds.size() * 2 +
|
||||
opus.size());
|
||||
size_t p = 2;
|
||||
buf[p++] = 5;
|
||||
buf[p++] = static_cast<uint8_t>(channelIds.size());
|
||||
buf[p++] = static_cast<uint8_t>(clientIds.size());
|
||||
for (uint64_t cid : channelIds) {
|
||||
for (int shift = 56; shift >= 0; shift -= 8) {
|
||||
buf[p++] = static_cast<uint8_t>((cid >> shift) & 0xff);
|
||||
}
|
||||
}
|
||||
for (uint16_t clientId : clientIds) {
|
||||
buf[p++] = static_cast<uint8_t>((clientId >> 8) & 0xff);
|
||||
buf[p++] = static_cast<uint8_t>(clientId & 0xff);
|
||||
}
|
||||
std::memcpy(buf.data() + p, opus.data(), opus.size());
|
||||
const auto voiceId = stream->second++;
|
||||
sendOutgoing(buf, PacketType::VoiceWhisper, 0, voiceId);
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---- Incoming ----------------------------------------------------------
|
||||
|
||||
std::vector<uint8_t> TsClient::decryptPacket(std::span<const uint8_t> raw,
|
||||
|
||||
@@ -48,3 +48,22 @@ TEST_CASE(whisper_targets_report_empty_state)
|
||||
targets.clients.push_back(ClientId{7});
|
||||
CHECK(!targets.empty());
|
||||
}
|
||||
|
||||
TEST_CASE(logical_voice_streams_have_strong_independent_handles)
|
||||
{
|
||||
Client client;
|
||||
auto first = client.createVoiceStream();
|
||||
auto second = client.createVoiceStream();
|
||||
CHECK(first.has_value());
|
||||
CHECK(second.has_value());
|
||||
CHECK(first->value != second->value);
|
||||
WhisperTargets targets;
|
||||
targets.clients.push_back(ClientId{7});
|
||||
const std::uint8_t frame[] = {1, 2, 3};
|
||||
auto sent = client.sendWhisper(*first, frame, targets);
|
||||
CHECK(!sent.has_value());
|
||||
CHECK(sent.error().code == Error::Code::NotConnected);
|
||||
CHECK(client.destroyVoiceStream(*first).has_value());
|
||||
CHECK(!client.destroyVoiceStream(*first).has_value());
|
||||
CHECK(client.destroyVoiceStream(*second).has_value());
|
||||
}
|
||||
Reference in new issue
Block a user