renatofilho@521: #vim:ts=4:sw=4:et
renatofilho@484: import pygst
renatofilho@484: pygst.require("0.10")
renatofilho@484: import gst
renatofilho@484: import gobject
renatofilho@484: import socket
renatofilho@521: from threading import Thread
renatofilho@484: 
renatofilho@484: class Media:
morphbr@534:     class StreamListener(Thread):
morphbr@534:         def __init__ (self, stream_data):
morphbr@534:             Thread.__init__(self)
morphbr@534:             self.stream = stream_data
morphbr@534:             print "Thread Created"
renatofilho@521: 
morphbr@534:         def run (self):
morphbr@534:             #Create socket
morphbr@534:             print "Waiting connection"
morphbr@534:             self.stream.Socket.listen(1)
morphbr@534:             self.stream.Connection, self.stream.Addr = self.stream.Socket.accept ()
morphbr@534:             print "Connection requested"
morphbr@534:             self.stream.Sink.set_property ("fd", self.stream.Connection.fileno())
morphbr@534:             self.stream.Pipe.set_state(gst.STATE_PLAYING)
morphbr@534:             print "PLAYING"
renatofilho@521: 
renatofilho@521: 
renatofilho@484:     class StreamData:
renatofilho@484:         stream_count = 0
renatofilho@484: 
morphbr@534:         def __init__ (self, pipe, abin, vbin, sink):
morphbr@534:             self.stream_count += 1
morphbr@534:             self.Id = self.stream_count
morphbr@534:             self.Pipe = pipe
morphbr@534:             self.Abin = abin
morphbr@534:             self.Vbin = vbin
morphbr@534:             self.Sink = sink
morphbr@534:             self.Loop = gobject.MainLoop()
morphbr@534:             self.ACaps = ""
morphbr@534:             self.VCaps = ""
morphbr@534:             self.Ready = False
morphbr@534:             self.Socket = None
morphbr@534:             self.Connection = None
morphbr@534:             self.Addr = None
renatofilho@484: 
renatofilho@484:     def __init__(self, config):
renatofilho@484:         # set gstreamer basic options
renatofilho@484:         self.config = config
renatofilho@484:         self.streams = []
renatofilho@521:         self.socket = None
renatofilho@521:         self.connection = None
renatofilho@521:         self.addr = None
morphbr@534:         self.ready = False
renatofilho@484: 
renatofilho@484: 
renatofilho@521:     def setup(self, uri, mux, vcodec, vbitrate,
renatofilho@484:               fps, acodec, abitrate, width, height, port, options):
renatofilho@484: 
renatofilho@484:         ## Pipelines
renatofilho@521:         pipe = gst.Pipeline ()
renatofilho@484:         print "Opening Uri:" + uri
renatofilho@484:         src = gst.element_make_from_uri (gst.URI_SRC, uri, "src")
renatofilho@484:         if (src is None):
renatofilho@521:             print "Fail to create src element"
renatofilho@484:             return None
renatofilho@521: 
renatofilho@484:         decode = gst.element_factory_make ("decodebin", "decode")
renatofilho@484:         if (decode is None):
renatofilho@521:             print "Fail to create decodebin"
renatofilho@484:             return None
renatofilho@484: 
renatofilho@521:         mux = gst.element_factory_make ("avimux", "mux")
renatofilho@484:         if (mux is None):
renatofilho@521:             print "Fail to create mux"
renatofilho@484:             return None
renatofilho@484: 
renatofilho@484:         sink = gst.element_factory_make ("fdsink", "sink")
renatofilho@484:         if (sink is None):
renatofilho@521:             print "Fail to create fdsink"
renatofilho@484:             return None
renatofilho@484: 
renatofilho@521: 
renatofilho@521:         #video encode
renatofilho@484:         #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
renatofilho@484:         vbin = gst.Bin ()
renatofilho@484:         vqueue = gst.element_factory_make ("queue", "vqueue")
renatofilho@484:         vscale = gst.element_factory_make ("videoscale", "vscale")
renatofilho@521:         colorspace = gst.element_factory_make ("ffmpegcolorspace", "")
renatofilho@484:         vrate = gst.element_factory_make ("videorate", "vrate")
renatofilho@484:         vencode = gst.element_factory_make ("ffenc_h263p", "vencode")
renatofilho@484: 
renatofilho@521: 
renatofilho@484:         if (None in [vbin, vqueue, vscale, vrate, vencode]):
renatofilho@484:             print "Fail to create video encode elements."
renatofilho@484:             return None
renatofilho@484: 
renatofilho@521:         '''
renatofilho@484:         vscale_pad = vscale.get_pad("sink")
renatofilho@484:         if (vscale_pad is None):
renatofilho@484:             print "Fail to get vscale sink pad."
renatofilho@484:             return None
renatofilho@484: 
renatofilho@521:         vscale_caps = gst.caps_from_string ("video/x-raw-yuv,width=%d,height=(int)%d" % (int(width), int(height)))
renatofilho@484:         if (vscale_caps is None):
renatofilho@484:             print "Fail to create video caps"
renatofilho@484:             return None
renatofilho@484: 
renatofilho@484:         if (not vscale_pad.set_caps (vscale_caps)):
renatofilho@484:             print "Fail to set video output caps"
renatofilho@484:             return None
renatofilho@521:         '''
renatofilho@521: 
renatofilho@521:         vbin.add (vqueue, vscale, colorspace, vrate, vencode)
renatofilho@521:         if (not vqueue.link (vscale)):
renatofilho@484:             print "Fail to link video elements"
renatofilho@484:             return None
renatofilho@521: 
renatofilho@521:         if (not vscale.link (colorspace, \
renatofilho@521:             gst.caps_from_string ("video/x-raw-yuv,width=%d,height=(int)%d" % (int(width), int(height))))):
renatofilho@521:             print "Fail to link video elements"
renatofilho@521:             return None
renatofilho@521: 
renatofilho@521:         if (not gst.element_link_many (colorspace, vrate, vencode)):
renatofilho@521:             print "Fail to link video elements"
renatofilho@521:             return None
renatofilho@521: 
renatofilho@484:         vbin.add_pad (gst.GhostPad ("sink", vqueue.get_pad ("sink")))
renatofilho@521:         vbin.add_pad (gst.GhostPad ("src", vencode.get_pad ("src")))
renatofilho@484: 
renatofilho@484:         #audio encode
renatofilho@484:         #audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !  udpsink name=upd_audio host=224.0.0.1 port=5002
renatofilho@484:         abin = gst.Bin ()
renatofilho@484:         aqueue = gst.element_factory_make ("queue", "vqueue")
renatofilho@484:         aconvert = gst.element_factory_make ("audioconvert", "aconvert")
renatofilho@521:         #aencode = gst.element_factory_make ("ffenc_ac3", "aencode")
renatofilho@521:         aencode = gst.element_factory_make ("lame", "aencode")
renatofilho@484: 
renatofilho@484:         if (None in [abin, aqueue, aconvert, aencode]):
renatofilho@484:             print "Fail to create video encode elements."
renatofilho@484:             return None
renatofilho@484: 
renatofilho@484:         abin.add (aqueue, aconvert, aencode)
renatofilho@484:         if (not gst.element_link_many (aqueue, aconvert, aencode)):
renatofilho@484:             print "Fail to link video elements"
renatofilho@484:             return None
renatofilho@521: 
renatofilho@484:         abin.add_pad (gst.GhostPad ("sink", aqueue.get_pad ("sink")))
renatofilho@484:         abin.add_pad (gst.GhostPad ("src", aencode.get_pad ("src")))
renatofilho@484: 
renatofilho@484:         #Finish Pipeline
renatofilho@484: 
renatofilho@521:         pipe.add (src, decode, abin, vbin, mux, sink)
renatofilho@521:         gst.element_link_many (src, decode)
renatofilho@484:         gst.element_link_many (mux, sink)
renatofilho@484: 
renatofilho@484:         #Linking decode with mux
renatofilho@484:         mux_audio = mux.get_pad ("audio_0")
renatofilho@484:         mux_video = mux.get_pad ("video_0")
renatofilho@484: 
renatofilho@521:         audio_pad = abin.get_pad ("src")
renatofilho@484:         video_pad = vbin.get_pad ("src")
renatofilho@484: 
renatofilho@484:         if (audio_pad.link (mux_audio) != gst.PAD_LINK_OK):
renatofilho@484:             print "Fail to link audio with mux"
renatofilho@484:             return None
renatofilho@521: 
renatofilho@484:         if (video_pad.link (mux_video) != gst.PAD_LINK_OK):
renatofilho@484:             print "Fail to link audio with mux"
renatofilho@484:             return None
renatofilho@484: 
renatofilho@521:         stream_data = self.StreamData (pipe, abin, vbin, sink)
renatofilho@521:         bus = pipe.get_bus()
renatofilho@521:         bus.add_signal_watch()
renatofilho@521:         bus.connect ("message", self.__on_bus_message, stream_data)
renatofilho@484: 
renatofilho@521:         decode.connect("new-decoded-pad", self.__on_decode_new_pad, stream_data)
renatofilho@521:         decode.connect("unknown-type", self.__on_decode_unknown_type, stream_data)
renatofilho@484: 
renatofilho@521:         pipe.set_state (gst.STATE_PAUSED)
renatofilho@484:         print "Running Pipe"
renatofilho@521:         stream_data.Loop.run ()
renatofilho@484:         print "End run"
renatofilho@484: 
renatofilho@521: 
morphbr@534:         #Create socket
morphbr@534:         stream_data.Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
morphbr@534:         print "Bind on port %d" % port
morphbr@534:         stream_data.Socket.bind(('', int (port)))
morphbr@534:         self.streams.append (stream_data)
morphbr@534:         return (True, "")
renatofilho@484: 
morphbr@534:     def play(self):
morphbr@534:         stream = self.streams[0]
morphbr@534:         current = self.StreamListener(stream)
morphbr@534:         current.start ()
morphbr@534:         print "Saindo"
morphbr@534:         return (True, "")
renatofilho@484: 
renatofilho@484:     def stop(self):
morphbr@534:         stream = self.streams[0]
morphbr@534:         stream.Pipe.set_state(gst.STATE_NULL)
morphbr@534:         stream.Connection.close ()
morphbr@534:         return (True, "")
renatofilho@484: 
renatofilho@484: 
renatofilho@484:     def __on_bus_message (self, bus, message, stream_data):
renatofilho@484: 
renatofilho@484:         t = message.type
renatofilho@484:         if (t == gst.MESSAGE_STATE_CHANGED):
renatofilho@484:             oldstate = -1
renatofilho@484:             newstate = -1
renatofilho@484:             pending = -1
renatofilho@484:             oldstate, newstate, pending = message.parse_state_changed ()
renatofilho@484:             if ((oldstate == gst.STATE_READY) and \
renatofilho@484:                 (newstate == gst.STATE_PAUSED) and \
renatofilho@484:                 (pending == gst.STATE_VOID_PENDING) and \
renatofilho@484:                 (stream_data.Ready == False)):
renatofilho@521:                 state_changed_status, current_state, pending_state = stream_data.Pipe.get_state ()
renatofilho@521:                 if ((current_state == gst.STATE_PAUSED) and \
renatofilho@484:                     (pending_state == gst.STATE_VOID_PENDING)):
renatofilho@484:                     print "Pipe paused"
renatofilho@484:                     stream_data.Loop.quit ()
renatofilho@484:                     stream_data.Ready = True
renatofilho@484:         elif (t == gst.MESSAGE_ERROR):
renatofilho@484:             err, debug = message.parse_error()
renatofilho@521:             print "Error: %s" % err, debug
renatofilho@484:             stream_data.Loop.quit ()
renatofilho@484:             stream_data.Ready = False
renatofilho@484: 
renatofilho@484:         return True
renatofilho@521: 
renatofilho@484:     def __on_decode_unknown_type (self, decode, pad, caps, stream_data):
renatofilho@484: 
renatofilho@484:         print "Unknown Type"
renatofilho@484:         return None
renatofilho@484: 
renatofilho@484:     def __on_decode_new_pad (self, decode, pad, arg1, stream_data):
renatofilho@521: 
renatofilho@484:         caps = pad.get_caps().to_string()
renatofilho@484:         print "New pad " + caps
renatofilho@521:         if (caps.rfind ("audio") != -1):
renatofilho@484:             apad = stream_data.Abin.get_pad ("sink")
renatofilho@484:             if (pad.link (apad) != gst.PAD_LINK_OK):
renatofilho@484:                 print "Error on link audio pad"
renatofilho@484:                 return None
renatofilho@484:         elif (caps.rfind ("video") != -1):
renatofilho@484:             vpad = stream_data.Vbin.get_pad ("sink")
renatofilho@484:             if (pad.link (vpad) != gst.PAD_LINK_OK):
renatofilho@484:                 print "Error on link video pad"
renatofilho@484:                 return None
renatofilho@484:         else:
renatofilho@484:             print "Invalid caps"
renatofilho@484: