-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
145 lines (118 loc) · 4.45 KB
/
main.cpp
File metadata and controls
145 lines (118 loc) · 4.45 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
142
143
144
145
/*
* 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.
*/
/// Publishes synthetic RGBA video and a data track. Run the receiver in another
/// process and pass this participant's identity (printed after connect).
///
/// Usage:
/// HelloLivekitSender <ws-url> <sender-token>
///
/// Or via environment variables:
/// LIVEKIT_URL, LIVEKIT_SENDER_TOKEN
#include <atomic>
#include <chrono>
#include <csignal>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <thread>
#include "livekit/livekit.h"
using namespace livekit;
constexpr int kWidth = 640;
constexpr int kHeight = 480;
constexpr const char* kVideoTrackName = "camera0";
constexpr const char* kDataTrackName = "app-data";
std::atomic<bool> g_running{true};
void handleSignal(int) { g_running.store(false); }
std::string getenvOrEmpty(const char* name) {
const char* v = std::getenv(name);
return v ? std::string(v) : std::string{};
}
int main(int argc, char* argv[]) {
std::string url = getenvOrEmpty("LIVEKIT_URL");
std::string sender_token = getenvOrEmpty("LIVEKIT_SENDER_TOKEN");
if (argc >= 3) {
url = argv[1];
sender_token = argv[2];
}
if (url.empty() || sender_token.empty()) {
std::cerr
<< "[error] Usage: HelloLivekitSender <ws-url> <sender-token>\n or set LIVEKIT_URL, LIVEKIT_SENDER_TOKEN\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, sender_token, options)) {
std::cerr << "[error] [sender] Failed to connect\n";
livekit::shutdown();
return 1;
}
std::shared_ptr<LocalDataTrack> data_track;
std::shared_ptr<LocalVideoTrack> video_track;
std::shared_ptr<VideoSource> video_source;
{
auto lp = room->localParticipant().lock();
if (!lp) {
throw std::runtime_error("[sender] local participant is null");
}
std::cout << "[info] [sender] Connected as identity='" << lp->identity() << "' room='" << room->roomInfo().name
<< "' — pass this identity to HelloLivekitReceiver\n";
video_source = std::make_shared<VideoSource>(kWidth, kHeight);
video_track = lp->publishVideoTrack(kVideoTrackName, video_source, TrackSource::SOURCE_CAMERA);
auto publish_result = lp->publishDataTrack(kDataTrackName);
if (!publish_result) {
const auto& error = publish_result.error();
std::cerr << "[error] Failed to publish data track: code=" << static_cast<std::uint32_t>(error.code)
<< " message=" << error.message << "\n";
room.reset();
livekit::shutdown();
return 1;
}
data_track = publish_result.value();
}
const auto t0 = std::chrono::steady_clock::now();
std::uint64_t count = 0;
std::cout << "[info] [sender] Publishing synthetic video + data on '" << kDataTrackName << "'; Ctrl-C to exit\n";
while (g_running.load()) {
VideoFrame vf = VideoFrame::create(kWidth, kHeight, VideoBufferType::RGBA);
video_source->captureFrame(std::move(vf));
const auto now = std::chrono::steady_clock::now();
const double ms = std::chrono::duration<double, std::milli>(now - t0).count();
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << ms << " ms, count: " << count;
const std::string msg = oss.str();
auto push_result = data_track->tryPush(std::vector<std::uint8_t>(msg.begin(), msg.end()));
if (!push_result) {
const auto& error = push_result.error();
std::cerr << "[warn] Failed to push data frame: code=" << static_cast<std::uint32_t>(error.code)
<< " message=" << error.message << "\n";
}
++count;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << "[info] [sender] Disconnecting\n";
room.reset();
livekit::shutdown();
return 0;
}