[svn r663] Added verification to GMYTH_FILE_READ_NEXT_PROG_CHAIN return values.
8 from threading import Thread
11 class StreamListener(Thread):
12 def __init__ (self, stream_data):
14 self.stream = stream_data
15 print "Thread Created"
19 print "Waiting connection"
20 self.stream.Socket.listen(1)
21 self.stream.Connection, self.stream.Addr = self.stream.Socket.accept ()
22 print "Connection requested"
23 self.stream.Sink.set_property ("fd", self.stream.Connection.fileno())
24 self.stream.Pipe.set_state(gst.STATE_PLAYING)
31 def __init__ (self, pipe, abin, vbin, sink):
32 self.stream_count += 1
33 self.Id = self.stream_count
38 self.Loop = gobject.MainLoop()
43 self.Connection = None
46 def __init__(self, config):
47 # set gstreamer basic options
51 self.connection = None
57 def setup(self, uri, mux, vcodec, vbitrate,
58 fps, acodec, abitrate, width, height, port, options):
61 pipe = gst.Pipeline ()
62 print "Opening Uri:" + uri
63 src = gst.element_make_from_uri (gst.URI_SRC, uri, "src")
64 #src = gst.element_factory_make ("gnomevfssrc", "src")
65 src.set_property ("location", uri)
67 print "Fail to create src element"
70 print ("Create source")
71 decode = gst.element_factory_make ("decodebin", "decode")
73 print "Fail to create decodebin"
76 print ("Create source")
77 mux = gst.element_factory_make ("avimux", "mux")
79 print "Fail to create mux"
82 sink = gst.element_factory_make ("fdsink", "sink")
84 print "Fail to create fdsink"
87 print ("Create source")
90 #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
92 vqueue = gst.element_factory_make ("queue", "vqueue")
93 colorspace = gst.element_factory_make ("ffmpegcolorspace", "")
94 vrate = gst.element_factory_make ("videorate", "vrate")
95 vencode = gst.element_factory_make ("ffenc_mpeg4", "vencode")
96 #vencode = gst.element_factory_make ("ffenc_msmpeg4v1", "vencode")
97 vqueue_src = gst.element_factory_make ("queue", "vqueue_src")
99 #if (int(vbitrate) > 0):
100 vencode.set_property ("bitrate", 200)
101 #vencode.set_property ("quant-type", 1)
102 vencode.set_property ("pass", 2)
103 vencode.set_property ("quantizer", 5)
104 #vencode.set_property ("me-method", 1)
107 if (None in [vbin, vqueue, vrate, vencode, vqueue_src]):
108 print "Fail to create video encode elements."
112 if ((int(width) > 0) and (int(height) > 0)):
113 print ("formating output to %d / %d" % (int(width), int(height)))
115 vscale = gst.element_factory_make ("ffvideoscale", "vscale")
118 if (not vqueue.link (vscale)):
119 print "Fail to link video elements"
122 vbin.add (colorspace)
124 if (not vscale.link (colorspace, \
125 gst.caps_from_string ("video/x-raw-yuv,width=(int)%d,height=(int)%d" % (int(width), int(height))))):
126 print "Fail to link video elements"
129 vbin.add (colorspace)
130 vqueue.link (colorspace)
132 vbin.add (vrate, vencode, vqueue_src)
133 if (not colorspace.link (vrate)):
134 print "Fail to colorspace with vrate"
138 if (not vrate.link (vencode, \
139 gst.caps_from_string ("video/x-raw-yuv,framerate=(fraction)10/1"))):
140 print "Fail to link vrate element"
143 if (not vencode.link (vqueue_src)):
144 print "Fail to link video encode with queue"
147 vbin.add_pad (gst.GhostPad ("sink", vqueue.get_pad ("sink")))
148 vbin.add_pad (gst.GhostPad ("src", vqueue_src.get_pad ("src")))
151 #audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay ! udpsink name=upd_audio host=224.0.0.1 port=5002
153 aqueue = gst.element_factory_make ("queue", "aqueue")
154 aconvert = gst.element_factory_make ("audioconvert", "aconvert")
155 arate = gst.element_factory_make ("audioresample", "arate")
156 #aencode = gst.element_factory_make ("ffenc_ac3", "aencode")
157 aencode = gst.element_factory_make ("queue", "aencode")
158 #aencode = gst.element_factory_make ("lame", "aencode")
159 #aencode = gst.element_factory_make ("ffenc_mp2", "aencode")
160 aqueue_src = gst.element_factory_make ("queue", "aqueue_src")
162 if (None in [abin, aqueue, arate, aencode, aqueue_src]):
163 print "Fail to create video encode elements."
166 abin.add (aqueue, aconvert, arate, aencode, aqueue_src)
167 if (not gst.element_link_many (aqueue, aconvert, arate, aencode, aqueue_src)):
168 print "Fail to link video elements"
171 abin.add_pad (gst.GhostPad ("sink", aqueue.get_pad ("sink")))
172 abin.add_pad (gst.GhostPad ("src", aqueue_src.get_pad ("src")))
175 pipe.add (src, decode, abin, vbin, mux, sink)
176 gst.element_link_many (src, decode)
177 gst.element_link_many (mux, sink)
179 #Linking decode with mux
180 mux_audio = mux.get_pad ("audio_0")
181 mux_video = mux.get_pad ("video_0")
183 audio_pad = abin.get_pad ("src")
184 video_pad = vbin.get_pad ("src")
186 if (audio_pad.link (mux_audio) != gst.PAD_LINK_OK):
187 print "Fail to link audio with mux"
190 if (video_pad.link (mux_video) != gst.PAD_LINK_OK):
191 print "Fail to link audio with mux"
194 stream_data = self.StreamData (pipe, abin, vbin, sink)
196 bus.add_signal_watch()
197 bus.connect ("message", self.__on_bus_message, stream_data)
199 decode.connect("new-decoded-pad", self.__on_decode_new_pad, stream_data)
200 decode.connect("unknown-type", self.__on_decode_unknown_type, stream_data)
202 print ("Create source")
203 pipe.set_state (gst.STATE_PAUSED)
205 stream_data.Loop.run ()
210 stream_data.Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
211 print "Bind on port %d" % port
212 stream_data.Socket.bind(('', int (port)))
213 self.streams.append (stream_data)
218 stream = self.streams[0]
219 self.current = self.StreamListener(stream)
220 self.current.start ()
227 stream = self.streams[0]
228 stream.Pipe.set_state(gst.STATE_NULL)
234 if (stream.Connection != None):
235 stream.Connection.close ()
242 def __on_bus_message (self, bus, message, stream_data):
245 if (t == gst.MESSAGE_STATE_CHANGED):
249 oldstate, newstate, pending = message.parse_state_changed ()
250 if ((oldstate == gst.STATE_READY) and \
251 (newstate == gst.STATE_PAUSED) and \
252 (pending == gst.STATE_VOID_PENDING) and \
253 (stream_data.Ready == False)):
254 state_changed_status, current_state, pending_state = stream_data.Pipe.get_state ()
255 if ((current_state == gst.STATE_PAUSED) and \
256 (pending_state == gst.STATE_VOID_PENDING)):
258 stream_data.Loop.quit ()
259 stream_data.Ready = True
260 elif (t == gst.MESSAGE_ERROR):
261 err, debug = message.parse_error()
262 print "Error: %s" % err, debug
263 stream_data.Loop.quit ()
264 stream_data.Ready = False
268 def __on_decode_unknown_type (self, decode, pad, caps, stream_data):
273 def __on_decode_new_pad (self, decode, pad, arg1, stream_data):
275 caps = pad.get_caps().to_string()
276 print "New pad " + caps
277 if (caps.rfind ("audio") != -1):
278 apad = stream_data.Abin.get_pad ("sink")
279 if (pad.link (apad) != gst.PAD_LINK_OK):
280 print "Error on link audio pad"
282 elif (caps.rfind ("video") != -1):
283 vpad = stream_data.Vbin.get_pad ("sink")
284 if (pad.link (vpad) != gst.PAD_LINK_OK):
285 print "Error on link video pad"