Skip to content

Commit 1ee53a6

Browse files
Fix/guard connection state (#114)
* fix: pragma once guards, Room::connectionState(), and test coverage - Convert #ifndef include guards to #pragma once in result.h, data_track_error.h, room.h, and subscription_thread_dispatcher.h for consistency with the rest of the public headers - Add Room::connectionState() public accessor (lock-guarded, purely additive — no existing API changed) - Fix VideoStream class-level doc comment (was copy-pasted from AudioStream: wrong class name and example types) - Add unit tests for Room::connectionState() in test_room_callbacks.cpp - Add unit tests for Result<T,E> and Result<void,E> in test_result.cpp (addresses TODO in result.h requesting invariant test coverage) --------- Co-authored-by: alan-george-lk <alan.george@livekit.io>
1 parent 56732cd commit 1ee53a6

14 files changed

Lines changed: 275 additions & 42 deletions

‎include/livekit/data_track_error.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef LIVEKIT_DATA_TRACK_ERROR_H
18-
#define LIVEKIT_DATA_TRACK_ERROR_H
17+
#pragma once
1918

2019
#include <cstdint>
2120
#include <string>
@@ -83,5 +82,3 @@ struct SubscribeDataTrackError {
8382
};
8483

8584
} // namespace livekit
86-
87-
#endif // LIVEKIT_DATA_TRACK_ERROR_H

‎include/livekit/local_audio_track.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class LIVEKIT_API LocalAudioTrack : public Track {
8686
/// Sets the publication that owns this track.
8787
/// Note: std::move on a const& silently falls back to a copy, so we assign
8888
/// directly. Changing the virtual signature to take by value would enable
89-
/// a true move but is an API-breaking change left for a future revision.
89+
/// a true move but is an API-breaking change hence left for a future revision.
9090
void setPublication(const std::shared_ptr<LocalTrackPublication>& publication) noexcept override {
9191
local_publication_ = publication;
9292
}
@@ -99,4 +99,4 @@ class LIVEKIT_API LocalAudioTrack : public Track {
9999
std::shared_ptr<LocalTrackPublication> local_publication_;
100100
};
101101

102-
} // namespace livekit
102+
} // namespace livekit

‎include/livekit/result.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef LIVEKIT_RESULT_H
18-
#define LIVEKIT_RESULT_H
17+
#pragma once
1918

2019
#include <cassert>
2120
#include <optional>
@@ -187,5 +186,3 @@ class [[nodiscard]] Result<void, E> {
187186
};
188187

189188
} // namespace livekit
190-
191-
#endif // LIVEKIT_RESULT_H

‎include/livekit/room.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef LIVEKIT_ROOM_H
18-
#define LIVEKIT_ROOM_H
17+
#pragma once
1918

2019
#include <cstdint>
2120
#include <memory>
@@ -173,6 +172,9 @@ class LIVEKIT_API Room {
173172
/// Returns a snapshot of all current remote participants.
174173
std::vector<std::shared_ptr<RemoteParticipant>> remoteParticipants() const;
175174

175+
/// Returns the current connection state of the room.
176+
ConnectionState connectionState() const;
177+
176178
/* Register a handler for incoming text streams on a specific topic.
177179
*
178180
* When a remote participant opens a text stream with the given topic,
@@ -324,5 +326,3 @@ class LIVEKIT_API Room {
324326
void OnEvent(const proto::FfiEvent& event);
325327
};
326328
} // namespace livekit
327-
328-
#endif /* LIVEKIT_ROOM_H */

‎include/livekit/subscription_thread_dispatcher.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef LIVEKIT_SUBSCRIPTION_THREAD_DISPATCHER_H
18-
#define LIVEKIT_SUBSCRIPTION_THREAD_DISPATCHER_H
17+
#pragma once
1918

2019
#include <cstdint>
2120
#include <functional>
@@ -459,5 +458,3 @@ class LIVEKIT_API SubscriptionThreadDispatcher {
459458
};
460459

461460
} // namespace livekit
462-
463-
#endif /* LIVEKIT_SUBSCRIPTION_THREAD_DISPATCHER_H */

‎include/livekit/video_stream.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ namespace proto {
4949
class FfiEvent;
5050
}
5151

52-
// Represents a pull-based stream of decoded PCM audio frames coming from
53-
// a remote (or local) LiveKit track. Similar to VideoStream, but for audio.
52+
// Represents a pull-based stream of decoded video frames coming from
53+
// a remote (or local) LiveKit track. Similar to AudioStream, but for video.
5454
//
5555
// Typical usage:
5656
//
5757
// VideoStream::Options opts;
5858
// auto stream = VideoStream::fromTrack(remoteVideoTrack, opts);
5959
//
60-
// AudioFrameEvent ev;
60+
// VideoFrameEvent ev;
6161
// while (stream->read(ev)) {
62-
// // ev.frame contains interleaved int16 PCM samples
62+
// // ev.frame contains the decoded video buffer
6363
// }
6464
//
6565
// stream->close(); // optional, called automatically in destructor

‎src/ffi_client.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef LIVEKIT_FFI_CLIENT_H
18-
#define LIVEKIT_FFI_CLIENT_H
17+
#pragma once
1918

2019
#include <atomic>
2120
#include <cstdint>
@@ -196,5 +195,3 @@ class LIVEKIT_INTERNAL_API FfiClient {
196195
std::atomic<bool> initialized_{false};
197196
};
198197
} // namespace livekit
199-
200-
#endif /* LIVEKIT_FFI_CLIENT_H */

‎src/lk_log.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef LIVEKIT_LK_LOG_H
18-
#define LIVEKIT_LK_LOG_H
17+
#pragma once
1918

2019
#include <spdlog/spdlog.h>
2120

@@ -51,5 +50,3 @@ LIVEKIT_INTERNAL_API void shutdownLogger();
5150
#define LK_LOG_WARN(...) SPDLOG_LOGGER_WARN(livekit::detail::getLogger(), __VA_ARGS__)
5251
#define LK_LOG_ERROR(...) SPDLOG_LOGGER_ERROR(livekit::detail::getLogger(), __VA_ARGS__)
5352
#define LK_LOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(livekit::detail::getLogger(), __VA_ARGS__)
54-
55-
#endif /* LIVEKIT_LK_LOG_H */

‎src/room.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ std::vector<std::shared_ptr<RemoteParticipant>> Room::remoteParticipants() const
220220
return out;
221221
}
222222

223+
ConnectionState Room::connectionState() const {
224+
const std::scoped_lock<std::mutex> g(lock_);
225+
return connection_state_;
226+
}
227+
223228
E2EEManager* Room::e2eeManager() const {
224229
const std::scoped_lock<std::mutex> g(lock_);
225230
return e2ee_manager_.get();

‎src/tests/unit/test_result.cpp‎

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
/*
2+
* Copyright 2026 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+
/// @file test_result.cpp
18+
/// @brief Unit tests for the Result<T, E> and Result<void, E> types.
19+
///
20+
/// Covers the invariants documented in result.h:
21+
/// - ok() / has_error() / bool conversion correctness
22+
/// - value() and error() accessor semantics for lvalue, rvalue, and const
23+
/// overloads
24+
/// - Move construction and forwarding behaviour
25+
/// - void specialization
26+
27+
#include <gtest/gtest.h>
28+
#include <livekit/result.h>
29+
30+
#include <memory>
31+
#include <string>
32+
#include <utility>
33+
34+
namespace livekit {
35+
36+
// ---------------------------------------------------------------------------
37+
// Helpers
38+
// ---------------------------------------------------------------------------
39+
40+
struct SimpleError {
41+
int code{0};
42+
std::string message;
43+
};
44+
45+
// ---------------------------------------------------------------------------
46+
// Result<T, E> — success path
47+
// ---------------------------------------------------------------------------
48+
49+
TEST(ResultTest, SuccessOkIsTrue) {
50+
auto r = Result<int, SimpleError>::success(42);
51+
EXPECT_TRUE(r.ok());
52+
}
53+
54+
TEST(ResultTest, SuccessHasErrorIsFalse) {
55+
auto r = Result<int, SimpleError>::success(42);
56+
EXPECT_FALSE(r.has_error());
57+
}
58+
59+
TEST(ResultTest, SuccessBoolConversionIsTrue) {
60+
auto r = Result<int, SimpleError>::success(42);
61+
EXPECT_TRUE(static_cast<bool>(r));
62+
}
63+
64+
TEST(ResultTest, SuccessValueMatchesInput) {
65+
auto r = Result<int, SimpleError>::success(99);
66+
EXPECT_EQ(r.value(), 99);
67+
}
68+
69+
TEST(ResultTest, SuccessConstValueMatchesInput) {
70+
const auto r = Result<int, SimpleError>::success(7);
71+
EXPECT_EQ(r.value(), 7);
72+
}
73+
74+
TEST(ResultTest, SuccessValueCanBeMutated) {
75+
auto r = Result<int, SimpleError>::success(1);
76+
r.value() = 100;
77+
EXPECT_EQ(r.value(), 100);
78+
}
79+
80+
TEST(ResultTest, SuccessStringValue) {
81+
auto r = Result<std::string, SimpleError>::success("hello");
82+
EXPECT_EQ(r.value(), "hello");
83+
}
84+
85+
TEST(ResultTest, SuccessMoveValueTransfersOwnership) {
86+
auto r = Result<std::unique_ptr<int>, SimpleError>::success(std::make_unique<int>(55));
87+
auto ptr = std::move(r).value();
88+
ASSERT_NE(ptr, nullptr);
89+
EXPECT_EQ(*ptr, 55);
90+
}
91+
92+
// ---------------------------------------------------------------------------
93+
// Result<T, E> — failure path
94+
// ---------------------------------------------------------------------------
95+
96+
TEST(ResultTest, FailureOkIsFalse) {
97+
auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"});
98+
EXPECT_FALSE(r.ok());
99+
}
100+
101+
TEST(ResultTest, FailureHasErrorIsTrue) {
102+
auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"});
103+
EXPECT_TRUE(r.has_error());
104+
}
105+
106+
TEST(ResultTest, FailureBoolConversionIsFalse) {
107+
auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"});
108+
EXPECT_FALSE(static_cast<bool>(r));
109+
}
110+
111+
TEST(ResultTest, FailureErrorCodeMatchesInput) {
112+
auto r = Result<int, SimpleError>::failure(SimpleError{42, "bad"});
113+
EXPECT_EQ(r.error().code, 42);
114+
EXPECT_EQ(r.error().message, "bad");
115+
}
116+
117+
TEST(ResultTest, FailureConstErrorMatchesInput) {
118+
const auto r = Result<int, SimpleError>::failure(SimpleError{3, "err"});
119+
EXPECT_EQ(r.error().code, 3);
120+
}
121+
122+
TEST(ResultTest, FailureMoveErrorTransfersOwnership) {
123+
auto r = Result<int, std::unique_ptr<SimpleError>>::failure(std::make_unique<SimpleError>(SimpleError{9, "moved"}));
124+
auto err = std::move(r).error();
125+
ASSERT_NE(err, nullptr);
126+
EXPECT_EQ(err->code, 9);
127+
}
128+
129+
TEST(ResultTest, FailureStringError) {
130+
auto r = Result<int, std::string>::failure("something went wrong");
131+
EXPECT_EQ(r.error(), "something went wrong");
132+
}
133+
134+
// ---------------------------------------------------------------------------
135+
// Result<void, E> — success path
136+
// ---------------------------------------------------------------------------
137+
138+
TEST(ResultVoidTest, SuccessOkIsTrue) {
139+
auto r = Result<void, SimpleError>::success();
140+
EXPECT_TRUE(r.ok());
141+
}
142+
143+
TEST(ResultVoidTest, SuccessHasErrorIsFalse) {
144+
auto r = Result<void, SimpleError>::success();
145+
EXPECT_FALSE(r.has_error());
146+
}
147+
148+
TEST(ResultVoidTest, SuccessBoolConversionIsTrue) {
149+
auto r = Result<void, SimpleError>::success();
150+
EXPECT_TRUE(static_cast<bool>(r));
151+
}
152+
153+
TEST(ResultVoidTest, SuccessValueIsCallable) {
154+
auto r = Result<void, SimpleError>::success();
155+
EXPECT_NO_THROW(r.value());
156+
}
157+
158+
// ---------------------------------------------------------------------------
159+
// Result<void, E> — failure path
160+
// ---------------------------------------------------------------------------
161+
162+
TEST(ResultVoidTest, FailureOkIsFalse) {
163+
auto r = Result<void, SimpleError>::failure(SimpleError{5, "void fail"});
164+
EXPECT_FALSE(r.ok());
165+
}
166+
167+
TEST(ResultVoidTest, FailureHasErrorIsTrue) {
168+
auto r = Result<void, SimpleError>::failure(SimpleError{5, "void fail"});
169+
EXPECT_TRUE(r.has_error());
170+
}
171+
172+
TEST(ResultVoidTest, FailureBoolConversionIsFalse) {
173+
auto r = Result<void, SimpleError>::failure(SimpleError{5, "void fail"});
174+
EXPECT_FALSE(static_cast<bool>(r));
175+
}
176+
177+
TEST(ResultVoidTest, FailureErrorMatchesInput) {
178+
auto r = Result<void, SimpleError>::failure(SimpleError{7, "nope"});
179+
EXPECT_EQ(r.error().code, 7);
180+
EXPECT_EQ(r.error().message, "nope");
181+
}
182+
183+
TEST(ResultVoidTest, FailureMoveError) {
184+
auto r = Result<void, std::string>::failure("void error");
185+
auto msg = std::move(r).error();
186+
EXPECT_EQ(msg, "void error");
187+
}
188+
189+
// ---------------------------------------------------------------------------
190+
// if-result idiom
191+
// ---------------------------------------------------------------------------
192+
193+
TEST(ResultTest, IfResultIdiomSuccessEntersBranch) {
194+
auto r = Result<int, SimpleError>::success(1);
195+
bool entered = false;
196+
if (r) {
197+
entered = true;
198+
}
199+
EXPECT_TRUE(entered);
200+
}
201+
202+
TEST(ResultTest, IfResultIdiomFailureSkipsBranch) {
203+
auto r = Result<int, SimpleError>::failure(SimpleError{});
204+
bool entered = false;
205+
if (r) {
206+
entered = true;
207+
}
208+
EXPECT_FALSE(entered);
209+
}
210+
211+
} // namespace livekit

0 commit comments

Comments
 (0)