Skip to content

Commit

Permalink
Fix some rewind issues relating to file loads / playlist changes (#698)
Browse files Browse the repository at this point in the history
* Re-introduce rewind double check

* Major re-work to playlist changing/advancement (#683 & #618)

* Remove stray print

* Fix mpv.net 'auto load folder' playlist advancement bug

* Update client.py to remove commented-out code
  • Loading branch information
Et0h authored Nov 18, 2024
1 parent dd3884b commit 6c632e3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
40 changes: 34 additions & 6 deletions syncplay/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def __init__(self, playerClass, ui, config):
self.lastRewindTime = None
self.lastUpdatedFileTime = None
self.lastAdvanceTime = None
self.fileOpenBeforeChangingPlaylistIndex = None
self.waitingToLoadNewfile = False
self.waitingToLoadNewfileSince = None
self.lastConnectTime = None
self.lastSetRoomTime = None
self.hadFirstPlaylistIndex = False
Expand Down Expand Up @@ -248,15 +251,23 @@ def updatePlayerStatus(self, paused, position):
if self._lastGlobalUpdate:
self._lastPlayerUpdate = time.time()
if (pauseChange or seeked) and self._protocol:
if self.recentlyRewound() or self._recentlyAdvanced():
self._protocol.sendState(self._globalPosition, self.getPlayerPaused(), False, None, True)
return
if seeked:
self.playerPositionBeforeLastSeek = self.getGlobalPosition()
self._protocol.sendState(self.getPlayerPosition(), self.getPlayerPaused(), seeked, None, True)

def prepareToChangeToNewPlaylistItemAndRewind(self):
self.ui.showDebugMessage("Preparing to change to new playlist index and rewind...")
self.fileOpenBeforeChangingPlaylistIndex = self.userlist.currentUser.file["path"] if self.userlist.currentUser.file else None
self.waitingToLoadNewfile = True
self.waitingToLoadNewfileSince = time.time()

def prepareToAdvancePlaylist(self):
if self.playlist.canSwitchToNextPlaylistIndex():
self.ui.showDebugMessage("Preparing to advance playlist...")
self.lastAdvanceTime = time.time()
self._protocol.sendState(0, True, True, None, True)
else:
self.ui.showDebugMessage("Not preparing to advance playlist because the next file cannot be switched to")

Expand Down Expand Up @@ -1847,7 +1858,7 @@ def changeToPlaylistIndex(self, index, username=None, resetPosition=False):
if self._client.playerIsNotReady():
if not self.addedChangeListCallback:
self.addedChangeListCallback = True
self._client.addPlayerReadyCallback(lambda x: self.changeToPlaylistIndex(index, username))
self._client.addPlayerReadyCallback(lambda x: self.changeToPlaylistIndex(index, username, resetPosition))
return
try:
filename = self._playlist[index]
Expand All @@ -1863,10 +1874,22 @@ def changeToPlaylistIndex(self, index, username=None, resetPosition=False):
self._playlistIndex = index
if username is None:
if self._client.isConnectedAndInARoom() and self._client.sharedPlaylistIsEnabled():
if resetPosition:
self._client.rewindFile()
self._client.setPlaylistIndex(index)
filename = self._playlist[index]
self._ui.setPlaylistIndexFilename(filename)
if resetPosition:
self._ui.showDebugMessage("Pausing due to index change")
state = {}
state["playstate"] = {}
state["playstate"]["position"] = 0
state["playstate"]["paused"] = True
self._client.lastAdvanceTime = time.time()
self._client._protocol.sendMessage({"State": state})
self._playerPaused = True
self._client.autoplayCheck()
elif index is not None:
filename = self._playlist[index]
self._ui.setPlaylistIndexFilename(filename)
self._ui.showMessage(getMessage("playlist-selection-changed-notification").format(username))
self.switchToNewPlaylistIndex(index, resetPosition=resetPosition)

Expand All @@ -1892,7 +1915,12 @@ def switchToNewPlaylistIndex(self, index, resetPosition = False):
self.queuedIndexFilename = self._playlist[index]
except:
self.queuedIndexFilename = None
self._ui.showDebugMessage("Failed to find index {} in plauylist".format(index))
self._ui.showDebugMessage("Failed to find index {} in playlist".format(index))
if resetPosition and index is not None:
filename = self._playlist[index]
if (not utils.isURL(filename)) or self._client.isURITrusted(filename):
self._client.prepareToChangeToNewPlaylistItemAndRewind()

self._lastPlaylistIndexChange = time.time()
if self._client.playerIsNotReady():
self._client.addPlayerReadyCallback(lambda x: self.switchToNewPlaylistIndex(index, resetPosition))
Expand All @@ -1913,7 +1941,7 @@ def switchToNewPlaylistIndex(self, index, resetPosition = False):
else:
path = self._client.fileSwitch.findFilepath(filename, highPriority=True)
if path:
self._client.openFile(path, resetPosition)
self._client.openFile(path, resetPosition=resetPosition)
else:
self._ui.showErrorMessage(getMessage("cannot-find-file-for-playlist-switch-error").format(filename))
return
Expand Down
3 changes: 2 additions & 1 deletion syncplay/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def getValueForOS(constantDict):
FOLDER_SEARCH_DOUBLE_CHECK_INTERVAL = 30.0 # Secs - Frequency of updating cache

# Usually there's no need to adjust these
DOUBLE_CHECK_REWIND = False
DOUBLE_CHECK_REWIND = True
LAST_PAUSED_DIFF_THRESHOLD = 2
FILENAME_STRIP_REGEX = r"[-~_\.\[\](): ]"
CONTROL_PASSWORD_STRIP_REGEX = r"[^a-zA-Z0-9\-]"
Expand Down Expand Up @@ -273,6 +273,7 @@ def getValueForOS(constantDict):
'term-playing-msg': '<SyncplayUpdateFile>\nANS_filename=${filename}\nANS_length=${=duration:${=length:0}}\nANS_path=${path}\n</SyncplayUpdateFile>',
'keep-open-pause': 'yes'
}
MPV_NET_EXTRA_ARGS = { 'auto-load-folder': 'no' }

IINA_PROPERTIES = {'geometry': '25%+100+100',
'idle': 'yes',
Expand Down
1 change: 1 addition & 0 deletions syncplay/players/mpvnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class MpvnetPlayer(MpvPlayer):

@staticmethod
def run(client, playerPath, filePath, args):
args.extend(constants.MPV_NET_EXTRA_ARGS)
constants.MPV_NEW_VERSION = True
constants.MPV_OSC_VISIBILITY_CHANGE_VERSION = True
return MpvnetPlayer(client, MpvnetPlayer.getExpandedPath(playerPath), filePath, args)
Expand Down

0 comments on commit 6c632e3

Please sign in to comment.