|
| 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 | +/// Subscribes to the sender's camera video and data track. Run |
| 18 | +/// HelloLivekitSender first; use the identity it prints, or the sender's known |
| 19 | +/// participant name. |
| 20 | +/// |
| 21 | +/// Usage: |
| 22 | +/// HelloLivekitReceiver <ws-url> <receiver-token> <sender-identity> |
| 23 | +/// |
| 24 | +/// Or via environment variables: |
| 25 | +/// LIVEKIT_URL, LIVEKIT_RECEIVER_TOKEN, LIVEKIT_SENDER_IDENTITY |
| 26 | + |
| 27 | +#include "livekit/livekit.h" |
| 28 | + |
| 29 | +#include <atomic> |
| 30 | +#include <chrono> |
| 31 | +#include <csignal> |
| 32 | +#include <cstdlib> |
| 33 | +#include <thread> |
| 34 | + |
| 35 | +using namespace livekit; |
| 36 | + |
| 37 | +constexpr const char *kDataTrackName = "app-data"; |
| 38 | +constexpr const char *kVideoTrackName = "camera0"; |
| 39 | + |
| 40 | +std::atomic<bool> g_running{true}; |
| 41 | + |
| 42 | +void handleSignal(int) { g_running.store(false); } |
| 43 | + |
| 44 | +std::string getenvOrEmpty(const char *name) { |
| 45 | + const char *v = std::getenv(name); |
| 46 | + return v ? std::string(v) : std::string{}; |
| 47 | +} |
| 48 | + |
| 49 | +int main(int argc, char *argv[]) { |
| 50 | + std::string url = getenvOrEmpty("LIVEKIT_URL"); |
| 51 | + std::string receiver_token = getenvOrEmpty("LIVEKIT_RECEIVER_TOKEN"); |
| 52 | + std::string sender_identity = getenvOrEmpty("LIVEKIT_SENDER_IDENTITY"); |
| 53 | + |
| 54 | + if (argc >= 4) { |
| 55 | + url = argv[1]; |
| 56 | + receiver_token = argv[2]; |
| 57 | + sender_identity = argv[3]; |
| 58 | + } |
| 59 | + |
| 60 | + if (url.empty() || receiver_token.empty() || sender_identity.empty()) { |
| 61 | + LK_LOG_ERROR("Usage: HelloLivekitReceiver <ws-url> <receiver-token> " |
| 62 | + "<sender-identity>\n" |
| 63 | + " or set LIVEKIT_URL, LIVEKIT_RECEIVER_TOKEN, " |
| 64 | + "LIVEKIT_SENDER_IDENTITY"); |
| 65 | + return 1; |
| 66 | + } |
| 67 | + |
| 68 | + std::signal(SIGINT, handleSignal); |
| 69 | +#ifdef SIGTERM |
| 70 | + std::signal(SIGTERM, handleSignal); |
| 71 | +#endif |
| 72 | + |
| 73 | + livekit::initialize(livekit::LogLevel::Info, livekit::LogSink::kConsole); |
| 74 | + |
| 75 | + auto room = std::make_unique<Room>(); |
| 76 | + RoomOptions options; |
| 77 | + options.auto_subscribe = true; |
| 78 | + options.dynacast = false; |
| 79 | + |
| 80 | + if (!room->Connect(url, receiver_token, options)) { |
| 81 | + LK_LOG_ERROR("[receiver] Failed to connect"); |
| 82 | + livekit::shutdown(); |
| 83 | + return 1; |
| 84 | + } |
| 85 | + |
| 86 | + LocalParticipant *lp = room->localParticipant(); |
| 87 | + assert(lp); |
| 88 | + |
| 89 | + LK_LOG_INFO("[receiver] Connected as identity='{}' room='{}'; subscribing " |
| 90 | + "to sender identity='{}'", |
| 91 | + lp->identity(), room->room_info().name, sender_identity); |
| 92 | + |
| 93 | + int video_frame_count = 0; |
| 94 | + room->setOnVideoFrameCallback( |
| 95 | + sender_identity, kVideoTrackName, |
| 96 | + [&video_frame_count](const VideoFrame &frame, std::int64_t timestamp_us) { |
| 97 | + const auto ts_ms = |
| 98 | + std::chrono::duration<double, std::milli>(timestamp_us).count(); |
| 99 | + const int n = video_frame_count++; |
| 100 | + if (n % 10 == 0) { |
| 101 | + LK_LOG_INFO("[receiver] Video frame #{} {}x{} ts_ms={}", n, |
| 102 | + frame.width(), frame.height(), ts_ms); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + int data_frame_count = 0; |
| 107 | + room->addOnDataFrameCallback( |
| 108 | + sender_identity, kDataTrackName, |
| 109 | + [&data_frame_count](const std::vector<std::uint8_t> &payload, |
| 110 | + std::optional<std::uint64_t> user_ts) { |
| 111 | + const int n = data_frame_count++; |
| 112 | + if (n % 10 == 0) { |
| 113 | + LK_LOG_INFO("[receiver] Data frame #{}", n); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + LK_LOG_INFO("[receiver] Listening for video track '{}' + data track '{}'; " |
| 118 | + "Ctrl-C to exit", |
| 119 | + kVideoTrackName, kDataTrackName); |
| 120 | + |
| 121 | + while (g_running.load()) { |
| 122 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 123 | + } |
| 124 | + |
| 125 | + LK_LOG_INFO("[receiver] Shutting down"); |
| 126 | + room.reset(); |
| 127 | + |
| 128 | + livekit::shutdown(); |
| 129 | + return 0; |
| 130 | +} |
0 commit comments