Remove asyncio for windows support

This commit is contained in:
jaseg 2016-01-04 13:20:42 +01:00
parent fb1704c4dc
commit a1c170d6ff
1 changed files with 7 additions and 22 deletions

29
mpv.py
View File

@ -134,7 +134,7 @@ class MpvEventLogMessage(Structure):
('text', c_char_p)] ('text', c_char_p)]
def as_dict(self): def as_dict(self):
return { name: getattr(self, name).value for name, _t in _fields_ } return { name: getattr(self, name) for name, _t in self._fields_ }
class MpvEventEndFile(c_int): class MpvEventEndFile(c_int):
EOF_OR_INIT_FAILURE = 0 EOF_OR_INIT_FAILURE = 0
@ -246,7 +246,7 @@ def _ensure_encoding(possibly_bytes):
def _event_generator(handle): def _event_generator(handle):
while True: while True:
event = _mpv_wait_event(handle, 0).contents event = _mpv_wait_event(handle, -1).contents
if event.event_id.value == MpvEventID.NONE: if event.event_id.value == MpvEventID.NONE:
raise StopIteration() raise StopIteration()
yield event yield event
@ -258,20 +258,16 @@ def load_lua():
class MPV: class MPV:
""" See man mpv(1) for the details of the implemented commands. """ """ See man mpv(1) for the details of the implemented commands. """
def __init__(self, evloop=None, **kwargs): def __init__(self, **kwargs):
""" Create an MPV instance. You should pass in an asyncio event loop that will handle the mpv event queue via """ Create an MPV instance.
the evloop argument. If you do not pass in one, one will be created for you and run in a freshly spawned thread
(works for prototypes but is likely not what you want).
Any kwargs given will be passed to mpv as options. """ Any kwargs given will be passed to mpv as options. """
self.handle = _mpv_create() self.handle = _mpv_create()
self.event_callbacks = [] self.event_callbacks = []
self._event_fd = _mpv_get_wakeup_pipe(self.handle)
self._playback_cond = threading.Condition() self._playback_cond = threading.Condition()
def mpv_event_extractor(): def event_loop():
os.read(self._event_fd, 512)
for event in _event_generator(self.handle): for event in _event_generator(self.handle):
devent = event.as_dict() # copy data from ctypes devent = event.as_dict() # copy data from ctypes
if devent['event_id'] in (MpvEventID.SHUTDOWN, MpvEventID.END_FILE, MpvEventID.PAUSE): if devent['event_id'] in (MpvEventID.SHUTDOWN, MpvEventID.END_FILE, MpvEventID.PAUSE):
@ -279,19 +275,8 @@ class MPV:
self._playback_cond.notify_all() self._playback_cond.notify_all()
for callback in self.event_callbacks: for callback in self.event_callbacks:
callback.call() callback.call()
if evloop: self._event_thread = threading.Thread(target=event_loop, daemon=True)
evloop.add_reader(self._event_fd, mpv_event_extractor) self._event_thread.start()
else:
def loop_runner():
loop = asyncio.new_event_loop()
loop.add_reader(self._event_fd, mpv_event_extractor)
try:
loop.run_forever()
finally:
loop.close()
self._event_thread = threading.Thread(target=loop_runner, daemon=True)
self._event_thread.start()
_mpv_set_option_string(self.handle, b'audio-display', b'no') _mpv_set_option_string(self.handle, b'audio-display', b'no')
istr = lambda o: ('yes' if o else 'no') if type(o) is bool else str(o) istr = lambda o: ('yes' if o else 'no') if type(o) is bool else str(o)