|
| 1 | +/* |
| 2 | + * Copyright 2026 LiveKit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * UserTimestampedVideoConsumer |
| 19 | + * |
| 20 | + * Receives remote camera frames via Room::setOnVideoFrameEventCallback() and |
| 21 | + * logs any VideoFrameMetadata::user_timestamp_us values that arrive. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <atomic> |
| 25 | +#include <csignal> |
| 26 | +#include <cstdlib> |
| 27 | +#include <iostream> |
| 28 | +#include <mutex> |
| 29 | +#include <optional> |
| 30 | +#include <string> |
| 31 | +#include <thread> |
| 32 | +#include <unordered_set> |
| 33 | +#include <vector> |
| 34 | + |
| 35 | +#include "livekit/livekit.h" |
| 36 | + |
| 37 | +using namespace livekit; |
| 38 | + |
| 39 | +namespace { |
| 40 | + |
| 41 | +std::atomic<bool> g_running{true}; |
| 42 | + |
| 43 | +void handleSignal(int) { g_running.store(false); } |
| 44 | + |
| 45 | +std::string getenvOrEmpty(const char *name) { |
| 46 | + const char *value = std::getenv(name); |
| 47 | + return value ? std::string(value) : std::string{}; |
| 48 | +} |
| 49 | + |
| 50 | +std::string |
| 51 | +formatUserTimestamp(const std::optional<VideoFrameMetadata> &metadata) { |
| 52 | + if (!metadata || !metadata->user_timestamp_us.has_value()) { |
| 53 | + return "n/a"; |
| 54 | + } |
| 55 | + |
| 56 | + return std::to_string(*metadata->user_timestamp_us); |
| 57 | +} |
| 58 | + |
| 59 | +void printUsage(const char *program) { |
| 60 | + std::cerr << "Usage:\n" |
| 61 | + << " " << program << " <ws-url> <token> [--ignore-user-timestamp]\n" |
| 62 | + << "or:\n" |
| 63 | + << " LIVEKIT_URL=... LIVEKIT_TOKEN=... " << program |
| 64 | + << " [--ignore-user-timestamp]\n"; |
| 65 | +} |
| 66 | + |
| 67 | +bool parseArgs(int argc, char *argv[], std::string &url, std::string &token, |
| 68 | + bool &read_user_timestamp) { |
| 69 | + read_user_timestamp = true; |
| 70 | + std::vector<std::string> positional; |
| 71 | + |
| 72 | + for (int i = 1; i < argc; ++i) { |
| 73 | + const std::string arg = argv[i]; |
| 74 | + if (arg == "-h" || arg == "--help") { |
| 75 | + return false; |
| 76 | + } |
| 77 | + if (arg == "--ignore-user-timestamp") { |
| 78 | + read_user_timestamp = false; |
| 79 | + continue; |
| 80 | + } |
| 81 | + if (arg == "--read-user-timestamp") { |
| 82 | + read_user_timestamp = true; |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + positional.push_back(arg); |
| 87 | + } |
| 88 | + |
| 89 | + url = getenvOrEmpty("LIVEKIT_URL"); |
| 90 | + token = getenvOrEmpty("LIVEKIT_TOKEN"); |
| 91 | + |
| 92 | + if (positional.size() >= 2) { |
| 93 | + url = positional[0]; |
| 94 | + token = positional[1]; |
| 95 | + } |
| 96 | + |
| 97 | + return !(url.empty() || token.empty()); |
| 98 | +} |
| 99 | + |
| 100 | +class UserTimestampedVideoConsumerDelegate : public RoomDelegate { |
| 101 | +public: |
| 102 | + UserTimestampedVideoConsumerDelegate(Room &room, bool read_user_timestamp) |
| 103 | + : room_(room), read_user_timestamp_(read_user_timestamp) {} |
| 104 | + |
| 105 | + void registerExistingParticipants() { |
| 106 | + for (const auto &participant : room_.remoteParticipants()) { |
| 107 | + if (participant) { |
| 108 | + registerRemoteCameraCallback(participant->identity()); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + void onParticipantConnected(Room &, |
| 114 | + const ParticipantConnectedEvent &event) override { |
| 115 | + if (!event.participant) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + std::cout << "[consumer] participant connected: " |
| 120 | + << event.participant->identity() << "\n"; |
| 121 | + registerRemoteCameraCallback(event.participant->identity()); |
| 122 | + } |
| 123 | + |
| 124 | + void onParticipantDisconnected( |
| 125 | + Room &, const ParticipantDisconnectedEvent &event) override { |
| 126 | + if (!event.participant) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + const std::string identity = event.participant->identity(); |
| 131 | + room_.clearOnVideoFrameCallback(identity, TrackSource::SOURCE_CAMERA); |
| 132 | + |
| 133 | + { |
| 134 | + std::lock_guard<std::mutex> lock(mutex_); |
| 135 | + registered_identities_.erase(identity); |
| 136 | + } |
| 137 | + |
| 138 | + std::cout << "[consumer] participant disconnected: " << identity << "\n"; |
| 139 | + } |
| 140 | + |
| 141 | +private: |
| 142 | + void registerRemoteCameraCallback(const std::string &identity) { |
| 143 | + { |
| 144 | + std::lock_guard<std::mutex> lock(mutex_); |
| 145 | + if (!registered_identities_.insert(identity).second) { |
| 146 | + return; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + VideoStream::Options stream_options; |
| 151 | + stream_options.format = VideoBufferType::RGBA; |
| 152 | + |
| 153 | + if (read_user_timestamp_) { |
| 154 | + room_.setOnVideoFrameEventCallback( |
| 155 | + identity, TrackSource::SOURCE_CAMERA, |
| 156 | + [identity](const VideoFrameEvent &event) { |
| 157 | + std::cout << "[consumer] from=" << identity |
| 158 | + << " size=" << event.frame.width() << "x" |
| 159 | + << event.frame.height() |
| 160 | + << " capture_ts_us=" << event.timestamp_us |
| 161 | + << " user_ts_us=" << formatUserTimestamp(event.metadata) |
| 162 | + << " rotation=" << static_cast<int>(event.rotation) |
| 163 | + << "\n"; |
| 164 | + }, |
| 165 | + stream_options); |
| 166 | + } else { |
| 167 | + room_.setOnVideoFrameCallback( |
| 168 | + identity, TrackSource::SOURCE_CAMERA, |
| 169 | + [identity](const VideoFrame &frame, const std::int64_t timestamp_us) { |
| 170 | + std::cout << "[consumer] from=" << identity |
| 171 | + << " size=" << frame.width() << "x" << frame.height() |
| 172 | + << " capture_ts_us=" << timestamp_us |
| 173 | + << " user_ts_us=ignored\n"; |
| 174 | + }, |
| 175 | + stream_options); |
| 176 | + } |
| 177 | + |
| 178 | + std::cout << "[consumer] listening for camera frames from " << identity |
| 179 | + << " with user timestamp " |
| 180 | + << (read_user_timestamp_ ? "enabled" : "ignored") << "\n"; |
| 181 | + } |
| 182 | + |
| 183 | + Room &room_; |
| 184 | + bool read_user_timestamp_; |
| 185 | + std::mutex mutex_; |
| 186 | + std::unordered_set<std::string> registered_identities_; |
| 187 | +}; |
| 188 | + |
| 189 | +} // namespace |
| 190 | + |
| 191 | +int main(int argc, char *argv[]) { |
| 192 | + std::string url; |
| 193 | + std::string token; |
| 194 | + bool read_user_timestamp = true; |
| 195 | + |
| 196 | + if (!parseArgs(argc, argv, url, token, read_user_timestamp)) { |
| 197 | + printUsage(argv[0]); |
| 198 | + return 1; |
| 199 | + } |
| 200 | + |
| 201 | + std::signal(SIGINT, handleSignal); |
| 202 | +#ifdef SIGTERM |
| 203 | + std::signal(SIGTERM, handleSignal); |
| 204 | +#endif |
| 205 | + |
| 206 | + livekit::initialize(livekit::LogLevel::Info, livekit::LogSink::kConsole); |
| 207 | + int exit_code = 0; |
| 208 | + |
| 209 | + { |
| 210 | + Room room; |
| 211 | + RoomOptions options; |
| 212 | + options.auto_subscribe = true; |
| 213 | + options.dynacast = false; |
| 214 | + |
| 215 | + UserTimestampedVideoConsumerDelegate delegate(room, read_user_timestamp); |
| 216 | + room.setDelegate(&delegate); |
| 217 | + |
| 218 | + std::cout << "[consumer] connecting to " << url << "\n"; |
| 219 | + if (!room.Connect(url, token, options)) { |
| 220 | + std::cerr << "[consumer] failed to connect\n"; |
| 221 | + exit_code = 1; |
| 222 | + } else { |
| 223 | + std::cout << "[consumer] connected as " |
| 224 | + << room.localParticipant()->identity() << " to room '" |
| 225 | + << room.room_info().name << "' with user timestamp " |
| 226 | + << (read_user_timestamp ? "enabled" : "ignored") << "\n"; |
| 227 | + |
| 228 | + delegate.registerExistingParticipants(); |
| 229 | + |
| 230 | + while (g_running.load(std::memory_order_relaxed)) { |
| 231 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 232 | + } |
| 233 | + |
| 234 | + for (const auto &participant : room.remoteParticipants()) { |
| 235 | + if (participant) { |
| 236 | + room.clearOnVideoFrameCallback(participant->identity(), |
| 237 | + TrackSource::SOURCE_CAMERA); |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + room.setDelegate(nullptr); |
| 243 | + } |
| 244 | + |
| 245 | + livekit::shutdown(); |
| 246 | + return exit_code; |
| 247 | +} |
0 commit comments