Skip to content

Commit 402e6fb

Browse files
raw ptr to weak_ptr
1 parent 361237b commit 402e6fb

12 files changed

Lines changed: 74 additions & 37 deletions

File tree

basic_room/main.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ int main(int argc, char* argv[]) {
132132

133133
std::shared_ptr<LocalTrackPublication> audioPub;
134134
try {
135-
room->localParticipant()->publishTrack(audioTrack, audioOpts);
135+
auto lp = room->localParticipant().lock();
136+
if (!lp) throw std::runtime_error("local participant unavailable");
137+
lp->publishTrack(audioTrack, audioOpts);
136138
audioPub = audioTrack->publication();
137139
std::cout << "Published audio: sid=" << audioPub->sid() << "\n";
138140
} catch (const std::exception& e) {
@@ -151,7 +153,9 @@ int main(int argc, char* argv[]) {
151153

152154
std::shared_ptr<LocalTrackPublication> videoPub;
153155
try {
154-
room->localParticipant()->publishTrack(videoTrack, videoOpts);
156+
auto lp = room->localParticipant().lock();
157+
if (!lp) throw std::runtime_error("local participant unavailable");
158+
lp->publishTrack(videoTrack, videoOpts);
155159
videoPub = videoTrack->publication();
156160
std::cout << "Published video: sid=" << videoPub->sid() << "\n";
157161
} catch (const std::exception& e) {
@@ -179,8 +183,10 @@ int main(int argc, char* argv[]) {
179183

180184
// Best-effort unpublish
181185
try {
182-
if (audioPub) room->localParticipant()->unpublishTrack(audioPub->sid());
183-
if (videoPub) room->localParticipant()->unpublishTrack(videoPub->sid());
186+
if (auto lp = room->localParticipant().lock()) {
187+
if (audioPub) lp->unpublishTrack(audioPub->sid());
188+
if (videoPub) lp->unpublishTrack(videoPub->sid());
189+
}
184190
} catch (...) {
185191
}
186192

hello_livekit/receiver/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int main(int argc, char* argv[]) {
8383
return 1;
8484
}
8585

86-
LocalParticipant* lp = room->localParticipant();
86+
auto lp = room->localParticipant().lock();
8787
assert(lp);
8888

8989
std::cout << "[info] [receiver] Connected as identity='" << lp->identity() << "' room='" << room->roomInfo().name

hello_livekit/sender/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int main(int argc, char* argv[]) {
8484
return 1;
8585
}
8686

87-
LocalParticipant* lp = room->localParticipant();
87+
auto lp = room->localParticipant().lock();
8888
assert(lp);
8989

9090
std::cout << "[info] [sender] Connected as identity='" << lp->identity() << "' room='" << room->roomInfo().name

ping_pong/ping/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int main(int argc, char* argv[]) {
9898
return 1;
9999
}
100100

101-
LocalParticipant* local_participant = room->localParticipant();
101+
auto local_participant = room->localParticipant().lock();
102102
assert(local_participant);
103103

104104
std::cout << "[info] ping connected as identity='" << local_participant->identity() << "' room='"

ping_pong/pong/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int main(int argc, char* argv[]) {
7777
return 1;
7878
}
7979

80-
LocalParticipant* local_participant = room->localParticipant();
80+
auto local_participant = room->localParticipant().lock();
8181
assert(local_participant);
8282

8383
std::cout << "[info] pong connected as identity='" << local_participant->identity() << "' room='"

simple_data_stream/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ std::string randomHexId(std::size_t nbytes = 16) {
6464
void greetParticipant(Room* room, const std::string& identity) {
6565
std::cout << "[DataStream] Greeting participant: " << identity << "\n";
6666

67-
LocalParticipant* lp = room->localParticipant();
67+
auto lp = room->localParticipant().lock();
6868
if (!lp) {
6969
std::cerr << "[DataStream] No local participant, cannot greet.\n";
7070
return;
@@ -240,7 +240,8 @@ int main(int argc, char* argv[]) {
240240
// Greet existing participants
241241
{
242242
auto remotes = room->remoteParticipants();
243-
for (const auto& rp : remotes) {
243+
for (const auto& weak_rp : remotes) {
244+
auto rp = weak_rp.lock();
244245
if (!rp) continue;
245246
std::cout << "Remote: " << rp->identity() << "\n";
246247
greetParticipant(room.get(), rp->identity());

simple_joystick/receiver/main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,14 @@ int main(int argc, char* argv[]) {
7878
std::cout << "[Receiver] Waiting for sender peer (up to 2 minutes)...\n";
7979

8080
// Register RPC handler for joystick commands
81-
LocalParticipant* lp = room->localParticipant();
81+
auto lp = room->localParticipant().lock();
82+
if (!lp) {
83+
std::cerr << "[Receiver] No local participant; cannot register RPC handler.\n";
84+
room->setDelegate(nullptr);
85+
room.reset();
86+
livekit::shutdown();
87+
return 1;
88+
}
8289
lp->registerRpcMethod("joystick_command", [](const RpcInvocationData& data) -> std::optional<std::string> {
8390
try {
8491
auto cmd = simple_joystick::json_to_joystick(data.payload);

simple_joystick/sender/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ int main(int argc, char* argv[]) {
150150
std::cout << "[Sender] Waiting for 'robot' to join (checking every 2s)...\n";
151151
printControls();
152152

153-
LocalParticipant* lp = room->localParticipant();
154153
double x = 0.0, y = 0.0, z = 0.0;
155154
bool receiver_connected = false;
156155
auto last_receiver_check = std::chrono::steady_clock::now();
@@ -160,7 +159,7 @@ int main(int argc, char* argv[]) {
160159
auto now = std::chrono::steady_clock::now();
161160
if (now - last_receiver_check >= 2s) {
162161
last_receiver_check = now;
163-
bool receiver_present = (room->remoteParticipant("robot") != nullptr);
162+
bool receiver_present = (room->remoteParticipant("robot").lock() != nullptr);
164163

165164
if (receiver_present && !receiver_connected) {
166165
std::cout << "[Sender] Receiver connected! Use keys to send commands.\n";
@@ -235,6 +234,8 @@ int main(int argc, char* argv[]) {
235234
std::cout << "[Sender] Sending: x=" << x << " y=" << y << " z=" << z << "\n";
236235

237236
try {
237+
auto lp = room->localParticipant().lock();
238+
if (!lp) throw std::runtime_error("local participant unavailable");
238239
std::string response = lp->performRpc("robot", "joystick_command", payload, 5.0);
239240
std::cout << "[Sender] Receiver acknowledged: " << response << "\n";
240241
} catch (const RpcError& e) {

simple_room/main.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ int main(int argc, char* argv[]) {
304304
audioOpts.dtx = false;
305305
audioOpts.simulcast = false;
306306
try {
307-
room->localParticipant()->publishTrack(audioTrack, audioOpts);
307+
auto lp = room->localParticipant().lock();
308+
if (!lp) throw std::runtime_error("local participant unavailable");
309+
lp->publishTrack(audioTrack, audioOpts);
308310
const auto audioPub = audioTrack->publication();
309311

310312
std::cout << "Published track:\n"
@@ -331,7 +333,9 @@ int main(int argc, char* argv[]) {
331333
try {
332334
// publishTrack takes std::shared_ptr<Track>, LocalAudioTrack derives from
333335
// Track
334-
room->localParticipant()->publishTrack(videoTrack, videoOpts);
336+
auto lp = room->localParticipant().lock();
337+
if (!lp) throw std::runtime_error("local participant unavailable");
338+
lp->publishTrack(videoTrack, videoOpts);
335339

336340
const auto videoPub = videoTrack->publication();
337341

@@ -368,11 +372,13 @@ int main(int argc, char* argv[]) {
368372
// Must be cleaned up before FfiClient::instance().shutdown();
369373
room->setDelegate(nullptr);
370374

371-
if (audioTrack->publication()) {
372-
room->localParticipant()->unpublishTrack(audioTrack->publication()->sid());
373-
}
374-
if (videoTrack->publication()) {
375-
room->localParticipant()->unpublishTrack(videoTrack->publication()->sid());
375+
if (auto lp = room->localParticipant().lock()) {
376+
if (audioTrack->publication()) {
377+
lp->unpublishTrack(audioTrack->publication()->sid());
378+
}
379+
if (videoTrack->publication()) {
380+
lp->unpublishTrack(videoTrack->publication()->sid());
381+
}
376382
}
377383
audioTrack.reset();
378384
videoTrack.reset();

simple_rpc/main.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ bool waitForParticipant(Room* room, const std::string& identity, std::chrono::mi
6666
auto start = std::chrono::steady_clock::now();
6767

6868
while (std::chrono::steady_clock::now() - start < timeout) {
69-
if (room->remoteParticipant(identity) != nullptr) {
69+
if (room->remoteParticipant(identity).lock() != nullptr) {
7070
return true;
7171
}
7272
std::this_thread::sleep_for(100ms);
@@ -207,8 +207,11 @@ std::string parseStringFromJson(const std::string& json) {
207207

208208
// RPC handler registration
209209
void registerReceiverMethods(Room* greeters_room, Room* math_genius_room) {
210-
LocalParticipant* greeter_lp = greeters_room->localParticipant();
211-
LocalParticipant* math_genius_lp = math_genius_room->localParticipant();
210+
auto greeter_lp = greeters_room->localParticipant().lock();
211+
auto math_genius_lp = math_genius_room->localParticipant().lock();
212+
if (!greeter_lp || !math_genius_lp) {
213+
throw std::runtime_error("local participant unavailable");
214+
}
212215

213216
// arrival
214217
greeter_lp->registerRpcMethod("arrival", [](const RpcInvocationData& data) -> std::optional<std::string> {
@@ -274,7 +277,9 @@ void performGreeting(Room* room) {
274277
std::cout << "[Caller] Letting the greeter know that I've arrived\n";
275278
double t0 = nowMs();
276279
try {
277-
std::string response = room->localParticipant()->performRpc("greeter", "arrival", "Hello", std::nullopt);
280+
auto lp = room->localParticipant().lock();
281+
if (!lp) throw std::runtime_error("local participant unavailable");
282+
std::string response = lp->performRpc("greeter", "arrival", "Hello", std::nullopt);
278283
double t1 = nowMs();
279284
std::cout << "[Caller] RTT: " << (t1 - t0) << " ms\n";
280285
std::cout << "[Caller] That's nice, the greeter said: \"" << response << "\"\n";
@@ -291,7 +296,9 @@ void performSquareRoot(Room* room) {
291296
double t0 = nowMs();
292297
try {
293298
std::string payload = makeNumberJson("number", 16.0);
294-
std::string response = room->localParticipant()->performRpc("math-genius", "square-root", payload, std::nullopt);
299+
auto lp = room->localParticipant().lock();
300+
if (!lp) throw std::runtime_error("local participant unavailable");
301+
std::string response = lp->performRpc("math-genius", "square-root", payload, std::nullopt);
295302
double t1 = nowMs();
296303
std::cout << "[Caller] RTT: " << (t1 - t0) << " ms\n";
297304
double result = parseNumberFromJson(response);
@@ -311,8 +318,10 @@ void performQuantumHyperGeometricSeries(Room* room) {
311318
double t0 = nowMs();
312319
try {
313320
std::string payload = makeNumberJson("number", 42.0);
321+
auto lp = room->localParticipant().lock();
322+
if (!lp) throw std::runtime_error("local participant unavailable");
314323
std::string response =
315-
room->localParticipant()->performRpc("math-genius", "quantum-hypergeometric-series", payload, std::nullopt);
324+
lp->performRpc("math-genius", "quantum-hypergeometric-series", payload, std::nullopt);
316325
double t1 = nowMs();
317326
std::cout << "[Caller] (Unexpected success) RTT=" << (t1 - t0) << " ms\n";
318327
std::cout << "[Caller] Result: " << response << "\n";
@@ -337,7 +346,9 @@ void performDivide(Room* room) {
337346
double t0 = nowMs();
338347
try {
339348
std::string payload = "{\"dividend\":10,\"divisor\":0}";
340-
std::string response = room->localParticipant()->performRpc("math-genius", "divide", payload, std::nullopt);
349+
auto lp = room->localParticipant().lock();
350+
if (!lp) throw std::runtime_error("local participant unavailable");
351+
std::string response = lp->performRpc("math-genius", "divide", payload, std::nullopt);
341352
double t1 = nowMs();
342353
std::cout << "[Caller] (Unexpected success) RTT=" << (t1 - t0) << " ms\n";
343354
std::cout << "[Caller] Result = " << response << "\n";
@@ -361,7 +372,9 @@ void performLongCalculation(Room* room) {
361372
std::cout << "[Caller] Giving only 10s to respond. EXPECTED RESULT: TIMEOUT.\n";
362373
double t0 = nowMs();
363374
try {
364-
std::string response = room->localParticipant()->performRpc("math-genius", "long-calculation", "{}", 10.0);
375+
auto lp = room->localParticipant().lock();
376+
if (!lp) throw std::runtime_error("local participant unavailable");
377+
std::string response = lp->performRpc("math-genius", "long-calculation", "{}", 10.0);
365378
double t1 = nowMs();
366379
std::cout << "[Caller] (Unexpected success) RTT=" << (t1 - t0) << " ms\n";
367380
std::cout << "[Caller] Result: " << response << "\n";

0 commit comments

Comments
 (0)