Skip to content

Commit fff0172

Browse files
Frame metadata example (#20)
* Renames user_timestamped_video example to frame_metadata * Adds sensor temperature JSON payload as an example user_data use case * Dropped CLI arg to enable/disable timestamp -- just opted to turn everything on for this example * Unified producer/consumer output * Overhauled docs/readme
1 parent 50628ed commit fff0172

14 files changed

Lines changed: 337 additions & 198 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ add_subdirectory(hello_livekit/receiver hello_livekit_receiver)
9292
add_subdirectory(platform_audio)
9393
add_subdirectory(ping_pong/ping ping_pong_ping)
9494
add_subdirectory(ping_pong/pong ping_pong_pong)
95-
add_subdirectory(user_timestamped_video)
95+
add_subdirectory(frame_metadata)

‎README.md‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,6 @@ For example:
7373
./build/basic_room/basic_room --url <ws-url> --token <token>
7474
```
7575

76-
### PlatformAudio
77-
78-
The `platform_audio` examples show microphone capture and speaker playout using
79-
WebRTC's platform Audio Device Module:
80-
81-
```bash
82-
./build/platform_audio/player/PlatformAudioPlayer <ws-url> <player-token>
83-
./build/platform_audio/sender/PlatformAudioSender <ws-url> <sender-token>
84-
```
85-
8676
### Supported platforms
8777

8878
Prebuilt SDKs are downloaded automatically for:

user_timestamped_video/consumer/CMakeLists.txt renamed to frame_metadata/CMakeLists.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
add_executable(UserTimestampedVideoConsumer
16-
main.cpp
15+
add_library(frame_metadata_support STATIC
16+
common/json_converters.cpp
17+
common/json_converters.h
18+
common/cli_utils.h
19+
common/constants.h
20+
common/messages.h
1721
)
1822

19-
target_include_directories(UserTimestampedVideoConsumer PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
20-
target_link_libraries(UserTimestampedVideoConsumer PRIVATE ${LIVEKIT_CORE_TARGET})
23+
target_include_directories(frame_metadata_support PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/common)
24+
target_link_libraries(frame_metadata_support PUBLIC nlohmann_json::nlohmann_json)
2125

22-
livekit_copy_windows_runtime_dlls(UserTimestampedVideoConsumer)
26+
add_subdirectory(producer)
27+
add_subdirectory(consumer)

‎frame_metadata/README.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Frame Metadata Example
2+
3+
This example is split into two executables and demonstrates LiveKit C++ SDK video frame metadata feature:
4+
5+
- `frame_metadata_producer` publishes a synthetic video track named `"sensor-camera"`.
6+
- Each video frame attaches additional data via the frame metadata features of the SDK:
7+
8+
| Field | Description |
9+
| --- | --- |
10+
| `user_timestamp_us` | Wall-clock capture time (us) |
11+
| `frame_id` | Monotonic frame counter |
12+
| `user_data` | Free-form user data, in this example it is used to send a JSON temperature reading correlated with the video frame |
13+
14+
- `frame_metadata_consumer` subscribes to the remote track by name and reads the
15+
metadata through `setOnVideoFrameEventCallback`.
16+
17+
Run them in the same room with different participant identities. `LIVEKIT_URL`
18+
defaults to `ws://localhost:7880` when unset:
19+
20+
```sh
21+
export LIVEKIT_TOKEN=<producer-token>
22+
./build/frame_metadata/producer/frame_metadata_producer
23+
24+
export LIVEKIT_TOKEN=<consumer-token>
25+
./build/frame_metadata/consumer/frame_metadata_consumer
26+
```
27+
28+
You can also pass URL and token explicitly:
29+
30+
```sh
31+
./build/frame_metadata/producer/frame_metadata_producer ws://localhost:7880 <producer-token>
32+
./build/frame_metadata/consumer/frame_metadata_consumer ws://localhost:7880 <consumer-token>
33+
```
34+
35+
Metadata notes for this example:
36+
37+
- `frame_id` and `user_ts_us` come from `VideoFrameMetadata` and are the values
38+
to compare end to end.
39+
- `user_data` is free-form frame metadata. This example uses it only for the
40+
temperature reading, similar to a robotics or sensor-fusion camera feed, in the following format:
41+
42+
```json
43+
{"temperature_c":23.4}
44+
```
45+
46+
- `capture_ts_us` on the producer is the timestamp submitted to `captureFrame`.
47+
- `capture_ts_us` on the consumer is the received WebRTC frame timestamp.
48+
- Producer and consumer `capture_ts_us` values are not expected to match exactly,
49+
because WebRTC may translate frame timestamps onto its own internal
50+
capture-time timeline before delivery.
Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@
1818

1919
#include <atomic>
2020
#include <csignal>
21+
#include <cstdint>
2122
#include <cstdlib>
2223
#include <iostream>
2324
#include <string>
2425
#include <vector>
2526

26-
namespace user_timestamped_video {
27+
namespace frame_metadata {
28+
29+
inline constexpr char kDefaultLiveKitUrl[] = "ws://localhost:7880";
2730

2831
enum class ParseResult { Ok, Help, Error };
2932

3033
struct CliOptions {
3134
std::string url;
3235
std::string token;
33-
bool use_user_timestamp = true;
3436
};
3537

3638
inline std::atomic<bool> g_running{true};
@@ -53,11 +55,15 @@ inline std::string getenvOrEmpty(const char* name) {
5355

5456
inline void printUsage(const char* program) {
5557
std::cerr << "Usage:\n"
56-
<< " " << program << " <ws-url> <token> "
57-
<< "[--with-user-timestamp|--without-user-timestamp]\n"
58-
<< "or:\n"
59-
<< " LIVEKIT_URL=... LIVEKIT_TOKEN=... " << program
60-
<< " [--with-user-timestamp|--without-user-timestamp]\n";
58+
<< " " << program << " [<ws-url> <token>]\n"
59+
<< "\n"
60+
<< "Environment:\n"
61+
<< " LIVEKIT_URL defaults to " << kDefaultLiveKitUrl << " when unset\n"
62+
<< " LIVEKIT_TOKEN required unless passed as the second argument\n"
63+
<< "\n"
64+
<< "Example:\n"
65+
<< " export LIVEKIT_TOKEN=<token>\n"
66+
<< " " << program << "\n";
6167
}
6268

6369
inline ParseResult parseArgs(int argc, char* argv[], CliOptions& options) {
@@ -69,14 +75,6 @@ inline ParseResult parseArgs(int argc, char* argv[], CliOptions& options) {
6975
if (arg == "-h" || arg == "--help") {
7076
return ParseResult::Help;
7177
}
72-
if (arg == "--without-user-timestamp") {
73-
options.use_user_timestamp = false;
74-
continue;
75-
}
76-
if (arg == "--with-user-timestamp") {
77-
options.use_user_timestamp = true;
78-
continue;
79-
}
8078
if (!arg.empty() && arg[0] == '-') {
8179
return ParseResult::Error;
8280
}
@@ -98,7 +96,19 @@ inline ParseResult parseArgs(int argc, char* argv[], CliOptions& options) {
9896
return ParseResult::Error;
9997
}
10098

101-
return (options.url.empty() || options.token.empty()) ? ParseResult::Error : ParseResult::Ok;
99+
if (options.url.empty()) {
100+
options.url = kDefaultLiveKitUrl;
101+
}
102+
103+
return options.token.empty() ? ParseResult::Error : ParseResult::Ok;
104+
}
105+
106+
inline std::vector<std::uint8_t> toPayload(const std::string& text) {
107+
return std::vector<std::uint8_t>(text.begin(), text.end());
108+
}
109+
110+
inline std::string toString(const std::vector<std::uint8_t>& payload) {
111+
return std::string(payload.begin(), payload.end());
102112
}
103113

104-
} // namespace user_timestamped_video
114+
} // namespace frame_metadata

‎frame_metadata/common/constants.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#pragma once
18+
19+
namespace frame_metadata {
20+
21+
inline constexpr char kVideoTrackName[] = "sensor-camera";
22+
23+
inline constexpr char kTemperatureCKey[] = "temperature_c";
24+
25+
} // namespace frame_metadata
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
#include "json_converters.h"
18+
19+
#include <nlohmann/json.hpp>
20+
21+
#include "constants.h"
22+
23+
namespace frame_metadata {
24+
25+
std::string sensorReadingToJson(const SensorReading& reading) {
26+
nlohmann::json json;
27+
json[kTemperatureCKey] = reading.temperature_c;
28+
return json.dump();
29+
}
30+
31+
std::optional<SensorReading> sensorReadingFromJson(const std::string& json_text) {
32+
const auto json = nlohmann::json::parse(json_text, nullptr, false);
33+
if (json.is_discarded() || !json.is_object()) {
34+
return std::nullopt;
35+
}
36+
37+
const auto temperature_it = json.find(kTemperatureCKey);
38+
if (temperature_it == json.end() || !temperature_it->is_number()) {
39+
return std::nullopt;
40+
}
41+
42+
SensorReading reading;
43+
reading.temperature_c = temperature_it->get<double>();
44+
return reading;
45+
}
46+
47+
} // namespace frame_metadata
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#pragma once
18+
19+
#include <optional>
20+
#include <string>
21+
22+
#include "messages.h"
23+
24+
namespace frame_metadata {
25+
26+
std::string sensorReadingToJson(const SensorReading& reading);
27+
std::optional<SensorReading> sensorReadingFromJson(const std::string& json_text);
28+
29+
} // namespace frame_metadata

‎frame_metadata/common/messages.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#pragma once
18+
19+
namespace frame_metadata {
20+
21+
struct SensorReading {
22+
double temperature_c = 0.0;
23+
};
24+
25+
} // namespace frame_metadata
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
add_subdirectory(producer)
16-
add_subdirectory(consumer)
15+
add_executable(frame_metadata_consumer
16+
main.cpp
17+
)
18+
19+
target_link_libraries(frame_metadata_consumer PRIVATE frame_metadata_support ${LIVEKIT_CORE_TARGET})
20+
21+
livekit_copy_windows_runtime_dlls(frame_metadata_consumer)

0 commit comments

Comments
 (0)