LiveKit C++ Client SDK v1.9.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 <optional>
26#include <string>
27#include <unordered_map>
28#include <vector>
29
30#include "livekit/data_track_error.h"
31#include "livekit/data_track_options.h"
32#include "livekit/data_track_schema.h"
33#include "livekit/ffi_handle.h"
34#include "livekit/local_audio_track.h"
35#include "livekit/local_data_track.h"
36#include "livekit/local_video_track.h"
37#include "livekit/participant.h"
38#include "livekit/result.h"
39#include "livekit/room_event_types.h"
40#include "livekit/rpc_error.h"
41#include "livekit/visibility.h"
42
43namespace livekit {
44
45struct ParticipantTrackPermission;
46
47class FfiClient;
48class Track;
49class LocalTrackPublication;
50
53 std::string request_id;
54 std::string caller_identity;
55 std::string payload;
56 double response_timeout_sec; // seconds
57};
58
60class LIVEKIT_API LocalParticipant : public Participant {
61public:
62 using PublicationMap = std::unordered_map<std::string, std::shared_ptr<LocalTrackPublication>>;
63 using TrackMap = std::unordered_map<std::string, std::weak_ptr<Track>>;
64
73 using RpcHandler = std::function<std::optional<std::string>(const RpcInvocationData&)>;
74
75 LocalParticipant(FfiHandle handle, std::string sid, std::string name, std::string identity, std::string metadata,
76 std::unordered_map<std::string, std::string> attributes, ParticipantKind kind,
77 DisconnectReason reason);
78
83 PublicationMap trackPublications() const;
84
93 void publishData(const std::vector<std::uint8_t>& payload, bool reliable = true,
94 const std::vector<std::string>& destination_identities = {}, const std::string& topic = {});
95
103 void publishDtmf(int code, const std::string& digit);
104
112 void setMetadata(const std::string& metadata);
113
121 void setName(const std::string& name);
122
130 void setAttributes(const std::unordered_map<std::string, std::string>& attributes);
131
136 void setTrackSubscriptionPermissions(bool allow_all_participants,
137 const std::vector<ParticipantTrackPermission>& participant_permissions = {});
138
142 void publishTrack(const std::shared_ptr<Track>& track, const TrackPublishOptions& options);
143
149 std::shared_ptr<LocalVideoTrack> publishVideoTrack(const std::string& name,
150 const std::shared_ptr<VideoSource>& source,
151 TrackSource track_source);
152
158 std::shared_ptr<LocalAudioTrack> publishAudioTrack(const std::string& name,
159 const std::shared_ptr<AudioSource>& source,
160 TrackSource track_source);
161
165 void unpublishTrack(const std::string& track_sid);
166
179
190 const DataTrackPublishOptions& options);
191
198 void unpublishDataTrack(const std::shared_ptr<LocalDataTrack>& track);
199
217 bool defineSchema(const DataTrackSchemaId& id, const std::string& definition);
218
229 std::optional<std::string> getSchema(const DataTrackSchemaId& id, const std::string& participant_identity);
230
246 std::string performRpc(const std::string& destination_identity, const std::string& method, const std::string& payload,
247 const std::optional<double>& response_timeout = std::nullopt);
248
262 void registerRpcMethod(const std::string& method_name, RpcHandler handler);
263
273 void unregisterRpcMethod(const std::string& method_name);
274
275protected:
280 void shutdown();
284 void handleRpcMethodInvocation(std::uint64_t invocation_id, const std::string& method, const std::string& request_id,
285 const std::string& caller_identity, const std::string& payload,
286 double response_timeout);
288 std::shared_ptr<TrackPublication> findTrackPublication(const std::string& sid) const override;
289 friend class Room;
290
291private:
295 mutable TrackMap published_tracks_by_sid_;
296
297 std::unordered_map<std::string, RpcHandler> rpc_handlers_;
298
299 // Shared state for RPC invocation tracking. Using shared_ptr so the state
300 // can outlive the LocalParticipant if there are in-flight invocations when
301 // the participant is destroyed.
302 struct RpcInvocationState {
303 std::mutex mutex;
304 std::condition_variable cv;
305 int active_invocations = 0;
306 bool shutting_down = false;
307 };
308 std::shared_ptr<RpcInvocationState> rpc_state_ = std::make_shared<RpcInvocationState>();
309};
310
311} // 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:60
bool defineSchema(const DataTrackSchemaId &id, const std::string &definition)
Store the definition of a data track schema.
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::optional< std::string > getSchema(const DataTrackSchemaId &id, const std::string &participant_identity)
Retrieve the definition for a data track schema.
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:73
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.
Result< std::shared_ptr< LocalDataTrack >, PublishDataTrackError > publishDataTrack(const DataTrackPublishOptions &options)
Publish a data track to the room with explicit options.
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:142
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25
TrackSource
Source category for a published track.
Definition track.h:42
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
Options for publishing a data track.
Definition data_track_options.h:30
Uniquely identifies a data track schema.
Definition data_track_schema.h:197
Error details returned when publishing a local data track fails.
Definition data_track_error.h:48
Data passed to a registered RPC method handler.
Definition local_participant.h:52
Options for publishing a track to the room.
Definition room_event_types.h:324