do not restart MPlayer for each entry
Stefan Schuermans

Stefan Schuermans commited on 2018-07-18 20:35:36
Showing 1 changed files, with 36 additions and 8 deletions.

... ...
@@ -51,6 +51,7 @@ class Synchronizer:
51 51
     self.posy_pause = None
52 52
     self.posy_pos = None
53 53
     self.posy_timestamp = None
54
+    self.restart = False
54 55
     self.sock = None
55 56
     self.verbose = False
56 57
     # startup
... ...
@@ -139,6 +140,21 @@ class Synchronizer:
139 140
       self.mplayer_pause = False
140 141
     self.mplayer_last_cmd_timestamp = datetime.datetime.now()
141 142
 
143
+  def mplayerSetFile(self, filename):
144
+    """set file played by MPlayer"""
145
+    # start MPlayer if not running or if restart mode is active
146
+    if self.mplayer is None or self.restart:
147
+      self.mplayerStart(filename)
148
+      return
149
+    # set new filename
150
+    self.dbg_print("MPlayer stdin: loadfile \"%s\"" % filename)
151
+    try:
152
+      self.mplayer.stdin.write("loadfile \"%s\"\n" % filename)
153
+    except:
154
+      pass
155
+    self.mplayer_name = filename
156
+    self.mplayer_last_cmd_timestamp = datetime.datetime.now()
157
+
142 158
   def mplayerSetPos(self, pos):
143 159
     """set MPlayer position"""
144 160
     # leave if no MPlayer running
... ...
@@ -180,8 +196,10 @@ class Synchronizer:
180 196
     # stop old MPlayer
181 197
     self.mplayerStop()
182 198
     # start MPlayer
183
-    cmd = [ "mplayer", "-volume", "80", "-slave", "-af", "scaletempo",
184
-            filename ]
199
+    cmd = [ "mplayer", "-volume", "80", "-slave", "-af", "scaletempo"]
200
+    if not self.restart:
201
+      cmd.append("-idle") # keep mplayer in idle mode if restart not desired
202
+    cmd.append(filename)
185 203
     print >>sys.stderr, "starting background process: " + " ".join(cmd)
186 204
     self.mplayer = subprocess.Popen(cmd, stdin = subprocess.PIPE,
187 205
                                          stdout = subprocess.PIPE,
... ...
@@ -286,7 +304,7 @@ class Synchronizer:
286 304
         self.playlist_idx += 1
287 305
         if self.playlist_idx >= len(self.playlist):
288 306
           self.playlist_idx = 0
289
-      self.mplayerStart(self.playlist[self.playlist_idx])
307
+      self.mplayerSetFile(self.playlist[self.playlist_idx])
290 308
 
291 309
   def playPosyName(self, posy_name):
292 310
     """play file by PoSy name (if found)"""
... ...
@@ -299,7 +317,7 @@ class Synchronizer:
299 317
     # file found -> (re-)start MPlayer
300 318
     else:
301 319
       self.playlist_idx = idx
302
-      self.mplayerStart(self.playlist[idx])
320
+      self.mplayerSetFile(self.playlist[idx])
303 321
 
304 322
   def posyCheckName(self, posy_name, file_name):
305 323
     """check if filename matches PoSyName"""
... ...
@@ -337,6 +355,10 @@ class Synchronizer:
337 355
     self.posy_pause = pause
338 356
     return True
339 357
 
358
+  def restartSet(self, restart):
359
+    """set restart mode"""
360
+    self.restart = restart
361
+
340 362
   def run(self):
341 363
     """run application"""
342 364
     try:
... ...
@@ -499,19 +521,25 @@ class Synchronizer:
499 521
 def main(argv):
500 522
   # check parameters
501 523
   if len(argv) < 2:
502
-    print >>sys.stderr, "usage: %s <playlist.txt> [-v]" % argv[0]
524
+    print >>sys.stderr, "usage: %s <playlist.txt> [<options>]" % argv[0]
525
+    print >>sys.stderr, "options: -v   verbose"
526
+    print >>sys.stderr, "         -r   restart MPlayer for each entry"
503 527
     return 2
504 528
   playlist = argv[1]
505 529
   verbose = False
506
-  if len(argv) >= 3:
507
-    if argv[2] == "-v":
530
+  restart = False
531
+  for arg in argv[2:]:
532
+    if arg == "-v":
508 533
       verbose = True
534
+    elif arg == "-r":
535
+      restart = True
509 536
     else:
510
-      print >>sys.stderr, "unknown option \"%s\"" % argv[2]
537
+      print >>sys.stderr, "unknown option \"%s\"" % arg
511 538
       return 3
512 539
   # run application
513 540
   app = Synchronizer()
514 541
   app.verboseSet(verbose)
542
+  app.restartSet(restart)
515 543
   if not app.playlistRead(playlist):
516 544
     print >>sys.stderr, "could not read playlist \"%s\"" % playlist
517 545
     return 4
518 546