Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ add_subdirectory(platform_audio)
add_subdirectory(ping_pong/ping ping_pong_ping)
add_subdirectory(ping_pong/pong ping_pong_pong)
add_subdirectory(frame_metadata)
add_subdirectory(data_track_dynacast/publisher data_track_dynacast_publisher)
add_subdirectory(data_track_dynacast/subscriber data_track_dynacast_subscriber)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ etc.) without requiring users to build the SDK from source.
| Example | Description |
| --- | --- |
| [`basic_room`](basic_room/) | Connects to a room and publishes synthetic audio and video tracks. |
| [`data_track_dynacast/publisher`](data_track_dynacast/publisher/) | Pushes a data frame at 4 Hz with dynacast on and prints each success or failure. |
| [`data_track_dynacast/subscriber`](data_track_dynacast/subscriber/) | Subscribes to the publisher's data track and toggles the subscription every 3 seconds. |
| [`frame_metadata`](frame_metadata/) | Publishes and consumes video frames with frame metadata such as IDs, timestamps, and user data. |
| [`hello_livekit/receiver`](hello_livekit/receiver/) | Subscribes to the sender example's video and data tracks. |
| [`hello_livekit/sender`](hello_livekit/sender/) | Publishes synthetic video and data for a paired receiver example. |
Expand Down
18 changes: 18 additions & 0 deletions data_track_dynacast/publisher/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2026 LiveKit, Inc.
#
# 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.

add_executable(DataTrackDynacastPublisher main.cpp)
target_link_libraries(DataTrackDynacastPublisher PRIVATE ${LIVEKIT_CORE_TARGET})

livekit_copy_windows_runtime_dlls(DataTrackDynacastPublisher)
69 changes: 69 additions & 0 deletions data_track_dynacast/publisher/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.
*/

/// Publishes a data track at 4 Hz with dynacast enabled and prints the result
/// of every push. When the subscriber unsubscribes, the push fails with
/// NO_SUBSCRIBERS. Usage: DataTrackDynacastPublisher <ws-url> <token>
/// (or set LIVEKIT_URL and LIVEKIT_TOKEN). Use a different identity than the
/// subscriber.

#include <chrono>
#include <cstdlib>
#include <iostream>
#include <string>
#include <thread>

#include "livekit/livekit.h"

using namespace livekit;

int main(int argc, char* argv[]) {
std::string url = argc >= 3 ? argv[1] : (std::getenv("LIVEKIT_URL") ? std::getenv("LIVEKIT_URL") : "");
std::string token = argc >= 3 ? argv[2] : (std::getenv("LIVEKIT_TOKEN") ? std::getenv("LIVEKIT_TOKEN") : "");
if (url.empty() || token.empty()) {
std::cerr << "LIVEKIT_URL and LIVEKIT_TOKEN (or <ws-url> <token>) are required\n";
return 1;
}

livekit::initialize(livekit::LogLevel::Warn);
Room room;
RoomOptions options;
options.dynacast = true; // the server tells the publisher when nobody subscribes
if (!room.connect(url, token, options)) {
std::cerr << "Failed to connect\n";
return 1;
}

auto publish_result = room.localParticipant().lock()->publishDataTrack("dynacast-demo");
if (!publish_result) {
std::cerr << "Failed to publish: " << publish_result.error().message << "\n";
return 1;
}
auto track = publish_result.value();

for (int i = 1;; ++i) { // runs until Ctrl+C
const std::string text = "frame " + std::to_string(i);
auto result = track->tryPush({text.begin(), text.end()});
if (result) {
std::cout << "sent " << text << "\n";
} else if (result.error().code == LocalDataTrackTryPushErrorCode::NO_SUBSCRIBERS) {
std::cout << "FAILED " << text << " (no subscribers, dynacast dropped it)\n";
} else {
std::cout << "FAILED " << text << " (" << result.error().message << ")\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
}
18 changes: 18 additions & 0 deletions data_track_dynacast/subscriber/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2026 LiveKit, Inc.
#
# 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.

add_executable(DataTrackDynacastSubscriber main.cpp)
target_link_libraries(DataTrackDynacastSubscriber PRIVATE ${LIVEKIT_CORE_TARGET})

livekit_copy_windows_runtime_dlls(DataTrackDynacastSubscriber)
96 changes: 96 additions & 0 deletions data_track_dynacast/subscriber/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.
*/

/// Subscribes to the publisher's data track, then toggles the subscription
/// every 3 seconds. Usage: DataTrackDynacastSubscriber <ws-url> <token>
/// (or set LIVEKIT_URL and LIVEKIT_TOKEN). Use a different identity than the
/// publisher.

#include <chrono>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <mutex>
#include <string>
#include <thread>

#include "livekit/livekit.h"
#include "livekit/remote_data_track.h"

using namespace livekit;

// Remembers the publisher's data track when the room announces it.
class Delegate : public RoomDelegate {
public:
void onDataTrackPublished(Room&, const DataTrackPublishedEvent& e) override {
std::lock_guard<std::mutex> lock(mutex_);
track_ = e.track;
}
std::shared_ptr<RemoteDataTrack> track() {
std::lock_guard<std::mutex> lock(mutex_);
return track_;
}

private:
std::mutex mutex_;
std::shared_ptr<RemoteDataTrack> track_;
};

int main(int argc, char* argv[]) {
std::string url = argc >= 3 ? argv[1] : (std::getenv("LIVEKIT_URL") ? std::getenv("LIVEKIT_URL") : "");
std::string token = argc >= 3 ? argv[2] : (std::getenv("LIVEKIT_TOKEN") ? std::getenv("LIVEKIT_TOKEN") : "");
if (url.empty() || token.empty()) {
std::cerr << "LIVEKIT_URL and LIVEKIT_TOKEN (or <ws-url> <token>) are required\n";
return 1;
}

livekit::initialize(livekit::LogLevel::Warn);
Room room;
Delegate delegate;
room.setDelegate(&delegate);
if (!room.connect(url, token, RoomOptions{})) {
std::cerr << "Failed to connect\n";
return 1;
}

std::cout << "waiting for the publisher's data track...\n";
std::shared_ptr<RemoteDataTrack> track;
while (!(track = delegate.track())) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

while (true) { // runs until Ctrl+C
auto result = track->subscribe();
if (!result) {
std::cerr << "Subscribe failed: " << result.error().message << "\n";
return 1;
}
auto stream = result.value();
std::cout << ">>> SUBSCRIBED\n";
std::thread reader([stream] {
DataTrackFrame frame;
while (stream->read(frame)) {
std::cout << "received " << std::string(frame.payload.begin(), frame.payload.end()) << "\n";
}
});

std::this_thread::sleep_for(std::chrono::seconds(3));
stream->close(); // unsubscribe; wakes the blocked read()
reader.join();
std::cout << ">>> UNSUBSCRIBED\n";
std::this_thread::sleep_for(std::chrono::seconds(3));
}
}
Loading