-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.cpp
More file actions
256 lines (223 loc) · 8.05 KB
/
main.cpp
File metadata and controls
256 lines (223 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <atomic>
#include <chrono>
#include <csignal>
#include <cstdlib>
#include <iostream>
#include <random>
#include <string>
#include <thread>
#include <vector>
#include "livekit/livekit.h"
// TODO(shijing), remove this livekit_ffi.h as it should be internal only.
#include "livekit_ffi.h"
using namespace livekit;
namespace {
std::atomic<bool> g_running{true};
void print_usage(const char *prog) {
std::cerr << "Usage:\n"
<< " " << prog << " <ws-url> <token>\n"
<< "or:\n"
<< " " << prog << " --url=<ws-url> --token=<token>\n"
<< " " << prog << " --url <ws-url> --token <token>\n\n"
<< "Env fallbacks:\n"
<< " LIVEKIT_URL, LIVEKIT_TOKEN\n";
}
void handle_sigint(int) { g_running.store(false); }
bool parse_args(int argc, char *argv[], std::string &url, std::string &token) {
// 1) --help
for (int i = 1; i < argc; ++i) {
std::string a = argv[i];
if (a == "-h" || a == "--help") {
return false;
}
}
// 2) flags: --url= / --token= or split form
auto get_flag_value = [&](const std::string &name, int &i) -> std::string {
std::string arg = argv[i];
const std::string eq = name + "=";
if (arg.rfind(name, 0) == 0) { // starts with name
if (arg.size() > name.size() && arg[name.size()] == '=') {
return arg.substr(eq.size());
} else if (i + 1 < argc) {
return std::string(argv[++i]);
}
}
return {};
};
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
if (a.rfind("--url", 0) == 0) {
auto v = get_flag_value("--url", i);
if (!v.empty())
url = v;
} else if (a.rfind("--token", 0) == 0) {
auto v = get_flag_value("--token", i);
if (!v.empty())
token = v;
}
}
// 3) positional if still empty
if (url.empty() || token.empty()) {
std::vector<std::string> pos;
for (int i = 1; i < argc; ++i) {
std::string a = argv[i];
if (a.rfind("--", 0) == 0)
continue; // skip flags we already parsed
pos.push_back(std::move(a));
}
if (pos.size() >= 2) {
if (url.empty())
url = pos[0];
if (token.empty())
token = pos[1];
}
}
// 4) env fallbacks
if (url.empty()) {
const char *e = std::getenv("LIVEKIT_URL");
if (e)
url = e;
}
if (token.empty()) {
const char *e = std::getenv("LIVEKIT_TOKEN");
if (e)
token = e;
}
return !(url.empty() || token.empty());
}
class SimpleRoomDelegate : public livekit::RoomDelegate {
public:
void onParticipantConnected(
livekit::Room & /*room*/,
const livekit::ParticipantConnectedEvent &ev) override {
std::cout << "[Room] participant connected: identity=" << ev.identity
<< " name=" << ev.name << "\n";
}
void onTrackSubscribed(livekit::Room & /*room*/,
const livekit::TrackSubscribedEvent &ev) override {
std::cout << "[Room] track subscribed: participant_identity="
<< ev.participant_identity << " track_sid=" << ev.track_sid
<< " name=" << ev.track_name << "\n";
// TODO(shijing): when you expose Track kind/source here, you can check
// whether this is a video track and start a VideoStream-like consumer. Use
// the python code as reference.
}
};
// Test utils to run a capture loop to publish noisy audio frames to the room
void runNoiseCaptureLoop(const std::shared_ptr<AudioSource> &source) {
const int sample_rate = source->sample_rate();
const int num_channels = source->num_channels();
const int frame_ms = 10;
const int samples_per_channel = sample_rate * frame_ms / 1000;
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int16_t> noise_dist(-5000, 5000);
using Clock = std::chrono::steady_clock;
auto next_deadline = Clock::now();
while (g_running.load(std::memory_order_relaxed)) {
AudioFrame frame =
AudioFrame::create(sample_rate, num_channels, samples_per_channel);
const std::size_t total_samples =
static_cast<std::size_t>(num_channels) *
static_cast<std::size_t>(samples_per_channel);
for (std::size_t i = 0; i < total_samples; ++i) {
frame.data()[i] = noise_dist(rng);
}
try {
source->captureFrame(frame);
} catch (const std::exception &e) {
// If something goes wrong, log and break out
std::cerr << "Error in captureFrame: " << e.what() << std::endl;
break;
}
// Pace the loop to roughly real-time
next_deadline += std::chrono::milliseconds(frame_ms);
std::this_thread::sleep_until(next_deadline);
}
// Optionally clear queued audio on exit
try {
source->clearQueue();
} catch (...) {
// ignore errors on shutdown
std::cout << "Error in clearQueue" << std::endl;
}
}
} // namespace
int main(int argc, char *argv[]) {
std::string url, token;
if (!parse_args(argc, argv, url, token)) {
print_usage(argv[0]);
return 1;
}
// Exit if token and url are not set
if (url.empty() || token.empty()) {
std::cerr << "LIVEKIT_URL and LIVEKIT_TOKEN (or CLI args) are required\n";
return 1;
}
std::cout << "Connecting to: " << url << std::endl;
// Handle Ctrl-C to exit the idle loop
std::signal(SIGINT, handle_sigint);
livekit::Room room{};
SimpleRoomDelegate delegate;
room.setDelegate(&delegate);
bool res = room.Connect(url, token);
std::cout << "Connect result is " << std::boolalpha << res << std::endl;
if (!res) {
std::cerr << "Failed to connect to room\n";
FfiClient::instance().shutdown();
return 1;
}
auto info = room.room_info();
std::cout << "Connected to room:\n"
<< " SID: " << (info.sid ? *info.sid : "(none)") << "\n"
<< " Name: " << info.name << "\n"
<< " Metadata: " << info.metadata << "\n"
<< " Max participants: " << info.max_participants << "\n"
<< " Num participants: " << info.num_participants << "\n"
<< " Num publishers: " << info.num_publishers << "\n"
<< " Active recording: " << (info.active_recording ? "yes" : "no")
<< "\n"
<< " Empty timeout (s): " << info.empty_timeout << "\n"
<< " Departure timeout (s): " << info.departure_timeout << "\n"
<< " Lossy DC low threshold: "
<< info.lossy_dc_buffered_amount_low_threshold << "\n"
<< " Reliable DC low threshold: "
<< info.reliable_dc_buffered_amount_low_threshold << "\n"
<< " Creation time (ms): " << info.creation_time << "\n";
auto audioSource = std::make_shared<AudioSource>(44100, 1, 10);
auto audioTrack =
LocalAudioTrack::createLocalAudioTrack("micTrack", audioSource);
TrackPublishOptions opts;
opts.source = TrackSource::SOURCE_MICROPHONE;
opts.dtx = false;
opts.simulcast = false;
try {
// publishTrack takes std::shared_ptr<Track>, LocalAudioTrack derives from
// Track
auto pub = room.local_participant()->publishTrack(audioTrack, opts);
std::cout << "Published track:\n"
<< " SID: " << pub->sid() << "\n"
<< " Name: " << pub->name() << "\n"
<< " Kind: " << static_cast<int>(pub->kind()) << "\n"
<< " Source: " << static_cast<int>(pub->source()) << "\n"
<< " Simulcasted: " << std::boolalpha << pub->simulcasted()
<< "\n"
<< " Muted: " << std::boolalpha << pub->muted() << "\n";
} catch (const std::exception &e) {
std::cerr << "Failed to publish track: " << e.what() << std::endl;
}
// TODO, if we have pre-buffering feature, we might consider starting the
// thread right after creating the source.
std::thread audioThread(runNoiseCaptureLoop, audioSource);
// Keep the app alive until Ctrl-C so we continue receiving events,
// similar to asyncio.run(main()) keeping the loop running.
while (g_running.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// Shutdown the audio thread.
if (audioThread.joinable()) {
audioThread.join();
}
FfiClient::instance().shutdown();
std::cout << "Exiting.\n";
return 0;
}