Skip to content

Commit be5304c

Browse files
authored
Fix/windows desktop capture (#263)
* fix: define RTC_ENABLE_WIN_WGC for the JNI wrapper (Windows ABI mismatch) Windows screen/window capture has been completely broken since the branch-heads/7339 -> 7977 (m140 -> m152) bump: zero frames captured, and the "screens" enumeration returned application windows. Root cause is a DesktopCaptureOptions ABI mismatch between webrtc.lib and the JNI wrapper. gn builds the lib with its default rtc_enable_win_wgc=true (webrtc.gni: rtc_enable_win_wgc = is_win), so the lib-side class contains the RTC_ENABLE_WIN_WGC-guarded members; the wrapper compiled without the define sees a smaller class (64 vs 80 bytes on 7977) with different member offsets for everything after the WGC block. DesktopCaptureOptions::CreateDefault() and the copy constructor are defined out-of-line in the lib, so `auto options = CreateDefault()` in the wrapper writes an 80-byte object into a 64-byte stack slot: 16 bytes of adjacent stack are zero-smashed on every capturer construction. That deterministic smash is what zeroed jni::DesktopCapturer's spilled screenCapturer argument (every ScreenCapturer.initialize() logged screenCapturer=0 and constructed the window capturer instead - the "Screens tab lists applications" bug), and general field-offset skew after the WGC block broke frame delivery entirely. The mismatch already existed on 7339, but the guarded block was then just 5 bools at the tail-adjacent position: an 8-byte zero overflow and skewed reads of cosmetic flags (disable_effects_), which happened to be survivable - which is why 0.14.0 worked. 7977 grew the block (7 bools + LUID) and added std::optional<Environment> env_ after it, making the corruption fatal. Fix: define RTC_ENABLE_WIN_WGC for the wrapper so both sides agree on the layout. This is the same ABI bug class already fixed on Linux with WEBRTC_USE_X11 (#236) and WEBRTC_USE_PIPEWIRE (#261). All allow_wgc_* options default to false, so capturer selection behavior is unchanged (DirectX/GDI as before). * fix: pass full frame height as libyuv src_height (broken window capture) With the RTC_ENABLE_WIN_WGC ABI fix in place, screen capture works but window (application) capture still delivered no frames: neither thumbnails (DesktopCaptureCallback) nor the live share (VideoTrackDesktopSource). Both files feed libyuv::ConvertToI420 with the cropped output height (i420Buffer/buffer->height() == crop_h) in the src_height slot. libyuv bounds-checks crop_y + crop_height <= src_height, so the conversion fails with -1 whenever crop_y > 0. The Windows border-crop branch sets crop_y = -top_left().y() for frames that start above the screen origin, which is the case for every MAXIMIZED window (its frame sits at -SM_CXPADDEDBORDER). Screen frames never enter that branch at all (exact stride, `fullscreen == true`), which is why only window capture appeared broken. Confirmed at runtime on a diagnostic build (Komm, Windows 11): size=1928x1043 crop_y=7 crop_w=1920 crop_h=1032 Failed to convert desktop frame to I420, libyuv result=-1 Also propagate non-SUCCESS capture results to the Java callback (with a null frame) instead of silently returning, so callers don't have to burn a timeout to notice a failed capture.
1 parent bce0b2e commit be5304c

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎webrtc-jni/src/main/cpp/dependencies/webrtc/CMakeLists.txt‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,15 @@ elseif(LINUX)
228228
target_compile_definitions(${PROJECT_NAME} PUBLIC WEBRTC_LINUX WEBRTC_POSIX WEBRTC_USE_H264 WEBRTC_USE_X11 WEBRTC_USE_PIPEWIRE WEBRTC_USE_GIO)
229229
target_link_libraries(${PROJECT_NAME} X11 Xext Xfixes Xdamage Xtst Xrandr Xcomposite dbus-1 ${GIO_LIBRARIES} ${GBM_LIBRARIES} ${DRM_LIBRARIES})
230230
elseif(WIN32)
231-
target_compile_definitions(${PROJECT_NAME} PUBLIC WEBRTC_WIN WEBRTC_USE_H264 NOMINMAX WIN32_LEAN_AND_MEAN NDEBUG)
231+
# RTC_ENABLE_WIN_WGC must match the gn build: webrtc.lib is compiled with gn's
232+
# default rtc_enable_win_wgc=true (webrtc.gni: "rtc_enable_win_wgc = is_win"),
233+
# and DesktopCaptureOptions has members guarded by that define. Compiling the
234+
# JNI wrapper without it changes the class layout and size (64 vs 80 bytes as
235+
# of branch-heads/7977) — the out-of-line CreateDefault()/copy constructor in
236+
# the lib then overflow the wrapper's smaller stack slot and every member
237+
# after the guarded block is read at wrong offsets, silently breaking desktop
238+
# capture (same ABI bug WEBRTC_USE_X11 fixed on Linux).
239+
target_compile_definitions(${PROJECT_NAME} PUBLIC WEBRTC_WIN WEBRTC_USE_H264 NOMINMAX WIN32_LEAN_AND_MEAN NDEBUG RTC_ENABLE_WIN_WGC)
232240
target_link_libraries(${PROJECT_NAME} D3D11 DXGI user32 gdi32 iphlpapi dmoguids msdmo secur32 strmiids winmm wmcodecdspuuid ws2_32)
233241
endif()
234242

‎webrtc-jni/src/main/cpp/src/media/video/VideoTrackDesktopSource.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,13 @@ namespace jni
194194
buffer->MutableDataU(), buffer->StrideU(),
195195
buffer->MutableDataV(), buffer->StrideV(),
196196
crop_x, crop_y,
197-
frame->stride() / webrtc::DesktopFrame::kBytesPerPixel, buffer->height(), crop_w, crop_h,
197+
// (src_width, src_height) must describe the FULL source frame, not the cropped
198+
// output. Passing buffer->height() (== crop_h) here made libyuv's internal
199+
// bounds check (crop_y + crop_height <= src_height) fail with -1 whenever
200+
// crop_y > 0 — i.e. for every maximized window, whose frame sits at
201+
// top_left().y() == -border. Screen frames never hit this branch (exact stride,
202+
// fullscreen == true), which is why only window capture appeared broken.
203+
frame->stride() / webrtc::DesktopFrame::kBytesPerPixel, height, crop_w, crop_h,
198204
libyuv::kRotate0,
199205
libyuv::FOURCC_ARGB);
200206

‎webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCaptureCallback.cpp‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ namespace jni
4444
JNIEnv * env = AttachCurrentThread();
4545

4646
if (result != webrtc::DesktopCapturer::Result::SUCCESS) {
47+
// Propagate the failure to Java instead of silently dropping it —
48+
// callers waiting for a frame otherwise have to rely on timeouts.
49+
auto jerror = JavaEnums::toJava(env, result);
50+
env->CallVoidMethod(callback, javaClass->onCaptureResult, jerror.get(), nullptr);
51+
ExceptionCheck(env);
4752
return;
4853
}
4954

@@ -83,7 +88,13 @@ namespace jni
8388
i420Buffer->MutableDataU(), i420Buffer->StrideU(),
8489
i420Buffer->MutableDataV(), i420Buffer->StrideV(),
8590
crop_x, crop_y,
86-
frame->stride() / webrtc::DesktopFrame::kBytesPerPixel, i420Buffer->height(), crop_w, crop_h,
91+
// (src_width, src_height) must describe the FULL source frame, not the cropped
92+
// output. Passing i420Buffer->height() (== crop_h) here made libyuv's internal
93+
// bounds check (crop_y + crop_height <= src_height) fail with -1 whenever
94+
// crop_y > 0 — i.e. for every maximized window, whose frame sits at
95+
// top_left().y() == -border. Screen frames never hit this branch (exact stride,
96+
// fullscreen == true), which is why only window capture appeared broken.
97+
frame->stride() / webrtc::DesktopFrame::kBytesPerPixel, height, crop_w, crop_h,
8798
libyuv::kRotate0,
8899
libyuv::FOURCC_ARGB);
89100

0 commit comments

Comments
 (0)