Skip to content

Commit 67b3c5a

Browse files
tighter guards around capture future and tests
1 parent a5682f0 commit 67b3c5a

4 files changed

Lines changed: 93 additions & 10 deletions

File tree

‎include/livekit/capture/capture_source.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ class LIVEKIT_API CaptureSource {
294294
/// successful @ref start().
295295
///
296296
/// @return A future containing the terminal result and captured frame count.
297-
/// @throws CaptureSourceError When awaiting the future if the FFI request is
298-
/// cancelled.
297+
/// @throws CaptureSourceError When awaiting the future if the FFI request
298+
/// fails, its result is invalid, or SDK shutdown cancels the operation.
299299
std::future<CaptureResult> join();
300300

301301
private:

‎src/ffi_client.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,10 @@ std::future<capture::CaptureResult> FfiClient::joinCaptureAsync(
14151415
},
14161416
[](const proto::FfiEvent& event, std::promise<capture::CaptureResult>& pr) {
14171417
const auto& callback = event.join_capture();
1418+
if (!callback.IsInitialized() || (!callback.has_error() && !callback.has_finished())) {
1419+
pr.set_exception(std::make_exception_ptr(capture::CaptureSourceError("Invalid capture join result")));
1420+
return;
1421+
}
14181422
capture::CaptureResult result;
14191423
if (callback.has_error()) {
14201424
const auto kind = captureErrorKindFromProto(callback.error().kind());
@@ -1443,6 +1447,9 @@ std::future<capture::CaptureResult> FfiClient::joinCaptureAsync(
14431447
if (!response.has_join_capture()) {
14441448
logAndThrow("FfiResponse missing join_capture");
14451449
}
1450+
if (!response.join_capture().IsInitialized() || response.join_capture().async_id() != async_id) {
1451+
logAndThrow("FfiResponse join_capture async_id does not match request");
1452+
}
14461453
} catch (...) {
14471454
cancelPendingByAsyncId(async_id);
14481455
throw;

‎src/ffi_client.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ class LIVEKIT_INTERNAL_API FfiClient {
181181
std::future<std::shared_ptr<capture::CaptureSource>> newCaptureSourceAsync(
182182
proto::NewCaptureSourceRequest request,
183183
const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send = {});
184+
// Rust retains the pump even after it exits; joining requests its terminal
185+
// result rather than subscribing to an unsolicited completion event.
184186
std::future<capture::CaptureResult> joinCaptureAsync(
185187
std::uint64_t capture_handle, const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send = {});
186188
#endif

‎src/tests/unit/test_capture_source.cpp‎

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ TEST_F(CaptureSourceCallbackTest, CallbackReturnsWrapperAndOwnsVideoSource) {
156156
EXPECT_EQ(source->width(), 1280);
157157
EXPECT_EQ(source->height(), 720);
158158
EXPECT_EQ(FfiClientTestAccess::listenerCount(), 0u);
159-
std::weak_ptr<capture::CaptureSource> weak_source = source;
160-
std::weak_ptr<VideoSource> weak_video = source->videoSource();
159+
const std::weak_ptr<capture::CaptureSource> weak_source = source;
160+
const std::weak_ptr<VideoSource> weak_video = source->videoSource();
161161
source.reset();
162162
EXPECT_TRUE(weak_source.expired());
163163
EXPECT_TRUE(weak_video.expired());
@@ -260,6 +260,63 @@ TEST_F(CaptureSourceCallbackTest, JoinCallbackMapsFinishedResult) {
260260
EXPECT_EQ(FfiClientTestAccess::pendingCount(), 0u);
261261
}
262262

263+
TEST_F(CaptureSourceCallbackTest, JoinCallbackMayCompleteBeforeSendReturns) {
264+
auto future = FfiClient::instance().joinCaptureAsync(42, [](const proto::FfiRequest& request) {
265+
const auto async_id = request.join_capture().request_async_id();
266+
emitFfiEvent(joinFinishedEvent(async_id, 3));
267+
proto::FfiResponse response;
268+
response.mutable_join_capture()->set_async_id(async_id);
269+
return response;
270+
});
271+
ASSERT_EQ(future.wait_for(std::chrono::seconds(0)), std::future_status::ready);
272+
const auto result = future.get();
273+
EXPECT_FALSE(result.error.has_value());
274+
EXPECT_EQ(result.frames_captured, 3u);
275+
EXPECT_EQ(result.exit, capture::CaptureExit::Stopped);
276+
EXPECT_EQ(FfiClientTestAccess::pendingCount(), 0u);
277+
}
278+
279+
TEST_F(CaptureSourceCallbackTest, JoinIgnoresUnrelatedCallbacksAndDuplicateCompletion) {
280+
auto future = beginJoin();
281+
emitFfiEvent(joinFinishedEvent(async_id_ + 1, 99));
282+
proto::FfiEvent unrelated;
283+
unrelated.mutable_new_capture_source()->set_async_id(async_id_);
284+
unrelated.mutable_new_capture_source()->set_error("unrelated creation failure");
285+
emitFfiEvent(unrelated);
286+
EXPECT_EQ(future.wait_for(std::chrono::seconds(0)), std::future_status::timeout);
287+
EXPECT_EQ(FfiClientTestAccess::pendingCount(), 1u);
288+
289+
emitFfiEvent(joinFinishedEvent(async_id_, 7));
290+
emitFfiEvent(joinFinishedEvent(async_id_, 99));
291+
ASSERT_EQ(future.wait_for(std::chrono::seconds(0)), std::future_status::ready);
292+
EXPECT_EQ(future.get().frames_captured, 7u);
293+
EXPECT_EQ(FfiClientTestAccess::pendingCount(), 0u);
294+
}
295+
296+
TEST_F(CaptureSourceCallbackTest, JoinsCompleteIndependentlyInReverseOrder) {
297+
auto first = beginJoin(42);
298+
const auto first_async_id = async_id_;
299+
auto second = beginJoin(43);
300+
emitFfiEvent(joinFinishedEvent(async_id_, 2));
301+
EXPECT_EQ(first.wait_for(std::chrono::seconds(0)), std::future_status::timeout);
302+
ASSERT_EQ(second.wait_for(std::chrono::seconds(0)), std::future_status::ready);
303+
EXPECT_EQ(second.get().frames_captured, 2u);
304+
emitFfiEvent(joinFinishedEvent(first_async_id, 1));
305+
ASSERT_EQ(first.wait_for(std::chrono::seconds(0)), std::future_status::ready);
306+
EXPECT_EQ(first.get().frames_captured, 1u);
307+
EXPECT_EQ(FfiClientTestAccess::pendingCount(), 0u);
308+
}
309+
310+
TEST_F(CaptureSourceCallbackTest, JoinMissingTerminalResultUsesCaptureSourceError) {
311+
auto future = beginJoin();
312+
proto::FfiEvent event;
313+
event.mutable_join_capture()->set_async_id(async_id_);
314+
emitFfiEvent(event);
315+
ASSERT_EQ(future.wait_for(std::chrono::seconds(0)), std::future_status::ready);
316+
EXPECT_THROW(static_cast<void>(future.get()), capture::CaptureSourceError);
317+
EXPECT_EQ(FfiClientTestAccess::pendingCount(), 0u);
318+
}
319+
263320
TEST_F(CaptureSourceCallbackTest, JoinCallbackMapsPumpError) {
264321
const auto verify_kind = [this](proto::CaptureErrorKind proto_kind, capture::CaptureErrorKind expected_kind) {
265322
auto future = beginJoin();
@@ -303,6 +360,21 @@ TEST_F(CaptureSourceCallbackTest, JoinMissingAcknowledgementRemovesPendingOperat
303360
EXPECT_EQ(FfiClientTestAccess::pendingCount(), 0u);
304361
}
305362

363+
TEST_F(CaptureSourceCallbackTest, JoinInvalidAcknowledgementRemovesPendingOperation) {
364+
for (const bool missing_id : {false, true}) {
365+
const auto send = [missing_id](const proto::FfiRequest& request) {
366+
proto::FfiResponse response;
367+
auto* acknowledgement = response.mutable_join_capture();
368+
if (!missing_id) {
369+
acknowledgement->set_async_id(request.join_capture().request_async_id() + 1);
370+
}
371+
return response;
372+
};
373+
EXPECT_THROW(FfiClient::instance().joinCaptureAsync(42, send), std::runtime_error);
374+
EXPECT_EQ(FfiClientTestAccess::pendingCount(), 0u);
375+
}
376+
}
377+
306378
TEST_F(CaptureSourceCallbackTest, JoinApiReportsSynchronousSendFailureThroughFuture) {
307379
auto source = completeCreation(makeOwned());
308380
FfiClient::instance().shutdown();
@@ -370,17 +442,19 @@ TEST_F(CaptureSourceCallbackTest, RecommendationsCannotReplaceOtherApplicationOp
370442
EXPECT_EQ(options.stream, application.stream);
371443
EXPECT_EQ(options.source, application.source);
372444
ASSERT_TRUE(options.video_encoding.has_value());
373-
EXPECT_EQ(options.video_encoding->max_bitrate, 2000000u);
374-
EXPECT_EQ(options.video_encoding->max_framerate, 30);
445+
const auto video_encoding = options.video_encoding.value_or(VideoEncodingOptions{});
446+
EXPECT_EQ(video_encoding.max_bitrate, 2000000u);
447+
EXPECT_EQ(video_encoding.max_framerate, 30);
375448
ASSERT_TRUE(options.audio_encoding.has_value());
376-
EXPECT_EQ(options.audio_encoding->max_bitrate, 64000u);
449+
EXPECT_EQ(options.audio_encoding.value_or(AudioEncodingOptions{}).max_bitrate, 64000u);
377450
EXPECT_EQ(options.dtx, application.dtx);
378451
EXPECT_EQ(options.red, application.red);
379452
EXPECT_EQ(options.preconnect_buffer, application.preconnect_buffer);
380453
ASSERT_TRUE(options.frame_metadata_features.has_value());
381-
EXPECT_TRUE(options.frame_metadata_features->user_timestamp);
382-
EXPECT_TRUE(options.frame_metadata_features->frame_id);
383-
EXPECT_TRUE(options.frame_metadata_features->user_data);
454+
const auto metadata_features = options.frame_metadata_features.value_or(FrameMetadataFeatures{});
455+
EXPECT_TRUE(metadata_features.user_timestamp);
456+
EXPECT_TRUE(metadata_features.frame_id);
457+
EXPECT_TRUE(metadata_features.user_data);
384458
EXPECT_EQ(options.degradation_preference, application.degradation_preference);
385459
}
386460
}

0 commit comments

Comments
 (0)