Skip to content

Commit

Permalink
Prevent finished_callback skipping videos.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwalton3 committed Jul 20, 2020
1 parent 564259d commit 8ea63c1
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions plex_mpv_shim/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import requests
import urllib.parse

from threading import RLock
from threading import RLock, Lock
from queue import Queue

from . import conffile
Expand Down Expand Up @@ -78,6 +78,7 @@ def __init__(self):
extra_options = {}
self._video = None
self._lock = RLock()
self._finished_lock = Lock()
self.last_update = Timer()
self.__part = 1
self.timeline_trigger = None
Expand Down Expand Up @@ -225,13 +226,15 @@ def handle_debug():
@self._player.property_observer('eof-reached')
def handle_end(_name, reached_end):
if self._video and reached_end:
self.put_task(self.finished_callback)
has_lock = self._finished_lock.acquire(False)
self.put_task(self.finished_callback, has_lock)

# Fires at the end.
@self._player.event_callback('idle')
def handle_end_idle(event):
if self._video:
self.put_task(self.finished_callback)
has_lock = self._finished_lock.acquire(False)
self.put_task(self.finished_callback, has_lock)

# Put a task to the event queue.
# This ensures the task executes outside
Expand Down Expand Up @@ -332,6 +335,8 @@ def _play_media(self, video, url, offset=0):

self._player.pause = False
self.timeline_handle()
if self._finished_lock.locked():
self._finished_lock.release()

def exec_stop_cmd(self):
if settings.stop_cmd:
Expand Down Expand Up @@ -403,24 +408,30 @@ def is_paused(self):
return False

@synchronous('_lock')
def finished_callback(self):
def finished_callback(self, has_lock):
if not self._video:
return

self._video.set_played()

if self._video.is_multipart():
log.debug("PlayerManager::finished_callback media is multi-part, checking for next part")
# Try to select the next part
next_part = self.__part+1
if self._video.select_part(next_part):
self.__part = next_part
log.debug("PlayerManager::finished_callback starting next part")
self.play(self._video)
if has_lock:
log.debug("PlayerManager::finished_callback media is multi-part, checking for next part")
# Try to select the next part
next_part = self.__part+1
if self._video.select_part(next_part):
self.__part = next_part
log.debug("PlayerManager::finished_callback starting next part")
self.play(self._video)
else:
log.debug("PlayerManager::finished_callback No lock, skipping...")

elif self._video.parent.has_next and settings.auto_play:
log.debug("PlayerManager::finished_callback starting next episode")
self.play(self._video.parent.get_next().get_video(0))
if has_lock:
log.debug("PlayerManager::finished_callback starting next episode")
self.play(self._video.parent.get_next().get_video(0))
else:
log.debug("PlayerManager::finished_callback No lock, skipping...")

else:
if settings.media_ended_cmd:
Expand Down

0 comments on commit 8ea63c1

Please sign in to comment.