Skip to content
Open
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 @@ -31,3 +31,4 @@ lib/
*.exe
livekit.log
web/
*trace.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
1 change: 1 addition & 0 deletions include/livekit/livekit.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "room.h"
#include "room_delegate.h"
#include "room_event_types.h"
#include "tracing.h"
#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 2024 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");

{
std::lock_guard<std::mutex> g(lock_);
if (connection_state_ != ConnectionState::Disconnected) {
Expand Down
90 changes: 90 additions & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,86 @@ 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
PRIVATE
livekit
GTest::gtest_main
GTest::gmock
)

target_include_directories(livekit_unit_tests
PRIVATE
${LIVEKIT_ROOT_DIR}/include
${LIVEKIT_ROOT_DIR}/src
${LIVEKIT_ROOT_DIR}/src/trace
${CMAKE_CURRENT_SOURCE_DIR}/benchmark
)

# 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
$<TARGET_FILE:livekit>
$<TARGET_FILE_DIR:livekit_unit_tests>
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<TARGET_FILE_DIR:livekit>/livekit_ffi.dll"
$<TARGET_FILE_DIR:livekit_unit_tests>
COMMENT "Copying DLLs to unit test directory"
)
elseif(APPLE)
add_custom_command(TARGET livekit_unit_tests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:livekit>
$<TARGET_FILE_DIR:livekit_unit_tests>
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<TARGET_FILE_DIR:livekit>/liblivekit_ffi.dylib"
$<TARGET_FILE_DIR:livekit_unit_tests>
COMMENT "Copying dylibs to unit test directory"
)
else()
add_custom_command(TARGET livekit_unit_tests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:livekit>
$<TARGET_FILE_DIR:livekit_unit_tests>
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<TARGET_FILE_DIR:livekit>/liblivekit_ffi.so"
$<TARGET_FILE_DIR:livekit_unit_tests>
COMMENT "Copying shared libraries to unit test directory"
)
endif()

# Register tests with CTest
gtest_discover_tests(livekit_unit_tests
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
PROPERTIES
LABELS "unit"
)
endif()

# ============================================================================
# Integration Tests
# ============================================================================
Expand All @@ -36,6 +116,7 @@ file(GLOB INTEGRATION_TEST_SOURCES
if(INTEGRATION_TEST_SOURCES)
add_executable(livekit_integration_tests
${INTEGRATION_TEST_SOURCES}
${BENCHMARK_UTILS_SOURCES}
)

target_link_libraries(livekit_integration_tests
Expand All @@ -49,6 +130,8 @@ if(INTEGRATION_TEST_SOURCES)
PRIVATE
${LIVEKIT_ROOT_DIR}/include
${LIVEKIT_ROOT_DIR}/src
${LIVEKIT_ROOT_DIR}/src/trace
${CMAKE_CURRENT_SOURCE_DIR}/benchmark
${LIVEKIT_BINARY_DIR}/generated
${Protobuf_INCLUDE_DIRS}
)
Expand Down Expand Up @@ -121,6 +204,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 @@ -134,6 +218,8 @@ if(STRESS_TEST_SOURCES)
PRIVATE
${LIVEKIT_ROOT_DIR}/include
${LIVEKIT_ROOT_DIR}/src
${LIVEKIT_ROOT_DIR}/src/trace
${CMAKE_CURRENT_SOURCE_DIR}/benchmark
)

# Copy shared libraries to test executable directory
Expand Down Expand Up @@ -188,6 +274,10 @@ add_custom_target(run_all_tests
COMMENT "Running all tests"
)

if(TARGET livekit_unit_tests)
add_dependencies(run_all_tests livekit_unit_tests)
endif()

if(TARGET livekit_integration_tests)
add_dependencies(run_all_tests livekit_integration_tests)
endif()
Expand Down
Loading
Loading