1

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.

ulvesked
  • 429
  • 3
  • 11
  • 1
    Possible duplicate of [How to elect a master node among the nodes running in a cluster?](https://stackoverflow.com/questions/4523185/how-to-elect-a-master-node-among-the-nodes-running-in-a-cluster) – fredrik Oct 09 '19 at 12:06
  • Thank you, I was trying to find the appropriate term but apparently it's Leader Election https://en.wikipedia.org/wiki/Leader_election. Now I've got some more reading to do. – ulvesked Oct 09 '19 at 12:20

1 Answers1

0

When sending a PING, include a randomly generated integer. When replying with a PONG, include that number. After deciding to become a master, repeat the 3 steps but if someone replies with a PONG that is lower than yours, become a slave instead.

Neil
  • 11,059
  • 3
  • 31
  • 56