I want to synchronize media playback on multiple devices. All players have the media files stored locally so no streaming should be involved.
My current implementation is using UDP multicast where the MASTER sends out its current playback position and the SLAVE checks if its own position is within acceptable range (+/- 50ms). If not, the slave adjusts the playback speed (0.9x or 1.1x) to catch up until it's again within range. If it's too far off it seeks to the MASTERs position.
This works fine, and I also had a proof of concept working with HTML5 video and syncing via WebSockets.
The master/slave roles are now set manually in the client's configuration but I want the clients to auto-negotiate their respective roles.
TL;DR;
How can I make some auto-negotiation of which client is the master? If a master is disconnected, all other clients should continue playing and one of them should assume the master role and start sending its playback position.
I have experimented with the following protocol:
- At startup, the client sends PING
- A master will reply to each PING message with a PONG
- If no PONG is received within 2 seconds, the client becomes MASTER
The problem with this solution is that multiple clients may assume the MASTER role simultaneously.