class PositionTracker(object): """ Tracks the last position of the player. This is necessary because Player.getTime() is not valid after the callback to Player.onPlayBackStopped() has completed. """ HISTORY_SECS = 5 # Number of seconds of history to keep around def __init__(self, player): self._player = player self._lastPos = 0.0 self._tracker = BoundedEvictingQueue( (1000 / SLEEP_MILLIS) * self.HISTORY_SECS) self._history = [] def onPlayBackStarted(self): log.debug('Starting position tracker...') self._tracker = threading.Thread(name='Position Tracker', target=self._trackPosition) self._tracker.start() def onPlayBackStopped(self): if self._tracker.isAlive(): log.debug('Position tracker stop called. Still alive = %s' % self._tracker.isAlive()) else: log.debug('Position tracker thread already dead.') def onPlayBackEnded(self): self.onPlayBackStopped() def getHistory(self, howFarBack): """Returns a list of TrackerSamples from 'howFarBack' seconds ago.""" endPos = self._lastPos startPos = endPos - howFarBack slice = [] for sample in self._history: if startPos <= sample.pos and sample.pos <= endPos: slice.append(sample) log.debug('Tracker history for %s secs = [%s] %s' % (howFarBack, len(slice), slice)) return slice def getLastPosition(self): return self._lastPos def _trackPosition(self): """Method run in a separate thread. Tracks last position of player as long as it is playing""" try: while self._player.isPlaying(): self._lastPos = self._player.getTime() self._history.append(TrackerSample(time.time(), self._lastPos)) #log.debug('Tracker time = %s' % self._lastPos) xbmc.sleep(SLEEP_MILLIS) log.debug('Position tracker thread exiting with lastPos = %s' % self.getLastPosition()) except: log.exception('_trackPosition catchall')
class PositionTracker(object): """ Tracks the last position of the player. This is necessary because Player.getTime() is not valid after the callback to Player.onPlayBackStopped() has completed. """ HISTORY_SECS = 5 # Number of seconds of history to keep around def __init__(self, player): self._player = player self._lastPos = 0.0 self._tracker = BoundedEvictingQueue((1000/SLEEP_MILLIS) * self.HISTORY_SECS) self._history = [] def onPlayBackStarted(self): log.debug('Starting position tracker...') self._tracker = threading.Thread( name='Position Tracker', target = self._trackPosition) self._tracker.start() def onPlayBackStopped(self): if self._tracker.isAlive(): log.debug('Position tracker stop called. Still alive = %s' % self._tracker.isAlive()) else: log.debug('Position tracker thread already dead.') def onPlayBackEnded(self): self.onPlayBackStopped() def getHistory(self, howFarBack): """Returns a list of TrackerSamples from 'howFarBack' seconds ago.""" endPos = self._lastPos startPos = endPos - howFarBack slice = [] for sample in self._history: if startPos <= sample.pos and sample.pos <= endPos: slice.append(sample) log.debug('Tracker history for %s secs = [%s] %s' % (howFarBack, len(slice), slice)) return slice def getLastPosition(self): return self._lastPos def _trackPosition(self): """Method run in a separate thread. Tracks last position of player as long as it is playing""" try: while self._player.isPlaying(): self._lastPos = self._player.getTime() self._history.append(TrackerSample(time.time(), self._lastPos)) #log.debug('Tracker time = %s' % self._lastPos) xbmc.sleep(SLEEP_MILLIS) log.debug('Position tracker thread exiting with lastPos = %s' % self.getLastPosition()) except: log.exception('_trackPosition catchall')
def __init__(self, player): self._player = player self._lastPos = 0.0 self._tracker = BoundedEvictingQueue((1000/SLEEP_MILLIS) * self.HISTORY_SECS) self._history = []
def __init__(self, player): self._player = player self._lastPos = 0.0 self._tracker = BoundedEvictingQueue( (1000 / SLEEP_MILLIS) * self.HISTORY_SECS) self._history = []