This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
forked from etiandre/jukebox-ultra-nrv
-
Notifications
You must be signed in to change notification settings - Fork 3
/
video-client.py
104 lines (92 loc) · 3.26 KB
/
video-client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import mpv
import jukebox.src.lib.idle as idle
import requests
import sys
import threading
import time
import subprocess
class MyMPV(mpv.MPV):
def __init__(self, argv):
super().__init__(argv, window_id=None, debug=False)
self.loaded = threading.Event()
def on_property_time_pos(self, position=None):
pass
def load(self, path):
self.loaded.clear()
self.command("loadfile", path, "replace")
self.set_property("playlist-pos", 0)
def on_file_loaded(self):
self.loaded.set()
def pos(self):
return self.get_property("time-pos")
def file(self):
try:
return self.get_property("path")
except mpv.MPVCommandError:
return None
def finished(self):
r = None
try:
r=self.get_property("eof-reached")
return r
except (mpv.MPVCommandError, BrokenPipeError):
return True
def play(self):
self.set_property("pause", False)
def pause(self):
self.set_property("pause", True)
def seek(self, position):
self.command("seek", position, "absolute")
if __name__ == "__main__":
url = "http://" + sys.argv[1] + "/sync"
timeout = int(sys.argv[2])
player = None
while True:
try:
sync_data = requests.get(url).json()
except:
print("connexion error, retrying...")
time.sleep(2)
if idle.getIdleSec() < timeout or len(sync_data["playlist"]) == 0:
if player:
print("User detected or playlist empty, switching to mame")
player.close()
try:
subprocess.run(["killall", "-s", "SIGCONT", "mame"])
subprocess.run(["wmctrl", "-a", "mame"])
except: pass
player = None
time.sleep(1)
continue
if not player:
print("Idle for more than {}, switching to mpv".format(timeout))
try:
subprocess.run(["killall", "-s", "SIGSTOP", "mame"])
except:
pass
player = MyMPV(["--no-input-default-bindings", "--no-audio", "--no-stop-screensaver", "--ontop", "--no-border", "--geometry=100%x100%+0+0"])
orig_t = time.time()
if player.file() != sync_data["playlist"][0]["url"]:
print("loading new track {} {}".format(player.file(),sync_data["playlist"][0]["url"]))
player.load(sync_data["playlist"][0]["url"])
player.pause()
player.loaded.wait()
print("loaded")
else:
local = player.pos()
remote = sync_data["time"]
delay = time.time()-orig_t
delta = remote - local
offset = 0.5
print("local: {} | remote : {} | delta {} | delay {}".format(local, remote, delta, delay))
if abs(delta) >= offset: # catch up
print("catching up")
player.seek(remote + delay + offset/2) # go ahead
elif delta > 0.001: # sync
print("en avance de {}".format(delta))
player.pause()
time.sleep(abs(delta+delay)/2)
player.play()
player.play()
time.sleep(1)
mpv.close()