/s, stateFor()), or WinZik's * Cast receiver gets STATE messages over a CastClient and answers REPORT / COMMAND. */ final class TvTarget implements CastTarget { /** seconds without a word from the page: lost */ private const SILENCE = 6.0; /** @var array|null */ private ?array $song = null; private bool $playing = false; private float $position = 0.0; private float $positionAt; private int $posSeq = 0; private ?int $volume = null; public string $notice = ''; /** @var list the songs coming next (shown on the TV) */ public array $next = []; /** the karaoke's mark over the syllable sung on the TV page (LyricsBar::KARAOKE: ball, star, note, bar, off) */ public string $karaoke = 'ball'; /** @var array{state: string, position: ?float, song: int, at: float}|null what the page said last */ private ?array $report = null; /** @var list the TV remote's keys, for CastSession */ private array $commands = []; private bool $dirty = true; private float $sent = 0.0; private bool $closed = false; public function __construct(private ?CastClient $client = null) { $this->positionAt = microtime(true); if ($client !== null) { $client->onMessage = function (array $m): void { if (($m['type'] ?? '') === 'REPORT') { $this->report($m); } elseif (($m['type'] ?? '') === 'COMMAND') { $this->command((string)($m['do'] ?? ''), $m); } }; } } public function viaCast(): bool { return $this->client !== null; } public function load(array $song, float $at, bool $play): void { $this->song = $song; $this->playing = $play; $this->setPosition($at); $this->dirty = true; } public function play(): void { $this->setPosition($this->expected()); $this->playing = true; $this->dirty = true; } public function pause(): void { $this->setPosition($this->expected()); $this->playing = false; $this->dirty = true; } public function seek(float $seconds): void { $this->setPosition($seconds); $this->posSeq++; $this->dirty = true; } public function stop(): void { $this->playing = false; $this->setPosition(0.0); $this->posSeq++; $this->dirty = true; } public function setVolume(int $volume): void { $this->volume = max(0, min(100, $volume)); $this->dirty = true; } public function poll(): void { if ($this->client === null || $this->closed) { return; } $this->client->poll(); $now = microtime(true); if ($this->client->ready() && ($this->dirty || $now - $this->sent > 1.0)) { // the lyrics go with the state until the page says it has the song (a Cast message holds 64 KB at most: cut to fit) $id = (int)($this->song['id'] ?? -1); $state = $this->state(true, $id !== (int)($this->report['song'] ?? -1)); $this->client->send(CastV2::WINZIK, ['type' => 'STATE', 'state' => $state]); $this->dirty = false; $this->sent = $now; } } public function status(): array { if ($this->client !== null && $this->client->closed()) { return ['state' => $this->client->status()['state'], 'position' => null, 'song' => -1]; } $r = $this->report; if ($r === null) { return ['state' => 'connecting', 'position' => null, 'song' => -1]; } if (microtime(true) - $r['at'] > self::SILENCE) { return ['state' => 'lost', 'position' => null, 'song' => $r['song']]; } $p = $r['position']; if ($p !== null && $r['state'] === 'playing') { $p += microtime(true) - $r['at']; } return ['state' => $r['state'] === 'ended' ? 'idle' : $r['state'], 'position' => $p, 'song' => $r['song']]; } public function error(): string { return $this->client?->error() ?? ''; } public function close(): void { $this->closed = true; $this->client?->close(); } /** A browser's question (TvServer): its report in, the state out. @param array $query */ public function stateFor(array $query): array { if (isset($query['st'])) { $this->report($query); } return $this->state(false); } /** @param array $r */ private function report(array $r): void { $this->report = ['state' => (string)($r['st'] ?? 'idle'), 'position' => isset($r['pos']) && is_numeric($r['pos']) ? (float)$r['pos'] : null, 'song' => (int)($r['song'] ?? -1), 'at' => microtime(true)]; } /** @param array $q */ public function command(string $do, array $q = []): void { if (in_array($do, ['toggle', 'play', 'pause', 'next', 'prev', 'seek'], true)) { $this->commands[] = ['do' => $do, 't' => isset($q['t']) && is_numeric($q['t']) ? (float)$q['t'] : null]; } } /** @return list the keys pressed on the TV since the last call */ public function takeCommands(): array { $c = $this->commands; $this->commands = []; return $c; } /** What the page follows. @return array */ public function state(bool $cast, bool $withLyrics = false): array { $song = $this->song; if ($song !== null) { $song = ['id' => (int)$song['id'], 'url' => (string)$song['url'], 'title' => (string)($song['title'] ?? ''), 'artist' => (string)($song['artist'] ?? ''), 'cover' => (string)($song['cover'] ?? ''), 'duration' => (float)($song['duration'] ?? 0), 'lyrics' => $cast ? '' : (string)($song['lyrics'] ?? '')] + ($withLyrics ? ['lyricsData' => self::fit($song['lyricsData'] ?? null)] : []); } return ['song' => $song, 'playing' => $this->playing, 'position' => round($this->expected(), 2), 'posSeq' => $this->posSeq, 'volume' => $this->volume, 'notice' => $this->notice, 'next' => $this->next, 'karaoke' => $this->karaoke]; } /** The lyrics, lines dropped from the end until the message fits in 48 KB. @return array|null */ private static function fit(mixed $lyrics): ?array { if (!is_array($lyrics)) { return null; } while (strlen((string)json_encode($lyrics, JSON_UNESCAPED_UNICODE)) > 49152 && ($lyrics['lines'] ?? []) !== []) { $lyrics['lines'] = array_slice($lyrics['lines'], 0, (int)(count($lyrics['lines']) * 0.8)); } return $lyrics; } private function expected(): float { return $this->position + ($this->playing ? microtime(true) - $this->positionAt : 0.0); } private function setPosition(float $at): void { $this->position = max(0.0, $at); $this->positionAt = microtime(true); } }