LiveKit C++ SDK
Real-time audio/video 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#ifndef LIVEKIT_ROOM_H
18#define LIVEKIT_ROOM_H
19
20#include "livekit/data_stream.h"
21#include "livekit/e2ee.h"
22#include "livekit/ffi_handle.h"
23#include "livekit/room_event_types.h"
24#include <memory>
25#include <mutex>
26
27namespace livekit {
28
29class RoomDelegate;
30struct RoomInfoData;
31namespace proto {
32class FfiEvent;
33}
34
35struct E2EEOptions;
36class E2EEManager;
37class LocalParticipant;
38class RemoteParticipant;
39
40// Represents a single ICE server configuration.
41struct IceServer {
42 // TURN/STUN server URL (e.g. "stun:stun.l.google.com:19302").
43 std::string url;
44
45 // Optional username for TURN authentication.
46 std::string username;
47
48 // Optional credential (password) for TURN authentication.
49 std::string credential;
50};
51
52// WebRTC configuration (ICE, transport, etc.).
53struct RtcConfig {
54 // ICE transport type (e.g., ALL, RELAY). Maps to proto::IceTransportType.
55 int ice_transport_type = 0;
56
57 // Continuous or single ICE gathering. Maps to
58 // proto::ContinualGatheringPolicy.
59 int continual_gathering_policy = 0;
60
61 // List of STUN/TURN servers for ICE candidate generation.
62 std::vector<IceServer> ice_servers;
63};
64
65// Top-level room connection options.
67 // If true (default), automatically subscribe to all remote tracks.
68 // This is CRITICAL. Without auto_subscribe, you will never receive:
69 // - `track_subscribed` events
70 // - remote audio/video frames
71 bool auto_subscribe = true;
72
73 // Enable dynacast (server sends optimal layers depending on subscribers).
74 bool dynacast = false;
75
76 // Optional WebRTC configuration (ICE policy, servers, etc.)
77 std::optional<RtcConfig> rtc_config;
78
79 // Optional end-to-end encryption settings.
80 std::optional<E2EEOptions> encryption;
81};
82
89class Room {
90public:
91 Room();
92 ~Room();
93
94 /* Assign a RoomDelegate that receives room lifecycle callbacks.
95 *
96 * The delegate must remain valid for the lifetime of the Room or until a
97 * different delegate is assigned. The Room does not take ownership.
98 * Typical usage:
99 * class MyDelegate : public RoomDelegate { ... };
100 * MyDelegate del;
101 * Room room;
102 * room.setDelegate(&del);
103 */
104 void setDelegate(RoomDelegate *delegate);
105
106 /* Connect to a LiveKit room using the given URL and token, applying the
107 * supplied connection options.
108 *
109 * Parameters:
110 * url — WebSocket URL of the LiveKit server.
111 * token — Access token for authentication.
112 * options — Connection options controlling auto-subscribe,
113 * dynacast, E2EE, and WebRTC configuration.
114 * Behavior:
115 * - Registers an FFI event listener *before* sending the connect request.
116 * - Sends a proto::FfiRequest::Connect with the URL, token,
117 * and the provided RoomOptions.
118 * - Blocks until the FFI connect response arrives.
119 * - Initializes local participant and remote participants.
120 * - Emits room/participant/track events to the delegate.
121 * IMPORTANT:
122 * RoomOptions defaults auto_subscribe = true.
123 * Without auto_subscribe enabled, remote tracks will NOT be subscribed
124 * automatically, and no remote audio/video will ever arrive.
125 */
126 bool Connect(const std::string &url, const std::string &token,
127 const RoomOptions &options);
128
129 // Accessors
130
131 /* Retrieve static metadata about the room.
132 * This contains fields such as:
133 * - SID
134 * - room name
135 * - metadata
136 * - participant counts
137 * - creation timestamp
138 */
139 RoomInfoData room_info() const;
140
141 /* Get the local participant.
142 *
143 * This object represents the current user, including:
144 * - published tracks (audio/video/screen)
145 * - identity, SID, metadata
146 * - publishing/unpublishing operations
147 * Return value:
148 * Non-null pointer after successful Connect().
149 */
150 LocalParticipant *localParticipant() const;
151
152 /* Look up a remote participant by identity.
153 *
154 * Parameters:
155 * identity — The participant’s identity string (not SID)
156 * Return value:
157 * Pointer to RemoteParticipant if present, otherwise nullptr.
158 * RemoteParticipant contains:
159 * - identity/name/metadata
160 * - track publications
161 * - callbacks for track subscribed/unsubscribed, muted/unmuted
162 */
163 RemoteParticipant *remoteParticipant(const std::string &identity) const;
164
166 std::vector<std::shared_ptr<RemoteParticipant>> remoteParticipants() const;
167
168 /* Register a handler for incoming text streams on a specific topic.
169 *
170 * When a remote participant opens a text stream with the given topic,
171 * the handler is invoked with:
172 * - a shared_ptr<TextStreamReader> for consuming the stream
173 * - the identity of the participant who sent the stream
174 *
175 * Notes:
176 * - Only one handler may be registered per topic.
177 * - If no handler is registered for a topic, incoming streams with that
178 * topic are ignored.
179 * - The handler is invoked on the Room event thread. The handler must
180 * not block; spawn a background thread if synchronous reading is
181 * required.
182 *
183 * Throws:
184 * std::runtime_error if a handler is already registered for the topic.
185 */
186 void registerTextStreamHandler(const std::string &topic,
187 TextStreamHandler handler);
188
189 /* Unregister the text stream handler for the given topic.
190 *
191 * If no handler exists for the topic, this function is a no-op.
192 */
193 void unregisterTextStreamHandler(const std::string &topic);
194
195 /* Register a handler for incoming byte streams on a specific topic.
196 *
197 * When a remote participant opens a byte stream with the given topic,
198 * the handler is invoked with:
199 * - a shared_ptr<ByteStreamReader> for consuming the stream
200 * - the identity of the participant who sent the stream
201 *
202 * Notes:
203 * - Only one handler may be registered per topic.
204 * - If no handler is registered for a topic, incoming streams with that
205 * topic are ignored.
206 * - The ByteStreamReader remains valid as long as the shared_ptr is held,
207 * preventing lifetime-related crashes when reading asynchronously.
208 *
209 * Throws:
210 * std::runtime_error if a handler is already registered for the topic.
211 */
212 void registerByteStreamHandler(const std::string &topic,
213 ByteStreamHandler handler);
214
215 /* Unregister the byte stream handler for the given topic.
216 *
217 * If no handler exists for the topic, this function is a no-op.
218 */
219 void unregisterByteStreamHandler(const std::string &topic);
220
230
231private:
232 mutable std::mutex lock_;
233 ConnectionState connection_state_ = ConnectionState::Disconnected;
234 RoomDelegate *delegate_ = nullptr; // Not owned
235 RoomInfoData room_info_;
236 std::shared_ptr<FfiHandle> room_handle_;
237 std::unique_ptr<LocalParticipant> local_participant_;
238 std::unordered_map<std::string, std::shared_ptr<RemoteParticipant>>
239 remote_participants_;
240 // Data stream
241 std::unordered_map<std::string, TextStreamHandler> text_stream_handlers_;
242 std::unordered_map<std::string, ByteStreamHandler> byte_stream_handlers_;
243 std::unordered_map<std::string, std::shared_ptr<TextStreamReader>>
244 text_stream_readers_;
245 std::unordered_map<std::string, std::shared_ptr<ByteStreamReader>>
246 byte_stream_readers_;
247 // E2EE
248 std::unique_ptr<E2EEManager> e2ee_manager_;
249
250 // FfiClient listener ID (0 means no listener registered)
251 int listener_id_{0};
252
253 void OnEvent(const proto::FfiEvent &event);
254};
255} // namespace livekit
256
257#endif /* LIVEKIT_ROOM_H */
Definition e2ee.h:109
Definition local_participant.h:54
Definition remote_participant.h:29
Definition room_delegate.h:34
Definition room.h:89
std::vector< std::shared_ptr< RemoteParticipant > > remoteParticipants() const
Returns a snapshot of all current remote participants.
E2EEManager * e2eeManager() const
Definition room.h:41
Definition room_event_types.h:133
Definition room.h:66
Definition room.h:53