LiveKit C++ Client SDK v1.12.2
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 <chrono>
20#include <cstdint>
21#include <future>
22#include <memory>
23#include <mutex>
24
25#include "livekit/audio_stream.h"
26#include "livekit/data_stream.h"
27#include "livekit/e2ee.h"
28#include "livekit/ffi_handle.h"
29#include "livekit/room_callbacks.h"
30#include "livekit/room_event_types.h"
31#include "livekit/stats.h"
32#include "livekit/video_stream.h"
33#include "livekit/visibility.h"
34
35namespace livekit {
36
37class RoomDelegate;
38struct RoomInfoData;
39namespace proto {
40class FfiEvent;
41}
42
43struct E2EEOptions;
44class E2EEManager;
45class LocalParticipant;
46class RemoteParticipant;
47class SubscriptionThreadDispatcher;
48
50struct IceServer {
52 std::string url;
53
55 std::string username;
56
58 std::string credential;
59};
60
62struct RtcConfig {
65
69
71 std::vector<IceServer> ice_servers;
72};
73
82 bool auto_subscribe = true;
83
96 std::optional<bool> adaptive_stream;
97
101 bool dynacast = false;
102
106 std::optional<E2EEOptions> encryption;
107
111 std::optional<RtcConfig> rtc_config;
112
116 std::optional<std::uint32_t> join_retries;
117
122
126 std::optional<std::chrono::milliseconds> connect_timeout;
127
135 std::optional<std::string> other_sdks;
137};
138
145class LIVEKIT_API Room {
146public:
147 Room();
148 ~Room();
149
159 void setDelegate(RoomDelegate* delegate);
160
179 bool connect(const std::string& url, const std::string& token, const RoomOptions& options);
180
195 bool disconnect(DisconnectReason reason = DisconnectReason::ClientInitiated);
196
197 // Accessors
198
207
222 std::weak_ptr<LocalParticipant> localParticipant() const;
223
234 std::weak_ptr<RemoteParticipant> remoteParticipant(const std::string& identity) const;
235
241 std::vector<std::weak_ptr<RemoteParticipant>> remoteParticipants() const;
242
245
257 std::future<SessionStats> getStats() const;
258
275 void registerTextStreamHandler(const std::string& topic, TextStreamHandler handler);
276
280 void unregisterTextStreamHandler(const std::string& topic);
281
297 void registerByteStreamHandler(const std::string& topic, ByteStreamHandler handler);
298
302 void unregisterByteStreamHandler(const std::string& topic);
303
313 std::weak_ptr<E2EEManager> e2eeManager() const;
314
315 // ---------------------------------------------------------------
316 // Frame callbacks
317 // ---------------------------------------------------------------
318
320 void setOnAudioFrameCallback(const std::string& participant_identity, const std::string& track_name,
321 AudioFrameCallback callback, const AudioStream::Options& opts = {});
322
324 void setOnVideoFrameCallback(const std::string& participant_identity, const std::string& track_name,
325 VideoFrameCallback callback, const VideoStream::Options& opts = {});
326
329 void setOnVideoFrameEventCallback(const std::string& participant_identity, const std::string& track_name,
330 VideoFrameEventCallback callback, const VideoStream::Options& opts = {});
331
333 void clearOnAudioFrameCallback(const std::string& participant_identity, const std::string& track_name);
334
336 void clearOnVideoFrameCallback(const std::string& participant_identity, const std::string& track_name);
337
339 DataFrameCallbackId addOnDataFrameCallback(const std::string& participant_identity, const std::string& track_name,
340 DataFrameCallback callback);
341
344
345private:
346 friend struct RoomTestAccess;
347
348 mutable std::mutex lock_;
349 ConnectionState connection_state_ = ConnectionState::Disconnected;
350 RoomDelegate* delegate_ = nullptr; // Not owned
351 RoomInfoData room_info_;
352 std::shared_ptr<FfiHandle> room_handle_;
355 std::shared_ptr<LocalParticipant> local_participant_;
356 std::unordered_map<std::string, std::shared_ptr<RemoteParticipant>> remote_participants_;
357 // Data stream
358 std::unordered_map<std::string, TextStreamHandler> text_stream_handlers_;
359 std::unordered_map<std::string, ByteStreamHandler> byte_stream_handlers_;
360 std::unordered_map<std::string, std::shared_ptr<TextStreamReader>> text_stream_readers_;
361 std::unordered_map<std::string, std::shared_ptr<ByteStreamReader>> byte_stream_readers_;
362 // The E2EE manager is owned by the room and is not shared with other objects.
363 // It is a shared_ptr just to utilize the weak_ptr interface for the e2eeManager() accessor.
364 std::shared_ptr<E2EEManager> e2ee_manager_;
365 std::shared_ptr<SubscriptionThreadDispatcher> subscription_thread_dispatcher_;
366
367 // FfiClient listener ID (0 means no listener registered)
368 int listener_id_{0};
369
370 void onEvent(const proto::FfiEvent& event);
371
372 // Shared shutdown path for explicit disconnect, server disconnect, EOS, and destruction.
373 bool shutdown(bool disconnect_ffi, DisconnectReason reason, bool notify_delegate);
374};
375} // namespace livekit
Interface for receiving room-level events.
Definition room_delegate.h:33
Represents a LiveKit room session.
Definition room.h:145
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 for incoming video frame events on a subscription reader thread.
Definition room_callbacks.h:37
std::uint64_t DataFrameCallbackId
Identifier for a data frame callback registration.
Definition room_callbacks.h:44
std::function< void(const std::vector< std::uint8_t > &payload, std::optional< std::uint64_t > user_timestamp)> DataFrameCallback
Callback for incoming data frames on a subscription reader thread.
Definition room_callbacks.h:41
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:280
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:272
std::function< void(const AudioFrame &)> AudioFrameCallback
Callback for incoming audio frames on a subscription reader thread.
Definition room_callbacks.h:31
ConnectionState
Current connection state of the room.
Definition room_event_types.h:50
DisconnectReason
Reason why a participant or room was disconnected.
Definition room_event_types.h:81
LIVEKIT_API void shutdown()
Shut down the LiveKit SDK.
std::function< void(const VideoFrame &frame, std::int64_t timestamp_us)> VideoFrameCallback
Callback for incoming video frames on a subscription reader thread.
Definition room_callbacks.h:34
Configuration options for AudioStream creation.
Definition audio_stream.h:66
Represents a single ICE server configuration.
Definition room.h:50
std::string username
Optional username for TURN authentication.
Definition room.h:55
std::string url
TURN/STUN server URL (e.g. "stun:stun.l.google.com:19302").
Definition room.h:52
std::string credential
Optional credential (password) for TURN authentication.
Definition room.h:58
Snapshot of core room information.
Definition room_event_types.h:144
Top-level room connection options.
Definition room.h:75
std::optional< bool > adaptive_stream
Enable adaptive stream for subscribed video tracks.
Definition room.h:96
bool single_peer_connection
Enable single peer connection mode.
Definition room.h:121
std::optional< std::uint32_t > join_retries
Number of retries for the initial room join after the first attempt.
Definition room.h:116
std::optional< std::chrono::milliseconds > connect_timeout
Timeout for each individual signal connection attempt.
Definition room.h:126
std::optional< RtcConfig > rtc_config
Optional WebRTC configuration (ICE policy, servers, etc.)
Definition room.h:111
bool dynacast
Enable dynacast (server sends optimal layers depending on subscribers).
Definition room.h:101
std::optional< E2EEOptions > encryption
Optional end-to-end encryption settings.
Definition room.h:106
bool auto_subscribe
If true (default), automatically subscribe to all remote tracks.
Definition room.h:82
WebRTC configuration (ICE, transport, etc.).
Definition room.h:62
int continual_gathering_policy
Continuous or single ICE gathering.
Definition room.h:68
std::vector< IceServer > ice_servers
List of STUN/TURN servers for ICE candidate generation.
Definition room.h:71
int ice_transport_type
ICE transport type (e.g., ALL, RELAY). Maps to proto::IceTransportType.
Definition room.h:64
Options for creating a decoded video frame stream.
Definition video_stream.h:69