# Player Configuration Fixes (2026-05-28)

## Bugs Found (7 total, 5 fixed)
Source: NuvioTV 0.6.21 `PlayerRuntimeControllerInitialization.kt` comparison audit.

### Fixed

#### 1. Audio attributes missing → no TV audio routing
**Before:** ExoPlayer built without `AudioAttributes`  
**After:** 
```kotlin
val audioAttributes = AudioAttributes.Builder()
    .setUsage(C.USAGE_MEDIA)
    .setContentType(C.AUDIO_CONTENT_TYPE_MOVIE)
    .build()
setAudioAttributes(audioAttributes, true)
```
Imports: `androidx.media3.common.AudioAttributes`  
Impact: Enables surround passthrough, audio focus, prevents silent playback on some TV devices.

#### 2. No buffer/LoadControl → stuttering on 4K debrid streams
**Before:** Default ExoPlayer buffer (~12.5 MB, 30s max)  
**After:**
```kotlin
val loadControl = DefaultLoadControl.Builder()
    .setTargetBufferBytes(100 * 1024 * 1024)  // 100 MB
    .setBufferDurationsMs(
        DefaultLoadControl.DEFAULT_MIN_BUFFER_MS,
        70_000,   // max buffer: 70s
        DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS,
        5_000,    // rebuffer: 5s
    )
    .build()
```
Import: `androidx.media3.exoplayer.DefaultLoadControl`

#### 3. Cross-protocol redirects missing on no-headers path
**Before:** Only headers path had `setAllowCrossProtocolRedirects(true)`  
**After:** Both paths in `rebuildAndPlay()` use it. Debrid CDNs (HTTPS→HTTP redirect) were failing.

#### 4. Headers silently dropped from PlaybackPlan
**Before:** `play()` hardcoded `currentHeaders = emptyMap()`  
**After:** `play(url, seekToMs, headers = emptyMap())` — optional headers parameter. Calling code must pass `plan.headers`. Still needs ViewModel-side wiring (`playStream()` should pass `plan.headers`).

#### 5. No cross-protocol redirects in factory
**Before:** `DefaultMediaSourceFactory(context)` without data source factory  
**After:** No-headers path now creates `DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(true)`

### Done (2026-05-29 — 7-source audit session)
- **PlayerErrorClassifier**: 13 failure categories (NETWORK_TRANSIENT, CODEC_INCOMPATIBLE, DRM_RESTRICTED, etc.) — `player/PlayerErrorClassifier.kt`
- **PlayerAudioFocusController**: Android audio focus with 0.2x ducking, gain/loss/transient handling — `player/PlayerAudioFocusController.kt`
- **PlayerEngineFailover**: ExoPlayer → software-decoder fallback on startup codec errors, gated by one-shot flag — `player/PlayerEngineFailover.kt`
- **StreamDeduplicator**: Completed missing types (DedupConfig, MultiGroupBehaviour, DedupMode, deduplicate()), SmartDetect second-pass — `data/source/StreamDeduplicator.kt`
- **DebridBudgetManager**: Per-provider sliding-window rate limiting (6/min, 30/hr) with canConsume/status/timeUntilNextSlotMs — `data/debrid/DebridBudgetManager.kt`

### Remaining (separate sessions)
- **OkHttpDataSource** instead of DefaultHttpDataSource (connection pooling, IPv4FirstDns)
- **Preferred audio languages**: `trackSelector.setParameters(buildUponParameters().setPreferredAudioLanguages("en"))`
- **Frame rate matching**: NuvioTV `FrameRateUtils.matchFrameRateAndWait()`
- **Header re-injection on cross-host redirects** (Authorization dropped by CDN redirects)

## NuvioTV Reference
`/home/rurouni/NuvioTV-0.6.21-beta/app/src/main/java/com/nuvio/tv/core/player/PlayerRuntimeControllerInitialization.kt`
- Lines 229-238: LoadControl configuration
- Lines 242-283: Preferred audio languages + captioning
- Lines 366-370: AudioAttributes
- Also see: `PlayerPlaybackNetworking.kt` for OkHttp-based data source with Authorization re-attachment
