openlp/openlp/core/ui/media/vlcplayer.py

250 lines
9.9 KiB
Python
Raw Normal View History

2011-07-10 21:43:07 +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-07-10 21:43:07 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2012-12-29 20:56:56 +00:00
# Copyright (c) 2008-2013 Raoul Snyman #
# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
2012-11-11 21:16:14 +00:00
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
2012-10-21 13:16:22 +00:00
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
2011-07-10 21:43:07 +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 #
###############################################################################
from datetime import datetime
2012-04-28 13:10:54 +00:00
from distutils.version import LooseVersion
2011-07-10 21:43:07 +00:00
import logging
2012-04-28 11:13:16 +00:00
import os
import sys
2012-12-30 08:33:42 +00:00
from PyQt4 import QtGui
2012-04-28 11:13:16 +00:00
2012-12-20 12:09:12 +00:00
from openlp.core.lib import Receiver, translate, Settings
from openlp.core.ui.media import MediaState
2012-09-09 06:06:44 +00:00
from openlp.core.ui.media.mediaplayer import MediaPlayer
log = logging.getLogger(__name__)
2012-04-28 11:13:16 +00:00
VLC_AVAILABLE = False
2011-07-10 21:43:07 +00:00
try:
2012-09-09 06:06:44 +00:00
from openlp.core.ui.media.vendor import vlc
2012-04-28 11:13:16 +00:00
VLC_AVAILABLE = bool(vlc.get_default_instance())
except (ImportError, NameError, NotImplementedError):
2012-04-28 11:13:16 +00:00
pass
2011-12-06 21:19:37 +00:00
except OSError, e:
if sys.platform.startswith('win'):
2012-04-28 11:13:16 +00:00
if not isinstance(e, WindowsError) and e.winerror != 126:
2011-12-06 21:19:37 +00:00
raise
else:
raise
2011-07-10 21:43:07 +00:00
if VLC_AVAILABLE:
try:
2012-04-28 13:10:54 +00:00
version = vlc.libvlc_get_version()
except:
version = u'0.0.0'
2012-04-28 16:22:37 +00:00
if LooseVersion(version) < LooseVersion('1.1.0'):
2012-04-28 13:10:54 +00:00
VLC_AVAILABLE = False
log.debug(u'VLC could not be loaded: %s' % version)
2011-07-10 21:43:07 +00:00
AUDIO_EXT = [u'*.mp3', u'*.wav', u'*.wma', u'*.ogg']
2011-11-02 20:27:53 +00:00
VIDEO_EXT = [
u'*.3gp',
u'*.asf', u'*.wmv',
u'*.au',
u'*.avi',
u'*.flv',
u'*.mov',
u'*.mp4', u'*.m4v',
u'*.ogm', u'*.ogv',
u'*.mkv', u'*.mka',
u'*.ts', u'*.mpg',
u'*.mpg', u'*.mp2',
u'*.nsc',
u'*.nsv',
u'*.nut',
u'*.ra', u'*.ram', u'*.rm', u'*.rv' ,u'*.rmbv',
u'*.a52', u'*.dts', u'*.aac', u'*.flac' ,u'*.dv', u'*.vid',
u'*.tta', u'*.tac',
u'*.ty',
u'*.dts',
u'*.xa',
u'*.iso',
u'*.vob',
u'*.webm'
]
2011-11-02 20:27:53 +00:00
2011-11-11 16:45:25 +00:00
class VlcPlayer(MediaPlayer):
2011-07-10 21:43:07 +00:00
"""
A specialised version of the MediaPlayer class, which provides a VLC
2011-11-02 20:27:53 +00:00
display.
2011-07-10 21:43:07 +00:00
"""
2011-09-22 18:22:35 +00:00
2011-07-10 21:43:07 +00:00
def __init__(self, parent):
2011-11-11 16:45:25 +00:00
MediaPlayer.__init__(self, parent, u'vlc')
self.original_name = u'VLC'
self.display_name = u'&VLC'
2011-07-10 21:43:07 +00:00
self.parent = parent
2011-09-22 18:22:35 +00:00
self.canFolder = True
2011-11-02 20:27:53 +00:00
self.audio_extensions_list = AUDIO_EXT
self.video_extensions_list = VIDEO_EXT
2011-07-10 21:43:07 +00:00
def setup(self, display):
2012-01-19 19:13:19 +00:00
display.vlcWidget = QtGui.QFrame(display)
2013-01-09 19:56:16 +00:00
display.vlcWidget.setFrameStyle(QtGui.QFrame.NoFrame)
2011-07-10 21:43:07 +00:00
# creating a basic vlc instance
2011-12-02 22:00:28 +00:00
command_line_options = u'--no-video-title-show'
if not display.hasAudio:
command_line_options += u' --no-audio --no-video-title-show'
if Settings().value(u'advanced/hide mouse') and display.controller.isLive:
2011-12-02 22:00:28 +00:00
command_line_options += u' --mouse-hide-timeout=0'
display.vlcInstance = vlc.Instance(command_line_options)
2011-07-10 21:43:07 +00:00
display.vlcInstance.set_log_verbosity(2)
# creating an empty vlc media player
display.vlcMediaPlayer = display.vlcInstance.media_player_new()
2012-01-19 19:13:19 +00:00
display.vlcWidget.resize(display.size())
2011-07-10 21:43:07 +00:00
display.vlcWidget.raise_()
display.vlcWidget.hide()
# The media player has to be 'connected' to the QFrame.
2011-07-10 21:43:07 +00:00
# (otherwise a video would be displayed in it's own window)
# This is platform specific!
# You have to give the id of the QFrame (or similar object)
# to vlc, different platforms have different functions for this.
win_id = int(display.vlcWidget.winId())
2012-06-09 15:46:01 +00:00
if sys.platform == "win32":
display.vlcMediaPlayer.set_hwnd(win_id)
2012-06-09 15:46:01 +00:00
elif sys.platform == "darwin":
# We have to use 'set_nsobject' since Qt4 on OSX uses Cocoa
# framework and not the old Carbon.
display.vlcMediaPlayer.set_nsobject(win_id)
else:
2011-12-08 20:51:44 +00:00
# for Linux using the X Server
display.vlcMediaPlayer.set_xwindow(win_id)
2011-07-10 21:43:07 +00:00
self.hasOwnWidget = True
def check_available(self):
2012-04-28 11:13:16 +00:00
return VLC_AVAILABLE
2011-07-10 21:43:07 +00:00
def load(self, display):
log.debug(u'load vid in Vlc Controller')
controller = display.controller
volume = controller.media_info.volume
2012-05-19 15:41:05 +00:00
file_path = str(controller.media_info.file_info.absoluteFilePath())
2011-07-10 21:43:07 +00:00
path = os.path.normcase(file_path)
# create the media
display.vlcMedia = display.vlcInstance.media_new_path(path)
# put the media in the media player
display.vlcMediaPlayer.set_media(display.vlcMedia)
# parse the metadata of the file
display.vlcMedia.parse()
self.volume(display, volume)
# We need to set media_info.length during load because we want
# to avoid start and stop the video twice. Once for real playback
# and once to just get media length.
2012-12-05 21:10:20 +00:00
#
# Media plugin depends on knowing media length before playback.
2012-12-11 19:55:47 +00:00
controller.media_info.length = int(display.vlcMediaPlayer.get_media().get_duration() / 1000)
2011-07-10 21:43:07 +00:00
return True
2011-08-29 19:55:58 +00:00
def media_state_wait(self, display, mediaState):
2011-07-10 21:43:07 +00:00
"""
Wait for the video to change its state
2011-12-02 22:00:28 +00:00
Wait no longer than 60 seconds. (loading an iso file needs a long time)
2011-07-10 21:43:07 +00:00
"""
start = datetime.now()
while not mediaState == display.vlcMedia.get_state():
if display.vlcMedia.get_state() == vlc.State.Error:
return False
Receiver.send_message(u'openlp_process_events')
2011-12-02 22:00:28 +00:00
if (datetime.now() - start).seconds > 60:
2011-07-10 21:43:07 +00:00
return False
return True
def resize(self, display):
display.vlcWidget.resize(display.size())
2012-01-19 19:13:19 +00:00
2011-07-10 21:43:07 +00:00
def play(self, display):
2011-07-25 20:56:39 +00:00
controller = display.controller
start_time = 0
2012-12-11 19:55:47 +00:00
if self.state != MediaState.Paused and controller.media_info.start_time > 0:
2011-07-25 20:56:39 +00:00
start_time = controller.media_info.start_time
2011-07-10 21:43:07 +00:00
display.vlcMediaPlayer.play()
2012-06-09 15:46:01 +00:00
if not self.media_state_wait(display, vlc.State.Playing):
2011-08-29 19:55:58 +00:00
return False
2012-12-13 18:55:11 +00:00
self.volume(display, controller.media_info.volume)
2012-06-09 15:46:01 +00:00
if start_time > 0:
self.seek(display, controller.media_info.start_time * 1000)
2012-12-11 19:55:47 +00:00
controller.media_info.length = int(display.vlcMediaPlayer.get_media().get_duration() / 1000)
2012-06-09 15:46:01 +00:00
controller.seekSlider.setMaximum(controller.media_info.length * 1000)
self.state = MediaState.Playing
display.vlcWidget.raise_()
return True
2011-07-10 21:43:07 +00:00
def pause(self, display):
2011-07-18 21:25:10 +00:00
if display.vlcMedia.get_state() != vlc.State.Playing:
return
2011-07-10 21:43:07 +00:00
display.vlcMediaPlayer.pause()
2011-08-29 19:55:58 +00:00
if self.media_state_wait(display, vlc.State.Paused):
2011-07-10 21:43:07 +00:00
self.state = MediaState.Paused
def stop(self, display):
display.vlcMediaPlayer.stop()
self.state = MediaState.Stopped
def volume(self, display, vol):
if display.hasAudio:
display.vlcMediaPlayer.audio_set_volume(vol)
def seek(self, display, seekVal):
if display.vlcMediaPlayer.is_seekable():
2011-07-25 20:56:39 +00:00
display.vlcMediaPlayer.set_time(seekVal)
2011-07-10 21:43:07 +00:00
def reset(self, display):
display.vlcMediaPlayer.stop()
display.vlcWidget.setVisible(False)
self.state = MediaState.Off
def set_visible(self, display, status):
if self.hasOwnWidget:
display.vlcWidget.setVisible(status)
def update_ui(self, display):
# Stop video if playback is finished.
if display.vlcMedia.get_state() == vlc.State.Ended:
self.stop(display)
2011-07-10 21:43:07 +00:00
controller = display.controller
2011-07-25 20:56:39 +00:00
if controller.media_info.end_time > 0:
2012-12-11 19:55:47 +00:00
if display.vlcMediaPlayer.get_time() > controller.media_info.end_time * 1000:
2011-07-25 20:56:39 +00:00
self.stop(display)
self.set_visible(display, False)
2011-07-10 21:43:07 +00:00
if not controller.seekSlider.isSliderDown():
2013-01-19 12:50:52 +00:00
controller.seekSlider.blockSignals(True)
2012-12-11 19:55:47 +00:00
controller.seekSlider.setSliderPosition(display.vlcMediaPlayer.get_time())
2013-01-19 12:50:52 +00:00
controller.seekSlider.blockSignals(False)
2011-12-02 15:15:31 +00:00
2012-10-15 17:35:14 +00:00
def get_info(self):
return(translate('Media.player', 'VLC is an external player which '
'supports a number of different formats.') +
u'<br/> <strong>' + translate('Media.player', 'Audio') +
u'</strong><br/>' + unicode(AUDIO_EXT) + u'<br/><strong>' +
translate('Media.player', 'Video') + u'</strong><br/>' +
2012-12-20 12:09:12 +00:00
unicode(VIDEO_EXT) + u'<br/>')