1616
1717#include " livekit/capture_source.h"
1818
19+ #include < limits>
1920#include < mutex>
2021#include < stdexcept>
2122
2425#include " ffi.pb.h"
2526#include " ffi_client.h"
2627#include " lk_log.h"
27- #include " room_proto_converter.h"
2828
2929namespace livekit {
3030
@@ -35,7 +35,6 @@ struct CaptureSource::Impl {
3535 int height = 0 ;
3636 std::optional<VideoCodec> codec;
3737 std::shared_ptr<VideoSource> video_source;
38- TrackPublishOptions source_publish_options;
3938 std::mutex callback_mutex;
4039 FinishedCallback on_finished;
4140 int listener_id = 0 ;
@@ -182,42 +181,36 @@ proto::NewCaptureSourceRequest toProto(const ClockVideoSourceConfig& config) {
182181std::future<std::shared_ptr<CaptureSource>> CaptureSource::createFromRequest (proto::NewCaptureSourceRequest request) {
183182 // A synchronous send failure is delivered through the
184183 // future rather than thrown from the factory.
185- std::future<proto::OwnedCaptureSource> owned;
186184 try {
187- owned = FfiClient::instance ().newCaptureSourceAsync (std::move (request));
185+ return FfiClient::instance ().newCaptureSourceAsync (std::move (request));
188186 } catch (const std::exception& e) {
189187 return readyCaptureError<std::shared_ptr<CaptureSource>>(e);
190188 }
191-
192- // Map the FFI payload onto a wrapper once the callback resolves. A helper
193- // thread keeps the returned future's wait semantics standard.
194- return std::async (std::launch::async, [owned = std::move (owned)]() mutable {
195- try {
196- return fromOwned (owned.get ());
197- } catch (const CaptureSourceError&) {
198- throw ;
199- } catch (const std::exception& e) {
200- throw CaptureSourceError (e.what ());
201- }
202- });
203189}
204190
205191std::shared_ptr<CaptureSource> CaptureSource::fromOwned (const proto::OwnedCaptureSource& owned) {
206192 const proto::CaptureSourceInfo& info = owned.info ();
193+ // Guard both Rust-owned handles before validation or wrapper allocations.
194+ FfiHandle capture_handle_owner (static_cast <uintptr_t >(owned.handle ().id ()));
195+ FfiHandle video_handle_owner (static_cast <uintptr_t >(info.video_source ().handle ().id ()));
196+ if (!owned.IsInitialized () || info.resolution ().width () == 0 || info.resolution ().height () == 0 ||
197+ info.resolution ().width () > static_cast <std::uint32_t >(std::numeric_limits<int >::max ()) ||
198+ info.resolution ().height () > static_cast <std::uint32_t >(std::numeric_limits<int >::max ()) ||
199+ (info.kind () == proto::CAPTURE_SOURCE_ENCODED && !info.has_codec ())) {
200+ throw CaptureSourceError (" Invalid capture source info" );
201+ }
207202
208203 std::shared_ptr<CaptureSource> source (new CaptureSource ());
209- source->impl_ ->handle = FfiHandle ( static_cast < uintptr_t >(owned. handle (). id ()) );
204+ source->impl_ ->handle = std::move (capture_handle_owner );
210205 source->impl_ ->kind = info.kind () == proto::CaptureSourceKind::CAPTURE_SOURCE_ENCODED ? CaptureSourceKind::Encoded
211206 : CaptureSourceKind::Pixel;
212207 source->impl_ ->width = static_cast <int >(info.resolution ().width ());
213208 source->impl_ ->height = static_cast <int >(info.resolution ().height ());
214209 if (info.has_codec ()) {
215210 source->impl_ ->codec = static_cast <VideoCodec>(info.codec ());
216211 }
217- source->impl_ ->source_publish_options = fromProto (info.recommended_publish_options ());
218- source->impl_ ->video_source =
219- std::shared_ptr<VideoSource>(new VideoSource (FfiHandle (static_cast <uintptr_t >(info.video_source ().handle ().id ())),
220- source->impl_ ->width , source->impl_ ->height ));
212+ source->impl_ ->video_source = std::shared_ptr<VideoSource>(
213+ new VideoSource (std::move (video_handle_owner), source->impl_ ->width , source->impl_ ->height ));
221214
222215 // The terminal CaptureSourceEvent is unsolicited (not async-id
223216 // correlated); observe it with a listener filtered by our handle. The
@@ -244,23 +237,11 @@ std::shared_ptr<CaptureSource> CaptureSource::fromOwned(const proto::OwnedCaptur
244237}
245238
246239TrackPublishOptions CaptureSource::publishOptions (TrackPublishOptions options) const {
247- const auto overlay = [](auto & target, const auto & source) {
248- if (source.has_value ()) {
249- target = source;
250- }
251- };
252- overlay (options.video_encoding , impl_->source_publish_options .video_encoding );
253- overlay (options.audio_encoding , impl_->source_publish_options .audio_encoding );
254- overlay (options.video_codec , impl_->source_publish_options .video_codec );
255- overlay (options.dtx , impl_->source_publish_options .dtx );
256- overlay (options.red , impl_->source_publish_options .red );
257- overlay (options.simulcast , impl_->source_publish_options .simulcast );
258- overlay (options.source , impl_->source_publish_options .source );
259- overlay (options.stream , impl_->source_publish_options .stream );
260- overlay (options.preconnect_buffer , impl_->source_publish_options .preconnect_buffer );
261- overlay (options.frame_metadata_features , impl_->source_publish_options .frame_metadata_features );
262- overlay (options.degradation_preference , impl_->source_publish_options .degradation_preference );
263- overlay (options.video_encoder , impl_->source_publish_options .video_encoder );
240+ if (impl_->kind == CaptureSourceKind::Encoded) {
241+ options.video_codec = impl_->codec ;
242+ options.video_encoder = VideoEncoderBackend::PreEncoded;
243+ options.simulcast = false ;
244+ }
264245 return options;
265246}
266247
0 commit comments