Skip to content

Commit ab79c0f

Browse files
alan-george-lkstephen-derosa
authored andcommitted
Conditional capture build sources
1 parent 9709913 commit ab79c0f

3 files changed

Lines changed: 118 additions & 101 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ include(uniffi_cpp)
418418
add_library(livekit SHARED
419419
src/audio_frame.cpp
420420
src/audio_processing_module.cpp
421-
src/capture_source.cpp
422421
src/audio_source.cpp
423422
src/audio_stream.cpp
424423
src/data_track_frame.cpp
@@ -472,6 +471,11 @@ add_library(livekit SHARED
472471
src/trace/trace_event.h
473472
src/trace/tracing.cpp
474473
)
474+
# Conditionally compile capture sources
475+
if(LIVEKIT_CAPTURE_ENABLED)
476+
target_sources(livekit PRIVATE src/capture_source.cpp)
477+
endif()
478+
475479
# Symbol visibility: keep liblivekit's exported ABI restricted to the public
476480
# LiveKit API. By default everything is hidden unless marked by the LIVEKIT_API
477481
# macro in code

‎src/ffi_client.cpp‎

Lines changed: 99 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "data_track_proto_converter.h"
2828
#include "ffi.pb.h"
2929
#include "livekit/build.h"
30-
#include "livekit/capture/capture_source.h"
3130
#include "livekit/data_track_error.h"
3231
#include "livekit/ffi_handle.h"
3332
#include "livekit/room.h"
@@ -37,6 +36,10 @@
3736
#include "room.pb.h"
3837
#include "room_proto_converter.h"
3938

39+
#if LIVEKIT_CAPTURE_ENABLED
40+
#include "livekit/capture/capture_source.h"
41+
#endif
42+
4043
namespace livekit {
4144

4245
namespace {
@@ -764,97 +767,6 @@ std::future<proto::OwnedTrackPublication> FfiClient::publishTrackAsync(
764767
return fut;
765768
}
766769

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-
858770
std::future<void> FfiClient::unpublishTrackAsync(std::uint64_t local_participant_handle, const std::string& track_sid,
859771
bool stop_on_unpublish) {
860772
// Generate client-side async_id first
@@ -1425,4 +1337,99 @@ std::future<Result<std::string, std::string>> FfiClient::getSchemaAsync(std::uin
14251337
return fut;
14261338
}
14271339

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+
14281435
} // namespace livekit

‎src/ffi_client.h‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "capture.pb.h"
3333
#include "data_track.pb.h"
34+
#include "livekit/build.h"
3435
#include "livekit/data_track_error.h"
3536
#include "livekit/data_track_options.h"
3637
#include "livekit/data_track_schema.h"
@@ -58,10 +59,12 @@ class DataStream;
5859

5960
struct RoomOptions;
6061
struct TrackPublishOptions;
62+
#if LIVEKIT_CAPTURE_ENABLED
6163
namespace capture {
6264
class CaptureSource;
6365
struct CaptureResult;
6466
} // namespace capture
67+
#endif
6568

6669
using FfiCallbackFn = void (*)(const uint8_t*, size_t);
6770
extern "C" void livekit_ffi_initialize(FfiCallbackFn cb, bool capture_logs, const char* sdk, const char* sdk_version);
@@ -132,14 +135,6 @@ class LIVEKIT_INTERNAL_API FfiClient {
132135
const std::string& digit,
133136
const std::vector<std::string>& destination_identities);
134137

135-
// Capture APIs
136-
// Optional request transport permits deterministic callback tests without
137-
// starting a GPU renderer or GStreamer pipeline.
138-
std::future<std::shared_ptr<capture::CaptureSource>> newCaptureSourceAsync(
139-
proto::NewCaptureSourceRequest request,
140-
const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send = {});
141-
std::future<capture::CaptureResult> joinCaptureAsync(
142-
std::uint64_t capture_handle, const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send = {});
143138
std::future<void> setLocalMetadataAsync(std::uint64_t local_participant_handle, const std::string& metadata);
144139
std::future<void> captureAudioFrameAsync(std::uint64_t source_handle, const proto::AudioFrameBufferInfo& buffer);
145140
std::future<std::string> performRpcAsync(std::uint64_t local_participant_handle,
@@ -179,6 +174,17 @@ class LIVEKIT_INTERNAL_API FfiClient {
179174
// of sendRequest.
180175
proto::FfiResponse sendRequest(const proto::FfiRequest& request) const;
181176

177+
#if LIVEKIT_CAPTURE_ENABLED
178+
// Capture APIs
179+
// Optional request transport permits deterministic callback tests without
180+
// starting a GPU renderer or GStreamer pipeline.
181+
std::future<std::shared_ptr<capture::CaptureSource>> newCaptureSourceAsync(
182+
proto::NewCaptureSourceRequest request,
183+
const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send = {});
184+
std::future<capture::CaptureResult> joinCaptureAsync(
185+
std::uint64_t capture_handle, const std::function<proto::FfiResponse(const proto::FfiRequest&)>& send = {});
186+
#endif
187+
182188
private:
183189
friend struct FfiClientTestAccess;
184190

0 commit comments

Comments
 (0)