2016-03-31 16:35:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import json
|
2016-07-24 08:59:48 +00:00
|
|
|
import logging
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
import xbmc
|
2016-08-30 05:22:54 +00:00
|
|
|
import xbmcvfs
|
2016-03-31 16:35:41 +00:00
|
|
|
import xbmcgui
|
|
|
|
|
|
|
|
import clientinfo
|
|
|
|
import downloadutils
|
2018-01-15 02:17:57 +00:00
|
|
|
import read_embyserver as embyserver
|
2016-03-31 16:35:41 +00:00
|
|
|
import websocket_client as wsc
|
2018-01-08 02:13:11 +00:00
|
|
|
from utils import window, settings, language as lang, JSONRPC
|
2016-11-02 04:11:04 +00:00
|
|
|
from ga_client import GoogleAnalytics, log_error
|
2016-07-24 08:59:48 +00:00
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
log = logging.getLogger("EMBY."+__name__)
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
class Player(xbmc.Player):
|
|
|
|
|
|
|
|
# Borg - multiple instances, shared state
|
|
|
|
_shared_state = {}
|
|
|
|
|
|
|
|
played_info = {}
|
|
|
|
currentFile = None
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
self.__dict__ = self._shared_state
|
|
|
|
|
|
|
|
self.clientInfo = clientinfo.ClientInfo()
|
|
|
|
self.doUtils = downloadutils.DownloadUtils().downloadUrl
|
2018-01-15 02:17:57 +00:00
|
|
|
self.emby = embyserver.Read_EmbyServer()
|
2016-09-09 03:13:25 +00:00
|
|
|
self.ws = wsc.WebSocketClient()
|
2016-03-31 16:35:41 +00:00
|
|
|
self.xbmcplayer = xbmc.Player()
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Starting playback monitor.")
|
2016-10-07 04:15:23 +00:00
|
|
|
xbmc.Player.__init__(self)
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2018-01-08 02:13:11 +00:00
|
|
|
def set_audio_subs(self, audio_index=None, subs_index=None):
|
|
|
|
|
|
|
|
''' Only for after playback started
|
|
|
|
'''
|
|
|
|
player = xbmc.Player()
|
|
|
|
log.info("Setting audio: %s subs: %s", audio_index, subs_index)
|
|
|
|
|
|
|
|
if audio_index and len(player.getAvailableAudioStreams()) > 1:
|
|
|
|
player.setAudioStream(audio_index - 1)
|
|
|
|
|
|
|
|
if subs_index:
|
|
|
|
mapping = window('emby_%s.indexMapping.json' % self.current_file)
|
|
|
|
|
|
|
|
if subs_index == -1:
|
|
|
|
player.showSubtitles(False)
|
|
|
|
|
|
|
|
elif mapping:
|
|
|
|
external_index = mapping
|
|
|
|
# If there's external subtitles added via playbackutils
|
|
|
|
for index in external_index:
|
|
|
|
if external_index[index] == subs_index:
|
|
|
|
player.setSubtitleStream(int(index))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# User selected internal subtitles
|
|
|
|
external = len(external_index)
|
|
|
|
audio_tracks = len(player.getAvailableAudioStreams())
|
|
|
|
player.setSubtitleStream(external + subs_index - audio_tracks - 1)
|
|
|
|
else:
|
|
|
|
# Emby merges audio and subtitle index together
|
|
|
|
audio_tracks = len(player.getAvailableAudioStreams())
|
|
|
|
player.setSubtitleStream(subs_index - audio_tracks - 1)
|
|
|
|
|
2016-11-02 04:11:04 +00:00
|
|
|
@log_error()
|
2016-03-31 16:35:41 +00:00
|
|
|
def onPlayBackStarted(self):
|
|
|
|
# Will be called when xbmc starts playing a file
|
|
|
|
self.stopAll()
|
|
|
|
|
|
|
|
# Get current file
|
|
|
|
try:
|
2016-03-31 16:37:41 +00:00
|
|
|
currentFile = self.xbmcplayer.getPlayingFile()
|
2016-03-31 16:35:41 +00:00
|
|
|
xbmc.sleep(300)
|
|
|
|
except:
|
|
|
|
currentFile = ""
|
|
|
|
count = 0
|
|
|
|
while not currentFile:
|
|
|
|
xbmc.sleep(100)
|
|
|
|
try:
|
2016-03-31 16:37:41 +00:00
|
|
|
currentFile = self.xbmcplayer.getPlayingFile()
|
2016-03-31 16:35:41 +00:00
|
|
|
except: pass
|
|
|
|
|
|
|
|
if count == 5: # try 5 times
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Cancelling playback report...")
|
2016-03-31 16:35:41 +00:00
|
|
|
break
|
|
|
|
else: count += 1
|
|
|
|
|
2016-11-12 23:39:14 +00:00
|
|
|
# if we did not get the current file return
|
|
|
|
if currentFile == "":
|
|
|
|
return
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2016-11-12 23:39:14 +00:00
|
|
|
# process the playing file
|
|
|
|
self.currentFile = currentFile
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2016-11-12 23:39:14 +00:00
|
|
|
# We may need to wait for info to be set in kodi monitor
|
2018-01-08 02:13:11 +00:00
|
|
|
item = window('emby_%s.json' % currentFile)
|
|
|
|
#itemId = window("emby_%s.itemid" % currentFile)
|
2016-11-12 23:39:14 +00:00
|
|
|
tryCount = 0
|
2018-01-08 02:13:11 +00:00
|
|
|
while not item:
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2016-11-12 23:39:14 +00:00
|
|
|
xbmc.sleep(200)
|
2018-01-08 02:13:11 +00:00
|
|
|
item = window('emby_%s.json' % currentFile)
|
2016-11-12 23:39:14 +00:00
|
|
|
if tryCount == 20: # try 20 times or about 10 seconds
|
2018-01-08 02:13:11 +00:00
|
|
|
log.info("Could not find item, cancelling playback report...")
|
2016-11-12 23:39:14 +00:00
|
|
|
break
|
|
|
|
else: tryCount += 1
|
|
|
|
|
|
|
|
else:
|
2018-01-08 02:13:11 +00:00
|
|
|
item_id = item.get('id')
|
|
|
|
log.info("ONPLAYBACK_STARTED: %s itemid: %s" % (currentFile, item_id))
|
2016-11-12 23:39:14 +00:00
|
|
|
|
|
|
|
# Only proceed if an itemId was found.
|
2018-01-08 02:13:11 +00:00
|
|
|
runtime = item.get('runtime')
|
|
|
|
refresh_id = item.get('refreshid')
|
|
|
|
play_method = item.get('playmethod')
|
|
|
|
item_type = item.get('type')
|
2018-01-15 02:17:57 +00:00
|
|
|
playsession_id = item.get('playsession_id')
|
|
|
|
mediasource_id = item.get('mediasource_id')
|
|
|
|
|
2016-11-12 23:39:14 +00:00
|
|
|
|
2018-01-08 02:13:11 +00:00
|
|
|
#self.set_audio_subs(item.get('forcedaudio'), item.get('forcedsubs'))
|
|
|
|
|
|
|
|
window('emby_skipWatched%s' % item_id, value="true")
|
2016-11-12 23:39:14 +00:00
|
|
|
customseek = window('emby_customPlaylist.seektime')
|
|
|
|
if window('emby_customPlaylist') == "true" and customseek:
|
|
|
|
# Start at, when using custom playlist (play to Kodi from webclient)
|
|
|
|
log.info("Seeking to: %s" % customseek)
|
|
|
|
self.xbmcplayer.seekTime(int(customseek)/10000000.0)
|
|
|
|
window('emby_customPlaylist.seektime', clear=True)
|
|
|
|
|
|
|
|
try:
|
2016-03-31 16:37:41 +00:00
|
|
|
seekTime = self.xbmcplayer.getTime()
|
2016-11-12 23:39:14 +00:00
|
|
|
except:
|
|
|
|
# at this point we should be playing and if not then bail out
|
|
|
|
return
|
|
|
|
|
|
|
|
# Get playback volume
|
2018-01-08 02:13:11 +00:00
|
|
|
result = JSONRPC('Application.GetProperties').execute({'properties': ["volume", "muted"]})
|
2016-11-12 23:39:14 +00:00
|
|
|
result = result.get('result')
|
|
|
|
|
|
|
|
volume = result.get('volume')
|
|
|
|
muted = result.get('muted')
|
|
|
|
|
|
|
|
# Postdata structure to send to Emby server
|
|
|
|
url = "{server}/emby/Sessions/Playing"
|
|
|
|
postdata = {
|
|
|
|
|
2018-01-15 02:17:57 +00:00
|
|
|
'QueueableMediaTypes': "Video,Audio",
|
2016-11-12 23:39:14 +00:00
|
|
|
'CanSeek': True,
|
2018-01-08 02:13:11 +00:00
|
|
|
'ItemId': item_id,
|
2018-01-15 02:17:57 +00:00
|
|
|
'MediaSourceId': mediasource_id or item_id,
|
2018-01-08 02:13:11 +00:00
|
|
|
'PlayMethod': play_method,
|
2016-11-12 23:39:14 +00:00
|
|
|
'VolumeLevel': volume,
|
|
|
|
'PositionTicks': int(seekTime * 10000000),
|
2018-01-15 02:17:57 +00:00
|
|
|
'IsMuted': muted,
|
|
|
|
'PlaySessionId': playsession_id
|
2016-11-12 23:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get the current audio track and subtitles
|
2018-01-08 02:13:11 +00:00
|
|
|
if play_method == "Transcode":
|
2016-11-12 23:39:14 +00:00
|
|
|
# property set in PlayUtils.py
|
|
|
|
postdata['AudioStreamIndex'] = window("%sAudioStreamIndex" % currentFile)
|
|
|
|
postdata['SubtitleStreamIndex'] = window("%sSubtitleStreamIndex" % currentFile)
|
|
|
|
else:
|
|
|
|
# Get the current kodi audio and subtitles and convert to Emby equivalent
|
2018-01-08 02:13:11 +00:00
|
|
|
params = {
|
|
|
|
'playerid': 1,
|
|
|
|
'properties': ["currentsubtitle","currentaudiostream","subtitleenabled"]
|
2016-03-31 16:35:41 +00:00
|
|
|
}
|
2018-01-08 02:13:11 +00:00
|
|
|
result = JSONRPC('Player.GetProperties').execute(params)
|
2017-01-11 01:12:10 +00:00
|
|
|
tracks_data = None
|
|
|
|
try:
|
|
|
|
tracks_data = json.loads(result)
|
|
|
|
tracks_data = tracks_data.get('result')
|
|
|
|
except:
|
|
|
|
tracks_data = None
|
|
|
|
|
2017-01-14 00:38:00 +00:00
|
|
|
try: # Audio tracks
|
|
|
|
indexAudio = tracks_data['currentaudiostream']['index']
|
|
|
|
except:
|
|
|
|
indexAudio = 0
|
|
|
|
|
|
|
|
try: # Subtitles tracks
|
|
|
|
indexSubs = tracks_data['currentsubtitle']['index']
|
|
|
|
except:
|
|
|
|
indexSubs = 0
|
|
|
|
|
|
|
|
try: # If subtitles are enabled
|
|
|
|
subsEnabled = tracks_data['subtitleenabled']
|
|
|
|
except:
|
|
|
|
subsEnabled = ""
|
2016-11-12 23:39:14 +00:00
|
|
|
|
|
|
|
# Postdata for the audio
|
|
|
|
postdata['AudioStreamIndex'] = indexAudio + 1
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2016-11-12 23:39:14 +00:00
|
|
|
# Postdata for the subtitles
|
|
|
|
if subsEnabled and len(xbmc.Player().getAvailableSubtitleStreams()) > 0:
|
|
|
|
|
|
|
|
# Number of audiotracks to help get Emby Index
|
|
|
|
audioTracks = len(xbmc.Player().getAvailableAudioStreams())
|
2018-01-08 02:13:11 +00:00
|
|
|
mapping = window("emby_%s.indexMapping" % currentFile)
|
2016-11-12 23:39:14 +00:00
|
|
|
|
|
|
|
if mapping: # Set in playbackutils.py
|
|
|
|
|
|
|
|
log.debug("Mapping for external subtitles index: %s" % mapping)
|
|
|
|
externalIndex = json.loads(mapping)
|
|
|
|
|
|
|
|
if externalIndex.get(str(indexSubs)):
|
|
|
|
# If the current subtitle is in the mapping
|
|
|
|
postdata['SubtitleStreamIndex'] = externalIndex[str(indexSubs)]
|
|
|
|
else:
|
|
|
|
# Internal subtitle currently selected
|
|
|
|
subindex = indexSubs - len(externalIndex) + audioTracks + 1
|
|
|
|
postdata['SubtitleStreamIndex'] = subindex
|
|
|
|
|
|
|
|
else: # Direct paths enabled scenario or no external subtitles set
|
|
|
|
postdata['SubtitleStreamIndex'] = indexSubs + audioTracks + 1
|
|
|
|
else:
|
|
|
|
postdata['SubtitleStreamIndex'] = ""
|
|
|
|
|
|
|
|
|
|
|
|
# Post playback to server
|
|
|
|
log.debug("Sending POST play started: %s." % postdata)
|
|
|
|
self.doUtils(url, postBody=postdata, action_type="POST")
|
|
|
|
|
|
|
|
# Ensure we do have a runtime
|
|
|
|
try:
|
|
|
|
runtime = int(runtime)
|
|
|
|
except ValueError:
|
2017-01-07 08:19:39 +00:00
|
|
|
try:
|
|
|
|
runtime = int(self.xbmcplayer.getTotalTime())
|
|
|
|
log.info("Runtime is missing, Kodi runtime: %s" % runtime)
|
|
|
|
except:
|
|
|
|
runtime = 0
|
|
|
|
log.info("Runtime is missing, Using Zero")
|
2016-11-12 23:39:14 +00:00
|
|
|
|
|
|
|
# Save data map for updates and position calls
|
|
|
|
data = {
|
|
|
|
|
|
|
|
'runtime': runtime,
|
2018-01-08 02:13:11 +00:00
|
|
|
'item_id': item_id,
|
2018-01-15 02:17:57 +00:00
|
|
|
'mediasource_id': mediasource_id,
|
2016-11-12 23:39:14 +00:00
|
|
|
'refresh_id': refresh_id,
|
|
|
|
'currentfile': currentFile,
|
|
|
|
'AudioStreamIndex': postdata['AudioStreamIndex'],
|
|
|
|
'SubtitleStreamIndex': postdata['SubtitleStreamIndex'],
|
2018-01-08 02:13:11 +00:00
|
|
|
'playmethod': play_method,
|
|
|
|
'Type': item_type,
|
2018-01-15 02:17:57 +00:00
|
|
|
'currentPosition': int(seekTime),
|
|
|
|
'playsession_id': playsession_id
|
2016-11-12 23:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.played_info[currentFile] = data
|
|
|
|
log.info("ADDING_FILE: %s" % self.played_info)
|
|
|
|
|
|
|
|
ga = GoogleAnalytics()
|
2018-01-08 02:13:11 +00:00
|
|
|
ga.sendEventData("PlayAction", item_type, play_method)
|
|
|
|
ga.sendScreenView(item_type)
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
def reportPlayback(self):
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("reportPlayback Called")
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
# Get current file
|
|
|
|
currentFile = self.currentFile
|
|
|
|
data = self.played_info.get(currentFile)
|
|
|
|
|
|
|
|
# only report playback if emby has initiated the playback (item_id has value)
|
|
|
|
if data:
|
|
|
|
# Get playback information
|
|
|
|
itemId = data['item_id']
|
|
|
|
audioindex = data['AudioStreamIndex']
|
|
|
|
subtitleindex = data['SubtitleStreamIndex']
|
|
|
|
playTime = data['currentPosition']
|
|
|
|
playMethod = data['playmethod']
|
|
|
|
paused = data.get('paused', False)
|
2018-01-15 02:17:57 +00:00
|
|
|
playsession_id = data.get('playsession_id')
|
|
|
|
mediasource_id = data.get('mediasource_id')
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Get playback volume
|
2018-01-08 02:13:11 +00:00
|
|
|
result = JSONRPC('Application.GetProperties').execute({'properties': ["volume", "muted"]})
|
2016-03-31 16:35:41 +00:00
|
|
|
result = result.get('result')
|
|
|
|
|
|
|
|
volume = result.get('volume')
|
|
|
|
muted = result.get('muted')
|
|
|
|
|
|
|
|
# Postdata for the websocketclient report
|
|
|
|
postdata = {
|
|
|
|
|
|
|
|
'QueueableMediaTypes': "Video",
|
|
|
|
'CanSeek': True,
|
|
|
|
'ItemId': itemId,
|
2018-01-15 02:17:57 +00:00
|
|
|
'MediaSourceId': mediasource_id or itemId,
|
2016-03-31 16:35:41 +00:00
|
|
|
'PlayMethod': playMethod,
|
|
|
|
'PositionTicks': int(playTime * 10000000),
|
|
|
|
'IsPaused': paused,
|
|
|
|
'VolumeLevel': volume,
|
2018-01-15 02:17:57 +00:00
|
|
|
'IsMuted': muted,
|
|
|
|
'PlaySessionId': playsession_id
|
2016-03-31 16:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if playMethod == "Transcode":
|
|
|
|
# Track can't be changed, keep reporting the same index
|
|
|
|
postdata['AudioStreamIndex'] = audioindex
|
|
|
|
postdata['AudioStreamIndex'] = subtitleindex
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Get current audio and subtitles track
|
2018-01-08 02:13:11 +00:00
|
|
|
params = {
|
|
|
|
'playerid': 1,
|
|
|
|
'properties': ["currentsubtitle","currentaudiostream","subtitleenabled"]
|
|
|
|
}
|
|
|
|
result = JSONRPC('Player.GetProperties').execute(params)
|
2016-03-31 16:35:41 +00:00
|
|
|
result = result.get('result')
|
|
|
|
|
|
|
|
try: # Audio tracks
|
|
|
|
indexAudio = result['currentaudiostream']['index']
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
indexAudio = 0
|
|
|
|
|
|
|
|
try: # Subtitles tracks
|
|
|
|
indexSubs = result['currentsubtitle']['index']
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
indexSubs = 0
|
|
|
|
|
|
|
|
try: # If subtitles are enabled
|
|
|
|
subsEnabled = result['subtitleenabled']
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
subsEnabled = ""
|
|
|
|
|
|
|
|
# Postdata for the audio
|
|
|
|
data['AudioStreamIndex'], postdata['AudioStreamIndex'] = [indexAudio + 1] * 2
|
|
|
|
|
|
|
|
# Postdata for the subtitles
|
|
|
|
if subsEnabled and len(xbmc.Player().getAvailableSubtitleStreams()) > 0:
|
|
|
|
|
|
|
|
# Number of audiotracks to help get Emby Index
|
|
|
|
audioTracks = len(xbmc.Player().getAvailableAudioStreams())
|
2016-06-18 03:03:28 +00:00
|
|
|
mapping = window("emby_%s.indexMapping" % currentFile)
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
if mapping: # Set in PlaybackUtils.py
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Mapping for external subtitles index: %s" % mapping)
|
2016-03-31 16:35:41 +00:00
|
|
|
externalIndex = json.loads(mapping)
|
|
|
|
|
|
|
|
if externalIndex.get(str(indexSubs)):
|
|
|
|
# If the current subtitle is in the mapping
|
|
|
|
subindex = [externalIndex[str(indexSubs)]] * 2
|
|
|
|
data['SubtitleStreamIndex'], postdata['SubtitleStreamIndex'] = subindex
|
|
|
|
else:
|
|
|
|
# Internal subtitle currently selected
|
|
|
|
subindex = [indexSubs - len(externalIndex) + audioTracks + 1] * 2
|
|
|
|
data['SubtitleStreamIndex'], postdata['SubtitleStreamIndex'] = subindex
|
|
|
|
|
|
|
|
else: # Direct paths enabled scenario or no external subtitles set
|
|
|
|
subindex = [indexSubs + audioTracks + 1] * 2
|
|
|
|
data['SubtitleStreamIndex'], postdata['SubtitleStreamIndex'] = subindex
|
|
|
|
else:
|
|
|
|
data['SubtitleStreamIndex'], postdata['SubtitleStreamIndex'] = [""] * 2
|
|
|
|
|
|
|
|
# Report progress via websocketclient
|
|
|
|
postdata = json.dumps(postdata)
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Report: %s" % postdata)
|
2016-09-07 07:14:02 +00:00
|
|
|
self.ws.send_progress_update(postdata)
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2016-11-02 04:11:04 +00:00
|
|
|
@log_error()
|
2016-03-31 16:35:41 +00:00
|
|
|
def onPlayBackPaused(self):
|
|
|
|
|
|
|
|
currentFile = self.currentFile
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("PLAYBACK_PAUSED: %s" % currentFile)
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
if self.played_info.get(currentFile):
|
|
|
|
self.played_info[currentFile]['paused'] = True
|
|
|
|
|
|
|
|
self.reportPlayback()
|
|
|
|
|
2016-11-02 04:11:04 +00:00
|
|
|
@log_error()
|
2016-03-31 16:35:41 +00:00
|
|
|
def onPlayBackResumed(self):
|
|
|
|
|
|
|
|
currentFile = self.currentFile
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("PLAYBACK_RESUMED: %s" % currentFile)
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
if self.played_info.get(currentFile):
|
|
|
|
self.played_info[currentFile]['paused'] = False
|
|
|
|
|
|
|
|
self.reportPlayback()
|
|
|
|
|
2016-11-02 04:11:04 +00:00
|
|
|
@log_error()
|
2016-03-31 16:35:41 +00:00
|
|
|
def onPlayBackSeek(self, time, seekOffset):
|
|
|
|
# Make position when seeking a bit more accurate
|
|
|
|
currentFile = self.currentFile
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("PLAYBACK_SEEK: %s" % currentFile)
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
if self.played_info.get(currentFile):
|
2016-11-10 00:49:38 +00:00
|
|
|
position = None
|
|
|
|
try:
|
|
|
|
position = self.xbmcplayer.getTime()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if position is not None:
|
|
|
|
self.played_info[currentFile]['currentPosition'] = position
|
|
|
|
self.reportPlayback()
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2016-11-02 04:11:04 +00:00
|
|
|
@log_error()
|
2016-03-31 16:35:41 +00:00
|
|
|
def onPlayBackStopped(self):
|
|
|
|
# Will be called when user stops xbmc playing a file
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("ONPLAYBACK_STOPPED")
|
2016-03-31 16:35:41 +00:00
|
|
|
window('emby_customPlaylist.seektime', clear=True)
|
|
|
|
self.stopAll()
|
|
|
|
|
2016-11-02 04:11:04 +00:00
|
|
|
@log_error()
|
2016-03-31 16:35:41 +00:00
|
|
|
def onPlayBackEnded(self):
|
|
|
|
# Will be called when xbmc stops playing a file
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("ONPLAYBACK_ENDED")
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_customPlaylist.seektime', clear=True)
|
2016-03-31 16:35:41 +00:00
|
|
|
self.stopAll()
|
|
|
|
|
|
|
|
def stopAll(self):
|
|
|
|
|
|
|
|
if not self.played_info:
|
2018-01-10 04:30:05 +00:00
|
|
|
return
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Played_information: %s" % self.played_info)
|
2016-03-31 16:35:41 +00:00
|
|
|
# Process each items
|
|
|
|
for item in self.played_info:
|
|
|
|
|
|
|
|
data = self.played_info.get(item)
|
|
|
|
if data:
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Item path: %s" % item)
|
|
|
|
log.debug("Item data: %s" % data)
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
runtime = data['runtime']
|
|
|
|
currentPosition = data['currentPosition']
|
|
|
|
itemid = data['item_id']
|
|
|
|
refresh_id = data['refresh_id']
|
|
|
|
currentFile = data['currentfile']
|
2016-04-06 18:37:19 +00:00
|
|
|
media_type = data['Type']
|
2016-03-31 16:35:41 +00:00
|
|
|
playMethod = data['playmethod']
|
|
|
|
|
|
|
|
# Prevent manually mark as watched in Kodi monitor
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_skipWatched%s' % itemid, value="true")
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2016-10-09 02:07:15 +00:00
|
|
|
self.stopPlayback(data)
|
|
|
|
|
2016-03-31 16:35:41 +00:00
|
|
|
if currentPosition and runtime:
|
|
|
|
try:
|
2018-01-08 02:13:11 +00:00
|
|
|
if window('emby.external'):
|
|
|
|
window('emby.external', clear=True)
|
|
|
|
raise ValueError
|
|
|
|
|
2016-03-31 16:35:41 +00:00
|
|
|
percentComplete = (currentPosition * 10000000) / int(runtime)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
# Runtime is 0.
|
|
|
|
percentComplete = 0
|
2018-01-08 02:13:11 +00:00
|
|
|
except ValueError:
|
|
|
|
percentComplete = 100
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
markPlayedAt = float(settings('markPlayed')) / 100
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Percent complete: %s Mark played at: %s"
|
|
|
|
% (percentComplete, markPlayedAt))
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
# Send the delete action to the server.
|
|
|
|
offerDelete = False
|
|
|
|
|
2016-04-06 18:37:19 +00:00
|
|
|
if media_type == "Episode" and settings('deleteTV') == "true":
|
2016-03-31 16:35:41 +00:00
|
|
|
offerDelete = True
|
2016-04-06 18:37:19 +00:00
|
|
|
elif media_type == "Movie" and settings('deleteMovies') == "true":
|
2016-03-31 16:35:41 +00:00
|
|
|
offerDelete = True
|
|
|
|
|
|
|
|
if settings('offerDelete') != "true":
|
|
|
|
# Delete could be disabled, even if the subsetting is enabled.
|
|
|
|
offerDelete = False
|
|
|
|
|
|
|
|
if percentComplete >= markPlayedAt and offerDelete:
|
|
|
|
resp = xbmcgui.Dialog().yesno(lang(30091), lang(33015), autoclose=120000)
|
2016-07-18 19:42:33 +00:00
|
|
|
if resp:
|
|
|
|
url = "{server}/emby/Items/%s?format=json" % itemid
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Deleting request: %s" % itemid)
|
2016-07-18 19:42:33 +00:00
|
|
|
self.doUtils(url, action_type="DELETE")
|
2016-07-18 22:56:39 +00:00
|
|
|
else:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("User skipped deletion.")
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2018-01-08 02:13:11 +00:00
|
|
|
window('emby.external_check', clear=True)
|
|
|
|
|
2018-01-10 04:30:05 +00:00
|
|
|
##### Track end of playlist
|
|
|
|
if media_type == "Audio":
|
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
|
|
|
|
else:
|
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
|
|
|
|
|
2018-01-11 08:09:18 +00:00
|
|
|
if int(playlist.getposition()) < 0:
|
|
|
|
''' If playback is stopped within the first 30 seconds,
|
|
|
|
Kodi doesn't consider this watched and playlist should not be cleared.
|
|
|
|
When the end is reached, position is -1.
|
|
|
|
'''
|
2018-01-10 22:44:19 +00:00
|
|
|
log.info("Clear playlist, end detected.")
|
2018-01-10 04:30:05 +00:00
|
|
|
playlist.clear()
|
|
|
|
|
2016-03-31 16:35:41 +00:00
|
|
|
# Stop transcoding
|
|
|
|
if playMethod == "Transcode":
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Transcoding for %s terminated." % itemid)
|
2016-09-05 03:33:34 +00:00
|
|
|
deviceId = self.clientInfo.get_device_id()
|
2016-03-31 16:35:41 +00:00
|
|
|
url = "{server}/emby/Videos/ActiveEncodings?DeviceId=%s" % deviceId
|
2016-04-04 21:21:05 +00:00
|
|
|
self.doUtils(url, action_type="DELETE")
|
2016-08-30 05:22:54 +00:00
|
|
|
|
|
|
|
path = xbmc.translatePath(
|
|
|
|
"special://profile/addon_data/plugin.video.emby/temp/").decode('utf-8')
|
|
|
|
|
|
|
|
dirs, files = xbmcvfs.listdir(path)
|
|
|
|
for file in files:
|
|
|
|
xbmcvfs.delete("%s%s" % (path, file))
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
self.played_info.clear()
|
2016-10-14 03:35:34 +00:00
|
|
|
|
|
|
|
ga = GoogleAnalytics()
|
|
|
|
ga.sendEventData("PlayAction", "Stopped")
|
2016-03-31 16:35:41 +00:00
|
|
|
|
|
|
|
def stopPlayback(self, data):
|
|
|
|
|
2018-01-15 02:17:57 +00:00
|
|
|
log.debug("stopPlayback called.")
|
2016-03-31 16:35:41 +00:00
|
|
|
|
2018-01-15 02:17:57 +00:00
|
|
|
position_ticks = int(data['currentPosition'] * 10000000)
|
|
|
|
position = data['runtime'] if position_ticks and window('emby.external') else position_ticks
|
|
|
|
|
|
|
|
self.emby.stop_playback(data['item_id'], position, data['playsession_id'], data.get('mediasource_id'))
|