-
Notifications
You must be signed in to change notification settings - Fork 2
Frame metadata example #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Frame Metadata Example | ||
|
|
||
| This example is split into two executables and demonstrates LiveKit C++ SDK video frame metadata feature: | ||
|
|
||
| - `frame_metadata_producer` publishes a synthetic video track named `"sensor-camera"`. | ||
| - Each video frame attaches additional data via the frame metadata features of the SDK: | ||
|
|
||
| | Field | Description | | ||
| | --- | --- | | ||
| | `user_timestamp_us` | Wall-clock capture time (us) | | ||
| | `frame_id` | Monotonic frame counter | | ||
| | `user_data` | Free-form user data, in this example it is used to send a JSON temperature reading correlated with the video frame | | ||
|
|
||
| - `frame_metadata_consumer` subscribes to the remote track by name and reads the | ||
| metadata through `setOnVideoFrameEventCallback`. | ||
|
|
||
| Run them in the same room with different participant identities. `LIVEKIT_URL` | ||
| defaults to `ws://localhost:7880` when unset: | ||
|
|
||
| ```sh | ||
| export LIVEKIT_TOKEN=<producer-token> | ||
| ./build/frame_metadata/producer/frame_metadata_producer | ||
|
|
||
| export LIVEKIT_TOKEN=<consumer-token> | ||
| ./build/frame_metadata/consumer/frame_metadata_consumer | ||
| ``` | ||
|
|
||
| You can also pass URL and token explicitly: | ||
|
|
||
| ```sh | ||
| ./build/frame_metadata/producer/frame_metadata_producer ws://localhost:7880 <producer-token> | ||
| ./build/frame_metadata/consumer/frame_metadata_consumer ws://localhost:7880 <consumer-token> | ||
| ``` | ||
|
|
||
| Metadata notes for this example: | ||
|
|
||
| - `frame_id` and `user_ts_us` come from `VideoFrameMetadata` and are the values | ||
| to compare end to end. | ||
| - `user_data` is free-form frame metadata. This example uses it only for the | ||
| temperature reading, similar to a robotics or sensor-fusion camera feed, in the following format: | ||
|
|
||
| ```json | ||
| {"temperature_c":23.4} | ||
| ``` | ||
|
|
||
| - `capture_ts_us` on the producer is the timestamp submitted to `captureFrame`. | ||
| - `capture_ts_us` on the consumer is the received WebRTC frame timestamp. | ||
| - Producer and consumer `capture_ts_us` values are not expected to match exactly, | ||
| because WebRTC may translate frame timestamps onto its own internal | ||
| capture-time timeline before delivery. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| namespace frame_metadata { | ||
|
|
||
| inline constexpr char kVideoTrackName[] = "sensor-camera"; | ||
|
|
||
| inline constexpr char kTemperatureCKey[] = "temperature_c"; | ||
|
|
||
| } // namespace frame_metadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #include "json_converters.h" | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "constants.h" | ||
|
|
||
| namespace frame_metadata { | ||
|
|
||
| std::string sensorReadingToJson(const SensorReading& reading) { | ||
| nlohmann::json json; | ||
| json[kTemperatureCKey] = reading.temperature_c; | ||
| return json.dump(); | ||
| } | ||
|
|
||
| std::optional<SensorReading> sensorReadingFromJson(const std::string& json_text) { | ||
| const auto json = nlohmann::json::parse(json_text, nullptr, false); | ||
| if (json.is_discarded() || !json.is_object()) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| const auto temperature_it = json.find(kTemperatureCKey); | ||
| if (temperature_it == json.end() || !temperature_it->is_number()) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| SensorReading reading; | ||
| reading.temperature_c = temperature_it->get<double>(); | ||
| return reading; | ||
| } | ||
|
|
||
| } // namespace frame_metadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <optional> | ||
| #include <string> | ||
|
|
||
| #include "messages.h" | ||
|
|
||
| namespace frame_metadata { | ||
|
|
||
| std::string sensorReadingToJson(const SensorReading& reading); | ||
| std::optional<SensorReading> sensorReadingFromJson(const std::string& json_text); | ||
|
|
||
| } // namespace frame_metadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| namespace frame_metadata { | ||
|
|
||
| struct SensorReading { | ||
| double temperature_c = 0.0; | ||
| }; | ||
|
|
||
| } // namespace frame_metadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.