OBSService provides core recording functionality for Segra using OBS Studio. It manages video encoding, audio capture, game detection integration, and replay buffer operations.
Overview
The Recording Service is implemented as a static class inBackend/Recorder/OBSService.cs and provides:
- OBS initialization and configuration
- Game capture with automatic hook detection
- Display capture fallback
- Session recording and replay buffer modes
- Multi-track audio encoding
- Video encoder configuration (hardware and software)
Public Properties
Indicates whether OBS has been successfully initialized
The detected GPU vendor (Nvidia, AMD, Intel, or Unknown) for hardware encoding
Width of the currently captured game window (null if not captured)
Height of the currently captured game window (null if not captured)
The version string of the installed OBS libraries
The active game capture source instance (null when not recording)
Core Methods
InitializeAsync
Initializes the OBS recording engine and sets up all necessary resources.- Downloads OBS binaries if not present
- Configures OBS with default video (1920x1080@60fps) and audio settings
- Sets up logging callbacks
- Detects available encoders
- Starts the game detection service
This method must be called before any recording operations. It will automatically download OBS libraries if they don’t exist.
StartRecording
Starts a new recording session with the specified game.The display name of the game being recorded
Full path to the game executable
If true, uses display capture only (no game hook)
Process ID of the game to record
bool - True if recording started successfully, false otherwise
Recording Modes:
- Session Mode: Records continuously to a single MP4 file
- Buffer Mode: Maintains a rolling replay buffer (save on demand)
- Hybrid Mode: Both session recording and replay buffer simultaneously
StopRecording
Stops the active recording session and performs cleanup.- Stops all active outputs (session and/or replay buffer)
- Waits up to 30 seconds for graceful stop, then force-stops if needed
- Generates metadata, thumbnails, and waveforms for recorded content
- Cleans up encoders and sources
- Optionally discards sessions without manual bookmarks (if configured)
SaveReplayBuffer
Saves the current replay buffer to disk and generates metadata.bool - True if the replay buffer was saved successfully
Process:
- Checks if replay buffer is active
- Triggers save operation
- Waits for save callback (up to 5 seconds)
- Retrieves saved file path
- Generates thumbnail, metadata, and waveform
- Resets buffer for next recording
Example Usage
After saving, the replay buffer is automatically reset so subsequent saves only include new footage.
Shutdown
Shuts down OBS and releases all resources.- Disposes OBS context
- Releases all native resources
- Sets
IsInitializedto false
Audio Configuration
The service supports multiple audio modes:Audio Output Modes
- All: Captures all desktop audio sources
- GameOnly: Captures only game audio (uses game capture’s built-in audio)
- GameAndDiscord: Captures game audio plus Discord application audio
Separate Audio Tracks
WhenEnableSeparateAudioTracks is enabled:
- Track 1: Full mix of all audio sources
- Tracks 2-6: Individual isolated sources (mic, desktop, game, Discord)
Video Configuration
Resolution and Aspect Ratio
- Base resolution matches the captured window or primary monitor
- Output resolution respects the max resolution setting from user preferences
- Automatic 4:3 to 16:9 stretching (when enabled)
- Downscaling maintains aspect ratio and rounds to multiples of 4
Encoders
Supported encoders are automatically detected based on GPU: NVIDIA:h264_nvenchevc_nvencav1_nvenc
h264_amfhevc_amfav1_amf
h264_qsvhevc_qsvav1_qsv
libx264libx265
Rate Control
- CBR (Constant Bitrate): Fixed bitrate
- VBR (Variable Bitrate): Min/max bitrate range
- CRF (Constant Rate Factor): Quality-based (software encoders)
- CQP (Constant Quantization Parameter): Quality-based (hardware encoders)
Game Capture
Hook Detection
The service monitors game capture hook status:- 90-second timeout for initial hook
- Automatic fallback to display capture if hook fails
- Event-based hook/unhook notifications
Capture Priority
- Game Capture (top layer, visible when hooked)
- Display Capture (fallback layer, always active)
File Organization
Recorded content is organized by type and game:Error Handling
The service includes robust error handling:- Automatic retry with force-stop if graceful shutdown fails
- Process priority management (High during recording, Normal after)
- Orphaned file detection and recovery
- Thread-safe stop operations with semaphore locking
Integration Points
- GameDetectionService: Automatically starts recording when games launch
- ContentService: Generates thumbnails and metadata
- KeybindCaptureService: Records user inputs during session
- GameIntegrationService: Manages game-specific overlays