mirror of https://github.com/jetkvm/kvm.git
560 lines
27 KiB
Go
560 lines
27 KiB
Go
package audio
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/jetkvm/kvm/internal/logging"
|
|
)
|
|
|
|
// AudioConfigConstants centralizes all hardcoded values used across audio components.
|
|
// This configuration system allows runtime tuning of audio performance, quality, and resource usage.
|
|
type AudioConfigConstants struct {
|
|
// Audio Quality Presets
|
|
MaxAudioFrameSize int // Maximum audio frame size in bytes (default: 4096)
|
|
MaxPCMBufferSize int // Maximum PCM buffer size in bytes for separate buffer optimization
|
|
|
|
// Opus Encoding Parameters
|
|
OpusBitrate int // Target bitrate for Opus encoding in bps (default: 128000)
|
|
OpusComplexity int // Computational complexity 0-10 (default: 10 for best quality)
|
|
OpusVBR int // Variable Bit Rate: 0=CBR, 1=VBR (default: 1)
|
|
OpusVBRConstraint int // VBR constraint: 0=unconstrained, 1=constrained (default: 0)
|
|
OpusDTX int // Discontinuous Transmission: 0=disabled, 1=enabled (default: 0)
|
|
|
|
// Audio Parameters
|
|
SampleRate int // Audio sampling frequency in Hz (default: 48000)
|
|
Channels int // Number of audio channels: 1=mono, 2=stereo (default: 2)
|
|
FrameSize int // Samples per audio frame (default: 960 for 20ms at 48kHz)
|
|
MaxPacketSize int // Maximum encoded packet size in bytes (default: 4000)
|
|
|
|
// Optimal Audio Configuration (S16_LE @ 48kHz stereo from HDMI)
|
|
// Single optimized setting - no quality presets needed
|
|
OptimalOutputBitrate int // Output bitrate: 96 kbps (optimal for stereo @ 48kHz)
|
|
OptimalInputBitrate int // Input bitrate: 48 kbps (optimal for mono mic @ 48kHz)
|
|
|
|
// Optimal OPUS Encoder Parameters (minimal CPU usage)
|
|
OptimalOpusComplexity int // Complexity: 1 (minimal CPU ~0.5%)
|
|
OptimalOpusVBR int // VBR: enabled for efficiency
|
|
OptimalOpusSignalType int // Signal: OPUS_SIGNAL_MUSIC (3002)
|
|
OptimalOpusBandwidth int // Bandwidth: WIDEBAND (1103 = native 48kHz)
|
|
OptimalOpusDTX int // DTX: disabled for continuous audio
|
|
|
|
// CGO Audio Constants
|
|
CGOOpusBitrate int // Native Opus encoder bitrate in bps (default: 96000)
|
|
|
|
CGOOpusComplexity int // Computational complexity for native Opus encoder (0-10)
|
|
CGOOpusVBR int // Variable Bit Rate in native Opus encoder (0=CBR, 1=VBR)
|
|
CGOOpusVBRConstraint int // Constrained VBR in native encoder (0/1)
|
|
CGOOpusSignalType int // Signal type hint for native Opus encoder
|
|
CGOOpusBandwidth int // Frequency bandwidth for native Opus encoder
|
|
CGOOpusDTX int // Discontinuous Transmission in native encoder (0/1)
|
|
CGOSampleRate int // Sample rate for native audio processing (Hz)
|
|
CGOChannels int // Channel count for native audio processing
|
|
CGOFrameSize int // Frame size for native Opus processing (samples)
|
|
CGOMaxPacketSize int // Maximum packet size for native encoding (bytes)
|
|
|
|
// Input IPC Constants
|
|
InputIPCSampleRate int // Sample rate for input IPC audio processing (Hz)
|
|
InputIPCChannels int // Channel count for input IPC audio processing
|
|
InputIPCFrameSize int // Frame size for input IPC processing (samples)
|
|
|
|
// Output IPC Constants
|
|
OutputMaxFrameSize int // Maximum frame size for output processing (bytes)
|
|
OutputHeaderSize int // Size of output message headers (bytes)
|
|
|
|
OutputMessagePoolSize int // Output message pool size (128)
|
|
|
|
// Socket Buffer Constants
|
|
SocketOptimalBuffer int // Optimal socket buffer size (128KB)
|
|
SocketMaxBuffer int // Maximum socket buffer size (256KB)
|
|
SocketMinBuffer int // Minimum socket buffer size (32KB)
|
|
|
|
// Process Management
|
|
MaxRestartAttempts int // Maximum restart attempts (5)
|
|
RestartWindow time.Duration // Restart attempt window (5m)
|
|
RestartDelay time.Duration // Initial restart delay (2s)
|
|
MaxRestartDelay time.Duration // Maximum restart delay (30s)
|
|
|
|
// Buffer Management
|
|
|
|
MaxPoolSize int
|
|
MessagePoolSize int
|
|
OptimalSocketBuffer int
|
|
MaxSocketBuffer int
|
|
MinSocketBuffer int
|
|
ChannelBufferSize int
|
|
AudioFramePoolSize int
|
|
PageSize int
|
|
InitialBufferFrames int
|
|
BytesToMBDivisor int
|
|
MinReadEncodeBuffer int
|
|
MaxDecodeWriteBuffer int
|
|
MinBatchSizeForThreadPinning int
|
|
|
|
MagicNumber uint32
|
|
MaxFrameSize int
|
|
WriteTimeout time.Duration
|
|
HeaderSize int
|
|
MetricsUpdateInterval time.Duration
|
|
WarmupSamples int
|
|
MetricsChannelBuffer int
|
|
LatencyHistorySize int
|
|
MaxCPUPercent float64
|
|
MinCPUPercent float64
|
|
DefaultClockTicks float64
|
|
DefaultMemoryGB int
|
|
MaxWarmupSamples int
|
|
WarmupCPUSamples int
|
|
LogThrottleIntervalSec int
|
|
MinValidClockTicks int
|
|
MaxValidClockTicks int
|
|
CPUFactor float64
|
|
MemoryFactor float64
|
|
LatencyFactor float64
|
|
|
|
// Timing Configuration
|
|
RetryDelay time.Duration // Retry delay
|
|
MaxRetryDelay time.Duration // Maximum retry delay
|
|
BackoffMultiplier float64 // Backoff multiplier
|
|
MaxConsecutiveErrors int // Maximum consecutive errors
|
|
DefaultSleepDuration time.Duration // 100ms
|
|
ShortSleepDuration time.Duration // 10ms
|
|
LongSleepDuration time.Duration // 200ms
|
|
DefaultTickerInterval time.Duration // 100ms
|
|
BufferUpdateInterval time.Duration // 500ms
|
|
InputSupervisorTimeout time.Duration // 5s
|
|
OutputSupervisorTimeout time.Duration // 5s
|
|
BatchProcessingDelay time.Duration // 10ms
|
|
|
|
// System threshold configuration for buffer management
|
|
LowCPUThreshold float64 // CPU usage threshold for performance optimization
|
|
HighCPUThreshold float64 // CPU usage threshold for performance limits
|
|
LowMemoryThreshold float64 // 50% memory threshold
|
|
HighMemoryThreshold float64 // 75% memory threshold
|
|
CooldownPeriod time.Duration // 30s cooldown period
|
|
RollbackThreshold time.Duration // 300ms rollback threshold
|
|
|
|
MaxLatencyThreshold time.Duration // 200ms max latency
|
|
JitterThreshold time.Duration // 20ms jitter threshold
|
|
LatencyOptimizationInterval time.Duration // 5s optimization interval
|
|
MicContentionTimeout time.Duration // 200ms contention timeout
|
|
PreallocPercentage int // 20% preallocation percentage
|
|
BackoffStart time.Duration // 50ms initial backoff
|
|
|
|
InputMagicNumber uint32 // Magic number for input IPC messages (0x4A4B4D49 "JKMI")
|
|
|
|
OutputMagicNumber uint32 // Magic number for output IPC messages (0x4A4B4F55 "JKOU")
|
|
|
|
// Calculation Constants
|
|
PercentageMultiplier float64 // Multiplier for percentage calculations (100.0)
|
|
AveragingWeight float64 // Weight for weighted averaging (0.7)
|
|
ScalingFactor float64 // General scaling factor (1.5)
|
|
CPUMemoryWeight float64 // Weight for CPU factor in calculations (0.5)
|
|
MemoryWeight float64 // Weight for memory factor (0.3)
|
|
LatencyWeight float64 // Weight for latency factor (0.2)
|
|
PoolGrowthMultiplier int // Multiplier for pool size growth (2)
|
|
LatencyScalingFactor float64 // Scaling factor for latency calculations (2.0)
|
|
OptimizerAggressiveness float64 // Aggressiveness level for optimization (0.7)
|
|
|
|
// CGO Audio Processing Constants
|
|
CGOUsleepMicroseconds int // Sleep duration for CGO usleep calls (1000μs)
|
|
|
|
CGOPCMBufferSize int // PCM buffer size for CGO audio processing
|
|
CGONanosecondsPerSecond float64 // Nanoseconds per second conversion
|
|
|
|
// Output Streaming Constants
|
|
OutputStreamingFrameIntervalMS int // Output frame interval (20ms for 50 FPS)
|
|
|
|
// IPC Constants
|
|
IPCInitialBufferFrames int // Initial IPC buffer size (500 frames)
|
|
|
|
EventTimeoutSeconds int
|
|
EventTimeFormatString string
|
|
EventSubscriptionDelayMS int
|
|
InputProcessingTimeoutMS int
|
|
InputSocketName string
|
|
OutputSocketName string
|
|
AudioInputComponentName string
|
|
AudioOutputComponentName string
|
|
AudioServerComponentName string
|
|
AudioRelayComponentName string
|
|
AudioEventsComponentName string
|
|
|
|
TestSocketTimeout time.Duration
|
|
TestBufferSize int
|
|
TestRetryDelay time.Duration
|
|
LatencyHistogramMaxSamples int
|
|
LatencyPercentile50 int
|
|
LatencyPercentile95 int
|
|
LatencyPercentile99 int
|
|
|
|
// Buffer Pool Configuration
|
|
BufferPoolDefaultSize int // Default buffer pool size when MaxPoolSize is invalid
|
|
BufferPoolControlSize int // Control buffer pool size
|
|
ZeroCopyPreallocSizeBytes int // Zero-copy frame pool preallocation size in bytes
|
|
ZeroCopyMinPreallocFrames int // Minimum preallocated frames for zero-copy pool
|
|
BufferPoolHitRateBase float64 // Base for hit rate percentage calculation
|
|
|
|
HitRateCalculationBase float64
|
|
MaxLatency time.Duration
|
|
MinMetricsUpdateInterval time.Duration
|
|
MaxMetricsUpdateInterval time.Duration
|
|
MinSampleRate int
|
|
MaxSampleRate int
|
|
MaxChannels int
|
|
|
|
// CGO Constants
|
|
CGOMaxBackoffMicroseconds int // Maximum CGO backoff time (500ms)
|
|
CGOMaxAttempts int // Maximum CGO retry attempts (5)
|
|
|
|
// Frame Duration Validation
|
|
MinFrameDuration time.Duration // Minimum frame duration (10ms)
|
|
MaxFrameDuration time.Duration // Maximum frame duration (100ms)
|
|
|
|
// Valid Sample Rates
|
|
// Validation Constants
|
|
ValidSampleRates []int // Supported sample rates (8kHz to 48kHz)
|
|
MinOpusBitrate int // Minimum Opus bitrate (6000 bps)
|
|
MaxOpusBitrate int // Maximum Opus bitrate (510000 bps)
|
|
MaxValidationTime time.Duration // Validation timeout (5s)
|
|
MinFrameSize int // Minimum frame size (64 bytes)
|
|
FrameSizeTolerance int // Frame size tolerance (512 bytes)
|
|
|
|
// Latency Histogram Buckets
|
|
LatencyBucket10ms time.Duration // 10ms latency bucket
|
|
LatencyBucket25ms time.Duration // 25ms latency bucket
|
|
LatencyBucket50ms time.Duration // 50ms latency bucket
|
|
LatencyBucket100ms time.Duration // 100ms latency bucket
|
|
LatencyBucket250ms time.Duration // 250ms latency bucket
|
|
LatencyBucket500ms time.Duration // 500ms latency bucket
|
|
LatencyBucket1s time.Duration // 1s latency bucket
|
|
LatencyBucket2s time.Duration // 2s latency bucket
|
|
|
|
MaxAudioProcessorWorkers int
|
|
MaxAudioReaderWorkers int
|
|
AudioProcessorQueueSize int
|
|
AudioReaderQueueSize int
|
|
WorkerMaxIdleTime time.Duration
|
|
|
|
// Connection Retry Configuration
|
|
MaxConnectionAttempts int // Maximum connection retry attempts
|
|
ConnectionRetryDelay time.Duration // Initial connection retry delay
|
|
MaxConnectionRetryDelay time.Duration // Maximum connection retry delay
|
|
ConnectionBackoffFactor float64 // Connection retry backoff factor
|
|
ConnectionTimeoutDelay time.Duration // Connection timeout for each attempt
|
|
ReconnectionInterval time.Duration // Interval for automatic reconnection attempts
|
|
HealthCheckInterval time.Duration // Health check interval for connections
|
|
|
|
// Quality Change Timeout Configuration
|
|
QualityChangeSupervisorTimeout time.Duration // Timeout for supervisor stop during quality changes
|
|
QualityChangeTickerInterval time.Duration // Ticker interval for supervisor stop polling
|
|
QualityChangeSettleDelay time.Duration // Delay for quality change to settle
|
|
QualityChangeRecoveryDelay time.Duration // Delay before attempting recovery
|
|
|
|
}
|
|
|
|
// DefaultAudioConfig returns the default configuration constants
|
|
// These values are carefully chosen based on JetKVM's embedded ARM environment,
|
|
// real-time audio requirements, and extensive testing for optimal performance.
|
|
func DefaultAudioConfig() *AudioConfigConstants {
|
|
return &AudioConfigConstants{
|
|
// Audio Quality Presets
|
|
MaxAudioFrameSize: 4096,
|
|
MaxPCMBufferSize: 8192, // Default PCM buffer size (2x MaxAudioFrameSize for safety)
|
|
|
|
// Opus Encoding Parameters
|
|
OpusBitrate: 128000,
|
|
OpusComplexity: 10,
|
|
OpusVBR: 1,
|
|
OpusVBRConstraint: 0,
|
|
OpusDTX: 0,
|
|
|
|
// Audio Parameters
|
|
SampleRate: 48000,
|
|
Channels: 2,
|
|
FrameSize: 960,
|
|
MaxPacketSize: 4000,
|
|
|
|
// Optimal Audio Configuration (single setting for all use cases)
|
|
OptimalOutputBitrate: 96, // 96 kbps for stereo @ 48kHz
|
|
OptimalInputBitrate: 48, // 48 kbps for mono mic @ 48kHz
|
|
OptimalOpusComplexity: 1, // Complexity 1: minimal CPU (~0.5%)
|
|
OptimalOpusVBR: 1, // VBR enabled for efficiency
|
|
OptimalOpusSignalType: 3002, // OPUS_SIGNAL_MUSIC
|
|
OptimalOpusBandwidth: 1103, // OPUS_BANDWIDTH_WIDEBAND (native 48kHz)
|
|
OptimalOpusDTX: 0, // DTX disabled for continuous audio
|
|
|
|
// CGO Audio Constants - Optimized for S16_LE @ 48kHz with minimal CPU
|
|
CGOOpusBitrate: 96000, // 96 kbps optimal for stereo @ 48kHz
|
|
CGOOpusComplexity: 1, // Complexity 1: minimal CPU (~0.5% on RV1106)
|
|
CGOOpusVBR: 1, // VBR enabled for efficiency
|
|
CGOOpusVBRConstraint: 0, // Unconstrained VBR: allows bitrate spikes for transients (beeps/sharp sounds)
|
|
CGOOpusSignalType: 3002, // OPUS_SIGNAL_MUSIC (better for HDMI audio)
|
|
CGOOpusBandwidth: 1103, // OPUS_BANDWIDTH_WIDEBAND (native 48kHz, no resampling)
|
|
CGOOpusDTX: 0, // DTX disabled for continuous audio
|
|
CGOSampleRate: 48000, // 48 kHz native HDMI sample rate
|
|
CGOChannels: 2, // Stereo
|
|
CGOFrameSize: 960, // 20ms frames at 48kHz
|
|
CGOMaxPacketSize: 1500, // Standard Ethernet MTU
|
|
|
|
// Input IPC Constants
|
|
InputIPCSampleRate: 48000, // Input IPC sample rate (48kHz)
|
|
InputIPCChannels: 2, // Input IPC channels (stereo)
|
|
InputIPCFrameSize: 960, // Input IPC frame size (960 samples)
|
|
|
|
// Output IPC Constants
|
|
OutputMaxFrameSize: 4096, // Maximum output frame size
|
|
OutputHeaderSize: 17, // Output frame header size
|
|
|
|
OutputMessagePoolSize: 128, // Output message pool size
|
|
|
|
// Socket Buffer Constants
|
|
SocketOptimalBuffer: 131072, // 128KB optimal socket buffer
|
|
SocketMaxBuffer: 262144, // 256KB maximum socket buffer
|
|
SocketMinBuffer: 32768, // 32KB minimum socket buffer
|
|
|
|
// Process Management
|
|
MaxRestartAttempts: 5, // Maximum restart attempts
|
|
|
|
RestartWindow: 5 * time.Minute, // Time window for restart attempt counting
|
|
RestartDelay: 1 * time.Second, // Initial delay before restart attempts
|
|
MaxRestartDelay: 30 * time.Second, // Maximum delay for exponential backoff
|
|
|
|
// Buffer Management
|
|
|
|
MaxPoolSize: 100, // Maximum object pool size
|
|
MessagePoolSize: 1024, // Significantly increased message pool for quality change bursts
|
|
OptimalSocketBuffer: 262144, // 256KB optimal socket buffer
|
|
MaxSocketBuffer: 1048576, // 1MB maximum socket buffer
|
|
MinSocketBuffer: 8192, // 8KB minimum socket buffer
|
|
ChannelBufferSize: 2048, // Significantly increased channel buffer for quality change bursts
|
|
AudioFramePoolSize: 1500, // Audio frame object pool size
|
|
PageSize: 4096, // Memory page size for alignment
|
|
InitialBufferFrames: 1000, // Increased initial buffer size during startup
|
|
BytesToMBDivisor: 1024 * 1024, // Byte to megabyte conversion
|
|
MinReadEncodeBuffer: 1276, // Minimum CGO read/encode buffer
|
|
MaxDecodeWriteBuffer: 4096, // Maximum CGO decode/write buffer
|
|
|
|
// IPC Configuration - Balanced for stability
|
|
MagicNumber: 0xDEADBEEF, // IPC message validation header
|
|
MaxFrameSize: 4096, // Maximum audio frame size (4KB)
|
|
WriteTimeout: 1000 * time.Millisecond, // Further increased timeout to handle quality change bursts
|
|
HeaderSize: 8, // IPC message header size
|
|
|
|
// Monitoring and Metrics - Balanced for stability
|
|
MetricsUpdateInterval: 1000 * time.Millisecond, // Stable metrics collection frequency
|
|
WarmupSamples: 10, // Adequate warmup samples for accuracy
|
|
MetricsChannelBuffer: 100, // Adequate metrics data channel buffer
|
|
LatencyHistorySize: 100, // Adequate latency measurements to keep
|
|
|
|
// Process Monitoring Constants
|
|
MaxCPUPercent: 100.0, // Maximum CPU percentage
|
|
MinCPUPercent: 0.01, // Minimum CPU percentage
|
|
DefaultClockTicks: 250.0, // Default clock ticks for embedded ARM systems
|
|
DefaultMemoryGB: 8, // Default memory in GB
|
|
MaxWarmupSamples: 3, // Maximum warmup samples
|
|
WarmupCPUSamples: 2, // CPU warmup samples
|
|
LogThrottleIntervalSec: 10, // Log throttle interval in seconds
|
|
MinValidClockTicks: 50, // Minimum valid clock ticks
|
|
MaxValidClockTicks: 1000, // Maximum valid clock ticks
|
|
|
|
// Performance Tuning
|
|
CPUFactor: 0.7, // CPU weight in performance calculations
|
|
MemoryFactor: 0.8, // Memory weight in performance calculations
|
|
LatencyFactor: 0.9, // Latency weight in performance calculations
|
|
|
|
// Error Handling
|
|
RetryDelay: 100 * time.Millisecond, // Initial retry delay
|
|
MaxRetryDelay: 5 * time.Second, // Maximum retry delay
|
|
BackoffMultiplier: 2.0, // Exponential backoff multiplier
|
|
MaxConsecutiveErrors: 5, // Consecutive error threshold
|
|
|
|
// Connection Retry Configuration
|
|
MaxConnectionAttempts: 15, // Maximum connection retry attempts
|
|
ConnectionRetryDelay: 50 * time.Millisecond, // Initial connection retry delay
|
|
MaxConnectionRetryDelay: 2 * time.Second, // Maximum connection retry delay
|
|
ConnectionBackoffFactor: 1.5, // Connection retry backoff factor
|
|
ConnectionTimeoutDelay: 5 * time.Second, // Connection timeout for each attempt
|
|
ReconnectionInterval: 30 * time.Second, // Interval for automatic reconnection attempts
|
|
HealthCheckInterval: 10 * time.Second, // Health check interval for connections
|
|
|
|
// Quality Change Timeout Configuration
|
|
QualityChangeSupervisorTimeout: 5 * time.Second, // Timeout for supervisor stop during quality changes
|
|
QualityChangeTickerInterval: 100 * time.Millisecond, // Ticker interval for supervisor stop polling
|
|
QualityChangeSettleDelay: 2 * time.Second, // Delay for quality change to settle
|
|
QualityChangeRecoveryDelay: 1 * time.Second, // Delay before attempting recovery
|
|
|
|
// Timing Constants - Optimized for quality change stability
|
|
DefaultSleepDuration: 100 * time.Millisecond, // Balanced polling interval
|
|
ShortSleepDuration: 10 * time.Millisecond, // Balanced high-frequency polling
|
|
LongSleepDuration: 200 * time.Millisecond, // Balanced background task delay
|
|
DefaultTickerInterval: 100 * time.Millisecond, // Balanced periodic task interval
|
|
BufferUpdateInterval: 250 * time.Millisecond, // Faster buffer size update frequency
|
|
InputSupervisorTimeout: 5 * time.Second, // Input monitoring timeout
|
|
OutputSupervisorTimeout: 5 * time.Second, // Output monitoring timeout
|
|
BatchProcessingDelay: 5 * time.Millisecond, // Reduced batch processing delay
|
|
|
|
// System Load Configuration - Optimized for single-core RV1106G3
|
|
LowCPUThreshold: 0.40, // Adjusted for single-core ARM system
|
|
HighCPUThreshold: 0.75, // Adjusted for single-core RV1106G3 (current load ~64%)
|
|
LowMemoryThreshold: 0.60,
|
|
HighMemoryThreshold: 0.85, // Adjusted for 200MB total memory system
|
|
|
|
CooldownPeriod: 15 * time.Second, // Reduced cooldown period
|
|
RollbackThreshold: 200 * time.Millisecond, // Lower rollback threshold
|
|
|
|
MaxLatencyThreshold: 150 * time.Millisecond, // Lower max latency threshold
|
|
JitterThreshold: 15 * time.Millisecond, // Reduced jitter threshold
|
|
LatencyOptimizationInterval: 3 * time.Second, // More frequent optimization
|
|
|
|
// Microphone Contention Configuration
|
|
MicContentionTimeout: 200 * time.Millisecond,
|
|
|
|
// Buffer Pool Configuration
|
|
PreallocPercentage: 20,
|
|
|
|
// Sleep and Backoff Configuration
|
|
BackoffStart: 50 * time.Millisecond,
|
|
|
|
// Protocol Magic Numbers
|
|
InputMagicNumber: 0x4A4B4D49, // "JKMI" (JetKVM Microphone Input)
|
|
OutputMagicNumber: 0x4A4B4F55, // "JKOU" (JetKVM Output)
|
|
|
|
// Calculation Constants
|
|
PercentageMultiplier: 100.0, // Standard percentage conversion (0.5 * 100 = 50%)
|
|
AveragingWeight: 0.7, // Weight for smoothing values (70% recent, 30% historical)
|
|
ScalingFactor: 1.5, // General scaling factor for adaptive adjustments
|
|
|
|
CPUMemoryWeight: 0.5, // CPU factor weight in combined calculations
|
|
MemoryWeight: 0.3, // Memory factor weight in combined calculations
|
|
LatencyWeight: 0.2, // Latency factor weight in combined calculations
|
|
PoolGrowthMultiplier: 2, // Pool growth multiplier
|
|
LatencyScalingFactor: 2.0, // Latency ratio scaling factor
|
|
OptimizerAggressiveness: 0.7, // Optimizer aggressiveness factor
|
|
|
|
// CGO Audio Processing Constants - Balanced for stability
|
|
CGOUsleepMicroseconds: 1000, // 1000 microseconds (1ms) for stable CGO usleep calls
|
|
CGOPCMBufferSize: 1920, // 1920 samples for PCM buffer (max 2ch*960)
|
|
CGONanosecondsPerSecond: 1000000000.0, // 1000000000.0 for nanosecond conversions
|
|
|
|
// Output Streaming Constants - Balanced for stability
|
|
OutputStreamingFrameIntervalMS: 20, // 20ms frame interval (50 FPS) for stability
|
|
|
|
// IPC Constants
|
|
IPCInitialBufferFrames: 500, // 500 frames for initial buffer
|
|
|
|
// Event Constants - Balanced for stability
|
|
EventTimeoutSeconds: 2, // 2 seconds for event timeout
|
|
EventTimeFormatString: "2006-01-02T15:04:05.000Z", // "2006-01-02T15:04:05.000Z" time format
|
|
EventSubscriptionDelayMS: 100, // 100ms subscription delay
|
|
|
|
// Goroutine Pool Configuration
|
|
MaxAudioProcessorWorkers: 16, // 16 workers for audio processing tasks
|
|
MaxAudioReaderWorkers: 8, // 8 workers for audio reading tasks
|
|
AudioProcessorQueueSize: 64, // 64 tasks queue size for processor pool
|
|
AudioReaderQueueSize: 32, // 32 tasks queue size for reader pool
|
|
WorkerMaxIdleTime: 60 * time.Second, // 60s maximum idle time before worker termination
|
|
|
|
// Input Processing Constants - Balanced for stability
|
|
InputProcessingTimeoutMS: 10, // 10ms processing timeout threshold
|
|
|
|
// Socket Names
|
|
InputSocketName: "audio_input.sock", // Socket name for audio input IPC
|
|
OutputSocketName: "audio_output.sock", // Socket name for audio output IPC
|
|
|
|
// Component Names
|
|
AudioInputComponentName: "audio-input", // Component name for input logging
|
|
AudioOutputComponentName: "audio-output", // Component name for output logging
|
|
AudioServerComponentName: "audio-server", // Component name for server logging
|
|
AudioRelayComponentName: "audio-relay", // Component name for relay logging
|
|
AudioEventsComponentName: "audio-events", // Component name for events logging
|
|
|
|
// Test Configuration
|
|
TestSocketTimeout: 100 * time.Millisecond, // 100ms timeout for test socket operations
|
|
TestBufferSize: 4096, // 4096 bytes buffer size for test operations
|
|
TestRetryDelay: 200 * time.Millisecond, // 200ms delay between test retry attempts
|
|
|
|
// Latency Histogram Configuration
|
|
LatencyHistogramMaxSamples: 1000, // 1000 samples for latency tracking
|
|
LatencyPercentile50: 50, // 50th percentile calculation factor
|
|
LatencyPercentile95: 95, // 95th percentile calculation factor
|
|
LatencyPercentile99: 99, // 99th percentile calculation factor
|
|
|
|
// Buffer Pool Configuration
|
|
BufferPoolDefaultSize: 64, // Default buffer pool size when MaxPoolSize is invalid
|
|
BufferPoolControlSize: 512, // Control buffer pool size
|
|
ZeroCopyPreallocSizeBytes: 1024 * 1024, // Zero-copy frame pool preallocation size in bytes (1MB)
|
|
ZeroCopyMinPreallocFrames: 1, // Minimum preallocated frames for zero-copy pool
|
|
BufferPoolHitRateBase: 100.0, // Base for hit rate percentage calculation
|
|
|
|
// Buffer Pool Efficiency Constants
|
|
HitRateCalculationBase: 100.0, // 100.0 base for hit rate percentage calculation
|
|
|
|
// Validation Constants
|
|
MaxLatency: 500 * time.Millisecond, // 500ms maximum allowed latency
|
|
MinMetricsUpdateInterval: 100 * time.Millisecond, // 100ms minimum metrics update interval
|
|
MaxMetricsUpdateInterval: 10 * time.Second, // 10s maximum metrics update interval
|
|
MinSampleRate: 8000, // 8kHz minimum sample rate
|
|
MaxSampleRate: 48000, // 48kHz maximum sample rate
|
|
MaxChannels: 8, // 8 maximum audio channels
|
|
|
|
// CGO Constants
|
|
CGOMaxBackoffMicroseconds: 500000, // 500ms maximum backoff in microseconds
|
|
CGOMaxAttempts: 5, // 5 maximum retry attempts
|
|
|
|
// Validation Frame Size Limits
|
|
MinFrameDuration: 10 * time.Millisecond, // 10ms minimum frame duration
|
|
MaxFrameDuration: 100 * time.Millisecond, // 100ms maximum frame duration
|
|
|
|
// Valid Sample Rates
|
|
ValidSampleRates: []int{8000, 12000, 16000, 22050, 24000, 44100, 48000}, // Supported sample rates
|
|
|
|
// Opus Bitrate Validation Constants
|
|
MinOpusBitrate: 6000, // 6000 bps minimum Opus bitrate
|
|
MaxOpusBitrate: 510000, // 510000 bps maximum Opus bitrate
|
|
|
|
// Validation Configuration
|
|
MaxValidationTime: 5 * time.Second, // 5s maximum validation timeout
|
|
MinFrameSize: 1, // 1 byte minimum frame size (allow small frames)
|
|
FrameSizeTolerance: 512, // 512 bytes frame size tolerance
|
|
|
|
// Latency Histogram Bucket Configuration
|
|
LatencyBucket10ms: 10 * time.Millisecond, // 10ms latency bucket
|
|
LatencyBucket25ms: 25 * time.Millisecond, // 25ms latency bucket
|
|
LatencyBucket50ms: 50 * time.Millisecond, // 50ms latency bucket
|
|
LatencyBucket100ms: 100 * time.Millisecond, // 100ms latency bucket
|
|
LatencyBucket250ms: 250 * time.Millisecond, // 250ms latency bucket
|
|
LatencyBucket500ms: 500 * time.Millisecond, // 500ms latency bucket
|
|
LatencyBucket1s: 1 * time.Second, // 1s latency bucket
|
|
LatencyBucket2s: 2 * time.Second, // 2s latency bucket
|
|
|
|
// Batch Audio Processing Configuration
|
|
MinBatchSizeForThreadPinning: 5, // Minimum batch size to pin thread
|
|
|
|
// Performance Configuration Flags - Production optimizations
|
|
|
|
}
|
|
}
|
|
|
|
// Global configuration instance
|
|
var Config = DefaultAudioConfig()
|
|
|
|
// UpdateConfig allows runtime configuration updates
|
|
func UpdateConfig(newConfig *AudioConfigConstants) {
|
|
// Validate the new configuration before applying it
|
|
if err := ValidateAudioConfigConstants(newConfig); err != nil {
|
|
// Log validation error and keep current configuration
|
|
logger := logging.GetDefaultLogger().With().Str("component", "AudioConfig").Logger()
|
|
logger.Error().Err(err).Msg("Configuration validation failed, keeping current configuration")
|
|
return
|
|
}
|
|
|
|
Config = newConfig
|
|
logger := logging.GetDefaultLogger().With().Str("component", "AudioConfig").Logger()
|
|
logger.Info().Msg("Audio configuration updated successfully")
|
|
}
|
|
|
|
// GetConfig returns the current configuration
|
|
func GetConfig() *AudioConfigConstants {
|
|
return Config
|
|
}
|