Skip to content

Instantly share code, notes, and snippets.

@spinitron
Last active April 16, 2020 18:50
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save spinitron/1950987cf8deb1013a350a698268e6e0 to your computer and use it in GitHub Desktop.
<?php
/*
* Reverse proxy to terminate HTTP and forward a "Now playing" update to an RDS via UDP.
*
* Use this to push metadata to an RDS encoder. Run the script on an HTTPS server that can be
* reached from the Internet, e.g. in your DMZ or on a hardened server, that can itself reach
* the RDS encoder over a secure private LAN, e.g. on a different network interface from the
* web server.
*
* Send the Now Playing message in the "text" query parameter, e.g. configure a Spinitron
* metadata push template like this
*
* https://yourserver.foo/path/to/RDS-proxy.php?text=%an% - %sn%
*
* This script has no client authentication. You could check $_SERVER['REMOTE_ADDR'] for
* Spinitron's addresses. Since you are using HTTPS, you could add a secret key in a query
* parameter, e.g. set the push template
*
* https://example.foo/script/path.php?key=my-secret-key&text=%an% - %sn%
*
* and check that $_GET['key'] === 'my-secret-key'. Or you could check basic auth and set
* username and password in Spinitron. There are many ways.
*/
// RDS network coordinates. You could send these from Spinitron to save configuring them here.
$rdsAddress = '127.0.0.1';
$rdsPort = 5001;
// Text to display on the car radio. Inthis example it's sent in the "text" URI query param.
$displayText = $_GET['text'];
// Convert text encoding if needed. Spinitron sends UTF-8. Idk if the RDS encoder expects a
// specific encoding or if it is transparent, in which case what car radios expect.
$displayText = iconv('UTF-8', 'CP1252//TRANSLIT', $displayText);
// Form the commands to send to the RDS terminal server. We could also truncate over-long messages
// but I think they do no harm. How to terminate the command? CR or LF, or both, what order, how many?
$message = "DPSTEXT=$displayText\r\n";
// Make sure strlen() returns the byte length.
ini_set('mbstring.func_overload', 0);
$sentBytes = socket_sendto(socket_create(AF_INET, SOCK_DGRAM, SOL_UDP),
$message, strlen($message), 0, $rdsAddress, $rdsPort);
echo "Sent $sentBytes bytes to udp:$rdsAddress:$rdsPort\r\n" . addcslashes($message, "\x00..\x1f\x7f") . "\r\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment