I'd like to synchronize the playing of a video on multiple clients. One client will give the server a URL to a video which can be streamed and click on play then every client is supposed to start playing the video at the exact same time. And while playing it has to be ensured that the every client is still in sync. If someone suddenly stops playing everyone else has to stop too as soon as possible. (At this point the client that stopped would be behind 1 second or so.) Then when the same person starts playing again everyone else would obviously have to wait a second until the one client caught up and then start playing too.
My first try was to set up a mySQL db and let each client constantly report their current video time to it. The server then calculated if any client in the db is behind and if so, calculated for each client how long they would have to pause to let it catch up.
But that didn't really work too well. It strongly depended on your latency and I wasn't able to get it reliably working with a delay of <300ms (a really good latency was required for that).
So what could I do to improve it? Should I change my setup (javscript<->ajax<->php<->mySQL)?
How can I reliably calculate the travel time of the response the server sends a client? I mean, I can't just assume the client's system time is the same as the server's... The best thing I could come up with would be something like this:
var start = new Date().getTime()
$.post( "sync.php", "")
.done(function(data) {
var end = new Date().getTime()/1000;
var clientServerClientTravelTime = (end-start);
var estimatedServerClientTravelTime = clientServerClientTravelTime/2;
});
Any idea how I could improve this whole concept?
Btw video cannot be on my own server.