Commits

Anonymous committed 62af619

Worked on fixing surface corruption issue, waiting on an answer from
libsdl mailing list. As well, worked on bringing other backends up to
interface compat with ffmpeg backend. Unfortunately vlc backend is...
bad. It doesn't seem to work...at all.

  • Participants
  • Parent commits a11f661
  • Branches tylerthemovie

Comments (0)

Files changed (3)

lib/_vlcbackend.py

 import subprocess, os, time, socket, select
 import threading
+import re
 
 import sys
 if('win' in sys.platform):
 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
+extra = "--rc-host %s:%d"
+
+class Communicator:
+    def __init__(self, player, remote, extra, port, hostname):
+        self.socket = socket.socket()
+        #print (hostname, port)
+        while 1:
+            try:
+                self.socket.connect((hostname, port))
+                break
+            except socket.error:
+                port+=1
+        self.commands =  ' '.join([player, remote, extra%(hostname, port)])
+        print self.commands
+        self.patterns = {
+            'size'  : re.compile("Resolution: \d{1,4}x\d{1,4}"), 
+            'width' : re.compile("Resolution: \d{1,4}(?=\d{1,4})"), 
+            'height': re.compile("Resolution: (?<=\dx)\d{1,4}|(?<=\d\dx)\d{1,4}|(?<=\d\d\dx)\d{1,4}|(?<=\d\d\d\dx)\d{1,4}"),
+                        }
+    def send(self, message):
+        self.socket.send(message)
+
+    def add(self, filename):
+        self.send("add %s\n" % filename)
+        
+    def enqueue(self, filename):
+        self.send("enqueue %s\n" % filename)
+        
+    def pause(self):
+        self.send("pause\n")
+        
+    def stop(self):
+        self.send("stop\n")
+        
+    def logout(self):
+        self.send("logout\n")
+
+    def info(self):
+        self.send("info\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)
+        return d
+        
+    def _get_time(self):
+        self.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
+    
+    def _get_height(self):
+        d=self.info()
+        p = self.patterns['height']
+        m =p.search(d)
+        if not m:
+            return 0
+        return int(m.group())
+
+    def _get_width(self):
+        d=self.info()
+        p= self.patterns['width']
+        m=p.search(d)
+        if not m:
+            return 0
+        return int(m.group())
 
 class Movie(object):
     """pygame._vlcbackend.Movie:
         self.playing = False
         self.paused  = False
         self._backend = "VLC"
+        self.comm = Communicator(player, remote, extra, port, hostname)
+        self.width = 0
+        self.height =0
     def getSurface(self):
         #special stuff here
         return self._surface
         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
             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)
+        comm = self.comm.commands
+        self.process=subprocess.Popen(comm, 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))
+        self.width = self.comm._get_width()
+        self.height = self.comm._get_height()
         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)
+            self.comm.add(self.filename)
+            self.t=MovieWatcher(self.comm, 0.1 , self.eternity, self.filename)
             return
         #otherwise, we add loops+1 copies of the video to the playlist
-        self.socket.send("add %s\n" % self.filename)
+        self.comm.add(self.filename)
         for i in range(1, self.loops+1):
-            self.socket.send("enqueue %s\n" % self.filename)
-            
+            self.comm.enqueue(self.filename)
+        
+        
     def pause(self):
         if self.process:
             #send value to pause playback
             self.paused = not self.paused
-            self.socket.send("pause\n")
+            self.comm.pause()
     
     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.comm.stop()
+            self.commd.logout()
             self.process.terminate()
             self.process=None
             
     def __repr__(self):
         if self.process:
-            return "(%s: %d)" % (self.filename, self._get_time()) #add on timestamp
+            return "(%s: %ds)" % (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
+            return self.comm._get_time()
         
-            
+    
+
 class MovieWatcher(threading.Thread):
-    def __init__(self, movie, time, eternity):
+    def __init__(self, comm, time, eternity, filename):
         threading.Thread.__init__(self)
-        self.movie=movie
+        self.comm=comm
         self.time = time
         self.eternity = eternity
+        self.filename = filename
     def run():
         while(1):        
             time.sleep(self.time)
-            read, write, exce = select.select([self.movie.socket], [], [], 0.1)
+            read, write, exce = select.select([self.comm.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)
+                d.append(self.comm.socket.recv(1))
+                read, write, exce = select.select([self.comm.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)
+                    self.comm.add(self.filename)
                     
 # to be on time for rendering that surface.
 # Without a surface argument, the ffmpeg-wrapper uses the sdl_overlay library. 
 #screen=pygame.display.set_mode((640, 368))
-m = movie.Movie(filename)
-print m.paused  #always False, unless .pause has been called
-print m.playing #False until play has been called. Will return to false when
-                # .stop() has been called.
-                
-print m.width   #default size values of the video file
-print m.height  # They can be modified on the fly, as will be demonstrated.
-
-print m         #calls __repr__, which will show the filename, and the current 
-                # timestamp. 
-#print "repeated looping plays.."#
-#m.play(10)
-#time.sleep(10*30)
-
-
-print "Playing infinitely"
-
-m.play(-1)       #We're going to use infinite play, so we can demonstrate all 
-                # the features.
-time.sleep(2)  #sleep for ten seconds to let one see the video play, and hear 
-                # the audio
-print m.paused
-print m.playing
-print m
+##m = movie.Movie(filename)
+##print m.paused  #always False, unless .pause has been called
+##print m.playing #False until play has been called. Will return to false when
+##                # .stop() has been called.
+##                
+##print m.width   #default size values of the video file
+##print m.height  # They can be modified on the fly, as will be demonstrated.
+##
+##print m         #calls __repr__, which will show the filename, and the current 
+##                # timestamp. 
+###print "repeated looping plays.."#
+###m.play(10)
+###time.sleep(10*30)
+##
+##
+##print "Playing infinitely"
+##
+##m.play(-1)       #We're going to use infinite play, so we can demonstrate all 
+##                # the features.
+##time.sleep(2)  #sleep for ten seconds to let one see the video play, and hear 
+##                # the audio
+##print m.paused
+##print m.playing
+##print m
 
 #Now we're going to play with the size of the window, affecting the video on 
 # the fly. resize(width, height) is the main function, changes them both at
 ##print "done restart play..."
 ##time.sleep(10)
 print "Surface time..."
-screen = pygame.display.set_mode((m.width, m.height))
+screen = pygame.display.set_mode((640, 348))
 #This will move the movie player from overlay mode to blitting to the surface 
 # we've given it. This means it is our responsibility to update the display on 
 # time.
 
+m=movie.Movie(filename, screen)
 counter = 0
-actions = {1: lambda x: x.paused, 6: lambda x:x.pause(), 11: lambda x:x.pause(), 15: lambda x: x.resize(x.width*2, x.height*2), 2000:lambda x: x.stop(), 3000: lambda x: x.play(-1)}
+actions = {1: lambda x: x.paused, 6: lambda x:x.pause(), 11: lambda x:x.pause(), 15: lambda x: x.resize(int(x.width*0.9), int(x.height*0.9)), 2000:lambda x: x.stop(), 3000: lambda x: x.play(-1)}
+m.play(-1)
 prev_time = time.time()
-m.surface = screen
+#m.surface = screen
 print "About to do surface gymnastics..."
 while(1):
     new_time=time.time()
 	        {
 	            th=screen->h;
 	        }
-	        GRABGIL
-	        PySys_WriteStdout("screen->BitsPerPixel: %i\nscreen->RMask: %i\nscreen->Gmask: %i\nscreen->Bmask: %i\nscreen->Amask: %i\n",
-	         screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
-	        RELEASEGIL
-	        vp->dest_surface = SDL_CreateRGBSurface(screen->flags,
-	                                                tw,
-	                                                th,
-	                                                screen->format->BitsPerPixel,
-	                                                screen->format->Rmask,
-	                                                screen->format->Gmask,
-	                                                screen->format->Bmask,
-	                                                screen->format->Amask);
+	        //GRABGIL
+	        //PySys_WriteStdout("screen->BitsPerPixel: %i\nscreen->RMask: %i\nscreen->Gmask: %i\nscreen->Bmask: %i\nscreen->Amask: %i\n",
+	        // screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
+	        //RELEASEGIL
+	        /*if(movie->resize_w||movie->resize_h)
+			{*/
+		        vp->dest_surface = SDL_CreateRGBSurface(screen->flags,
+		                                                tw,
+		                                                th,
+		                                                screen->format->BitsPerPixel,
+		                                                screen->format->Rmask,
+		                                                screen->format->Gmask,
+		                                                screen->format->Bmask,
+		                                                screen->format->Amask);
+		                                                
+			/*}
+			else
+	        {
+	        	SDL_PixelFormat *format = screen->format;
+	        	vp->dest_surface = SDL_ConvertSurface(screen, format, screen->flags);
+	        }*/
+	        
 	        if (!vp->dest_surface)
 	        {
 	            GRABGIL