Skip to content

Commit e7d3ec3

Browse files
user_timestamped_video
1 parent f231c0c commit e7d3ec3

7 files changed

Lines changed: 593 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`.
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: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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` 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+
std::atomic<bool> g_running{true};
48+
49+
void handleSignal(int) { g_running.store(false); }
50+
51+
std::string getenvOrEmpty(const char *name) {
52+
const char *value = std::getenv(name);
53+
return value ? std::string(value) : std::string{};
54+
}
55+
56+
std::string
57+
formatUserTimestamp(const std::optional<VideoFrameMetadata> &metadata) {
58+
if (!metadata || !metadata->user_timestamp.has_value()) {
59+
return "n/a";
60+
}
61+
62+
return std::to_string(*metadata->user_timestamp);
63+
}
64+
65+
void printUsage(const char *program) {
66+
std::cerr << "Usage:\n"
67+
<< " " << program
68+
<< " <ws-url> <token> [--ignore-user-timestamp]\n"
69+
<< "or:\n"
70+
<< " LIVEKIT_URL=... LIVEKIT_TOKEN=... " << program
71+
<< " [--ignore-user-timestamp]\n";
72+
}
73+
74+
bool parseArgs(int argc, char *argv[], std::string &url, std::string &token,
75+
bool &read_user_timestamp) {
76+
read_user_timestamp = true;
77+
std::vector<std::string> positional;
78+
79+
for (int i = 1; i < argc; ++i) {
80+
const std::string arg = argv[i];
81+
if (arg == "-h" || arg == "--help") {
82+
return false;
83+
}
84+
if (arg == "--ignore-user-timestamp") {
85+
read_user_timestamp = false;
86+
continue;
87+
}
88+
if (arg == "--read-user-timestamp") {
89+
read_user_timestamp = true;
90+
continue;
91+
}
92+
93+
positional.push_back(arg);
94+
}
95+
96+
url = getenvOrEmpty("LIVEKIT_URL");
97+
token = getenvOrEmpty("LIVEKIT_TOKEN");
98+
99+
if (positional.size() >= 2) {
100+
url = positional[0];
101+
token = positional[1];
102+
}
103+
104+
return !(url.empty() || token.empty());
105+
}
106+
107+
class UserTimestampedVideoConsumerDelegate : public RoomDelegate {
108+
public:
109+
UserTimestampedVideoConsumerDelegate(Room &room, bool read_user_timestamp)
110+
: room_(room), read_user_timestamp_(read_user_timestamp) {}
111+
112+
void registerExistingParticipants() {
113+
for (const auto &participant : room_.remoteParticipants()) {
114+
if (participant) {
115+
registerRemoteCameraCallback(participant->identity());
116+
}
117+
}
118+
}
119+
120+
void onParticipantConnected(Room &,
121+
const ParticipantConnectedEvent &event) override {
122+
if (!event.participant) {
123+
return;
124+
}
125+
126+
std::cout << "[consumer] participant connected: "
127+
<< event.participant->identity() << "\n";
128+
registerRemoteCameraCallback(event.participant->identity());
129+
}
130+
131+
void onParticipantDisconnected(
132+
Room &, const ParticipantDisconnectedEvent &event) override {
133+
if (!event.participant) {
134+
return;
135+
}
136+
137+
const std::string identity = event.participant->identity();
138+
room_.clearOnVideoFrameCallback(identity, TrackSource::SOURCE_CAMERA);
139+
140+
{
141+
std::lock_guard<std::mutex> lock(mutex_);
142+
registered_identities_.erase(identity);
143+
}
144+
145+
std::cout << "[consumer] participant disconnected: " << identity << "\n";
146+
}
147+
148+
private:
149+
void registerRemoteCameraCallback(const std::string &identity) {
150+
{
151+
std::lock_guard<std::mutex> lock(mutex_);
152+
if (!registered_identities_.insert(identity).second) {
153+
return;
154+
}
155+
}
156+
157+
VideoStream::Options stream_options;
158+
stream_options.format = VideoBufferType::RGBA;
159+
160+
if (read_user_timestamp_) {
161+
room_.setOnVideoFrameEventCallback(
162+
identity, TrackSource::SOURCE_CAMERA,
163+
[identity](const VideoFrameEvent &event) {
164+
std::cout << "[consumer] from=" << identity
165+
<< " size=" << event.frame.width() << "x"
166+
<< event.frame.height()
167+
<< " capture_ts_us=" << event.timestamp_us
168+
<< " user_ts_us=" << formatUserTimestamp(event.metadata)
169+
<< " rotation=" << static_cast<int>(event.rotation)
170+
<< "\n";
171+
},
172+
stream_options);
173+
} else {
174+
room_.setOnVideoFrameCallback(
175+
identity, TrackSource::SOURCE_CAMERA,
176+
[identity](const VideoFrame &frame, const std::int64_t timestamp_us) {
177+
std::cout << "[consumer] from=" << identity
178+
<< " size=" << frame.width() << "x" << frame.height()
179+
<< " capture_ts_us=" << timestamp_us
180+
<< " user_ts_us=ignored\n";
181+
},
182+
stream_options);
183+
}
184+
185+
std::cout << "[consumer] listening for camera frames from " << identity
186+
<< " with user timestamp "
187+
<< (read_user_timestamp_ ? "enabled" : "ignored") << "\n";
188+
}
189+
190+
Room &room_;
191+
bool read_user_timestamp_;
192+
std::mutex mutex_;
193+
std::unordered_set<std::string> registered_identities_;
194+
};
195+
196+
} // namespace
197+
198+
int main(int argc, char *argv[]) {
199+
std::string url;
200+
std::string token;
201+
bool read_user_timestamp = true;
202+
203+
if (!parseArgs(argc, argv, url, token, read_user_timestamp)) {
204+
printUsage(argv[0]);
205+
return 1;
206+
}
207+
208+
std::signal(SIGINT, handleSignal);
209+
#ifdef SIGTERM
210+
std::signal(SIGTERM, handleSignal);
211+
#endif
212+
213+
livekit::initialize(livekit::LogLevel::Info, livekit::LogSink::kConsole);
214+
int exit_code = 0;
215+
216+
{
217+
Room room;
218+
RoomOptions options;
219+
options.auto_subscribe = true;
220+
options.dynacast = false;
221+
222+
UserTimestampedVideoConsumerDelegate delegate(room, read_user_timestamp);
223+
room.setDelegate(&delegate);
224+
225+
std::cout << "[consumer] connecting to " << url << "\n";
226+
if (!room.Connect(url, token, options)) {
227+
std::cerr << "[consumer] failed to connect\n";
228+
exit_code = 1;
229+
} else {
230+
std::cout << "[consumer] connected as "
231+
<< room.localParticipant()->identity() << " to room '"
232+
<< room.room_info().name << "' with user timestamp "
233+
<< (read_user_timestamp ? "enabled" : "ignored") << "\n";
234+
235+
delegate.registerExistingParticipants();
236+
237+
while (g_running.load(std::memory_order_relaxed)) {
238+
std::this_thread::sleep_for(std::chrono::milliseconds(50));
239+
}
240+
241+
for (const auto &participant : room.remoteParticipants()) {
242+
if (participant) {
243+
room.clearOnVideoFrameCallback(participant->identity(),
244+
TrackSource::SOURCE_CAMERA);
245+
}
246+
}
247+
}
248+
249+
room.setDelegate(nullptr);
250+
}
251+
252+
livekit::shutdown();
253+
return exit_code;
254+
}
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)