Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ lib/
*.exe
livekit.log
web/
*trace.json
compile_commands.json
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ add_library(livekit SHARED
src/remote_video_track.cpp
src/video_utils.cpp
src/video_utils.h
# Tracing support (Chrome trace format)
src/trace/event_tracer.cpp
src/trace/event_tracer.h
src/trace/event_tracer_internal.h
src/trace/trace_event.h
src/trace/tracing.cpp
)
if(WIN32)
set_target_properties(livekit PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
Expand All @@ -373,6 +379,7 @@ target_include_directories(livekit
$<INSTALL_INTERFACE:include>
PRIVATE
${LIVEKIT_ROOT_DIR}/src
${LIVEKIT_ROOT_DIR}/src/trace
${RUST_ROOT}/livekit-ffi/include
${PROTO_BINARY_DIR}
)
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,42 @@ and a **ROS2 bridge** that maps `LogLevel` to `RCLCPP_*` macros.

---

## Tracing

The SDK includes built-in support for [Chromium tracing](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool/), allowing you to capture detailed performance traces for debugging and optimization.

### Basic Usage

```cpp
#include <livekit/livekit.h>

// Start tracing to a file
livekit::startTracing("trace.json");

// ... run your application ...

// Stop tracing and flush to file
livekit::stopTracing();
```

### Filtering by Category

You can optionally filter which categories to trace:

```cpp
// Trace only specific categories (supports wildcards)
livekit::startTracing("trace.json", {"livekit.*", "webrtc.*"});
```

### Viewing Traces

Open the generated trace file in one of these viewers:

1. **Chrome**: Navigate to `chrome://tracing` and click "Load" to open the trace file
2. **Perfetto**: Go to https://ui.perfetto.dev and drag-drop your trace file

---

## 🧪 Integration & Stress Tests

The SDK includes integration and stress tests using Google Test (gtest).
Expand Down
1 change: 1 addition & 0 deletions include/livekit/livekit.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "room.h"
#include "room_delegate.h"
#include "room_event_types.h"
#include "tracing.h"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the reason for including this here if it doesnt get used?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The APIs at tracing.h is public:
bool startTracing(const std::string &trace_file_path,
const std::vectorstd::string &categories = {});
void stopTracing();
bool isTracingEnabled();

Developers can start / stop tracing based on their needs, by default it is stopped.

#include "track_publication.h"
#include "video_frame.h"
#include "video_source.h"
Expand Down
56 changes: 56 additions & 0 deletions include/livekit/tracing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2026 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <string>
#include <vector>

namespace livekit {

/**
* Start tracing and write events to a file.
*
* Events are written to the file asynchronously by a background thread.
* The file is written in Chrome trace format (JSON), viewable in:
* - Chrome: chrome://tracing
* - Perfetto: https://ui.perfetto.dev
*
* @param trace_file_path Path to the output trace file (e.g., "trace.json")
* @param categories Categories to enable (empty = all categories).
* Supports wildcards: "livekit.*" matches all livekit
* categories.
* @return true if tracing was started, false if already running or file error
*/
bool startTracing(const std::string &trace_file_path,
const std::vector<std::string> &categories = {});

/**
* Stop tracing and flush remaining events to file.
*
* This blocks until all pending events are written and the file is closed.
* After stopping, the trace file is complete and ready for analysis.
*/
void stopTracing();

/**
* Check if tracing is currently active.
*
* @return true if tracing is running
*/
bool isTracingEnabled();

} // namespace livekit
3 changes: 3 additions & 0 deletions src/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "livekit_ffi.h"
#include "room.pb.h"
#include "room_proto_converter.h"
#include "trace/trace_event.h"
#include "track.pb.h"
#include "track_proto_converter.h"
#include <functional>
Expand Down Expand Up @@ -107,6 +108,8 @@ void Room::setDelegate(RoomDelegate *delegate) {

bool Room::Connect(const std::string &url, const std::string &token,
const RoomOptions &options) {
TRACE_EVENT0("livekit", "Room::Connect");
Comment thread
xianshijing-lk marked this conversation as resolved.

{
std::lock_guard<std::mutex> g(lock_);
if (connection_state_ != ConnectionState::Disconnected) {
Expand Down
19 changes: 19 additions & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,28 @@ FetchContent_MakeAvailable(googletest)
enable_testing()
include(GoogleTest)

# ============================================================================
# Benchmark Utilities (shared by all test types)
# ============================================================================

set(BENCHMARK_UTILS_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/benchmark/benchmark_utils.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/benchmark/benchmark_utils.h"
)

# ============================================================================
# Unit Tests
# ============================================================================

# Collect all unit test sources
file(GLOB UNIT_TEST_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/unit/*.cpp"
)

if(UNIT_TEST_SOURCES)
add_executable(livekit_unit_tests
${UNIT_TEST_SOURCES}
${BENCHMARK_UTILS_SOURCES}
)

target_link_libraries(livekit_unit_tests
Expand All @@ -49,6 +60,8 @@ if(UNIT_TEST_SOURCES)
PRIVATE
${LIVEKIT_ROOT_DIR}/include
${LIVEKIT_ROOT_DIR}/src
${LIVEKIT_ROOT_DIR}/src/trace
${CMAKE_CURRENT_SOURCE_DIR}/benchmark
)

target_compile_definitions(livekit_unit_tests
Expand All @@ -59,6 +72,7 @@ if(UNIT_TEST_SOURCES)
$<$<PLATFORM_ID:Windows>:_USE_MATH_DEFINES>
)

# Copy shared libraries to test executable directory
if(WIN32)
add_custom_command(TARGET livekit_unit_tests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
Expand Down Expand Up @@ -91,6 +105,7 @@ if(UNIT_TEST_SOURCES)
)
endif()

# Register tests with CTest
gtest_discover_tests(livekit_unit_tests
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
DISCOVERY_MODE PRE_TEST
Expand All @@ -111,6 +126,7 @@ file(GLOB INTEGRATION_TEST_SOURCES
if(INTEGRATION_TEST_SOURCES)
add_executable(livekit_integration_tests
${INTEGRATION_TEST_SOURCES}
${BENCHMARK_UTILS_SOURCES}
)

# On Windows, protobuf default-instance symbols (constinit globals) are not
Expand All @@ -133,6 +149,7 @@ if(INTEGRATION_TEST_SOURCES)
PRIVATE
${LIVEKIT_ROOT_DIR}/include
${LIVEKIT_ROOT_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/benchmark
${LIVEKIT_BINARY_DIR}/generated
${Protobuf_INCLUDE_DIRS}
)
Expand Down Expand Up @@ -207,6 +224,7 @@ file(GLOB STRESS_TEST_SOURCES
if(STRESS_TEST_SOURCES)
add_executable(livekit_stress_tests
${STRESS_TEST_SOURCES}
${BENCHMARK_UTILS_SOURCES}
)

target_link_libraries(livekit_stress_tests
Expand All @@ -220,6 +238,7 @@ if(STRESS_TEST_SOURCES)
PRIVATE
${LIVEKIT_ROOT_DIR}/include
${LIVEKIT_ROOT_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/benchmark
)

target_compile_definitions(livekit_stress_tests
Expand Down
Loading
Loading