openlp/openlp/core/ui/media/systemplayer.py

333 lines
12 KiB
Python
Raw Normal View History

2011-04-18 19:50:12 +00:00
# -*- coding: utf-8 -*-
2012-12-11 19:55:47 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2011-04-18 19:50:12 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2019-02-14 15:09:09 +00:00
# Copyright (c) 2008-2019 OpenLP Developers #
2011-04-18 19:50:12 +00:00
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
2015-11-07 00:49:40 +00:00
The :mod:`~openlp.core.ui.media.systemplayer` contains the system (aka QtMultimedia) player component.
"""
import functools
2011-05-21 08:18:52 +00:00
import logging
2011-05-23 19:37:44 +00:00
import mimetypes
2011-05-21 08:18:52 +00:00
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtMultimedia, QtMultimediaWidgets
2011-04-18 19:50:12 +00:00
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import translate
2018-10-02 04:39:42 +00:00
from openlp.core.threading import ThreadWorker, is_thread_finished, run_thread
2011-11-11 16:45:25 +00:00
from openlp.core.ui.media import MediaState
2012-09-09 06:06:44 +00:00
from openlp.core.ui.media.mediaplayer import MediaPlayer
2018-10-02 04:39:42 +00:00
2011-04-18 19:50:12 +00:00
2011-05-21 08:18:52 +00:00
log = logging.getLogger(__name__)
2011-11-02 20:27:53 +00:00
ADDITIONAL_EXT = {
2013-09-05 22:41:24 +00:00
'audio/ac3': ['.ac3'],
'audio/flac': ['.flac'],
'audio/x-m4a': ['.m4a'],
'audio/midi': ['.mid', '.midi'],
'audio/x-mp3': ['.mp3'],
'audio/mpeg': ['.mp3', '.mp2', '.mpga', '.mpega', '.m4a'],
'audio/qcelp': ['.qcp'],
'audio/x-wma': ['.wma'],
'audio/x-ms-wma': ['.wma'],
'video/x-flv': ['.flv'],
'video/x-matroska': ['.mpv', '.mkv'],
'video/x-wmv': ['.wmv'],
'video/x-mpg': ['.mpg'],
'video/mpeg': ['.mp4', '.mts', '.mov'],
'video/x-ms-wmv': ['.wmv']
2012-10-04 17:28:49 +00:00
}
2011-11-02 20:27:53 +00:00
2015-11-07 00:49:40 +00:00
class SystemPlayer(MediaPlayer):
2011-11-02 20:27:53 +00:00
"""
2015-11-07 00:49:40 +00:00
A specialised version of the MediaPlayer class, which provides a QtMultimedia display.
2011-11-02 20:27:53 +00:00
"""
2015-11-07 00:49:40 +00:00
2011-11-02 20:27:53 +00:00
def __init__(self, parent):
"""
Constructor
"""
2015-11-07 00:49:40 +00:00
super(SystemPlayer, self).__init__(parent, 'system')
self.original_name = 'System'
self.display_name = '&System'
2011-11-02 20:27:53 +00:00
self.parent = parent
self.additional_extensions = ADDITIONAL_EXT
2015-11-07 00:49:40 +00:00
self.media_player = QtMultimedia.QMediaPlayer(None, QtMultimedia.QMediaPlayer.VideoSurface)
2011-05-23 19:37:44 +00:00
mimetypes.init()
2015-11-07 00:49:40 +00:00
media_service = self.media_player.service()
log.info(media_service.__class__.__name__)
# supportedMimeTypes doesn't return anything on Linux and Windows and
# the mimetypes it returns on Mac OS X may not be playable.
2015-12-07 21:40:45 +00:00
supported_codecs = self.media_player.supportedMimeTypes()
for mime_type in supported_codecs:
mime_type = str(mime_type)
log.info(mime_type)
if mime_type.startswith('audio/'):
self._add_to_list(self.audio_extensions_list, mime_type)
elif mime_type.startswith('video/'):
self._add_to_list(self.video_extensions_list, mime_type)
2011-05-10 20:39:17 +00:00
def _add_to_list(self, mime_type_list, mime_type):
"""
Add mimetypes to the provided list
"""
2011-05-23 19:37:44 +00:00
# Add all extensions which mimetypes provides us for supported types.
extensions = mimetypes.guess_all_extensions(mime_type)
2011-05-23 19:37:44 +00:00
for extension in extensions:
2013-08-31 18:17:38 +00:00
ext = '*%s' % extension
2014-03-17 19:05:55 +00:00
if ext not in mime_type_list:
mime_type_list.append(ext)
log.info('MediaPlugin: %s extensions: %s', mime_type, ' '.join(extensions))
2011-05-23 19:37:44 +00:00
def disconnect_slots(self, signal):
"""
Safely disconnect the slots from `signal`
"""
try:
signal.disconnect()
except TypeError:
# If disconnect() is called on a signal without slots, it throws a TypeError
pass
2011-06-05 21:10:49 +00:00
def setup(self, display):
"""
Set up the player widgets
2015-11-07 00:49:40 +00:00
:param display:
"""
display.video_widget = QtMultimediaWidgets.QVideoWidget(display)
display.video_widget.resize(display.size())
display.media_player = QtMultimedia.QMediaPlayer(display)
display.media_player.setVideoOutput(display.video_widget)
display.video_widget.raise_()
display.video_widget.hide()
2013-03-06 22:54:16 +00:00
self.has_own_widget = True
2011-05-17 19:50:00 +00:00
2011-06-08 13:18:05 +00:00
def check_available(self):
"""
Check if the player is available
"""
2015-11-07 00:49:40 +00:00
return True
2011-05-17 19:50:00 +00:00
2011-06-05 21:10:49 +00:00
def load(self, display):
"""
Load a video into the display
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
"""
log.debug('load vid in System Controller')
2011-06-06 19:12:14 +00:00
controller = display.controller
2011-06-05 21:10:49 +00:00
volume = controller.media_info.volume
path = controller.media_info.file_info.absoluteFilePath()
# Check if file is playable due to mimetype filters being nonexistent on Linux and Windows
if self.check_media(path):
display.media_player.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(path)))
self.volume(display, volume)
return True
else:
return False
2011-05-21 08:18:52 +00:00
2011-06-05 21:10:49 +00:00
def resize(self, display):
"""
Resize the display
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
"""
2015-11-07 00:49:40 +00:00
display.video_widget.resize(display.size())
2011-04-18 19:50:12 +00:00
def play(self, display):
"""
Play the current media item
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
"""
2015-11-07 00:49:40 +00:00
log.info('Play the current item')
2011-06-06 19:12:14 +00:00
controller = display.controller
2011-07-25 20:56:39 +00:00
start_time = 0
2016-02-26 16:00:04 +00:00
if display.controller.is_live:
if self.get_live_state() != QtMultimedia.QMediaPlayer.PausedState and controller.media_info.start_time > 0:
start_time = controller.media_info.start_time
else:
if self.get_preview_state() != QtMultimedia.QMediaPlayer.PausedState and \
2016-04-13 19:15:53 +00:00
controller.media_info.start_time > 0:
2016-02-26 16:00:04 +00:00
start_time = controller.media_info.start_time
2015-11-07 00:49:40 +00:00
display.media_player.play()
2012-06-09 15:46:01 +00:00
if start_time > 0:
self.seek(display, controller.media_info.start_time * 1000)
self.volume(display, controller.media_info.volume)
self.disconnect_slots(display.media_player.durationChanged)
display.media_player.durationChanged.connect(functools.partial(self.set_duration, controller))
2016-02-26 16:00:04 +00:00
self.set_state(MediaState.Playing, display)
2015-11-07 00:49:40 +00:00
display.video_widget.raise_()
2012-06-09 15:46:01 +00:00
return True
2011-04-18 19:50:12 +00:00
def pause(self, display):
"""
Pause the current media item
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
"""
2015-11-07 00:49:40 +00:00
display.media_player.pause()
2016-02-26 16:00:04 +00:00
if display.controller.is_live:
if self.get_live_state() == QtMultimedia.QMediaPlayer.PausedState:
self.set_state(MediaState.Paused, display)
else:
if self.get_preview_state() == QtMultimedia.QMediaPlayer.PausedState:
self.set_state(MediaState.Paused, display)
2011-04-18 19:50:12 +00:00
def stop(self, display):
"""
Stop the current media item
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
"""
2015-11-07 00:49:40 +00:00
display.media_player.stop()
2011-06-06 19:12:14 +00:00
self.set_visible(display, False)
2016-02-26 16:00:04 +00:00
self.set_state(MediaState.Stopped, display)
2011-05-10 20:39:17 +00:00
2016-02-14 16:14:48 +00:00
def volume(self, display, volume):
"""
Set the volume
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
:param volume: The volume to be set
"""
2013-03-06 22:54:16 +00:00
if display.has_audio:
2016-02-14 16:14:48 +00:00
display.media_player.setVolume(volume)
2011-04-18 19:50:12 +00:00
2013-03-23 07:28:24 +00:00
def seek(self, display, seek_value):
"""
Go to a particular point in the current media item
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
2016-03-07 21:55:13 +00:00
:param seek_value: The where to seek to
"""
2015-11-07 00:49:40 +00:00
display.media_player.setPosition(seek_value)
2011-04-18 19:50:12 +00:00
def reset(self, display):
"""
Reset the media player
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
"""
2015-11-07 00:49:40 +00:00
display.media_player.stop()
display.media_player.setMedia(QtMultimedia.QMediaContent())
2011-07-10 21:43:07 +00:00
self.set_visible(display, False)
2015-11-07 00:49:40 +00:00
display.video_widget.setVisible(False)
2016-03-07 21:55:13 +00:00
self.set_state(MediaState.Off, display)
2011-05-17 19:50:00 +00:00
def set_visible(self, display, status):
"""
Set the visibility of the widget
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
:param status: The visibility status to be set
"""
2013-03-06 22:54:16 +00:00
if self.has_own_widget:
2015-11-07 00:49:40 +00:00
display.video_widget.setVisible(status)
2011-05-10 20:39:17 +00:00
@staticmethod
def set_duration(controller, duration):
2016-02-14 16:14:48 +00:00
"""
:param controller: the controller displaying the media
:param duration: how long is the media
:return:
"""
2016-02-14 08:26:04 +00:00
controller.seek_slider.setMaximum(controller.media_info.length)
2011-06-05 21:10:49 +00:00
def update_ui(self, display):
"""
Update the UI
2016-02-14 16:14:48 +00:00
:param display: The display where the media is
"""
2015-11-07 00:49:40 +00:00
if display.media_player.state() == QtMultimedia.QMediaPlayer.PausedState and self.state != MediaState.Paused:
self.pause(display)
2011-06-06 19:12:14 +00:00
controller = display.controller
2011-06-05 21:10:49 +00:00
if controller.media_info.end_time > 0:
2016-02-14 08:26:04 +00:00
if display.media_player.position() > controller.media_info.end_time:
2011-05-24 20:03:57 +00:00
self.stop(display)
2011-06-05 21:10:49 +00:00
self.set_visible(display, False)
2013-03-23 07:45:08 +00:00
if not controller.seek_slider.isSliderDown():
2013-03-23 07:28:24 +00:00
controller.seek_slider.blockSignals(True)
2015-11-07 00:49:40 +00:00
controller.seek_slider.setSliderPosition(display.media_player.position())
2013-03-23 07:28:24 +00:00
controller.seek_slider.blockSignals(False)
2012-10-04 17:28:49 +00:00
def get_media_display_css(self):
"""
Add css style sheets to htmlbuilder
"""
2013-09-05 22:41:24 +00:00
return ''
2012-10-15 17:35:14 +00:00
def get_info(self):
"""
Return some info about this player
"""
2015-11-07 00:49:40 +00:00
return (translate('Media.player', 'This media player uses your operating system '
'to provide media capabilities.') +
'<br/> <strong>' + translate('Media.player', 'Audio') +
'</strong><br/>' + str(self.audio_extensions_list) +
'<br/><strong>' + translate('Media.player', 'Video') +
'</strong><br/>' + str(self.video_extensions_list) + '<br/>')
def check_media(self, path):
"""
Check if a file can be played
Uses a separate QMediaPlayer in a thread
:param path: Path to file to be checked
:return: True if file can be played otherwise False
"""
check_media_worker = CheckMediaWorker(path)
check_media_worker.setVolume(0)
run_thread(check_media_worker, 'check_media')
while not is_thread_finished('check_media'):
self.application.processEvents()
return check_media_worker.result
class CheckMediaWorker(QtMultimedia.QMediaPlayer, ThreadWorker):
"""
Class used to check if a media file is playable
"""
def __init__(self, path):
super(CheckMediaWorker, self).__init__(None, QtMultimedia.QMediaPlayer.VideoSurface)
self.path = path
def start(self):
"""
Start the thread worker
"""
self.result = None
self.error.connect(functools.partial(self.signals, 'error'))
self.mediaStatusChanged.connect(functools.partial(self.signals, 'media'))
self.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(self.path)))
self.play()
def signals(self, origin, status):
if origin == 'media' and status == self.BufferedMedia:
self.result = True
self.stop()
self.quit.emit()
elif origin == 'error' and status != self.NoError:
self.result = False
self.stop()
self.quit.emit()