Skip to content

Avoid eager CUDA context creation during NVDEC capability detection - #1496

Closed
alan-george-lk wants to merge 2 commits into
alan/bugfix-remote-room-closurefrom
alan/bugfix-cuda-device-info
Closed

alan-george-lk wants to merge 2 commits into
alan/bugfix-remote-room-closurefrom
alan/bugfix-cuda-device-info

Conversation

@alan-george-lk

@alan-george-lk alan-george-lk commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Independent NVIDIA lifecycle change; no longer part of the room-deletion fix stack.

Summary

  • Query NVIDIA device capabilities directly when constructing the decoder factory.
  • Avoid creating a CUDA context merely to advertise NVDEC formats.
  • Preserve lazy CUDA initialization when an NVIDIA codec is actually created.

Validation

  • Release C++ SDK/test build succeeds with LLVM 21.

@devin-ai-integration devin-ai-integration Bot left a comment

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.

Devin Review found 2 potential issues.

Devin Review

RTC_LOG(LS_ERROR) << "Failed to get CUDA device.";
return;
}
supported_formats_ = SupportedNvDecoderCodecs(device);

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.

🔴 Unavailable CUDA contexts break video decoding

When cuCtxCreate fails but cuDeviceGet succeeds, supported_formats_ still advertises NVDEC. VideoDecoderFactory::Create then returns the failed NVDEC decoder without trying software decoding.

Learn more

The outer video decoder factory gives NVIDIA formats priority over its software H.264 decoder. Before this change, the NVIDIA factory initialized its CUDA context before publishing formats, so a context creation failure left its format list empty. Now device enumeration can succeed even when cuCtxCreate fails later, such as when the GPU cannot allocate another context. VideoDecoderFactory::Create immediately returns the selected factory's result, including null, without trying the software fallback.

Example: A GPU permits cuDeviceGet(0) but rejects cuCtxCreate under resource pressure. H.264 is advertised through NVDEC; Create chooses NVDEC, gets null, and never reaches its working software H.264 decoder.

Recommended fix: Keep lazy context initialization but ensure that a failed NVDEC Create allows VideoDecoderFactory::Create to try the next implementation. Also account for H.265, which has no software decoder in that factory, when deciding whether formats can be advertised.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +115 to +118
CUdevice device;
if (cuDeviceGet(&device, 0) != CUDA_SUCCESS) {
RTC_LOG(LS_ERROR) << "Failed to get CUDA device.";
return;

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.

🟡 Direct factory construction loses NVIDIA formats

Constructing NvidiaVideoDecoderFactory before CUDA initialization makes cuDeviceGet fail, leaving supported_formats_ empty. The normal wrapper calls IsSupported first, but direct callers do not.

Learn more

CUDA driver device enumeration requires cuInit(0). Previously the factory's constructor called CudaContext::Initialize, which called cuInit before cuDeviceGet. The wrapper VideoDecoderFactory still calls IsSupported before construction, so it initializes CUDA first; this does not protect direct construction of the public NVIDIA factory.

Example: A caller creates NvidiaVideoDecoderFactory on a fresh process with an available GPU and libnvcuvid.so.1. The constructor's device query fails before initialization and GetSupportedFormats() returns an empty vector, unlike before this change.

Recommended fix: Initialize the CUDA driver with cuInit(0) before querying the device in this constructor. Do not create a CUDA context until decoder creation.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@alan-george-lk
alan-george-lk added this pull request to stack #1498 October 5, 2026 19:28
@alan-george-lk
alan-george-lk force-pushed the alan/bugfix-cuda-device-info branch from 0a4acda to 6b1b88b Compare October 5, 2026 19:56
alan-george-lk and others added 2 commits October 5, 2026 12:56
Co-authored-by: Cursor <cursoragent@cursor.com>
@alan-george-lk

Copy link
Copy Markdown
Contributor Author

Closing to recreate this branch as an independent PR against main; GitHub does not allow changing the base of a PR already registered in a stack.

@alan-george-lk
alan-george-lk removed this pull request from stack #1498 October 5, 2026 20:05
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Changeset ✓

This PR includes a changeset covering all affected packages:

Package Bump
libwebrtc patch
livekit patch
livekit-capture patch
livekit-ffi patch
webrtc-sys patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant