bitter-answer-37944
09/20/2025, 1:41 AMbool NvidiaVideoEncoderFactory::IsSupported() {
// Check if the CUDA context can be initialized.
auto cu_context = std::make_unique<livekit::CudaContext>();
if (!cu_context->Initialize()) {
std::cout << "Failed to initialize CUDA context." << std::endl;
return false;
}
std::cout << "Nvidia Encoder is supported." << std::endl;
return true;
}
It's exactly when that "Nvidia Encoder is supported" line that gets printed, that all of my cuda related code crashes afterwards.
And I am pretty sure it's livekit's fault doing something messy with the CUDA contexts, since if I remove livekit completely or perform all of my cuda calls before any of livekit causes a call to this code on the ffi server, it works perfectly.
Another workaround that I found and that confirms this issue is "re-claiming" the context with
dev = cuda.Device(0)
ctx = dev.retain_primary_context()
ctx.push()
However this breaks the code in other areas and also is not ideal to switch context after every call from livekit's encoder.
So my final question (that I assume might be a workaround for this), is it possible to make Livekit not be so greedy with the CUDA context, or even easier to start the background ffi server without CUDA access?tall-airline-16756
09/20/2025, 1:41 AMtall-airline-16756
09/20/2025, 1:41 AMcuCtxPushCurrent and cuCtxPopCurrent to manage the context switching more smoothly.
• Consider initializing your own CUDA context using cuCtxCreate and then clean it up with cuCtxDestroy to avoid any surprises from implicit contexts.
• If you're working with multiple threads, using cuvidCtxLockCreate along with a floating context can help make things thread-safe.
• And if you’d prefer to stop LiveKit from initializing CUDA altogether, you might need to tweak the LiveKit code to skip CUDA initialization or start the background FFI server without CUDA access.
Please feel free to reach out if you’d like me to walk you through any of these steps or if you have any other questions—I’m here to help!
Sources: webrtc-sys/src/nvidia/implib/libcuda.so.tramp.S