# TiviLux v0.4.57 / Build 73 — Live TV lifecycle patch

Baseline: the supplied `TiviLux-Native-v0.4.56-BUILD72-LIVE-CONNECTION-FIX.zip`.

## Findings and limits

The exact cause of the reported XUI rows is **not proven** from source alone. Build 72 already performs `stop()` and `clearMediaItems()` before assigning a replacement source. Its ViewModel resolves live URLs synchronously, and ordinary Live TV has one shared ExoPlayer. There is no evidence here of four normal Live TV player instances or four asynchronous URL-resolution jobs. A panel row does not establish continuing media transfer.

Concrete defects/gaps found:

- Build 72 inserts a fixed 180 ms delay when replacing different live URLs. This is not a completion signal for Media3/network teardown and violates the requested no-delay constraint.
- The normal live session has no cancellation-finally source cleanup. Navigation cleanup lives separately in the application, while its recovery loop merely checks foreground state.
- ON_STOP only pauses the shared live player. Pausing can retain loaded media/loading resources until later disposal; HOME normally finishes the Activity, but not every ON_STOP immediately destroys it.
- Its URL-bound `Player.Listener` marks failure without checking the originating media session. Rebinding the listener alone does not identify a queued old-source event.

This patch addresses these lifecycle gaps. It does **not** prove that lingering XUI rows will disappear immediately or that blocked underlying HTTP reads finish synchronously. Media3 owns loader/network teardown. Confirm actual old-stream byte activity on a device before calling the reported connection issue resolved.

## Changed source files

Paths are relative to the project root.

| File | Change |
| --- | --- |
| `app/src/main/java/tv/tivilux/nativeplayer/ui/components/LivePlaybackSession.kt` | Removes the 180 ms delay; binds the existing tune/recovery coroutine to STARTED lifecycle; rethrows cancellation; tags each source attempt with a session/attempt media ID; filters analytics errors by the event timeline and current media ID; cancels and cleans up the owned source; prevents an older session's finalizer from stopping a newer item. Also bounds initial fallback by the actual current candidate index. |
| `app/src/main/java/tv/tivilux/nativeplayer/ui/TiviLuxApp.kt` | ON_STOP sets Live TV playWhenReady false and stops/clears its media instead of only pausing. VOD behavior remains unchanged. |
| `app/build.gradle.kts` | Only versionName 0.4.56 → 0.4.57 and versionCode 72 → 73. |

Other changed existing files: `latest.json` and `updates/latest.json` (release metadata).

Added files: `latest-v0.4.57.json`, this audit report, and `CHANGES-v0.4.57.patch` (exact baseline diff). Historical version manifests and release notes are retained.

## Cancellation and ownership

The existing `LaunchedEffect(player, url, autoPlay, lifecycle)` remains the single normal Live TV tune/recovery job. Compose cancels it when its keys change or its video layer leaves composition. The lifecycle child is cancelled at ON_STOP. It restarts for the latest composed URL if the same Activity returns; explicit HOME retains the existing finish-and-remove-task behavior.

All player mutations run on the existing Compose/main application thread. Cancellation checks occur before preparing sources and after the existing recovery suspension; cancellation exceptions are rethrown. No detached tune or retry jobs are added.

Each source attempt receives a UUID session plus attempt number as its MediaItem ID. The error callback requires both the event's MediaItem ID and current player's ID to match. The recovery loop exits if the player no longer contains its owned item. Finally removes its analytics listener and stops/clears only an item it still owns, so delayed cancellation cleanup cannot stop a newer session.

Rapid A → B → C → ... → J changes therefore cancel older composed jobs; a cancelled job cannot resume after a suspension and prepare its old channel. Requests occurring in one Compose frame may coalesce directly to the newest URL. ViewModel channel state remains synchronous and unchanged. This has been inspected, not exercised on a device.

## Live entry-point audit

| Entry point | Existing route / result |
| --- | --- |
| TV Guide, categories, locals, Favorites | `selectChannel` → `setLivePlayer` → shared persistent live `VideoSurface(managedLive = true)` |
| Fullscreen DPAD up/down and remote channel keys | MainActivity key routing / PlayerScreen → `tuneAdjacentLive` → same live state and surface |
| Previous channel / quick return | `tunePreviousLive` → `setLivePlayer` |
| Fullscreen channel list | `tuneLiveFromPlayer` → `setLivePlayer` |
| Sports Hub / Game Center / matching feeds / team feeds | Sports screen `onPlayChannel` → `playSportsChannel` → `play(..., SPORTS)` → `setLivePlayer` and the shared live layer |
| Docked preview ↔ fullscreen | Same player and same live surface composition; layout/surface type changes, with no second normal player. Unchanged URL/lifecycle keys retain the session. |
| Leaving Live TV for Home/Settings/Movies/Series/Sports browse | Live surface leaves composition; its job cleans up. Existing application inactive-player stop/clear remains as a second cleanup path. |
| Sign out | Existing ViewModel clears live state and navigation; live surface is removed. |
| Profile changes | Existing selection/navigation rules retained; no new profile-related playback behavior. |
| Network loss/error | Existing recovery policy and candidate ordering retained, with session ownership guards. |

No Favorites layout, scrolling, order, EPG, navigation, or sports catalog code changed.

## MultiView

MultiView retains its four separate players and does not call this normal Live TV session function. It is intentionally exempt from the one-stream rule. A tile uses its own existing `VideoSurface`; replacing that tile's media does not mutate the other players. Empty slots already stop/clear, and leaving MultiView releases its remembered players. Main single-player playback is already stopped/cleared when MultiView is active.

MultiView code is byte-for-byte unchanged. Its existing ON_STOP handler pauses tiles and HOME subsequently finishes/releases the Activity. This patch does not change that behavior or claim new instantaneous MultiView background teardown. MultiView exit, tile replacement, and HOME require device validation.

## Playback preservation

Verified against every original archive entry:

- The entire `LuxComponents.kt` player factory is byte-for-byte unchanged: Media3 1.5.1, buffer durations, LoadControl, memory budget use, decoder fallback, renderers, connect/read timeouts, redirects, HTTP factory, and surface behavior.
- `PlaybackBufferBudget.java`, `LiveRecoveryPolicy.java`, and `XtreamRepository.kt` are byte-for-byte unchanged.
- HLS preference, HLS/TS URL generation, candidate order, and the recovery action decision block are unchanged. Only ownership/cancellation surrounds those decisions.
- HTTP keep-alive is not disabled; no Connection: close header is added. The application HTTP configuration is unchanged.
- No channel-switch wait is added. The old 180 ms wait is removed. The pre-existing foreground wait and one-second recovery sampling remain; there is no additional cleanup timer, poller, watchdog, or healthy-stream restart loop.
- No player release/recreation on channel changes is added. The existing shared player remains.
- Package/application ID, dependencies, signing/build configuration apart from version, branding, and updater endpoint are unchanged.

A healthy foreground stream follows the existing recovery policy. Long-duration playback settings are preserved in source; absence of buffering/jitter and switching speed have not been measured after this patch.

## Verification performed

- Compared all existing files against the supplied ZIP: 218 files identical; exactly two Kotlin source files, the build version file, and two manifests changed.
- Verified the protected playback factory, recovery policy, buffer-budget, URL-generation, ViewModel, MainActivity and MultiView files are identical.
- Compared the recovery action block after normalizing indentation/local variable naming: unchanged.
- Checked version/manifest agreement and ZIP integrity.

No Android SDK, Gradle executable/wrapper, or cached Android toolchain was available. **No APK compilation, instrumentation test, device playback test, packet capture, XUI inspection, or long-duration test was performed.** This is a source project for the existing builder, not a compiled or device-validated APK.

## Required device acceptance checks

All remain pending: A→B actual byte teardown; rapid A→J with no stale reconnect; Guide; Favorites and scrolling; fullscreen channel up/down; Sports feed handoff; leaving Live TV; HOME/background and return; four-tile MultiView switch/exit; extended single-channel playback.

Use a test account/device whose other streams are known. Compare old connection byte activity/logs, not only row presence. Confirm J alone continues receiving media after rapid tuning, allowing server display expiration to differ from actual client teardown. Compare startup, channel changes and long playback with Build 72. Do not describe the patch as a confirmed production fix until these checks pass.

## Build and updater

- Version: **0.4.57**
- Version code: **73**
- APK filename after building: **TiviLux-Native-v0.4.57.apk**
- APK URL: `https://play.theappdrop.store/tivilux-updates/TiviLux-Native-v0.4.57.apk`
- `required`: false
- `sha256`: empty, as in the baseline; the actual APK hash cannot be known before building.

The separate latest.json matches the copies in the project. The APK has not been built or uploaded, and the updater manifest has not been published. Build with the existing package/signing setup, test, upload the APK at the matching URL, then publish latest.json.

API reference used: https://developer.android.com/reference/androidx/media3/common/Player (stop releases loaded media/resources while retaining the reusable player); https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsListener.EventTime (event timeline identity). The project's Media3 dependency remains 1.5.1.
