From 72de3b67965ea84f47d06be4f5f99ffbab924bf7 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Fri, 15 Dec 2017 16:11:19 +0100 Subject: [PATCH] Companion: enable audio and subtitle stream switch --- resources/lib/PlexCompanion.py | 20 ++++++++++++++++++++ resources/lib/companion.py | 5 +++++ resources/lib/json_rpc.py | 12 ++++++++++-- resources/lib/playlist_func.py | 17 +++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/resources/lib/PlexCompanion.py b/resources/lib/PlexCompanion.py index 25a6a78a..89fd4b68 100644 --- a/resources/lib/PlexCompanion.py +++ b/resources/lib/PlexCompanion.py @@ -14,6 +14,7 @@ from plexbmchelper import listener, plexgdm, subscribers, httppersist from PlexFunctions import ParseContainerKey, GetPlexMetadata from PlexAPI import API from playlist_func import get_pms_playqueue, get_plextype_from_xml +import json_rpc as js import player import variables as v import state @@ -155,6 +156,25 @@ class PlexCompanion(Thread): playqueue, data['playQueueID']) + elif task['action'] == 'setStreams': + # Plex Companion client adjusted audio or subtitle stream + playqueue = self.mgr.playqueue.get_playqueue_from_type( + v.KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[data['type']]) + pos = js.get_position(playqueue.playlistid) + if 'audioStreamID' in data: + index = playqueue.items[pos].kodi_stream_index( + data['audioStreamID'], 'audio') + self.player.setAudioStream(index) + elif 'subtitleStreamID' in data: + if data['subtitleStreamID'] == '0': + self.player.showSubtitles(False) + else: + index = playqueue.items[pos].kodi_stream_index( + data['subtitleStreamID'], 'subtitle') + self.player.setSubtitleStream(index) + else: + LOG.error('Unknown setStreams command: %s', task) + def run(self): """ Ensure that diff --git a/resources/lib/companion.py b/resources/lib/companion.py index feda49a6..08db67c6 100644 --- a/resources/lib/companion.py +++ b/resources/lib/companion.py @@ -107,5 +107,10 @@ def process_command(request_path, params, queue=None): js.input_home() elif request_path == "player/navigation/back": js.input_back() + elif request_path == "player/playback/setStreams": + queue.put({ + 'action': 'setStreams', + 'data': params + }) else: LOG.error('Unknown request path: %s', request_path) diff --git a/resources/lib/json_rpc.py b/resources/lib/json_rpc.py index c92fa456..a8fa0f94 100644 --- a/resources/lib/json_rpc.py +++ b/resources/lib/json_rpc.py @@ -50,9 +50,8 @@ def get_players(): 'picture': ... } """ - info = JsonRPC("Player.GetActivePlayers").execute()['result'] ret = {} - for player in info: + for player in JsonRPC("Player.GetActivePlayers").execute()['result']: player['playerid'] = int(player['playerid']) ret[player['type']] = player return ret @@ -405,6 +404,15 @@ def get_player_props(playerid): 'currentsubtitle']})['result'] +def get_position(playerid): + """ + Returns the currently playing item's position [int] within the playlist + """ + return JsonRPC('Player.GetProperties').execute({ + 'playerid': playerid, + 'properties': ['position']})['result']['position'] + + def current_audiostream(playerid): """ Returns a dict of the active audiostream for playerid [int]: diff --git a/resources/lib/playlist_func.py b/resources/lib/playlist_func.py index 826b5f1d..f5a39e96 100644 --- a/resources/lib/playlist_func.py +++ b/resources/lib/playlist_func.py @@ -166,6 +166,23 @@ class Playlist_Item(object): return stream.attrib['id'] count += 1 + def kodi_stream_index(self, plex_stream_index, stream_type): + """ + Pass in the kodi_stream_index [int] in order to receive the Plex stream + index. + + stream_type: 'video', 'audio', 'subtitle' + + Returns None if unsuccessful + """ + stream_type = v.PLEX_STREAM_TYPE_FROM_STREAM_TYPE[stream_type] + count = 0 + for stream in self.xml[0][self.part]: + if stream.attrib['streamType'] == stream_type: + if stream.attrib['id'] == plex_stream_index: + return count + count += 1 + def playlist_item_from_kodi(kodi_item): """