Skip to content

Commit 6f49856

Browse files
simple_robot -> simple_joystick
1 parent 18ce9f8 commit 6f49856

File tree

9 files changed

+85
-84
lines changed

9 files changed

+85
-84
lines changed

examples/CMakeLists.txt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,46 +117,46 @@ target_link_libraries(SimpleRpc
117117
spdlog::spdlog
118118
)
119119

120-
# --- SimpleRobot example (robot + human executables with shared json_utils) ---
120+
# --- SimpleJoystick example (sender + receiver executables with shared json_utils) ---
121121

122-
add_library(simple_robot_json_utils STATIC
123-
simple_robot/json_utils.cpp
124-
simple_robot/json_utils.h
125-
simple_robot/utils.cpp
126-
simple_robot/utils.h
122+
add_library(simple_joystick_json_utils STATIC
123+
simple_joystick/json_utils.cpp
124+
simple_joystick/json_utils.h
125+
simple_joystick/utils.cpp
126+
simple_joystick/utils.h
127127
)
128128

129-
target_include_directories(simple_robot_json_utils PUBLIC
130-
${CMAKE_CURRENT_SOURCE_DIR}/simple_robot
129+
target_include_directories(simple_joystick_json_utils PUBLIC
130+
${CMAKE_CURRENT_SOURCE_DIR}/simple_joystick
131131
)
132132

133-
target_link_libraries(simple_robot_json_utils
133+
target_link_libraries(simple_joystick_json_utils
134134
PUBLIC
135135
nlohmann_json::nlohmann_json
136136
)
137137

138-
add_executable(SimpleRobot
139-
simple_robot/robot.cpp
138+
add_executable(SimpleJoystickReceiver
139+
simple_joystick/receiver.cpp
140140
)
141141

142-
target_include_directories(SimpleRobot PRIVATE ${EXAMPLES_PRIVATE_INCLUDE_DIRS})
142+
target_include_directories(SimpleJoystickReceiver PRIVATE ${EXAMPLES_PRIVATE_INCLUDE_DIRS})
143143

144-
target_link_libraries(SimpleRobot
144+
target_link_libraries(SimpleJoystickReceiver
145145
PRIVATE
146-
simple_robot_json_utils
146+
simple_joystick_json_utils
147147
livekit
148148
spdlog::spdlog
149149
)
150150

151-
add_executable(SimpleHuman
152-
simple_robot/human.cpp
151+
add_executable(SimpleJoystickSender
152+
simple_joystick/sender.cpp
153153
)
154154

155-
target_include_directories(SimpleHuman PRIVATE ${EXAMPLES_PRIVATE_INCLUDE_DIRS})
155+
target_include_directories(SimpleJoystickSender PRIVATE ${EXAMPLES_PRIVATE_INCLUDE_DIRS})
156156

157-
target_link_libraries(SimpleHuman
157+
target_link_libraries(SimpleJoystickSender
158158
PRIVATE
159-
simple_robot_json_utils
159+
simple_joystick_json_utils
160160
livekit
161161
spdlog::spdlog
162162
)
@@ -269,7 +269,7 @@ if(WIN32)
269269
)
270270

271271
# Copy DLLs to each example's output directory
272-
foreach(EXAMPLE SimpleRoom SimpleRpc SimpleRobot SimpleHuman SimpleDataStream LoggingLevelsBasicUsage LoggingLevelsCustomSinks BridgeRobot BridgeHuman)
272+
foreach(EXAMPLE SimpleRoom SimpleRpc SimpleJoystickSender SimpleJoystickReceiver SimpleDataStream LoggingLevelsBasicUsage LoggingLevelsCustomSinks BridgeRobot BridgeHuman)
273273
foreach(DLL ${REQUIRED_DLLS})
274274
add_custom_command(TARGET ${EXAMPLE} POST_BUILD
275275
COMMAND ${CMAKE_COMMAND} -E copy_if_different
@@ -304,7 +304,7 @@ if(UNIX)
304304
endif()
305305

306306
# Copy shared library to each example's output directory
307-
foreach(EXAMPLE SimpleRoom SimpleRpc SimpleRobot SimpleHuman SimpleDataStream LoggingLevelsBasicUsage LoggingLevelsCustomSinks BridgeRobot BridgeHuman)
307+
foreach(EXAMPLE SimpleRoom SimpleRpc SimpleJoystickSender SimpleJoystickReceiver SimpleDataStream LoggingLevelsBasicUsage LoggingLevelsCustomSinks BridgeRobot BridgeHuman)
308308
add_custom_command(TARGET ${EXAMPLE} POST_BUILD
309309
COMMAND ${CMAKE_COMMAND} -E copy_if_different
310310
"${LIVEKIT_LIB_DIR}/${FFI_SHARED_LIB}"

examples/bridge_human_robot/human.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
* Run alongside the "robot" example (which publishes with identity "robot").
4444
*/
4545

46-
#include "lk_log.h"
4746
#include "livekit/audio_frame.h"
4847
#include "livekit/track.h"
4948
#include "livekit/video_frame.h"
5049
#include "livekit_bridge/livekit_bridge.h"
50+
#include "lk_log.h"
5151
#include "sdl_media.h"
5252

5353
#include <SDL3/SDL.h>

examples/bridge_human_robot/robot.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,10 @@ int main(int argc, char *argv[]) {
374374
auto sim_cam =
375375
bridge.createVideoTrack("robot-sim-frame", kSimWidth, kSimHeight,
376376
livekit::TrackSource::SOURCE_SCREENSHARE);
377-
LK_LOG_INFO("[robot] Publishing {} sim audio ({} Hz, {} ch), cam + sim frame ({}x{} / {}x{}).",
378-
use_mic ? "mic + " : "(no mic) ", kSampleRate, kChannels,
379-
kWidth, kHeight, kSimWidth, kSimHeight);
377+
LK_LOG_INFO("[robot] Publishing {} sim audio ({} Hz, {} ch), cam + sim frame "
378+
"({}x{} / {}x{}).",
379+
use_mic ? "mic + " : "(no mic) ", kSampleRate, kChannels, kWidth,
380+
kHeight, kSimWidth, kSimHeight);
380381

381382
// ----- SDL Mic capture (only when use_mic) -----
382383
// SDLMicSource pulls 10ms frames from the default recording device and
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <nlohmann/json.hpp>
2020
#include <stdexcept>
2121

22-
namespace simple_robot {
22+
namespace simple_joystick {
2323

2424
std::string joystick_to_json(const JoystickCommand &cmd) {
2525
nlohmann::json j;
@@ -43,4 +43,4 @@ JoystickCommand json_to_joystick(const std::string &json) {
4343
}
4444
}
4545

46-
} // namespace simple_robot
46+
} // namespace simple_joystick
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include <string>
2020

21-
namespace simple_robot {
21+
namespace simple_joystick {
2222

2323
/// Represents a joystick command with three axes.
2424
struct JoystickCommand {
@@ -35,4 +35,4 @@ std::string joystick_to_json(const JoystickCommand &cmd);
3535
/// Throws std::runtime_error if the JSON is invalid or missing fields.
3636
JoystickCommand json_to_joystick(const std::string &json);
3737

38-
} // namespace simple_robot
38+
} // namespace simple_joystick
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using namespace std::chrono_literals;
3131
namespace {
3232

3333
std::atomic<bool> g_running{true};
34-
std::atomic<bool> g_human_connected{false};
34+
std::atomic<bool> g_sender_connected{false};
3535

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

@@ -42,7 +42,7 @@ void printUsage(const char *prog) {
4242
<< " " << prog << " --url=<ws-url> --token=<token>\n\n"
4343
<< "Env fallbacks:\n"
4444
<< " LIVEKIT_URL, LIVEKIT_TOKEN\n\n"
45-
<< "This is the 'robot' role. It waits for a 'human' peer to\n"
45+
<< "This is the receiver. It waits for a sender peer to\n"
4646
<< "connect and send joystick commands via RPC.\n"
4747
<< "Exits after 2 minutes if no commands are received.\n";
4848
}
@@ -51,12 +51,12 @@ void printUsage(const char *prog) {
5151

5252
int main(int argc, char *argv[]) {
5353
std::string url, token;
54-
if (!simple_robot::parseArgs(argc, argv, url, token)) {
54+
if (!simple_joystick::parseArgs(argc, argv, url, token)) {
5555
printUsage(argv[0]);
5656
return 1;
5757
}
5858

59-
std::cout << "[Robot] Connecting to: " << url << "\n";
59+
std::cout << "[Receiver] Connecting to: " << url << "\n";
6060
std::signal(SIGINT, handleSignal);
6161

6262
livekit::initialize(livekit::LogLevel::Info, livekit::LogSink::kConsole);
@@ -66,36 +66,36 @@ int main(int argc, char *argv[]) {
6666
options.dynacast = false;
6767

6868
bool res = room->Connect(url, token, options);
69-
std::cout << "[Robot] Connect result: " << std::boolalpha << res << "\n";
69+
std::cout << "[Receiver] Connect result: " << std::boolalpha << res << "\n";
7070
if (!res) {
71-
std::cerr << "[Robot] Failed to connect to room\n";
71+
std::cerr << "[Receiver] Failed to connect to room\n";
7272
livekit::shutdown();
7373
return 1;
7474
}
7575

7676
auto info = room->room_info();
77-
std::cout << "[Robot] Connected to room: " << info.name << "\n";
78-
std::cout << "[Robot] Waiting for 'human' peer (up to 2 minutes)...\n";
77+
std::cout << "[Receiver] Connected to room: " << info.name << "\n";
78+
std::cout << "[Receiver] Waiting for sender peer (up to 2 minutes)...\n";
7979

8080
// Register RPC handler for joystick commands
8181
LocalParticipant *lp = room->localParticipant();
8282
lp->registerRpcMethod(
8383
"joystick_command",
8484
[](const RpcInvocationData &data) -> std::optional<std::string> {
8585
try {
86-
auto cmd = simple_robot::json_to_joystick(data.payload);
87-
g_human_connected.store(true);
88-
std::cout << "[Robot] Joystick from '" << data.caller_identity
86+
auto cmd = simple_joystick::json_to_joystick(data.payload);
87+
g_sender_connected.store(true);
88+
std::cout << "[Receiver] Joystick from '" << data.caller_identity
8989
<< "': x=" << cmd.x << " y=" << cmd.y << " z=" << cmd.z
9090
<< "\n";
9191
return std::optional<std::string>{"ok"};
9292
} catch (const std::exception &e) {
93-
std::cerr << "[Robot] Bad joystick payload: " << e.what() << "\n";
93+
std::cerr << "[Receiver] Bad joystick payload: " << e.what() << "\n";
9494
throw;
9595
}
9696
});
9797

98-
std::cout << "[Robot] RPC handler 'joystick_command' registered. "
98+
std::cout << "[Receiver] RPC handler 'joystick_command' registered. "
9999
<< "Listening for commands...\n";
100100

101101
// Wait up to 2 minutes for activity, then exit as failure
@@ -106,16 +106,16 @@ int main(int argc, char *argv[]) {
106106
}
107107

108108
if (!g_running.load()) {
109-
std::cout << "[Robot] Interrupted by signal. Shutting down.\n";
110-
} else if (!g_human_connected.load()) {
111-
std::cerr << "[Robot] Timed out after 2 minutes with no human connection. "
109+
std::cout << "[Receiver] Interrupted by signal. Shutting down.\n";
110+
} else if (!g_sender_connected.load()) {
111+
std::cerr << "[Receiver] Timed out after 2 minutes with no sender connection. "
112112
<< "Exiting as failure.\n";
113113
room->setDelegate(nullptr);
114114
room.reset();
115115
livekit::shutdown();
116116
return 1;
117117
} else {
118-
std::cout << "[Robot] Session complete.\n";
118+
std::cout << "[Receiver] Session complete.\n";
119119
}
120120

121121
room->setDelegate(nullptr);
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ void printUsage(const char *prog) {
9898
<< " " << prog << " --url=<ws-url> --token=<token>\n\n"
9999
<< "Env fallbacks:\n"
100100
<< " LIVEKIT_URL, LIVEKIT_TOKEN\n\n"
101-
<< "This is the 'human' role. It connects to the room and\n"
102-
<< "continuously checks for a 'robot' peer every 2 seconds.\n"
101+
<< "This is the sender. It connects to the room and\n"
102+
<< "continuously checks for a receiver peer every 2 seconds.\n"
103103
<< "Once connected, use keyboard to send joystick commands:\n"
104104
<< " w / s = +x / -x\n"
105105
<< " d / a = +y / -y\n"
106106
<< " z / c = +z / -z\n"
107107
<< " q = quit\n"
108-
<< "Automatically reconnects if robot leaves.\n";
108+
<< "Automatically reconnects if receiver leaves.\n";
109109
}
110110

111111
void printControls() {
@@ -121,12 +121,12 @@ void printControls() {
121121

122122
int main(int argc, char *argv[]) {
123123
std::string url, token;
124-
if (!simple_robot::parseArgs(argc, argv, url, token)) {
124+
if (!simple_joystick::parseArgs(argc, argv, url, token)) {
125125
printUsage(argv[0]);
126126
return 1;
127127
}
128128

129-
std::cout << "[Human] Connecting to: " << url << "\n";
129+
std::cout << "[Sender] Connecting to: " << url << "\n";
130130
std::signal(SIGINT, handleSignal);
131131

132132
livekit::initialize(livekit::LogLevel::Info, livekit::LogSink::kConsole);
@@ -136,40 +136,40 @@ int main(int argc, char *argv[]) {
136136
options.dynacast = false;
137137

138138
bool res = room->Connect(url, token, options);
139-
std::cout << "[Human] Connect result: " << std::boolalpha << res << "\n";
139+
std::cout << "[Sender] Connect result: " << std::boolalpha << res << "\n";
140140
if (!res) {
141-
std::cerr << "[Human] Failed to connect to room\n";
141+
std::cerr << "[Sender] Failed to connect to room\n";
142142
livekit::shutdown();
143143
return 1;
144144
}
145145

146146
auto info = room->room_info();
147-
std::cout << "[Human] Connected to room: " << info.name << "\n";
147+
std::cout << "[Sender] Connected to room: " << info.name << "\n";
148148

149149
// Enable raw terminal mode for immediate keypress detection
150150
enableRawMode();
151151

152-
std::cout << "[Human] Waiting for 'robot' to join (checking every 2s)...\n";
152+
std::cout << "[Sender] Waiting for 'robot' to join (checking every 2s)...\n";
153153
printControls();
154154

155155
LocalParticipant *lp = room->localParticipant();
156156
double x = 0.0, y = 0.0, z = 0.0;
157-
bool robot_connected = false;
158-
auto last_robot_check = std::chrono::steady_clock::now();
157+
bool receiver_connected = false;
158+
auto last_receiver_check = std::chrono::steady_clock::now();
159159

160160
while (g_running.load()) {
161-
// Periodically check robot presence every 2 seconds
161+
// Periodically check receiver presence every 2 seconds
162162
auto now = std::chrono::steady_clock::now();
163-
if (now - last_robot_check >= 2s) {
164-
last_robot_check = now;
165-
bool robot_present = (room->remoteParticipant("robot") != nullptr);
166-
167-
if (robot_present && !robot_connected) {
168-
std::cout << "[Human] 'robot' connected! Use keys to send commands.\n";
169-
robot_connected = true;
170-
} else if (!robot_present && robot_connected) {
171-
std::cout << "[Human] 'robot' disconnected. Waiting for reconnect...\n";
172-
robot_connected = false;
163+
if (now - last_receiver_check >= 2s) {
164+
last_receiver_check = now;
165+
bool receiver_present = (room->remoteParticipant("robot") != nullptr);
166+
167+
if (receiver_present && !receiver_connected) {
168+
std::cout << "[Sender] Receiver connected! Use keys to send commands.\n";
169+
receiver_connected = true;
170+
} else if (!receiver_present && receiver_connected) {
171+
std::cout << "[Sender] Receiver disconnected. Waiting for reconnect...\n";
172+
receiver_connected = false;
173173
}
174174
}
175175

@@ -182,7 +182,7 @@ int main(int argc, char *argv[]) {
182182

183183
// Handle quit
184184
if (key == 'q' || key == 'Q') {
185-
std::cout << "\n[Human] Quit requested.\n";
185+
std::cout << "\n[Sender] Quit requested.\n";
186186
break;
187187
}
188188

@@ -226,37 +226,37 @@ int main(int argc, char *argv[]) {
226226
if (!changed)
227227
continue;
228228

229-
if (!robot_connected) {
230-
std::cout << "[Human] (no robot connected) x=" << x << " y=" << y
229+
if (!receiver_connected) {
230+
std::cout << "[Sender] (no receiver connected) x=" << x << " y=" << y
231231
<< " z=" << z << "\n";
232232
continue;
233233
}
234234

235235
// Send joystick command via RPC
236-
simple_robot::JoystickCommand cmd{x, y, z};
237-
std::string payload = simple_robot::joystick_to_json(cmd);
236+
simple_joystick::JoystickCommand cmd{x, y, z};
237+
std::string payload = simple_joystick::joystick_to_json(cmd);
238238

239-
std::cout << "[Human] Sending: x=" << x << " y=" << y << " z=" << z << "\n";
239+
std::cout << "[Sender] Sending: x=" << x << " y=" << y << " z=" << z << "\n";
240240

241241
try {
242242
std::string response =
243243
lp->performRpc("robot", "joystick_command", payload, 5.0);
244-
std::cout << "[Human] Robot acknowledged: " << response << "\n";
244+
std::cout << "[Sender] Receiver acknowledged: " << response << "\n";
245245
} catch (const RpcError &e) {
246-
std::cerr << "[Human] RPC error: " << e.message() << "\n";
246+
std::cerr << "[Sender] RPC error: " << e.message() << "\n";
247247
if (static_cast<RpcError::ErrorCode>(e.code()) ==
248248
RpcError::ErrorCode::RECIPIENT_DISCONNECTED) {
249-
std::cout << "[Human] Robot disconnected. Waiting for reconnect...\n";
250-
robot_connected = false;
249+
std::cout << "[Sender] Receiver disconnected. Waiting for reconnect...\n";
250+
receiver_connected = false;
251251
}
252252
} catch (const std::exception &e) {
253-
std::cerr << "[Human] Error sending command: " << e.what() << "\n";
253+
std::cerr << "[Sender] Error sending command: " << e.what() << "\n";
254254
}
255255
}
256256

257257
disableRawMode();
258258

259-
std::cout << "[Human] Done. Shutting down.\n";
259+
std::cout << "[Sender] Done. Shutting down.\n";
260260
room->setDelegate(nullptr);
261261
room.reset();
262262
livekit::shutdown();

0 commit comments

Comments
 (0)