Files
LlamaCasty/Services/StreamRecovery.cs
T
gramps 72cb3ad55a feat(stream): retry a dropped push instead of ending the session (TASK 32·1)
A dead encoder used to run the whole stop path, so a one-second blip ended the broadcast. Now a dropped
push is relaunched against the same ingest while the broadcast is still alive.

- `Services/StreamRetryPolicy.cs` — immediate, +1 s, +2 s, +3 s. Minimum waits on purpose (creator
  ruling): YouTube's grace is short, so every second not pushing is grace spent.
- `Services/StreamRecovery.cs` — the loop, with the audio pipe recreated before every attempt (its
  server is single-instance and unusable after the old client dies, so a naive relaunch fails on a
  missing input). After a push comes up it asks the API whether the broadcast survived: "complete"
  ends recovery as `BroadcastComplete` instead of showing a live sign over a dead stream; an API
  hiccup counts as recovered rather than ending a healthy session.
- `FramePump.EncoderFailed` — a dead push is recoverable; a compositor fault still goes through
  `Failed` and ends the session, because retrying it would loop.
- `MainViewModel.Recovery.cs` — wires the steps, reports each episode into the left panel alert stack
  (one row per episode, updated in place, log range painted), and cancels on Stop.
- Alert stack polish: a rule and a gap now separate the stack from the scene layers (they read as one
  list before), and the licence indicator uses the creator's wording — `STEAM ID NOT PRESENT` /
  `Steam ID Limit: 30fps` / `Click for More Info`, becoming `STEAM ID PRESENT` / `60fps` with a close
  control once owned.

Tests: `StreamRecoveryTests` (schedule, recover on attempt 2 with the pipe restarted each time,
complete-broadcast honesty, unknown liveness, give-up, cancel, audio-restart fault),
`FramePumpTests` now pin that process death raises `EncoderFailed` and not `Failed`.
FAST gate: 538 passed (one documented pre-existing audio failure), 0 warnings.
2026-11-05 12:09:38 -08:00

151 lines
6.0 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
namespace ytLive.Services;
/// <summary>What YouTube says about the broadcast once a push is back.</summary>
public enum BroadcastLiveness
{
/// <summary>Could not tell (no token, network, API hiccup) — treat as recovered.</summary>
Unknown,
/// <summary>The broadcast is still live/ready: the push landed and the show continues.</summary>
Live,
/// <summary>The broadcast is complete. It cannot be resumed; the push is talking to a dead ingest.</summary>
Complete,
}
/// <summary>Everything the recovery loop needs from the app, as delegates so it can be tested
/// without ffmpeg, without a network, and without a window.</summary>
public sealed class StreamRecoverySteps
{
/// <summary>Recreates the audio pipe server. The old one is unusable after its client died
/// (single-instance server), and the encoder cannot start without it.</summary>
public required Func<CancellationToken, Task> RestartAudio { get; init; }
/// <summary>Starts the push. True when the encoder came up.</summary>
public required Func<CancellationToken, Task<bool>> StartPush { get; init; }
/// <summary>Asks YouTube whether the broadcast survived. Only called after a push starts.</summary>
public required Func<CancellationToken, Task<BroadcastLiveness>> CheckBroadcast { get; init; }
/// <summary>State for the alert stack: attempt numbers and the outcome.</summary>
public required Action<string> Report { get; init; }
/// <summary>The record: one line per attempt, into the app log.</summary>
public required Action<string> Log { get; init; }
public Func<TimeSpan, CancellationToken, Task> Delay { get; init; }
= (delay, ct) => delay <= TimeSpan.Zero ? Task.CompletedTask : Task.Delay(delay, ct);
}
public enum RecoveryOutcome
{
Recovered,
GaveUp,
Cancelled,
BroadcastComplete,
}
public sealed record RecoveryResult(RecoveryOutcome Outcome, int Attempts, TimeSpan Elapsed);
/// <summary>
/// TASK 32·1 — the blip retry. When the encoder dies while the session is live, relaunch the same push
/// against the same ingest, restarting the audio pipe each time (its server is single-instance and the
/// dead client leaves it unusable).
/// <para><b>The check that stops us lying:</b> a reconnect can succeed at the socket level after
/// YouTube has already ended the broadcast. So each successful start asks the API whether the broadcast
/// is still there; "complete" ends recovery instead of showing a live sign over a dead stream.</para>
/// </summary>
public sealed class StreamRecovery
{
private readonly StreamRetryPolicy _policy;
private readonly StreamRecoverySteps _steps;
public StreamRecovery(StreamRetryPolicy policy, StreamRecoverySteps steps)
{
_policy = policy;
_steps = steps;
}
public async Task<RecoveryResult> RunAsync(CancellationToken cancellationToken = default)
{
var clock = System.Diagnostics.Stopwatch.StartNew();
var attempts = 0;
for (var attempt = 0; attempt < _policy.Attempts; attempt++)
{
if (cancellationToken.IsCancellationRequested) return Result(RecoveryOutcome.Cancelled);
attempts = attempt + 1;
var wait = _policy.WaitBefore(attempt);
if (wait > TimeSpan.Zero)
{
try { await _steps.Delay(wait, cancellationToken).ConfigureAwait(false); }
catch (OperationCanceledException) { return Result(RecoveryOutcome.Cancelled); }
}
if (cancellationToken.IsCancellationRequested) return Result(RecoveryOutcome.Cancelled);
_steps.Report($"Stream dropped — reconnecting (attempt {attempts} of {_policy.Attempts})");
_steps.Log($"RECONNECT: attempt {attempts} of {_policy.Attempts} — restarting the audio pipe and the push");
try
{
await _steps.RestartAudio(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_steps.Log($"RECONNECT: audio pipe restart failed on attempt {attempts}: {ex.Message}");
continue;
}
bool started;
try
{
started = await _steps.StartPush(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_steps.Log($"RECONNECT: push restart failed on attempt {attempts}: {ex.Message}");
continue;
}
if (!started)
{
_steps.Log($"RECONNECT: attempt {attempts} of {_policy.Attempts} failed — the encoder did not come up");
continue;
}
var liveness = BroadcastLiveness.Unknown;
try
{
liveness = await _steps.CheckBroadcast(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_steps.Log($"RECONNECT: could not check the broadcast on attempt {attempts}: {ex.Message}");
}
if (liveness == BroadcastLiveness.Complete)
{
_steps.Report("Stream ended — YouTube reports this broadcast complete. It cannot be resumed.");
_steps.Log("RECONNECT: push came back, but the broadcast is complete — cannot resume");
return Result(RecoveryOutcome.BroadcastComplete);
}
_steps.Report($"Stream recovered after {clock.Elapsed.TotalSeconds:0.0} s");
_steps.Log($"RECONNECT: push re-established after {clock.Elapsed.TotalSeconds:0.0} s — still live");
return Result(RecoveryOutcome.Recovered);
}
_steps.Report($"Stream dropped — {_policy.Attempts} attempts failed. The stream is off air.");
_steps.Log($"RECONNECT: gave up after {_policy.Attempts} attempts — stream is off air");
return Result(RecoveryOutcome.GaveUp);
RecoveryResult Result(RecoveryOutcome outcome) => new(outcome, attempts, clock.Elapsed);
}
}