Skip to content

Commit bd95181

Browse files
user_timestamped_video
1 parent f231c0c commit bd95181

7 files changed

Lines changed: 597 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,4 @@ add_subdirectory(simple_joystick_sender)
9393
add_subdirectory(simple_joystick_receiver)
9494
add_subdirectory(ping_pong_ping)
9595
add_subdirectory(ping_pong_pong)
96+
add_subdirectory(user_timestamped_video)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2026 LiveKit, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
add_subdirectory(producer)
16+
add_subdirectory(consumer)

user_timestamped_video/README.md

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

0 commit comments

Comments
 (0)