Skip to content

Commit 1bdfc79

Browse files
move unpublish() from local_data_track.h to local_participant.h
1 parent c850343 commit 1bdfc79

7 files changed

Lines changed: 51 additions & 31 deletions

File tree

bridge/include/livekit_bridge/bridge_data_track.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace livekit {
2727
class LocalDataTrack;
28+
class LocalParticipant;
2829
} // namespace livekit
2930

3031
namespace livekit_bridge {
@@ -104,7 +105,8 @@ class BridgeDataTrack {
104105
friend class test::BridgeDataTrackTest;
105106

106107
BridgeDataTrack(std::string name,
107-
std::shared_ptr<livekit::LocalDataTrack> track);
108+
std::shared_ptr<livekit::LocalDataTrack> track,
109+
livekit::LocalParticipant *participant);
108110

109111
/** Protects released_ and track_ for thread-safe access. */
110112
mutable std::mutex mutex_;
@@ -117,6 +119,9 @@ class BridgeDataTrack {
117119

118120
/** Underlying SDK data track handle. Nulled on release(). */
119121
std::shared_ptr<livekit::LocalDataTrack> track_;
122+
123+
/** Participant that published this track; used for unpublish. Not owned. */
124+
livekit::LocalParticipant *participant_ = nullptr;
120125
};
121126

122127
} // namespace livekit_bridge

bridge/src/bridge_data_track.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818

1919
#include "livekit/data_frame.h"
2020
#include "livekit/local_data_track.h"
21+
#include "livekit/local_participant.h"
2122

2223
#include <iostream>
2324

2425
namespace livekit_bridge {
2526

2627
BridgeDataTrack::BridgeDataTrack(std::string name,
27-
std::shared_ptr<livekit::LocalDataTrack> track)
28-
: name_(std::move(name)), track_(std::move(track)) {}
28+
std::shared_ptr<livekit::LocalDataTrack> track,
29+
livekit::LocalParticipant *participant)
30+
: name_(std::move(name)), track_(std::move(track)),
31+
participant_(participant) {}
2932

3033
BridgeDataTrack::~BridgeDataTrack() { release(); }
3134

@@ -77,16 +80,17 @@ void BridgeDataTrack::release() {
7780
}
7881
released_ = true;
7982

80-
if (track_) {
83+
if (participant_ && track_) {
8184
try {
82-
track_->unpublish();
85+
participant_->unpublishDataTrack(track_);
8386
} catch (...) {
84-
std::cerr << "[BridgeDataTrack] unpublish error, continuing with "
85-
"cleanup\n";
87+
std::cerr << "[BridgeDataTrack] unpublishDataTrack error, continuing "
88+
"with cleanup\n";
8689
}
8790
}
8891

8992
track_.reset();
93+
participant_ = nullptr;
9094
}
9195

9296
} // namespace livekit_bridge

bridge/src/livekit_bridge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ LiveKitBridge::createDataTrack(const std::string &name) {
312312
<< "(sid=" << track->info().sid << ").\n";
313313

314314
auto bridge_track = std::shared_ptr<BridgeDataTrack>(
315-
new BridgeDataTrack(name, std::move(track)));
315+
new BridgeDataTrack(name, std::move(track), room_->localParticipant()));
316316
published_data_tracks_.emplace_back(bridge_track);
317317
return bridge_track;
318318
}

include/livekit/local_data_track.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ class OwnedLocalDataTrack;
4040
*
4141
* Typical usage:
4242
*
43-
* auto dt = room->localParticipant()->publishDataTrack("sensor-data");
43+
* auto lp = room->localParticipant();
44+
* auto dt = lp->publishDataTrack("sensor-data");
4445
* DataFrame frame;
4546
* frame.payload = {0x01, 0x02, 0x03};
4647
* dt->tryPush(frame);
47-
* dt->unpublish();
48+
* lp->unpublishDataTrack(dt);
4849
*/
4950
class LocalDataTrack {
5051
public:
@@ -67,18 +68,12 @@ class LocalDataTrack {
6768
/// Whether the track is still published in the room.
6869
bool isPublished() const;
6970

70-
/**
71-
* Unpublish this data track from the room.
72-
*
73-
* After this call, tryPush() will fail and the track cannot be
74-
* re-published. The underlying FFI handle is released.
75-
*/
76-
void unpublish();
77-
7871
/// Construct from an owned proto (called by LocalParticipant).
7972
explicit LocalDataTrack(const proto::OwnedLocalDataTrack &owned);
8073

8174
private:
75+
friend class LocalParticipant;
76+
8277
/// Raw FFI handle id for internal use.
8378
uintptr_t ffi_handle_id() const noexcept { return handle_.get(); }
8479

include/livekit/local_participant.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class LocalParticipant : public Participant {
147147
*
148148
* Data tracks carry arbitrary binary frames and are independent of the
149149
* audio/video track hierarchy. The returned LocalDataTrack can push
150-
* frames via tryPush() and be unpublished via unpublish().
150+
* frames via tryPush() and be unpublished via unpublishDataTrack().
151151
*
152152
* @param name Unique track name visible to other participants.
153153
* @return Shared pointer to the published data track.
@@ -156,6 +156,16 @@ class LocalParticipant : public Participant {
156156
std::shared_ptr<LocalDataTrack>
157157
publishDataTrack(const std::string &name);
158158

159+
/**
160+
* Unpublish a data track from the room.
161+
*
162+
* After this call, tryPush() on the track will fail and the track
163+
* cannot be re-published.
164+
*
165+
* @param track The data track to unpublish. Must not be null.
166+
*/
167+
void unpublishDataTrack(const std::shared_ptr<LocalDataTrack> &track);
168+
159169
/**
160170
* Initiate an RPC call to a remote participant.
161171
*

src/local_data_track.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,4 @@ bool LocalDataTrack::isPublished() const {
6262
return resp.local_data_track_is_published().is_published();
6363
}
6464

65-
void LocalDataTrack::unpublish() {
66-
if (!handle_.valid()) {
67-
return;
68-
}
69-
70-
proto::FfiRequest req;
71-
auto *msg = req.mutable_local_data_track_unpublish();
72-
msg->set_track_handle(static_cast<uint64_t>(handle_.get()));
73-
74-
(void)FfiClient::instance().sendRequest(req);
75-
}
76-
7765
} // namespace livekit

src/local_participant.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,24 @@ LocalParticipant::publishDataTrack(const std::string &name) {
240240
return std::make_shared<LocalDataTrack>(owned);
241241
}
242242

243+
void LocalParticipant::unpublishDataTrack(
244+
const std::shared_ptr<LocalDataTrack> &track) {
245+
if (!track) {
246+
return;
247+
}
248+
249+
auto handle_id = track->ffi_handle_id();
250+
if (handle_id == 0) {
251+
return;
252+
}
253+
254+
proto::FfiRequest req;
255+
auto *msg = req.mutable_local_data_track_unpublish();
256+
msg->set_track_handle(static_cast<uint64_t>(handle_id));
257+
258+
(void)FfiClient::instance().sendRequest(req);
259+
}
260+
243261
std::string LocalParticipant::performRpc(
244262
const std::string &destination_identity, const std::string &method,
245263
const std::string &payload, std::optional<double> response_timeout) {

0 commit comments

Comments
 (0)