Skip to content

Commit 7899536

Browse files
use TrackKind to deliminate between audio and video tracks with the same name
1 parent 9db8fa8 commit 7899536

4 files changed

Lines changed: 113 additions & 42 deletions

File tree

‎include/livekit/subscription_thread_dispatcher.h‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class LIVEKIT_API SubscriptionThreadDispatcher {
156156
/// @ref Room calls this after it has processed a track-subscription event and
157157
/// updated its publication state. If a matching callback registration exists,
158158
/// the dispatcher creates the appropriate stream type and launches a reader
159-
/// thread for the `(participant, track_name)` key.
159+
/// thread for the `(participant, track_name, kind)` key.
160160
///
161161
/// If no matching callback is registered, this is a no-op.
162162
///
@@ -170,15 +170,14 @@ class LIVEKIT_API SubscriptionThreadDispatcher {
170170
/// Stop reader dispatch for an unsubscribed remote track.
171171
///
172172
/// @ref Room calls this when a remote track is unsubscribed. Any active
173-
/// reader stream for the given `(participant, track_name)` key is closed and its
173+
/// reader stream for the given `(participant, track_name, kind)` key is closed and its
174174
/// thread is joined. Callback registration is preserved so future
175175
/// re-subscription can start dispatch again automatically.
176176
///
177177
/// @param participant_identity Identity of the remote participant.
178-
/// @param source Track source associated with the subscription.
178+
/// @param kind Track kind associated with the subscription.
179179
/// @param track_name Track name associated with the subscription.
180-
void handleTrackUnsubscribed(const std::string& participant_identity, TrackSource source,
181-
const std::string& track_name);
180+
void handleTrackUnsubscribed(const std::string& participant_identity, TrackKind kind, const std::string& track_name);
182181

183182
// ---------------------------------------------------------------
184183
// Data track callbacks
@@ -240,9 +239,10 @@ class LIVEKIT_API SubscriptionThreadDispatcher {
240239
struct CallbackKey {
241240
std::string participant_identity;
242241
std::string track_name;
242+
TrackKind kind;
243243

244244
bool operator==(const CallbackKey& o) const {
245-
return participant_identity == o.participant_identity && track_name == o.track_name;
245+
return participant_identity == o.participant_identity && track_name == o.track_name && kind == o.kind;
246246
}
247247
};
248248

@@ -251,7 +251,8 @@ class LIVEKIT_API SubscriptionThreadDispatcher {
251251
std::size_t operator()(const CallbackKey& k) const {
252252
auto h1 = std::hash<std::string>{}(k.participant_identity);
253253
auto h2 = std::hash<std::string>{}(k.track_name);
254-
return h1 ^ (h2 << 1);
254+
auto h3 = std::hash<int>{}(static_cast<int>(k.kind));
255+
return h1 ^ (h2 << 1) ^ (h3 << 2);
255256
}
256257
};
257258

‎src/room.cpp‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ void readyForRoomEvent(std::uint64_t room_handle) {
7272
}
7373
}
7474

75+
TrackKind trackKindFromSource(TrackSource source) {
76+
switch (source) {
77+
case TrackSource::SOURCE_MICROPHONE:
78+
case TrackSource::SOURCE_SCREENSHARE_AUDIO:
79+
return TrackKind::KIND_AUDIO;
80+
case TrackSource::SOURCE_CAMERA:
81+
case TrackSource::SOURCE_SCREENSHARE:
82+
return TrackKind::KIND_VIDEO;
83+
case TrackSource::SOURCE_UNKNOWN:
84+
return TrackKind::KIND_UNKNOWN;
85+
}
86+
87+
return TrackKind::KIND_UNKNOWN;
88+
}
89+
7590
} // namespace
7691
Room::Room() : subscription_thread_dispatcher_(std::make_unique<SubscriptionThreadDispatcher>()) {}
7792

@@ -631,7 +646,7 @@ void Room::onEvent(const FfiEvent& event) {
631646
}
632647
case proto::RoomEvent::kTrackUnsubscribed: {
633648
TrackUnsubscribedEvent ev;
634-
TrackSource unsub_source = TrackSource::SOURCE_UNKNOWN;
649+
TrackKind unsub_kind = TrackKind::KIND_UNKNOWN;
635650
std::string unsub_identity;
636651
{
637652
const std::scoped_lock<std::mutex> guard(lock_);
@@ -654,8 +669,10 @@ void Room::onEvent(const FfiEvent& event) {
654669
break;
655670
}
656671
auto publication = pubIt->second;
657-
unsub_source = publication->source();
658672
auto track = publication->track();
673+
if (track) {
674+
unsub_kind = track->kind();
675+
}
659676
publication->setTrack(nullptr);
660677
publication->setSubscribed(false);
661678
ev.participant = rparticipant;
@@ -667,9 +684,12 @@ void Room::onEvent(const FfiEvent& event) {
667684
delegate_snapshot->onTrackUnsubscribed(*this, ev);
668685
}
669686

670-
if (subscription_thread_dispatcher_ && unsub_source != TrackSource::SOURCE_UNKNOWN) {
671-
subscription_thread_dispatcher_->handleTrackUnsubscribed(unsub_identity, unsub_source,
687+
if (subscription_thread_dispatcher_ && unsub_kind != TrackKind::KIND_UNKNOWN) {
688+
subscription_thread_dispatcher_->handleTrackUnsubscribed(unsub_identity, unsub_kind,
672689
ev.publication ? ev.publication->name() : "");
690+
} else {
691+
LK_LOG_WARN("track_unsubscribed for participant {} has unknown kind and cannot stop a reader",
692+
unsub_identity);
673693
}
674694
break;
675695
}

‎src/subscription_thread_dispatcher.cpp‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SubscriptionThreadDispatcher::~SubscriptionThreadDispatcher() {
6060
void SubscriptionThreadDispatcher::setOnAudioFrameCallback(const std::string& participant_identity,
6161
const std::string& track_name, AudioFrameCallback callback,
6262
const AudioStream::Options& opts) {
63-
const CallbackKey key{participant_identity, track_name};
63+
const CallbackKey key{participant_identity, track_name, TrackKind::KIND_AUDIO};
6464
const std::scoped_lock<std::mutex> lock(lock_);
6565
const bool replacing = audio_callbacks_.find(key) != audio_callbacks_.end();
6666
audio_callbacks_[key] = RegisteredAudioCallback{std::move(callback), opts};
@@ -74,7 +74,7 @@ void SubscriptionThreadDispatcher::setOnVideoFrameEventCallback(const std::strin
7474
const std::string& track_name,
7575
VideoFrameEventCallback callback,
7676
const VideoStream::Options& opts) {
77-
const CallbackKey key{participant_identity, track_name};
77+
const CallbackKey key{participant_identity, track_name, TrackKind::KIND_VIDEO};
7878
const std::scoped_lock<std::mutex> lock(lock_);
7979
const bool replacing = video_callbacks_.find(key) != video_callbacks_.end();
8080
video_callbacks_[key] = RegisteredVideoCallback{
@@ -91,7 +91,7 @@ void SubscriptionThreadDispatcher::setOnVideoFrameEventCallback(const std::strin
9191
void SubscriptionThreadDispatcher::setOnVideoFrameCallback(const std::string& participant_identity,
9292
const std::string& track_name, VideoFrameCallback callback,
9393
const VideoStream::Options& opts) {
94-
const CallbackKey key{participant_identity, track_name};
94+
const CallbackKey key{participant_identity, track_name, TrackKind::KIND_VIDEO};
9595
const std::scoped_lock<std::mutex> lock(lock_);
9696
const bool replacing = video_callbacks_.find(key) != video_callbacks_.end();
9797
video_callbacks_[key] = RegisteredVideoCallback{
@@ -107,7 +107,7 @@ void SubscriptionThreadDispatcher::setOnVideoFrameCallback(const std::string& pa
107107

108108
void SubscriptionThreadDispatcher::clearOnAudioFrameCallback(const std::string& participant_identity,
109109
const std::string& track_name) {
110-
const CallbackKey key{participant_identity, track_name};
110+
const CallbackKey key{participant_identity, track_name, TrackKind::KIND_AUDIO};
111111
std::thread old_thread;
112112
bool removed_callback = false;
113113
{
@@ -126,7 +126,7 @@ void SubscriptionThreadDispatcher::clearOnAudioFrameCallback(const std::string&
126126

127127
void SubscriptionThreadDispatcher::clearOnVideoFrameCallback(const std::string& participant_identity,
128128
const std::string& track_name) {
129-
const CallbackKey key{participant_identity, track_name};
129+
const CallbackKey key{participant_identity, track_name, TrackKind::KIND_VIDEO};
130130
std::thread old_thread;
131131
bool removed_callback = false;
132132
{
@@ -157,7 +157,7 @@ void SubscriptionThreadDispatcher::handleTrackSubscribed(const std::string& part
157157
LK_LOG_DEBUG("Handling subscribed track for participant={} source={} kind={}", participant_identity,
158158
static_cast<int>(source), trackKindName(track->kind()));
159159

160-
const CallbackKey key{participant_identity, track_name};
160+
const CallbackKey key{participant_identity, track_name, track->kind()};
161161
std::thread old_thread;
162162
{
163163
const std::scoped_lock<std::mutex> lock(lock_);
@@ -168,17 +168,24 @@ void SubscriptionThreadDispatcher::handleTrackSubscribed(const std::string& part
168168
}
169169
}
170170

171-
void SubscriptionThreadDispatcher::handleTrackUnsubscribed(const std::string& participant_identity, TrackSource source,
171+
void SubscriptionThreadDispatcher::handleTrackUnsubscribed(const std::string& participant_identity, TrackKind kind,
172172
const std::string& track_name) {
173-
const CallbackKey key{participant_identity, track_name};
173+
if (kind == TrackKind::KIND_UNKNOWN) {
174+
LK_LOG_ERROR(
175+
"handleTrackUnsubscribed called with unknown kind for participant={} track_name={}. Subscription may not be "
176+
"stopped correctly.",
177+
participant_identity, track_name);
178+
return;
179+
}
180+
const CallbackKey key{participant_identity, track_name, kind};
174181
std::thread old_thread;
175182
{
176183
const std::scoped_lock<std::mutex> lock(lock_);
177184
old_thread = extractReaderThreadLocked(key);
178185
LK_LOG_DEBUG(
179-
"Handling unsubscribed track for participant={} source={} "
180-
"track_name={} stopped_reader={}",
181-
participant_identity, static_cast<int>(source), track_name, old_thread.joinable());
186+
"Handling unsubscribed track for participant={} "
187+
"track_name={} kind={} stopped_reader={}",
188+
participant_identity, track_name, trackKindName(kind), old_thread.joinable());
182189
}
183190
if (old_thread.joinable()) {
184191
old_thread.join();
@@ -337,12 +344,13 @@ void SubscriptionThreadDispatcher::stopAll() {
337344
std::thread SubscriptionThreadDispatcher::extractReaderThreadLocked(const CallbackKey& key) {
338345
auto it = active_readers_.find(key);
339346
if (it == active_readers_.end()) {
340-
LK_LOG_TRACE("No active reader to extract for participant={} track_name={}", key.participant_identity,
341-
key.track_name);
347+
LK_LOG_TRACE("No active reader to extract for participant={} track_name={} kind={}", key.participant_identity,
348+
key.track_name, trackKindName(key.kind));
342349
return {};
343350
}
344351

345-
LK_LOG_DEBUG("Extracting active reader for participant={} track_name={}", key.participant_identity, key.track_name);
352+
LK_LOG_DEBUG("Extracting active reader for participant={} track_name={} kind={}", key.participant_identity,
353+
key.track_name, trackKindName(key.kind));
346354
ActiveReader reader = std::move(it->second);
347355
active_readers_.erase(it);
348356

‎src/tests/unit/test_subscription_thread_dispatcher.cpp‎

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class SubscriptionThreadDispatcherTest : public ::testing::Test {
3434

3535
using CallbackKey = SubscriptionThreadDispatcher::CallbackKey;
3636
using CallbackKeyHash = SubscriptionThreadDispatcher::CallbackKeyHash;
37+
using ActiveReader = SubscriptionThreadDispatcher::ActiveReader;
3738
using DataCallbackKey = SubscriptionThreadDispatcher::DataCallbackKey;
3839
using DataCallbackKeyHash = SubscriptionThreadDispatcher::DataCallbackKeyHash;
3940

@@ -51,20 +52,26 @@ class SubscriptionThreadDispatcherTest : public ::testing::Test {
5152
// ============================================================================
5253

5354
TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyEqualKeysCompareEqual) {
54-
CallbackKey a{"alice", "mic-main"};
55-
CallbackKey b{"alice", "mic-main"};
55+
CallbackKey a{"alice", "mic-main", TrackKind::KIND_AUDIO};
56+
CallbackKey b{"alice", "mic-main", TrackKind::KIND_AUDIO};
5657
EXPECT_TRUE(a == b);
5758
}
5859

5960
TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyDifferentIdentityNotEqual) {
60-
CallbackKey a{"alice", "mic-main"};
61-
CallbackKey b{"bob", "mic-main"};
61+
CallbackKey a{"alice", "mic-main", TrackKind::KIND_AUDIO};
62+
CallbackKey b{"bob", "mic-main", TrackKind::KIND_AUDIO};
6263
EXPECT_FALSE(a == b);
6364
}
6465

6566
TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyDifferentTrackNameNotEqual) {
66-
CallbackKey a{"alice", "cam-main"};
67-
CallbackKey b{"alice", "cam-backup"};
67+
CallbackKey a{"alice", "cam-main", TrackKind::KIND_VIDEO};
68+
CallbackKey b{"alice", "cam-backup", TrackKind::KIND_VIDEO};
69+
EXPECT_FALSE(a == b);
70+
}
71+
72+
TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyDifferentKindNotEqual) {
73+
CallbackKey a{"alice", "shared", TrackKind::KIND_AUDIO};
74+
CallbackKey b{"alice", "shared", TrackKind::KIND_VIDEO};
6875
EXPECT_FALSE(a == b);
6976
}
7077

@@ -73,28 +80,30 @@ TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyDifferentTrackNameNotEqual)
7380
// ============================================================================
7481

7582
TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyHashEqualKeysProduceSameHash) {
76-
CallbackKey a{"alice", "mic-main"};
77-
CallbackKey b{"alice", "mic-main"};
83+
CallbackKey a{"alice", "mic-main", TrackKind::KIND_AUDIO};
84+
CallbackKey b{"alice", "mic-main", TrackKind::KIND_AUDIO};
7885
CallbackKeyHash hasher;
7986
EXPECT_EQ(hasher(a), hasher(b));
8087
}
8188

8289
TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyHashDifferentKeysLikelyDifferentHash) {
8390
CallbackKeyHash hasher;
84-
CallbackKey mic{"alice", "mic-main"};
85-
CallbackKey cam{"alice", "cam-main"};
86-
CallbackKey bob{"bob", "mic-main"};
91+
CallbackKey mic{"alice", "mic-main", TrackKind::KIND_AUDIO};
92+
CallbackKey cam{"alice", "cam-main", TrackKind::KIND_VIDEO};
93+
CallbackKey bob{"bob", "mic-main", TrackKind::KIND_AUDIO};
94+
CallbackKey video_same_name{"alice", "mic-main", TrackKind::KIND_VIDEO};
8795

8896
EXPECT_NE(hasher(mic), hasher(cam));
8997
EXPECT_NE(hasher(mic), hasher(bob));
98+
EXPECT_NE(hasher(mic), hasher(video_same_name));
9099
}
91100

92101
TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyWorksAsUnorderedMapKey) {
93102
std::unordered_map<CallbackKey, int, CallbackKeyHash> map;
94103

95-
CallbackKey k1{"alice", "mic-main"};
96-
CallbackKey k2{"bob", "cam-main"};
97-
CallbackKey k3{"alice", "cam-backup"};
104+
CallbackKey k1{"alice", "shared", TrackKind::KIND_AUDIO};
105+
CallbackKey k2{"alice", "shared", TrackKind::KIND_VIDEO};
106+
CallbackKey k3{"bob", "cam-backup", TrackKind::KIND_VIDEO};
98107

99108
map[k1] = 1;
100109
map[k2] = 2;
@@ -115,8 +124,8 @@ TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyWorksAsUnorderedMapKey) {
115124
}
116125

117126
TEST_F(SubscriptionThreadDispatcherTest, CallbackKeyEmptyIdentityWorks) {
118-
CallbackKey a{"", "mic-main"};
119-
CallbackKey b{"", "mic-main"};
127+
CallbackKey a{"", "mic-main", TrackKind::KIND_AUDIO};
128+
CallbackKey b{"", "mic-main", TrackKind::KIND_AUDIO};
120129
CallbackKeyHash hasher;
121130
EXPECT_TRUE(a == b);
122131
EXPECT_EQ(hasher(a), hasher(b));
@@ -204,6 +213,26 @@ TEST_F(SubscriptionThreadDispatcherTest, MultipleDistinctCallbacksAreIndependent
204213
EXPECT_EQ(videoCallbacks(dispatcher).size(), 2u);
205214
}
206215

216+
TEST_F(SubscriptionThreadDispatcherTest, SameTrackNameAudioAndVideoCallbacksAreIndependent) {
217+
SubscriptionThreadDispatcher dispatcher;
218+
dispatcher.setOnAudioFrameCallback("alice", "shared", [](const AudioFrame&) {});
219+
dispatcher.setOnVideoFrameCallback("alice", "shared", [](const VideoFrame&, std::int64_t) {});
220+
221+
const CallbackKey audio_key{"alice", "shared", TrackKind::KIND_AUDIO};
222+
const CallbackKey video_key{"alice", "shared", TrackKind::KIND_VIDEO};
223+
ASSERT_EQ(audioCallbacks(dispatcher).size(), 1u);
224+
ASSERT_EQ(videoCallbacks(dispatcher).size(), 1u);
225+
EXPECT_EQ(audioCallbacks(dispatcher).count(audio_key), 1u);
226+
EXPECT_EQ(videoCallbacks(dispatcher).count(video_key), 1u);
227+
228+
dispatcher.clearOnAudioFrameCallback("alice", "shared");
229+
EXPECT_TRUE(audioCallbacks(dispatcher).empty());
230+
EXPECT_EQ(videoCallbacks(dispatcher).count(video_key), 1u);
231+
232+
dispatcher.clearOnVideoFrameCallback("alice", "shared");
233+
EXPECT_TRUE(videoCallbacks(dispatcher).empty());
234+
}
235+
207236
TEST_F(SubscriptionThreadDispatcherTest, ClearingOneTrackNameDoesNotAffectOther) {
208237
SubscriptionThreadDispatcher dispatcher;
209238
dispatcher.setOnAudioFrameCallback("alice", "mic-main", [](const AudioFrame&) {});
@@ -213,7 +242,7 @@ TEST_F(SubscriptionThreadDispatcherTest, ClearingOneTrackNameDoesNotAffectOther)
213242
dispatcher.clearOnAudioFrameCallback("alice", "mic-main");
214243
EXPECT_EQ(audioCallbacks(dispatcher).size(), 1u);
215244

216-
CallbackKey remaining{"alice", "screenshare-main"};
245+
CallbackKey remaining{"alice", "screenshare-main", TrackKind::KIND_AUDIO};
217246
EXPECT_EQ(audioCallbacks(dispatcher).count(remaining), 1u);
218247
}
219248

@@ -234,6 +263,19 @@ TEST_F(SubscriptionThreadDispatcherTest, ActiveReadersEmptyAfterCallbackRegistra
234263
"readers";
235264
}
236265

266+
TEST_F(SubscriptionThreadDispatcherTest, ActiveReadersDistinguishAudioAndVideoWithSameTrackName) {
267+
SubscriptionThreadDispatcher dispatcher;
268+
const CallbackKey audio_key{"alice", "shared", TrackKind::KIND_AUDIO};
269+
const CallbackKey video_key{"alice", "shared", TrackKind::KIND_VIDEO};
270+
271+
activeReaders(dispatcher).try_emplace(audio_key, ActiveReader{});
272+
activeReaders(dispatcher).try_emplace(video_key, ActiveReader{});
273+
274+
EXPECT_EQ(activeReaders(dispatcher).size(), 2u);
275+
EXPECT_EQ(activeReaders(dispatcher).count(audio_key), 1u);
276+
EXPECT_EQ(activeReaders(dispatcher).count(video_key), 1u);
277+
}
278+
237279
// ============================================================================
238280
// Destruction safety
239281
// ============================================================================

0 commit comments

Comments
 (0)