winzik/apps/winzik/src/Cast/TvPage.php
David Nurdin 53a818a957 WinZik : caster sur la TV la musique et les paroles (Google Cast, DLNA, écran WinZik dans le navigateur de la TV)
Découverte (SSDP + mDNS), serveur LAN pour la TV, mode streaming (DLNA AVTransport,
Google Cast CASTV2 en PHP pur : Default Media Receiver) et mode interface (page TV
karaoké télécommandée, récepteur Cast avec App ID ou navigateur de la TV + QR code).
Menu « Caster sur la TV… » (CastForm) et page Plus > « Caster sur la TV » du téléphone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018nX3czKgBRS7VAsu58aajS
2026-11-21 06:02:38 +01:00

73 lines
3.2 KiB
PHP

<?php
namespace App\Cast;
/**
* The WinZik TV screen (assets/cast/tv.html): the song, its cover, the lyrics line by line with the syllable sung
* lit (and the translation under a line when there is one), the songs coming next; it plays the song itself
* (<audio>) and follows WinZik, the phone / PC being the remote; the TV's own remote works too (OK, ◀ ▶, ▲ ▼).
*
* One page, two ways to talk to WinZik:
* - 'http': opened in a TV / console / tablet browser from WinZik's LAN server (/tv/<code>): it asks the state
* (/c/<token>/s, with its own position) 3 times a second and sends the remote's keys (/c/<token>/k);
* - 'cast': WinZik's Google Cast receiver (an App ID registered by the user, the page on HTTPS:
* site/cast/receiver.html, made by receiverFile()): the same messages over the Cast connection, namespace
* urn:x-cast:net.jumphp.winzik (STATE from WinZik, REPORT and COMMAND from the page).
*/
final class TvPage
{
public static function template(): string
{
return (string)file_get_contents(__DIR__ . '/../../assets/cast/tv.html');
}
public static function html(string $transport): string
{
$html = self::template();
if ($transport === 'cast') {
$sdk = '<script src="https://www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>';
$js = <<<'JS'
// Google Cast: WinZik's messages over the Cast connection (this page is WinZik's receiver application)
const NS = 'urn:x-cast:net.jumphp.winzik';
const ctx = cast.framework.CastReceiverContext.getInstance();
const transport = {
command(c) { try { ctx.sendCustomMessage(NS, undefined, Object.assign({ type: 'COMMAND' }, c)); } catch (e) {} },
};
ctx.addCustomMessageListener(NS, (ev) => { const d = ev.data || {}; if (d.type === 'STATE') { apply(d.state); } });
setInterval(() => { try { ctx.sendCustomMessage(NS, undefined, Object.assign({ type: 'REPORT' }, report())); } catch (e) {} }, 500);
const options = new cast.framework.CastReceiverOptions();
options.disableIdleTimeout = true;
options.skipPlayersLoad = true;
options.customNamespaces = { [NS]: cast.framework.system.MessageType.JSON };
ctx.start(options);
JS;
} else {
$sdk = '';
$js = <<<'JS'
// a browser of the home network: WinZik's LAN server, asked 3 times a second
const BASE = '{{BASE}}';
let failures = 0;
const transport = {
busy: false,
command(c) { fetch(BASE + '/k?' + new URLSearchParams(c), { cache: 'no-store' }).catch(() => {}); },
tick() {
if (this.busy) { return; }
this.busy = true;
fetch(BASE + '/s?' + new URLSearchParams(report()), { cache: 'no-store' }).then((r) => r.json())
.then((s) => { failures = 0; apply(s); })
.catch(() => { if (++failures > 6) { notice('WinZik ne répond plus (le PC ou le téléphone est-il allumé ?)'); } })
.finally(() => { transport.busy = false; });
},
};
setInterval(() => transport.tick(), 300);
JS;
}
return str_replace(['<!--RECEIVER_SDK-->', '/*TRANSPORT*/'], [$sdk, $js], $html);
}
/** site/cast/receiver.html: the page to host on HTTPS for a Cast App ID (AGENTS.md "Cast"). */
public static function receiverFile(): string
{
return __DIR__ . '/../../site/cast/receiver.html';
}
}