<?phpnamespace App\Cast;/** * The Google Cast protocol (CASTV2) on the wire, written by hand: every message is a protobuf `CastMessage` * (cast_channel.proto of Chromium) preceded by its length on 4 bytes, big-endian. * * message CastMessage { * required ProtocolVersion protocol_version = 1; // 0 = CASTV2_1_0 * required string source_id = 2; // "sender-0" * required string destination_id = 3; // "receiver-0", or an application's transportId * required string namespace = 4; // urn:x-cast:com.google.cast.tp.connection ... * required PayloadType payload_type = 5; // 0 = STRING, 1 = BINARY * optional string payload_utf8 = 6; // JSON * optional bytes payload_binary = 7; * } * * Only small numbers here (lengths < 64 KB, enums): the varints never need 64-bit integers (the browser has 32). */final class CastV2{ public const CONNECTION = 'urn:x-cast:com.google.cast.tp.connection'; public const HEARTBEAT = 'urn:x-cast:com.google.cast.tp.heartbeat'; public const RECEIVER = 'urn:x-cast:com.google.cast.receiver'; public const MEDIA = 'urn:x-cast:com.google.cast.media'; /** WinZik's own messages to its receiver page (the TV screen: song, lyrics, commands) */ public const WINZIK = 'urn:x-cast:net.jumphp.winzik'; /** the Default Media Receiver of Google: plays a media URL, shows its title, artist and picture */ public const DEFAULT_RECEIVER = 'CC1AD845'; /** a frame bigger than this is refused (the protocol's own limit is 64 KB) */ public const MAX_FRAME = 65536; /** * @param array{source?: string, destination?: string, namespace: string, payload?: string, binary?: string} $m */ public static function encode(array $m): string { $out = self::key(1, 0) . self::varint(0) . self::key(2, 2) . self::bytes($m['source'] ?? 'sender-0') . self::key(3, 2) . self::bytes($m['destination'] ?? 'receiver-0') . self::key(4, 2) . self::bytes($m['namespace']); if (isset($m['binary'])) { return $out . self::key(5, 0) . self::varint(1) . self::key(7, 2) . self::bytes($m['binary']); } return $out . self::key(5, 0) . self::varint(0) . self::key(6, 2) . self::bytes($m['payload'] ?? ''); } /** A message with a JSON payload, framed (ready to write on the socket). */ public static function json(string $namespace, array $data, string $destination = 'receiver-0', string $source = 'sender-0'): string { $json = (string)json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); return self::frame(self::encode(['source' => $source, 'destination' => $destination, 'namespace' => $namespace, 'payload' => $json])); } public static function frame(string $message): string { return pack('N', strlen($message)) . $message; } /** * The whole frames at the start of $buffer, taken out of it (what is left waits for more bytes). * @return list<array{source: string, destination: string, namespace: string, payload: string, binary: ?string, version: int}> * @throws \RuntimeException a frame too big or not a CastMessage (the connection is not a Cast device) */ public static function unframe(string &$buffer): array { $out = []; while (strlen($buffer) >= 4) { $length = (int)unpack('N', substr($buffer, 0, 4))[1]; if ($length < 0 || $length > self::MAX_FRAME) { throw new \RuntimeException('trame Cast invalide'); } if (strlen($buffer) < 4 + $length) { break; } $out[] = self::decode(substr($buffer, 4, $length)); $buffer = (string)substr($buffer, 4 + $length); } return $out; } /** * @return array{source: string, destination: string, namespace: string, payload: string, binary: ?string, version: int} * @throws \RuntimeException not a CastMessage */ public static function decode(string $bytes): array { $m = ['source' => '', 'destination' => '', 'namespace' => '', 'payload' => '', 'binary' => null, 'version' => 0]; $i = 0; $n = strlen($bytes); while ($i < $n) { $key = self::readVarint($bytes, $i); $field = $key >> 3; $wire = $key & 7; if ($wire === 0) { $value = self::readVarint($bytes, $i); if ($field === 1) { $m['version'] = $value; } } elseif ($wire === 2) { $length = self::readVarint($bytes, $i); if ($i + $length > $n) { throw new \RuntimeException('message Cast tronqué'); } $value = substr($bytes, $i, $length); $i += $length; match ($field) { 2 => $m['source'] = $value, 3 => $m['destination'] = $value, 4 => $m['namespace'] = $value, 6 => $m['payload'] = $value, 7 => $m['binary'] = $value, default => null, }; } elseif ($wire === 5) { $i += 4; } elseif ($wire === 1) { $i += 8; } else { throw new \RuntimeException('message Cast illisible'); } } if ($i > $n) { throw new \RuntimeException('message Cast tronqué'); } return $m; } private static function key(int $field, int $wire): string { return self::varint($field << 3 | $wire); } public static function varint(int $value): string { $out = ''; while ($value > 0x7F) { $out .= chr(($value & 0x7F) | 0x80); $value >>= 7; } return $out . chr($value); } private static function bytes(string $s): string { return self::varint(strlen($s)) . $s; } private static function readVarint(string $bytes, int &$i): int { $value = 0; $shift = 0; $n = strlen($bytes); while (true) { if ($i >= $n || $shift > 28) { throw new \RuntimeException('message Cast tronqué'); } $b = ord($bytes[$i++]); $value |= ($b & 0x7F) << $shift; if ($b < 0x80) { return $value; } $shift += 7; } }}