-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
141 lines (114 loc) · 4.51 KB
/
Copy pathmain.cpp
File metadata and controls
141 lines (114 loc) · 4.51 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
/*
* Copyright 2026 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// Pong participant: listens on the "ping" data track and publishes responses
/// on the "pong" data track. Use a token whose identity is `pong`.
#include <atomic>
#include <cassert>
#include <csignal>
#include <cstdint>
#include <exception>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <thread>
#include <vector>
#include "constants.h"
#include "json_converters.h"
#include "livekit/livekit.h"
#include "messages.h"
#include "utils.h"
using namespace livekit;
namespace {
std::atomic<bool> g_running{true};
void handleSignal(int) { g_running.store(false); }
} // namespace
int main(int argc, char* argv[]) {
std::string url = ping_pong::getenvOrEmpty("LIVEKIT_URL");
std::string token = ping_pong::getenvOrEmpty("LIVEKIT_TOKEN");
if (argc >= 3) {
url = argv[1];
token = argv[2];
}
if (url.empty() || token.empty()) {
std::cerr << "[error] LIVEKIT_URL and LIVEKIT_TOKEN (or <ws-url> <token>) are required\n";
return 1;
}
std::signal(SIGINT, handleSignal);
#ifdef SIGTERM
std::signal(SIGTERM, handleSignal);
#endif
livekit::initialize(livekit::LogLevel::Info);
auto room = std::make_unique<Room>();
RoomOptions options;
options.auto_subscribe = true;
options.dynacast = false;
if (!room->connect(url, token, options)) {
std::cerr << "[error] Failed to connect to room\n";
livekit::shutdown();
return 1;
}
LocalParticipant* local_participant = room->localParticipant();
assert(local_participant);
std::cout << "[info] pong connected as identity='" << local_participant->identity() << "' room='"
<< room->roomInfo().name << "'\n";
auto publish_result = local_participant->publishDataTrack(ping_pong::kPongTrackName);
if (!publish_result) {
const auto& error = publish_result.error();
std::cerr << "[error] Failed to publish pong data track: code=" << static_cast<std::uint32_t>(error.code)
<< " message=" << error.message << "\n";
room->setDelegate(nullptr);
room.reset();
livekit::shutdown();
return 1;
}
std::shared_ptr<LocalDataTrack> pong_track = publish_result.value();
const auto callback_id = room->addOnDataFrameCallback(
ping_pong::kPingParticipantIdentity, ping_pong::kPingTrackName,
[pong_track](const std::vector<std::uint8_t>& payload, std::optional<std::uint64_t> /*user_timestamp*/) {
try {
if (payload.empty()) {
std::cout << "[debug] Ignoring empty ping payload\n";
return;
}
const auto ping_message = ping_pong::pingMessageFromJson(ping_pong::toString(payload));
ping_pong::PongMessage pong_message;
pong_message.rec_id = ping_message.id;
pong_message.ts_ns = ping_pong::timeSinceEpochNs();
const std::string json = ping_pong::pongMessageToJson(pong_message);
auto push_result = pong_track->tryPush(ping_pong::toPayload(json));
if (!push_result) {
const auto& error = push_result.error();
std::cerr << "[warn] Failed to push pong data frame: code=" << static_cast<std::uint32_t>(error.code)
<< " message=" << error.message << "\n";
return;
}
std::cout << "[info] received ping id=" << ping_message.id << " ts_ns=" << ping_message.ts_ns
<< " and sent pong rec_id=" << pong_message.rec_id << " ts_ns=" << pong_message.ts_ns << "\n";
} catch (const std::exception& e) {
std::cerr << "[warn] Failed to process ping payload: " << e.what() << "\n";
}
});
std::cout << "[info] published data track '" << ping_pong::kPongTrackName << "' and listening for '"
<< ping_pong::kPingTrackName << "' from '" << ping_pong::kPingParticipantIdentity << "'\n";
while (g_running.load()) {
std::this_thread::sleep_for(ping_pong::kPollPeriod);
}
std::cout << "[info] shutting down pong participant\n";
room.reset();
livekit::shutdown();
return 0;
}