Skip to content

Commit 485660c

Browse files
DataTrackStreamReadResponse has optional DataTrackStreamEOS field
1 parent 4951765 commit 485660c

4 files changed

Lines changed: 100 additions & 12 deletions

File tree

‎client-sdk-rust‎

‎include/livekit/data_track_stream.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
#include <condition_variable>
2020
#include <cstdint>
21-
#include <deque>
22-
#include <memory>
2321
#include <mutex>
2422
#include <optional>
2523

@@ -32,7 +30,8 @@ namespace livekit {
3230

3331
namespace proto {
3432
class FfiEvent;
35-
}
33+
class DataTrackStreamReadResponse;
34+
} // namespace proto
3635

3736
/**
3837
* Represents a pull-based stream of frames from a remote data track.
@@ -111,6 +110,12 @@ class LIVEKIT_API DataTrackStream {
111110
/// FFI event handler, called by FfiClient.
112111
void onFfiEvent(const proto::FfiEvent& event);
113112

113+
/// Handle the immediate response returned by a read request.
114+
void handleReadResponse(const proto::DataTrackStreamReadResponse& response);
115+
116+
/// Mark the stream failed due to an invalid FFI protocol response.
117+
[[noreturn]] void failProtocolError(const char* message);
118+
114119
/// Push a received DataTrackFrame to the internal storage.
115120
void pushFrame(DataTrackFrame&& frame);
116121

‎src/data_track_stream.cpp‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "livekit/data_track_stream.h"
1818

19+
#include <optional>
1920
#include <utility>
2021

2122
#include "data_track.pb.h"
@@ -27,6 +28,19 @@ namespace livekit {
2728

2829
using proto::FfiEvent;
2930

31+
namespace {
32+
33+
constexpr char kMissingReadResponseError[] = "DataTrackStream::read: FFI response missing data_track_stream_read";
34+
35+
std::optional<SubscribeDataTrackError> terminalErrorFromEos(const proto::DataTrackStreamEOS& eos) {
36+
if (!eos.has_error()) {
37+
return std::nullopt;
38+
}
39+
return SubscribeDataTrackError::fromProto(eos.error());
40+
}
41+
42+
} // namespace
43+
3044
DataTrackStream::~DataTrackStream() { close(); }
3145

3246
void DataTrackStream::init(FfiHandle subscription_handle) {
@@ -36,6 +50,9 @@ void DataTrackStream::init(FfiHandle subscription_handle) {
3650
}
3751

3852
bool DataTrackStream::read(DataTrackFrame& out) {
53+
proto::DataTrackStreamReadResponse read_response;
54+
bool missing_read_response = false;
55+
3956
{
4057
const std::scoped_lock<std::mutex> lock(mutex_);
4158
if (closed_ || eof_) {
@@ -50,9 +67,20 @@ bool DataTrackStream::read(DataTrackFrame& out) {
5067
proto::FfiRequest req;
5168
auto* msg = req.mutable_data_track_stream_read();
5269
msg->set_stream_handle(subscription_handle);
53-
FfiClient::instance().sendRequest(req);
70+
const proto::FfiResponse resp = FfiClient::instance().sendRequest(req);
71+
if (!resp.has_data_track_stream_read()) {
72+
missing_read_response = true;
73+
} else {
74+
read_response = resp.data_track_stream_read();
75+
}
76+
}
77+
78+
if (missing_read_response) {
79+
failProtocolError(kMissingReadResponseError);
5480
}
5581

82+
handleReadResponse(read_response);
83+
5684
std::unique_lock<std::mutex> lock(mutex_);
5785
cv_.wait(lock, [this] { return frame_.has_value() || eof_ || closed_; });
5886

@@ -113,15 +141,23 @@ void DataTrackStream::onFfiEvent(const FfiEvent& event) {
113141
DataTrackFrame frame = DataTrackFrame::fromOwnedInfo(fr);
114142
pushFrame(std::move(frame));
115143
} else if (dts.has_eos()) {
116-
std::optional<SubscribeDataTrackError> error;
117-
const auto& eos = dts.eos();
118-
if (eos.has_error()) {
119-
error = SubscribeDataTrackError::fromProto(eos.error());
120-
}
121-
pushEos(std::move(error));
144+
pushEos(terminalErrorFromEos(dts.eos()));
122145
}
123146
}
124147

148+
void DataTrackStream::handleReadResponse(const proto::DataTrackStreamReadResponse& response) {
149+
if (!response.has_eos_event()) {
150+
return;
151+
}
152+
pushEos(terminalErrorFromEos(response.eos_event()));
153+
}
154+
155+
void DataTrackStream::failProtocolError(const char* message) {
156+
LK_LOG_ERROR("{}", message);
157+
pushEos(SubscribeDataTrackError{SubscribeDataTrackErrorCode::PROTOCOL_ERROR, message});
158+
close();
159+
}
160+
125161
void DataTrackStream::pushFrame(DataTrackFrame&& frame) {
126162
const std::scoped_lock<std::mutex> lock(mutex_);
127163

@@ -141,7 +177,7 @@ void DataTrackStream::pushFrame(DataTrackFrame&& frame) {
141177
void DataTrackStream::pushEos(std::optional<SubscribeDataTrackError> error) {
142178
{
143179
const std::scoped_lock<std::mutex> lock(mutex_);
144-
if (eof_) {
180+
if (closed_ || eof_) {
145181
return;
146182
}
147183
eof_ = true;

‎src/tests/unit/test_data_track_stream.cpp‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class DataTrackStreamTest : public ::testing::Test {
3333

3434
static void pushEvent(DataTrackStream& stream, const proto::FfiEvent& event) { stream.onFfiEvent(event); }
3535

36+
static void handleReadResponse(DataTrackStream& stream, const proto::DataTrackStreamReadResponse& response) {
37+
stream.handleReadResponse(response);
38+
}
39+
40+
static void failProtocolError(DataTrackStream& stream, const char* message) { stream.failProtocolError(message); }
41+
3642
static proto::FfiEvent makeEosEvent(std::optional<proto::SubscribeDataTrackErrorCode> code = std::nullopt,
3743
const std::string& message = {}) {
3844
proto::FfiEvent event;
@@ -47,6 +53,18 @@ class DataTrackStreamTest : public ::testing::Test {
4753
return event;
4854
}
4955

56+
static proto::DataTrackStreamReadResponse makeEosReadResponse(
57+
std::optional<proto::SubscribeDataTrackErrorCode> code = std::nullopt, const std::string& message = {}) {
58+
proto::DataTrackStreamReadResponse response;
59+
auto* eos = response.mutable_eos_event();
60+
if (code.has_value()) {
61+
auto* error = eos->mutable_error();
62+
error->set_code(code.value());
63+
error->set_message(message);
64+
}
65+
return response;
66+
}
67+
5068
static proto::FfiEvent makeAudioStreamEvent() {
5169
proto::FfiEvent event;
5270
event.mutable_audio_stream_event()->set_stream_handle(0);
@@ -71,6 +89,15 @@ TEST_F(DataTrackStreamTest, TerminalErrorEmptyForNormalEos) {
7189
EXPECT_FALSE(stream->terminalError().has_value());
7290
}
7391

92+
TEST_F(DataTrackStreamTest, ReadResponseNormalEosEndsStreamWithoutTerminalError) {
93+
auto stream = makeStream();
94+
handleReadResponse(*stream, makeEosReadResponse());
95+
96+
DataTrackFrame frame;
97+
EXPECT_FALSE(stream->read(frame));
98+
EXPECT_FALSE(stream->terminalError().has_value());
99+
}
100+
74101
TEST_F(DataTrackStreamTest, TerminalErrorStoredForSubscribeFailureEos) {
75102
auto stream = makeStream();
76103
pushEvent(*stream, makeEosEvent(proto::SUBSCRIBE_DATA_TRACK_ERROR_CODE_UNPUBLISHED,
@@ -85,6 +112,26 @@ TEST_F(DataTrackStreamTest, TerminalErrorStoredForSubscribeFailureEos) {
85112
EXPECT_EQ(error->message, "track unpublished before subscription completed");
86113
}
87114

115+
TEST_F(DataTrackStreamTest, ReadResponseSubscribeFailureEosStoresTerminalError) {
116+
auto stream = makeStream();
117+
handleReadResponse(*stream, makeEosReadResponse(proto::SUBSCRIBE_DATA_TRACK_ERROR_CODE_UNPUBLISHED,
118+
"track unpublished before read completed"));
119+
120+
DataTrackFrame frame;
121+
EXPECT_FALSE(stream->read(frame));
122+
expectTerminalError(*stream, SubscribeDataTrackErrorCode::UNPUBLISHED, "track unpublished before read completed");
123+
}
124+
125+
TEST_F(DataTrackStreamTest, ProtocolErrorClosesStreamAndStoresTerminalError) {
126+
auto stream = makeStream();
127+
128+
EXPECT_NO_THROW(failProtocolError(*stream, "malformed FFI response"));
129+
130+
DataTrackFrame frame;
131+
EXPECT_FALSE(stream->read(frame));
132+
expectTerminalError(*stream, SubscribeDataTrackErrorCode::PROTOCOL_ERROR, "malformed FFI response");
133+
}
134+
88135
TEST_F(DataTrackStreamTest, CloseBeforeEosSuppressesLaterTerminalError) {
89136
auto stream = makeStream();
90137
stream->close();

0 commit comments

Comments
 (0)