Skip to content

Commit a747afe

Browse files
Migrate all examples from client-sdk-cpp
1 parent a74ca36 commit a747afe

57 files changed

Lines changed: 5627 additions & 34 deletions

Some content is hidden

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

.github/workflows/builds.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ jobs:
4040
sudo apt-get install -y \
4141
cmake ninja-build pkg-config \
4242
protobuf-compiler libprotobuf-dev \
43+
libasound2-dev libpulse-dev \
44+
libx11-dev libxext-dev libxrandr-dev libxcursor-dev libxfixes-dev \
45+
libxi-dev libxss-dev libxtst-dev libxkbcommon-dev \
46+
libdrm-dev libgbm-dev libgl1-mesa-dev libgles2-mesa-dev \
47+
libegl1-mesa-dev libdbus-1-dev libibus-1.0-dev libudev-dev \
48+
libpipewire-0.3-dev libwayland-dev libdecor-0-dev liburing-dev \
4349
libssl-dev \
50+
libspdlog-dev \
4451
curl
4552
4653
- name: Install deps (macOS)
@@ -50,6 +57,7 @@ jobs:
5057
set -eux
5158
brew update
5259
brew install cmake ninja protobuf
60+
brew install spdlog
5361
5462
- name: Install deps (Windows)
5563
if: runner.os == 'Windows'

CMakeLists.txt

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,95 @@
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")
823

924
set(LIVEKIT_SDK_VERSION "latest" CACHE STRING "LiveKit C++ SDK version (e.g. 0.2.0 or latest)")
25+
set(LIVEKIT_LOCAL_SDK_DIR "" CACHE PATH "Path to a local LiveKit SDK install prefix (skips download)")
1026

11-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
12-
include(LiveKitSDK)
27+
if(LIVEKIT_LOCAL_SDK_DIR)
28+
message(STATUS "Using local LiveKit SDK: ${LIVEKIT_LOCAL_SDK_DIR}")
29+
list(PREPEND CMAKE_PREFIX_PATH "${LIVEKIT_LOCAL_SDK_DIR}")
30+
else()
31+
include(LiveKitSDK)
32+
livekit_sdk_setup(
33+
VERSION "${LIVEKIT_SDK_VERSION}"
34+
SDK_DIR "${CMAKE_BINARY_DIR}/_deps/livekit-sdk"
35+
GITHUB_TOKEN "$ENV{GITHUB_TOKEN}"
36+
)
37+
endif()
38+
find_package(LiveKit CONFIG REQUIRED)
1339

14-
livekit_sdk_setup(
15-
VERSION "${LIVEKIT_SDK_VERSION}"
16-
SDK_DIR "${CMAKE_BINARY_DIR}/_deps/livekit-sdk"
17-
GITHUB_TOKEN "$ENV{GITHUB_TOKEN}"
18-
)
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+
set(LIVEKIT_DATA_DIR "")
49+
if(DEFINED LIVEKIT_ROOT_DIR AND EXISTS "${LIVEKIT_ROOT_DIR}/data")
50+
set(LIVEKIT_DATA_DIR "${LIVEKIT_ROOT_DIR}/data")
51+
endif()
52+
53+
function(livekit_copy_windows_runtime_dlls target_name)
54+
if(NOT WIN32)
55+
return()
56+
endif()
57+
58+
get_filename_component(_lk_cmake_parent "${LiveKit_DIR}" DIRECTORY)
59+
get_filename_component(_lk_lib_dir "${_lk_cmake_parent}" DIRECTORY)
60+
get_filename_component(_lk_prefix "${_lk_lib_dir}" DIRECTORY)
61+
set(_lk_bin_dir "${_lk_prefix}/bin")
62+
63+
foreach(_lk_runtime_dll IN ITEMS livekit.dll livekit_ffi.dll)
64+
if(EXISTS "${_lk_bin_dir}/${_lk_runtime_dll}")
65+
add_custom_command(TARGET ${target_name} POST_BUILD
66+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
67+
"${_lk_bin_dir}/${_lk_runtime_dll}"
68+
"$<TARGET_FILE_DIR:${target_name}>"
69+
)
70+
endif()
71+
endforeach()
72+
73+
if(TARGET SDL3::SDL3)
74+
add_custom_command(TARGET ${target_name} POST_BUILD
75+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
76+
"$<TARGET_FILE:SDL3::SDL3>"
77+
"$<TARGET_FILE_DIR:${target_name}>"
78+
)
79+
endif()
80+
endfunction()
81+
82+
include(ExampleDeps)
1983

20-
find_package(LiveKit CONFIG REQUIRED)
2184
add_subdirectory(basic_room)
85+
add_subdirectory(simple_room)
86+
add_subdirectory(simple_rpc)
87+
add_subdirectory(simple_data_stream)
88+
add_subdirectory(logging_levels_basic_usage)
89+
add_subdirectory(logging_levels_custom_sinks)
90+
add_subdirectory(hello_livekit_sender)
91+
add_subdirectory(hello_livekit_receiver)
92+
add_subdirectory(simple_joystick_sender)
93+
add_subdirectory(simple_joystick_receiver)
94+
add_subdirectory(ping_pong_ping)
95+
add_subdirectory(ping_pong_pong)

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ rm -rf build
3535
cmake -S . -B build -DLIVEKIT_SDK_VERSION=0.3.1
3636
```
3737

38+
Build against a local SDK:
39+
```bash
40+
rm -rf build
41+
# install the SDK into $HOME/livekit-sdk-install (or any other directory)
42+
cmake --install <sdk-build-dir> --prefix $HOME/livekit-sdk-install
43+
44+
# build the examples against the local SDK
45+
cmake -S . -B build -DLIVEKIT_LOCAL_SDK_DIR=$HOME/livekit-sdk-install
46+
```
47+
3848

3949
### Building the examples
4050
#### macOS / Linux

basic_room/CMakeLists.txt

Lines changed: 20 additions & 13 deletions
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,19 +27,10 @@ get_filename_component(_lk_lib_dir "${_lk_cmake_dir}" DIRECTORY) # .../lib
1327
target_link_directories(basic_room PRIVATE "${_lk_lib_dir}")
1428

1529

16-
17-
# Nice-to-have: copy runtime DLLs next to the exe on Windows for "run from build dir".
18-
# Only do this if your exported package provides these targets.
30+
# Nice-to-have: copy extra runtime DLLs next to the exe on Windows for "run from build dir".
31+
# The common LiveKit runtime copies are handled by livekit_copy_windows_runtime_dlls()
32+
# in this target's directory below.
1933
if(WIN32)
20-
# livekit_ffi.dll
21-
if(TARGET LiveKit::livekit_ffi)
22-
add_custom_command(TARGET basic_room POST_BUILD
23-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
24-
"$<TARGET_FILE:LiveKit::livekit_ffi>"
25-
"$<TARGET_FILE_DIR:basic_room>"
26-
)
27-
endif()
28-
2934
# If you also export protobuf/abseil runtime targets, copy them too (optional).
3035
if(TARGET protobuf::libprotobuf)
3136
add_custom_command(TARGET basic_room POST_BUILD
@@ -40,6 +45,8 @@ if(WIN32)
4045
COMMAND ${CMAKE_COMMAND} -E copy_if_different
4146
"$<TARGET_FILE:absl::abseil_dll>"
4247
"$<TARGET_FILE_DIR:basic_room>"
43-
)
48+
)
4449
endif()
4550
endif()
51+
52+
livekit_copy_windows_runtime_dlls(basic_room)

basic_room/main.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ void printUsage(const char *prog) {
4141
<< " LIVEKIT_URL, LIVEKIT_TOKEN\n";
4242
}
4343

44-
bool parseArgs(int argc, char *argv[], std::string &url, std::string &token, bool &self_test) {
44+
bool parseArgs(int argc, char *argv[], std::string &url, std::string &token,
45+
bool &self_test) {
4546
for (int i = 1; i < argc; ++i) {
4647
const std::string a = argv[i];
4748
if (a == "-h" || a == "--help")
@@ -85,14 +86,9 @@ bool parseArgs(int argc, char *argv[], std::string &url, std::string &token, boo
8586
}
8687

8788
void print_livekit_version() {
88-
std::cout
89-
<< "LiveKit version: "
90-
<< LIVEKIT_BUILD_VERSION_FULL
91-
<< " (" << LIVEKIT_BUILD_FLAVOR
92-
<< ", commit " << LIVEKIT_BUILD_COMMIT
93-
<< ", built " << LIVEKIT_BUILD_DATE
94-
<< ")"
95-
<< std::endl;
89+
std::cout << "LiveKit version: " << LIVEKIT_BUILD_VERSION_FULL << " ("
90+
<< LIVEKIT_BUILD_FLAVOR << ", commit " << LIVEKIT_BUILD_COMMIT
91+
<< ", built " << LIVEKIT_BUILD_DATE << ")" << std::endl;
9692
}
9793

9894
} // namespace
@@ -106,7 +102,7 @@ int main(int argc, char *argv[]) {
106102
return 1;
107103
}
108104
if (self_test) {
109-
livekit::initialize(livekit::LogSink::kConsole);
105+
livekit::initialize(livekit::LogLevel::Info, livekit::LogSink::kConsole);
110106
livekit::shutdown();
111107
std::cout << "self-test ok" << std::endl;
112108
return 0;
@@ -115,7 +111,7 @@ int main(int argc, char *argv[]) {
115111
std::signal(SIGINT, handleSignal);
116112

117113
// Init LiveKit
118-
livekit::initialize(livekit::LogSink::kConsole);
114+
livekit::initialize(livekit::LogLevel::Info, livekit::LogSink::kConsole);
119115

120116
auto room = std::make_unique<Room>();
121117

@@ -145,7 +141,8 @@ int main(int argc, char *argv[]) {
145141

146142
std::shared_ptr<LocalTrackPublication> audioPub;
147143
try {
148-
audioPub = room->localParticipant()->publishTrack(audioTrack, audioOpts);
144+
room->localParticipant()->publishTrack(audioTrack, audioOpts);
145+
audioPub = audioTrack->publication();
149146
std::cout << "Published audio: sid=" << audioPub->sid() << "\n";
150147
} catch (const std::exception &e) {
151148
std::cerr << "Failed to publish audio: " << e.what() << "\n";
@@ -163,7 +160,8 @@ int main(int argc, char *argv[]) {
163160

164161
std::shared_ptr<LocalTrackPublication> videoPub;
165162
try {
166-
videoPub = room->localParticipant()->publishTrack(videoTrack, videoOpts);
163+
room->localParticipant()->publishTrack(videoTrack, videoOpts);
164+
videoPub = videoTrack->publication();
167165
std::cout << "Published video: sid=" << videoPub->sid() << "\n";
168166
} catch (const std::exception &e) {
169167
std::cerr << "Failed to publish video: " << e.what() << "\n";

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
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})
21+
22+
livekit_copy_windows_runtime_dlls(HelloLivekitReceiver)

0 commit comments

Comments
 (0)