This article covers the details and explanation of my Cast v2 library in PHP. If you just want to go straight to the library, it’s available on github here.
For more information and principles, read on…
What is Cast V2?
A Chromecast dongle enables your TV to become a Smart TV, enabling you to broadcast videos or other information over the network from your tablet, phone, or computer to your TV. For example, for streaming videos.
The original version of the Chromecast used the DIAL protocol for communications, but this was later switched to a combination of mDNS and a custom protocol running over SSL using Google’s protobufs. This second protocol is known as Cast V2.
Each command/packet within the protocol is based of JSON formatted commands/responses. This protocol is proprietary but has been reverse engineered by me (and several others).
Originally I wrote PC software in .NET and Java to control the Chromecast from a PC. Due to improvements in Chrome and elsewhere, this software has proven no longer necessary. I am, however, regularly asked for protocol details. Thus, the primary purpose of the code is to demonstrate how to discover and talk to the Chromecast in an easy to understand language.
The secondary purpose of the code is to allow servers to communicate with a Chromecast. Because other devices, e.g. the Google Home, also use the protocol for their audio – it is possible to talk directly to them in code. This opens up possible applications for, for example, server to Chromecast interaction (e.g. giving alerts of server events). Also of interest is the possibility of control a Chromecast to provide digital signage (although the Chromecast proves to be rather unreliable over long time periods).
State of Development
The discovery portion for finding Chromecast devices is based on my PHP mDNS code. It’s therefore subject to the same timing issues, but seems to work reasonably well.
The Chromecast protocol uses a keepalive method where a PING is sent and a PONG is replied. Because PHP is running linearly, it is impractical to do this properly at good times. Luckily the Chromecast is fairly tolerant.
The audio features of Chromecast devices have been extended as new devices are released (e.g. the Google Home), for example with audio groups. The library/sample code has not yet caught up.
People are, of course, welcome to contribute any enhancements or fixes on github on the project page here.