Skip to content

Commit 4f57e10

Browse files
Add missing reply_to_stream_id field to TextStreamInfo (#202)
* feat: Add missing reply_to_stream_id field to TextStreamInfo and forward it from proto::DataStream.text_header().reply_to_stream_id() * Fill reply_to_stream_id field in TextStreamInfo on writer side as well * Add empty reply_to_stream_id guard in TextStreamWriter constructor Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> * Fix potential crash if detached threads outlive Text/BytesStreamCollector in data stream tests * Remove .detach() calls in Text/BytesStreamCollecton recv_thread_ + add .joinable() guard before trying to join the thread * Address style feedback --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 2cbf9a3 commit 4f57e10

4 files changed

Lines changed: 230 additions & 1 deletion

File tree

‎include/livekit/data_stream.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ struct BaseStreamInfo {
6161

6262
/// Metadata for a text stream.
6363
struct TextStreamInfo : BaseStreamInfo {
64-
/// IDs of any attached streams (for replies / threads).
64+
/// IDs of any attached streams (for attached files).
6565
std::vector<std::string> attachments;
66+
/// If this stream is a reply to another stream, this field holds its ID
67+
std::optional<std::string> reply_to_stream_id;
6668
};
6769

6870
/// Metadata for a byte stream.

‎src/data_stream.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ TextStreamWriter::TextStreamWriter(LocalParticipant& local_participant, const st
287287
reply_to_id_ = reply_to_id;
288288
// ✅ Canonical user-facing metadata comes from BaseStreamWriter fields.
289289
fillBaseInfo(info_, stream_id_, mime_type_, topic_, timestamp_ms_, total_size_, attributes_);
290+
if (!reply_to_id.empty()) {
291+
info_.reply_to_stream_id = reply_to_id;
292+
}
290293
}
291294

292295
void TextStreamWriter::write(const std::string& text) {

‎src/room_proto_converter.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,9 @@ TextStreamInfo makeTextInfo(const proto::DataStream::Header& header) {
619619
info.mime_type = header.mime_type();
620620
info.topic = header.topic();
621621
info.timestamp = header.timestamp();
622+
if (header.text_header().has_reply_to_stream_id()) {
623+
info.reply_to_stream_id = header.text_header().reply_to_stream_id();
624+
}
622625

623626
if (header.has_total_length()) {
624627
info.size = static_cast<std::size_t>(header.total_length());
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
// End-to-end coverage for text/byte data streams (registerTextStreamHandler /
18+
// registerByteStreamHandler + TextStreamWriter / ByteStreamWriter). Requires a
19+
// local SFU; see test_data_track.cpp for setup instructions. Run with:
20+
// ./build-debug/bin/livekit_integration_tests --gtest_filter=*DataStream*
21+
22+
#include <livekit/data_stream.h>
23+
24+
#include <condition_variable>
25+
#include <cstdint>
26+
#include <mutex>
27+
#include <string>
28+
#include <thread>
29+
#include <vector>
30+
31+
#include "../common/test_common.h"
32+
33+
namespace livekit::test {
34+
35+
using namespace std::chrono_literals;
36+
37+
namespace {
38+
39+
constexpr auto kStreamWaitTimeout = 10s;
40+
41+
std::string makeTopic(const std::string& suffix) {
42+
return "test_topic_" + suffix + "_" + std::to_string(getTimestampUs());
43+
}
44+
45+
/// Waits for a single incoming text stream on `topic` and captures its info
46+
/// plus fully-read content. Registers itself as the topic's handler.
47+
class TextStreamCollector {
48+
public:
49+
~TextStreamCollector() {
50+
if (recv_thread_.joinable()) {
51+
recv_thread_.join();
52+
}
53+
}
54+
void registerOn(Room& room, const std::string& topic) {
55+
// Handlers run on the Room event thread and must not block (per
56+
// registerTextStreamHandler's docs), since later chunk/close events for
57+
// this same reader are dispatched on that same thread. readAll() blocks
58+
// until close, so it has to happen on a separate thread.
59+
room.registerTextStreamHandler(
60+
topic, [this](std::shared_ptr<TextStreamReader> reader, const std::string& participant_identity) {
61+
recv_thread_ = std::thread([this, reader, participant_identity] {
62+
auto info = reader->info();
63+
auto text = reader->readAll();
64+
std::lock_guard<std::mutex> lock(mutex_);
65+
info_ = std::move(info);
66+
text_ = std::move(text);
67+
sender_identity_ = participant_identity;
68+
done_ = true;
69+
cv_.notify_all();
70+
});
71+
});
72+
}
73+
74+
bool wait(std::chrono::milliseconds timeout) {
75+
std::unique_lock<std::mutex> lock(mutex_);
76+
return cv_.wait_for(lock, timeout, [this] { return done_; });
77+
}
78+
79+
const TextStreamInfo& info() const { return info_; }
80+
const std::string& text() const { return text_; }
81+
const std::string& senderIdentity() const { return sender_identity_; }
82+
83+
private:
84+
std::thread recv_thread_;
85+
std::mutex mutex_;
86+
std::condition_variable cv_;
87+
bool done_ = false;
88+
TextStreamInfo info_;
89+
std::string text_;
90+
std::string sender_identity_;
91+
};
92+
93+
/// Same idea as TextStreamCollector, for byte streams.
94+
class ByteStreamCollector {
95+
public:
96+
~ByteStreamCollector() {
97+
if (recv_thread_.joinable()) {
98+
recv_thread_.join();
99+
}
100+
}
101+
void registerOn(Room& room, const std::string& topic) {
102+
// See TextStreamCollector::registerOn: the blocking readNext() loop must
103+
// not run on the Room event thread.
104+
room.registerByteStreamHandler(
105+
topic, [this](std::shared_ptr<ByteStreamReader> reader, const std::string& participant_identity) {
106+
recv_thread_ = std::thread([this, reader, participant_identity] {
107+
auto info = reader->info();
108+
std::vector<std::uint8_t> content;
109+
std::vector<std::uint8_t> chunk;
110+
while (reader->readNext(chunk)) {
111+
content.insert(content.end(), chunk.begin(), chunk.end());
112+
}
113+
std::lock_guard<std::mutex> lock(mutex_);
114+
info_ = std::move(info);
115+
content_ = std::move(content);
116+
sender_identity_ = participant_identity;
117+
done_ = true;
118+
cv_.notify_all();
119+
});
120+
});
121+
}
122+
123+
bool wait(std::chrono::milliseconds timeout) {
124+
std::unique_lock<std::mutex> lock(mutex_);
125+
return cv_.wait_for(lock, timeout, [this] { return done_; });
126+
}
127+
128+
const ByteStreamInfo& info() const { return info_; }
129+
const std::vector<std::uint8_t>& content() const { return content_; }
130+
const std::string& senderIdentity() const { return sender_identity_; }
131+
132+
private:
133+
std::thread recv_thread_;
134+
std::mutex mutex_;
135+
std::condition_variable cv_;
136+
bool done_ = false;
137+
ByteStreamInfo info_;
138+
std::vector<std::uint8_t> content_;
139+
std::string sender_identity_;
140+
};
141+
142+
} // namespace
143+
144+
class DataStreamE2ETest : public LiveKitTestBase {};
145+
146+
TEST_F(DataStreamE2ETest, TextStreamRoundTripEndToEnd) {
147+
const auto topic = makeTopic("text");
148+
149+
auto rooms = testRooms(2);
150+
auto& sender_room = rooms[0];
151+
auto& receiver_room = rooms[1];
152+
auto sender = lockLocalParticipant(*sender_room);
153+
const auto sender_identity = sender->identity();
154+
155+
TextStreamCollector collector;
156+
collector.registerOn(*receiver_room, topic);
157+
158+
TextStreamWriter writer(*sender, topic);
159+
writer.write("hello, ");
160+
writer.write("world!");
161+
writer.close();
162+
163+
ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "Timed out waiting for text stream";
164+
EXPECT_EQ(collector.text(), "hello, world!");
165+
EXPECT_EQ(collector.senderIdentity(), sender_identity);
166+
EXPECT_EQ(collector.info().topic, topic);
167+
EXPECT_EQ(collector.info().mime_type, "text/plain");
168+
}
169+
170+
// Regression coverage for the `text_header.reply_to_stream_id` field: writing
171+
// a text stream with a reply-to id set should surface that id on the
172+
// receiving side's TextStreamInfo. If this fails while the C++-side
173+
// conversion (room_proto_converter.cpp: makeTextInfo) looks correct, the gap
174+
// is upstream in the Rust FFI layer not forwarding the field.
175+
TEST_F(DataStreamE2ETest, TextStreamReplyToStreamIdIsRoutedEndToEnd) {
176+
const auto topic = makeTopic("text_reply");
177+
const std::string reply_to_id = "original-stream-" + std::to_string(getTimestampUs());
178+
179+
auto rooms = testRooms(2);
180+
auto& sender_room = rooms[0];
181+
auto& receiver_room = rooms[1];
182+
183+
TextStreamCollector collector;
184+
collector.registerOn(*receiver_room, topic);
185+
186+
TextStreamWriter writer(*lockLocalParticipant(*sender_room), topic, /*attributes=*/{}, /*stream_id=*/"",
187+
/*total_size=*/std::nullopt, reply_to_id);
188+
writer.write("reply payload");
189+
writer.close();
190+
191+
ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "Timed out waiting for text stream";
192+
EXPECT_EQ(collector.text(), "reply payload");
193+
ASSERT_TRUE(collector.info().reply_to_stream_id.has_value())
194+
<< "reply_to_stream_id was not routed through FFI from the Rust SDK";
195+
EXPECT_EQ(collector.info().reply_to_stream_id.value(), reply_to_id);
196+
}
197+
198+
TEST_F(DataStreamE2ETest, ByteStreamRoundTripEndToEnd) {
199+
const auto topic = makeTopic("bytes");
200+
const std::vector<std::uint8_t> payload{0x00, 0x01, 0x02, 0xFE, 0xFF, 'h', 'i'};
201+
202+
auto rooms = testRooms(2);
203+
auto& sender_room = rooms[0];
204+
auto& receiver_room = rooms[1];
205+
const auto sender_identity = lockLocalParticipant(*sender_room)->identity();
206+
207+
ByteStreamCollector collector;
208+
collector.registerOn(*receiver_room, topic);
209+
210+
ByteStreamWriter writer(*lockLocalParticipant(*sender_room), /*name=*/"payload.bin", topic);
211+
writer.write(payload);
212+
writer.close();
213+
214+
ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "Timed out waiting for byte stream";
215+
EXPECT_EQ(collector.content(), payload);
216+
EXPECT_EQ(collector.senderIdentity(), sender_identity);
217+
EXPECT_EQ(collector.info().topic, topic);
218+
EXPECT_EQ(collector.info().name, "payload.bin");
219+
}
220+
221+
} // namespace livekit::test

0 commit comments

Comments
 (0)