|
| 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 <cstdint> |
| 20 | +#include <memory> |
| 21 | +#include <mutex> |
| 22 | +#include <optional> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace livekit { |
| 27 | +class LocalDataTrack; |
| 28 | +class LocalParticipant; |
| 29 | +} // namespace livekit |
| 30 | + |
| 31 | +namespace livekit_bridge { |
| 32 | + |
| 33 | +namespace test { |
| 34 | +class BridgeDataTrackTest; |
| 35 | +} // namespace test |
| 36 | + |
| 37 | +/** |
| 38 | + * Handle to a published local data track. |
| 39 | + * |
| 40 | + * Created via LiveKitBridge::createDataTrack(). The bridge retains a |
| 41 | + * reference to every track it creates and will automatically release all |
| 42 | + * tracks when disconnect() is called. To unpublish a track mid-session, |
| 43 | + * call release() explicitly. |
| 44 | + * |
| 45 | + * Unlike BridgeAudioTrack / BridgeVideoTrack, data tracks have no |
| 46 | + * Source, Publication, mute(), or unmute(). They carry arbitrary binary |
| 47 | + * frames via pushFrame(). |
| 48 | + * |
| 49 | + * All public methods are thread-safe. |
| 50 | + * |
| 51 | + * Usage: |
| 52 | + * auto dt = bridge.createDataTrack("sensor-data"); |
| 53 | + * dt->pushFrame({0x01, 0x02, 0x03}); |
| 54 | + * dt->release(); // unpublishes mid-session |
| 55 | + */ |
| 56 | +class BridgeDataTrack { |
| 57 | +public: |
| 58 | + ~BridgeDataTrack(); |
| 59 | + |
| 60 | + BridgeDataTrack(const BridgeDataTrack &) = delete; |
| 61 | + BridgeDataTrack &operator=(const BridgeDataTrack &) = delete; |
| 62 | + |
| 63 | + /** |
| 64 | + * Push a binary frame to all subscribers of this data track. |
| 65 | + * |
| 66 | + * @param payload Raw bytes to send. |
| 67 | + * @param user_timestamp Optional application-defined timestamp. |
| 68 | + * @return true if the frame was pushed, false if the track has been |
| 69 | + * released or the push failed (e.g. back-pressure). |
| 70 | + */ |
| 71 | + bool pushFrame(const std::vector<std::uint8_t> &payload, |
| 72 | + std::optional<std::uint64_t> user_timestamp = std::nullopt); |
| 73 | + |
| 74 | + /** |
| 75 | + * Push a binary frame from a raw pointer. |
| 76 | + * |
| 77 | + * @param data Pointer to raw bytes. |
| 78 | + * @param size Number of bytes. |
| 79 | + * @param user_timestamp Optional application-defined timestamp. |
| 80 | + * @return true on success, false if released or push failed. |
| 81 | + */ |
| 82 | + bool pushFrame(const std::uint8_t *data, std::size_t size, |
| 83 | + std::optional<std::uint64_t> user_timestamp = std::nullopt); |
| 84 | + |
| 85 | + /// Track name as provided at creation. |
| 86 | + const std::string &name() const noexcept { return name_; } |
| 87 | + |
| 88 | + /// Whether the track is still published in the room. |
| 89 | + bool isPublished() const; |
| 90 | + |
| 91 | + /// Whether this track has been released / unpublished. |
| 92 | + bool isReleased() const noexcept; |
| 93 | + |
| 94 | + /** |
| 95 | + * Explicitly unpublish the track and release underlying SDK resources. |
| 96 | + * |
| 97 | + * After this call, pushFrame() returns false. Called automatically by the |
| 98 | + * destructor and by LiveKitBridge::disconnect(). Safe to call multiple |
| 99 | + * times (idempotent). |
| 100 | + */ |
| 101 | + void release(); |
| 102 | + |
| 103 | +private: |
| 104 | + friend class LiveKitBridge; |
| 105 | + friend class test::BridgeDataTrackTest; |
| 106 | + |
| 107 | + BridgeDataTrack(std::string name, |
| 108 | + std::shared_ptr<livekit::LocalDataTrack> track, |
| 109 | + livekit::LocalParticipant *participant); |
| 110 | + |
| 111 | + /** Protects released_ and track_ for thread-safe access. */ |
| 112 | + mutable std::mutex mutex_; |
| 113 | + |
| 114 | + /** Publisher-assigned track name (immutable after construction). */ |
| 115 | + std::string name_; |
| 116 | + |
| 117 | + /** True after release() or disconnect(); prevents further pushFrame(). */ |
| 118 | + bool released_ = false; |
| 119 | + |
| 120 | + /** Underlying SDK data track handle. Nulled on release(). */ |
| 121 | + std::shared_ptr<livekit::LocalDataTrack> track_; |
| 122 | + |
| 123 | + /** Participant that published this track; used for unpublish. Not owned. */ |
| 124 | + livekit::LocalParticipant *participant_ = nullptr; |
| 125 | +}; |
| 126 | + |
| 127 | +} // namespace livekit_bridge |
0 commit comments