Skip to content

Commit 892a374

Browse files
clang
1 parent 7490e49 commit 892a374

3 files changed

Lines changed: 64 additions & 94 deletions

File tree

‎include/livekit/capture_source.h‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,25 @@ class LIVEKIT_API CaptureSource {
143143
public:
144144
/// Terminal notification for a started capture, invoked exactly once on
145145
/// the FFI event thread (like room delegate callbacks).
146-
using FinishedCallback = std::function<void(const CaptureResult &)>;
146+
using FinishedCallback = std::function<void(const CaptureResult&)>;
147147

148148
/// Create a capture source from a GStreamer pipeline configuration.
149149
///
150150
/// Completes asynchronously: construction starts the pipeline and may wait
151151
/// for its first output to discover stream settings. Errors (invalid
152152
/// pipeline, missing capture feature, discovery timeout) are thrown from
153153
/// the future as @ref CaptureSourceError.
154-
static std::future<std::shared_ptr<CaptureSource>>
155-
create(GstreamerVideoSourceConfig config);
154+
static std::future<std::shared_ptr<CaptureSource>> create(GstreamerVideoSourceConfig config);
156155

157156
/// Create the built-in demo capture source (solid cycling colors).
158-
static std::future<std::shared_ptr<CaptureSource>>
159-
create(DemoVideoSourceConfig config);
157+
static std::future<std::shared_ptr<CaptureSource>> create(DemoVideoSourceConfig config);
160158

161159
~CaptureSource();
162160

163-
CaptureSource(const CaptureSource &) = delete;
164-
CaptureSource &operator=(const CaptureSource &) = delete;
165-
CaptureSource(CaptureSource &&) = delete;
166-
CaptureSource &operator=(CaptureSource &&) = delete;
161+
CaptureSource(const CaptureSource&) = delete;
162+
CaptureSource& operator=(const CaptureSource&) = delete;
163+
CaptureSource(CaptureSource&&) = delete;
164+
CaptureSource& operator=(CaptureSource&&) = delete;
167165

168166
/// Kind of media this source produces.
169167
CaptureSourceKind kind() const noexcept { return kind_; }
@@ -177,9 +175,7 @@ class LIVEKIT_API CaptureSource {
177175

178176
/// RTC video source fed by this capture source; use it with
179177
/// LocalVideoTrack::createLocalVideoTrack().
180-
std::shared_ptr<VideoSource> videoSource() const noexcept {
181-
return video_source_;
182-
}
178+
std::shared_ptr<VideoSource> videoSource() const noexcept { return video_source_; }
183179

184180
/// Returns publish options for this track, applying application options.
185181
///
@@ -210,12 +206,10 @@ class LIVEKIT_API CaptureSource {
210206

211207
/// Shared creation path: sends the request and maps the callback payload
212208
/// onto a wrapper instance.
213-
static std::future<std::shared_ptr<CaptureSource>>
214-
createFromRequest(proto::NewCaptureSourceRequest request);
209+
static std::future<std::shared_ptr<CaptureSource>> createFromRequest(proto::NewCaptureSourceRequest request);
215210

216211
/// Builds the wrapper from the callback payload, adopting its handles.
217-
static std::shared_ptr<CaptureSource>
218-
fromOwned(const proto::OwnedCaptureSource &owned);
212+
static std::shared_ptr<CaptureSource> fromOwned(const proto::OwnedCaptureSource& owned);
219213

220214
FfiHandle handle_;
221215
CaptureSourceKind kind_ = CaptureSourceKind::Pixel;

‎src/capture_source.cpp‎

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,33 @@ namespace {
2929

3030
proto::GstreamerBitrateUnit toProto(GstreamerBitrateUnit unit) {
3131
switch (unit) {
32-
case GstreamerBitrateUnit::Bps:
33-
return proto::GstreamerBitrateUnit::GSTREAMER_BITRATE_UNIT_BPS;
34-
case GstreamerBitrateUnit::Kbps:
35-
return proto::GstreamerBitrateUnit::GSTREAMER_BITRATE_UNIT_KBPS;
32+
case GstreamerBitrateUnit::Bps:
33+
return proto::GstreamerBitrateUnit::GSTREAMER_BITRATE_UNIT_BPS;
34+
case GstreamerBitrateUnit::Kbps:
35+
return proto::GstreamerBitrateUnit::GSTREAMER_BITRATE_UNIT_KBPS;
3636
}
3737
return proto::GstreamerBitrateUnit::GSTREAMER_BITRATE_UNIT_BPS;
3838
}
3939

40-
CaptureResult resultFromEvent(const proto::CaptureSourceEvent &event) {
40+
CaptureResult resultFromEvent(const proto::CaptureSourceEvent& event) {
4141
CaptureResult result;
4242
if (event.has_error()) {
4343
result.error = event.error().error();
4444
return result;
4545
}
4646
if (event.has_finished()) {
4747
result.frames_captured = event.finished().frames_captured();
48-
result.exit = event.finished().exit() ==
49-
proto::CaptureExit::CAPTURE_EXIT_END_OF_STREAM
50-
? CaptureExit::EndOfStream
51-
: CaptureExit::Stopped;
48+
result.exit = event.finished().exit() == proto::CaptureExit::CAPTURE_EXIT_END_OF_STREAM ? CaptureExit::EndOfStream
49+
: CaptureExit::Stopped;
5250
}
5351
return result;
5452
}
5553

5654
} // namespace
5755

58-
std::future<std::shared_ptr<CaptureSource>>
59-
CaptureSource::create(GstreamerVideoSourceConfig config) {
56+
std::future<std::shared_ptr<CaptureSource>> CaptureSource::create(GstreamerVideoSourceConfig config) {
6057
proto::NewCaptureSourceRequest request;
61-
auto *gstreamer = request.mutable_gstreamer();
58+
auto* gstreamer = request.mutable_gstreamer();
6259
gstreamer->set_pipeline(config.pipeline);
6360
if (config.codec) {
6461
gstreamer->set_codec(static_cast<proto::VideoCodec>(*config.codec));
@@ -68,23 +65,21 @@ CaptureSource::create(GstreamerVideoSourceConfig config) {
6865
gstreamer->mutable_resolution()->set_height(config.resolution->height);
6966
}
7067
if (config.rate_control) {
71-
auto *rate_control = gstreamer->mutable_rate_control();
68+
auto* rate_control = gstreamer->mutable_rate_control();
7269
rate_control->set_element(config.rate_control->element);
7370
rate_control->set_property(config.rate_control->property);
7471
rate_control->set_unit(toProto(config.rate_control->unit));
7572
}
7673
return createFromRequest(std::move(request));
7774
}
7875

79-
std::future<std::shared_ptr<CaptureSource>>
80-
CaptureSource::create(DemoVideoSourceConfig /*config*/) {
76+
std::future<std::shared_ptr<CaptureSource>> CaptureSource::create(DemoVideoSourceConfig /*config*/) {
8177
proto::NewCaptureSourceRequest request;
8278
request.mutable_demo();
8379
return createFromRequest(std::move(request));
8480
}
8581

86-
std::future<std::shared_ptr<CaptureSource>>
87-
CaptureSource::createFromRequest(proto::NewCaptureSourceRequest request) {
82+
std::future<std::shared_ptr<CaptureSource>> CaptureSource::createFromRequest(proto::NewCaptureSourceRequest request) {
8883
auto owned = FfiClient::instance().newCaptureSourceAsync(std::move(request));
8984
// Map the FFI payload onto a wrapper once the callback resolves. A helper
9085
// thread keeps the returned future's wait semantics standard.
@@ -99,56 +94,49 @@ CaptureSource::createFromRequest(proto::NewCaptureSourceRequest request) {
9994
});
10095
}
10196

102-
std::shared_ptr<CaptureSource>
103-
CaptureSource::fromOwned(const proto::OwnedCaptureSource &owned) {
104-
const proto::CaptureSourceInfo &info = owned.info();
97+
std::shared_ptr<CaptureSource> CaptureSource::fromOwned(const proto::OwnedCaptureSource& owned) {
98+
const proto::CaptureSourceInfo& info = owned.info();
10599

106100
std::shared_ptr<CaptureSource> source(new CaptureSource());
107101
source->handle_ = FfiHandle(static_cast<uintptr_t>(owned.handle().id()));
108-
source->kind_ =
109-
info.kind() == proto::CaptureSourceKind::CAPTURE_SOURCE_ENCODED
110-
? CaptureSourceKind::Encoded
111-
: CaptureSourceKind::Pixel;
102+
source->kind_ = info.kind() == proto::CaptureSourceKind::CAPTURE_SOURCE_ENCODED ? CaptureSourceKind::Encoded
103+
: CaptureSourceKind::Pixel;
112104
source->width_ = static_cast<int>(info.resolution().width());
113105
source->height_ = static_cast<int>(info.resolution().height());
114106
if (info.has_codec()) {
115107
source->codec_ = static_cast<VideoCodec>(info.codec());
116108
}
117109
source->source_publish_options_ = fromProto(info.recommended_publish_options());
118110
source->video_source_ = std::shared_ptr<VideoSource>(new VideoSource(
119-
FfiHandle(static_cast<uintptr_t>(info.video_source().handle().id())),
120-
source->width_, source->height_));
111+
FfiHandle(static_cast<uintptr_t>(info.video_source().handle().id())), source->width_, source->height_));
121112

122113
// The terminal CaptureSourceEvent is unsolicited (not async-id
123114
// correlated); observe it with a listener filtered by our handle. The
124115
// destructor removes the listener before releasing the handle, so no
125116
// callback can outlive the wrapper.
126-
CaptureSource *raw = source.get();
117+
CaptureSource* raw = source.get();
127118
const std::uint64_t capture_handle = source->handle_.get();
128-
source->listener_id_ = FfiClient::instance().addListener(
129-
[raw, capture_handle](const proto::FfiEvent &event) {
130-
if (!event.has_capture_source_event() ||
131-
event.capture_source_event().capture_handle() != capture_handle) {
132-
return;
133-
}
134-
const CaptureResult result =
135-
resultFromEvent(event.capture_source_event());
136-
FinishedCallback callback;
137-
{
138-
const std::scoped_lock lock(raw->callback_mutex_);
139-
callback = raw->on_finished_;
140-
}
141-
if (callback) {
142-
callback(result);
143-
}
144-
});
119+
source->listener_id_ = FfiClient::instance().addListener([raw, capture_handle](const proto::FfiEvent& event) {
120+
if (!event.has_capture_source_event() || event.capture_source_event().capture_handle() != capture_handle) {
121+
return;
122+
}
123+
const CaptureResult result = resultFromEvent(event.capture_source_event());
124+
FinishedCallback callback;
125+
{
126+
const std::scoped_lock lock(raw->callback_mutex_);
127+
callback = raw->on_finished_;
128+
}
129+
if (callback) {
130+
callback(result);
131+
}
132+
});
145133

146134
return source;
147135
}
148136

149137
TrackPublishOptions CaptureSource::publishOptions(TrackPublishOptions options) const {
150-
const TrackPublishOptions &dictated = source_publish_options_;
151-
const auto overlay = [](auto &target, const auto &source) {
138+
const TrackPublishOptions& dictated = source_publish_options_;
139+
const auto overlay = [](auto& target, const auto& source) {
152140
if (source.has_value()) {
153141
target = source;
154142
}

‎src/tests/integration/test_capture_source.cpp‎

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ constexpr int kMinFramesReceived = 5;
4545
std::shared_ptr<CaptureSource> createDemoCaptureOrSkip() {
4646
try {
4747
return CaptureSource::create(DemoVideoSourceConfig{}).get();
48-
} catch (const std::exception &e) {
49-
if (std::string(e.what()).find("without the 'capture' feature") !=
50-
std::string::npos) {
48+
} catch (const std::exception& e) {
49+
if (std::string(e.what()).find("without the 'capture' feature") != std::string::npos) {
5150
return nullptr;
5251
}
5352
throw;
@@ -83,8 +82,7 @@ TEST_F(CaptureSourceServerTest, DemoCaptureSourcePublishesFramesEndToEnd) {
8382
ASSERT_FALSE(sender_room.localParticipant().expired());
8483
ASSERT_FALSE(receiver_room.localParticipant().expired());
8584

86-
const std::string sender_identity =
87-
lockLocalParticipant(sender_room)->identity();
85+
const std::string sender_identity = lockLocalParticipant(sender_room)->identity();
8886
ASSERT_FALSE(sender_identity.empty());
8987
ASSERT_TRUE(waitForParticipant(&receiver_room, sender_identity, 10s));
9088

@@ -95,56 +93,47 @@ TEST_F(CaptureSourceServerTest, DemoCaptureSourcePublishesFramesEndToEnd) {
9593
int frames_received = 0;
9694

9795
const std::string track_name = "demo-capture-track";
98-
receiver_room.setOnVideoFrameEventCallback(
99-
sender_identity, track_name,
100-
[&mutex, &cv, &frames_received](const VideoFrameEvent & /*event*/) {
101-
std::lock_guard<std::mutex> lock(mutex);
102-
if (++frames_received >= kMinFramesReceived) {
103-
cv.notify_all();
104-
}
105-
});
96+
receiver_room.setOnVideoFrameEventCallback(sender_identity, track_name,
97+
[&mutex, &cv, &frames_received](const VideoFrameEvent& /*event*/) {
98+
std::lock_guard<std::mutex> lock(mutex);
99+
if (++frames_received >= kMinFramesReceived) {
100+
cv.notify_all();
101+
}
102+
});
106103

107104
// Publish a track backed by the capture source's RTC video source,
108105
// merging application options over the source-derived ones.
109-
auto track = LocalVideoTrack::createLocalVideoTrack(track_name,
110-
capture->videoSource());
106+
auto track = LocalVideoTrack::createLocalVideoTrack(track_name, capture->videoSource());
111107

112108
// Application options are merged in; source-dictated fields win.
113109
TrackPublishOptions app_options;
114110
app_options.source = TrackSource::SOURCE_CAMERA;
115-
ASSERT_NO_THROW(lockLocalParticipant(sender_room)
116-
->publishTrack(track, capture->publishOptions(app_options)));
111+
ASSERT_NO_THROW(lockLocalParticipant(sender_room)->publishTrack(track, capture->publishOptions(app_options)));
117112

118113
// Observe the capture's terminal event.
119114
std::optional<CaptureResult> finished;
120-
capture->setOnFinishedCallback([&mutex, &cv, &finished](const CaptureResult &result) {
115+
capture->setOnFinishedCallback([&mutex, &cv, &finished](const CaptureResult& result) {
121116
std::lock_guard<std::mutex> lock(mutex);
122117
finished = result;
123118
cv.notify_all();
124119
});
125120

126121
ASSERT_NO_THROW(capture->start());
127-
EXPECT_THROW(capture->start(), CaptureSourceError)
128-
<< "double start must be rejected";
122+
EXPECT_THROW(capture->start(), CaptureSourceError) << "double start must be rejected";
129123

130124
{
131125
std::unique_lock<std::mutex> lock(mutex);
132-
const bool got_frames = cv.wait_for(lock, 30s, [&frames_received] {
133-
return frames_received >= kMinFramesReceived;
134-
});
135-
ASSERT_TRUE(got_frames)
136-
<< "Timed out waiting for demo capture frames; received "
137-
<< frames_received;
126+
const bool got_frames =
127+
cv.wait_for(lock, 30s, [&frames_received] { return frames_received >= kMinFramesReceived; });
128+
ASSERT_TRUE(got_frames) << "Timed out waiting for demo capture frames; received " << frames_received;
138129
}
139130

140131
// Stop is a signal; the terminal callback delivers the stats.
141132
ASSERT_NO_THROW(capture->stop());
142133
{
143134
std::unique_lock<std::mutex> lock(mutex);
144-
const bool got_finished =
145-
cv.wait_for(lock, 10s, [&finished] { return finished.has_value(); });
146-
ASSERT_TRUE(got_finished)
147-
<< "Timed out waiting for the capture finished callback";
135+
const bool got_finished = cv.wait_for(lock, 10s, [&finished] { return finished.has_value(); });
136+
ASSERT_TRUE(got_finished) << "Timed out waiting for the capture finished callback";
148137
}
149138

150139
ASSERT_FALSE(finished->error.has_value()) << *finished->error;
@@ -153,8 +142,7 @@ TEST_F(CaptureSourceServerTest, DemoCaptureSourcePublishesFramesEndToEnd) {
153142

154143
receiver_room.clearOnVideoFrameCallback(sender_identity, track_name);
155144
if (track->publication()) {
156-
lockLocalParticipant(sender_room)
157-
->unpublishTrack(track->publication()->sid());
145+
lockLocalParticipant(sender_room)->unpublishTrack(track->publication()->sid());
158146
}
159147
}
160148

0 commit comments

Comments
 (0)