LiveKit C++ SDK
Real-time audio/video SDK for C++
Loading...
Searching...
No Matches
local_participant.h
1/*
2 * Copyright 2025 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 "livekit/ffi_handle.h"
20#include "livekit/participant.h"
21#include "livekit/room_event_types.h"
22#include "livekit/rpc_error.h"
23
24#include <atomic>
25#include <condition_variable>
26#include <cstdint>
27#include <functional>
28#include <memory>
29#include <mutex>
30#include <string>
31#include <unordered_map>
32#include <vector>
33
34namespace livekit {
35
36struct ParticipantTrackPermission;
37
38class FfiClient;
39class Track;
40class LocalTrackPublication;
41
43 std::string request_id;
44 std::string caller_identity;
45 std::string payload;
46 double response_timeout_sec; // seconds
47};
48
55public:
56 using PublicationMap =
57 std::unordered_map<std::string, std::shared_ptr<LocalTrackPublication>>;
58
69 using RpcHandler =
70 std::function<std::optional<std::string>(const RpcInvocationData &)>;
71
72 LocalParticipant(FfiHandle handle, std::string sid, std::string name,
73 std::string identity, std::string metadata,
74 std::unordered_map<std::string, std::string> attributes,
75 ParticipantKind kind, DisconnectReason reason);
76
78 const PublicationMap &trackPublications() const noexcept {
79 return track_publications_;
80 }
81
92 void publishData(const std::vector<std::uint8_t> &payload,
93 bool reliable = true,
94 const std::vector<std::string> &destination_identities = {},
95 const std::string &topic = {});
96
100 void publishDtmf(int code, const std::string &digit);
101
102 // -------------------------------------------------------------------------
103 // Metadata APIs (set metadata / name / attributes)
104 // -------------------------------------------------------------------------
105
106 void setMetadata(const std::string &metadata);
107 void setName(const std::string &name);
108 void
109 setAttributes(const std::unordered_map<std::string, std::string> &attributes);
110
117 void
118 setTrackSubscriptionPermissions(bool allow_all_participants,
119 const std::vector<ParticipantTrackPermission>
120 &participant_permissions = {});
121
127 std::shared_ptr<LocalTrackPublication>
128 publishTrack(const std::shared_ptr<Track> &track,
129 const TrackPublishOptions &options);
130
136 void unpublishTrack(const std::string &track_sid);
137
155 std::string performRpc(const std::string &destination_identity,
156 const std::string &method, const std::string &payload,
157 std::optional<double> response_timeout = std::nullopt);
158
175 void registerRpcMethod(const std::string &method_name, RpcHandler handler);
176
188 void unregisterRpcMethod(const std::string &method_name);
189
190protected:
197 void shutdown();
198 // Called by Room when an rpc_method_invocation event is received from the
199 // SFU. This is internal plumbing and not intended to be called directly by
200 // SDK users.
201 void handleRpcMethodInvocation(std::uint64_t invocation_id,
202 const std::string &method,
203 const std::string &request_id,
204 const std::string &caller_identity,
205 const std::string &payload,
206 double response_timeout);
207 // Called by Room events like kTrackMuted.
208 std::shared_ptr<TrackPublication>
209 findTrackPublication(const std::string &sid) const override;
210 friend class Room;
211
212private:
213 PublicationMap track_publications_;
214 std::unordered_map<std::string, RpcHandler> rpc_handlers_;
215
216 // Shared state for RPC invocation tracking. Using shared_ptr so the state
217 // can outlive the LocalParticipant if there are in-flight invocations when
218 // the participant is destroyed.
219 struct RpcInvocationState {
220 std::mutex mutex;
221 std::condition_variable cv;
222 int active_invocations = 0;
223 bool shutting_down = false;
224 };
225 std::shared_ptr<RpcInvocationState> rpc_state_ =
226 std::make_shared<RpcInvocationState>();
227};
228
229} // namespace livekit
RAII wrapper for an FFI handle (uintptr_t) coming from Rust.
Definition ffi_handle.h:29
Definition local_participant.h:54
void publishDtmf(int code, const std::string &digit)
std::shared_ptr< LocalTrackPublication > publishTrack(const std::shared_ptr< Track > &track, const TrackPublishOptions &options)
void unregisterRpcMethod(const std::string &method_name)
std::function< std::optional< std::string >(const RpcInvocationData &)> RpcHandler
Definition local_participant.h:70
void unpublishTrack(const std::string &track_sid)
void publishData(const std::vector< std::uint8_t > &payload, bool reliable=true, const std::vector< std::string > &destination_identities={}, const std::string &topic={})
const PublicationMap & trackPublications() const noexcept
Track publications associated with this participant, keyed by track SID.
Definition local_participant.h:78
std::string performRpc(const std::string &destination_identity, const std::string &method, const std::string &payload, std::optional< double > response_timeout=std::nullopt)
void registerRpcMethod(const std::string &method_name, RpcHandler handler)
void setTrackSubscriptionPermissions(bool allow_all_participants, const std::vector< ParticipantTrackPermission > &participant_permissions={})
Definition participant.h:31
Definition room.h:89
Definition local_participant.h:42
Definition room_event_types.h:305