All pastes #626035 Raw Edit

python - 2ways of object inherit

public text v1 · immutable
·published 2007-07-19 10:39 UTC
rendered paste body
#!/usr/bin/python
# change the names of the ogg-videos to display! these are my test-videos
# - test-dr
import gobject
import gst
import os
import imghdr
import pygtk
import gtk

class IdentifyFile():
  filename = ""
  exists   = False
  isFile   = False
  img      = ""
  
  def __init__(self, filename):
    print "init IdentifyFile for ", filename
    self.filename = filename
  def init(self, filename):
    self.filename = filename
  def check(self):
    #print "checking", self
    try:
      self.exists = os.path.exists(self.filename)
    except:
      self.exists = False
    if self.exists:
      self.lstat = os.lstat(self.filename)
      self.isFile = not os.path.isdir(self.filename)
      self.img  = imghdr.what(self.filename)
#stat structure, namely: st_mode (protection bits), st_ino (inode
#number), st_dev (device), st_nlink (number of hard links), st_uid (user ID of owner), st_gid
#(group ID of owner), st_size (size of file, in bytes), st_atime (time of most recent access),
#st_mtime (time of most recent content modification), st_ctime (platform dependent; time of most
#recent metadata change on Unix, or the time of creation on Windows)

gobject.threads_init()	#this should enable threading

class _GST_counter(): #a global counter, to count creation
  GST_counter = 0
  def increment(self):
    self.GST_counter += 1
    return self.GST_counter
GST_counter = _GST_counter()

class GST1(gobject.GObject):
  counter = 0
  signalsid = []
  mainloop = None
  bus = None
  pipeline = None
  GetOnlyPadsInfo = False

  def init(self):
    gobject.GObject.__init__(self)
    self.counter = GST_counter.increment()
    print "GST1 init", self.counter

  def __init__(self, *args):
    self.init(*args)

  def abort(self):
    if self.mainloop != None:
      self.mainloop.quit()

  def noMorePadsCb(self, data=None):
    print "NoMorePadsCb kicked..."

  def bus_eventCb(self, bus, message):
    t = message.type
    if t == gst.MESSAGE_EOS:
      print "need EOS action"
      self.abort()
      return False
    elif t == gst.MESSAGE_ERROR:
      err, debug = message.parse_error()
      print "Error: %s" % err, debug
      return False
    elif t == gst.MESSAGE_STATE_CHANGED:
      #print "STATE_CHANGED"
      prev, new, pending = message.parse_state_changed()
      #print prev, new, pending, "(", gst.STATE_PLAYING
      #if prev == gst.STATE_READY and new == gst.STATE_PAUSED and pending == gst.STATE_VOID_PENDING:
      #  if self.GetOnlyPadsInfo:
      #    self.scanPads()
      #    self.GetOnlyPadsInfo = False
      #if self.GetOnlyPadsInfo == True and new == gst.STATE_PLAYING:
      #  print "go to STATE_PLAYING .... scan now?"
      #  self.scanPads()
      #  self.GetOnlyPadsInfo = False
      #  self.pipeline_set_paused()
      if message.src == self.pipeline:
        prev, new, pending = message.parse_state_changed()
        if prev == gst.STATE_READY and new == gst.STATE_PAUSED and pending == gst.STATE_VOID_PENDING:
          print " pipeline prerolled ..."
          self.prerolled = True
          if self.GetOnlyPadsInfo == True:
            self.scanPads()
            self.GetOnlyPadsInfo = False
    return True

  def scanPads(self, GstObjectName="scanpads"):
    if GstObjectName == None:
      return False
    for pad in list(self.pipeline.get_by_name(GstObjectName).pads()):
      if pad.get_direction() == gst.PAD_SINK:
        continue
      caps = pad.get_caps()
      if not caps.is_fixed():
        print "caps not fixed"
        caps = pad.get_negotiated_caps()
      print "testing pad %s : %s" % (pad,caps)
      if caps:
        print caps.to_string()
        try:
          length, format = pad.query_duration(gst.FORMAT_TIME)
        except:
          print "............................ query duration failed"
        else:
          print length, format
  def scanPadsAndStop(self, GstObjectName="scanpads"):
    if GstObjectName == None:
      return False
    self.GetOnlyPadsInfo = True
    print ".. go for scan pads and stop if playing"
    self.pipeline_set_paused()
    while gtk.events_pending():
      print ".PadsAndStop . have pending events"
      gtk.main_iteration()
    self.scanPads()
    #self.pipeline_set_playing()
    #self.mainloop.run()

  def build_pipeline_from_description(self, str="videotestsrc ! ffmpegcolorspace name=scanpads ! timeoverlay ! xvimagesink"):
    self.pipeline = gst.Pipeline("GST1-%d" % self.counter)
    #str = "filesrc location=%s ! decodebin2 ! timeoverlay ! xvimagesink" % self.filename
    bin = gst.gst_parse_bin_from_description(str, 0)
    assert bin
    self.pipeline.add(bin)
    print "pipeline created from:", str
    self.mainloop = gobject.MainLoop()
    self.bus = self.pipeline.get_bus()
    self.signalsid.append( (self.bus, self.bus.connect("message", self.bus_eventCb) ) )
    self.signalsid.append( (bin, bin.connect("no-more-pads", self.noMorePadsCb) ) )
    self.bus.add_signal_watch()

  def pipeline_set_playing(self):
    self.pipeline.set_state(gst.STATE_PLAYING)
    #self.pipeline.set_state(gst.STATE_NULL)

  def pipeline_set_paused(self):
    self.pipeline.set_state(gst.STATE_PAUSED)

  def pipeline_set_ready(self):
    self.pipeline.set_state(gst.STATE_READY)
      
class IdentifyMediaFile(IdentifyFile, GST1):
  def __init__(self, filename):
    print "init IdentifyMediaFile for ", filename
    # this is using the object in add-on namespace
    #self.IdFile = IdentifyFile(filename)
    #self.IdFile.check()
    # this is using oo-way overwriting in own namespace
    IdentifyFile.init(self, filename)
  def mycheck(self):
    print ".. mycheck"
    self.check()
    self.GstTest = GST1()
    str = "filesrc location=%s ! oggdemux ! theoradec name=scanpads ! ffmpegcolorspace ! fakesink" % self.filename
    print str
    self.GstTest.build_pipeline_from_description(str)
    #self.GstTest.pipeline_set_ready()
    self.GstTest.pipeline_set_paused()
    
class MainWindow:
  video = None
  videorunning = False
  def delete_event(self, widget, event, data=None):
    return False
  def quit(self, widget, data=None):
    if self.video != None:
      self.video.abort()
    gtk.main_quit()
  def __init__(self):
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.connect("delete_event", self.delete_event)
    self.window.connect("destroy", self.quit, None)
    self.vbox = gtk.VBox(homogeneous=False, spacing=0)
    self.window.add(self.vbox)
    self.button = gtk.Button("Quit")
    self.button.connect("clicked", self.quit, None)
    self.vbox.add(self.button)
    self.button = gtk.Button("Start")
    self.button.connect("clicked", self.startCB, None)
    self.vbox.add(self.button)
    self.button = gtk.Button("Stop")
    self.button.connect("clicked", self.stopCB, None)
    self.vbox.add(self.button)
    self.button = gtk.Button("Scan pads")
    self.button.connect("clicked", self.scanpadsCB, None)
    self.vbox.add(self.button)
    self.button = gtk.Button("Scan I pads")
    self.button.connect("clicked", self.scanpadsICB, None)
    self.vbox.add(self.button)
    self.window.show_all()
  def scanpadsICB(self, widget, data):
    self.video.scanPadsAndStop()
  def scanpadsCB(self, widget, data):
    self.video.scanPads()
  def setvideo(self, video=None):
    self.video = video
  def startCB(self, widget, data):
    print "startCB"
    self.videorunning = True
    self.video.pipeline_set_playing()
  def stopCB(self, widget, data):
    print "stopCB"
    self.videorunning = False
    self.video.pipeline_set_paused()
  
  
gtk.gdk.threads_init()  #again some initializations

i = IdentifyMediaFile("onlyvideo.ogg")
print i.filename
i.check()
if i.exists:
  print "size:%Ld mode 0x%x" %( i.lstat.st_size, i.lstat.st_mode)
  print "is a file:", i.isFile, " is a picture:", i.img
  while gtk.events_pending():
    print ".1. have pending events"
    gtk.main_iteration()
  i.mycheck()
  while gtk.events_pending():
    print ".2. have pending events"
    gtk.main_iteration()
  i.GstTest.scanPadsAndStop()
  #i.GstTest.pipeline_set_playing()
else:
  print "check failed for file", i.filename
while gtk.events_pending():
  print ".3. have pending events"
  gtk.main_iteration()

print "now gui with gst.. .. and some things happen now, although calls were made earlier above"
v = GST1()
v.build_pipeline_from_description("filesrc location=metronom.ogg ! decodebin2 name=scanpads ! xvimagesink scanpads. ! audioconvert ! alsasink" )
#v.build_pipeline_from_description()
main = MainWindow()
main.setvideo(v)
gtk.main()
#i.pipeline_set_playing()