From 32c32a0f65bd8bf35241f25c1765d4dc02540484 Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 13 Nov 2026 21:28:54 -0800 Subject: [PATCH 1/2] Send the track sid with FrameCryptor's setKeyIndex() and setEnabled() The FFI requires the track sid to find a cryptor, and panics (aborting the process) when it's missing. FrameCryptor now keeps the sid that frameCryptors() receives, sends it, and exposes it as trackSid(). Fixes #311 Co-Authored-By: Claude Opus 5.5 --- include/livekit/e2ee.h | 8 ++++++-- src/e2ee.cpp | 10 ++++++--- src/tests/integration/test_e2ee.cpp | 32 +++++++++++++++++++++++++++++ src/tests/unit/test_e2ee.cpp | 3 ++- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/include/livekit/e2ee.h b/include/livekit/e2ee.h index f0c50c34..cddfbb2b 100644 --- a/include/livekit/e2ee.h +++ b/include/livekit/e2ee.h @@ -160,10 +160,11 @@ class LIVEKIT_API E2EEManager { KeyProviderOptions options_; }; - /// @brief Frame-level cryptor controls for one participant. + /// @brief Frame-level cryptor controls for one participant's track. class LIVEKIT_API FrameCryptor { public: - FrameCryptor(std::uint64_t room_handle, std::string participant_identity, int key_index, bool enabled); + FrameCryptor(std::uint64_t room_handle, std::string participant_identity, std::string track_sid, int key_index, + bool enabled); ~FrameCryptor() = default; FrameCryptor(const FrameCryptor&) = delete; FrameCryptor& operator=(const FrameCryptor&) = delete; @@ -171,6 +172,8 @@ class LIVEKIT_API E2EEManager { FrameCryptor& operator=(FrameCryptor&&) noexcept = default; const std::string& participantIdentity() const; + /// The sid of the track this cryptor encrypts or decrypts. + const std::string& trackSid() const; int keyIndex() const; bool enabled() const; @@ -184,6 +187,7 @@ class LIVEKIT_API E2EEManager { std::uint64_t room_handle_{0}; bool enabled_{false}; std::string participant_identity_; + std::string track_sid_; int key_index_{0}; }; diff --git a/src/e2ee.cpp b/src/e2ee.cpp index 403582df..b88bf87d 100644 --- a/src/e2ee.cpp +++ b/src/e2ee.cpp @@ -102,14 +102,16 @@ std::vector E2EEManager::KeyProvider::ratchetKey(const std::string // FrameCryptor // ============================================================================ -E2EEManager::FrameCryptor::FrameCryptor(std::uint64_t room_handle, std::string participant_identity, int key_index, - bool enabled) +E2EEManager::FrameCryptor::FrameCryptor(std::uint64_t room_handle, std::string participant_identity, + std::string track_sid, int key_index, bool enabled) : room_handle_(room_handle), enabled_(enabled), participant_identity_(std::move(participant_identity)), + track_sid_(std::move(track_sid)), key_index_(key_index) {} const std::string& E2EEManager::FrameCryptor::participantIdentity() const { return participant_identity_; } +const std::string& E2EEManager::FrameCryptor::trackSid() const { return track_sid_; } int E2EEManager::FrameCryptor::keyIndex() const { return key_index_; } bool E2EEManager::FrameCryptor::enabled() const { return enabled_; } @@ -117,6 +119,7 @@ void E2EEManager::FrameCryptor::setEnabled(bool enabled) { proto::FfiRequest req; req.mutable_e2ee()->set_room_handle(room_handle_); req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_participant_identity(participant_identity_); + req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_track_sid(track_sid_); req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_enabled(enabled); FfiClient::instance().sendRequest(req); } @@ -125,6 +128,7 @@ void E2EEManager::FrameCryptor::setKeyIndex(int key_index) { proto::FfiRequest req; req.mutable_e2ee()->set_room_handle(room_handle_); req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_participant_identity(participant_identity_); + req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_track_sid(track_sid_); req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_key_index(key_index); FfiClient::instance().sendRequest(req); } @@ -161,7 +165,7 @@ std::vector E2EEManager::frameCryptors() const { const auto& list = resp.e2ee().manager_get_frame_cryptors().frame_cryptors(); out.reserve(static_cast(list.size())); for (const auto& fc : list) { - out.emplace_back(room_handle_, fc.participant_identity(), fc.key_index(), fc.enabled()); + out.emplace_back(room_handle_, fc.participant_identity(), fc.track_sid(), fc.key_index(), fc.enabled()); } return out; } diff --git a/src/tests/integration/test_e2ee.cpp b/src/tests/integration/test_e2ee.cpp index 68880c59..19f96f69 100644 --- a/src/tests/integration/test_e2ee.cpp +++ b/src/tests/integration/test_e2ee.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "../common/audio_utils.h" @@ -53,4 +54,35 @@ TEST_F(E2EEIntegrationTest, FrameCryptorsIncludePublishedTrack) { participant->unpublishTrack(track->publication()->sid()); } +TEST_F(E2EEIntegrationTest, FrameCryptorSettersApplyToItsTrack) { + failIfNotConfigured(); + + Room room; + RoomOptions room_options; + room_options.encryption = E2EEOptions{}; + room_options.encryption->key_provider_options.shared_key = std::vector{'k', 'e', 'y'}; + ASSERT_TRUE(room.connect(config_.url, config_.token_a, room_options)); + auto participant = lockLocalParticipant(room); + auto source = std::make_shared(kDefaultAudioSampleRate, kDefaultAudioChannels, 0); + auto track = participant->publishAudioTrack("e2ee-key-index", source, TrackSource::SOURCE_MICROPHONE); + const std::string track_sid = track->publication()->sid(); + auto manager = room.e2eeManager().lock(); + ASSERT_NE(manager, nullptr); + manager->keyProvider().lock()->setSharedKey(std::vector{'n', 'e', 'w'}, 1); + + for (auto& cryptor : manager->frameCryptors()) { + cryptor.setKeyIndex(1); + cryptor.setEnabled(false); + } + + std::vector> states; + for (const auto& cryptor : manager->frameCryptors()) { + states.emplace_back(cryptor.participantIdentity(), cryptor.trackSid(), cryptor.keyIndex(), cryptor.enabled()); + } + EXPECT_EQ( + states, + (std::vector>{{participant->identity(), track_sid, 1, false}})); + participant->unpublishTrack(track_sid); +} + } // namespace livekit::test diff --git a/src/tests/unit/test_e2ee.cpp b/src/tests/unit/test_e2ee.cpp index 678c8f62..82859b96 100644 --- a/src/tests/unit/test_e2ee.cpp +++ b/src/tests/unit/test_e2ee.cpp @@ -35,9 +35,10 @@ TEST(E2EETest, E2EEOptionsDefaults) { } TEST(E2EETest, FrameCryptorAccessors) { - E2EEManager::FrameCryptor cryptor(/*room_handle=*/0, "alice", /*key_index=*/3, + E2EEManager::FrameCryptor cryptor(/*room_handle=*/0, "alice", "TR_alice", /*key_index=*/3, /*enabled=*/true); EXPECT_EQ(cryptor.participantIdentity(), "alice"); + EXPECT_EQ(cryptor.trackSid(), "TR_alice"); EXPECT_EQ(cryptor.keyIndex(), 3); EXPECT_TRUE(cryptor.enabled()); } From 7e87577335006c718d3e0184b5718af925a57bd5 Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 13 Nov 2026 21:57:48 -0800 Subject: [PATCH 2/2] Keep FrameCryptor's API and layout, and find the participant's tracks instead Adding the track sid to FrameCryptor changed its constructor and its layout, which existing binaries rely on (its destructor and moves are inline). Instead, its setters now look up the participant's tracks and apply to each, as their documentation already describes. Co-Authored-By: Claude Opus 5.5 --- include/livekit/e2ee.h | 8 +--- src/e2ee.cpp | 61 +++++++++++++++++++---------- src/tests/integration/test_e2ee.cpp | 27 +++++++------ src/tests/unit/test_e2ee.cpp | 3 +- 4 files changed, 57 insertions(+), 42 deletions(-) diff --git a/include/livekit/e2ee.h b/include/livekit/e2ee.h index cddfbb2b..f0c50c34 100644 --- a/include/livekit/e2ee.h +++ b/include/livekit/e2ee.h @@ -160,11 +160,10 @@ class LIVEKIT_API E2EEManager { KeyProviderOptions options_; }; - /// @brief Frame-level cryptor controls for one participant's track. + /// @brief Frame-level cryptor controls for one participant. class LIVEKIT_API FrameCryptor { public: - FrameCryptor(std::uint64_t room_handle, std::string participant_identity, std::string track_sid, int key_index, - bool enabled); + FrameCryptor(std::uint64_t room_handle, std::string participant_identity, int key_index, bool enabled); ~FrameCryptor() = default; FrameCryptor(const FrameCryptor&) = delete; FrameCryptor& operator=(const FrameCryptor&) = delete; @@ -172,8 +171,6 @@ class LIVEKIT_API E2EEManager { FrameCryptor& operator=(FrameCryptor&&) noexcept = default; const std::string& participantIdentity() const; - /// The sid of the track this cryptor encrypts or decrypts. - const std::string& trackSid() const; int keyIndex() const; bool enabled() const; @@ -187,7 +184,6 @@ class LIVEKIT_API E2EEManager { std::uint64_t room_handle_{0}; bool enabled_{false}; std::string participant_identity_; - std::string track_sid_; int key_index_{0}; }; diff --git a/src/e2ee.cpp b/src/e2ee.cpp index b88bf87d..440f56bf 100644 --- a/src/e2ee.cpp +++ b/src/e2ee.cpp @@ -34,6 +34,26 @@ std::string bytesToString(const std::vector& v) { std::vector stringToBytes(const std::string& s) { return std::vector(s.begin(), s.end()); } +proto::FfiResponse getFrameCryptors(std::uint64_t room_handle) { + proto::FfiRequest req; + req.mutable_e2ee()->set_room_handle(room_handle); + req.mutable_e2ee()->mutable_manager_get_frame_cryptors(); + return FfiClient::instance().sendRequest(req); +} + +// The FFI addresses a cryptor by participant and track, but a FrameCryptor only knows its participant (adding the +// track would change its layout), so its setters apply to each of the participant's tracks. +std::vector participantTrackSids(std::uint64_t room_handle, const std::string& participant_identity) { + const auto resp = getFrameCryptors(room_handle); + std::vector track_sids; + for (const auto& fc : resp.e2ee().manager_get_frame_cryptors().frame_cryptors()) { + if (fc.participant_identity() == participant_identity) { + track_sids.push_back(fc.track_sid()); + } + } + return track_sids; +} + } // namespace // ============================================================================ @@ -102,35 +122,37 @@ std::vector E2EEManager::KeyProvider::ratchetKey(const std::string // FrameCryptor // ============================================================================ -E2EEManager::FrameCryptor::FrameCryptor(std::uint64_t room_handle, std::string participant_identity, - std::string track_sid, int key_index, bool enabled) +E2EEManager::FrameCryptor::FrameCryptor(std::uint64_t room_handle, std::string participant_identity, int key_index, + bool enabled) : room_handle_(room_handle), enabled_(enabled), participant_identity_(std::move(participant_identity)), - track_sid_(std::move(track_sid)), key_index_(key_index) {} const std::string& E2EEManager::FrameCryptor::participantIdentity() const { return participant_identity_; } -const std::string& E2EEManager::FrameCryptor::trackSid() const { return track_sid_; } int E2EEManager::FrameCryptor::keyIndex() const { return key_index_; } bool E2EEManager::FrameCryptor::enabled() const { return enabled_; } void E2EEManager::FrameCryptor::setEnabled(bool enabled) { - proto::FfiRequest req; - req.mutable_e2ee()->set_room_handle(room_handle_); - req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_participant_identity(participant_identity_); - req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_track_sid(track_sid_); - req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_enabled(enabled); - FfiClient::instance().sendRequest(req); + for (const auto& track_sid : participantTrackSids(room_handle_, participant_identity_)) { + proto::FfiRequest req; + req.mutable_e2ee()->set_room_handle(room_handle_); + req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_participant_identity(participant_identity_); + req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_track_sid(track_sid); + req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_enabled(enabled); + FfiClient::instance().sendRequest(req); + } } void E2EEManager::FrameCryptor::setKeyIndex(int key_index) { - proto::FfiRequest req; - req.mutable_e2ee()->set_room_handle(room_handle_); - req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_participant_identity(participant_identity_); - req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_track_sid(track_sid_); - req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_key_index(key_index); - FfiClient::instance().sendRequest(req); + for (const auto& track_sid : participantTrackSids(room_handle_, participant_identity_)) { + proto::FfiRequest req; + req.mutable_e2ee()->set_room_handle(room_handle_); + req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_participant_identity(participant_identity_); + req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_track_sid(track_sid); + req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_key_index(key_index); + FfiClient::instance().sendRequest(req); + } } // ============================================================================ @@ -157,15 +179,12 @@ std::weak_ptr E2EEManager::keyProvider() { return key_ std::weak_ptr E2EEManager::keyProvider() const { return key_provider_; } std::vector E2EEManager::frameCryptors() const { - proto::FfiRequest req; - req.mutable_e2ee()->set_room_handle(room_handle_); - req.mutable_e2ee()->mutable_manager_get_frame_cryptors(); - auto resp = FfiClient::instance().sendRequest(req); + auto resp = getFrameCryptors(room_handle_); std::vector out; const auto& list = resp.e2ee().manager_get_frame_cryptors().frame_cryptors(); out.reserve(static_cast(list.size())); for (const auto& fc : list) { - out.emplace_back(room_handle_, fc.participant_identity(), fc.track_sid(), fc.key_index(), fc.enabled()); + out.emplace_back(room_handle_, fc.participant_identity(), fc.key_index(), fc.enabled()); } return out; } diff --git a/src/tests/integration/test_e2ee.cpp b/src/tests/integration/test_e2ee.cpp index 19f96f69..47df0f43 100644 --- a/src/tests/integration/test_e2ee.cpp +++ b/src/tests/integration/test_e2ee.cpp @@ -54,7 +54,7 @@ TEST_F(E2EEIntegrationTest, FrameCryptorsIncludePublishedTrack) { participant->unpublishTrack(track->publication()->sid()); } -TEST_F(E2EEIntegrationTest, FrameCryptorSettersApplyToItsTrack) { +TEST_F(E2EEIntegrationTest, FrameCryptorSettersApplyToEachOfTheParticipantsTracks) { failIfNotConfigured(); Room room; @@ -64,25 +64,26 @@ TEST_F(E2EEIntegrationTest, FrameCryptorSettersApplyToItsTrack) { ASSERT_TRUE(room.connect(config_.url, config_.token_a, room_options)); auto participant = lockLocalParticipant(room); auto source = std::make_shared(kDefaultAudioSampleRate, kDefaultAudioChannels, 0); - auto track = participant->publishAudioTrack("e2ee-key-index", source, TrackSource::SOURCE_MICROPHONE); - const std::string track_sid = track->publication()->sid(); + auto microphone = participant->publishAudioTrack("e2ee-microphone", source, TrackSource::SOURCE_MICROPHONE); + auto screen_share = + participant->publishAudioTrack("e2ee-screen-share", source, TrackSource::SOURCE_SCREENSHARE_AUDIO); auto manager = room.e2eeManager().lock(); ASSERT_NE(manager, nullptr); manager->keyProvider().lock()->setSharedKey(std::vector{'n', 'e', 'w'}, 1); - for (auto& cryptor : manager->frameCryptors()) { - cryptor.setKeyIndex(1); - cryptor.setEnabled(false); - } + auto cryptors = manager->frameCryptors(); + ASSERT_FALSE(cryptors.empty()); + cryptors.front().setKeyIndex(1); + cryptors.front().setEnabled(false); - std::vector> states; + std::vector> states; for (const auto& cryptor : manager->frameCryptors()) { - states.emplace_back(cryptor.participantIdentity(), cryptor.trackSid(), cryptor.keyIndex(), cryptor.enabled()); + states.emplace_back(cryptor.participantIdentity(), cryptor.keyIndex(), cryptor.enabled()); } - EXPECT_EQ( - states, - (std::vector>{{participant->identity(), track_sid, 1, false}})); - participant->unpublishTrack(track_sid); + EXPECT_EQ(states, (std::vector>{{participant->identity(), 1, false}, + {participant->identity(), 1, false}})); + participant->unpublishTrack(microphone->publication()->sid()); + participant->unpublishTrack(screen_share->publication()->sid()); } } // namespace livekit::test diff --git a/src/tests/unit/test_e2ee.cpp b/src/tests/unit/test_e2ee.cpp index 82859b96..678c8f62 100644 --- a/src/tests/unit/test_e2ee.cpp +++ b/src/tests/unit/test_e2ee.cpp @@ -35,10 +35,9 @@ TEST(E2EETest, E2EEOptionsDefaults) { } TEST(E2EETest, FrameCryptorAccessors) { - E2EEManager::FrameCryptor cryptor(/*room_handle=*/0, "alice", "TR_alice", /*key_index=*/3, + E2EEManager::FrameCryptor cryptor(/*room_handle=*/0, "alice", /*key_index=*/3, /*enabled=*/true); EXPECT_EQ(cryptor.participantIdentity(), "alice"); - EXPECT_EQ(cryptor.trackSid(), "TR_alice"); EXPECT_EQ(cryptor.keyIndex(), 3); EXPECT_TRUE(cryptor.enabled()); }