mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
Merge pull request #675 from kelciour/mpv-changes-2
A few mpv changes 2
This commit is contained in:
commit
35975b142b
2 changed files with 39 additions and 19 deletions
|
@ -245,10 +245,13 @@ class MPVBase:
|
||||||
else:
|
else:
|
||||||
r, w, e = select.select([self._sock], [], [], 1)
|
r, w, e = select.select([self._sock], [], [], 1)
|
||||||
if r:
|
if r:
|
||||||
|
try:
|
||||||
b = self._sock.recv(1024)
|
b = self._sock.recv(1024)
|
||||||
if not b:
|
if not b:
|
||||||
break
|
break
|
||||||
buf += b
|
buf += b
|
||||||
|
except ConnectionResetError:
|
||||||
|
return
|
||||||
|
|
||||||
newline = buf.find(b"\n")
|
newline = buf.find(b"\n")
|
||||||
while newline >= 0:
|
while newline >= 0:
|
||||||
|
@ -373,7 +376,7 @@ class MPVBase:
|
||||||
return self._get_response(timeout)
|
return self._get_response(timeout)
|
||||||
except MPVCommandError as e:
|
except MPVCommandError as e:
|
||||||
raise MPVCommandError("%r: %s" % (message["command"], e))
|
raise MPVCommandError("%r: %s" % (message["command"], e))
|
||||||
except MPVTimeoutError as e:
|
except Exception as e:
|
||||||
if _retry:
|
if _retry:
|
||||||
print("mpv timed out, restarting")
|
print("mpv timed out, restarting")
|
||||||
self._stop_process()
|
self._stop_process()
|
||||||
|
@ -381,6 +384,11 @@ class MPVBase:
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def _register_callbacks(self):
|
||||||
|
"""Will be called after mpv restart to reinitialize callbacks
|
||||||
|
defined in MPV subclass
|
||||||
|
"""
|
||||||
|
|
||||||
#
|
#
|
||||||
# Public API
|
# Public API
|
||||||
#
|
#
|
||||||
|
@ -400,6 +408,7 @@ class MPVBase:
|
||||||
self._start_socket()
|
self._start_socket()
|
||||||
self._prepare_thread()
|
self._prepare_thread()
|
||||||
self._start_thread()
|
self._start_thread()
|
||||||
|
self._register_callbacks()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Shutdown the mpv process and our communication setup.
|
"""Shutdown the mpv process and our communication setup.
|
||||||
|
@ -438,6 +447,9 @@ class MPV(MPVBase):
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
self._register_callbacks()
|
||||||
|
|
||||||
|
def _register_callbacks(self):
|
||||||
self._callbacks = {}
|
self._callbacks = {}
|
||||||
self._property_serials = {}
|
self._property_serials = {}
|
||||||
self._new_serial = iter(range(sys.maxsize))
|
self._new_serial = iter(range(sys.maxsize))
|
||||||
|
@ -448,6 +460,10 @@ class MPV(MPVBase):
|
||||||
if not inspect.ismethod(method):
|
if not inspect.ismethod(method):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Bypass MPVError: no such event 'init'
|
||||||
|
if method_name == "on_init":
|
||||||
|
continue
|
||||||
|
|
||||||
if method_name.startswith("on_property_"):
|
if method_name.startswith("on_property_"):
|
||||||
name = method_name[12:]
|
name = method_name[12:]
|
||||||
name = name.replace("_", "-")
|
name = name.replace("_", "-")
|
||||||
|
@ -479,24 +495,18 @@ class MPV(MPVBase):
|
||||||
"""Start up the communication threads.
|
"""Start up the communication threads.
|
||||||
"""
|
"""
|
||||||
super()._start_thread()
|
super()._start_thread()
|
||||||
|
if not hasattr(self, "_event_thread"):
|
||||||
self._event_thread = threading.Thread(target=self._event_reader)
|
self._event_thread = threading.Thread(target=self._event_reader)
|
||||||
self._event_thread.daemon = True
|
self._event_thread.daemon = True
|
||||||
self._event_thread.start()
|
self._event_thread.start()
|
||||||
|
|
||||||
def _stop_thread(self):
|
|
||||||
"""Stop the communication threads.
|
|
||||||
"""
|
|
||||||
super()._stop_thread()
|
|
||||||
if hasattr(self, "_event_thread"):
|
|
||||||
self._event_thread.join()
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Event/callback API
|
# Event/callback API
|
||||||
#
|
#
|
||||||
def _event_reader(self):
|
def _event_reader(self):
|
||||||
"""Collect incoming event messages and call the event handler.
|
"""Collect incoming event messages and call the event handler.
|
||||||
"""
|
"""
|
||||||
while not self._stop_event.is_set():
|
while True:
|
||||||
message = self._get_event(timeout=1)
|
message = self._get_event(timeout=1)
|
||||||
if message is None:
|
if message is None:
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -19,7 +19,7 @@ from anki.lang import _
|
||||||
from anki.sound import AV_REF_RE, AVTag, SoundOrVideoTag
|
from anki.sound import AV_REF_RE, AVTag, SoundOrVideoTag
|
||||||
from anki.utils import isLin, isMac, isWin
|
from anki.utils import isLin, isMac, isWin
|
||||||
from aqt import gui_hooks
|
from aqt import gui_hooks
|
||||||
from aqt.mpv import MPV, MPVBase
|
from aqt.mpv import MPV, MPVBase, MPVCommandError
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
from aqt.taskman import TaskManager
|
from aqt.taskman import TaskManager
|
||||||
from aqt.utils import restoreGeom, saveGeom, showWarning, startup_info
|
from aqt.utils import restoreGeom, saveGeom, showWarning, startup_info
|
||||||
|
@ -334,6 +334,16 @@ class MpvManager(MPV, SoundOrVideoPlayer):
|
||||||
self.default_argv += ["--config-dir=" + base_path]
|
self.default_argv += ["--config-dir=" + base_path]
|
||||||
super().__init__(window_id=None, debug=False)
|
super().__init__(window_id=None, debug=False)
|
||||||
|
|
||||||
|
def on_init(self) -> None:
|
||||||
|
try:
|
||||||
|
self.command("keybind", "q", "stop")
|
||||||
|
self.command("keybind", "Q", "stop")
|
||||||
|
self.command("keybind", "CLOSE_WIN", "stop")
|
||||||
|
self.command("keybind", "ctrl+w", "stop")
|
||||||
|
self.command("keybind", "ctrl+c", "stop")
|
||||||
|
except MPVCommandError:
|
||||||
|
print("mpv too old")
|
||||||
|
|
||||||
def play(self, tag: AVTag, on_done: OnDoneCallback) -> None:
|
def play(self, tag: AVTag, on_done: OnDoneCallback) -> None:
|
||||||
assert isinstance(tag, SoundOrVideoTag)
|
assert isinstance(tag, SoundOrVideoTag)
|
||||||
self._on_done = on_done
|
self._on_done = on_done
|
||||||
|
@ -350,8 +360,8 @@ class MpvManager(MPV, SoundOrVideoPlayer):
|
||||||
def seek_relative(self, secs: int) -> None:
|
def seek_relative(self, secs: int) -> None:
|
||||||
self.command("seek", secs, "relative")
|
self.command("seek", secs, "relative")
|
||||||
|
|
||||||
def on_property_idle_active(self, val) -> None:
|
def on_end_file(self) -> None:
|
||||||
if val and self._on_done:
|
if self._on_done:
|
||||||
self._on_done()
|
self._on_done()
|
||||||
|
|
||||||
def shutdown(self) -> None:
|
def shutdown(self) -> None:
|
||||||
|
|
Loading…
Reference in a new issue