Skip to content

Commit d74f35b

Browse files
Merge pull request #1 from livekit-examples/sxian/BOT-182/add_cpp_examples_to_use_cpp_release
added basic_room
2 parents 164d69d + 95fad02 commit d74f35b

9 files changed

Lines changed: 943 additions & 1 deletion

File tree

.github/workflows/builds.yml

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
name: Build examples against latest LiveKit SDK (via CMake)
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
branches: ["main"]
8+
workflow_dispatch:
9+
10+
env:
11+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12+
13+
jobs:
14+
build:
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
include:
19+
- os: ubuntu-latest
20+
name: linux-x64
21+
- os: macos-latest
22+
name: macos-arm64
23+
- os: windows-latest
24+
name: windows-x64
25+
26+
name: Build (${{ matrix.name }})
27+
runs-on: ${{ matrix.os }}
28+
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v4
32+
33+
# ---------- deps ----------
34+
- name: Install deps (Ubuntu)
35+
if: runner.os == 'Linux'
36+
shell: bash
37+
run: |
38+
set -eux
39+
sudo apt-get update
40+
sudo apt-get install -y \
41+
cmake ninja-build pkg-config \
42+
protobuf-compiler libprotobuf-dev \
43+
libssl-dev \
44+
curl
45+
46+
- name: Install deps (macOS)
47+
if: runner.os == 'macOS'
48+
shell: bash
49+
run: |
50+
set -eux
51+
brew update
52+
brew install cmake ninja protobuf
53+
54+
- name: Install deps (Windows)
55+
if: runner.os == 'Windows'
56+
shell: pwsh
57+
run: |
58+
choco install -y cmake --installargs 'ADD_CMAKE_TO_PATH=System'
59+
choco install -y ninja
60+
61+
- name: Setup MSVC (Windows)
62+
if: runner.os == 'Windows'
63+
uses: ilammy/msvc-dev-cmd@v1
64+
with:
65+
arch: x64
66+
67+
# ---------- configure + build ----------
68+
- name: Configure (Unix)
69+
if: runner.os != 'Windows'
70+
shell: bash
71+
env:
72+
GITHUB_TOKEN: ${{ github.token }}
73+
run: |
74+
set -eux
75+
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
76+
77+
- name: Build (Unix)
78+
if: runner.os != 'Windows'
79+
shell: bash
80+
run: |
81+
set -eux
82+
cmake --build build --config Release
83+
84+
- name: Configure (Windows)
85+
if: runner.os == 'Windows'
86+
shell: pwsh
87+
run: |
88+
cmake -S . -B build -G Ninja `
89+
-DCMAKE_BUILD_TYPE=Release `
90+
-DCMAKE_C_COMPILER=cl `
91+
-DCMAKE_CXX_COMPILER=cl
92+
93+
- name: Build (Windows)
94+
if: runner.os == 'Windows'
95+
shell: pwsh
96+
run: |
97+
cmake --build build --config Release
98+
99+
# ---------- smoke test ----------
100+
- name: Smoke test (Linux/macOS)
101+
if: runner.os != 'Windows'
102+
shell: bash
103+
run: |
104+
set -euo pipefail
105+
106+
sdk_root="$(ls -d build/_deps/livekit-sdk/* | head -n 1)"
107+
echo "SDK root: ${sdk_root}"
108+
ls -la "${sdk_root}/lib" || true
109+
# Locate executable (it may not be directly under build/)
110+
exe="$(find build -type f -name basic_room -perm -111 | head -n 1)"
111+
if [[ -z "${exe}" ]]; then
112+
echo "basic_room executable not found under build/"
113+
find build -maxdepth 3 -type f -print
114+
exit 1
115+
fi
116+
echo "Running: ${exe} --self-test"
117+
if [[ "$RUNNER_OS" == "Linux" ]]; then
118+
export LD_LIBRARY_PATH="${sdk_root}/lib:${LD_LIBRARY_PATH:-}"
119+
else
120+
export DYLD_LIBRARY_PATH="${sdk_root}/lib:${DYLD_LIBRARY_PATH:-}"
121+
fi
122+
"${exe}" --self-test
123+
124+
125+
- name: Smoke test (Windows)
126+
if: runner.os == 'Windows'
127+
shell: pwsh
128+
run: |
129+
# Locate SDK root
130+
$sdkRoot = Get-ChildItem -Directory "build/_deps/livekit-sdk" | Select-Object -First 1
131+
if (-not $sdkRoot) {
132+
throw "SDK root not found under build/_deps/livekit-sdk"
133+
}
134+
Write-Host "SDK root: $($sdkRoot.FullName)"
135+
# Make sure DLLs are found at runtime
136+
$env:PATH = "$($sdkRoot.FullName)\bin;$($sdkRoot.FullName)\lib;$env:PATH"
137+
# Locate the built executable
138+
$exe = Get-ChildItem -Recurse build -Filter basic_room.exe | Select-Object -First 1
139+
if (-not $exe) {
140+
throw "basic_room.exe not found in build directory"
141+
}
142+
Write-Host "Running $($exe.FullName) --help"
143+
# Try to execute it. We only care that it launches.
144+
$out = & $exe.FullName --self-test 2>&1
145+
$code = $LASTEXITCODE
146+
if ($code -ne 0) {
147+
Write-Host $out
148+
throw "basic_room.exe --self-test failed with exit code $code"
149+
}
150+
Write-Host $out
151+
152+
# ---------- upload build output ----------
153+
- name: Upload binary
154+
uses: actions/upload-artifact@v4
155+
with:
156+
name: basic_room-${{ matrix.name }}
157+
path: |
158+
build/basic_room*
159+
build/basic_room.exe
160+
retention-days: 7

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.cache/
2+
CMakeFiles/
3+
CMakeCache.txt
4+
.DS_Store
5+
Makefile
6+
cmake_install.cmake
7+
out
8+
build/
9+
build-debug/
10+
build-release/
11+
vcpkg_installed/
12+
received_green.avif
13+
docs/*.bak
14+
docs/html/
15+
docs/latex/
16+
.vs/
17+
.vscode/
18+
# Compiled output
19+
bin/
20+
lib/
21+
*.lib
22+
*.a
23+
*.so
24+
*.dylib
25+
*.dll
26+
*.exe

CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(livekit_cpp_example_collection LANGUAGES CXX)
3+
set(CMAKE_CXX_STANDARD 17)
4+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5+
6+
# Make "include(LiveKitSDK)" search in ./cmake
7+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
8+
9+
set(LIVEKIT_SDK_VERSION "latest" CACHE STRING "LiveKit C++ SDK version (e.g. 0.2.0 or latest)")
10+
11+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
12+
include(LiveKitSDK)
13+
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+
)
19+
20+
find_package(LiveKit CONFIG REQUIRED)
21+
add_subdirectory(basic_room)

README.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,73 @@
11
# cpp-example-collection
2-
A collection of small examples for the LiveKit C++ SDK: https://github.com/livekit/client-sdk-cpp
2+
This repository contains a collection of small, self-contained examples for the
3+
[LiveKit C++ SDK](https://github.com/livekit/client-sdk-cpp).
4+
5+
The goal of these examples is to demonstrate common usage patterns of the
6+
LiveKit C++ SDK (connecting to a room, publishing tracks, RPC, data streams,
7+
etc.) without requiring users to build the SDK from source.
8+
9+
10+
## How the SDK is provided
11+
12+
These examples **automatically download a prebuilt LiveKit C++ SDK release**
13+
from GitHub at CMake configure time.
14+
15+
This is handled by the cmake helper: [LiveKitSDK.cmake ](https://github.com/livekit-examples/cpp-example-collection/cmake/LiveKitSDK.cmake)
16+
17+
## Selecting a LiveKit SDK version
18+
19+
By default, the examples download the **latest released** LiveKit C++ SDK.
20+
21+
You can pin a specific SDK version using the `LIVEKIT_SDK_VERSION` CMake option.
22+
23+
### Examples
24+
25+
Use the latest release:
26+
```bash
27+
cmake -S . -B build
28+
# Or use a specific version (recommended for reproducibility):
29+
cmake -S . -B build -DLIVEKIT_SDK_VERSION=0.2.0
30+
```
31+
32+
Reconfigure to change versions:
33+
```bash
34+
rm -rf build
35+
cmake -S . -B build -DLIVEKIT_SDK_VERSION=0.3.1
36+
```
37+
38+
39+
### Building the examples
40+
#### macOS / Linux
41+
```bash
42+
cmake -S . -B build # add -DLIVEKIT_SDK_VERSION=0.2.0 if using a specific version
43+
cmake --build build
44+
```
45+
46+
#### Windows (Visual Studio generator)
47+
```powershell
48+
cmake -S . -B build # add -DLIVEKIT_SDK_VERSION=0.2.0 if using a specific version
49+
cmake --build build --config Release
50+
```
51+
52+
The Livekit Release SDK is downloaded into **build/_deps/livekit-sdk/**
53+
54+
### Running the examples
55+
56+
After building, example binaries are located under:
57+
```bash
58+
build/<example-name>/
59+
```
60+
61+
For example:
62+
```bash
63+
./build/basic_room/basic_room --url <ws-url> --token <token>
64+
```
65+
66+
### Supported platforms
67+
68+
Prebuilt SDKs are downloaded automatically for:
69+
* Windows: x64
70+
* macOS: x64, arm64 (Apple Silicon)
71+
* Linux: x64
72+
73+
If no matching SDK is available for your platform, CMake configuration will fail with a clear error.

basic_room/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
add_executable(basic_room
2+
main.cpp
3+
capture_utils.cpp
4+
capture_utils.h
5+
)
6+
7+
target_include_directories(basic_room PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
8+
target_link_libraries(basic_room PRIVATE LiveKit::livekit)
9+
10+
# Make -llivekit_ffi resolvable
11+
get_filename_component(_lk_cmake_dir "${LiveKit_DIR}" DIRECTORY) # .../lib/cmake
12+
get_filename_component(_lk_lib_dir "${_lk_cmake_dir}" DIRECTORY) # .../lib
13+
target_link_directories(basic_room PRIVATE "${_lk_lib_dir}")
14+
15+
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.
19+
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+
29+
# If you also export protobuf/abseil runtime targets, copy them too (optional).
30+
if(TARGET protobuf::libprotobuf)
31+
add_custom_command(TARGET basic_room POST_BUILD
32+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
33+
"$<TARGET_FILE:protobuf::libprotobuf>"
34+
"$<TARGET_FILE_DIR:basic_room>"
35+
)
36+
endif()
37+
38+
if(TARGET absl::abseil_dll)
39+
add_custom_command(TARGET basic_room POST_BUILD
40+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
41+
"$<TARGET_FILE:absl::abseil_dll>"
42+
"$<TARGET_FILE_DIR:basic_room>"
43+
)
44+
endif()
45+
endif()

0 commit comments

Comments
 (0)