Skip to content

Commit 64c57ec

Browse files
livekit::capture
1 parent 9d78b90 commit 64c57ec

10 files changed

Lines changed: 104 additions & 82 deletions

File tree

‎docs/capture-sources.md‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> **NOTE:** LiveKit Capture is not officially supported on Intel-based Macs.
44
5-
`CaptureSource` connects a Rust video producer to a regular C++ `VideoSource`.
5+
`livekit::capture::CaptureSource` connects a Rust video producer to a regular C++ `VideoSource`.
66
The capture pump runs in Rust, so C++ does not send one FFI request per frame.
77
The SDK supports three source configurations:
88

@@ -26,7 +26,8 @@ Native camera device capture and device enumeration are not part of this API.
2626
For build flags, release bundle naming, GStreamer prerequisites, and runtime
2727
requirements, see [Building — Capture sources](building.md#capture-sources).
2828

29-
Capture-enabled bundles install the capture API in `include/livekit/capture/`:
29+
Capture types live in the `livekit::capture` namespace. Capture-enabled bundles
30+
install the API in `include/livekit/capture/`:
3031

3132
```cpp
3233
#include <livekit/capture/capture_source.h>
@@ -50,8 +51,8 @@ continues, and the source is released when the callback arrives.
5051
#include <livekit/livekit.h>
5152

5253
// The SDK is initialized, and room is connected.
53-
auto pending = livekit::CaptureSource::create(
54-
livekit::PatternVideoSourceConfig{{1280, 720}, 30, livekit::Pattern::Gradient});
54+
auto pending = livekit::capture::CaptureSource::create(
55+
livekit::capture::PatternVideoSourceConfig{{1280, 720}, 30, livekit::capture::Pattern::Gradient});
5556
// Other application setup can run while the producer is being constructed.
5657
auto capture = pending.get();
5758
// For a wall clock, use ClockVideoSourceConfig{{1280, 720}, 30} instead.
@@ -63,7 +64,7 @@ if (!participant) {
6364
}
6465
participant->publishTrack(track, capture->publishOptions());
6566

66-
capture->setOnFinishedCallback([](const livekit::CaptureResult& result) {
67+
capture->setOnFinishedCallback([](const livekit::capture::CaptureResult& result) {
6768
// Runs on the FFI callback thread. Offload work that would block.
6869
});
6970
if (!capture->start()) {
@@ -81,12 +82,12 @@ Provide `appsink name=lk_appsink`, or leave exactly one encoded video source pad
8182
unlinked so the source can attach its own sink:
8283

8384
```cpp
84-
livekit::GstreamerVideoSourceConfig config;
85+
livekit::capture::GstreamerVideoSourceConfig config;
8586
config.pipeline = "videotestsrc ! video/x-raw,width=1280,height=720,framerate=30/1 ! "
8687
"vp8enc name=lk_encoder deadline=1 ! appsink name=lk_appsink";
87-
config.rate_control = livekit::GstreamerRateControl{
88-
"lk_encoder", "target-bitrate", livekit::GstreamerBitrateUnit::Bps};
89-
auto capture = livekit::CaptureSource::create(config).get();
88+
config.rate_control = livekit::capture::GstreamerRateControl{
89+
"lk_encoder", "target-bitrate", livekit::capture::GstreamerBitrateUnit::Bps};
90+
auto capture = livekit::capture::CaptureSource::create(config).get();
9091
```
9192
9293
The optional rate-control binding forwards WebRTC bitrate targets to the named

‎include/livekit/capture/capture_source.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@
3737

3838
namespace livekit {
3939

40+
class FfiClient;
41+
4042
namespace proto {
4143
class OwnedCaptureSource;
4244
} // namespace proto
4345

46+
} // namespace livekit
47+
48+
/// @brief Capture sources and their configuration types.
49+
namespace livekit::capture {
50+
4451
/// @brief Error raised when capture source creation or control fails.
4552
class LIVEKIT_API CaptureSourceError : public std::runtime_error {
4653
public:
@@ -269,7 +276,7 @@ class LIVEKIT_API CaptureSource {
269276
bool stop();
270277

271278
private:
272-
friend class FfiClient;
279+
friend class livekit::FfiClient;
273280

274281
CaptureSource();
275282

@@ -280,4 +287,4 @@ class LIVEKIT_API CaptureSource {
280287
std::unique_ptr<Impl> impl_;
281288
};
282289

283-
} // namespace livekit
290+
} // namespace livekit::capture

‎include/livekit/video_source.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
namespace livekit {
2727

28+
namespace capture {
29+
class CaptureSource;
30+
} // namespace capture
31+
2832
class VideoFrame;
2933
class EncodedVideoSource;
3034

@@ -97,12 +101,15 @@ class LIVEKIT_API VideoSource {
97101

98102
private:
99103
friend class EncodedVideoSource;
100-
friend class CaptureSource;
104+
friend class capture::CaptureSource;
101105

102106
enum class SourceType { Native, Encoded };
103107
VideoSource(int width, int height, SourceType source_type);
104108

105-
/// Adopt an existing FFI video source handle (used by CaptureSource).
109+
/// @brief Adopt an existing FFI video source handle for capture sources.
110+
/// @param handle Rust-owned video source handle.
111+
/// @param width Frame width in pixels.
112+
/// @param height Frame height in pixels.
106113
VideoSource(FfiHandle&& handle, int width, int height) noexcept
107114
: handle_(std::move(handle)), width_(width), height_(height) {}
108115

‎src/capture_source.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "ffi_client.h"
2727
#include "lk_log.h"
2828

29-
namespace livekit {
29+
namespace livekit::capture {
3030

3131
struct CaptureSource::Impl {
3232
FfiHandle handle;
@@ -271,4 +271,4 @@ bool controlCapture(std::uint64_t handle, bool start) {
271271
bool CaptureSource::start() { return controlCapture(impl_->handle.get(), true); }
272272
bool CaptureSource::stop() { return controlCapture(impl_->handle.get(), false); }
273273

274-
} // namespace livekit
274+
} // namespace livekit::capture

‎src/capture_source_internal.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
#include "livekit/capture/capture_source.h"
2020
#include "livekit/visibility.h"
2121

22-
namespace livekit {
23-
24-
namespace proto {
22+
namespace livekit::proto {
2523
class NewCaptureSourceRequest;
26-
} // namespace proto
24+
} // namespace livekit::proto
25+
26+
namespace livekit::capture {
2727

2828
/// Serialize capture configurations for the FFI request.
2929
/// Internal test seam; not part of the public SDK API.
3030
LIVEKIT_INTERNAL_API proto::NewCaptureSourceRequest toProto(const GstreamerVideoSourceConfig& config);
3131
LIVEKIT_INTERNAL_API proto::NewCaptureSourceRequest toProto(const PatternVideoSourceConfig& config);
3232
LIVEKIT_INTERNAL_API proto::NewCaptureSourceRequest toProto(const ClockVideoSourceConfig& config);
3333

34-
} // namespace livekit
34+
} // namespace livekit::capture

‎src/ffi_client.cpp‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ std::optional<FfiClient::AsyncId> ExtractAsyncId(const proto::FfiEvent& event) {
165165
case E::kTextStreamReaderEvent:
166166
case E::kDataTrackStreamEvent:
167167
case E::kRpcMethodInvocation:
168-
// Delivered to the owning CaptureSource's listener, not to a pending
169-
// request; see CaptureSource's capture-event handler.
168+
// Delivered to the owning capture::CaptureSource's listener, not to a pending
169+
// request; see capture::CaptureSource's capture-event handler.
170170
case E::kCaptureSourceEvent:
171171
case E::kLogs:
172172
case E::kPanic:
@@ -718,37 +718,37 @@ std::future<proto::OwnedTrackPublication> FfiClient::publishTrackAsync(std::uint
718718
return fut;
719719
}
720720

721-
std::future<std::shared_ptr<CaptureSource>> FfiClient::newCaptureSourceAsync(
721+
std::future<std::shared_ptr<capture::CaptureSource>> FfiClient::newCaptureSourceAsync(
722722
proto::NewCaptureSourceRequest request, const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send) {
723723
// Generate client-side async_id first
724724
const AsyncId async_id = generateAsyncId();
725725

726726
// Register the async handler BEFORE sending the request
727-
auto fut = registerAsync<std::shared_ptr<CaptureSource>>(
727+
auto fut = registerAsync<std::shared_ptr<capture::CaptureSource>>(
728728
async_id,
729729
[async_id](const proto::FfiEvent& event) {
730730
return event.has_new_capture_source() && event.new_capture_source().async_id() == async_id;
731731
},
732-
[](const proto::FfiEvent& event, std::promise<std::shared_ptr<CaptureSource>>& pr) {
732+
[](const proto::FfiEvent& event, std::promise<std::shared_ptr<capture::CaptureSource>>& pr) {
733733
const auto& cb = event.new_capture_source();
734734
try {
735735
if (cb.has_error()) {
736-
throw CaptureSourceError(cb.error());
736+
throw capture::CaptureSourceError(cb.error());
737737
}
738738
if (!cb.has_source()) {
739-
throw CaptureSourceError("NewCaptureSourceCallback missing source");
739+
throw capture::CaptureSourceError("NewCaptureSourceCallback missing source");
740740
}
741741
// Adopt the handles before satisfying the promise. If the caller has
742742
// discarded its future, destroying the pending promise releases the
743743
// wrapper and both handles after this callback completes.
744-
pr.set_value(CaptureSource::fromOwned(cb.source()));
744+
pr.set_value(capture::CaptureSource::fromOwned(cb.source()));
745745
} catch (const std::exception& error) {
746-
pr.set_exception(std::make_exception_ptr(CaptureSourceError(error.what())));
746+
pr.set_exception(std::make_exception_ptr(capture::CaptureSourceError(error.what())));
747747
} catch (...) {
748-
pr.set_exception(std::make_exception_ptr(CaptureSourceError("Capture source construction failed")));
748+
pr.set_exception(std::make_exception_ptr(capture::CaptureSourceError("Capture source construction failed")));
749749
}
750750
},
751-
std::make_exception_ptr(CaptureSourceError("Async operation cancelled")));
751+
std::make_exception_ptr(capture::CaptureSourceError("Async operation cancelled")));
752752

753753
request.set_request_async_id(async_id);
754754
proto::FfiRequest req;

‎src/ffi_client.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ class DataStream;
5858

5959
struct RoomOptions;
6060
struct TrackPublishOptions;
61+
namespace capture {
6162
class CaptureSource;
63+
} // namespace capture
6264

6365
using FfiCallbackFn = void (*)(const uint8_t*, size_t);
6466
extern "C" void livekit_ffi_initialize(FfiCallbackFn cb, bool capture_logs, const char* sdk, const char* sdk_version);
@@ -130,7 +132,7 @@ class LIVEKIT_INTERNAL_API FfiClient {
130132
// Capture APIs
131133
// Optional request transport permits deterministic callback tests without
132134
// starting a GPU renderer or GStreamer pipeline.
133-
std::future<std::shared_ptr<CaptureSource>> newCaptureSourceAsync(
135+
std::future<std::shared_ptr<capture::CaptureSource>> newCaptureSourceAsync(
134136
proto::NewCaptureSourceRequest request,
135137
const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send = {});
136138
std::future<void> setLocalMetadataAsync(std::uint64_t local_participant_handle, const std::string& metadata);

‎src/tests/integration/test_capture_source.cpp‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ constexpr int kCaptureWidth = 1280;
4545
constexpr int kCaptureHeight = 720;
4646
constexpr std::uint32_t kCaptureFramerateFps = 30;
4747

48-
std::shared_ptr<CaptureSource> createCapture(bool clock) {
48+
std::shared_ptr<capture::CaptureSource> createCapture(bool clock) {
4949
if (clock) {
50-
ClockVideoSourceConfig config;
50+
capture::ClockVideoSourceConfig config;
5151
config.resolution = {kCaptureWidth, kCaptureHeight};
5252
config.framerate_fps = kCaptureFramerateFps;
53-
return CaptureSource::create(config).get();
53+
return capture::CaptureSource::create(config).get();
5454
}
55-
PatternVideoSourceConfig config;
55+
capture::PatternVideoSourceConfig config;
5656
config.resolution = {kCaptureWidth, kCaptureHeight};
5757
config.framerate_fps = kCaptureFramerateFps;
58-
return CaptureSource::create(config).get();
58+
return capture::CaptureSource::create(config).get();
5959
}
6060

6161
} // namespace
@@ -77,7 +77,7 @@ TEST_P(CaptureSourceServerTest, CaptureSourcePublishesFramesEndToEnd) {
7777
ASSERT_NE(capture, nullptr);
7878

7979
// The pixel source reports back the resolution it was configured with.
80-
ASSERT_EQ(capture->kind(), CaptureSourceKind::Pixel);
80+
ASSERT_EQ(capture->kind(), capture::CaptureSourceKind::Pixel);
8181
ASSERT_EQ(capture->width(), kCaptureWidth);
8282
ASSERT_EQ(capture->height(), kCaptureHeight);
8383
ASSERT_FALSE(capture->codec().has_value());
@@ -121,8 +121,8 @@ TEST_P(CaptureSourceServerTest, CaptureSourcePublishesFramesEndToEnd) {
121121
ASSERT_NO_THROW(lockLocalParticipant(sender_room)->publishTrack(track, capture->publishOptions(app_options)));
122122

123123
// Observe the capture's terminal event.
124-
std::optional<CaptureResult> finished;
125-
capture->setOnFinishedCallback([&mutex, &cv, &finished](const CaptureResult& result) {
124+
std::optional<capture::CaptureResult> finished;
125+
capture->setOnFinishedCallback([&mutex, &cv, &finished](const capture::CaptureResult& result) {
126126
std::lock_guard<std::mutex> lock(mutex);
127127
finished = result;
128128
cv.notify_all();
@@ -148,7 +148,7 @@ TEST_P(CaptureSourceServerTest, CaptureSourcePublishesFramesEndToEnd) {
148148
}
149149

150150
ASSERT_FALSE(finished->error.has_value()) << *finished->error;
151-
EXPECT_EQ(finished->exit, CaptureExit::Stopped);
151+
EXPECT_EQ(finished->exit, capture::CaptureExit::Stopped);
152152
EXPECT_GT(finished->frames_captured, 0u);
153153
const auto capture_seconds =
154154
std::chrono::duration<double>(std::chrono::steady_clock::now() - capture_started).count();
@@ -162,6 +162,8 @@ TEST_P(CaptureSourceServerTest, CaptureSourcePublishesFramesEndToEnd) {
162162
}
163163

164164
INSTANTIATE_TEST_SUITE_P(PixelSources, CaptureSourceServerTest, ::testing::Bool(),
165-
[](const ::testing::TestParamInfo<bool>& info) { return info.param ? "Clock" : "Pattern"; });
165+
[](const ::testing::TestParamInfo<bool>& info) {
166+
return info.param ? "Clock" : "capture::Pattern";
167+
});
166168

167169
} // namespace livekit::test

‎src/tests/packaging/test_capture_bundle.cpp‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ namespace {
2727
// Strict GPU checks require successful creation of enabled sources. Omitted
2828
// backends must reject a valid configuration with the feature-disabled error.
2929
template <typename Config>
30-
bool checkSource(std::string_view name, const Config& config, bool expected, livekit::CaptureSourceKind kind) {
30+
bool checkSource(std::string_view name, const Config& config, bool expected, livekit::capture::CaptureSourceKind kind) {
3131
try {
32-
auto future = livekit::CaptureSource::create(config);
32+
auto future = livekit::capture::CaptureSource::create(config);
3333
if (future.wait_for(std::chrono::seconds(15)) != std::future_status::ready) {
3434
std::cerr << name << ": source creation timed out\n";
3535
return false;
3636
}
3737
auto source = future.get();
3838
if (!expected || !source || source->kind() != kind || source->width() != 320 || source->height() != 240 ||
3939
!source->videoSource() ||
40-
(kind == livekit::CaptureSourceKind::Encoded && source->codec() != livekit::VideoCodec::VP8)) {
40+
(kind == livekit::capture::CaptureSourceKind::Encoded && source->codec() != livekit::VideoCodec::VP8)) {
4141
std::cerr << name << ": unexpected source availability or metadata\n";
4242
return false;
4343
}
4444
return true;
45-
} catch (const livekit::CaptureSourceError& error) {
45+
} catch (const livekit::capture::CaptureSourceError& error) {
4646
constexpr std::string_view disabled = "capture source is not enabled in this build";
4747
const std::string_view message(error.what());
4848
if (!expected && message.find(disabled) != std::string_view::npos) {
@@ -51,7 +51,7 @@ bool checkSource(std::string_view name, const Config& config, bool expected, liv
5151
// Runners without an adapter can still prove that the selected backend
5252
// reached its renderer. All other errors fail; strict GPU jobs also fail
5353
// on a missing adapter.
54-
if (expected && kind == livekit::CaptureSourceKind::Pixel && !LIVEKIT_VERIFY_PIXEL_SOURCES &&
54+
if (expected && kind == livekit::capture::CaptureSourceKind::Pixel && !LIVEKIT_VERIFY_PIXEL_SOURCES &&
5555
message.find("no compatible GPU adapter") != std::string_view::npos) {
5656
std::cout << name << ": creation not exercised (no GPU adapter); backend availability verified\n";
5757
return true;
@@ -65,22 +65,24 @@ bool checkSource(std::string_view name, const Config& config, bool expected, liv
6565
}
6666

6767
bool checkSources() {
68-
livekit::GstreamerVideoSourceConfig gstreamer;
68+
livekit::capture::GstreamerVideoSourceConfig gstreamer;
6969
gstreamer.pipeline =
7070
"videotestsrc num-buffers=4 ! video/x-raw,width=320,height=240,framerate=30/1 ! "
7171
"vp8enc deadline=1 ! appsink name=lk_appsink";
7272
bool success =
73-
checkSource("gstreamer", gstreamer, LIVEKIT_EXPECT_GSTREAMER != 0, livekit::CaptureSourceKind::Encoded);
73+
checkSource("gstreamer", gstreamer, LIVEKIT_EXPECT_GSTREAMER != 0, livekit::capture::CaptureSourceKind::Encoded);
7474

75-
livekit::PatternVideoSourceConfig pattern;
75+
livekit::capture::PatternVideoSourceConfig pattern;
7676
pattern.resolution = {320, 240};
7777
pattern.framerate_fps = 30;
78-
success = checkSource("pattern", pattern, LIVEKIT_EXPECT_PATTERN != 0, livekit::CaptureSourceKind::Pixel) && success;
78+
success = checkSource("pattern", pattern, LIVEKIT_EXPECT_PATTERN != 0, livekit::capture::CaptureSourceKind::Pixel) &&
79+
success;
7980

80-
livekit::ClockVideoSourceConfig clock;
81+
livekit::capture::ClockVideoSourceConfig clock;
8182
clock.resolution = {320, 240};
8283
clock.framerate_fps = 30;
83-
success = checkSource("clock", clock, LIVEKIT_EXPECT_CLOCK != 0, livekit::CaptureSourceKind::Pixel) && success;
84+
success =
85+
checkSource("clock", clock, LIVEKIT_EXPECT_CLOCK != 0, livekit::capture::CaptureSourceKind::Pixel) && success;
8486
return success;
8587
}
8688

0 commit comments

Comments
 (0)