From b1d59e65bec8c55b5bdc60035a8e09d8ac121b27 Mon Sep 17 00:00:00 2001 From: croneter Date: Mon, 5 Aug 2019 18:21:54 +0200 Subject: [PATCH] Fix Plex Companion device restarting playback when reconnecting to PKC --- resources/lib/playlist_func.py | 15 +++++++++++ resources/lib/plex_companion.py | 44 ++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/resources/lib/playlist_func.py b/resources/lib/playlist_func.py index d32912f8..47ce5670 100644 --- a/resources/lib/playlist_func.py +++ b/resources/lib/playlist_func.py @@ -129,6 +129,21 @@ class Playqueue_Object(object): self.kodi_playlist_playback = False LOG.debug('Playlist cleared: %s', self) + def position_from_plex_id(self, plex_id): + """ + Returns the position [int] for the very first item with plex_id [int] + (Plex seems uncapable of adding the same element multiple times to a + playqueue or playlist) + + Raises KeyError if not found + """ + for position, item in enumerate(self.items): + if item.plex_id == plex_id: + break + else: + raise KeyError('Did not find plex_id %s in %s', plex_id, self) + return position + class Playlist_Item(object): """ diff --git a/resources/lib/plex_companion.py b/resources/lib/plex_companion.py index dbdea11e..5571aef7 100644 --- a/resources/lib/plex_companion.py +++ b/resources/lib/plex_companion.py @@ -55,7 +55,16 @@ def update_playqueue_from_PMS(playqueue, except AttributeError: LOG.error('Could now download playqueue %s', playqueue_id) return - playqueue.clear() + if playqueue.id == playqueue_id: + # This seems to be happening ONLY if a Plex Companion device + # reconnects and Kodi is already playing something - silly, really + # For all other cases, a new playqueue is generated by Plex + LOG.debug('Update for existing playqueue detected') + new = False + else: + new = True + playqueue.clear() + # Get new metadata for the playqueue first try: PL.get_playlist_details_from_xml(playqueue, xml) except PL.PlaylistError: @@ -63,10 +72,33 @@ def update_playqueue_from_PMS(playqueue, return playqueue.repeat = 0 if not repeat else int(repeat) playqueue.plex_transient_token = transient_token - playback.play_xml(playqueue, - xml, - offset=offset, - start_plex_id=start_plex_id) + if new: + playback.play_xml(playqueue, + xml, + offset=offset, + start_plex_id=start_plex_id) + return + # Updates to playqueues could potentially become a bit more ugly... + if app.APP.is_playing: + try: + playerid = js.get_player_ids()[0] + except IndexError: + LOG.error('Unexpectately could not get Kodi player id') + return + if app.PLAYSTATE.player_states[playerid]['plex_id'] == start_plex_id: + # Nothing to do - let's not seek to avoid jumps in playback + return + pos = playqueue.position_from_plex_id(start_plex_id) + LOG.debug('Skipping to position %s for %s', pos, playqueue) + js.skipto(pos) + if offset: + js.seek_to(offset) + return + # Need to initiate playback again using our existing playqueue + app.APP.player.play(playqueue.kodi_pl, + None, + False, + playqueue.position_from_plex_id(start_plex_id)) class PlexCompanion(backgroundthread.KillableThread): @@ -165,7 +197,7 @@ class PlexCompanion(backgroundthread.KillableThread): update_playqueue_from_PMS(playqueue, playqueue_id=container_key, repeat=query.get('repeat'), - offset=data.get('offset'), + offset=utils.cast(int, data.get('offset')), transient_token=data.get('token'), start_plex_id=key)