Creator challenge (2026-11-05): "I really don't think that's going to be a constant — it's going to
depend on so many variables, some that are dark to us." Correct, so the measured-grace constant and
the countdown are dropped from the spec. YouTube's cutoff is server-side and depends on ingest health,
prior stream quality, region and load; a pinned number would be a guess wearing a timer, and it would
be wrong exactly when the creator is staring at it.
What ships instead:
- **Amber RECONNECTING** in the top bar with measured facts only: elapsed seconds and the attempt
number ("0:12 · attempt 2 of 4"). The seconds come from a stopwatch started at the drop.
- **The status poll speeds up while recovering** (30 s → 4 s) and drives the sign; it restores 30 s
when recovery ends.
- **`chatEndedEvent`** — already in the chat feed we poll and previously skipped — now raises a signal
that prompts an immediate status check. It never decides anything: `liveBroadcasts.list`
`lifeCycleStatus` stays authoritative, with `liveStreams.list` health as the push-received signal.
- At `complete`: red OFF AIR (the existing stop path) rather than a pretend-live sign.
Tests: `ChatEndedSignalTests` (the marker is not a chat row, the service raises the signal, and a
complete broadcast ends recovery with the honest message). FAST gate: 543 passed, 0 warnings.
230 lines
8.6 KiB
C#
230 lines
8.6 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using ytLive.Helpers;
|
|
using ytLive.Services;
|
|
using ytLive.Services.Encoder;
|
|
|
|
namespace ytLive.ViewModels;
|
|
|
|
/// <summary>
|
|
/// TASK 32·1 — a dropped push is retried instead of ending the session. The loop itself lives in
|
|
/// <see cref="StreamRecovery"/> (pure, testable); this partial supplies the app-side steps and reports
|
|
/// each episode into the left panel's alert stack.
|
|
/// <para>Deliberately narrow: only the ENCODER dying is recoverable. A compositor fault still goes
|
|
/// through <see cref="OnFramePumpFailed"/> and ends the session, because retrying it would loop.</para>
|
|
/// </summary>
|
|
public partial class MainViewModel
|
|
{
|
|
private CancellationTokenSource? _recoveryCts;
|
|
|
|
/// <summary>True while a push is being relaunched — the top bar shows amber RECONNECTING.</summary>
|
|
public bool IsReconnecting
|
|
{
|
|
get => _isReconnecting;
|
|
private set
|
|
{
|
|
if (!SetProperty(ref _isReconnecting, value)) return;
|
|
RaiseTopBarModes();
|
|
OnPropertyChanged(nameof(ReconnectDetailText));
|
|
ApplyReconnectPollCadence();
|
|
}
|
|
}
|
|
|
|
private bool _isReconnecting;
|
|
|
|
/// <summary>The attempt currently in flight (0 when idle).</summary>
|
|
public int ReconnectAttempt
|
|
{
|
|
get => _reconnectAttempt;
|
|
private set
|
|
{
|
|
if (!SetProperty(ref _reconnectAttempt, value)) return;
|
|
OnPropertyChanged(nameof(ReconnectDetailText));
|
|
}
|
|
}
|
|
|
|
private int _reconnectAttempt;
|
|
|
|
/// <summary>Whole seconds since the drop — measured, never predicted.</summary>
|
|
public int ReconnectElapsedSeconds
|
|
{
|
|
get => _reconnectElapsedSeconds;
|
|
private set
|
|
{
|
|
if (!SetProperty(ref _reconnectElapsedSeconds, value)) return;
|
|
OnPropertyChanged(nameof(ReconnectDetailText));
|
|
}
|
|
}
|
|
|
|
private int _reconnectElapsedSeconds;
|
|
private System.Windows.Threading.DispatcherTimer? _reconnectTick;
|
|
|
|
/// <summary>While recovering, ask YouTube much more often — the status IS the sign, so it has to
|
|
/// keep up with a reconnect that can succeed or die inside a few seconds.</summary>
|
|
private void ApplyReconnectPollCadence()
|
|
{
|
|
if (IsReconnecting)
|
|
{
|
|
_healthPollTimer.Interval = TimeSpan.FromSeconds(4);
|
|
_reconnectTick ??= new System.Windows.Threading.DispatcherTimer
|
|
{
|
|
Interval = TimeSpan.FromSeconds(1),
|
|
};
|
|
_reconnectTick.Tick -= OnReconnectTick;
|
|
_reconnectTick.Tick += OnReconnectTick;
|
|
_reconnectTick.Start();
|
|
}
|
|
else
|
|
{
|
|
_healthPollTimer.Interval = TimeSpan.FromSeconds(30);
|
|
if (_reconnectTick != null)
|
|
{
|
|
_reconnectTick.Stop();
|
|
_reconnectTick.Tick -= OnReconnectTick;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnReconnectTick(object? sender, EventArgs e)
|
|
=> ReconnectElapsedSeconds = (int)Math.Round(_reconnectStarted?.Elapsed.TotalSeconds ?? 0);
|
|
|
|
// Measured from the drop, so the displayed seconds are facts rather than a projection.
|
|
private System.Diagnostics.Stopwatch? _reconnectStarted;
|
|
|
|
private void OnEncoderFailed(object? sender, string message)
|
|
{
|
|
if (!IsLive && !IsRecording) return;
|
|
if (_recoveryCts != null) return; // one recovery at a time
|
|
if (!IsLive) return; // a recording-only session has nothing to relive
|
|
|
|
AppLog.Write($"RECONNECT: encoder process failed: {message}");
|
|
_recoveryCts = new CancellationTokenSource();
|
|
_ = RecoverStreamAsync(message, _recoveryCts.Token);
|
|
}
|
|
|
|
private async Task RecoverStreamAsync(string reason, CancellationToken cancellationToken)
|
|
{
|
|
ReconnectElapsedSeconds = 0;
|
|
_reconnectStarted = System.Diagnostics.Stopwatch.StartNew();
|
|
IsReconnecting = true;
|
|
ReconnectAttempt = 0;
|
|
var dropped = _reconnectStarted;
|
|
|
|
var logFrom = AppLog.SnapshotCurrentLaunch().Count > 0
|
|
? AppLog.SnapshotCurrentLaunch()[^1].Sequence + 1
|
|
: 1;
|
|
|
|
var recovery = new StreamRecovery(
|
|
StreamRetryPolicy.Default,
|
|
new StreamRecoverySteps
|
|
{
|
|
RestartAudio = _ =>
|
|
{
|
|
// The audio pipe's server is single-instance and its client just died, so it has
|
|
// to be recreated before ffmpeg can attach — otherwise the restart fails on a
|
|
// missing input.
|
|
_audioMixer.StopLive();
|
|
_audioMixer.StartLive(EncoderOptions.DefaultAudioPipeName);
|
|
return Task.CompletedTask;
|
|
},
|
|
StartPush = async ct =>
|
|
{
|
|
await _framePump.StartAsync(ct);
|
|
return _framePump.IsRunning;
|
|
},
|
|
CheckBroadcast = async ct =>
|
|
{
|
|
if (_currentBroadcastId == null || _reusableStream?.Id == null)
|
|
return BroadcastLiveness.Unknown;
|
|
var status = await _youtubeStream.GetBroadcastStatusAsync(_currentBroadcastId);
|
|
return status switch
|
|
{
|
|
"complete" => BroadcastLiveness.Complete,
|
|
null => BroadcastLiveness.Unknown,
|
|
_ => BroadcastLiveness.Live,
|
|
};
|
|
},
|
|
Report = text =>
|
|
{
|
|
var progress = text.StartsWith("Stream dropped", StringComparison.Ordinal)
|
|
? ParseAttempt(text)
|
|
: 0;
|
|
if (progress > 0) ReconnectAttempt = progress;
|
|
|
|
var to = AppLog.SnapshotCurrentLaunch() is { Count: > 0 } entries
|
|
? entries[^1].Sequence
|
|
: logFrom;
|
|
PushAttentionAlert(
|
|
"stream-recovery",
|
|
text,
|
|
logFrom,
|
|
Math.Max(logFrom, to));
|
|
},
|
|
Log = line => AppLog.Write(line, AppLogMessageType.Error, AppLogErrorClass.Class2),
|
|
});
|
|
|
|
try
|
|
{
|
|
var result = await recovery.RunAsync(cancellationToken);
|
|
|
|
switch (result.Outcome)
|
|
{
|
|
case RecoveryOutcome.Recovered:
|
|
ReconnectAttempt = result.Attempts;
|
|
AppLog.WriteSuccess(
|
|
$"RECONNECT: recovered on attempt {result.Attempts} after {result.Elapsed.TotalSeconds:0.0} s");
|
|
break;
|
|
|
|
case RecoveryOutcome.Cancelled:
|
|
// The creator stopped the stream mid-recovery: nothing to report.
|
|
break;
|
|
|
|
default:
|
|
// Gave up, or the broadcast was already complete: this session is over. The alert
|
|
// stack keeps the reason; the normal stop path clears pills, audio and recording.
|
|
AppLog.Write("RECONNECT: session ended — the broadcast could not be recovered",
|
|
AppLogMessageType.Error, AppLogErrorClass.Class1);
|
|
StopStream();
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
AppLog.Write(ex, "RECONNECT: recovery loop faulted");
|
|
StopStream();
|
|
}
|
|
finally
|
|
{
|
|
_reconnectStarted = null;
|
|
IsReconnecting = false;
|
|
ReconnectAttempt = 0;
|
|
_recoveryCts?.Dispose();
|
|
_recoveryCts = null;
|
|
}
|
|
}
|
|
|
|
private static int ParseAttempt(string text)
|
|
{
|
|
// "Stream dropped — reconnecting (attempt 2 of 4)"
|
|
var marker = text.IndexOf("attempt ", StringComparison.Ordinal);
|
|
if (marker < 0) return 0;
|
|
var rest = text[(marker + "attempt ".Length)..];
|
|
var space = rest.IndexOf(' ');
|
|
return int.TryParse(space > 0 ? rest[..space] : rest, out var attempt) ? attempt : 0;
|
|
}
|
|
|
|
/// <summary>The creator asked to stop: never race a stop against a relaunch.</summary>
|
|
private void CancelStreamRecovery() => _recoveryCts?.Cancel();
|
|
|
|
/// <summary>YouTube ended the chat: ask the broadcast status now rather than at the next tick.
|
|
/// The API decides — this signal only saves up to 30 s of lying to the creator.</summary>
|
|
private void OnChatEnded()
|
|
{
|
|
if (!IsLive) return;
|
|
AppLog.Write("Chat ended event received — checking the broadcast status now");
|
|
_ = PollHealthAsync();
|
|
}
|
|
}
|