Merge pull request #1643 from croneter/py3-fix-subs

Refactor stream code and fix Kodi not activating subtitle when it should
This commit is contained in:
croneter 2021-09-30 14:57:21 +02:00 committed by GitHub
commit 7e3a1c9ddc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 62 deletions

View file

@ -382,72 +382,17 @@ class KodiMonitor(xbmc.Monitor):
if not playerid == v.KODI_VIDEO_PLAYER_ID: if not playerid == v.KODI_VIDEO_PLAYER_ID:
# We're just messing with Kodi's videoplayer # We're just messing with Kodi's videoplayer
return return
item = app.PLAYSTATE.item
if item is None:
# Player might've quit
return
if not self._switched_to_plex_streams: if not self._switched_to_plex_streams:
# We need to switch to the Plex streams ONCE upon playback start # We need to switch to the Plex streams ONCE upon playback start
# after onavchange has been fired # after onavchange has been fired
self.switch_to_plex_streams() item.switch_to_plex_streams()
self._switched_to_plex_streams = True self._switched_to_plex_streams = True
else: else:
item = app.PLAYSTATE.item item.on_av_change(playerid)
if item is None:
# Player might've quit
return
kodi_audio_stream = js.get_current_audio_stream_index(playerid)
sub_enabled = js.get_subtitle_enabled(playerid)
kodi_sub_stream = js.get_current_subtitle_stream_index(playerid)
# Audio
if kodi_audio_stream != item.current_kodi_audio_stream:
item.on_kodi_audio_stream_change(kodi_audio_stream)
# Subtitles - CURRENTLY BROKEN ON THE KODI SIDE!
# current_kodi_sub_stream may also be zero
subs_off = (None, False)
if ((sub_enabled and item.current_kodi_sub_stream in subs_off)
or (not sub_enabled and item.current_kodi_sub_stream not in subs_off)
or (kodi_sub_stream is not None
and kodi_sub_stream != item.current_kodi_sub_stream)):
item.on_kodi_subtitle_stream_change(kodi_sub_stream,
sub_enabled)
@staticmethod
def switch_to_plex_streams():
"""
Override Kodi audio and subtitle streams with Plex PMS' selection
"""
item = app.PLAYSTATE.item
if item is None:
# Player might've quit
return
for typus in ('audio', 'subtitle'):
try:
plex_index, language_tag = item.active_plex_stream_index(typus)
except TypeError:
LOG.debug('Deactivating Kodi subtitles because the PMS '
'told us to not show any subtitles')
app.APP.player.showSubtitles(False)
item.current_kodi_sub_stream = False
continue
LOG.debug('The PMS wants to display %s stream with Plex id %s and '
'languageTag %s',
typus, plex_index, language_tag)
kodi_index = item.kodi_stream_index(plex_index, typus)
if kodi_index is None:
LOG.debug('Leaving Kodi %s stream settings untouched since we '
'could not parse Plex %s stream with id %s to a Kodi'
' index', typus, typus, plex_index)
else:
LOG.debug('Switching to Kodi %s stream number %s because the '
'PMS told us to show stream with Plex id %s',
typus, kodi_index, plex_index)
# If we're choosing an "illegal" index, this function does
# need seem to fail nor log any errors
if typus == 'audio':
app.APP.player.setAudioStream(kodi_index)
else:
app.APP.player.setSubtitleStream(kodi_index)
if typus == 'audio':
item.current_kodi_audio_stream = kodi_index
else:
item.current_kodi_sub_stream = kodi_index
def _playback_cleanup(ended=False): def _playback_cleanup(ended=False):

View file

@ -324,6 +324,58 @@ class PlaylistItem(object):
PF.change_audio_stream(plex_stream_index, self.api.part_id()) PF.change_audio_stream(plex_stream_index, self.api.part_id())
self.current_kodi_audio_stream = kodi_stream_index self.current_kodi_audio_stream = kodi_stream_index
def switch_to_plex_streams(self):
self.switch_to_plex_stream('audio')
self.switch_to_plex_stream('subtitle')
def switch_to_plex_stream(self, typus):
try:
plex_index, language_tag = self.active_plex_stream_index(typus)
except TypeError:
LOG.debug('Deactivating Kodi subtitles because the PMS '
'told us to not show any subtitles')
app.APP.player.showSubtitles(False)
self.current_kodi_sub_stream = False
return
LOG.debug('The PMS wants to display %s stream with Plex id %s and '
'languageTag %s', typus, plex_index, language_tag)
kodi_index = self.kodi_stream_index(plex_index, typus)
if kodi_index is None:
LOG.debug('Leaving Kodi %s stream settings untouched since we '
'could not parse Plex %s stream with id %s to a Kodi'
' index', typus, typus, plex_index)
else:
LOG.debug('Switching to Kodi %s stream number %s because the '
'PMS told us to show stream with Plex id %s',
typus, kodi_index, plex_index)
# If we're choosing an "illegal" index, this function does
# need seem to fail nor log any errors
if typus == 'audio':
app.APP.player.setAudioStream(kodi_index)
else:
app.APP.player.setSubtitleStream(kodi_index)
app.APP.player.showSubtitles(True)
if typus == 'audio':
self.current_kodi_audio_stream = kodi_index
else:
self.current_kodi_sub_stream = kodi_index
def on_av_change(self, playerid):
kodi_audio_stream = js.get_current_audio_stream_index(playerid)
sub_enabled = js.get_subtitle_enabled(playerid)
kodi_sub_stream = js.get_current_subtitle_stream_index(playerid)
# Audio
if kodi_audio_stream != self.current_kodi_audio_stream:
self.on_kodi_audio_stream_change(kodi_audio_stream)
# Subtitles - CURRENTLY BROKEN ON THE KODI SIDE!
# current_kodi_sub_stream may also be zero
subs_off = (None, False)
if ((sub_enabled and self.current_kodi_sub_stream in subs_off)
or (not sub_enabled and self.current_kodi_sub_stream not in subs_off)
or (kodi_sub_stream is not None
and kodi_sub_stream != self.current_kodi_sub_stream)):
self.on_kodi_subtitle_stream_change(kodi_sub_stream, sub_enabled)
def playlist_item_from_kodi(kodi_item): def playlist_item_from_kodi(kodi_item):
""" """

View file

@ -16,12 +16,15 @@ METADATA_PROVIDERS = (('imdb', utils.REGEX_IMDB),
('tvdb', utils.REGEX_TVDB), ('tvdb', utils.REGEX_TVDB),
('tmdb', utils.REGEX_TMDB), ('tmdb', utils.REGEX_TMDB),
('anidb', utils.REGEX_ANIDB)) ('anidb', utils.REGEX_ANIDB))
class Base(object): class Base(object):
""" """
Processes a Plex media server's XML response Processes a Plex media server's XML response
xml: xml.etree.ElementTree element xml: xml.etree.ElementTree element
""" """
def __init__(self, xml): def __init__(self, xml):
self.xml = xml self.xml = xml
# which media part in the XML response shall we look at if several # which media part in the XML response shall we look at if several