|
27 | 27 | #include "data_track_proto_converter.h" |
28 | 28 | #include "ffi.pb.h" |
29 | 29 | #include "livekit/build.h" |
30 | | -#include "livekit/capture/capture_source.h" |
31 | 30 | #include "livekit/data_track_error.h" |
32 | 31 | #include "livekit/ffi_handle.h" |
33 | 32 | #include "livekit/room.h" |
|
37 | 36 | #include "room.pb.h" |
38 | 37 | #include "room_proto_converter.h" |
39 | 38 |
|
| 39 | +#if LIVEKIT_CAPTURE_ENABLED |
| 40 | +#include "livekit/capture/capture_source.h" |
| 41 | +#endif |
| 42 | + |
40 | 43 | namespace livekit { |
41 | 44 |
|
42 | 45 | namespace { |
@@ -764,97 +767,6 @@ std::future<proto::OwnedTrackPublication> FfiClient::publishTrackAsync( |
764 | 767 | return fut; |
765 | 768 | } |
766 | 769 |
|
767 | | -std::future<std::shared_ptr<capture::CaptureSource>> FfiClient::newCaptureSourceAsync( |
768 | | - proto::NewCaptureSourceRequest request, const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send) { |
769 | | - // Generate client-side async_id first |
770 | | - const AsyncId async_id = generateAsyncId(); |
771 | | - |
772 | | - // Register the async handler BEFORE sending the request |
773 | | - auto fut = registerAsync<std::shared_ptr<capture::CaptureSource>>( |
774 | | - async_id, |
775 | | - [async_id](const proto::FfiEvent& event) { |
776 | | - return event.has_new_capture_source() && event.new_capture_source().async_id() == async_id; |
777 | | - }, |
778 | | - [](const proto::FfiEvent& event, std::promise<std::shared_ptr<capture::CaptureSource>>& pr) { |
779 | | - const auto& cb = event.new_capture_source(); |
780 | | - try { |
781 | | - if (cb.has_error()) { |
782 | | - throw capture::CaptureSourceError(cb.error()); |
783 | | - } |
784 | | - if (!cb.has_source()) { |
785 | | - throw capture::CaptureSourceError("NewCaptureSourceCallback missing source"); |
786 | | - } |
787 | | - // Adopt the handles before satisfying the promise. If the caller has |
788 | | - // discarded its future, destroying the pending promise releases the |
789 | | - // wrapper and both handles after this callback completes. |
790 | | - pr.set_value(capture::CaptureSource::fromOwned(cb.source())); |
791 | | - } catch (const std::exception& error) { |
792 | | - pr.set_exception(std::make_exception_ptr(capture::CaptureSourceError(error.what()))); |
793 | | - } catch (...) { |
794 | | - pr.set_exception(std::make_exception_ptr(capture::CaptureSourceError("Capture source construction failed"))); |
795 | | - } |
796 | | - }, |
797 | | - std::make_exception_ptr(capture::CaptureSourceError("Async operation cancelled"))); |
798 | | - |
799 | | - request.set_request_async_id(async_id); |
800 | | - proto::FfiRequest req; |
801 | | - req.mutable_new_capture_source()->CopyFrom(request); |
802 | | - |
803 | | - try { |
804 | | - const proto::FfiResponse resp = send ? send(req) : sendRequest(req); |
805 | | - if (!resp.has_new_capture_source()) { |
806 | | - logAndThrow("FfiResponse missing new_capture_source"); |
807 | | - } |
808 | | - } catch (...) { |
809 | | - cancelPendingByAsyncId(async_id); |
810 | | - throw; |
811 | | - } |
812 | | - |
813 | | - return fut; |
814 | | -} |
815 | | - |
816 | | -std::future<capture::CaptureResult> FfiClient::joinCaptureAsync( |
817 | | - std::uint64_t capture_handle, const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send) { |
818 | | - const AsyncId async_id = generateAsyncId(); |
819 | | - |
820 | | - auto fut = registerAsync<capture::CaptureResult>( |
821 | | - async_id, |
822 | | - [async_id](const proto::FfiEvent& event) { |
823 | | - return event.has_join_capture() && event.join_capture().async_id() == async_id; |
824 | | - }, |
825 | | - [](const proto::FfiEvent& event, std::promise<capture::CaptureResult>& pr) { |
826 | | - const auto& callback = event.join_capture(); |
827 | | - capture::CaptureResult result; |
828 | | - if (callback.has_error()) { |
829 | | - result.error = callback.error().error(); |
830 | | - } else if (callback.has_finished()) { |
831 | | - result.frames_captured = callback.finished().frames_captured(); |
832 | | - result.exit = callback.finished().exit() == proto::CaptureExit::CAPTURE_EXIT_END_OF_STREAM |
833 | | - ? capture::CaptureExit::EndOfStream |
834 | | - : capture::CaptureExit::Stopped; |
835 | | - } |
836 | | - pr.set_value(std::move(result)); |
837 | | - }, |
838 | | - std::make_exception_ptr(capture::CaptureSourceError("Capture join cancelled"))); |
839 | | - |
840 | | - proto::FfiRequest request; |
841 | | - auto* message = request.mutable_join_capture(); |
842 | | - message->set_capture_handle(capture_handle); |
843 | | - message->set_request_async_id(async_id); |
844 | | - |
845 | | - try { |
846 | | - const proto::FfiResponse response = send ? send(request) : sendRequest(request); |
847 | | - if (!response.has_join_capture()) { |
848 | | - logAndThrow("FfiResponse missing join_capture"); |
849 | | - } |
850 | | - } catch (...) { |
851 | | - cancelPendingByAsyncId(async_id); |
852 | | - throw; |
853 | | - } |
854 | | - |
855 | | - return fut; |
856 | | -} |
857 | | - |
858 | 770 | std::future<void> FfiClient::unpublishTrackAsync(std::uint64_t local_participant_handle, const std::string& track_sid, |
859 | 771 | bool stop_on_unpublish) { |
860 | 772 | // Generate client-side async_id first |
@@ -1425,4 +1337,99 @@ std::future<Result<std::string, std::string>> FfiClient::getSchemaAsync(std::uin |
1425 | 1337 | return fut; |
1426 | 1338 | } |
1427 | 1339 |
|
| 1340 | +#if LIVEKIT_CAPTURE_ENABLED |
| 1341 | + |
| 1342 | +std::future<std::shared_ptr<capture::CaptureSource>> FfiClient::newCaptureSourceAsync( |
| 1343 | + proto::NewCaptureSourceRequest request, const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send) { |
| 1344 | + // Generate client-side async_id first |
| 1345 | + const AsyncId async_id = generateAsyncId(); |
| 1346 | + |
| 1347 | + // Register the async handler BEFORE sending the request |
| 1348 | + auto fut = registerAsync<std::shared_ptr<capture::CaptureSource>>( |
| 1349 | + async_id, |
| 1350 | + [async_id](const proto::FfiEvent& event) { |
| 1351 | + return event.has_new_capture_source() && event.new_capture_source().async_id() == async_id; |
| 1352 | + }, |
| 1353 | + [](const proto::FfiEvent& event, std::promise<std::shared_ptr<capture::CaptureSource>>& pr) { |
| 1354 | + const auto& cb = event.new_capture_source(); |
| 1355 | + try { |
| 1356 | + if (cb.has_error()) { |
| 1357 | + throw capture::CaptureSourceError(cb.error()); |
| 1358 | + } |
| 1359 | + if (!cb.has_source()) { |
| 1360 | + throw capture::CaptureSourceError("NewCaptureSourceCallback missing source"); |
| 1361 | + } |
| 1362 | + // Adopt the handles before satisfying the promise. If the caller has |
| 1363 | + // discarded its future, destroying the pending promise releases the |
| 1364 | + // wrapper and both handles after this callback completes. |
| 1365 | + pr.set_value(capture::CaptureSource::fromOwned(cb.source())); |
| 1366 | + } catch (const std::exception& error) { |
| 1367 | + pr.set_exception(std::make_exception_ptr(capture::CaptureSourceError(error.what()))); |
| 1368 | + } catch (...) { |
| 1369 | + pr.set_exception(std::make_exception_ptr(capture::CaptureSourceError("Capture source construction failed"))); |
| 1370 | + } |
| 1371 | + }, |
| 1372 | + std::make_exception_ptr(capture::CaptureSourceError("Async operation cancelled"))); |
| 1373 | + |
| 1374 | + request.set_request_async_id(async_id); |
| 1375 | + proto::FfiRequest req; |
| 1376 | + req.mutable_new_capture_source()->CopyFrom(request); |
| 1377 | + |
| 1378 | + try { |
| 1379 | + const proto::FfiResponse resp = send ? send(req) : sendRequest(req); |
| 1380 | + if (!resp.has_new_capture_source()) { |
| 1381 | + logAndThrow("FfiResponse missing new_capture_source"); |
| 1382 | + } |
| 1383 | + } catch (...) { |
| 1384 | + cancelPendingByAsyncId(async_id); |
| 1385 | + throw; |
| 1386 | + } |
| 1387 | + |
| 1388 | + return fut; |
| 1389 | +} |
| 1390 | + |
| 1391 | +std::future<capture::CaptureResult> FfiClient::joinCaptureAsync( |
| 1392 | + std::uint64_t capture_handle, const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send) { |
| 1393 | + const AsyncId async_id = generateAsyncId(); |
| 1394 | + |
| 1395 | + auto fut = registerAsync<capture::CaptureResult>( |
| 1396 | + async_id, |
| 1397 | + [async_id](const proto::FfiEvent& event) { |
| 1398 | + return event.has_join_capture() && event.join_capture().async_id() == async_id; |
| 1399 | + }, |
| 1400 | + [](const proto::FfiEvent& event, std::promise<capture::CaptureResult>& pr) { |
| 1401 | + const auto& callback = event.join_capture(); |
| 1402 | + capture::CaptureResult result; |
| 1403 | + if (callback.has_error()) { |
| 1404 | + result.error = callback.error().error(); |
| 1405 | + } else if (callback.has_finished()) { |
| 1406 | + result.frames_captured = callback.finished().frames_captured(); |
| 1407 | + result.exit = callback.finished().exit() == proto::CaptureExit::CAPTURE_EXIT_END_OF_STREAM |
| 1408 | + ? capture::CaptureExit::EndOfStream |
| 1409 | + : capture::CaptureExit::Stopped; |
| 1410 | + } |
| 1411 | + pr.set_value(std::move(result)); |
| 1412 | + }, |
| 1413 | + std::make_exception_ptr(capture::CaptureSourceError("Capture join cancelled"))); |
| 1414 | + |
| 1415 | + proto::FfiRequest request; |
| 1416 | + auto* message = request.mutable_join_capture(); |
| 1417 | + message->set_capture_handle(capture_handle); |
| 1418 | + message->set_request_async_id(async_id); |
| 1419 | + |
| 1420 | + try { |
| 1421 | + const proto::FfiResponse response = send ? send(request) : sendRequest(request); |
| 1422 | + if (!response.has_join_capture()) { |
| 1423 | + logAndThrow("FfiResponse missing join_capture"); |
| 1424 | + } |
| 1425 | + } catch (...) { |
| 1426 | + cancelPendingByAsyncId(async_id); |
| 1427 | + throw; |
| 1428 | + } |
| 1429 | + |
| 1430 | + return fut; |
| 1431 | +} |
| 1432 | + |
| 1433 | +#endif |
| 1434 | + |
1428 | 1435 | } // namespace livekit |
0 commit comments