Skip to content

Commit c08507e

Browse files
committed
Add CaptureSource wrapper
1 parent bbbcd01 commit c08507e

5 files changed

Lines changed: 452 additions & 0 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ add_custom_target(build_rust_ffi
387387
add_library(livekit SHARED
388388
src/audio_frame.cpp
389389
src/audio_processing_module.cpp
390+
src/capture_source.cpp
390391
src/audio_source.cpp
391392
src/audio_stream.cpp
392393
src/data_track_frame.cpp

‎include/livekit/capture_source.h‎

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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+
#pragma once
18+
19+
#include <cstdint>
20+
#include <functional>
21+
#include <future>
22+
#include <memory>
23+
#include <mutex>
24+
#include <optional>
25+
#include <stdexcept>
26+
#include <string>
27+
28+
#include "livekit/ffi_handle.h"
29+
#include "livekit/room_event_types.h"
30+
#include "livekit/video_source.h"
31+
#include "livekit/visibility.h"
32+
33+
namespace livekit {
34+
35+
namespace proto {
36+
class NewCaptureSourceRequest;
37+
class OwnedCaptureSource;
38+
} // namespace proto
39+
40+
/// Error raised when capture source creation or control fails.
41+
class LIVEKIT_API CaptureSourceError : public std::runtime_error {
42+
public:
43+
/// Create a capture source error.
44+
///
45+
/// @param message Human-readable error message.
46+
explicit CaptureSourceError(const std::string& message) : std::runtime_error(message) {}
47+
};
48+
49+
/// Kind of media a capture source produces.
50+
enum class CaptureSourceKind {
51+
/// Pixel frames, published through the WebRTC encoder.
52+
Pixel = 0,
53+
/// Pre-encoded access units, published as passthrough.
54+
Encoded = 1,
55+
};
56+
57+
/// Bitrate unit expected by a GStreamer encoder property.
58+
enum class GstreamerBitrateUnit {
59+
/// Bits per second.
60+
Bps = 0,
61+
/// Kilobits per second.
62+
Kbps = 1,
63+
};
64+
65+
/// Video resolution in pixels.
66+
struct CaptureResolution {
67+
int width = 0;
68+
int height = 0;
69+
};
70+
71+
/// Binding from WebRTC rate-control targets to a GStreamer encoder property.
72+
struct GstreamerRateControl {
73+
/// Name of the encoder element in the pipeline (e.g. `lk_encoder`).
74+
std::string element;
75+
76+
/// Bitrate property to set on the element (e.g. `bitrate` for x264enc,
77+
/// `target-bitrate` for vp8enc/vp9enc).
78+
std::string property;
79+
80+
/// Unit the property expects.
81+
GstreamerBitrateUnit unit = GstreamerBitrateUnit::Bps;
82+
};
83+
84+
/// Configuration for encoded ingest from a GStreamer pipeline.
85+
struct GstreamerVideoSourceConfig {
86+
/// GStreamer launch description for the encoded producer pipeline.
87+
///
88+
/// Must contain `appsink name=lk_appsink`, or leave exactly one encoded
89+
/// video source pad unlinked for the source to attach one to.
90+
std::string pipeline;
91+
92+
/// Codec expected from the pipeline; inferred from pipeline caps when
93+
/// omitted.
94+
std::optional<VideoCodec> codec;
95+
96+
/// Encoded frame resolution. When omitted, it is discovered from the
97+
/// pipeline's negotiated caps; when set, the pipeline output is verified
98+
/// against it.
99+
std::optional<CaptureResolution> resolution;
100+
101+
/// Forwards WebRTC rate-control targets to an encoder element's bitrate
102+
/// property. Without this, the pipeline encodes at a fixed bitrate.
103+
std::optional<GstreamerRateControl> rate_control;
104+
};
105+
106+
/// Configuration for the built-in test source producing solid-color frames.
107+
struct DemoVideoSourceConfig {};
108+
109+
/// Why a capture ended without error.
110+
enum class CaptureExit {
111+
/// Stopped via @ref CaptureSource::stop (or source destruction).
112+
Stopped = 0,
113+
/// The producer reached the end of its stream.
114+
EndOfStream = 1,
115+
};
116+
117+
/// Terminal result of a started capture.
118+
struct CaptureResult {
119+
/// Error message when the capture failed; empty on success.
120+
std::optional<std::string> error;
121+
122+
/// Number of frames or access units captured.
123+
std::uint64_t frames_captured = 0;
124+
125+
/// Why the capture ended; meaningful only when @ref error is empty.
126+
CaptureExit exit = CaptureExit::Stopped;
127+
};
128+
129+
/// A server-side capture source (livekit-capture) that produces video
130+
/// without per-frame FFI traffic.
131+
///
132+
/// The source owns its producer (e.g. a GStreamer pipeline) and the pump
133+
/// that feeds an RTC video source. Publish it like any other source: create
134+
/// a track from @ref videoSource(), publish with application options merged
135+
/// over @ref recommendedPublishOptions(), then call @ref start().
136+
///
137+
/// Requires the FFI library to be built with the `capture` feature
138+
/// (`LIVEKIT_ENABLE_CAPTURE`); otherwise creation fails.
139+
///
140+
/// @note Keep this object alive while the track is published. Destroying it
141+
/// stops a running capture.
142+
class LIVEKIT_API CaptureSource {
143+
public:
144+
/// Terminal notification for a started capture, invoked exactly once on
145+
/// the FFI event thread (like room delegate callbacks).
146+
using FinishedCallback = std::function<void(const CaptureResult &)>;
147+
148+
/// Create a capture source from a GStreamer pipeline configuration.
149+
///
150+
/// Completes asynchronously: construction starts the pipeline and may wait
151+
/// for its first output to discover stream settings. Errors (invalid
152+
/// pipeline, missing capture feature, discovery timeout) are thrown from
153+
/// the future as @ref CaptureSourceError.
154+
static std::future<std::shared_ptr<CaptureSource>>
155+
create(GstreamerVideoSourceConfig config);
156+
157+
/// Create the built-in demo capture source (solid cycling colors).
158+
static std::future<std::shared_ptr<CaptureSource>>
159+
create(DemoVideoSourceConfig config);
160+
161+
~CaptureSource();
162+
163+
CaptureSource(const CaptureSource &) = delete;
164+
CaptureSource &operator=(const CaptureSource &) = delete;
165+
CaptureSource(CaptureSource &&) = delete;
166+
CaptureSource &operator=(CaptureSource &&) = delete;
167+
168+
/// Kind of media this source produces.
169+
CaptureSourceKind kind() const noexcept { return kind_; }
170+
171+
/// Declared or discovered stream resolution.
172+
int width() const noexcept { return width_; }
173+
int height() const noexcept { return height_; }
174+
175+
/// Codec produced by the source; encoded sources only.
176+
std::optional<VideoCodec> codec() const noexcept { return codec_; }
177+
178+
/// RTC video source fed by this capture source; use it with
179+
/// LocalVideoTrack::createLocalVideoTrack().
180+
std::shared_ptr<VideoSource> videoSource() const noexcept {
181+
return video_source_;
182+
}
183+
184+
/// Returns publish options for this track, applying application options.
185+
///
186+
/// Fields the source dictates (e.g. codec, encoder backend, and simulcast
187+
/// for encoded sources) are required for correct publication and override
188+
/// the application values; all other fields are taken from @p options
189+
/// unchanged.
190+
TrackPublishOptions publishOptions(TrackPublishOptions options = {}) const;
191+
192+
/// Set the terminal notification. Set this before @ref start().
193+
void setOnFinishedCallback(FinishedCallback callback);
194+
195+
/// Start pumping frames into the RTC video source.
196+
///
197+
/// @throws CaptureSourceError if the capture was already started or the
198+
/// FFI call fails.
199+
void start();
200+
201+
/// Signal a running capture to stop after the frame in flight. The
202+
/// finished callback fires shortly after. Stopping an already-finished
203+
/// capture is a no-op.
204+
///
205+
/// @throws CaptureSourceError if the FFI call fails.
206+
void stop();
207+
208+
private:
209+
CaptureSource() = default;
210+
211+
/// Shared creation path: sends the request and maps the callback payload
212+
/// onto a wrapper instance.
213+
static std::future<std::shared_ptr<CaptureSource>>
214+
createFromRequest(proto::NewCaptureSourceRequest request);
215+
216+
/// Builds the wrapper from the callback payload, adopting its handles.
217+
static std::shared_ptr<CaptureSource>
218+
fromOwned(const proto::OwnedCaptureSource &owned);
219+
220+
FfiHandle handle_;
221+
CaptureSourceKind kind_ = CaptureSourceKind::Pixel;
222+
int width_ = 0;
223+
int height_ = 0;
224+
std::optional<VideoCodec> codec_;
225+
std::shared_ptr<VideoSource> video_source_;
226+
/// Fields set here are dictated by the source and win in publishOptions().
227+
TrackPublishOptions source_publish_options_;
228+
229+
std::mutex callback_mutex_;
230+
FinishedCallback on_finished_;
231+
int listener_id_ = 0;
232+
};
233+
234+
} // namespace livekit

‎include/livekit/livekit.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "livekit/audio_source.h"
2222
#include "livekit/audio_stream.h"
2323
#include "livekit/build.h"
24+
#include "livekit/capture_source.h"
2425
#include "livekit/e2ee.h"
2526
#include "livekit/local_audio_track.h"
2627
#include "livekit/local_participant.h"

‎include/livekit/video_source.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ class LIVEKIT_API VideoSource {
9595
VideoRotation rotation = VideoRotation::VIDEO_ROTATION_0);
9696

9797
private:
98+
friend class CaptureSource;
99+
100+
/// Adopt an existing FFI video source handle (used by CaptureSource).
101+
VideoSource(FfiHandle&& handle, int width, int height) noexcept
102+
: handle_(std::move(handle)), width_(width), height_(height) {}
103+
98104
FfiHandle handle_; // owned FFI handle
99105
int width_{0};
100106
int height_{0};

0 commit comments

Comments
 (0)