LiveKit C++ Client SDK v1.1.0
Real-time audio/video/data 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 <atomic>
20#include <condition_variable>
21#include <cstdint>
22#include <functional>
23#include <memory>
24#include <mutex>
25#include <string>
26#include <unordered_map>
27#include <vector>
28
29#include "livekit/ffi_handle.h"
30#include "livekit/local_audio_track.h"
31#include "livekit/local_data_track.h"
32#include "livekit/local_video_track.h"
33#include "livekit/participant.h"
34#include "livekit/room_event_types.h"
35#include "livekit/rpc_error.h"
36#include "livekit/visibility.h"
37
38namespace livekit {
39
40struct ParticipantTrackPermission;
41
42class FfiClient;
43class Track;
44class LocalTrackPublication;
45
48 std::string request_id;
49 std::string caller_identity;
50 std::string payload;
51 double response_timeout_sec; // seconds
52};
53
55class LIVEKIT_API LocalParticipant : public Participant {
56public:
57 using PublicationMap = std::unordered_map<std::string, std::shared_ptr<LocalTrackPublication>>;
58 using TrackMap = std::unordered_map<std::string, std::weak_ptr<Track>>;
59
68 using RpcHandler = std::function<std::optional<std::string>(const RpcInvocationData&)>;
69
70 LocalParticipant(FfiHandle handle, std::string sid, std::string name, std::string identity, std::string metadata,
71 std::unordered_map<std::string, std::string> attributes, ParticipantKind kind,
72 DisconnectReason reason);
73
78 PublicationMap trackPublications() const;
79
88 void publishData(const std::vector<std::uint8_t>& payload, bool reliable = true,
89 const std::vector<std::string>& destination_identities = {}, const std::string& topic = {});
90
98 void publishDtmf(int code, const std::string& digit);
99
107 void setMetadata(const std::string& metadata);
108
116 void setName(const std::string& name);
117
125 void setAttributes(const std::unordered_map<std::string, std::string>& attributes);
126
131 void setTrackSubscriptionPermissions(bool allow_all_participants,
132 const std::vector<ParticipantTrackPermission>& participant_permissions = {});
133
137 void publishTrack(const std::shared_ptr<Track>& track, const TrackPublishOptions& options);
138
144 std::shared_ptr<LocalVideoTrack> publishVideoTrack(const std::string& name,
145 const std::shared_ptr<VideoSource>& source,
146 TrackSource track_source);
147
153 std::shared_ptr<LocalAudioTrack> publishAudioTrack(const std::string& name,
154 const std::shared_ptr<AudioSource>& source,
155 TrackSource track_source);
156
160 void unpublishTrack(const std::string& track_sid);
161
174
181 void unpublishDataTrack(const std::shared_ptr<LocalDataTrack>& track);
182
198 std::string performRpc(const std::string& destination_identity, const std::string& method, const std::string& payload,
199 const std::optional<double>& response_timeout = std::nullopt);
200
214 void registerRpcMethod(const std::string& method_name, RpcHandler handler);
215
225 void unregisterRpcMethod(const std::string& method_name);
226
227protected:
232 void shutdown();
236 void handleRpcMethodInvocation(std::uint64_t invocation_id, const std::string& method, const std::string& request_id,
237 const std::string& caller_identity, const std::string& payload,
238 double response_timeout);
240 std::shared_ptr<TrackPublication> findTrackPublication(const std::string& sid) const override;
241 friend class Room;
242
243private:
247 mutable TrackMap published_tracks_by_sid_;
248
249 std::unordered_map<std::string, RpcHandler> rpc_handlers_;
250
251 // Shared state for RPC invocation tracking. Using shared_ptr so the state
252 // can outlive the LocalParticipant if there are in-flight invocations when
253 // the participant is destroyed.
254 struct RpcInvocationState {
255 std::mutex mutex;
256 std::condition_variable cv;
257 int active_invocations = 0;
258 bool shutting_down = false;
259 };
260 std::shared_ptr<RpcInvocationState> rpc_state_ = std::make_shared<RpcInvocationState>();
261};
262
263} // namespace livekit
RAII wrapper for an FFI handle (uintptr_t) coming from Rust.
Definition ffi_handle.h:29
Represents the local participant in a room.
Definition local_participant.h:55
PublicationMap trackPublications() const
Track publications for this participant, keyed by publication SID.
void publishDtmf(int code, const std::string &digit)
Publish a SIP DTMF (phone keypad) tone into the room.
std::shared_ptr< LocalVideoTrack > publishVideoTrack(const std::string &name, const std::shared_ptr< VideoSource > &source, TrackSource track_source)
Create a LocalVideoTrack backed by the given VideoSource, publish it, and return the track.
void unregisterRpcMethod(const std::string &method_name)
Unregister a previously registered RPC method handler.
std::shared_ptr< TrackPublication > findTrackPublication(const std::string &sid) const override
Called by Room events like kTrackMuted.
std::function< std::optional< std::string >(const RpcInvocationData &)> RpcHandler
Type of callback used to handle incoming RPC method invocations.
Definition local_participant.h:68
void publishTrack(const std::shared_ptr< Track > &track, const TrackPublishOptions &options)
Publish a local track to the room.
Result< std::shared_ptr< LocalDataTrack >, PublishDataTrackError > publishDataTrack(const std::string &name)
Publish a data track to the room.
void unpublishTrack(const std::string &track_sid)
Unpublish a track from the room by SID.
std::shared_ptr< LocalAudioTrack > publishAudioTrack(const std::string &name, const std::shared_ptr< AudioSource > &source, TrackSource track_source)
Create a LocalAudioTrack backed by the given AudioSource, publish it, and return the track.
void publishData(const std::vector< std::uint8_t > &payload, bool reliable=true, const std::vector< std::string > &destination_identities={}, const std::string &topic={})
Publish arbitrary data to the room.
void setName(const std::string &name)
Update this participant's display name on the server.
void unpublishDataTrack(const std::shared_ptr< LocalDataTrack > &track)
Unpublish a data track from the room.
void handleRpcMethodInvocation(std::uint64_t invocation_id, const std::string &method, const std::string &request_id, const std::string &caller_identity, const std::string &payload, double response_timeout)
Called by Room when an rpc_method_invocation event is received from the SFU.
void registerRpcMethod(const std::string &method_name, RpcHandler handler)
Register a handler for an incoming RPC method.
void shutdown()
Shutdown the local participant, cleaning up all resources.
void setMetadata(const std::string &metadata)
Update this participant's metadata on the server.
void setAttributes(const std::unordered_map< std::string, std::string > &attributes)
Update this participant's attributes on the server.
void setTrackSubscriptionPermissions(bool allow_all_participants, const std::vector< ParticipantTrackPermission > &participant_permissions={})
Set track subscription permissions for this participant.
std::string performRpc(const std::string &destination_identity, const std::string &method, const std::string &payload, const std::optional< double > &response_timeout=std::nullopt)
Initiate an RPC call to a remote participant.
Base class for local and remote room participants.
Definition participant.h:34
Lightweight success-or-error return type for non-exceptional API failures.
Definition result.h:40
Represents a LiveKit room session.
Definition room.h:98
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25
TrackSource
Source category for a published track.
Definition track.h:43
ParticipantKind
Identifies the type of participant connected to a room.
Definition participant.h:31
DisconnectReason
Reason why a participant or room was disconnected.
Definition room_event_types.h:80
Error details returned when publishing a local data track fails.
Definition data_track_error.h:47
Data passed to a registered RPC method handler.
Definition local_participant.h:47
Options for publishing a track to the room.
Definition room_event_types.h:286