Commits

Anonymous committed 170c07d

Added vlc backend module, which minimally works, and a dummy-backend
module which has the right interface and members, and right status
indicators, but does nothing. Last-ditch fallback module. Added single
module which deals with importing which movie module, does the system
checks for a file...

  • Participants
  • Parent commits 17a2fc0
  • Branches tylerthemovie

Comments (0)

Files changed (3)

lib/_dummybackend.py

+"""dummy Movie class if all else fails """
+class Movie:
+    def __init__(self, filename, surface=None):
+        self.filename=filename
+        self.surface = surface
+        self.process = None
+        self.loops=0
+        self.playing = False
+        self.paused  = False
+        self._backend = "DUMMY"
+        self.width=0
+        self.height=0
+    def play(self, loops=0):
+        self.playing= not self.playing
+        
+    def stop(self):
+        self.playing=not self.playing
+        self.paused =not self.paused
+        
+    def pause(self):
+        self.paused=not self.paused
+        
+    def resize(self, w, h):
+        self.width=w
+        self.height=h
+        
+    def __repr__(self):
+        return "(%s 0.0s)"%self.filename
+    

lib/_vlcbackend.py

+import subprocess, os, time, socket, select
+import threading
+
+import sys
+if('win' in sys.platform):
+    player="vlc.exe"
+else:
+    player= "vlc"
+remote= "-I rc"
+port = 10000
+hostname = socket.getaddrinfo('localhost', 10000)[0][4][0]
+extra = "--rc-host %s:%d" % (hostname, 10000)
+commands = [player, remote, extra]
+print commands
+
+class Movie(object):
+    """pygame._vlcbackend.Movie:
+        plays a video file via subprocess and the available vlc executable
+    """
+    def __init__(self, filename, surface=None):
+        self.filename=filename
+        self._surface = surface
+        self.process = None
+        self.loops=0
+        self.playing = False
+        self.paused  = False
+        self._backend = "VLC"
+    def getSurface(self):
+        #special stuff here
+        return self._surface
+    
+    def setSurface(self, value):
+        #special stuff here, like redirecting movie output here
+        self._surface = value
+    def delSurface(self):
+        del self._surface
+    surface=property(fget=getSurface, fset=setSurface, fdel=delSurface)
+    
+    def play(self, loops=0):
+        self.loops=loops
+        if loops<=-1: self.eternity=1
+        else:         self.eternity=0
+        self.loops -= 1
+        if not self.process:
+            self._play()
+        #otherwise stop playback, and start again with the new loops value.
+        else:
+            self.stop()
+            self._play()
+    def _play(self):
+        print ' '.join(commands)
+        self.process=subprocess.Popen(' '.join(commands), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+        self.playing = not self.playing
+        self.socket = socket.socket()
+        print (hostname, port)
+        self.socket.connect((hostname, port))
+        if(self.eternity):
+            #well, we need to watch vlc, see when it finishes playing, then play the video again, over and over
+            self.socket.send("add %s\n" % self.filename)
+            self.t=MovieWatcher(movie, 1 , eternity)
+            return
+        #otherwise, we add loops+1 copies of the video to the playlist
+        self.socket.send("add %s\n" % self.filename)
+        for i in range(1, self.loops+1):
+            self.socket.send("enqueue %s\n" % self.filename)
+            
+    def pause(self):
+        if self.process:
+            #send value to pause playback
+            self.paused = not self.paused
+            self.socket.send("pause\n")
+    
+    def stop(self):
+        if self.process:
+            #we kill the process...
+            self.paused = not self.paused
+            self.playing= not self.playing
+            self.socket.send("stop\n")
+            self.socket.send("logout\n")
+            self.process.terminate()
+            self.process=None
+            
+    def __repr__(self):
+        if self.process:
+            return "(%s: %d)" % (self.filename, self._get_time()) #add on timestamp
+        else:
+            return "(%s: 0.0s)" % self.filename
+    
+    def _get_time(self):
+        if self.process:
+            self.socket.send("get_time\n")
+            d=[]
+            read =[0]
+            while(len(read)>0):
+                read, write, exce = select.select([self.socket], [], [], 0.10)
+                if(len(read)>0):
+                    d.append(self.socket.recv(1))
+            d=''.join(d)
+            d=int(d)#transforms into an int
+            return d
+        
+            
+class MovieWatcher(threading.Thread):
+    def __init__(self, movie, time, eternity):
+        threading.Thread.__init__(self)
+        self.movie=movie
+        self.time = time
+        self.eternity = eternity
+    def run():
+        while(1):        
+            time.sleep(self.time)
+            read, write, exce = select.select([self.movie.socket], [], [], 0.1)
+            d=[]
+            while(len(read)>0):
+                d.append(self.movie.socket.recv(1))
+                read, write, exce = select.select([self.movie.socket], [], [], 0.1)
+            s = ''.join(d)
+            if("status change: ( stop state: 0 )" in s): 
+                if("nothing to play" in s):
+                    self.movie.socket.send("add %s\n" % self.movie.filename)
+                    
+"""Main newmovie module, imports first from _movie.so, then _vlcbackend if it finds the vlc executable."""
+
+try:
+    from pygame._movie import Movie
+except ImportError:
+    #try to transparently load the _vlcbackend.py Movie object.
+    import os, os.path, sys
+    path=os.path
+    if('win' in sys.platform):
+        if(os.path.exists(path.join(path.join(path.join('C:', 'Program Files'), 'VideoLan'), 'VLC'))):
+            try:
+                from pygame._vlcbackend import Movie, MovieWatcher
+            except ImportError:
+                #you're really hooped now...
+                print "Unable to find a working movie backend. Loading the dummy movie class..."
+                from pygame._dummybackend import Movie
+        else:
+            print "Unable to find a working movie backend. Loading the dummy movie class..."
+            from pygame._dummybackend import Movie
+    else:
+        #POSIX
+        if(os.path.exists(path.join(path.join(path.join(os.sep, 'usr'), 'bin'), 'vlc'))):
+            try:
+                from pygame._vlcbackend import Movie, MovieWatcher
+            except ImportError:
+                #oh man, I didn't mean for this to happen so badly...
+                print "Unable to find a working movie backend. Loading the dummy movie class..."
+                from pygame._dummybackend import Movie
+        else:
+            print "Unable to find a working movie backend. Loading the dummy movie class..."
+            from pygame._dummybackend import Movie
+