Skip to content

Commit b95e4b0

Browse files
DataTrackStreamReadResponse (#130)
1 parent 8f570b7 commit b95e4b0

3 files changed

Lines changed: 101 additions & 12 deletions

File tree

‎include/livekit/data_track_stream.h‎

Lines changed: 9 additions & 4 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+
void failProtocolError(const char* message);
118+
114119
/// Push a received DataTrackFrame to the internal storage.
115120
void pushFrame(DataTrackFrame&& frame);
116121

@@ -143,7 +148,7 @@ class LIVEKIT_API DataTrackStream {
143148
FfiHandle subscription_handle_;
144149

145150
/** FfiClient listener id for routing FfiEvent callbacks to this object. */
146-
std::int32_t listener_id_{0};
151+
std::int32_t listener_id_{-1};
147152
};
148153

149154
} // namespace livekit

‎src/data_track_stream.cpp‎

Lines changed: 45 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,21 @@ 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);
80+
return false;
5481
}
5582

83+
handleReadResponse(read_response);
84+
5685
std::unique_lock<std::mutex> lock(mutex_);
5786
cv_.wait(lock, [this] { return frame_.has_value() || eof_ || closed_; });
5887

@@ -113,13 +142,21 @@ void DataTrackStream::onFfiEvent(const FfiEvent& event) {
113142
DataTrackFrame frame = DataTrackFrame::fromOwnedInfo(fr);
114143
pushFrame(std::move(frame));
115144
} 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));
145+
pushEos(terminalErrorFromEos(dts.eos()));
146+
}
147+
}
148+
149+
void DataTrackStream::handleReadResponse(const proto::DataTrackStreamReadResponse& response) {
150+
if (!response.has_eos_event()) {
151+
return;
122152
}
153+
pushEos(terminalErrorFromEos(response.eos_event()));
154+
}
155+
156+
void DataTrackStream::failProtocolError(const char* message) {
157+
LK_LOG_ERROR("{}", message);
158+
pushEos(SubscribeDataTrackError{SubscribeDataTrackErrorCode::PROTOCOL_ERROR, message});
159+
close();
123160
}
124161

125162
void DataTrackStream::pushFrame(DataTrackFrame&& frame) {
@@ -141,7 +178,7 @@ void DataTrackStream::pushFrame(DataTrackFrame&& frame) {
141178
void DataTrackStream::pushEos(std::optional<SubscribeDataTrackError> error) {
142179
{
143180
const std::scoped_lock<std::mutex> lock(mutex_);
144-
if (eof_) {
181+
if (closed_ || eof_) {
145182
return;
146183
}
147184
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)