Skip to content

Commit 2cd9048

Browse files
committed
Add capture source integration test
1 parent c08507e commit 2cd9048

1 file changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright 2026 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an “AS IS” BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/// End-to-end test for capture sources (livekit-capture over FFI).
18+
///
19+
/// Publishes the built-in demo source — the capture pump pushes frames
20+
/// server-side, so the test drives no frames itself — and verifies that a
21+
/// second participant receives real video through the SFU.
22+
///
23+
/// Requires the Rust FFI built with the `capture` feature
24+
/// (-DLIVEKIT_ENABLE_CAPTURE=ON); otherwise the test skips.
25+
26+
#include <chrono>
27+
#include <condition_variable>
28+
#include <mutex>
29+
#include <optional>
30+
#include <string>
31+
32+
#include "../common/test_common.h"
33+
#include "livekit/capture_source.h"
34+
35+
namespace livekit::test {
36+
37+
using namespace std::chrono_literals;
38+
39+
namespace {
40+
41+
constexpr int kMinFramesReceived = 5;
42+
43+
/// Creates the capture source, or skips the test when the FFI library was
44+
/// built without the capture feature.
45+
std::shared_ptr<CaptureSource> createDemoCaptureOrSkip() {
46+
try {
47+
return CaptureSource::create(DemoVideoSourceConfig{}).get();
48+
} catch (const std::exception &e) {
49+
if (std::string(e.what()).find("without the 'capture' feature") !=
50+
std::string::npos) {
51+
return nullptr;
52+
}
53+
throw;
54+
}
55+
}
56+
57+
} // namespace
58+
59+
class CaptureSourceServerTest : public LiveKitTestBase {};
60+
61+
TEST_F(CaptureSourceServerTest, DemoCaptureSourcePublishesFramesEndToEnd) {
62+
failIfNotConfigured();
63+
64+
auto capture = createDemoCaptureOrSkip();
65+
if (capture == nullptr) {
66+
GTEST_SKIP() << "livekit-ffi built without the 'capture' feature; "
67+
"configure with -DLIVEKIT_ENABLE_CAPTURE=ON";
68+
}
69+
70+
// The demo source produces 1280x720 pixel frames.
71+
ASSERT_EQ(capture->kind(), CaptureSourceKind::Pixel);
72+
ASSERT_EQ(capture->width(), 1280);
73+
ASSERT_EQ(capture->height(), 720);
74+
ASSERT_FALSE(capture->codec().has_value());
75+
ASSERT_NE(capture->videoSource(), nullptr);
76+
77+
Room sender_room;
78+
Room receiver_room;
79+
RoomOptions options;
80+
81+
ASSERT_TRUE(receiver_room.connect(config_.url, config_.token_b, options));
82+
ASSERT_TRUE(sender_room.connect(config_.url, config_.token_a, options));
83+
ASSERT_FALSE(sender_room.localParticipant().expired());
84+
ASSERT_FALSE(receiver_room.localParticipant().expired());
85+
86+
const std::string sender_identity =
87+
lockLocalParticipant(sender_room)->identity();
88+
ASSERT_FALSE(sender_identity.empty());
89+
ASSERT_TRUE(waitForParticipant(&receiver_room, sender_identity, 10s));
90+
91+
// Count frames arriving at the receiver: proof of media flowing through
92+
// the SFU without the test pushing a single frame.
93+
std::mutex mutex;
94+
std::condition_variable cv;
95+
int frames_received = 0;
96+
97+
const std::string track_name = "demo-capture-track";
98+
receiver_room.setOnVideoFrameEventCallback(
99+
sender_identity, track_name,
100+
[&mutex, &cv, &frames_received](const VideoFrameEvent & /*event*/) {
101+
std::lock_guard<std::mutex> lock(mutex);
102+
if (++frames_received >= kMinFramesReceived) {
103+
cv.notify_all();
104+
}
105+
});
106+
107+
// Publish a track backed by the capture source's RTC video source,
108+
// merging application options over the source-derived ones.
109+
auto track = LocalVideoTrack::createLocalVideoTrack(track_name,
110+
capture->videoSource());
111+
112+
// Application options are merged in; source-dictated fields win.
113+
TrackPublishOptions app_options;
114+
app_options.source = TrackSource::SOURCE_CAMERA;
115+
ASSERT_NO_THROW(lockLocalParticipant(sender_room)
116+
->publishTrack(track, capture->publishOptions(app_options)));
117+
118+
// Observe the capture's terminal event.
119+
std::optional<CaptureResult> finished;
120+
capture->setOnFinishedCallback([&mutex, &cv, &finished](const CaptureResult &result) {
121+
std::lock_guard<std::mutex> lock(mutex);
122+
finished = result;
123+
cv.notify_all();
124+
});
125+
126+
ASSERT_NO_THROW(capture->start());
127+
EXPECT_THROW(capture->start(), CaptureSourceError)
128+
<< "double start must be rejected";
129+
130+
{
131+
std::unique_lock<std::mutex> lock(mutex);
132+
const bool got_frames = cv.wait_for(lock, 30s, [&frames_received] {
133+
return frames_received >= kMinFramesReceived;
134+
});
135+
ASSERT_TRUE(got_frames)
136+
<< "Timed out waiting for demo capture frames; received "
137+
<< frames_received;
138+
}
139+
140+
// Stop is a signal; the terminal callback delivers the stats.
141+
ASSERT_NO_THROW(capture->stop());
142+
{
143+
std::unique_lock<std::mutex> lock(mutex);
144+
const bool got_finished =
145+
cv.wait_for(lock, 10s, [&finished] { return finished.has_value(); });
146+
ASSERT_TRUE(got_finished)
147+
<< "Timed out waiting for the capture finished callback";
148+
}
149+
150+
ASSERT_FALSE(finished->error.has_value()) << *finished->error;
151+
EXPECT_EQ(finished->exit, CaptureExit::Stopped);
152+
EXPECT_GT(finished->frames_captured, 0u);
153+
154+
receiver_room.clearOnVideoFrameCallback(sender_identity, track_name);
155+
if (track->publication()) {
156+
lockLocalParticipant(sender_room)
157+
->unpublishTrack(track->publication()->sid());
158+
}
159+
}
160+
161+
} // namespace livekit::test

0 commit comments

Comments
 (0)