LiveKit C++ Client SDK v1.1.0
Real-time audio/video/data SDK for C++
Loading...
Searching...
No Matches
room.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 <future>
20#include <memory>
21#include <mutex>
22
23#include "livekit/data_stream.h"
24#include "livekit/e2ee.h"
25#include "livekit/ffi_handle.h"
26#include "livekit/room_event_types.h"
27#include "livekit/stats.h"
28#include "livekit/subscription_thread_dispatcher.h"
29#include "livekit/visibility.h"
30
31namespace livekit {
32
33class RoomDelegate;
34struct RoomInfoData;
35namespace proto {
36class FfiEvent;
37}
38
39struct E2EEOptions;
40class E2EEManager;
41class LocalParticipant;
42class RemoteParticipant;
43
45struct IceServer {
47 std::string url;
48
50 std::string username;
51
53 std::string credential;
54};
55
57struct RtcConfig {
60
64
66 std::vector<IceServer> ice_servers;
67};
68
75 bool auto_subscribe = true;
76
78 bool dynacast = false;
79
84
86 std::optional<RtcConfig> rtc_config;
87
89 std::optional<E2EEOptions> encryption;
90};
91
98class LIVEKIT_API Room {
99public:
100 Room();
101 ~Room();
102
112 void setDelegate(RoomDelegate* delegate);
113
132 bool connect(const std::string& url, const std::string& token, const RoomOptions& options);
133
148 bool disconnect(DisconnectReason reason = DisconnectReason::ClientInitiated);
149
150 // Accessors
151
160
175 std::weak_ptr<LocalParticipant> localParticipant() const;
176
187 std::weak_ptr<RemoteParticipant> remoteParticipant(const std::string& identity) const;
188
194 std::vector<std::weak_ptr<RemoteParticipant>> remoteParticipants() const;
195
198
210 std::future<SessionStats> getStats() const;
211
228 void registerTextStreamHandler(const std::string& topic, TextStreamHandler handler);
229
233 void unregisterTextStreamHandler(const std::string& topic);
234
250 void registerByteStreamHandler(const std::string& topic, ByteStreamHandler handler);
251
255 void unregisterByteStreamHandler(const std::string& topic);
256
266 std::weak_ptr<E2EEManager> e2eeManager() const;
267
268 // ---------------------------------------------------------------
269 // Frame callbacks
270 // ---------------------------------------------------------------
271
273 void setOnAudioFrameCallback(const std::string& participant_identity, const std::string& track_name,
274 AudioFrameCallback callback, const AudioStream::Options& opts = {});
275
277 void setOnVideoFrameCallback(const std::string& participant_identity, const std::string& track_name,
278 VideoFrameCallback callback, const VideoStream::Options& opts = {});
279
282 void setOnVideoFrameEventCallback(const std::string& participant_identity, const std::string& track_name,
283 VideoFrameEventCallback callback, const VideoStream::Options& opts = {});
284
286 void clearOnAudioFrameCallback(const std::string& participant_identity, const std::string& track_name);
287
289 void clearOnVideoFrameCallback(const std::string& participant_identity, const std::string& track_name);
290
292 DataFrameCallbackId addOnDataFrameCallback(const std::string& participant_identity, const std::string& track_name,
293 DataFrameCallback callback);
294
297
298private:
299 friend class RoomCallbackTest;
300
301 mutable std::mutex lock_;
302 ConnectionState connection_state_ = ConnectionState::Disconnected;
303 RoomDelegate* delegate_ = nullptr; // Not owned
304 RoomInfoData room_info_;
305 std::shared_ptr<FfiHandle> room_handle_;
308 std::shared_ptr<LocalParticipant> local_participant_;
309 std::unordered_map<std::string, std::shared_ptr<RemoteParticipant>> remote_participants_;
310 // Data stream
311 std::unordered_map<std::string, TextStreamHandler> text_stream_handlers_;
312 std::unordered_map<std::string, ByteStreamHandler> byte_stream_handlers_;
313 std::unordered_map<std::string, std::shared_ptr<TextStreamReader>> text_stream_readers_;
314 std::unordered_map<std::string, std::shared_ptr<ByteStreamReader>> byte_stream_readers_;
315 // The E2EE manager is owned by the room and is not shared with other objects.
316 // It is a shared_ptr just to utilize the weak_ptr interface for the e2eeManager() accessor.
317 std::shared_ptr<E2EEManager> e2ee_manager_;
318 std::shared_ptr<SubscriptionThreadDispatcher> subscription_thread_dispatcher_;
319
320 // FfiClient listener ID (0 means no listener registered)
321 int listener_id_{0};
322
323 void onEvent(const proto::FfiEvent& event);
324};
325} // namespace livekit
Interface for receiving room-level events.
Definition room_delegate.h:33
Represents a LiveKit room session.
Definition room.h:98
void registerByteStreamHandler(const std::string &topic, ByteStreamHandler handler)
Register a handler for incoming byte streams on a specific topic.
void setDelegate(RoomDelegate *delegate)
Assign a RoomDelegate that receives room lifecycle callbacks.
bool disconnect(DisconnectReason reason=DisconnectReason::ClientInitiated)
Disconnect from the room.
void setOnVideoFrameEventCallback(const std::string &participant_identity, const std::string &track_name, VideoFrameEventCallback callback, const VideoStream::Options &opts={})
Sets the video frame event callback via SubscriptionThreadDispatcher.
std::future< SessionStats > getStats() const
Retrieve aggregated WebRTC stats for this room session.
void unregisterByteStreamHandler(const std::string &topic)
Unregister the byte stream handler for the given topic.
std::weak_ptr< E2EEManager > e2eeManager() const
Returns the room's E2EE manager as a weak handle, or an empty handle if E2EE was not enabled at conne...
std::weak_ptr< LocalParticipant > localParticipant() const
Get the local participant.
void registerTextStreamHandler(const std::string &topic, TextStreamHandler handler)
Register a handler for incoming text streams on a specific topic.
void removeOnDataFrameCallback(DataFrameCallbackId id)
Removes the data frame callback via SubscriptionThreadDispatcher.
void unregisterTextStreamHandler(const std::string &topic)
Unregister the text stream handler for the given topic.
void setOnVideoFrameCallback(const std::string &participant_identity, const std::string &track_name, VideoFrameCallback callback, const VideoStream::Options &opts={})
Sets the video frame callback via SubscriptionThreadDispatcher.
RoomInfoData roomInfo() const
Retrieve static metadata about the room.
bool connect(const std::string &url, const std::string &token, const RoomOptions &options)
Connect to a LiveKit room using the given URL and token, applying the supplied connection options.
void clearOnVideoFrameCallback(const std::string &participant_identity, const std::string &track_name)
Clears the video frame callback via SubscriptionThreadDispatcher.
void setOnAudioFrameCallback(const std::string &participant_identity, const std::string &track_name, AudioFrameCallback callback, const AudioStream::Options &opts={})
Sets the audio frame callback via SubscriptionThreadDispatcher.
DataFrameCallbackId addOnDataFrameCallback(const std::string &participant_identity, const std::string &track_name, DataFrameCallback callback)
Adds a data frame callback via SubscriptionThreadDispatcher.
std::vector< std::weak_ptr< RemoteParticipant > > remoteParticipants() const
Returns a snapshot of all current remote participants.
void clearOnAudioFrameCallback(const std::string &participant_identity, const std::string &track_name)
Clears the audio frame callback via SubscriptionThreadDispatcher.
ConnectionState connectionState() const
Returns the current connection state of the room.
std::weak_ptr< RemoteParticipant > remoteParticipant(const std::string &identity) const
Look up a remote participant by identity.
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25
std::function< void(const VideoFrameEvent &)> VideoFrameEventCallback
Callback type for incoming video frame events.
Definition subscription_thread_dispatcher.h:51
std::uint64_t DataFrameCallbackId
Opaque identifier returned by addOnDataFrameCallback, used to remove an individual subscription via r...
Definition subscription_thread_dispatcher.h:62
std::function< void(const std::vector< std::uint8_t > &payload, std::optional< std::uint64_t > user_timestamp)> DataFrameCallback
Callback type for incoming data track frames.
Definition subscription_thread_dispatcher.h:58
std::function< void(std::shared_ptr< ByteStreamReader >, const std::string &participant_identity)> ByteStreamHandler
Callback invoked when a new incoming byte stream is opened.
Definition data_stream.h:278
std::function< void(std::shared_ptr< TextStreamReader >, const std::string &participant_identity)> TextStreamHandler
Callback invoked when a new incoming text stream is opened.
Definition data_stream.h:270
std::function< void(const AudioFrame &)> AudioFrameCallback
Callback type for incoming audio frames.
Definition subscription_thread_dispatcher.h:43
ConnectionState
Current connection state of the room.
Definition room_event_types.h:49
DisconnectReason
Reason why a participant or room was disconnected.
Definition room_event_types.h:80
std::function< void(const VideoFrame &frame, std::int64_t timestamp_us)> VideoFrameCallback
Callback type for incoming video frames.
Definition subscription_thread_dispatcher.h:47
Configuration options for AudioStream creation.
Definition audio_stream.h:66
Represents a single ICE server configuration.
Definition room.h:45
std::string username
Optional username for TURN authentication.
Definition room.h:50
std::string url
TURN/STUN server URL (e.g. "stun:stun.l.google.com:19302").
Definition room.h:47
std::string credential
Optional credential (password) for TURN authentication.
Definition room.h:53
Snapshot of core room information.
Definition room_event_types.h:125
Top-level room connection options.
Definition room.h:70
bool single_peer_connection
Enable single peer connection mode.
Definition room.h:83
std::optional< RtcConfig > rtc_config
Optional WebRTC configuration (ICE policy, servers, etc.)
Definition room.h:86
bool dynacast
Enable dynacast (server sends optimal layers depending on subscribers).
Definition room.h:78
std::optional< E2EEOptions > encryption
Optional end-to-end encryption settings.
Definition room.h:89
bool auto_subscribe
If true (default), automatically subscribe to all remote tracks.
Definition room.h:75
WebRTC configuration (ICE, transport, etc.).
Definition room.h:57
int continual_gathering_policy
Continuous or single ICE gathering.
Definition room.h:63
std::vector< IceServer > ice_servers
List of STUN/TURN servers for ICE candidate generation.
Definition room.h:66
int ice_transport_type
ICE transport type (e.g., ALL, RELAY). Maps to proto::IceTransportType.
Definition room.h:59
Options for creating a decoded video frame stream.
Definition video_stream.h:69