Some refactoring and fixes for VLC

This commit is contained in:
Uriziel 2013-01-18 01:53:50 +01:00
parent e03cfe12c3
commit c354e2d3fd

View File

@ -3,7 +3,6 @@ import re
import threading
from syncplay.players.basePlayer import BasePlayer
from syncplay import constants
from syncplay.messages import getMessage
import os
import random
import socket
@ -13,8 +12,8 @@ class VlcPlayer(BasePlayer):
speedSupported = True
RE_ANSWER = re.compile(r"(?:^(?P<command>[a-zA-Z_]+)(?:\: )?(?P<argument>.*))")
VLC_MIN_PORT = 10000
VLC_MAX_PORT = 65000
SLAVE_ARGS = ['--extraintf=luaintf','--lua-intf=syncplay','-vvv']
VLC_MAX_PORT = 55000
SLAVE_ARGS = ['--extraintf=luaintf','--lua-intf=syncplay']
random.seed()
vlcport = random.randrange(VLC_MIN_PORT, VLC_MAX_PORT)
@ -26,32 +25,24 @@ class VlcPlayer(BasePlayer):
self._duration = None
self._filename = None
self._filepath = None
self._readyforchange = True
self._updatenotification = False
self._playerReady = True
try:
self._listener = self.__Listener(self, playerPath, filePath, args)
except ValueError:
self._client.ui.showMessage("Failed to load VLC")
self._client.stop(True)
return
self._listener.setDaemon(True)
self._listener.start()
self._durationAsk = threading.Event()
self._filenameAsk = threading.Event()
self._pathAsk = threading.Event()
self._positionAsk = threading.Event()
self._pausedAsk = threading.Event()
self._vlcready = threading.Event()
self._vlcready.wait()
self._preparePlayer()
def _fileUpdateClearEvents(self):
self._durationAsk.clear()
self._filenameAsk.clear()
@ -61,27 +52,22 @@ class VlcPlayer(BasePlayer):
self._durationAsk.wait()
self._filenameAsk.wait()
self._pathAsk.wait()
def _onFileUpdate(self):
self._fileUpdateClearEvents()
self._getFilename()
self._getFilepath()
self._getLength()
self._getFileInfo()
self._fileUpdateWaitEvents()
self._client.updateFile(self._filename, self._duration, self._filepath)
self.setPaused(self._client.getGlobalPaused())
self.setPosition(self._client.getGlobalPosition())
def _preparePlayer(self):
self.setPaused(self._client.getGlobalPaused())
self.setPosition(self._client.getGlobalPosition())
self._client.initPlayer(self)
self._readyforchange = True
self._playerReady = True
def askForStatus(self):
if (self._updatenotification):
self._updatenotification = False
self._onFileUpdate()
self._positionAsk.clear()
self._pausedAsk.clear()
self._listener.sendLine(".")
@ -104,83 +90,39 @@ class VlcPlayer(BasePlayer):
self._paused = value
self._listener.sendLine('set-playstate: {}'.format("paused" if value else "playing"))
def _getFilename(self):
self._listener.sendLine("get-filename")
def _getLength(self):
def _getFileInfo(self):
self._listener.sendLine("get-duration")
def _getFilepath(self):
self._listener.sendLine("get-filepath")
self._listener.sendLine("get-filename")
def _getPaused(self):
self._listener.sendLine(".")
def _getPosition(self):
self._listener.sendLine(".")
def lineReceived(self, line):
#print "received: {}".format(line)
if (line[:16] == "VLC media player"):
self._vlcready.set()
return
elif(line == "filepath-change-notification"):
if (self._readyforchange):
self._updatenotification = True
return
match = self.RE_ANSWER.match(line)
if not match:
return
name, value = match.group('command'), match.group('argument')
if (name == "filepath"):
if (value != "no-input"):
self._filepath = value
self._pathAsk.set()
match, name, value = self.RE_ANSWER.match(line), "", ""
if match:
name, value = match.group('command'), match.group('argument')
elif(name == "duration"):
if (value != "no-input"):
self._duration = float(value)
print line
self._durationAsk.set() #
if (name == "filepath" and value != "no-input"):
self._filepath = value
self._pathAsk.set()
if(line == "filepath-change-notification"):
t = threading.Thread(target=self._onFileUpdate)
t.setDaemon(True)
t.start()
elif(name == "duration" and (value != "no-input")):
self._duration = float(value)
self._durationAsk.set()
elif(name == "playstate"):
if(value == "no-input"):
self._paused = self._client.getGlobalPaused()
else:
self._paused = bool(value != 'playing')
self._paused = bool(value == 'paused') if(value != "no-input") else self._client.getGlobalPaused()
self._pausedAsk.set()
elif(name == "position"):
if (value == "no-input"):
self._position = self._client.getGlobalPosition()
else:
self._position = float(value)
self._position = float(value) if (value != "no-input") else self._client.getGlobalPosition()
self._positionAsk.set()
elif(name == "get-interface-version"):
print name
elif(name == "play-error"):
print name
elif(name == "set-playstate-error"):
print name
elif(name == "set-rate-error"):
print name
elif(name == "display-osd-error"):
print name
elif(name == "filename"):
self._filename = value
self._filenameAsk.set()
elif (line[:16] == "VLC media player"):
self._vlcready.set()
@staticmethod
def run(client, playerPath, filePath, args):
vlc = VlcPlayer(client, VlcPlayer.getExpandedPath(playerPath), filePath, args)
@ -218,6 +160,7 @@ class VlcPlayer(BasePlayer):
self._positionAsk.set()
self._vlcready.set()
self._pausedAsk.set()
self.__running = False
self._client.stop(False)
class __Listener(threading.Thread, asynchat.async_chat):
@ -230,25 +173,37 @@ class VlcPlayer(BasePlayer):
if(args):
call.extend(args)
self.__process = subprocess.Popen(call)
self.__process = subprocess.Popen(call, stderr=subprocess.PIPE)
threading.Thread.__init__(self, name="VLC Listener")
asynchat.async_chat.__init__(self)
self.set_terminator("\n")
self._ibuffer = []
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self._sendingData = threading.Lock()
self.connect(('localhost', self.__playerController.vlcport))
def initiate_send(self):
with self._sendingData:
asynchat.async_chat.initiate_send(self)
def run(self):
self.__running = True
asyncore.loop()
def collect_incoming_data(self, data):
self._ibuffer.append(data)
def close(self):
self.__running = False
self.__playerController.drop()
def found_terminator(self):
# print "received: {}".format("".join(self._ibuffer))
self.__playerController.lineReceived("".join(self._ibuffer))
self._ibuffer = []
def sendLine(self, line):
self.__playerController._vlcready.wait()
#print "send: {}".format(line)
self.push(line + "\n")
if(self.__running):
# print "send: {}".format(line)
self.push(line + "\n")