Skip to content

Commit 9aed590

Browse files
authored
Fix unknown SID from local track (#270)
* Add test coverage * Update SID after publication complete * Clean up * Make setSID protected * Also test audio track
1 parent e6e191b commit 9aed590

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

‎include/livekit/local_audio_track.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ class LIVEKIT_API LocalAudioTrack : public Track {
116116
/// the association.
117117
void setPublication(const std::shared_ptr<LocalTrackPublication>& publication) noexcept override {
118118
local_publication_ = publication;
119+
if (publication) {
120+
setSid(publication->sid());
121+
}
119122
}
120123

121124
private:

‎include/livekit/local_video_track.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class LIVEKIT_API LocalVideoTrack : public Track {
8989
/// a true move but is a API-breaking change hence left for a future revision.
9090
void setPublication(const std::shared_ptr<LocalTrackPublication>& publication) noexcept override {
9191
local_publication_ = publication;
92+
if (publication) {
93+
setSid(publication->sid());
94+
}
9295
}
9396

9497
private:

‎include/livekit/track.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class LIVEKIT_API Track {
122122
Track(FfiHandle handle, std::string sid, std::string name, TrackKind kind, StreamState state, bool muted,
123123
bool remote);
124124

125+
void setSid(std::string sid) noexcept { sid_ = std::move(sid); }
126+
125127
void setPublicationFields(std::optional<TrackSource> source, std::optional<bool> simulcasted,
126128
std::optional<uint32_t> width, std::optional<uint32_t> height,
127129
std::optional<std::string> mime_type);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2026 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <gtest/gtest.h>
18+
19+
#include <memory>
20+
#include <string>
21+
22+
#include "../common/audio_utils.h"
23+
#include "../common/test_common.h"
24+
25+
namespace livekit::test {
26+
namespace {
27+
28+
void expectTrackSidAssigned(const Track& track, const LocalTrackPublication& publication) {
29+
const std::string& track_sid = track.sid();
30+
const std::string& publication_sid = publication.sid();
31+
EXPECT_NE(track_sid, "TR_unknown");
32+
EXPECT_FALSE(track_sid.empty());
33+
EXPECT_EQ(track_sid, publication_sid);
34+
}
35+
36+
} // namespace
37+
38+
class LocalTrackPublishSidTest : public LiveKitTestBase {};
39+
40+
TEST_F(LocalTrackPublishSidTest, PublishVideoTrackAssignsSid) {
41+
failIfNotConfigured();
42+
43+
Room room;
44+
const RoomOptions room_options;
45+
ASSERT_TRUE(room.connect(config_.url, config_.token_a, room_options));
46+
47+
auto source = std::make_shared<EncodedVideoSource>(VideoCodec::H264, 16, 16);
48+
std::shared_ptr<LocalVideoTrack> track;
49+
ASSERT_NO_THROW(
50+
track = lockLocalParticipant(room)->publishVideoTrack("video-sid-check", source, TrackSource::SOURCE_CAMERA));
51+
ASSERT_NE(track, nullptr);
52+
ASSERT_NE(track->publication(), nullptr);
53+
54+
expectTrackSidAssigned(*track, *track->publication());
55+
lockLocalParticipant(room)->unpublishTrack(track->publication()->sid());
56+
}
57+
58+
TEST_F(LocalTrackPublishSidTest, PublishAudioTrackAssignsSid) {
59+
failIfNotConfigured();
60+
61+
Room room;
62+
const RoomOptions room_options;
63+
ASSERT_TRUE(room.connect(config_.url, config_.token_a, room_options));
64+
65+
auto source = std::make_shared<AudioSource>(kDefaultAudioSampleRate, kDefaultAudioChannels, 0);
66+
std::shared_ptr<LocalAudioTrack> track;
67+
ASSERT_NO_THROW(
68+
track = lockLocalParticipant(room)->publishAudioTrack("audio-sid-check", source, TrackSource::SOURCE_MICROPHONE));
69+
ASSERT_NE(track, nullptr);
70+
ASSERT_NE(track->publication(), nullptr);
71+
72+
expectTrackSidAssigned(*track, *track->publication());
73+
lockLocalParticipant(room)->unpublishTrack(track->publication()->sid());
74+
}
75+
76+
} // namespace livekit::test

0 commit comments

Comments
 (0)