1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 08:56:17 +00:00

Jukebox status: report (simulated) position

This commit is contained in:
Alban Féron 2019-09-07 17:37:04 +02:00
parent 9de96bb530
commit ee2efec59a
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165
3 changed files with 20 additions and 1 deletions

View File

@ -78,7 +78,12 @@ def jukebox_control():
except DaemonUnavailableError:
raise GenericError("Jukebox unavaliable")
rv = dict(currentIndex=status.index, playing=status.playing, gain=status.gain)
rv = dict(
currentIndex=status.index,
playing=status.playing,
gain=status.gain,
position=status.position,
)
if action == "get":
playlist = []
for path in status.playlist:

View File

@ -115,10 +115,12 @@ class JukeboxResult(DaemonCommandResult):
self.playing = False
self.index = -1
self.gain = 1.0
self.position = 0
else:
self.playing = jukebox.playing
self.index = jukebox.index
self.gain = jukebox.gain
self.position = jukebox.position
self.playlist = ()

View File

@ -12,6 +12,7 @@ import os
import shlex
import time
from datetime import datetime
from pony.orm import db_session, ObjectNotFound
from random import shuffle
from subprocess import Popen
@ -27,6 +28,7 @@ class Jukebox(object):
self.__cmd = shlex.split(cmd)
self.__playlist = []
self.__index = 0
self.__start = None
self.__devnull = None
@ -42,6 +44,12 @@ class Jukebox(object):
gain = property(lambda self: 1.0)
playlist = property(lambda self: list(self.__playlist))
@property
def position(self):
if self.__start is None:
return 0
return int((datetime.utcnow() - self.__start).total_seconds())
# subprocess.DEVNULL doesn't exist on Python 2.7
def _get_devnull(self):
if self.__devnull is None:
@ -79,6 +87,7 @@ class Jukebox(object):
with self.__lock:
self.__index = index
self.__start = None
self.__skip.set()
self.start()
@ -131,6 +140,7 @@ class Jukebox(object):
proc = self.__play_file()
elif proc.poll() is not None:
with self.__lock:
self.__start = None
self.__index += 1
if self.__index >= len(self.__playlist):
break
@ -142,6 +152,7 @@ class Jukebox(object):
proc.terminate()
proc.wait()
self._close_devnull()
self.__start = None
def __play_file(self):
path = self.__playlist[self.__index]
@ -149,6 +160,7 @@ class Jukebox(object):
logger.debug("Start playing with command %s", args)
try:
self.__start = datetime.utcnow()
return Popen(
args,
stdin=self._get_devnull(),