forward pause/play to peers

This commit is contained in:
j 2024-03-23 10:00:47 +01:00
parent a6479d1746
commit 6cdbf4f1b9

View file

@ -93,6 +93,7 @@ class Sync(Thread):
self.mpv.register_key_binding('q', self.q_binding) self.mpv.register_key_binding('q', self.q_binding)
self.mpv.register_key_binding('s', self.s_binding) self.mpv.register_key_binding('s', self.s_binding)
self.mpv.register_key_binding('p', self.p_binding) self.mpv.register_key_binding('p', self.p_binding)
self.mpv.register_key_binding('SPACE', self.space_binding)
self.playlist = kwargs['playlist'] self.playlist = kwargs['playlist']
self.playlist_mtime = os.stat(self.playlist).st_mtime self.playlist_mtime = os.stat(self.playlist).st_mtime
self.mpv.loadlist(self.playlist) self.mpv.loadlist(self.playlist)
@ -152,18 +153,34 @@ class Sync(Thread):
self.mpv.stop() self.mpv.stop()
def q_binding(self, *args): def q_binding(self, *args):
if args[0] != 'd-':
return
self.stop() self.stop()
self.mpv.stop() self.mpv.stop()
def space_binding(self, *args):
if args[0] != 'd-':
return
if self.mpv.pause:
self.p_binding(*args)
else:
self.s_binding(*args)
def s_binding(self, *args): def s_binding(self, *args):
if args[0] != 'd-':
return
self.mpv.pause = True self.mpv.pause = True
if self.sax: if self.sax:
self.sax.pause = True self.sax.pause = True
self.send_playback_state()
def p_binding(self, *args): def p_binding(self, *args):
if args[0] != 'd-':
return
self.mpv.pause = False self.mpv.pause = False
if self.sax: if self.sax:
self.sax.pause = False self.sax.pause = False
self.send_playback_state()
def stop(self, *args): def stop(self, *args):
self.active = False self.active = False
@ -251,18 +268,34 @@ class Sync(Thread):
except socket.error as e: except socket.error as e:
logger.error("send failed: %s", e) logger.error("send failed: %s", e)
def send_playback_state(self):
state = 'pause' if self.mpv.pause else 'play'
msg = ("%s -1" % state).encode()
try:
self.sock.send(msg)
except socket.error as e:
logger.error("send failed: %s", e)
# #
# follower specific # follower specific
# #
_last_ping = None
def read_position_main(self): def read_position_main(self):
self.sock.settimeout(5) self.sock.settimeout(5)
try: try:
data = self.sock.recvfrom(1024)[0].decode().split(" ", 1) data = self.sock.recvfrom(1024)[0].decode().split(" ", 1)
except socket.timeout: except socket.timeout:
if self._last_ping != "pause":
logger.error("failed to receive data from main") logger.error("failed to receive data from main")
except OSError: except OSError:
logger.error("socket closed") logger.error("socket closed")
else:
self._last_ping = data[0]
if data[0] == "pause":
self.mpv.pause = True
elif data[0] == "play":
self.mpv.pause = False
else: else:
self.main.time_pos = float(data[0]) self.main.time_pos = float(data[0])
self.main.playlist_current_pos = int(data[1]) self.main.playlist_current_pos = int(data[1])