From a468b1dcdb4460f69188c04ae2e46f63e5e50f63 Mon Sep 17 00:00:00 2001 From: Stephen DeRosa Date: Fri, 20 Nov 2026 10:41:43 -0700 Subject: [PATCH] dynacast subscriber/publisher --- CMakeLists.txt | 2 + README.md | 2 + data_track_dynacast/publisher/CMakeLists.txt | 18 ++++ data_track_dynacast/publisher/main.cpp | 69 +++++++++++++ data_track_dynacast/subscriber/CMakeLists.txt | 18 ++++ data_track_dynacast/subscriber/main.cpp | 96 +++++++++++++++++++ 6 files changed, 205 insertions(+) create mode 100644 data_track_dynacast/publisher/CMakeLists.txt create mode 100644 data_track_dynacast/publisher/main.cpp create mode 100644 data_track_dynacast/subscriber/CMakeLists.txt create mode 100644 data_track_dynacast/subscriber/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bc36e83..ed7ff33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/README.md b/README.md index 95dc691..f23d122 100644 --- a/README.md +++ b/README.md @@ -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. | diff --git a/data_track_dynacast/publisher/CMakeLists.txt b/data_track_dynacast/publisher/CMakeLists.txt new file mode 100644 index 0000000..188ad07 --- /dev/null +++ b/data_track_dynacast/publisher/CMakeLists.txt @@ -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) diff --git a/data_track_dynacast/publisher/main.cpp b/data_track_dynacast/publisher/main.cpp new file mode 100644 index 0000000..4f91f2d --- /dev/null +++ b/data_track_dynacast/publisher/main.cpp @@ -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 +/// (or set LIVEKIT_URL and LIVEKIT_TOKEN). Use a different identity than the +/// subscriber. + +#include +#include +#include +#include +#include + +#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 ) 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)); + } +} diff --git a/data_track_dynacast/subscriber/CMakeLists.txt b/data_track_dynacast/subscriber/CMakeLists.txt new file mode 100644 index 0000000..7c866f3 --- /dev/null +++ b/data_track_dynacast/subscriber/CMakeLists.txt @@ -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) diff --git a/data_track_dynacast/subscriber/main.cpp b/data_track_dynacast/subscriber/main.cpp new file mode 100644 index 0000000..d44c97b --- /dev/null +++ b/data_track_dynacast/subscriber/main.cpp @@ -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 +/// (or set LIVEKIT_URL and LIVEKIT_TOKEN). Use a different identity than the +/// publisher. + +#include +#include +#include +#include +#include +#include +#include + +#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 lock(mutex_); + track_ = e.track; + } + std::shared_ptr track() { + std::lock_guard lock(mutex_); + return track_; + } + +private: + std::mutex mutex_; + std::shared_ptr 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 ) 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 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)); + } +}