mirror of
https://github.com/Vateron-Media/XC_VM.git
synced 2026-10-03 20:02:29 +02:00
With fanout_supervise on, StreamProcess::startMonitor() no longer spawns a PHP watchdog. It builds the stream's commands and hands them to the daemon's supervisor (PUT /monitor/<id>), which starts, watches and restarts them: failover, priority backup, forced source, stalled output, audio loss, frame-rate drop and scheduled restart. PHP still builds every command and makes every database write. A copy-only live stream's command is the daemon's native remuxer, `xc_fanout remux`, composed by the new buildNativeLive() exactly as buildLive() composes an ffmpeg line. It reads the source natively and writes the same on-disk HLS and daemon feed as the ffmpeg -f tee output, with no ffmpeg process. fanout_source_backend decides: auto = remuxer with the ffmpeg command as fallback_cmd (taken when the remuxer exits 3, "cannot serve this source": fMP4 or encrypted HLS, rtmp, no keyframes), native = remuxer only, ffmpeg = ffmpeg only. Eligibility is explicit (isNativeEligible / isNativeSource): no transcode, custom ffmpeg, custom map, RTMP output, external push, timestamp repair, read-native or forced input codec; http(s)/udp/rtp sources only. The rest of the panel learns who owns the producer: - superviseStream() asks the daemon first and touches nothing unless it is accepting; without a restart it adopts a running encoder, so cron:streams moves PHP-monitored streams over with no blip. A producer the daemon cannot adopt (PHP LLOD, PHP loopback) is replaced, never left beside the new one. The row is marked watched before the hand-over, so the reconcile cannot release a stream mid-start. - reconcileSupervised() copies the daemon's state (status, pid, source, codecs, resolution, measured bitrate) into streams_servers: every cron:streams pass and every 5 s from the signals daemon. Supervised streams whose row is gone or stopped are released. - stopStream() and the on-demand reaper release before killing anything; killing the producer first is what the supervisor restarts. - isWatched() replaces bare isMonitorAlive() checks in live.php, admin/live.php, rtmp.php and cron:streams: a supervised stream's monitor_pid is the daemon's. MonitorCommand stands down for supervised streams; startMonitor() releases one before falling back to PHP, so turning supervision off does bring streams back on their next restart. - force_stream goes through the daemon for a supervised stream (the .force file is only read by the PHP monitor). - ProcessManager::isStreamRunning() recognises the remuxer, so the archive, thumbnail and delay workers follow it like ffmpeg. - A supervised loopback child tees into the daemon (the supervisor judges a stream by the bytes it receives); legacy loopback is unchanged. Delay streams, created channels and yt-dlp platform sources stay on the PHP monitor. A daemon without /monitors/state (older than this) is never handed a stream, so the panel is safe against an un-upgraded node. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012QL93N6dzGkmKoQgA4oh16
181 lines
7.6 KiB
PHP
181 lines
7.6 KiB
PHP
<?php
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use XcVm\Domain\Stream\StreamProcess;
|
|
|
|
/**
|
|
* Characterisation tests for StreamProcess::buildLive() — the pure live ffmpeg
|
|
* command assembler extracted from startStream. These lock the string structure
|
|
* per branch (simple / custom_ffmpeg / loopback / delay / rtmp) and, crucially,
|
|
* assert no unresolved {TOKEN} survives — the most likely extraction defect.
|
|
*
|
|
* The authoritative equivalence oracle is the shadow-diff wired into startStream
|
|
* (both strings computed on real traffic, divergence logged); these fixtures
|
|
* guard against future drift.
|
|
*/
|
|
final class StreamProcessBuildLiveTest extends TestCase {
|
|
|
|
public static function setUpBeforeClass(): void {
|
|
foreach ([
|
|
'SERVER_ID' => 1,
|
|
'STREAMS_PATH' => '/tmp/xcvm-test-streams/',
|
|
'DELAY_PATH' => '/tmp/xcvm-test-delay/',
|
|
'FFMPEG_BIN_40' => '/bin/ffmpeg40',
|
|
'FFPROBE_BIN_40' => '/bin/ffprobe40',
|
|
] as $k => $v) {
|
|
if (!defined($k)) {
|
|
define($k, $v);
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Base $data for a plain live restream; override per test. */
|
|
private function data(array $overrides = []): array {
|
|
$base = [
|
|
'stream' => [
|
|
'stream_info' => [
|
|
'custom_ffmpeg' => '',
|
|
'stream_all' => 0,
|
|
'custom_map' => '',
|
|
'type_key' => 'live_streams',
|
|
'gen_timestamps' => 0,
|
|
'read_native' => 0,
|
|
'enable_transcode' => 0,
|
|
'transcode_profile_id' => 0,
|
|
'transcode_attributes' => '[]',
|
|
'profile_options' => '[]',
|
|
'delay_minutes' => 0,
|
|
'rtmp_output' => 0,
|
|
],
|
|
'server_info' => ['parent_id' => 0, 'server_id' => 1],
|
|
'stream_arguments' => [],
|
|
],
|
|
'settings' => [
|
|
'ffmpeg_warnings' => 0,
|
|
'read_native_hls' => 0,
|
|
'dts_legacy_ffmpeg' => 0,
|
|
'ignore_keyframes' => 0,
|
|
'live_streaming_pass' => 'secret',
|
|
],
|
|
'servers' => [1 => ['rtmp_port' => 1935]],
|
|
'streamID' => 42,
|
|
'streamSource' => 'http://src.example/live.ts',
|
|
'fetchOptions' => '',
|
|
'ffprobe' => ['container' => 'mpegts', 'codecs' => ['video' => ['codec_name' => 'h264'], 'audio' => ['codec_name' => 'aac']]],
|
|
'protocol' => 'http',
|
|
'source' => 'http://src.example/live.ts',
|
|
'segmentSettings' => ['seg_time' => 6, 'seg_list_size' => 8, 'seg_delete_threshold' => 4],
|
|
'externalPush' => [],
|
|
'probesize' => 1000000,
|
|
'analyseDuration' => 500000,
|
|
'llod' => false,
|
|
'loopback' => false,
|
|
'segmentStart' => 0,
|
|
'delayActive' => false,
|
|
'ffmpegCpu' => '/bin/ffmpeg',
|
|
'ffmpegGpu' => '/bin/ffmpeg-gpu',
|
|
];
|
|
// shallow-merge stream_info / settings so tests can tweak single keys
|
|
if (isset($overrides['stream_info'])) {
|
|
$base['stream']['stream_info'] = array_merge($base['stream']['stream_info'], $overrides['stream_info']);
|
|
unset($overrides['stream_info']);
|
|
}
|
|
if (isset($overrides['server_info'])) {
|
|
$base['stream']['server_info'] = array_merge($base['stream']['server_info'], $overrides['server_info']);
|
|
unset($overrides['server_info']);
|
|
}
|
|
return array_merge($base, $overrides);
|
|
}
|
|
|
|
private function build(array $overrides = []): string {
|
|
$m = new ReflectionMethod(StreamProcess::class, 'buildLive');
|
|
$m->setAccessible(true);
|
|
return $m->invoke(null, $this->data($overrides));
|
|
}
|
|
|
|
// ── the key invariant: every placeholder is resolved ───────
|
|
|
|
public function testNoUnresolvedPlaceholderRemains(): void {
|
|
foreach ([
|
|
[], // simple
|
|
['loopback' => true], // loopback
|
|
['delayActive' => true, 'segmentStart' => 3, 'stream_info' => ['delay_minutes' => 5]],
|
|
['stream_info' => ['rtmp_output' => 1]], // rtmp
|
|
['stream_info' => ['custom_ffmpeg' => '-i in -c copy out']],
|
|
] as $ov) {
|
|
$out = $this->build($ov);
|
|
$this->assertStringNotContainsString('{', $out, 'unresolved placeholder in: ' . $out);
|
|
}
|
|
}
|
|
|
|
// ── simple live ────────────────────────────────────────────
|
|
|
|
public function testSimpleLiveStructure(): void {
|
|
$out = $this->build();
|
|
$this->assertStringStartsWith('/bin/ffmpeg ', $out, 'cpu bin (no gpu)');
|
|
$this->assertStringContainsString('-progress "' . STREAMS_PATH . '42_.progress"', $out);
|
|
$this->assertStringContainsString("-i 'http://src.example/live.ts'", $out);
|
|
$this->assertStringContainsString(STREAMS_PATH . '42_%d.ts', $out, 'hls segments');
|
|
$this->assertStringContainsString('>/dev/null 2>>' . STREAMS_PATH . '42.errors', $out);
|
|
$this->assertStringContainsString('echo $! > ' . STREAMS_PATH . '42_.pid', $out);
|
|
}
|
|
|
|
// ── supervised (launched by the fanout daemon) ─────────────
|
|
|
|
/** The daemon is the parent: the shell's redirect/background/pid tail must go. */
|
|
public function testSupervisedOmitsTheLaunchTail(): void {
|
|
$out = $this->build(['supervised' => true]);
|
|
$this->assertStringNotContainsString('>/dev/null', $out);
|
|
$this->assertStringNotContainsString('echo $!', $out);
|
|
$this->assertStringNotContainsString(' & ', $out);
|
|
$this->assertStringContainsString(STREAMS_PATH . '42_.m3u8', $out, 'still names its playlist (adopt_match)');
|
|
}
|
|
|
|
/** A supervised loopback feeds the daemon, which confirms and judges it by those bytes. */
|
|
public function testSupervisedLoopbackTeesIntoTheDaemon(): void {
|
|
$out = $this->build(['loopback' => true, 'ingestSock' => '/run/ingest/42.sock', 'supervised' => true]);
|
|
$this->assertStringContainsString('-f tee', $out);
|
|
$this->assertStringContainsString('unix:/run/ingest/42.sock', $out);
|
|
$this->assertStringContainsString('-map 0 -copy_unknown', $out);
|
|
}
|
|
|
|
/** The legacy path registers no ingest for loopback and must stay on-disk only. */
|
|
public function testLegacyLoopbackStaysOnDiskOnly(): void {
|
|
$this->assertStringNotContainsString('-f tee', $this->build(['loopback' => true]));
|
|
}
|
|
|
|
// ── custom_ffmpeg branch ───────────────────────────────────
|
|
|
|
public function testCustomFfmpegBypassesTemplate(): void {
|
|
$out = $this->build(['stream_info' => ['custom_ffmpeg' => '-i INPUT -c:v libx264 OUTPUT']]);
|
|
$this->assertStringContainsString('-i INPUT -c:v libx264 OUTPUT', $out);
|
|
// custom path emits neither gen_pts input flags nor a fetch-options slot
|
|
$this->assertStringNotContainsString('-thread_queue_size', $out);
|
|
}
|
|
|
|
// ── loopback ───────────────────────────────────────────────
|
|
|
|
public function testLoopbackHasNoReconnectAndNoLlod(): void {
|
|
$out = $this->build(['loopback' => true, 'llod' => true]);
|
|
$this->assertStringNotContainsString('-reconnect', $out, 'loopback source is not http-reconnect');
|
|
$this->assertStringNotContainsString('-tune zerolatency', $out, 'LLOD options suppressed on loopback');
|
|
}
|
|
|
|
// ── delay resume ───────────────────────────────────────────
|
|
|
|
public function testDelayUsesDelayPathAndStartNumber(): void {
|
|
$out = $this->build(['delayActive' => true, 'segmentStart' => 3, 'stream_info' => ['delay_minutes' => 5]]);
|
|
$this->assertStringContainsString('-start_number 3', $out);
|
|
$this->assertStringContainsString(DELAY_PATH . '42_%d.ts', $out);
|
|
$this->assertStringContainsString('-f hls', $out);
|
|
}
|
|
|
|
// ── rtmp output ────────────────────────────────────────────
|
|
|
|
public function testRtmpOutputAppendsFlvTarget(): void {
|
|
$out = $this->build(['stream_info' => ['rtmp_output' => 1]]);
|
|
$this->assertStringContainsString('rtmp://127.0.0.1:1935/live/42?password=secret', $out);
|
|
$this->assertStringContainsString('-f flv', $out);
|
|
}
|
|
}
|