refactor(streaming): extract startStream probe/failover helpers and default its command variables

Continue breaking down StreamProcess::startStream:
  - extract resolveProbeSettings() (ffprobe/analysis timing) and
    rotateSourcesPastCurrent() (source-failover ordering) as private static
    helpers, with unit tests locking in their behaviour;
  - simplify 15 inverted empty-if/else blocks across the class into a single
    positive condition (no behaviour change).

Also fix the latent undefined-variable bugs the refactor surfaced under PHPStan:
startStream assembles the ffmpeg command from state produced inside the source
failover loop (which closes before the command is built) and inside the
non-custom_ffmpeg branch. On an empty source list, or when a custom ffmpeg line
is used, several variables were read while possibly undefined -- masked at
runtime only by ternary guards and suppressed in the PHPStan baseline. Default
them at the two scopes that own them:
  - loop-scoped, read after the loop: $rSource, $rRealSource, $rStreamSource,
    $rProtocol, $rFFProbeOutput
  - non-custom_ffmpeg-branch-scoped, read in the {MAP}/{GEN_PTS}/{READ_NATIVE}
    substitution and delay sleep: $rMap, $rGenPTS, $rReadNative, $rSleepTime
and drop the 9 now-obsolete entries from phpstan-baseline.neon.

Behaviour is unchanged on the happy path (the loop/branch overwrite the
defaults) and the defaults match the existing runtime ternary-guard results.

Verified: phpstan level 5 green (whole project, entries un-suppressed),
phpunit 369/369, make gates green.
This commit is contained in:
Divarion_D
2026-08-07 20:20:09 +03:00
parent 2abd19b0d5
commit b88ae60dc1
3 changed files with 142 additions and 110 deletions
@@ -133,4 +133,56 @@ final class StreamProcessMovieOutputTest extends TestCase {
$out
);
}
// ── resolveProbeSettings (first startStream extraction) ────
private const PROBE_SETTINGS = ['stream_max_analyze' => 5000000, 'probesize' => 3000000, 'probe_extra_wait' => 5];
public function testProbeSettingsOnDemandLlod(): void {
[$probe, $analyze, $timeout] = $this->call('resolveProbeSettings', 1, 2000000, true, self::PROBE_SETTINGS);
$this->assertSame(2000000, $probe);
$this->assertSame('500000', $analyze);
$this->assertSame(5, $timeout); // intval(500000/1e6)=0 + 5
}
public function testProbeSettingsOnDemandDefaultProbesize(): void {
[$probe, $analyze, $timeout] = $this->call('resolveProbeSettings', 1, 0, false, self::PROBE_SETTINGS);
$this->assertSame(1000000, $probe); // intval(0) ?: 1000000
$this->assertSame('10000000', $analyze);
$this->assertSame(15, $timeout); // intval(1e7/1e6)=10 + 5
}
public function testProbeSettingsGlobalWhenNotOnDemand(): void {
[$probe, $analyze, $timeout] = $this->call('resolveProbeSettings', 0, 999, true, self::PROBE_SETTINGS);
$this->assertSame(3000000, $probe);
$this->assertSame(5000000, $analyze); // abs(intval(...))
$this->assertSame(10, $timeout); // intval(5e6/1e6)=5 + 5
}
// ── rotateSourcesPastCurrent (source failover ordering) ────
public function testPriorityBackupKeepsOrder(): void {
$this->assertSame(['a', 'b', 'c'], $this->call('rotateSourcesPastCurrent', ['a', 'b', 'c'], 1, 'b'));
}
public function testEmptyCurrentKeepsOrder(): void {
$this->assertSame(['a', 'b', 'c'], $this->call('rotateSourcesPastCurrent', ['a', 'b', 'c'], 0, ''));
}
public function testUnknownCurrentKeepsOrder(): void {
$this->assertSame(['a', 'b', 'c'], $this->call('rotateSourcesPastCurrent', ['a', 'b', 'c'], 0, 'zzz'));
}
public function testCurrentInMiddleRotatesTriedToEnd(): void {
// current 'b' (idx 1): 'c','d' lead, tried 'a','b' become fallbacks.
$this->assertSame(['c', 'd', 'a', 'b'], $this->call('rotateSourcesPastCurrent', ['a', 'b', 'c', 'd'], 0, 'b'));
}
public function testCurrentFirstMovesItLast(): void {
$this->assertSame(['b', 'c', 'd', 'a'], $this->call('rotateSourcesPastCurrent', ['a', 'b', 'c', 'd'], 0, 'a'));
}
public function testCurrentLastLeavesOrderUnchanged(): void {
$this->assertSame(['a', 'b', 'c', 'd'], $this->call('rotateSourcesPastCurrent', ['a', 'b', 'c', 'd'], 0, 'd'));
}
}