Skip to content

Commit dce08a2

Browse files
all examples from cpp SDK
1 parent e701750 commit dce08a2

54 files changed

Lines changed: 5575 additions & 2 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1+
# Copyright 2026 LiveKit, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
cmake_minimum_required(VERSION 3.20)
216
project(livekit_cpp_example_collection LANGUAGES CXX)
317
set(CMAKE_CXX_STANDARD 17)
418
set(CMAKE_CXX_STANDARD_REQUIRED ON)
19+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
520

621
# Make "include(LiveKitSDK)" search in ./cmake
722
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
@@ -21,5 +36,37 @@ else()
2136
)
2237
endif()
2338
find_package(LiveKit CONFIG REQUIRED)
39+
40+
if(TARGET LiveKit::livekit)
41+
set(LIVEKIT_CORE_TARGET LiveKit::livekit)
42+
elseif(TARGET livekit)
43+
set(LIVEKIT_CORE_TARGET livekit)
44+
else()
45+
message(FATAL_ERROR "Could not find a LiveKit core target (expected LiveKit::livekit or livekit).")
46+
endif()
47+
48+
if(DEFINED _SPDLOG_ACTIVE_LEVEL AND NOT "${_SPDLOG_ACTIVE_LEVEL}" STREQUAL "")
49+
add_compile_definitions(SPDLOG_ACTIVE_LEVEL=${_SPDLOG_ACTIVE_LEVEL})
50+
else()
51+
add_compile_definitions(SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_INFO)
52+
endif()
53+
54+
set(LIVEKIT_DATA_DIR "")
55+
if(DEFINED LIVEKIT_ROOT_DIR AND EXISTS "${LIVEKIT_ROOT_DIR}/data")
56+
set(LIVEKIT_DATA_DIR "${LIVEKIT_ROOT_DIR}/data")
57+
endif()
58+
59+
include(ExampleDeps)
60+
2461
add_subdirectory(basic_room)
25-
add_subdirectory(all-cpp-examples)
62+
add_subdirectory(simple_room)
63+
add_subdirectory(simple_rpc)
64+
add_subdirectory(simple_data_stream)
65+
add_subdirectory(logging_levels_basic_usage)
66+
add_subdirectory(logging_levels_custom_sinks)
67+
add_subdirectory(hello_livekit_sender)
68+
add_subdirectory(hello_livekit_receiver)
69+
add_subdirectory(simple_joystick_sender)
70+
add_subdirectory(simple_joystick_receiver)
71+
add_subdirectory(ping_pong_ping)
72+
add_subdirectory(ping_pong_pong)

basic_room/CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2026 LiveKit, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
add_executable(basic_room
216
main.cpp
317
capture_utils.cpp
@@ -13,7 +27,6 @@ get_filename_component(_lk_lib_dir "${_lk_cmake_dir}" DIRECTORY) # .../lib
1327
target_link_directories(basic_room PRIVATE "${_lk_lib_dir}")
1428

1529

16-
1730
# Nice-to-have: copy runtime DLLs next to the exe on Windows for "run from build dir".
1831
# Only do this if your exported package provides these targets.
1932
if(WIN32)

cmake/ExampleDeps.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
include(FetchContent)
2+
3+
find_package(nlohmann_json CONFIG QUIET)
4+
if(NOT TARGET nlohmann_json::nlohmann_json)
5+
FetchContent_Declare(
6+
nlohmann_json
7+
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
8+
)
9+
FetchContent_MakeAvailable(nlohmann_json)
10+
endif()
11+
12+
find_package(SDL3 CONFIG QUIET)
13+
set(_need_sdl3_fetch TRUE)
14+
if(TARGET SDL3::SDL3)
15+
get_target_property(_sdl3_include_dirs SDL3::SDL3 INTERFACE_INCLUDE_DIRECTORIES)
16+
if(_sdl3_include_dirs)
17+
set(_need_sdl3_fetch FALSE)
18+
endif()
19+
endif()
20+
21+
if(_need_sdl3_fetch)
22+
FetchContent_Declare(
23+
SDL3
24+
URL https://github.com/libsdl-org/SDL/releases/download/release-3.2.26/SDL3-3.2.26.tar.gz
25+
)
26+
FetchContent_MakeAvailable(SDL3)
27+
endif()
28+
29+
unset(_need_sdl3_fetch)
30+
unset(_sdl3_include_dirs)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2026 LiveKit, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
add_executable(HelloLivekitReceiver
16+
main.cpp
17+
)
18+
19+
target_include_directories(HelloLivekitReceiver PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
20+
target_link_libraries(HelloLivekitReceiver PRIVATE ${LIVEKIT_CORE_TARGET} spdlog::spdlog)

hello_livekit_receiver/main.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
/// Subscribes to the sender's camera video and data track. Run
18+
/// HelloLivekitSender first; use the identity it prints, or the sender's known
19+
/// participant name.
20+
///
21+
/// Usage:
22+
/// HelloLivekitReceiver <ws-url> <receiver-token> <sender-identity>
23+
///
24+
/// Or via environment variables:
25+
/// LIVEKIT_URL, LIVEKIT_RECEIVER_TOKEN, LIVEKIT_SENDER_IDENTITY
26+
27+
#include "livekit/livekit.h"
28+
29+
#include <atomic>
30+
#include <chrono>
31+
#include <csignal>
32+
#include <cstdlib>
33+
#include <thread>
34+
35+
using namespace livekit;
36+
37+
constexpr const char *kDataTrackName = "app-data";
38+
constexpr const char *kVideoTrackName = "camera0";
39+
40+
std::atomic<bool> g_running{true};
41+
42+
void handleSignal(int) { g_running.store(false); }
43+
44+
std::string getenvOrEmpty(const char *name) {
45+
const char *v = std::getenv(name);
46+
return v ? std::string(v) : std::string{};
47+
}
48+
49+
int main(int argc, char *argv[]) {
50+
std::string url = getenvOrEmpty("LIVEKIT_URL");
51+
std::string receiver_token = getenvOrEmpty("LIVEKIT_RECEIVER_TOKEN");
52+
std::string sender_identity = getenvOrEmpty("LIVEKIT_SENDER_IDENTITY");
53+
54+
if (argc >= 4) {
55+
url = argv[1];
56+
receiver_token = argv[2];
57+
sender_identity = argv[3];
58+
}
59+
60+
if (url.empty() || receiver_token.empty() || sender_identity.empty()) {
61+
LK_LOG_ERROR("Usage: HelloLivekitReceiver <ws-url> <receiver-token> "
62+
"<sender-identity>\n"
63+
" or set LIVEKIT_URL, LIVEKIT_RECEIVER_TOKEN, "
64+
"LIVEKIT_SENDER_IDENTITY");
65+
return 1;
66+
}
67+
68+
std::signal(SIGINT, handleSignal);
69+
#ifdef SIGTERM
70+
std::signal(SIGTERM, handleSignal);
71+
#endif
72+
73+
livekit::initialize(livekit::LogLevel::Info, livekit::LogSink::kConsole);
74+
75+
auto room = std::make_unique<Room>();
76+
RoomOptions options;
77+
options.auto_subscribe = true;
78+
options.dynacast = false;
79+
80+
if (!room->Connect(url, receiver_token, options)) {
81+
LK_LOG_ERROR("[receiver] Failed to connect");
82+
livekit::shutdown();
83+
return 1;
84+
}
85+
86+
LocalParticipant *lp = room->localParticipant();
87+
assert(lp);
88+
89+
LK_LOG_INFO("[receiver] Connected as identity='{}' room='{}'; subscribing "
90+
"to sender identity='{}'",
91+
lp->identity(), room->room_info().name, sender_identity);
92+
93+
int video_frame_count = 0;
94+
room->setOnVideoFrameCallback(
95+
sender_identity, kVideoTrackName,
96+
[&video_frame_count](const VideoFrame &frame, std::int64_t timestamp_us) {
97+
const auto ts_ms =
98+
std::chrono::duration<double, std::milli>(timestamp_us).count();
99+
const int n = video_frame_count++;
100+
if (n % 10 == 0) {
101+
LK_LOG_INFO("[receiver] Video frame #{} {}x{} ts_ms={}", n,
102+
frame.width(), frame.height(), ts_ms);
103+
}
104+
});
105+
106+
int data_frame_count = 0;
107+
room->addOnDataFrameCallback(
108+
sender_identity, kDataTrackName,
109+
[&data_frame_count](const std::vector<std::uint8_t> &payload,
110+
std::optional<std::uint64_t> user_ts) {
111+
const int n = data_frame_count++;
112+
if (n % 10 == 0) {
113+
LK_LOG_INFO("[receiver] Data frame #{}", n);
114+
}
115+
});
116+
117+
LK_LOG_INFO("[receiver] Listening for video track '{}' + data track '{}'; "
118+
"Ctrl-C to exit",
119+
kVideoTrackName, kDataTrackName);
120+
121+
while (g_running.load()) {
122+
std::this_thread::sleep_for(std::chrono::milliseconds(50));
123+
}
124+
125+
LK_LOG_INFO("[receiver] Shutting down");
126+
room.reset();
127+
128+
livekit::shutdown();
129+
return 0;
130+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2026 LiveKit, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
add_executable(HelloLivekitSender
16+
main.cpp
17+
)
18+
19+
target_include_directories(HelloLivekitSender PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
20+
target_link_libraries(HelloLivekitSender PRIVATE ${LIVEKIT_CORE_TARGET} spdlog::spdlog)

0 commit comments

Comments
 (0)