Package screenlets :: Package plugins :: Module Gstreamer

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.Gstreamer

     1  import pygtk, gtk, gobject
     2  import pygst
     3  pygst.require("0.10")
     4  import gst
     5
     6 -class gstreamer:
     7
     8 -        def __init__(self):
     9                  self.player = gst.element_factory_make("playbin", "player")
    10                  fakesink = gst.element_factory_make('fakesink', "my-fakesink")
    11                  self.player.set_property("video-sink", fakesink)
    12                  bus = self.player.get_bus()
    13                  bus.add_signal_watch()
    14                  bus.connect('message', self.on_message)
    15
    16                  self.player = gst.Pipeline("player")
    17                  source = gst.element_factory_make("filesrc", "file-source")
    18                  self.player.add(source)
    19                  demuxer = gst.element_factory_make("oggdemux", "demuxer")
    20                  self.player.add(demuxer)
    21                  demuxer.connect("pad-added", self.demuxer_callback)
    22                  self.audio_decoder = gst.element_factory_make("vorbisdec", "vorbis-decoder")
    23                  self.player.add(self.audio_decoder)
    24                  audioconv = gst.element_factory_make("audioconvert", "converter")
    25                  self.player.add(audioconv)
    26                  audiosink = gst.element_factory_make("autoaudiosink", "audio-output")
    27                  self.player.add(audiosink)
    28
    29                  gst.element_link_many(source, demuxer)
    30                  gst.element_link_many(self.audio_decoder, audioconv, audiosink)
    31
    32                  bus = self.player.get_bus()
    33                  bus.add_signal_watch()
    34                  bus.connect("message", self.on_message)
    35
    36
    37 -        def demuxer_callback(self, demuxer, pad):
    38                  adec_pad = self.audio_decoder.get_pad("sink")
    39                  pad.link(adec_pad)
    40
    41
    42 -        def start(self):
    43                  try:
    44                          self.player.set_property('uri', ta)
    45                          self.player.set_state(gst.STATE_PLAYING)
    46                  except Exception, ex:
    47                          print 'unable to connect'
    48
    49 -        def stop(self):
    50
    51                          self.player.set_state(gst.STATE_NULL)
    52
    53 -        def on_message(self, bus, message):
    54                  t = message.type
    55                  if t == gst.MESSAGE_EOS:
    56                          self.player.set_state(gst.STATE_NULL)
    57
    58                  elif t == gst.MESSAGE_ERROR:
    59                          err, debug = message.parse_error()
    60                          print "Error: %s" % err, debug
    61                          self.player.set_state(gst.STATE_NULL)
    62
    63 -        def rewind_callback(self, w):
    64                  pos_int = self.player.query_position(self.time_format, None)[0]
    65                  seek_ns = pos_int - (10 * 1000000000)
    66                  self.player.seek_simple(self.time_format, gst.SEEK_FLAG_FLUSH, seek_ns)
    67
    68 -        def forward_callback(self, w):
    69                  pos_int = self.player.query_position(self.time_format, None)[0]
    70                  seek_ns = pos_int + (10 * 1000000000)
    71                  self.player.seek_simple(self.time_format, gst.SEEK_FLAG_FLUSH, seek_ns)
    72
    73 -        def convert_ns(self, time_int):
    74                  time_int = time_int / 1000000000
    75                  time_str = ""
    76                  if time_int >= 3600:
    77                          _hours = time_int / 3600
    78                          time_int = time_int - (_hours * 3600)
    79                          time_str = str(_hours) + ":"
    80                  if time_int >= 600:
    81                          _mins = time_int / 60
    82                          time_int = time_int - (_mins * 60)
    83                          time_str = time_str + str(_mins) + ":"
    84                  elif time_int >= 60:
    85                          _mins = time_int / 60
    86                          time_int = time_int - (_mins * 60)
    87                          time_str = time_str + "0" + str(_mins) + ":"
    88                  else:
    89                          time_str = time_str + "00:"
    90                  if time_int > 9:
    91                          time_str = time_str + str(time_int)
    92                  else:
    93                          time_str = time_str + "0" + str(time_int)
    94
    95                  return time_str
    96