uDesktopDuplication icon indicating copy to clipboard operation
uDesktopDuplication copied to clipboard

Stutterless capture?

Open 3DI70R opened this issue 4 years ago • 2 comments

Hello

I'm using your library in personal OBS-like project and while overall its working great, i noticed that captured desktop texture stutters and never achieve desktop framerate. Fps are pretty close, but sometimes frame drops occur. If i understand correctly, that because frame grab occur on background thread and copied to unity on demand without any sync.

While experimenting i moved duplication from Duplicator::Start() to Monitor::Render(), and now stuters are gone, since AcquireNextFrame locks thread untill new image is captured image

I'm not very familiar with native-side things and i don't understand how this dirty hack didn't slowed all things down to a crawl, but it works for me.

I understand that your library prioritizes unity performance over capture performance, but "official" setting for stutterless capture would be nice.

Also, is there any unforeseen consequences in my hack, except frame skips synced to desktop skips?

3DI70R avatar Apr 08 '20 15:04 3DI70R

Thank you for using uDD! And thank you for providing a good solution to enhance a frame rate. I'll check your code also in my environment and if it doesn't have big problem, I'll add a parameter to executing Duplicate() in the render thread.

hecomi avatar Apr 14 '20 16:04 hecomi

I'd want to notice you about my new findings.

Well, i've tried to use my "hack" with dual monitor setup, and its problem became very apparent. It worked fine with one monitor just because my unity app is fullscreen transparent window, and it forced desktop to update on every frame. When nothing happens on second monitor, app freezes, just because it waits for new frame from second monitor.

So, i probably found different, and more correct solution. I always used OBS as reference during stutter detection, and here is OBS implementation of desktop duplicator: https://github.com/obsproject/obs-studio/blob/master/libobs-d3d11/d3d11-duplicator.cpp (gs_duplicator_update_frame) It uses no timeouts and releases frame right after its copied to output texture.

I've removed timeout in Duplicate, and called release after Monitor::Render(), and to my surprise, now capture is very smooth, even with multiple monitors. I didn't tested it very much, but no side effects found yet.

3DI70R avatar Oct 06 '20 10:10 3DI70R