diff --git a/sync_mpc.py b/sync_mpc.py index e45c49f..57fc22e 100755 --- a/sync_mpc.py +++ b/sync_mpc.py @@ -1,32 +1,16 @@ #coding:utf8 import thread -import sys, os +import sys from twisted.internet import reactor - from syncplay import client from syncplay.players import mpc -def stdin_thread(sock): - try: - fd = sys.stdin.fileno() - while True: - data = os.read(fd, 1024) - if not data: - break - sock.execute_command(data.rstrip('\n\r')) - except: - pass + +import common_functions if __name__ == '__main__': - args = sys.argv[1:] - host = args.pop(0) - name = args.pop(0) - if ':' in host: - host, port = host.split(':', 1) - port = int(port) - else: - port = 8999 + host,port, name = common_functions.get_configuration() manager = client.Manager(host, port, name, lambda m: mpc.run_mpc(m)) - thread.start_new_thread(stdin_thread, (manager,)) + thread.start_new_thread(common_functions.stdin_thread, (manager,)) manager.start() diff --git a/sync_mplayer.py b/sync_mplayer.py index 7a08cc2..c9e6cbe 100755 --- a/sync_mplayer.py +++ b/sync_mplayer.py @@ -1,12 +1,14 @@ #coding:utf8 - +import thread import sys from twisted.internet import reactor - from syncplay import client from syncplay.players import mplayer + +import common_functions + if __name__ == '__main__': args = sys.argv[1:] host = args.pop(0) @@ -20,5 +22,6 @@ if __name__ == '__main__': args.extend(('-slave', '-msglevel', 'all=1:global=4')) manager = client.Manager(host, port, name, lambda m: mplayer.run_mplayer(m, 'mplayer', args)) + thread.start_new_thread(common_functions.stdin_thread, (manager,)) manager.start() diff --git a/syncplay/client.py b/syncplay/client.py index c8fc476..86df936 100644 --- a/syncplay/client.py +++ b/syncplay/client.py @@ -19,7 +19,6 @@ from .utils import ( class SyncClientProtocol(CommandProtocol): def __init__(self, manager): CommandProtocol.__init__(self) - self.manager = manager def connectionMade(self): @@ -101,7 +100,6 @@ class SyncClientProtocol(CommandProtocol): def send_playing(self, filename): self.send_message('playing', filename) - states = dict( init = dict( present = 'handle_init_present', @@ -280,22 +278,33 @@ class Manager(object): if self.protocol and self.player_filename: self.protocol.send_playing(self.player_filename) - def execute_command(self, data): - RE_SEEK = re.compile("^s ?(\d+)?(:(\d{1,2}))?$") - m = RE_SEEK.match(data) - if m : - minutes, seconds = m.group(1), m.group(3) - if minutes <> None: - minutes = int(minutes) * 60 - else: - minutes = 0 + def exectue_seek_cmd(self, seek_type, minutes, seconds): + self.player_position_before_last_seek = self.player_position + if seek_type == 's': + self.counter += 1 if seconds <> None: seconds = int(seconds) else: seconds = 0 - self.player_position_before_last_seek = self.player_position - self.counter += 1 - self.protocol.send_seek(self.counter, time.time(), minutes+seconds) + if minutes <> None: + seconds += int(minutes) * 60 + self.protocol.send_seek(self.counter, time.time(), seconds) + else: #seek_type s+ + if seconds <> None: + seconds = int(seconds) + else: + seconds = 20 + if minutes <> None: + seconds += int(minutes) * 60 + else: + seconds += 60 + self.protocol.send_seek(self.counter, time.time(), self.player_position+seconds) + + def execute_command(self, data): + RE_SEEK = re.compile("^(s[+s]?) ?(-?\d+)?([^0-9](\d+))?$") + matched_seek = RE_SEEK.match(data) + if matched_seek : + self.exectue_seek_cmd(matched_seek.group(1), matched_seek.group(2), matched_seek.group(4)) elif data == "r": self.counter += 1 tmp_pos = self.player_position