Skip to content

Commit 3757652

Browse files
alan-george-lkstephen-derosa
authored andcommitted
Integrate capture error type for enum
1 parent ab79c0f commit 3757652

4 files changed

Lines changed: 62 additions & 14 deletions

File tree

‎include/livekit/capture/capture_source.h‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,29 @@ enum class CaptureExit {
151151
EndOfStream = 1,
152152
};
153153

154+
/// @brief Why a capture ended with an error.
155+
enum class CaptureErrorKind {
156+
/// @brief The capture source failed or violated its contract.
157+
Source = 0,
158+
/// @brief The RTC video source rejected a frame.
159+
Rtc = 1,
160+
/// @brief The pump thread panicked.
161+
Panicked = 2,
162+
};
163+
164+
/// @brief Terminal capture failure.
165+
struct CaptureError {
166+
/// @brief Human-readable error message.
167+
std::string message;
168+
169+
/// @brief Error category.
170+
CaptureErrorKind kind = CaptureErrorKind::Source;
171+
};
172+
154173
/// @brief Terminal result of a started capture.
155174
struct CaptureResult {
156-
/// @brief Error message when the capture failed; empty on success.
157-
std::optional<std::string> error;
175+
/// @brief Failure details when the capture failed; empty on success.
176+
std::optional<CaptureError> error;
158177

159178
/// @brief Number of frames or access units captured.
160179
std::uint64_t frames_captured = 0;

‎src/ffi_client.cpp‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,22 @@ std::future<Result<std::string, std::string>> FfiClient::getSchemaAsync(std::uin
13391339

13401340
#if LIVEKIT_CAPTURE_ENABLED
13411341

1342+
namespace {
1343+
1344+
std::optional<capture::CaptureErrorKind> captureErrorKindFromProto(proto::CaptureErrorKind kind) {
1345+
switch (kind) {
1346+
case proto::CAPTURE_ERROR_KIND_SOURCE:
1347+
return capture::CaptureErrorKind::Source;
1348+
case proto::CAPTURE_ERROR_KIND_RTC:
1349+
return capture::CaptureErrorKind::Rtc;
1350+
case proto::CAPTURE_ERROR_KIND_PANICKED:
1351+
return capture::CaptureErrorKind::Panicked;
1352+
}
1353+
return std::nullopt;
1354+
}
1355+
1356+
} // namespace
1357+
13421358
std::future<std::shared_ptr<capture::CaptureSource>> FfiClient::newCaptureSourceAsync(
13431359
proto::NewCaptureSourceRequest request, const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send) {
13441360
// Generate client-side async_id first
@@ -1401,7 +1417,12 @@ std::future<capture::CaptureResult> FfiClient::joinCaptureAsync(
14011417
const auto& callback = event.join_capture();
14021418
capture::CaptureResult result;
14031419
if (callback.has_error()) {
1404-
result.error = callback.error().error();
1420+
const auto kind = captureErrorKindFromProto(callback.error().kind());
1421+
if (!kind) {
1422+
pr.set_exception(std::make_exception_ptr(capture::CaptureSourceError("Unknown capture error kind")));
1423+
return;
1424+
}
1425+
result.error = capture::CaptureError{callback.error().error(), *kind};
14051426
} else if (callback.has_finished()) {
14061427
result.frames_captured = callback.finished().frames_captured();
14071428
result.exit = callback.finished().exit() == proto::CaptureExit::CAPTURE_EXIT_END_OF_STREAM

‎src/tests/integration/test_capture_source.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ TEST_P(CaptureSourceServerTest, CaptureSourcePublishesFramesEndToEnd) {
136136
ASSERT_EQ(joined.wait_for(10s), std::future_status::ready) << "Timed out waiting for capture join";
137137
const auto finished = joined.get();
138138

139-
ASSERT_FALSE(finished.error.has_value()) << *finished.error;
139+
ASSERT_FALSE(finished.error.has_value()) << finished.error->message;
140140
EXPECT_EQ(finished.exit, capture::CaptureExit::Stopped);
141141
EXPECT_GT(finished.frames_captured, 0u);
142142
const auto capture_seconds =

‎src/tests/unit/test_capture_source.cpp‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,24 @@ TEST_F(CaptureSourceCallbackTest, JoinCallbackMapsFinishedResult) {
261261
}
262262

263263
TEST_F(CaptureSourceCallbackTest, JoinCallbackMapsPumpError) {
264-
auto future = beginJoin();
265-
proto::FfiEvent event;
266-
auto* callback = event.mutable_join_capture();
267-
callback->set_async_id(async_id_);
268-
callback->mutable_error()->set_error("capture failed");
269-
emitFfiEvent(event);
270-
271-
const auto result = future.get();
272-
ASSERT_TRUE(result.error.has_value());
273-
EXPECT_EQ(*result.error, "capture failed");
264+
const auto verify_kind = [this](proto::CaptureErrorKind proto_kind, capture::CaptureErrorKind expected_kind) {
265+
auto future = beginJoin();
266+
proto::FfiEvent event;
267+
auto* callback = event.mutable_join_capture();
268+
callback->set_async_id(async_id_);
269+
callback->mutable_error()->set_error("capture failed");
270+
callback->mutable_error()->set_kind(proto_kind);
271+
emitFfiEvent(event);
272+
273+
const auto result = future.get();
274+
ASSERT_TRUE(result.error.has_value());
275+
EXPECT_EQ(result.error->message, "capture failed");
276+
EXPECT_EQ(result.error->kind, expected_kind);
277+
};
278+
279+
verify_kind(proto::CAPTURE_ERROR_KIND_SOURCE, capture::CaptureErrorKind::Source);
280+
verify_kind(proto::CAPTURE_ERROR_KIND_RTC, capture::CaptureErrorKind::Rtc);
281+
verify_kind(proto::CAPTURE_ERROR_KIND_PANICKED, capture::CaptureErrorKind::Panicked);
274282
EXPECT_EQ(FfiClientTestAccess::pendingCount(), 0u);
275283
}
276284

0 commit comments

Comments
 (0)