2727#include < future>
2828#include < memory>
2929#include < optional>
30- #include < stdexcept>
3130#include < string>
3231
32+ #include " livekit/result.h"
3333#include " livekit/room_event_types.h"
3434#include " livekit/video_source.h"
3535#include " livekit/visibility.h"
@@ -47,15 +47,6 @@ class OwnedCaptureSource;
4747// / @brief Capture sources and their configuration types.
4848namespace livekit ::capture {
4949
50- // / @brief Error raised when capture source creation or control fails.
51- class LIVEKIT_API CaptureSourceError : public std::runtime_error {
52- public:
53- // / @brief Create a capture source error.
54- // /
55- // / @param message Human-readable error message.
56- explicit CaptureSourceError (const std::string& message) : std::runtime_error(message) {}
57- };
58-
5950// / @brief Kind of media a capture source produces.
6051enum class CaptureSourceKind {
6152 // / @brief Pixel frames, published through the WebRTC encoder.
@@ -151,7 +142,7 @@ enum class CaptureExit {
151142 EndOfStream = 1 ,
152143};
153144
154- // / @brief Why a capture ended with an error .
145+ // / @brief Terminal pump failure category; matches the FFI CaptureErrorKind enum .
155146enum class CaptureErrorKind {
156147 // / @brief The capture source failed or violated its contract.
157148 Source = 0 ,
@@ -161,7 +152,7 @@ enum class CaptureErrorKind {
161152 Panicked = 2 ,
162153};
163154
164- // / @brief Terminal capture failure.
155+ // / @brief Structured terminal pump failure reported by the Rust FFI .
165156struct CaptureError {
166157 // / @brief Human-readable error message.
167158 std::string message;
@@ -170,15 +161,12 @@ struct CaptureError {
170161 CaptureErrorKind kind = CaptureErrorKind::Source;
171162};
172163
173- // / @brief Terminal result of a started capture.
164+ // / @brief Successful terminal statistics of a started capture.
174165struct CaptureResult {
175- // / @brief Failure details when the capture failed; empty on success.
176- std::optional<CaptureError> error;
177-
178166 // / @brief Number of frames or access units captured.
179167 std::uint64_t frames_captured = 0 ;
180168
181- // / Why the capture ended; meaningful only when @ref error is empty .
169+ // / @brief Why the capture ended successfully .
182170 CaptureExit exit = CaptureExit::Stopped;
183171};
184172
@@ -190,6 +178,12 @@ struct CaptureResult {
190178// / a track from @ref CaptureSource::videoSource(), merge application options
191179// / through @ref CaptureSource::publishOptions(), then call @ref CaptureSource::start().
192180// /
181+ // / Structured pump failures are returned as Result values containing @ref CaptureError.
182+ // / Failures without a protobuf error kind throw exceptions. Invalid C++ input
183+ // / throws std::invalid_argument; initialization, transport, protocol, cancellation,
184+ // / and untyped Rust failures throw std::runtime_error. For create() and join(),
185+ // / these exceptions are delivered when awaiting the future.
186+ // /
193187// / Requires a capture-enabled SDK build (`LIVEKIT_CAPTURE`). Creation fails
194188// / when the requested source was not compiled into the Rust FFI.
195189// /
@@ -200,16 +194,20 @@ class LIVEKIT_API CaptureSource {
200194 // / @brief Create a capture source from a GStreamer pipeline configuration.
201195 // /
202196 // / Completes asynchronously: construction starts the pipeline and may wait
203- // / for its first output to discover stream settings. Errors (invalid
204- // / pipeline, missing capture feature, discovery timeout) are thrown from
205- // / the future as @ref CaptureSourceError .
197+ // / for its first output to discover stream settings. Configuration, producer
198+ // / setup, transport, protocol, and cancellation failures are delivered as
199+ // / exceptions through the future .
206200 // / Destroying the future does not wait for creation. The source is released
207201 // / when creation completes if the future was discarded.
208202 // /
209203 // / @param config GStreamer pipeline configuration.
210- // / @return A future that resolves to the created capture source.
211- // / @throws CaptureSourceError When awaiting the future if creation fails.
212- static std::future<std::shared_ptr<CaptureSource>> create (const GstreamerVideoSourceConfig& config);
204+ // / @return A future containing a successful Result with the created source.
205+ // / @throws std::invalid_argument When awaiting the future if the C++ configuration is invalid.
206+ // / @throws std::runtime_error When awaiting the future if setup, initialization,
207+ // / transport, protocol validation, or cancellation fails.
208+ // / @throws std::bad_alloc If allocation fails, including when awaiting the future.
209+ [[nodiscard]] static std::future<Result<std::shared_ptr<CaptureSource>, CaptureError>> create (
210+ const GstreamerVideoSourceConfig& config);
213211
214212 // / @brief Create the built-in test pattern capture source.
215213 // /
@@ -219,9 +217,13 @@ class LIVEKIT_API CaptureSource {
219217 // / released when creation completes.
220218 // /
221219 // / @param config Pattern source configuration.
222- // / @return A future that resolves to the created capture source.
223- // / @throws CaptureSourceError When awaiting the future if creation fails.
224- static std::future<std::shared_ptr<CaptureSource>> create (PatternVideoSourceConfig config);
220+ // / @return A future containing a successful Result with the created source.
221+ // / @throws std::invalid_argument When awaiting the future if the C++ configuration is invalid.
222+ // / @throws std::runtime_error When awaiting the future if setup, initialization,
223+ // / transport, protocol validation, or cancellation fails.
224+ // / @throws std::bad_alloc If allocation fails, including when awaiting the future.
225+ [[nodiscard]] static std::future<Result<std::shared_ptr<CaptureSource>, CaptureError>> create (
226+ PatternVideoSourceConfig config);
225227
226228 // / @brief Create the built-in wall clock capture source.
227229 // /
@@ -231,9 +233,13 @@ class LIVEKIT_API CaptureSource {
231233 // / released when creation completes.
232234 // /
233235 // / @param config Clock source configuration.
234- // / @return A future that resolves to the created capture source.
235- // / @throws CaptureSourceError When awaiting the future if creation fails.
236- static std::future<std::shared_ptr<CaptureSource>> create (ClockVideoSourceConfig config);
236+ // / @return A future containing a successful Result with the created source.
237+ // / @throws std::invalid_argument When awaiting the future if the C++ configuration is invalid.
238+ // / @throws std::runtime_error When awaiting the future if setup, initialization,
239+ // / transport, protocol validation, or cancellation fails.
240+ // / @throws std::bad_alloc If allocation fails, including when awaiting the future.
241+ [[nodiscard]] static std::future<Result<std::shared_ptr<CaptureSource>, CaptureError>> create (
242+ ClockVideoSourceConfig config);
237243
238244 ~CaptureSource ();
239245
@@ -275,35 +281,46 @@ class LIVEKIT_API CaptureSource {
275281
276282 // / @brief Start pumping frames into the RTC video source.
277283 // /
278- // / @return @c true if the start request succeeds; otherwise @c false.
279- bool start ();
284+ // / @return A successful Result when Rust acknowledges the request.
285+ // / @throws std::runtime_error If the SDK is not initialized, Rust rejects the
286+ // / request, or transport or protocol validation fails.
287+ // / @throws std::bad_alloc If memory allocation fails.
288+ [[nodiscard]] Result<void , CaptureError> start ();
280289
281290 // / @brief Signal a running capture to stop after the frame in flight.
282291 // /
283292 // / This method does not wait for the pump thread to exit. Call @ref join()
284293 // / to synchronize and retrieve the terminal result. Stopping an
285294 // / already-finished capture is a no-op.
286295 // /
287- // / @return @c true if the stop request succeeds; otherwise @c false.
288- bool stop ();
296+ // / @return A successful Result when Rust acknowledges the request.
297+ // / @throws std::runtime_error If the SDK is not initialized, Rust rejects the
298+ // / request, or transport or protocol validation fails.
299+ // / @throws std::bad_alloc If memory allocation fails.
300+ [[nodiscard]] Result<void , CaptureError> stop ();
289301
290302 // / @brief Consume the running pump and wait asynchronously for it to exit.
291303 // /
292304 // / This method does not signal the pump to stop. Call @ref stop() first when
293305 // / ending an unbounded source. Join may be called only once after a
294306 // / successful @ref start().
295307 // /
296- // / @return A future containing the terminal result and captured frame count.
297- // / @throws CaptureSourceError When awaiting the future if the FFI request
298- // / fails, its result is invalid, or SDK shutdown cancels the operation.
299- std::future<CaptureResult> join ();
308+ // / @return A future containing statistics on success or a structured pump
309+ // / CaptureError whose kind is supplied by Rust.
310+ // / @throws std::runtime_error When awaiting the future if initialization,
311+ // / transport, protocol validation, or cancellation fails.
312+ // / @throws std::bad_alloc If allocation fails, including when awaiting the future.
313+ [[nodiscard]] std::future<Result<CaptureResult, CaptureError>> join ();
300314
301315private:
302316 friend class livekit ::FfiClient;
303317
304318 CaptureSource ();
305319
306320 // / @brief Builds the wrapper from the callback payload, adopting its handles.
321+ // / @param owned Rust-owned source handles and metadata.
322+ // / @return The capture source wrapper.
323+ // / @throws std::runtime_error If the source metadata is invalid.
307324 static std::shared_ptr<CaptureSource> fromOwned (const proto::OwnedCaptureSource& owned);
308325
309326 struct Impl ;
0 commit comments