Skip to content

Commit 86f0485

Browse files
user_timestamped_video
1 parent f231c0c commit 86f0485

7 files changed

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