Fluxer Desktop Source Capture Performance: How the Electron App Minimizes Latency and CPU Usage
Fluxer implements a 60-second time-to-live cache for desktop sources, platform-specific Linux xdg-portal handling, and optimized 320×180 thumbnails to eliminate main thread blocking and reduce CPU overhead during screen capture.
Fluxer (fluxerapp/fluxer) is an open-source screen sharing application built on Electron that requires high-performance desktop source capture across macOS, Windows, and Linux. The implementation in fluxer_desktop/src/main/Window.tsx deliberately minimizes latency through strategic caching, platform-specific optimizations, and careful memory management to ensure the UI remains responsive while capturing displays.
Global Source Caching with TTL
The most critical performance optimization in Fluxer's desktop source capture is the global source cache that avoids costly round-trips to the OS for every display media request.
The implementation defines latestDesktopSources and latestDesktopSourcesTimestamp at the top of Window.tsx (lines 14-17) to store the most recently fetched source array and its timestamp. A constant DESKTOP_SOURCE_CACHE_TTL_MS = 60_000 defines a 60-second freshness window (lines 14-15). This TTL strikes a balance between keeping the source list current (accounting for new windows or closed applications) and preventing excessive OS queries that would block the main thread.
When the IPC handler for get-desktop-sources fires, it checks this cache before invoking desktopCapturer.getSources. If the cache is fresh, the stored sources return immediately, keeping the UI snappy when users repeatedly toggle the picker (lines 31-38).
Additionally, Fluxer maintains a per-request cache tied to the requestId. When a media request originates from the renderer, the current source list is stored in pending.cachedSources (lines 40-47). If the user selects a source after the picker UI appears, the same list can be reused without another desktopCapturer.getSources call, eliminating race conditions particularly prevalent on Linux (lines 80-87).
Platform-Specific Handling for Linux xdg-portal
Fluxer implements distinct code paths for Linux versus macOS and Windows to handle platform-specific performance characteristics.
On Linux, desktopCapturer invokes xdg-portal, which presents its own native picker UI. The source code contains a specific guard clause (lines 89-96) that prevents a second desktopCapturer.getSources call when the global cache is stale. This avoids duplicate portal dialogs and eliminates an unnecessary round-trip that would block the main thread, ensuring users see a single, consistent picker experience.
On macOS and Windows, if the global cache is stale, the code falls back to a fresh desktopCapturer.getSources call (lines 98-104). On these platforms, the OS-level call is inexpensive enough that fresh data is preferred over potentially stale cached sources. This platform-aware branching ensures optimal performance characteristics for each operating system.
Memory Optimization and Thumbnail Sizing
Fluxer carefully constrains memory usage through explicit thumbnail dimensions in the desktopCapturer.getSources call (lines 31-36):
await desktopCapturer.getSources({
types,
thumbnailSize: {width: 320, height: 180},
fetchWindowIcons: true,
});
The 320×180 thumbnail size provides sufficient resolution for clear UI previews while keeping memory footprint low across potentially dozens of windows. The fetchWindowIcons: true parameter adds minimal overhead because Electron caches these icons internally, reusing them across subsequent calls when the list refreshes.
Request Deduplication and Audio Efficiency
To prevent subtle race conditions and extra work, Fluxer tracks whether the display media callback has already been invoked using a callbackInvoked flag (lines 65-71). If a request times out or is cancelled, the invokeCallback helper ensures the callback executes only once, protecting the main thread from re-entering the same code path.
Audio handling is similarly optimized for platform capabilities:
if (withAudio && process.platform === 'win32') {
streams.audio = 'loopback';
}
Only Windows supports loop-back audio capture (lines 26-28). Other operating systems skip this branch entirely, avoiding unnecessary audio-pipeline setup and resource allocation on platforms where loopback capture requires different mechanisms.
Robust Source Resolution Fallbacks
The resolveSelectedDesktopSource helper function (lines 26-58) implements graceful degradation to minimize failed lookups that would otherwise require redundant OS queries. If the user-selected source ID does not match any cached source, the code first attempts a normalized ID match specifically for Linux (lines 38-48).
If normalization fails and the cache contains only a single source entry, the function automatically falls back to that sole entry (lines 49-55). This heuristic reduces the probability of a failed capture session and eliminates the need for additional desktopCapturer calls that would delay the stream start.
IPC Architecture and Preload Bridge
The renderer process never blocks on OS calls because the heavy lifting remains in the main process. The preload script at fluxer_desktop/src/preload/index.tsx exposes a thin wrapper (lines 56-57):
getDesktopSources: (types, requestId) =>
ipcRenderer.invoke('get-desktop-sources', types, requestId)
This architecture ensures that the IPC round-trip—measured in microseconds—never interferes with UI responsiveness, while the actual desktopCapturer work executes asynchronously in the main thread. The separation of concerns between renderer and main process prevents frame drops in the picker UI even when the OS is enumerating dozens of windows.
Summary
- Global 60-second TTL cache (
latestDesktopSources) prevents redundant OS queries while keeping source lists reasonably fresh - Linux-specific xdg-portal handling avoids duplicate native picker dialogs and main thread blocking when the cache expires
- 320×180 thumbnail constraints balance preview clarity with memory efficiency across all platforms
- Per-request caching via
requestIdeliminates race conditions between source enumeration and user selection - Platform-restricted audio setup avoids unnecessary pipeline initialization on macOS and Linux
- Callback deduplication (
callbackInvokedflag) prevents re-entrancy and potential memory leaks on cancelled requests
Frequently Asked Questions
How does Fluxer prevent the desktop source picker from freezing on Linux?
Fluxer prevents freezing by avoiding multiple concurrent calls to desktopCapturer.getSources on Linux. When the 60-second cache expires and a user attempts to select a source, the code explicitly checks if the cached sources exist before proceeding (lines 89-96). If the cache is stale, Fluxer logs a warning and aborts rather than triggering a second xdg-portal dialog that would block the main thread.
What thumbnail size does Fluxer use for screen capture previews and why?
Fluxer uses a fixed 320×180 pixel thumbnail size (lines 32-36). This resolution provides sufficient clarity for users to identify windows and displays in the picker UI while remaining small enough to keep memory usage low when enumerating 50+ sources. The dimensions match a 16:9 aspect ratio that scales cleanly across different display densities.
Why does Fluxer cache desktop sources separately for each media request?
Fluxer stores sources in pending.cachedSources keyed by requestId (lines 40-47) to handle the asynchronous gap between presenting the picker UI and receiving the user's selection. If the global 60-second cache expires during this interval, the per-request cache ensures the selection can still complete without requiring a new desktopCapturer.getSources call that might return different source IDs or trigger a second native dialog on Linux.
How does Fluxer's desktop capture implementation differ between Windows and macOS versus Linux?
On Windows and macOS, Fresh desktopCapturer calls are inexpensive, so Fluxer falls back to real-time source enumeration when the global cache expires (lines 98-104). On Linux, the implementation prioritizes avoiding duplicate xdg-portal invocations over data freshness, preferring to fail gracefully with cached data rather than risk multiple blocking native dialogs (lines 89-96).
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →