Skip to content

Commit 078b316

Browse files
DataTrackStreamReadResponse has optional DataTrackStreamEOS field
1 parent 82ee340 commit 078b316

4 files changed

Lines changed: 74 additions & 12 deletions

File tree

‎client-sdk-rust‎

‎include/livekit/data_track_stream.h‎

Lines changed: 5 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,9 @@ 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+
114116
/// Push a received DataTrackFrame to the internal storage.
115117
void pushFrame(DataTrackFrame&& frame);
116118

‎src/data_track_stream.cpp‎

Lines changed: 33 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,17 @@ namespace livekit {
2728

2829
using proto::FfiEvent;
2930

31+
namespace {
32+
33+
std::optional<SubscribeDataTrackError> terminalErrorFromEos(const proto::DataTrackStreamEOS& eos) {
34+
if (!eos.has_error()) {
35+
return std::nullopt;
36+
}
37+
return SubscribeDataTrackError::fromProto(eos.error());
38+
}
39+
40+
} // namespace
41+
3042
DataTrackStream::~DataTrackStream() { close(); }
3143

3244
void DataTrackStream::init(FfiHandle subscription_handle) {
@@ -36,6 +48,8 @@ void DataTrackStream::init(FfiHandle subscription_handle) {
3648
}
3749

3850
bool DataTrackStream::read(DataTrackFrame& out) {
51+
proto::DataTrackStreamReadResponse read_response;
52+
3953
{
4054
const std::scoped_lock<std::mutex> lock(mutex_);
4155
if (closed_ || eof_) {
@@ -50,9 +64,18 @@ bool DataTrackStream::read(DataTrackFrame& out) {
5064
proto::FfiRequest req;
5165
auto* msg = req.mutable_data_track_stream_read();
5266
msg->set_stream_handle(subscription_handle);
53-
FfiClient::instance().sendRequest(req);
67+
const proto::FfiResponse resp = FfiClient::instance().sendRequest(req);
68+
if (!resp.has_data_track_stream_read()) {
69+
// should we throw here? This would mean the proto is mismatched
70+
LK_LOG_ERROR("DataTrackStream::read: FFI response missing data_track_stream_read");
71+
return false;
72+
}
73+
74+
read_response = resp.data_track_stream_read();
5475
}
5576

77+
handleReadResponse(read_response);
78+
5679
std::unique_lock<std::mutex> lock(mutex_);
5780
cv_.wait(lock, [this] { return frame_.has_value() || eof_ || closed_; });
5881

@@ -113,13 +136,15 @@ void DataTrackStream::onFfiEvent(const FfiEvent& event) {
113136
DataTrackFrame frame = DataTrackFrame::fromOwnedInfo(fr);
114137
pushFrame(std::move(frame));
115138
} 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));
139+
pushEos(terminalErrorFromEos(dts.eos()));
140+
}
141+
}
142+
143+
void DataTrackStream::handleReadResponse(const proto::DataTrackStreamReadResponse& response) {
144+
if (!response.has_eos_event()) {
145+
return;
122146
}
147+
pushEos(terminalErrorFromEos(response.eos_event()));
123148
}
124149

125150
void DataTrackStream::pushFrame(DataTrackFrame&& frame) {
@@ -141,7 +166,7 @@ void DataTrackStream::pushFrame(DataTrackFrame&& frame) {
141166
void DataTrackStream::pushEos(std::optional<SubscribeDataTrackError> error) {
142167
{
143168
const std::scoped_lock<std::mutex> lock(mutex_);
144-
if (eof_) {
169+
if (closed_ || eof_) {
145170
return;
146171
}
147172
eof_ = true;

‎src/tests/unit/test_data_track_stream.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ 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+
3640
static proto::FfiEvent makeEosEvent(std::optional<proto::SubscribeDataTrackErrorCode> code = std::nullopt,
3741
const std::string& message = {}) {
3842
proto::FfiEvent event;
@@ -47,6 +51,18 @@ class DataTrackStreamTest : public ::testing::Test {
4751
return event;
4852
}
4953

54+
static proto::DataTrackStreamReadResponse makeEosReadResponse(
55+
std::optional<proto::SubscribeDataTrackErrorCode> code = std::nullopt, const std::string& message = {}) {
56+
proto::DataTrackStreamReadResponse response;
57+
auto* eos = response.mutable_eos_event();
58+
if (code.has_value()) {
59+
auto* error = eos->mutable_error();
60+
error->set_code(code.value());
61+
error->set_message(message);
62+
}
63+
return response;
64+
}
65+
5066
static proto::FfiEvent makeAudioStreamEvent() {
5167
proto::FfiEvent event;
5268
event.mutable_audio_stream_event()->set_stream_handle(0);
@@ -71,6 +87,15 @@ TEST_F(DataTrackStreamTest, TerminalErrorEmptyForNormalEos) {
7187
EXPECT_FALSE(stream->terminalError().has_value());
7288
}
7389

90+
TEST_F(DataTrackStreamTest, ReadResponseNormalEosEndsStreamWithoutTerminalError) {
91+
auto stream = makeStream();
92+
handleReadResponse(*stream, makeEosReadResponse());
93+
94+
DataTrackFrame frame;
95+
EXPECT_FALSE(stream->read(frame));
96+
EXPECT_FALSE(stream->terminalError().has_value());
97+
}
98+
7499
TEST_F(DataTrackStreamTest, TerminalErrorStoredForSubscribeFailureEos) {
75100
auto stream = makeStream();
76101
pushEvent(*stream, makeEosEvent(proto::SUBSCRIBE_DATA_TRACK_ERROR_CODE_UNPUBLISHED,
@@ -85,6 +110,16 @@ TEST_F(DataTrackStreamTest, TerminalErrorStoredForSubscribeFailureEos) {
85110
EXPECT_EQ(error->message, "track unpublished before subscription completed");
86111
}
87112

113+
TEST_F(DataTrackStreamTest, ReadResponseSubscribeFailureEosStoresTerminalError) {
114+
auto stream = makeStream();
115+
handleReadResponse(*stream, makeEosReadResponse(proto::SUBSCRIBE_DATA_TRACK_ERROR_CODE_UNPUBLISHED,
116+
"track unpublished before read completed"));
117+
118+
DataTrackFrame frame;
119+
EXPECT_FALSE(stream->read(frame));
120+
expectTerminalError(*stream, SubscribeDataTrackErrorCode::UNPUBLISHED, "track unpublished before read completed");
121+
}
122+
88123
TEST_F(DataTrackStreamTest, CloseBeforeEosSuppressesLaterTerminalError) {
89124
auto stream = makeStream();
90125
stream->close();

0 commit comments

Comments
 (0)