2121#include < livekit/track.h>
2222
2323#include < chrono>
24+ #include < limits>
2425#include < new>
2526#include < stdexcept>
2627#include < thread>
28+ #include < type_traits>
2729#include < utility>
2830
2931#include " ../common/ffi_utils.h"
@@ -664,31 +666,119 @@ TEST(CaptureSourceProtoTest, ErrorKindsMatchProtoExactly) {
664666 }
665667}
666668
667- TEST (CaptureSourceProtoTest, RejectsNonPositiveDimensionsBeforeUnsignedConversion) {
669+ TEST (CaptureSourceProtoTest, ResolutionMatchesUnsignedProtoFields) {
670+ static_assert (
671+ std::is_same_v<decltype (capture::CaptureResolution::width), decltype (proto::VideoSourceResolution{}.width ())>);
672+ static_assert (
673+ std::is_same_v<decltype (capture::CaptureResolution::height), decltype (proto::VideoSourceResolution{}.height ())>);
674+ constexpr auto kMaxDimension = std::numeric_limits<std::uint32_t >::max ();
668675 for (const capture::CaptureResolution resolution :
669- {capture::CaptureResolution{0 , 720 }, capture::CaptureResolution{-1 , 720 }, capture::CaptureResolution{1280 , 0 },
670- capture::CaptureResolution{1280 , -1 }}) {
676+ {capture::CaptureResolution{0 , 720 }, capture::CaptureResolution{1280 , 0 },
677+ capture::CaptureResolution{kMaxDimension , kMaxDimension }}) {
678+ const capture::PatternVideoSourceConfig pattern{resolution, 30 };
679+ const capture::ClockVideoSourceConfig clock{resolution, 30 };
680+ capture::GstreamerVideoSourceConfig gstreamer;
681+ gstreamer.resolution = resolution;
682+ for (const auto & request : {toProto (pattern), toProto (clock), toProto (gstreamer)}) {
683+ ASSERT_TRUE (request.IsInitialized ());
684+ const auto & serialized = request.has_pattern () ? request.pattern ().resolution ()
685+ : request.has_clock () ? request.clock ().resolution ()
686+ : request.gstreamer ().resolution ();
687+ EXPECT_EQ (serialized.width (), resolution.width );
688+ EXPECT_EQ (serialized.height (), resolution.height );
689+ }
690+ }
691+ }
692+
693+ TEST_F (CaptureSourceCallbackTest, RustRejectsZeroDimensions) {
694+ for (const capture::CaptureResolution resolution :
695+ {capture::CaptureResolution{0 , 720 }, capture::CaptureResolution{1280 , 0 }}) {
671696 capture::PatternVideoSourceConfig pattern;
672697 pattern.resolution = resolution;
673698 pattern.framerate_fps = 30 ;
674- expectException<std::invalid_argument>([&pattern] { return toProto (pattern); }, " CaptureSource::create:" );
699+ auto pattern_future = capture::CaptureSource::create (pattern);
700+ ASSERT_EQ (pattern_future.wait_for (std::chrono::seconds (5 )), std::future_status::ready);
701+ expectException ([&pattern_future] { return pattern_future.get (); },
702+ " CaptureSource::create: Rust source setup failed: invalid request: "
703+ " pattern source resolution must be non-zero" );
704+
675705 capture::ClockVideoSourceConfig clock;
676706 clock.resolution = resolution;
677707 clock.framerate_fps = 30 ;
678- expectException<std::invalid_argument>([&clock] { return toProto (clock); }, " CaptureSource::create:" );
679- capture::GstreamerVideoSourceConfig gstreamer;
680- gstreamer.resolution = resolution;
681- expectException<std::invalid_argument>([&gstreamer] { return toProto (gstreamer); }, " CaptureSource::create:" );
708+ auto clock_future = capture::CaptureSource::create (clock);
709+ ASSERT_EQ (clock_future.wait_for (std::chrono::seconds (5 )), std::future_status::ready);
710+ expectException ([&clock_future] { return clock_future.get (); },
711+ " CaptureSource::create: Rust source setup failed: invalid request: "
712+ " clock source resolution must be non-zero" );
682713 }
683714}
684715
685- TEST (CaptureSourceProtoTest, RejectsZeroFrameRate ) {
716+ TEST_F (CaptureSourceCallbackTest, RustRejectsZeroFrameRate ) {
686717 capture::PatternVideoSourceConfig pattern;
687718 pattern.resolution = {1280 , 720 };
688- expectException<std::invalid_argument>([&pattern] { return toProto (pattern); }, " CaptureSource::create:" );
719+ auto pattern_future = capture::CaptureSource::create (pattern);
720+ ASSERT_EQ (pattern_future.wait_for (std::chrono::seconds (5 )), std::future_status::ready);
721+ expectException ([&pattern_future] { return pattern_future.get (); },
722+ " CaptureSource::create: Rust source setup failed: invalid request: "
723+ " pattern source frame rate must be non-zero" );
724+
689725 capture::ClockVideoSourceConfig clock;
690726 clock.resolution = {1280 , 720 };
691- expectException<std::invalid_argument>([&clock] { return toProto (clock); }, " CaptureSource::create:" );
727+ auto clock_future = capture::CaptureSource::create (clock);
728+ ASSERT_EQ (clock_future.wait_for (std::chrono::seconds (5 )), std::future_status::ready);
729+ expectException ([&clock_future] { return clock_future.get (); },
730+ " CaptureSource::create: Rust source setup failed: invalid request: "
731+ " clock source frame rate must be non-zero" );
732+ }
733+
734+ TEST_F (CaptureSourceCallbackTest, RustRejectsEmptyGstreamerPipeline) {
735+ auto future = capture::CaptureSource::create (capture::GstreamerVideoSourceConfig{});
736+ ASSERT_EQ (future.wait_for (std::chrono::seconds (5 )), std::future_status::ready);
737+ expectException ([&future] { return future.get (); },
738+ " CaptureSource::create: Rust source setup failed: invalid request: "
739+ " GStreamer pipeline error: failed to create pipeline:" );
740+ }
741+
742+ TEST_F (CaptureSourceCallbackTest, RustRejectsEmptyRateControlElement) {
743+ capture::GstreamerVideoSourceConfig config;
744+ config.pipeline = " appsrc ! video/x-vp8 ! appsink name=lk_appsink" ;
745+ config.rate_control = capture::GstreamerRateControl{" " , " bitrate" , capture::GstreamerBitrateUnit::Bps};
746+ auto future = capture::CaptureSource::create (config);
747+ ASSERT_EQ (future.wait_for (std::chrono::seconds (5 )), std::future_status::ready);
748+ expectException ([&future] { return future.get (); },
749+ " CaptureSource::create: Rust source setup failed: invalid request: "
750+ " pipeline has no element named '' for rate control" );
751+ }
752+
753+ TEST_F (CaptureSourceCallbackTest, RustAcceptsEmptyRateControlPropertyDuringCreation) {
754+ capture::GstreamerVideoSourceConfig config;
755+ config.pipeline =
756+ " videotestsrc num-buffers=4 ! video/x-raw,width=320,height=240,framerate=30/1 ! "
757+ " vp8enc name=encoder deadline=1 ! appsink name=lk_appsink" ;
758+ config.rate_control = capture::GstreamerRateControl{" encoder" , " " , capture::GstreamerBitrateUnit::Bps};
759+ auto future = capture::CaptureSource::create (config);
760+ ASSERT_EQ (future.wait_for (std::chrono::seconds (5 )), std::future_status::ready);
761+ const auto created = future.get ();
762+ ASSERT_TRUE (created.ok ());
763+ EXPECT_EQ (created.value ()->width (), 320 );
764+ EXPECT_EQ (created.value ()->height (), 240 );
765+ }
766+
767+ TEST_F (CaptureSourceCallbackTest, RustRejectsGstreamerResolutionMismatch) {
768+ for (const capture::CaptureResolution resolution :
769+ {capture::CaptureResolution{0 , 240 }, capture::CaptureResolution{320 , 0 }}) {
770+ capture::GstreamerVideoSourceConfig config;
771+ config.pipeline =
772+ " videotestsrc num-buffers=4 ! video/x-raw,width=320,height=240,framerate=30/1 ! "
773+ " vp8enc deadline=1 ! appsink name=lk_appsink" ;
774+ config.resolution = resolution;
775+ auto future = capture::CaptureSource::create (config);
776+ ASSERT_EQ (future.wait_for (std::chrono::seconds (5 )), std::future_status::ready);
777+ expectException ([&future] { return future.get (); },
778+ " CaptureSource::create: Rust source setup failed: invalid request: "
779+ " pipeline produces 320x240, but the configuration declares " +
780+ std::to_string (resolution.width ) + " x" + std::to_string (resolution.height ));
781+ }
692782}
693783
694784// Deliberately exercise invalid enum inputs at the public API boundary.
@@ -705,16 +795,18 @@ TEST(CaptureSourceProtoTest, RejectsInvalidEnumValues) {
705795 capture::GstreamerRateControl{" encoder" , " bitrate" , static_cast <capture::GstreamerBitrateUnit>(99 )};
706796 expectException<std::invalid_argument>([&gstreamer] { return toProto (gstreamer); }, " CaptureSource::create:" );
707797}
708- // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange)
709798
710- TEST (CaptureSourceProtoTest, ValidationErrorsAreDeliveredThroughFuture ) {
799+ TEST (CaptureSourceProtoTest, InvalidEnumsAreDeliveredThroughFuture ) {
711800 std::future<CreationOutcome> future;
712- EXPECT_NO_THROW (future = capture::CaptureSource::create (capture::ClockVideoSourceConfig{}));
801+ const capture::PatternVideoSourceConfig config{{1280 , 720 }, 30 , static_cast <capture::Pattern>(99 )};
802+ EXPECT_NO_THROW (future = capture::CaptureSource::create (config));
713803 ASSERT_TRUE (future.valid ());
714804 expectException<std::invalid_argument>([&future] { return future.get (); },
715- " CaptureSource::create: resolution dimensions must be positive " );
805+ " CaptureSource::create: invalid capture pattern " );
716806}
717807
808+ // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange)
809+
718810TEST (CaptureSourceProtoTest, SendErrorsAreDeliveredThroughFuture) {
719811 FfiClient::instance ().shutdown ();
720812 capture::ClockVideoSourceConfig config;
0 commit comments