PlexKodiConnect/resources/lib/player.py

97 lines
2.9 KiB
Python
Raw Normal View History

2015-12-24 14:07:00 -06:00
# -*- coding: utf-8 -*-
###############################################################################
2017-12-09 14:35:08 +01:00
from logging import getLogger
2015-12-24 14:07:00 -06:00
2018-01-21 18:31:49 +01:00
from xbmc import Player
2015-12-24 14:07:00 -06:00
2018-01-21 18:31:49 +01:00
from downloadutils import DownloadUtils as DU
from plexbmchelper.subscribers import LOCKER
import playqueue as PQ
import variables as v
2017-05-17 20:22:16 +02:00
import state
2015-12-24 14:07:00 -06:00
###############################################################################
2015-12-24 14:07:00 -06:00
2017-12-09 14:35:08 +01:00
LOG = getLogger("PLEX." + __name__)
2016-09-01 19:41:55 +02:00
###############################################################################
2015-12-24 14:07:00 -06:00
2018-01-25 17:15:38 +01:00
@LOCKER.lockthis
def playback_cleanup():
"""
PKC cleanup after playback ends/is stopped
"""
# We might have saved a transient token from a user flinging media via
# Companion (if we could not use the playqueue to store the token)
LOG.debug('playback_cleanup called')
2018-01-25 17:15:38 +01:00
state.PLEX_TRANSIENT_TOKEN = None
for playerid in state.ACTIVE_PLAYERS:
status = state.PLAYER_STATES[playerid]
# Remember the last played item later
state.OLD_PLAYER_STATES[playerid] = dict(status)
# Stop transcoding
if status['playmethod'] == 'Transcode':
2018-02-03 14:05:18 +01:00
LOG.debug('Tell the PMS to stop transcoding')
2018-01-25 17:15:38 +01:00
DU().downloadUrl(
'{server}/video/:/transcode/universal/stop',
parameters={'session': v.PKC_MACHINE_IDENTIFIER})
# Kodi will not clear the playqueue (because there is not really any)
# if there is only 1 item in it
if len(PQ.PLAYQUEUES[playerid].items) == 1:
PQ.PLAYQUEUES[playerid].clear()
2018-01-25 17:15:38 +01:00
# Reset the player's status
status = dict(state.PLAYSTATE)
# As all playback has halted, reset the players that have been active
state.ACTIVE_PLAYERS = []
2018-02-03 14:05:18 +01:00
LOG.debug('Finished PKC playback cleanup')
2018-01-25 17:15:38 +01:00
2017-12-09 14:35:08 +01:00
class PKC_Player(Player):
2015-12-24 14:07:00 -06:00
def __init__(self):
2017-12-09 14:35:08 +01:00
Player.__init__(self)
2017-12-08 19:43:06 +01:00
LOG.info("Started playback monitor.")
2015-12-24 14:07:00 -06:00
2016-02-17 02:13:37 -06:00
def onPlayBackStarted(self):
"""
2016-09-01 19:41:55 +02:00
Will be called when xbmc starts playing a file.
"""
2018-01-21 18:31:49 +01:00
pass
2015-12-24 14:07:00 -06:00
2016-02-20 17:21:39 -06:00
def onPlayBackPaused(self):
2018-01-21 18:31:49 +01:00
"""
Will be called when playback is paused
"""
pass
2015-12-24 14:07:00 -06:00
2016-02-20 17:21:39 -06:00
def onPlayBackResumed(self):
2018-01-21 18:31:49 +01:00
"""
Will be called when playback is resumed
"""
pass
2015-12-24 14:07:00 -06:00
2016-02-20 17:21:39 -06:00
def onPlayBackSeek(self, time, seekOffset):
2018-01-21 18:31:49 +01:00
"""
Will be called when user seeks to a certain time during playback
"""
pass
2016-02-20 17:21:39 -06:00
def onPlayBackStopped(self):
2018-01-21 18:31:49 +01:00
"""
Will be called when playback is stopped by the user
"""
2018-02-03 14:05:18 +01:00
LOG.debug("ONPLAYBACK_STOPPED")
if state.PKC_CAUSED_STOP is True:
state.PKC_CAUSED_STOP = False
LOG.debug('PKC caused this playback stop - ignoring')
else:
playback_cleanup()
2016-03-16 18:01:07 +01:00
2018-01-21 18:31:49 +01:00
def onPlayBackEnded(self):
"""
Will be called when playback ends due to the media file being finished
"""
2018-02-03 14:05:18 +01:00
LOG.debug("ONPLAYBACK_ENDED")
2018-01-25 17:15:38 +01:00
playback_cleanup()