gmyth-stream/server/plugins/media/gstreamer.py
branchtrunk
changeset 561 c4169fee6878
parent 534 cb1c3e2988b9
     1.1 --- a/gmyth-stream/server/plugins/media/gstreamer.py	Thu Apr 12 20:35:08 2007 +0100
     1.2 +++ b/gmyth-stream/server/plugins/media/gstreamer.py	Tue Apr 17 21:13:22 2007 +0100
     1.3 @@ -4,6 +4,7 @@
     1.4  import gst
     1.5  import gobject
     1.6  import socket
     1.7 +import time
     1.8  from threading import Thread
     1.9  
    1.10  class Media:
    1.11 @@ -50,6 +51,7 @@
    1.12          self.connection = None
    1.13          self.addr = None
    1.14          self.ready = False
    1.15 +		self.current = None
    1.16  
    1.17  
    1.18      def setup(self, uri, mux, vcodec, vbitrate,
    1.19 @@ -59,15 +61,19 @@
    1.20          pipe = gst.Pipeline ()
    1.21          print "Opening Uri:" + uri
    1.22          src = gst.element_make_from_uri (gst.URI_SRC, uri, "src")
    1.23 +        #src = gst.element_factory_make ("gnomevfssrc", "src")
    1.24 +        src.set_property ("location", uri)
    1.25          if (src is None):
    1.26              print "Fail to create src element"
    1.27              return None
    1.28  
    1.29 +        print ("Create source")
    1.30          decode = gst.element_factory_make ("decodebin", "decode")
    1.31          if (decode is None):
    1.32              print "Fail to create decodebin"
    1.33              return None
    1.34  
    1.35 +        print ("Create source")
    1.36          mux = gst.element_factory_make ("avimux", "mux")
    1.37          if (mux is None):
    1.38              print "Fail to create mux"
    1.39 @@ -78,76 +84,94 @@
    1.40              print "Fail to create fdsink"
    1.41              return None
    1.42  
    1.43 +        print ("Create source")
    1.44  
    1.45          #video encode
    1.46          #queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! videorate ! ffenc_h263p bitrate=256000 me-method=2 ! rtph263ppay ! udpsink  host=224.0.0.1 port=5000
    1.47          vbin = gst.Bin ()
    1.48          vqueue = gst.element_factory_make ("queue", "vqueue")
    1.49 -        vscale = gst.element_factory_make ("videoscale", "vscale")
    1.50          colorspace = gst.element_factory_make ("ffmpegcolorspace", "")
    1.51          vrate = gst.element_factory_make ("videorate", "vrate")
    1.52 -        vencode = gst.element_factory_make ("ffenc_h263p", "vencode")
    1.53 +        vencode = gst.element_factory_make ("ffenc_mpeg4", "vencode")
    1.54 +        #vencode = gst.element_factory_make ("ffenc_msmpeg4v1", "vencode")
    1.55 +        vqueue_src = gst.element_factory_make ("queue", "vqueue_src")
    1.56  
    1.57 +        #if (int(vbitrate) > 0):
    1.58 +        vencode.set_property ("bitrate", 200)
    1.59 +        #vencode.set_property ("quant-type", 1)
    1.60 +        vencode.set_property ("pass", 2)
    1.61 +        vencode.set_property ("quantizer", 5)
    1.62 +        #vencode.set_property ("me-method", 1)
    1.63  
    1.64 -        if (None in [vbin, vqueue, vscale, vrate, vencode]):
    1.65 +
    1.66 +        if (None in [vbin, vqueue, vrate, vencode, vqueue_src]):
    1.67              print "Fail to create video encode elements."
    1.68              return None
    1.69  
    1.70 -        '''
    1.71 -        vscale_pad = vscale.get_pad("sink")
    1.72 -        if (vscale_pad is None):
    1.73 -            print "Fail to get vscale sink pad."
    1.74 +        vbin.add (vqueue)
    1.75 +        if ((int(width) > 0) and (int(height) > 0)):
    1.76 +            print ("formating output to %d / %d" % (int(width), int(height)))
    1.77 +  
    1.78 +            vscale = gst.element_factory_make ("ffvideoscale", "vscale")
    1.79 +
    1.80 +            vbin.add (vscale);
    1.81 +            if (not vqueue.link (vscale)):
    1.82 +                print "Fail to link video elements"
    1.83 +                return None
    1.84 +
    1.85 +            vbin.add (colorspace)
    1.86 +
    1.87 +            if (not vscale.link (colorspace, \
    1.88 +                gst.caps_from_string ("video/x-raw-yuv,width=(int)%d,height=(int)%d" % (int(width), int(height))))):
    1.89 +                print "Fail to link video elements"
    1.90 +                return None
    1.91 +        else:                
    1.92 +            vbin.add (colorspace)
    1.93 +            vqueue.link (colorspace)
    1.94 +        
    1.95 +        vbin.add (vrate, vencode, vqueue_src)
    1.96 +        if (not colorspace.link (vrate)):
    1.97 +            print "Fail to colorspace with vrate"
    1.98              return None
    1.99  
   1.100 -        vscale_caps = gst.caps_from_string ("video/x-raw-yuv,width=%d,height=(int)%d" % (int(width), int(height)))
   1.101 -        if (vscale_caps is None):
   1.102 -            print "Fail to create video caps"
   1.103 +
   1.104 +        if (not vrate.link (vencode, \
   1.105 +            gst.caps_from_string ("video/x-raw-yuv,framerate=(fraction)10/1"))):
   1.106 +            print "Fail to link vrate element"
   1.107              return None
   1.108  
   1.109 -        if (not vscale_pad.set_caps (vscale_caps)):
   1.110 -            print "Fail to set video output caps"
   1.111 -            return None
   1.112 -        '''
   1.113 -
   1.114 -        vbin.add (vqueue, vscale, colorspace, vrate, vencode)
   1.115 -        if (not vqueue.link (vscale)):
   1.116 -            print "Fail to link video elements"
   1.117 -            return None
   1.118 -
   1.119 -        if (not vscale.link (colorspace, \
   1.120 -            gst.caps_from_string ("video/x-raw-yuv,width=%d,height=(int)%d" % (int(width), int(height))))):
   1.121 -            print "Fail to link video elements"
   1.122 -            return None
   1.123 -
   1.124 -        if (not gst.element_link_many (colorspace, vrate, vencode)):
   1.125 -            print "Fail to link video elements"
   1.126 +        if (not vencode.link (vqueue_src)):
   1.127 +            print "Fail to link video encode with queue"
   1.128              return None
   1.129  
   1.130          vbin.add_pad (gst.GhostPad ("sink", vqueue.get_pad ("sink")))
   1.131 -        vbin.add_pad (gst.GhostPad ("src", vencode.get_pad ("src")))
   1.132 +        vbin.add_pad (gst.GhostPad ("src", vqueue_src.get_pad ("src")))
   1.133  
   1.134          #audio encode
   1.135          #audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !  udpsink name=upd_audio host=224.0.0.1 port=5002
   1.136          abin = gst.Bin ()
   1.137 -        aqueue = gst.element_factory_make ("queue", "vqueue")
   1.138 +        aqueue = gst.element_factory_make ("queue", "aqueue")
   1.139          aconvert = gst.element_factory_make ("audioconvert", "aconvert")
   1.140 +        arate = gst.element_factory_make ("audioresample", "arate")
   1.141          #aencode = gst.element_factory_make ("ffenc_ac3", "aencode")
   1.142 -        aencode = gst.element_factory_make ("lame", "aencode")
   1.143 +        aencode = gst.element_factory_make ("queue", "aencode")
   1.144 +        #aencode = gst.element_factory_make ("lame", "aencode")
   1.145 +        #aencode = gst.element_factory_make ("ffenc_mp2", "aencode")
   1.146 +        aqueue_src = gst.element_factory_make ("queue", "aqueue_src")
   1.147  
   1.148 -        if (None in [abin, aqueue, aconvert, aencode]):
   1.149 +        if (None in [abin, aqueue, arate, aencode, aqueue_src]):
   1.150              print "Fail to create video encode elements."
   1.151              return None
   1.152  
   1.153 -        abin.add (aqueue, aconvert, aencode)
   1.154 -        if (not gst.element_link_many (aqueue, aconvert, aencode)):
   1.155 +        abin.add (aqueue, aconvert, arate, aencode, aqueue_src)
   1.156 +        if (not gst.element_link_many (aqueue,  aconvert, arate, aencode, aqueue_src)):
   1.157              print "Fail to link video elements"
   1.158              return None
   1.159  
   1.160          abin.add_pad (gst.GhostPad ("sink", aqueue.get_pad ("sink")))
   1.161 -        abin.add_pad (gst.GhostPad ("src", aencode.get_pad ("src")))
   1.162 +        abin.add_pad (gst.GhostPad ("src", aqueue_src.get_pad ("src")))
   1.163  
   1.164          #Finish Pipeline
   1.165 -
   1.166          pipe.add (src, decode, abin, vbin, mux, sink)
   1.167          gst.element_link_many (src, decode)
   1.168          gst.element_link_many (mux, sink)
   1.169 @@ -175,6 +199,7 @@
   1.170          decode.connect("new-decoded-pad", self.__on_decode_new_pad, stream_data)
   1.171          decode.connect("unknown-type", self.__on_decode_unknown_type, stream_data)
   1.172  
   1.173 +        print ("Create source")
   1.174          pipe.set_state (gst.STATE_PAUSED)
   1.175          print "Running Pipe"
   1.176          stream_data.Loop.run ()
   1.177 @@ -189,16 +214,28 @@
   1.178          return (True, "")
   1.179  
   1.180      def play(self):
   1.181 +        print "Play"
   1.182          stream = self.streams[0]
   1.183 -        current = self.StreamListener(stream)
   1.184 -        current.start ()
   1.185 -        print "Saindo"
   1.186 +        self.current = self.StreamListener(stream)
   1.187 +        self.current.start ()
   1.188 +        time.sleep (1)
   1.189          return (True, "")
   1.190  
   1.191      def stop(self):
   1.192 +		self.current.join ()
   1.193 +        self.current = None
   1.194          stream = self.streams[0]
   1.195          stream.Pipe.set_state(gst.STATE_NULL)
   1.196 -        stream.Connection.close ()
   1.197 +        del (stream.Pipe)
   1.198 +        stream.Pipe = None
   1.199 +        stream.Abin = None
   1.200 +        stream.Vbin = None
   1.201 +        stream.Sink = None
   1.202 +		if (stream.Connection != None):
   1.203 +	        stream.Connection.close ()
   1.204 +
   1.205 +        self.streams = []
   1.206 +        time.sleep (5)             
   1.207          return (True, "")
   1.208  
   1.209  
   1.210 @@ -249,4 +286,5 @@
   1.211                  return None
   1.212          else:
   1.213              print "Invalid caps"
   1.214 +        print "Linked"            
   1.215