Files
XC_VM/tests/Unit/FanoutClientTest.php
T
Divarion_D 401e2307e4 feat(streaming): P2/A3 — route non-proxy live viewers through the daemon
Completes Phase A (ADR 0003): non-proxy live streams now both feed the
daemon (A2 tee) and are SERVED by it. At live.php's non-proxy delivery
point, when the daemon is reachable and FanoutClient::isStreamFed($id)
(control GET /streams/<id> → has_data), hand the byte path to nginx→daemon
via X-Accel-Redirect: /xc_fanout/<id> — like proxy mode — instead of
pinning the worker in the per-viewer .ts chase-read. Daemon down / not fed
⇒ the legacy chase-read runs (the reachability rollback).

The tee feeds the daemon from the same ffmpeg that writes the on-disk HLS,
so by delivery time (playlist ready) the daemon has data. Ingest-fed
streams report running:false (no puller), so the gate is has_data.

Box-validated: 6 concurrent non-proxy viewers on stream 569 all served by
the daemon (daemon_conns=6) with php-fpm workers flat (not 1-per-viewer),
valid TS. PHPStan clean, 10 FanoutClient tests.

ADR 0003: A3 done; added a ⚠️ TODO that LLOD v3 (LlodCommand, streams.llod=2)
has no daemon path (separate PHP segmenter) and must be integrated before
Phase D — llod=0/1 already covered via buildLive+tee.

Refs ADR 0003 (Phase A).
2026-08-16 18:37:42 +03:00

94 lines
3.4 KiB
PHP

<?php
use XcVm\Streaming\Fanout\FanoutClient;
use PHPUnit\Framework\TestCase;
/**
* @covers \XcVm\Streaming\Fanout\FanoutClient
*
* Exercises the pure source-config builder that feeds the xc_fanout daemon's
* control API (ADR 0002, P2). buildSource() mirrors ProxyCommand's argument
* extraction; the output shape must match the daemon's streamConfig JSON
* (urls/ua/proxy/cookie/ffmpeg).
*/
final class FanoutClientTest extends TestCase {
public function testBuildSourceDecodesStreamSourceArray() {
$row = ['stream_source' => json_encode(['http://a/1.ts', 'http://b/2.m3u8'])];
$src = FanoutClient::buildSource($row, []);
$this->assertSame(['http://a/1.ts', 'http://b/2.m3u8'], $src['urls']);
$this->assertArrayHasKey('ua', $src);
$this->assertArrayHasKey('proxy', $src);
$this->assertArrayHasKey('cookie', $src);
$this->assertArrayHasKey('ffmpeg', $src);
}
public function testBuildSourceTrimsAndDropsEmptyUrls() {
$row = ['stream_source' => json_encode([' http://a/1.ts ', '', ' '])];
$src = FanoutClient::buildSource($row, []);
$this->assertSame(['http://a/1.ts'], $src['urls']);
}
public function testBuildSourceDefaultsUserAgentWhenArgumentMissing() {
$row = ['stream_source' => json_encode(['http://a/1.ts'])];
$src = FanoutClient::buildSource($row, []);
$this->assertSame('Mozilla/5.0', $src['ua']);
$this->assertSame('', $src['proxy']);
$this->assertSame('', $src['cookie']);
}
public function testBuildSourceFallsBackToArgumentDefaultUserAgent() {
$row = ['stream_source' => json_encode(['http://a/1.ts'])];
$args = ['user_agent' => ['value' => '', 'argument_default_value' => 'VLC/3.0']];
$src = FanoutClient::buildSource($row, $args);
$this->assertSame('VLC/3.0', $src['ua']);
}
public function testBuildSourcePrefersExplicitUserAgentValue() {
$row = ['stream_source' => json_encode(['http://a/1.ts'])];
$args = ['user_agent' => ['value' => 'MyPlayer/1.0', 'argument_default_value' => 'VLC/3.0']];
$src = FanoutClient::buildSource($row, $args);
$this->assertSame('MyPlayer/1.0', $src['ua']);
}
public function testBuildSourceExtractsProxyAndCookie() {
$row = ['stream_source' => json_encode(['http://a/1.ts'])];
$args = [
'proxy' => ['value' => '1.2.3.4:8080'],
'cookie' => ['value' => 'sid=xyz'],
];
$src = FanoutClient::buildSource($row, $args);
$this->assertSame('1.2.3.4:8080', $src['proxy']);
$this->assertSame('sid=xyz', $src['cookie']);
}
public function testBuildSourceHandlesMissingOrInvalidStreamSource() {
$this->assertSame([], FanoutClient::buildSource([], [])['urls']);
$this->assertSame([], FanoutClient::buildSource(['stream_source' => ''], [])['urls']);
$this->assertSame([], FanoutClient::buildSource(['stream_source' => 'not-json'], [])['urls']);
}
public function testRegisterRejectsEmptyUrlsWithoutTouchingDaemon() {
// No daemon is running in tests; empty urls must short-circuit to false
// before any socket call is attempted.
$this->assertFalse(FanoutClient::register(42, ['urls' => []]));
}
public function testRegisterIngestReturnsNullWithoutDaemon() {
// FANOUT_CTL_SOCK is not defined in the unit env, so registerIngest must
// fail closed (null) — the caller then launches ffmpeg legacy-only.
$this->assertNull(FanoutClient::registerIngest(42));
}
public function testIsStreamFedFalseWithoutDaemon() {
// No daemon socket in the unit env → not fed → live.php uses the legacy feed.
$this->assertFalse(FanoutClient::isStreamFed(42));
}
}