diff --git a/README.md b/README.md index 4ba9eab..6556ee3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/include/Void/TeamSpeak/Client.hpp b/include/Void/TeamSpeak/Client.hpp index 27093f7..0ac1f93 100644 --- a/include/Void/TeamSpeak/Client.hpp +++ b/include/Void/TeamSpeak/Client.hpp @@ -128,6 +128,14 @@ namespace Void::TeamSpeak [[nodiscard]] Void::Result::Result sendWhisper(std::span opusFrame, const WhisperTargets& targets); + [[nodiscard]] Void::Result::Result + createVoiceStream(); + [[nodiscard]] Void::Result::Result + destroyVoiceStream(VoiceStreamId stream); + [[nodiscard]] Void::Result::Result + sendWhisper(VoiceStreamId stream, + std::span opusFrame, + const WhisperTargets& targets); void stopVoice() noexcept; private: diff --git a/include/Void/TeamSpeak/Protocol/TsClient.hpp b/include/Void/TeamSpeak/Protocol/TsClient.hpp index c7aa308..bc656e2 100644 --- a/include/Void/TeamSpeak/Protocol/TsClient.hpp +++ b/include/Void/TeamSpeak/Protocol/TsClient.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -107,6 +108,12 @@ namespace Void::TeamSpeak::Protocol void sendWhisper(std::span opus, const std::vector& channelIds, const std::vector& clientIds); + [[nodiscard]] uint32_t createVoiceStream(); + [[nodiscard]] bool destroyVoiceStream(uint32_t streamId); + [[nodiscard]] bool sendWhisper( + uint32_t streamId, std::span opus, + const std::vector& channelIds, + const std::vector& clientIds); // End-of-voice marker (empty frame). void sendVoiceStop(); @@ -134,7 +141,8 @@ namespace Void::TeamSpeak::Protocol void loopThread(); void handleIncomingPacket(std::span raw); void sendOutgoing(std::span data, PacketType type, - uint8_t extraFlags = 0); + uint8_t extraFlags = 0, + std::optional voiceId = std::nullopt); std::vector buildRawPacket(std::span data, PacketType type, uint16_t packetId, @@ -192,6 +200,8 @@ namespace Void::TeamSpeak::Protocol std::array packetCounter_{}; std::array generationCounter_{}; + uint32_t nextVoiceStreamId_{1}; + std::map voiceStreamCounters_; bool cryptoInitComplete_ = false; std::vector ivStruct_; diff --git a/include/Void/TeamSpeak/Types.hpp b/include/Void/TeamSpeak/Types.hpp index bea4320..a6b319c 100644 --- a/include/Void/TeamSpeak/Types.hpp +++ b/include/Void/TeamSpeak/Types.hpp @@ -16,6 +16,7 @@ namespace Void::TeamSpeak }; using ClientId = Id; + using VoiceStreamId = Id; using ClientDatabaseId = Id; using ChannelId = Id; using ServerConnectionId = Id; diff --git a/src/Client.cpp b/src/Client.cpp index 0fe9ed2..58a790a 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -490,5 +490,41 @@ Client::removeFromServerGroup(ServerGroupId group, ClientDatabaseId client) return {}; } + Void::Result::Result Client::createVoiceStream() + { + return VoiceStreamId{impl_->client.createVoiceStream()}; + } + + Void::Result::Result + 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 Client::sendWhisper( + VoiceStreamId stream, std::span frame, + const WhisperTargets& targets) + { + if (state() != ConnectionState::Connected) return Void::Result::fail( + Error{Error::Code::NotConnected, "not connected"}); + std::vector channels; + std::vector 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(); } } diff --git a/src/TsClient.cpp b/src/TsClient.cpp index 4334ccd..df53d4f 100644 --- a/src/TsClient.cpp +++ b/src/TsClient.cpp @@ -270,7 +270,8 @@ namespace Void::TeamSpeak::Protocol } void TsClient::sendOutgoing(std::span dataIn, PacketType type, - uint8_t extraFlags) + uint8_t extraFlags, + std::optional 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 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 lock(mu_); + return voiceStreamCounters_.erase(streamId) != 0; + } + + bool TsClient::sendWhisper(uint32_t streamId, std::span opus, + const std::vector& channelIds, + const std::vector& clientIds) + { + std::lock_guard 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 buf; + buf.resize(5 + channelIds.size() * 8 + clientIds.size() * 2 + + opus.size()); + size_t p = 2; + buf[p++] = 5; + buf[p++] = static_cast(channelIds.size()); + buf[p++] = static_cast(clientIds.size()); + for (uint64_t cid : channelIds) { + for (int shift = 56; shift >= 0; shift -= 8) { + buf[p++] = static_cast((cid >> shift) & 0xff); + } + } + for (uint16_t clientId : clientIds) { + buf[p++] = static_cast((clientId >> 8) & 0xff); + buf[p++] = static_cast(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 TsClient::decryptPacket(std::span raw, diff --git a/tests/test_modern_api.cpp b/tests/test_modern_api.cpp index bdb4fd2..db1b037 100644 --- a/tests/test_modern_api.cpp +++ b/tests/test_modern_api.cpp @@ -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()); +}