Skip to content

Commit 0ad1cb6

Browse files
let rust error, not the cpp sdk
1 parent 709ac96 commit 0ad1cb6

4 files changed

Lines changed: 128 additions & 50 deletions

File tree

‎docs/capture-sources.md‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ headers do not name it.
3838

3939
## Create and publish a source
4040

41-
Dimensions must be positive. Pattern and clock frame rates must also be positive.
42-
Invalid C++ configurations throw `std::invalid_argument` when the returned
43-
future is awaited. For GStreamer, resolution and codec can be omitted to discover
44-
them from negotiated pipeline caps. Producer setup failures reported by Rust
45-
throw `std::runtime_error` through that future.
41+
Rust validates capture configurations, including zero dimensions and frame rates,
42+
and reports failures as `std::runtime_error` when the returned future is awaited.
43+
The C++ wrapper rejects only negative dimensions and invalid enum values that
44+
cannot be serialized, using `std::invalid_argument`. For GStreamer, resolution
45+
and codec can be omitted to discover them from negotiated pipeline caps.
46+
Producer setup failures reported by Rust also throw `std::runtime_error` through
47+
that future.
4648

4749
Discarding a creation future does not wait for creation to finish. Creation
4850
continues, and the source is released when the callback arrives.
@@ -126,7 +128,9 @@ A structured pump error received from Rust becomes a failed
126128
preserved. Check the result before calling `value()` or `error()`.
127129
128130
Failures without a protobuf error kind throw a clear exception with the operation
129-
name and diagnostic. C++ configuration validation throws `std::invalid_argument`.
131+
name and diagnostic. Negative dimensions and invalid enum values that cannot be
132+
serialized throw `std::invalid_argument`. Rust configuration validation errors
133+
throw `std::runtime_error`.
130134
Uninitialized SDK use, transport failures, malformed responses, shutdown
131135
cancellation, and untyped errors from Rust throw `std::runtime_error`.
132136
`create()` and `join()` deliver these exceptions from the returned future's
@@ -144,8 +148,3 @@ successful results when their requests succeed; the current protocol supplies
144148
structured error kinds only for the terminal pump result. The SDK does not
145149
classify untyped Rust messages by parsing their text. Allocation failures may
146150
also throw.
147-
148-
This replaces the unreleased C++-only error categories. Code that handled
149-
`InvalidConfiguration`, `NotInitialized`, `Rejected`, `Transport`, `Protocol`, or
150-
`Cancelled` as enum values must now catch the corresponding exception. Existing
151-
`Source`, `Rtc`, and `Panicked` pump-error handling remains unchanged.

‎include/livekit/capture/capture_source.h‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,11 @@ struct CaptureResult {
179179
/// through @ref CaptureSource::publishOptions(), then call @ref CaptureSource::start().
180180
///
181181
/// 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(),
182+
/// Failures without a protobuf error kind throw exceptions. Rust validates source
183+
/// configurations and reports failures as std::runtime_error. Negative dimensions
184+
/// and invalid enum values cannot be serialized and throw std::invalid_argument.
185+
/// Initialization, transport, protocol, cancellation, and untyped Rust failures
186+
/// throw std::runtime_error. For create() and join(),
185187
/// these exceptions are delivered when awaiting the future.
186188
///
187189
/// Requires a capture-enabled SDK build (`LIVEKIT_CAPTURE`). Creation fails
@@ -202,9 +204,10 @@ class LIVEKIT_API CaptureSource {
202204
///
203205
/// @param config GStreamer pipeline configuration.
204206
/// @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.
207+
/// @throws std::invalid_argument When awaiting the future if dimensions are negative
208+
/// or enum values cannot be serialized.
209+
/// @throws std::runtime_error When awaiting the future if Rust rejects the configuration
210+
/// or setup, initialization, transport, protocol validation, or cancellation fails.
208211
/// @throws std::bad_alloc If allocation fails, including when awaiting the future.
209212
[[nodiscard]] static std::future<Result<std::shared_ptr<CaptureSource>, CaptureError>> create(
210213
const GstreamerVideoSourceConfig& config);
@@ -218,9 +221,10 @@ class LIVEKIT_API CaptureSource {
218221
///
219222
/// @param config Pattern source configuration.
220223
/// @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::invalid_argument When awaiting the future if dimensions are negative
225+
/// or enum values cannot be serialized.
226+
/// @throws std::runtime_error When awaiting the future if Rust rejects the configuration
227+
/// or setup, initialization, transport, protocol validation, or cancellation fails.
224228
/// @throws std::bad_alloc If allocation fails, including when awaiting the future.
225229
[[nodiscard]] static std::future<Result<std::shared_ptr<CaptureSource>, CaptureError>> create(
226230
PatternVideoSourceConfig config);
@@ -234,9 +238,9 @@ class LIVEKIT_API CaptureSource {
234238
///
235239
/// @param config Clock source configuration.
236240
/// @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.
241+
/// @throws std::invalid_argument When awaiting the future if dimensions are negative.
242+
/// @throws std::runtime_error When awaiting the future if Rust rejects the configuration
243+
/// or setup, initialization, transport, protocol validation, or cancellation fails.
240244
/// @throws std::bad_alloc If allocation fails, including when awaiting the future.
241245
[[nodiscard]] static std::future<Result<std::shared_ptr<CaptureSource>, CaptureError>> create(
242246
ClockVideoSourceConfig config);

‎src/capture_source.cpp‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,18 @@ std::future<CreationResult> createCaptureSource(const Config& config) {
5959
}
6060
}
6161

62-
void validateResolution(const CaptureResolution& resolution) {
63-
if (resolution.width <= 0 || resolution.height <= 0) {
64-
throw std::invalid_argument("CaptureSource::create: resolution dimensions must be positive");
62+
void copyResolution(proto::VideoSourceResolution& target, const CaptureResolution& resolution) {
63+
// Rust validates source settings. Only guard the signed-to-unsigned conversion here.
64+
if (resolution.width < 0 || resolution.height < 0) {
65+
throw std::invalid_argument("CaptureSource::create: resolution dimensions cannot be negative");
6566
}
67+
target.set_width(static_cast<std::uint32_t>(resolution.width));
68+
target.set_height(static_cast<std::uint32_t>(resolution.height));
6669
}
6770

6871
template <typename ProtoConfig, typename Config>
6972
void copyPixelConfig(ProtoConfig& target, const Config& config) {
70-
validateResolution(config.resolution);
71-
if (config.framerate_fps == 0) {
72-
throw std::invalid_argument("CaptureSource::create: frame rate must be positive");
73-
}
74-
target.mutable_resolution()->set_width(config.resolution.width);
75-
target.mutable_resolution()->set_height(config.resolution.height);
73+
copyResolution(*target.mutable_resolution(), config.resolution);
7674
target.set_framerate_fps(config.framerate_fps);
7775
}
7876

@@ -104,14 +102,11 @@ std::future<Result<std::shared_ptr<CaptureSource>, CaptureError>> CaptureSource:
104102
}
105103

106104
proto::NewCaptureSourceRequest toProto(const GstreamerVideoSourceConfig& config) {
107-
if (config.resolution) {
108-
validateResolution(*config.resolution);
109-
}
110-
if (config.pipeline.empty() || (config.codec && !proto::VideoCodec_IsValid(static_cast<int>(*config.codec))) ||
111-
(config.rate_control && (config.rate_control->element.empty() || config.rate_control->property.empty() ||
112-
(config.rate_control->unit != GstreamerBitrateUnit::Bps &&
113-
config.rate_control->unit != GstreamerBitrateUnit::Kbps)))) {
114-
throw std::invalid_argument("CaptureSource::create: invalid GStreamer pipeline, codec, or rate-control binding");
105+
// Protobuf enum setters assert on invalid values; reject those before serialization.
106+
if ((config.codec && !proto::VideoCodec_IsValid(static_cast<int>(*config.codec))) ||
107+
(config.rate_control && config.rate_control->unit != GstreamerBitrateUnit::Bps &&
108+
config.rate_control->unit != GstreamerBitrateUnit::Kbps)) {
109+
throw std::invalid_argument("CaptureSource::create: invalid GStreamer codec or bitrate unit");
115110
}
116111
proto::NewCaptureSourceRequest request;
117112
auto* gstreamer = request.mutable_gstreamer();
@@ -120,8 +115,7 @@ proto::NewCaptureSourceRequest toProto(const GstreamerVideoSourceConfig& config)
120115
gstreamer->set_codec(static_cast<proto::VideoCodec>(*config.codec));
121116
}
122117
if (config.resolution) {
123-
gstreamer->mutable_resolution()->set_width(config.resolution->width);
124-
gstreamer->mutable_resolution()->set_height(config.resolution->height);
118+
copyResolution(*gstreamer->mutable_resolution(), *config.resolution);
125119
}
126120
if (config.rate_control) {
127121
auto* rate_control = gstreamer->mutable_rate_control();

‎src/tests/unit/test_capture_source.cpp‎

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,9 @@ TEST(CaptureSourceProtoTest, ErrorKindsMatchProtoExactly) {
664664
}
665665
}
666666

667-
TEST(CaptureSourceProtoTest, RejectsNonPositiveDimensionsBeforeUnsignedConversion) {
667+
TEST(CaptureSourceProtoTest, RejectsNegativeDimensionsBeforeUnsignedConversion) {
668668
for (const capture::CaptureResolution resolution :
669-
{capture::CaptureResolution{0, 720}, capture::CaptureResolution{-1, 720}, capture::CaptureResolution{1280, 0},
670-
capture::CaptureResolution{1280, -1}}) {
669+
{capture::CaptureResolution{-1, 720}, capture::CaptureResolution{1280, -1}}) {
671670
capture::PatternVideoSourceConfig pattern;
672671
pattern.resolution = resolution;
673672
pattern.framerate_fps = 30;
@@ -682,13 +681,95 @@ TEST(CaptureSourceProtoTest, RejectsNonPositiveDimensionsBeforeUnsignedConversio
682681
}
683682
}
684683

685-
TEST(CaptureSourceProtoTest, RejectsZeroFrameRate) {
684+
TEST_F(CaptureSourceCallbackTest, RustRejectsZeroDimensions) {
685+
for (const capture::CaptureResolution resolution :
686+
{capture::CaptureResolution{0, 720}, capture::CaptureResolution{1280, 0}}) {
687+
capture::PatternVideoSourceConfig pattern;
688+
pattern.resolution = resolution;
689+
pattern.framerate_fps = 30;
690+
auto pattern_future = capture::CaptureSource::create(pattern);
691+
ASSERT_EQ(pattern_future.wait_for(std::chrono::seconds(5)), std::future_status::ready);
692+
expectException([&pattern_future] { return pattern_future.get(); },
693+
"CaptureSource::create: Rust source setup failed: invalid request: "
694+
"pattern source resolution must be non-zero");
695+
696+
capture::ClockVideoSourceConfig clock;
697+
clock.resolution = resolution;
698+
clock.framerate_fps = 30;
699+
auto clock_future = capture::CaptureSource::create(clock);
700+
ASSERT_EQ(clock_future.wait_for(std::chrono::seconds(5)), std::future_status::ready);
701+
expectException([&clock_future] { return clock_future.get(); },
702+
"CaptureSource::create: Rust source setup failed: invalid request: "
703+
"clock source resolution must be non-zero");
704+
}
705+
}
706+
707+
TEST_F(CaptureSourceCallbackTest, RustRejectsZeroFrameRate) {
686708
capture::PatternVideoSourceConfig pattern;
687709
pattern.resolution = {1280, 720};
688-
expectException<std::invalid_argument>([&pattern] { return toProto(pattern); }, "CaptureSource::create:");
710+
auto pattern_future = capture::CaptureSource::create(pattern);
711+
ASSERT_EQ(pattern_future.wait_for(std::chrono::seconds(5)), std::future_status::ready);
712+
expectException([&pattern_future] { return pattern_future.get(); },
713+
"CaptureSource::create: Rust source setup failed: invalid request: "
714+
"pattern source frame rate must be non-zero");
715+
689716
capture::ClockVideoSourceConfig clock;
690717
clock.resolution = {1280, 720};
691-
expectException<std::invalid_argument>([&clock] { return toProto(clock); }, "CaptureSource::create:");
718+
auto clock_future = capture::CaptureSource::create(clock);
719+
ASSERT_EQ(clock_future.wait_for(std::chrono::seconds(5)), std::future_status::ready);
720+
expectException([&clock_future] { return clock_future.get(); },
721+
"CaptureSource::create: Rust source setup failed: invalid request: "
722+
"clock source frame rate must be non-zero");
723+
}
724+
725+
TEST_F(CaptureSourceCallbackTest, RustRejectsEmptyGstreamerPipeline) {
726+
auto future = capture::CaptureSource::create(capture::GstreamerVideoSourceConfig{});
727+
ASSERT_EQ(future.wait_for(std::chrono::seconds(5)), std::future_status::ready);
728+
expectException([&future] { return future.get(); },
729+
"CaptureSource::create: Rust source setup failed: invalid request: "
730+
"GStreamer pipeline error: failed to create pipeline:");
731+
}
732+
733+
TEST_F(CaptureSourceCallbackTest, RustRejectsEmptyRateControlElement) {
734+
capture::GstreamerVideoSourceConfig config;
735+
config.pipeline = "appsrc ! video/x-vp8 ! appsink name=lk_appsink";
736+
config.rate_control = capture::GstreamerRateControl{"", "bitrate", capture::GstreamerBitrateUnit::Bps};
737+
auto future = capture::CaptureSource::create(config);
738+
ASSERT_EQ(future.wait_for(std::chrono::seconds(5)), std::future_status::ready);
739+
expectException([&future] { return future.get(); },
740+
"CaptureSource::create: Rust source setup failed: invalid request: "
741+
"pipeline has no element named '' for rate control");
742+
}
743+
744+
TEST_F(CaptureSourceCallbackTest, RustAcceptsEmptyRateControlPropertyDuringCreation) {
745+
capture::GstreamerVideoSourceConfig config;
746+
config.pipeline =
747+
"videotestsrc num-buffers=4 ! video/x-raw,width=320,height=240,framerate=30/1 ! "
748+
"vp8enc name=encoder deadline=1 ! appsink name=lk_appsink";
749+
config.rate_control = capture::GstreamerRateControl{"encoder", "", capture::GstreamerBitrateUnit::Bps};
750+
auto future = capture::CaptureSource::create(config);
751+
ASSERT_EQ(future.wait_for(std::chrono::seconds(5)), std::future_status::ready);
752+
const auto created = future.get();
753+
ASSERT_TRUE(created.ok());
754+
EXPECT_EQ(created.value()->width(), 320);
755+
EXPECT_EQ(created.value()->height(), 240);
756+
}
757+
758+
TEST_F(CaptureSourceCallbackTest, RustRejectsGstreamerResolutionMismatch) {
759+
for (const capture::CaptureResolution resolution :
760+
{capture::CaptureResolution{0, 240}, capture::CaptureResolution{320, 0}}) {
761+
capture::GstreamerVideoSourceConfig config;
762+
config.pipeline =
763+
"videotestsrc num-buffers=4 ! video/x-raw,width=320,height=240,framerate=30/1 ! "
764+
"vp8enc deadline=1 ! appsink name=lk_appsink";
765+
config.resolution = resolution;
766+
auto future = capture::CaptureSource::create(config);
767+
ASSERT_EQ(future.wait_for(std::chrono::seconds(5)), std::future_status::ready);
768+
expectException([&future] { return future.get(); },
769+
"CaptureSource::create: Rust source setup failed: invalid request: "
770+
"pipeline produces 320x240, but the configuration declares " +
771+
std::to_string(resolution.width) + "x" + std::to_string(resolution.height));
772+
}
692773
}
693774

694775
// Deliberately exercise invalid enum inputs at the public API boundary.
@@ -709,10 +790,10 @@ TEST(CaptureSourceProtoTest, RejectsInvalidEnumValues) {
709790

710791
TEST(CaptureSourceProtoTest, ValidationErrorsAreDeliveredThroughFuture) {
711792
std::future<CreationOutcome> future;
712-
EXPECT_NO_THROW(future = capture::CaptureSource::create(capture::ClockVideoSourceConfig{}));
793+
EXPECT_NO_THROW(future = capture::CaptureSource::create(capture::ClockVideoSourceConfig{{-1, 720}, 30}));
713794
ASSERT_TRUE(future.valid());
714795
expectException<std::invalid_argument>([&future] { return future.get(); },
715-
"CaptureSource::create: resolution dimensions must be positive");
796+
"CaptureSource::create: resolution dimensions cannot be negative");
716797
}
717798

718799
TEST(CaptureSourceProtoTest, SendErrorsAreDeliveredThroughFuture) {

0 commit comments

Comments
 (0)