Skip to content

Commit 19b55da

Browse files
keyProvider() raw -> weak
1 parent 15f16f7 commit 19b55da

3 files changed

Lines changed: 23 additions & 14 deletions

File tree

‎include/livekit/e2ee.h‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,17 @@ class LIVEKIT_API E2EEManager {
203203
/// will result in undecodable media (black video / silent audio).
204204
void setEnabled(bool enabled);
205205

206-
/// Returns the key provider if E2EE was configured for the room; otherwise
207-
/// nullptr.
208-
KeyProvider* keyProvider();
209-
const KeyProvider* keyProvider() const;
206+
/// Returns a weak reference to the key provider if E2EE was configured for
207+
/// the room; otherwise an expired weak_ptr.
208+
///
209+
/// The KeyProvider is owned by this E2EEManager (which is in turn owned by
210+
/// Room). Callers must lock() the returned weak_ptr before use and must not
211+
/// retain the resulting shared_ptr beyond the lifetime of the Room.
212+
///
213+
/// @return A weak_ptr to the KeyProvider, or an expired weak_ptr if E2EE was
214+
/// not configured.
215+
std::weak_ptr<KeyProvider> keyProvider();
216+
std::weak_ptr<const KeyProvider> keyProvider() const;
210217

211218
/// Retrieves the current list of frame cryptors from the underlying runtime.
212219
std::vector<E2EEManager::FrameCryptor> frameCryptors() const;
@@ -220,7 +227,9 @@ class LIVEKIT_API E2EEManager {
220227
std::uint64_t room_handle_{0};
221228
bool enabled_{false};
222229
E2EEOptions options_;
223-
KeyProvider key_provider_;
230+
/// The key provider is owned by the E2EEManager and is not shared with other objects.
231+
/// It is a shared_ptr just to utilize the weak_ptr interface for the keyProvider() accessor.
232+
std::shared_ptr<KeyProvider> key_provider_;
224233
};
225234

226235
} // namespace livekit

‎src/e2ee.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ E2EEManager::E2EEManager(std::uint64_t room_handle, const E2EEOptions& options)
137137
: room_handle_(room_handle),
138138
enabled_(true), // or false, depending on your desired default behavior
139139
options_(options),
140-
key_provider_(room_handle, options.key_provider_options) {}
140+
key_provider_(std::shared_ptr<KeyProvider>(new KeyProvider(room_handle, options.key_provider_options))) {}
141141

142142
bool E2EEManager::enabled() const { return enabled_; }
143143

@@ -149,8 +149,8 @@ void E2EEManager::setEnabled(bool enabled) {
149149
enabled_ = enabled;
150150
}
151151

152-
E2EEManager::KeyProvider* E2EEManager::keyProvider() { return &key_provider_; }
153-
const E2EEManager::KeyProvider* E2EEManager::keyProvider() const { return &key_provider_; }
152+
std::weak_ptr<E2EEManager::KeyProvider> E2EEManager::keyProvider() { return key_provider_; }
153+
std::weak_ptr<const E2EEManager::KeyProvider> E2EEManager::keyProvider() const { return key_provider_; }
154154

155155
std::vector<E2EEManager::FrameCryptor> E2EEManager::frameCryptors() const {
156156
proto::FfiRequest req;

‎src/tests/integration/test_data_track.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,16 @@ void runEncryptedDataTrackRoundTrip(KeyDerivationFunction key_derivation_functio
210210

211211
ASSERT_FALSE(publisher_room->e2eeManager().expired());
212212
ASSERT_FALSE(subscriber_room->e2eeManager().expired());
213-
ASSERT_NE(publisher_room->e2eeManager().lock()->keyProvider(), nullptr);
214-
ASSERT_NE(subscriber_room->e2eeManager().lock()->keyProvider(), nullptr);
215-
EXPECT_EQ(publisher_room->e2eeManager().lock()->keyProvider()->options().key_derivation_function,
213+
ASSERT_FALSE(publisher_room->e2eeManager().lock()->keyProvider().expired());
214+
ASSERT_FALSE(subscriber_room->e2eeManager().lock()->keyProvider().expired());
215+
EXPECT_EQ(publisher_room->e2eeManager().lock()->keyProvider().lock()->options().key_derivation_function,
216216
key_derivation_function);
217-
EXPECT_EQ(subscriber_room->e2eeManager().lock()->keyProvider()->options().key_derivation_function,
217+
EXPECT_EQ(subscriber_room->e2eeManager().lock()->keyProvider().lock()->options().key_derivation_function,
218218
key_derivation_function);
219219
publisher_room->e2eeManager().lock()->setEnabled(true);
220220
subscriber_room->e2eeManager().lock()->setEnabled(true);
221-
EXPECT_EQ(publisher_room->e2eeManager().lock()->keyProvider()->exportSharedKey(), e2eeSharedKey());
222-
EXPECT_EQ(subscriber_room->e2eeManager().lock()->keyProvider()->exportSharedKey(), e2eeSharedKey());
221+
EXPECT_EQ(publisher_room->e2eeManager().lock()->keyProvider().lock()->exportSharedKey(), e2eeSharedKey());
222+
EXPECT_EQ(subscriber_room->e2eeManager().lock()->keyProvider().lock()->exportSharedKey(), e2eeSharedKey());
223223

224224
auto publish_result = lockLocalParticipant(*publisher_room)->publishDataTrack(track_name);
225225
if (!publish_result) {

0 commit comments

Comments
 (0)