Skip to content

Conversation

@richiemcilroy
Copy link
Member

@richiemcilroy richiemcilroy commented Jan 7, 2026

  • Ensure even dimensions for video encoding: H264 and HEVC encoders now guarantee even output dimensions, preventing encoding failures on certain hardware
    • Refactor ensure_even to shared module: Consolidated duplicate dimension-rounding logic into crates/recording/src/resolution_limits.rs for reuse across the pipeline
    • Improve GPU resolution compatibility: Enhanced logic for determining valid output resolutions with better hardware compatibility and comprehensive tests
    • Fix pipeline fallback dimensions: Output pipeline now ensures even dimensions in fallback paths
    • Refactor audio drift correction: Improved audio mixer drift correction logic with better test coverage
    • Fix settings store sync: Prevent settings overwrite on initialization

Greptile Summary

This PR improves video encoding robustness by ensuring all output dimensions are even numbers, consolidates duplicate dimension-rounding logic into a shared module, refactors audio drift correction to use frame timestamps instead of sample counts, and fixes a settings store initialization race condition.

Key Changes:

  • Consolidated ensure_even function from two duplicate locations into crates/media-info/src/lib.rs for reuse across H264/HEVC encoders
  • Enhanced calculate_gpu_compatible_size to handle odd dimensions even when under the max limit, preventing encoding failures on hardware that requires even dimensions
  • H264 and HEVC encoders now auto-adjust odd dimensions instead of returning validation errors, with warning logs for transparency
  • Refactored AudioDriftTracker to track frame timestamps relative to first frame instead of cumulative sample counts, improving drift correction accuracy
  • Fixed queries.ts to prevent settings store from overwriting persisted mode on initialization using an initialized flag and batch() for atomic updates
  • Added comprehensive test coverage for dimension handling (40+ test cases) and audio drift correction (10+ test cases)

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • All changes are defensive improvements with comprehensive test coverage. The dimension handling prevents encoding failures, audio drift correction is more accurate, and the settings fix prevents data loss. No breaking changes or risky refactors.
  • No files require special attention

Important Files Changed

Filename Overview
crates/recording/src/resolution_limits.rs Re-exports ensure_even from media-info, enhanced GPU compatibility with comprehensive tests
crates/enc-ffmpeg/src/video/h264.rs Removed dimension validation error, auto-adjusts odd dimensions with warning
crates/enc-ffmpeg/src/video/hevc.rs Removed dimension validation error, auto-adjusts odd dimensions with warning
crates/recording/src/output_pipeline/core.rs Refactored audio drift correction to use frame timestamps instead of sample counts, improved accuracy with comprehensive tests
apps/desktop/src/utils/queries.ts Fixed settings store initialization race condition preventing mode overwrite

Sequence Diagram

sequenceDiagram
    participant App as Desktop App
    participant Settings as Settings Store
    participant IR as InstantRecording
    participant RL as ResolutionLimits
    participant H264 as H264Encoder
    participant HEVC as HEVCEncoder
    participant Pipeline as OutputPipeline
    participant Audio as AudioDriftTracker

    Note over App,Settings: Settings Store Initialization Fix
    App->>Settings: Initialize (load from store)
    Settings-->>App: mode data
    Note over App: Set initialized=true AFTER loading
    App->>Settings: createEffect watches state changes
    Note over App: Only persist if initialized=true
    Settings-->>App: Prevents overwrite on init

    Note over IR,HEVC: Video Dimension Handling
    IR->>IR: create_pipeline(screen_info)
    alt max_output_size defined
        IR->>IR: clamp_size()
    else fallback path
        IR->>RL: ensure_even(width)
        IR->>RL: ensure_even(height)
        RL-->>IR: (even_width, even_height)
    end

    IR->>H264: build encoder
    H264->>RL: ensure_even(raw_width)
    H264->>RL: ensure_even(raw_height)
    RL-->>H264: (even_width, even_height)
    Note over H264: Log warning if adjusted
    H264-->>IR: Encoder ready

    IR->>HEVC: build encoder
    HEVC->>RL: ensure_even(raw_width)
    HEVC->>RL: ensure_even(raw_height)
    RL-->>HEVC: (even_width, even_height)
    Note over HEVC: Log warning if adjusted
    HEVC-->>IR: Encoder ready

    Note over Pipeline,Audio: Audio Drift Correction
    Pipeline->>Audio: new()
    Note over Audio: first_frame_timestamp_secs = None
    
    loop Each audio frame
        Pipeline->>Audio: calculate_timestamp(frame_ts, wall_clock)
        
        alt First frame
            Audio->>Audio: Store first_frame_timestamp_secs
        end
        
        Audio->>Audio: frame_elapsed = frame_ts - first_ts
        
        alt Before warmup (< 2s)
            Audio-->>Pipeline: Duration(frame_elapsed)
        else After warmup (>= 2s)
            alt First warmup call
                Audio->>Audio: baseline = frame_elapsed - wall_clock
            end
            Audio->>Audio: adjusted = frame_elapsed - baseline
            Audio->>Audio: drift_ratio = wall_clock / adjusted
            Audio->>Audio: corrected = adjusted * drift_ratio
            Audio-->>Pipeline: Duration(corrected)
        end
        
        Pipeline->>Pipeline: send_audio_frame(timestamp)
    end
Loading

@richiemcilroy richiemcilroy merged commit 4b7d1e0 into main Jan 7, 2026
16 checks passed
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.

2 participants