forked from platypush/platypush
LINT fixes for mpv plugin
This commit is contained in:
parent
82ab7face2
commit
fef7aff245
1 changed files with 104 additions and 62 deletions
|
@ -3,8 +3,15 @@ import threading
|
||||||
|
|
||||||
from platypush.context import get_bus
|
from platypush.context import get_bus
|
||||||
from platypush.plugins.media import PlayerState, MediaPlugin
|
from platypush.plugins.media import PlayerState, MediaPlugin
|
||||||
from platypush.message.event.media import MediaPlayEvent, MediaPlayRequestEvent, \
|
from platypush.message.event.media import (
|
||||||
MediaPauseEvent, MediaStopEvent, NewPlayingMediaEvent, MediaSeekEvent, MediaResumeEvent
|
MediaPlayEvent,
|
||||||
|
MediaPlayRequestEvent,
|
||||||
|
MediaPauseEvent,
|
||||||
|
MediaStopEvent,
|
||||||
|
NewPlayingMediaEvent,
|
||||||
|
MediaSeekEvent,
|
||||||
|
MediaResumeEvent,
|
||||||
|
)
|
||||||
|
|
||||||
from platypush.plugins import action
|
from platypush.plugins import action
|
||||||
|
|
||||||
|
@ -66,29 +73,58 @@ class MediaMpvPlugin(MediaPlugin):
|
||||||
|
|
||||||
def _event_callback(self):
|
def _event_callback(self):
|
||||||
def callback(event):
|
def callback(event):
|
||||||
from mpv import MpvEventID as Event
|
from mpv import (
|
||||||
from mpv import MpvEventEndFile as EndFile
|
MpvEvent,
|
||||||
|
MpvEventID as Event,
|
||||||
|
MpvEventEndFile as EndFile,
|
||||||
|
)
|
||||||
|
|
||||||
self.logger.info('Received mpv event: {}'.format(event))
|
self.logger.info('Received mpv event: {}'.format(event))
|
||||||
|
|
||||||
|
if isinstance(event, MpvEvent):
|
||||||
|
event = event.as_dict()
|
||||||
|
|
||||||
evt = event.get('event_id')
|
evt = event.get('event_id')
|
||||||
if not evt:
|
if not evt:
|
||||||
return
|
return
|
||||||
|
|
||||||
if (evt == Event.FILE_LOADED or evt == Event.START_FILE) and self._get_current_resource():
|
if (
|
||||||
|
evt == Event.FILE_LOADED or evt == Event.START_FILE
|
||||||
|
) and self._get_current_resource():
|
||||||
self._playback_rebounce_event.set()
|
self._playback_rebounce_event.set()
|
||||||
self._post_event(NewPlayingMediaEvent, resource=self._get_current_resource(),
|
self._post_event(
|
||||||
title=self._player.filename)
|
NewPlayingMediaEvent,
|
||||||
|
resource=self._get_current_resource(),
|
||||||
|
title=self._player.filename,
|
||||||
|
)
|
||||||
elif evt == Event.PLAYBACK_RESTART:
|
elif evt == Event.PLAYBACK_RESTART:
|
||||||
self._playback_rebounce_event.set()
|
self._playback_rebounce_event.set()
|
||||||
self._post_event(MediaPlayEvent, resource=self._get_current_resource(), title=self._player.filename)
|
self._post_event(
|
||||||
|
MediaPlayEvent,
|
||||||
|
resource=self._get_current_resource(),
|
||||||
|
title=self._player.filename,
|
||||||
|
)
|
||||||
elif evt == Event.PAUSE:
|
elif evt == Event.PAUSE:
|
||||||
self._post_event(MediaPauseEvent, resource=self._get_current_resource(), title=self._player.filename)
|
self._post_event(
|
||||||
|
MediaPauseEvent,
|
||||||
|
resource=self._get_current_resource(),
|
||||||
|
title=self._player.filename,
|
||||||
|
)
|
||||||
elif evt == Event.UNPAUSE:
|
elif evt == Event.UNPAUSE:
|
||||||
self._post_event(MediaResumeEvent, resource=self._get_current_resource(), title=self._player.filename)
|
self._post_event(
|
||||||
elif evt == Event.SHUTDOWN or evt == Event.IDLE or (
|
MediaResumeEvent,
|
||||||
evt == Event.END_FILE and event.get('event', {}).get('reason') in
|
resource=self._get_current_resource(),
|
||||||
[EndFile.EOF, EndFile.ABORTED, EndFile.QUIT]):
|
title=self._player.filename,
|
||||||
|
)
|
||||||
|
elif (
|
||||||
|
evt == Event.SHUTDOWN
|
||||||
|
or evt == Event.IDLE
|
||||||
|
or (
|
||||||
|
evt == Event.END_FILE
|
||||||
|
and event.get('event', {}).get('reason')
|
||||||
|
in [EndFile.EOF, EndFile.ABORTED, EndFile.QUIT]
|
||||||
|
)
|
||||||
|
):
|
||||||
playback_rebounced = self._playback_rebounce_event.wait(timeout=0.5)
|
playback_rebounced = self._playback_rebounce_event.wait(timeout=0.5)
|
||||||
if playback_rebounced:
|
if playback_rebounced:
|
||||||
self._playback_rebounce_event.clear()
|
self._playback_rebounce_event.clear()
|
||||||
|
@ -211,8 +247,7 @@ class MediaMpvPlugin(MediaPlugin):
|
||||||
return None, 'No mpv instance is running'
|
return None, 'No mpv instance is running'
|
||||||
if not self._player.seekable:
|
if not self._player.seekable:
|
||||||
return None, 'The resource is not seekable'
|
return None, 'The resource is not seekable'
|
||||||
pos = min(self._player.time_pos + self._player.time_remaining,
|
pos = min(self._player.time_pos + self._player.time_remaining, max(0, position))
|
||||||
max(0, position))
|
|
||||||
self._player.time_pos = pos
|
self._player.time_pos = pos
|
||||||
return self.status()
|
return self.status()
|
||||||
|
|
||||||
|
@ -233,8 +268,10 @@ class MediaMpvPlugin(MediaPlugin):
|
||||||
return None, 'No mpv instance is running'
|
return None, 'No mpv instance is running'
|
||||||
if not self._player.seekable:
|
if not self._player.seekable:
|
||||||
return None, 'The resource is not seekable'
|
return None, 'The resource is not seekable'
|
||||||
pos = min(self._player.time_pos + self._player.time_remaining,
|
pos = min(
|
||||||
self._player.time_pos + offset)
|
self._player.time_pos + self._player.time_remaining,
|
||||||
|
self._player.time_pos + offset,
|
||||||
|
)
|
||||||
return self.seek(pos)
|
return self.seek(pos)
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
@ -261,11 +298,6 @@ class MediaMpvPlugin(MediaPlugin):
|
||||||
"""Add a subtitles file"""
|
"""Add a subtitles file"""
|
||||||
return self._player.sub_add(filename)
|
return self._player.sub_add(filename)
|
||||||
|
|
||||||
@action
|
|
||||||
def remove_subtitles(self, sub_id):
|
|
||||||
""" Remove a subtitles track by id """
|
|
||||||
return self._player.sub_remove(sub_id)
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def toggle_fullscreen(self):
|
def toggle_fullscreen(self):
|
||||||
"""Toggle the fullscreen mode"""
|
"""Toggle the fullscreen mode"""
|
||||||
|
@ -324,10 +356,12 @@ class MediaMpvPlugin(MediaPlugin):
|
||||||
return self.set_property(subfile=filename, sub_visibility=True)
|
return self.set_property(subfile=filename, sub_visibility=True)
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def remove_subtitles(self):
|
def remove_subtitles(self, sub_id=None):
|
||||||
"""Removes (hides) the subtitles"""
|
"""Removes (hides) the subtitles"""
|
||||||
if not self._player:
|
if not self._player:
|
||||||
return None, 'No mpv instance is running'
|
return None, 'No mpv instance is running'
|
||||||
|
if sub_id:
|
||||||
|
return self._player.sub_remove(sub_id)
|
||||||
self._player.sub_visibility = False
|
self._player.sub_visibility = False
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
@ -382,28 +416,35 @@ class MediaMpvPlugin(MediaPlugin):
|
||||||
return {'state': PlayerState.STOP.value}
|
return {'state': PlayerState.STOP.value}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'audio_channels': getattr(self._player, 'audio_channels'),
|
'audio_channels': getattr(self._player, 'audio_channels', None),
|
||||||
'audio_codec': getattr(self._player, 'audio_codec_name'),
|
'audio_codec': getattr(self._player, 'audio_codec_name', None),
|
||||||
'delay': getattr(self._player, 'delay'),
|
'delay': getattr(self._player, 'delay', None),
|
||||||
'duration': getattr(self._player, 'playback_time', 0) + getattr(self._player, 'playtime_remaining', 0)
|
'duration': getattr(self._player, 'playback_time', 0)
|
||||||
if getattr(self._player, 'playtime_remaining') else None,
|
+ getattr(self._player, 'playtime_remaining', 0)
|
||||||
'filename': getattr(self._player, 'filename'),
|
if getattr(self._player, 'playtime_remaining', None)
|
||||||
'file_size': getattr(self._player, 'file_size'),
|
else None,
|
||||||
'fullscreen': getattr(self._player, 'fs'),
|
'filename': getattr(self._player, 'filename', None),
|
||||||
'mute': getattr(self._player, 'mute'),
|
'file_size': getattr(self._player, 'file_size', None),
|
||||||
'name': getattr(self._player, 'name'),
|
'fullscreen': getattr(self._player, 'fs', None),
|
||||||
'pause': getattr(self._player, 'pause'),
|
'mute': getattr(self._player, 'mute', None),
|
||||||
'percent_pos': getattr(self._player, 'percent_pos'),
|
'name': getattr(self._player, 'name', None),
|
||||||
'position': getattr(self._player, 'playback_time'),
|
'pause': getattr(self._player, 'pause', None),
|
||||||
'seekable': getattr(self._player, 'seekable'),
|
'percent_pos': getattr(self._player, 'percent_pos', None),
|
||||||
'state': (PlayerState.PAUSE.value if self._player.pause else PlayerState.PLAY.value),
|
'position': getattr(self._player, 'playback_time', None),
|
||||||
'title': getattr(self._player, 'media_title') or getattr(self._player, 'filename'),
|
'seekable': getattr(self._player, 'seekable', None),
|
||||||
|
'state': (
|
||||||
|
PlayerState.PAUSE.value
|
||||||
|
if self._player.pause
|
||||||
|
else PlayerState.PLAY.value
|
||||||
|
),
|
||||||
|
'title': getattr(self._player, 'media_title', None)
|
||||||
|
or getattr(self._player, 'filename', None),
|
||||||
'url': self._get_current_resource(),
|
'url': self._get_current_resource(),
|
||||||
'video_codec': getattr(self._player, 'video_codec'),
|
'video_codec': getattr(self._player, 'video_codec', None),
|
||||||
'video_format': getattr(self._player, 'video_format'),
|
'video_format': getattr(self._player, 'video_format', None),
|
||||||
'volume': getattr(self._player, 'volume'),
|
'volume': getattr(self._player, 'volume', None),
|
||||||
'volume_max': getattr(self._player, 'volume_max'),
|
'volume_max': getattr(self._player, 'volume_max', None),
|
||||||
'width': getattr(self._player, 'width'),
|
'width': getattr(self._player, 'width', None),
|
||||||
}
|
}
|
||||||
|
|
||||||
def on_stop(self, callback):
|
def on_stop(self, callback):
|
||||||
|
@ -413,8 +454,9 @@ class MediaMpvPlugin(MediaPlugin):
|
||||||
if not self._player or not self._player.stream_path:
|
if not self._player or not self._player.stream_path:
|
||||||
return
|
return
|
||||||
|
|
||||||
return ('file://' if os.path.isfile(self._player.stream_path)
|
return (
|
||||||
else '') + self._player.stream_path
|
'file://' if os.path.isfile(self._player.stream_path) else ''
|
||||||
|
) + self._player.stream_path
|
||||||
|
|
||||||
def _get_resource(self, resource):
|
def _get_resource(self, resource):
|
||||||
if self._is_youtube_resource(resource):
|
if self._is_youtube_resource(resource):
|
||||||
|
|
Loading…
Reference in a new issue