Skip to content

Commit 66efda9

Browse files
Apply clang-format to example collection (#12)
1 parent 46083ea commit 66efda9

41 files changed

Lines changed: 797 additions & 1085 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

basic_room/capture_utils.cpp

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
using namespace livekit;
3030

3131
// Test utils to run a capture loop to publish noisy audio frames to the room
32-
void runNoiseCaptureLoop(const std::shared_ptr<AudioSource> &source,
33-
std::atomic<bool> &running_flag) {
32+
void runNoiseCaptureLoop(const std::shared_ptr<AudioSource>& source, std::atomic<bool>& running_flag) {
3433
const int sample_rate = source->sample_rate();
3534
const int num_channels = source->num_channels();
3635
const int frame_ms = 10;
@@ -44,16 +43,15 @@ void runNoiseCaptureLoop(const std::shared_ptr<AudioSource> &source,
4443
auto next_deadline = Clock::now();
4544

4645
while (running_flag.load(std::memory_order_relaxed)) {
47-
AudioFrame frame =
48-
AudioFrame::create(sample_rate, num_channels, samples_per_channel);
49-
auto &pcm = frame.data();
50-
for (auto &s : pcm) {
46+
AudioFrame frame = AudioFrame::create(sample_rate, num_channels, samples_per_channel);
47+
auto& pcm = frame.data();
48+
for (auto& s : pcm) {
5149
s = static_cast<int16_t>(dist(rng));
5250
}
5351

5452
try {
5553
source->captureFrame(frame);
56-
} catch (const std::exception &e) {
54+
} catch (const std::exception& e) {
5755
std::cerr << "Error in captureFrame (noise): " << e.what() << std::endl;
5856
break;
5957
}
@@ -70,38 +68,35 @@ void runNoiseCaptureLoop(const std::shared_ptr<AudioSource> &source,
7068
}
7169

7270
// Fake video source: solid color cycling
73-
void runFakeVideoCaptureLoop(const std::shared_ptr<VideoSource> &source,
74-
std::atomic<bool> &running_flag) {
71+
void runFakeVideoCaptureLoop(const std::shared_ptr<VideoSource>& source, std::atomic<bool>& running_flag) {
7572
auto frame = VideoFrame::create(1280, 720, VideoBufferType::RGBA);
7673
const double framerate = 1.0 / 30.0;
7774

7875
while (running_flag.load(std::memory_order_relaxed)) {
7976
static auto start = std::chrono::high_resolution_clock::now();
80-
float t = std::chrono::duration<float>(
81-
std::chrono::high_resolution_clock::now() - start)
82-
.count();
77+
float t = std::chrono::duration<float>(std::chrono::high_resolution_clock::now() - start).count();
8378
// Cycle every 4 seconds: 0=red, 1=green, 2=blue, 3=black
8479
int stage = static_cast<int>(t) % 4;
8580

8681
std::array<uint8_t, 4> rgb{};
8782
switch (stage) {
88-
case 0: // red
89-
rgb = {255, 0, 0, 0};
90-
break;
91-
case 1: // green
92-
rgb = {0, 255, 0, 0};
93-
break;
94-
case 2: // blue
95-
rgb = {0, 0, 255, 0};
96-
break;
97-
case 3: // black
98-
default:
99-
rgb = {0, 0, 0, 0};
100-
break;
83+
case 0: // red
84+
rgb = {255, 0, 0, 0};
85+
break;
86+
case 1: // green
87+
rgb = {0, 255, 0, 0};
88+
break;
89+
case 2: // blue
90+
rgb = {0, 0, 255, 0};
91+
break;
92+
case 3: // black
93+
default:
94+
rgb = {0, 0, 0, 0};
95+
break;
10196
}
10297

10398
// ARGB
104-
uint8_t *data = frame.data();
99+
uint8_t* data = frame.data();
105100
const size_t size = frame.dataSize();
106101
for (size_t i = 0; i < size; i += 4) {
107102
data[i + 0] = 255; // A
@@ -114,9 +109,8 @@ void runFakeVideoCaptureLoop(const std::shared_ptr<VideoSource> &source,
114109
// If VideoSource is ARGB-capable, pass frame.
115110
// If it expects I420, pass i420 instead.
116111
source->captureFrame(frame, 0, VideoRotation::VIDEO_ROTATION_0);
117-
} catch (const std::exception &e) {
118-
std::cerr << "Error in captureFrame (fake video): " << e.what()
119-
<< std::endl;
112+
} catch (const std::exception& e) {
113+
std::cerr << "Error in captureFrame (fake video): " << e.what() << std::endl;
120114
break;
121115
}
122116

basic_room/capture_utils.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class AudioSource;
2424
class VideoSource;
2525
} // namespace livekit
2626

27-
void runNoiseCaptureLoop(const std::shared_ptr<livekit::AudioSource> &source,
28-
std::atomic<bool> &running_flag);
27+
void runNoiseCaptureLoop(const std::shared_ptr<livekit::AudioSource>& source, std::atomic<bool>& running_flag);
2928

30-
void runFakeVideoCaptureLoop(
31-
const std::shared_ptr<livekit::VideoSource> &source,
32-
std::atomic<bool> &running_flag);
29+
void runFakeVideoCaptureLoop(const std::shared_ptr<livekit::VideoSource>& source, std::atomic<bool>& running_flag);

basic_room/main.cpp

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,66 +34,58 @@ std::atomic<bool> g_running{true};
3434

3535
void handleSignal(int) { g_running.store(false); }
3636

37-
void printUsage(const char *prog) {
37+
void printUsage(const char* prog) {
3838
std::cerr << "Usage:\n"
3939
<< " " << prog << " --url <ws-url> --token <token>\n"
4040
<< "Env fallbacks:\n"
4141
<< " LIVEKIT_URL, LIVEKIT_TOKEN\n";
4242
}
4343

44-
bool parseArgs(int argc, char *argv[], std::string &url, std::string &token,
45-
bool &self_test) {
44+
bool parseArgs(int argc, char* argv[], std::string& url, std::string& token, bool& self_test) {
4645
for (int i = 1; i < argc; ++i) {
4746
const std::string a = argv[i];
48-
if (a == "-h" || a == "--help")
49-
return false;
47+
if (a == "-h" || a == "--help") return false;
5048

5149
if (a == "--self-test") {
5250
self_test = true;
5351
return true;
5452
}
5553

56-
auto take = [&](std::string &out) -> bool {
57-
if (i + 1 >= argc)
58-
return false;
54+
auto take = [&](std::string& out) -> bool {
55+
if (i + 1 >= argc) return false;
5956
out = argv[++i];
6057
return true;
6158
};
6259

6360
if (a == "--url") {
64-
if (!take(url))
65-
return false;
61+
if (!take(url)) return false;
6662
} else if (a.rfind("--url=", 0) == 0) {
6763
url = a.substr(std::string("--url=").size());
6864
} else if (a == "--token") {
69-
if (!take(token))
70-
return false;
65+
if (!take(token)) return false;
7166
} else if (a.rfind("--token=", 0) == 0) {
7267
token = a.substr(std::string("--token=").size());
7368
}
7469
}
7570

7671
if (url.empty()) {
77-
if (const char *e = std::getenv("LIVEKIT_URL"))
78-
url = e;
72+
if (const char* e = std::getenv("LIVEKIT_URL")) url = e;
7973
}
8074
if (token.empty()) {
81-
if (const char *e = std::getenv("LIVEKIT_TOKEN"))
82-
token = e;
75+
if (const char* e = std::getenv("LIVEKIT_TOKEN")) token = e;
8376
}
8477

8578
return !(url.empty() || token.empty());
8679
}
8780

8881
void print_livekit_version() {
89-
std::cout << "LiveKit version: " << LIVEKIT_BUILD_VERSION_FULL << " ("
90-
<< LIVEKIT_BUILD_FLAVOR << ", commit " << LIVEKIT_BUILD_COMMIT
91-
<< ", built " << LIVEKIT_BUILD_DATE << ")" << std::endl;
82+
std::cout << "LiveKit version: " << LIVEKIT_BUILD_VERSION_FULL << " (" << LIVEKIT_BUILD_FLAVOR << ", commit "
83+
<< LIVEKIT_BUILD_COMMIT << ", built " << LIVEKIT_BUILD_DATE << ")" << std::endl;
9284
}
9385

9486
} // namespace
9587

96-
int main(int argc, char *argv[]) {
88+
int main(int argc, char* argv[]) {
9789
print_livekit_version();
9890
std::string url, token;
9991
bool self_test = false;
@@ -131,8 +123,7 @@ int main(int argc, char *argv[]) {
131123
// ---- Create & publish AUDIO (noise) ----
132124
// Match your runNoiseCaptureLoop pacing: it assumes frame_ms=10.
133125
auto audioSource = std::make_shared<AudioSource>(48000, 1, 10);
134-
auto audioTrack =
135-
LocalAudioTrack::createLocalAudioTrack("noise", audioSource);
126+
auto audioTrack = LocalAudioTrack::createLocalAudioTrack("noise", audioSource);
136127

137128
TrackPublishOptions audioOpts;
138129
audioOpts.source = TrackSource::SOURCE_MICROPHONE;
@@ -144,7 +135,7 @@ int main(int argc, char *argv[]) {
144135
room->localParticipant()->publishTrack(audioTrack, audioOpts);
145136
audioPub = audioTrack->publication();
146137
std::cout << "Published audio: sid=" << audioPub->sid() << "\n";
147-
} catch (const std::exception &e) {
138+
} catch (const std::exception& e) {
148139
std::cerr << "Failed to publish audio: " << e.what() << "\n";
149140
}
150141

@@ -163,18 +154,16 @@ int main(int argc, char *argv[]) {
163154
room->localParticipant()->publishTrack(videoTrack, videoOpts);
164155
videoPub = videoTrack->publication();
165156
std::cout << "Published video: sid=" << videoPub->sid() << "\n";
166-
} catch (const std::exception &e) {
157+
} catch (const std::exception& e) {
167158
std::cerr << "Failed to publish video: " << e.what() << "\n";
168159
}
169160

170161
// ---- Start synthetic capture loops ----
171162
std::atomic<bool> audio_running{true};
172163
std::atomic<bool> video_running{true};
173164

174-
std::thread audioThread(
175-
[&] { runNoiseCaptureLoop(audioSource, audio_running); });
176-
std::thread videoThread(
177-
[&] { runFakeVideoCaptureLoop(videoSource, video_running); });
165+
std::thread audioThread([&] { runNoiseCaptureLoop(audioSource, audio_running); });
166+
std::thread videoThread([&] { runFakeVideoCaptureLoop(videoSource, video_running); });
178167

179168
// Keep alive until Ctrl-C
180169
while (g_running.load()) {
@@ -185,17 +174,13 @@ int main(int argc, char *argv[]) {
185174
audio_running.store(false);
186175
video_running.store(false);
187176

188-
if (audioThread.joinable())
189-
audioThread.join();
190-
if (videoThread.joinable())
191-
videoThread.join();
177+
if (audioThread.joinable()) audioThread.join();
178+
if (videoThread.joinable()) videoThread.join();
192179

193180
// Best-effort unpublish
194181
try {
195-
if (audioPub)
196-
room->localParticipant()->unpublishTrack(audioPub->sid());
197-
if (videoPub)
198-
room->localParticipant()->unpublishTrack(videoPub->sid());
182+
if (audioPub) room->localParticipant()->unpublishTrack(audioPub->sid());
183+
if (videoPub) room->localParticipant()->unpublishTrack(videoPub->sid());
199184
} catch (...) {
200185
}
201186

hello_livekit_receiver/main.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
/// Or via environment variables:
2525
/// LIVEKIT_URL, LIVEKIT_RECEIVER_TOKEN, LIVEKIT_SENDER_IDENTITY
2626

27-
#include "livekit/livekit.h"
28-
2927
#include <atomic>
3028
#include <cassert>
3129
#include <chrono>
@@ -34,21 +32,23 @@
3432
#include <iostream>
3533
#include <thread>
3634

35+
#include "livekit/livekit.h"
36+
3737
using namespace livekit;
3838

39-
constexpr const char *kDataTrackName = "app-data";
40-
constexpr const char *kVideoTrackName = "camera0";
39+
constexpr const char* kDataTrackName = "app-data";
40+
constexpr const char* kVideoTrackName = "camera0";
4141

4242
std::atomic<bool> g_running{true};
4343

4444
void handleSignal(int) { g_running.store(false); }
4545

46-
std::string getenvOrEmpty(const char *name) {
47-
const char *v = std::getenv(name);
46+
std::string getenvOrEmpty(const char* name) {
47+
const char* v = std::getenv(name);
4848
return v ? std::string(v) : std::string{};
4949
}
5050

51-
int main(int argc, char *argv[]) {
51+
int main(int argc, char* argv[]) {
5252
std::string url = getenvOrEmpty("LIVEKIT_URL");
5353
std::string receiver_token = getenvOrEmpty("LIVEKIT_RECEIVER_TOKEN");
5454
std::string sender_identity = getenvOrEmpty("LIVEKIT_SENDER_IDENTITY");
@@ -60,7 +60,8 @@ int main(int argc, char *argv[]) {
6060
}
6161

6262
if (url.empty() || receiver_token.empty() || sender_identity.empty()) {
63-
std::cerr << "[error] Usage: HelloLivekitReceiver <ws-url> <receiver-token> <sender-identity>\n or set LIVEKIT_URL, LIVEKIT_RECEIVER_TOKEN, LIVEKIT_SENDER_IDENTITY\n";
63+
std::cerr << "[error] Usage: HelloLivekitReceiver <ws-url> <receiver-token> <sender-identity>\n or set "
64+
"LIVEKIT_URL, LIVEKIT_RECEIVER_TOKEN, LIVEKIT_SENDER_IDENTITY\n";
6465
return 1;
6566
}
6667

@@ -82,35 +83,35 @@ int main(int argc, char *argv[]) {
8283
return 1;
8384
}
8485

85-
LocalParticipant *lp = room->localParticipant();
86+
LocalParticipant* lp = room->localParticipant();
8687
assert(lp);
8788

88-
std::cout << "[info] [receiver] Connected as identity='" << lp->identity() << "' room='" << room->room_info().name << "'; subscribing to sender identity='" << sender_identity << "'\n";
89+
std::cout << "[info] [receiver] Connected as identity='" << lp->identity() << "' room='" << room->room_info().name
90+
<< "'; subscribing to sender identity='" << sender_identity << "'\n";
8991

9092
int video_frame_count = 0;
91-
room->setOnVideoFrameCallback(
92-
sender_identity, kVideoTrackName,
93-
[&video_frame_count](const VideoFrame &frame, std::int64_t timestamp_us) {
94-
const auto ts_ms =
95-
std::chrono::duration<double, std::milli>(timestamp_us).count();
96-
const int n = video_frame_count++;
97-
if (n % 10 == 0) {
98-
std::cout << "[info] [receiver] Video frame #" << n << " " << frame.width() << "x" << frame.height() << " ts_ms=" << ts_ms << "\n";
99-
}
100-
});
93+
room->setOnVideoFrameCallback(sender_identity, kVideoTrackName,
94+
[&video_frame_count](const VideoFrame& frame, std::int64_t timestamp_us) {
95+
const auto ts_ms = std::chrono::duration<double, std::milli>(timestamp_us).count();
96+
const int n = video_frame_count++;
97+
if (n % 10 == 0) {
98+
std::cout << "[info] [receiver] Video frame #" << n << " " << frame.width() << "x"
99+
<< frame.height() << " ts_ms=" << ts_ms << "\n";
100+
}
101+
});
101102

102103
int data_frame_count = 0;
103104
room->addOnDataFrameCallback(
104105
sender_identity, kDataTrackName,
105-
[&data_frame_count](const std::vector<std::uint8_t> &payload,
106-
std::optional<std::uint64_t> user_ts) {
106+
[&data_frame_count](const std::vector<std::uint8_t>& payload, std::optional<std::uint64_t> user_ts) {
107107
const int n = data_frame_count++;
108108
if (n % 10 == 0) {
109109
std::cout << "[info] [receiver] Data frame #" << n << "\n";
110110
}
111111
});
112112

113-
std::cout << "[info] [receiver] Listening for video track '" << kVideoTrackName << "' + data track '" << kDataTrackName << "'; Ctrl-C to exit\n";
113+
std::cout << "[info] [receiver] Listening for video track '" << kVideoTrackName << "' + data track '"
114+
<< kDataTrackName << "'; Ctrl-C to exit\n";
114115

115116
while (g_running.load()) {
116117
std::this_thread::sleep_for(std::chrono::milliseconds(50));

0 commit comments

Comments
 (0)