-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add play position tracking for all play types
Related: #1946
- Loading branch information
Showing
2 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/jukebox/components/playermpd/play_position_tracker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
""" | ||
Keeps track of playlist and in-song position for played single tracks, | ||
albums or folders. | ||
Syncs to disk every FLUSH_INTERVAL seconds. | ||
Provides methods to retrieve the stored values to resume playing. | ||
""" | ||
import time | ||
import os | ||
import logging | ||
import threading | ||
import json | ||
|
||
|
||
NO_SEEK_IF_NEAR_START_END_CUTOFF = 5 | ||
FLUSH_INTERVAL = 30 | ||
|
||
logger = logging.getLogger('jb.PlayerMPD.PlayPositionTracker') | ||
|
||
|
||
def play_target_to_key(play_target): | ||
return '|'.join([str(x) for x in play_target]) | ||
|
||
|
||
class PlayPositionTracker: | ||
flush_interval = 30 | ||
_last_flush_timestamp = 0 | ||
_last_json = None | ||
|
||
def __init__(self, path): | ||
self._lock = threading.RLock() | ||
self._path = path | ||
self._tmp_path = path + '.tmp' | ||
self._current_play_target = None | ||
with self._lock: | ||
self._load() | ||
|
||
def _load(self): | ||
logger.debug(f'Loading from {self._path}') | ||
try: | ||
with open(self._path) as f: | ||
d = json.load(f) | ||
except FileNotFoundError: | ||
logger.debug('File not found, assuming empty list') | ||
self._play_targets = {} | ||
self.flush() | ||
return | ||
self._play_targets = d['positions_by_play_target'] | ||
logger.debug(f'Loaded {len(self._play_targets.keys())} saved target play positions') | ||
|
||
def set_current_play_target(self, play_target): | ||
with self._lock: | ||
self._current_play_target = play_target_to_key(play_target) | ||
|
||
def is_current_play_target(self, play_target): | ||
return self._current_play_target == play_target | ||
|
||
def get_playlist_position_by_play_target(self, play_target): | ||
return self._play_targets.get(play_target_to_key(play_target), {}).get('playlist_position') | ||
|
||
def get_seek_position_by_play_target(self, play_target): | ||
return self._play_targets.get(play_target_to_key(play_target), {}).get('seek_position') | ||
|
||
def handle_mpd_status(self, status): | ||
if not self._current_play_target: | ||
return | ||
playlist_len = int(status.get('playlistlength', -1)) | ||
playlist_pos = int(status.get('pos', 0)) | ||
elapsed = float(status.get('elapsed', 0)) | ||
duration = float(status.get('duration', 0)) | ||
is_end_of_playlist = playlist_pos == playlist_len - 1 | ||
is_end_of_track = duration - elapsed < NO_SEEK_IF_NEAR_START_END_CUTOFF | ||
if status.get('state') == 'stop' and is_end_of_playlist and is_end_of_track: | ||
# If we are at the end of the playlist, | ||
# we want to restart the playlist the next time the card is present. | ||
# Therefore, delete all resume information: | ||
if self._current_play_target in self._play_targets: | ||
with self._lock: | ||
del self._play_targets[self._current_play_target] | ||
return | ||
with self._lock: | ||
if self._current_play_target not in self._play_targets: | ||
self._play_targets[self._current_play_target] = {} | ||
self._play_targets[self._current_play_target]['playlist_position'] = playlist_pos | ||
if (elapsed < NO_SEEK_IF_NEAR_START_END_CUTOFF or | ||
((duration - elapsed) < NO_SEEK_IF_NEAR_START_END_CUTOFF)): | ||
# restart song next time: | ||
elapsed = 0 | ||
with self._lock: | ||
if self._current_play_target not in self._play_targets: | ||
self._play_targets[self._current_play_target] = {} | ||
self._play_targets[self._current_play_target]['seek_position'] = elapsed | ||
self._flush_if_necessary() | ||
|
||
def _flush_if_necessary(self): | ||
now = time.time() | ||
if self._last_flush_timestamp + FLUSH_INTERVAL < now: | ||
return self.flush() | ||
|
||
def flush(self): | ||
with self._lock: | ||
self._last_flush_timestamp = time.time() | ||
new_json = json.dumps( | ||
{ | ||
'positions_by_play_target': self._play_targets, | ||
}, indent=2, sort_keys=True) | ||
if self._last_json == new_json: | ||
return | ||
with open(self._tmp_path, 'w') as f: | ||
f.write(new_json) | ||
os.rename(self._tmp_path, self._path) | ||
self._last_json = new_json | ||
logger.debug(f'Flushed state to {self._path}') | ||
|
||
def __del__(self): | ||
self.flush() |