implement UDP sync protocol output
Stefan Schuermans

Stefan Schuermans commited on 2013-11-23 20:48:50
Showing 1 changed files, with 19 additions and 2 deletions.

... ...
@@ -10,9 +10,10 @@ import os
10 10
 from gi.repository import Gtk
11 11
 import gobject
12 12
 import pango
13
+import socket
14
+import struct
13 15
 import sys
14 16
 import time
15
-
16 17
 import playlist
17 18
 import time_fmt
18 19
 
... ...
@@ -57,7 +58,11 @@ class SyncGui:
57 58
     self.stDuration = 0 # current entry has zero size
58 59
     self.stPosition = 0 # at begin of current entry
59 60
     self.stPlaying = False # not playing
61
+    self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
62
+    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
63
+    self.sock.connect(("255.255.255.255", 5740))
60 64
     gobject.timeout_add(10, self.onTimer10ms)
65
+    gobject.timeout_add(100, self.onTimer100ms)
61 66
     self.updateEntry()
62 67
     self.updateButtonVisibility()
63 68
 
... ...
@@ -262,7 +267,19 @@ class SyncGui:
262 267
         self.updateEntry()
263 268
       else:
264 269
         self.updatePosition()
265
-    return True
270
+    return True # call again
271
+
272
+  def onTimer100ms(self):
273
+    """timer callback, every 100ms"""
274
+    # send sync packet
275
+    flags = 0
276
+    if self.stPlaying:
277
+      flags = flags | 1
278
+    name = self.stName
279
+    pos_ms = round(self.stPosition * 1000)
280
+    data = "PoSy" + struct.pack("!I64sI", flags, name, pos_ms)
281
+    self.sock.send(data)
282
+    return True # call again
266 283
 
267 284
 # main application entry point
268 285
 if __name__ == "__main__":
269 286