1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/server/0.2/plugins/transcoders/gstreamer.py Wed Apr 18 23:13:26 2007 +0100
1.3 @@ -0,0 +1,305 @@
1.4 +#vim:ts=4:sw=4:et
1.5 +import pygst
1.6 +pygst.require("0.10")
1.7 +import gst
1.8 +import gobject
1.9 +import time
1.10 +import lib.utils as utils
1.11 +import lib.server as server
1.12 +
1.13 +from threading import Thread
1.14 +
1.15 +__all__ = ("TranscoderGstreamer",)
1.16 +
1.17 +class TranscoderGstreamer(server.Transcoder):
1.18 + gstreamer_path = utils.which("gst-launch-0.10")
1.19 + name = "gstreamer"
1.20 + priority = -2
1.21 +
1.22 + # StreamListener()
1.23 +
1.24 + class StreamData:
1.25 + stream_count = 0
1.26 +
1.27 + def __init__(self, log, pipe, abin, vbin, sink):
1.28 + self.log = log
1.29 + self.stream_count += 1
1.30 + self.Id = self.stream_count
1.31 + self.Pipe = pipe
1.32 + self.Abin = abin
1.33 + self.Vbin = vbin
1.34 + self.Sink = sink
1.35 + self.Loop = gobject.MainLoop()
1.36 + self.ACaps = ""
1.37 + self.VCaps = ""
1.38 + self.Ready = False
1.39 + self.Connection = None
1.40 + self.Addr = None
1.41 + # __init__()
1.42 +
1.43 + # StreamData()
1.44 +
1.45 +
1.46 + def __init__(self, params):
1.47 + server.Transcoder.__init__(self, params)
1.48 + # set gstreamer basic options
1.49 + self.connection = None
1.50 + self.addr = None
1.51 + self.ready = False
1.52 + self.quit = False
1.53 +
1.54 + self.log.info("Params for Gstreamer: %s" % self.params)
1.55 + # __init__()
1.56 +
1.57 +
1.58 + def _create_start_elements(self, uri):
1.59 + self.log.info("Opening Uri:" + uri)
1.60 + src = gst.element_make_from_uri(gst.URI_SRC, uri, "src")
1.61 + decode = gst.element_factory_make("decodebin", "decode")
1.62 + mux = gst.element_factory_make("avimux", "mux")
1.63 + sink = gst.element_factory_make("fdsink", "sink")
1.64 +
1.65 + return [src, decode, mux, sink]
1.66 + # _create_start_elements()
1.67 +
1.68 +
1.69 + def _setup_video_encode(self, vbin, width, height):
1.70 + vqueue = gst.element_factory_make("queue", "vqueue")
1.71 + colorspace = gst.element_factory_make("ffmpegcolorspace", "")
1.72 + vrate = gst.element_factory_make("videorate", "vrate")
1.73 + vencode = gst.element_factory_make("ffenc_mpeg4", "vencode")
1.74 + vqueue_src = gst.element_factory_make("queue", "vqueue_src")
1.75 +
1.76 + vencode.set_property("bitrate", 200)
1.77 +
1.78 + if None in [vbin, vqueue, vrate, vencode, vqueue_src]:
1.79 + self.log.info("Fail to create video encode elements.")
1.80 + return False
1.81 +
1.82 + vbin.add(vqueue)
1.83 + if int(width) > 0 and int(height) > 0:
1.84 + self.log.info(("Formating output to %d / %d" %(int(width), int(height))))
1.85 +
1.86 + vscale = gst.element_factory_make("ffvideoscale", "vscale")
1.87 +
1.88 + vbin.add(vscale);
1.89 + if not vqueue.link(vscale):
1.90 + self.log.info("Fail to link video elements")
1.91 + return False
1.92 +
1.93 + vbin.add(colorspace)
1.94 +
1.95 + if not vscale.link(colorspace, \
1.96 + gst.caps_from_string("video/x-raw-yuv,width=(int)%d,height=(int)%d" %(\
1.97 + int(width), int(height)))):
1.98 + self.log.info("Fail to link video elements")
1.99 + return False
1.100 + else:
1.101 + vbin.add(colorspace)
1.102 + vqueue.link(colorspace)
1.103 +
1.104 + vbin.add(vrate, vencode, vqueue_src)
1.105 + if not colorspace.link(vrate):
1.106 + self.log.info("Fail to colorspace with vrate")
1.107 + return False
1.108 +
1.109 + if not vrate.link(vencode, \
1.110 + gst.caps_from_string("video/x-raw-yuv,framerate=(fraction)10/1")):
1.111 + self.log.info("Fail to link vrate element")
1.112 + return False
1.113 +
1.114 + if not vencode.link(vqueue_src):
1.115 + self.log.info("Fail to link video encode with queue")
1.116 + return False
1.117 +
1.118 + vbin.add_pad(gst.GhostPad("sink", vqueue.get_pad("sink")))
1.119 + vbin.add_pad(gst.GhostPad("src", vqueue_src.get_pad("src")))
1.120 +
1.121 + return True
1.122 + # _setup_video_encode()
1.123 +
1.124 +
1.125 + def _setup_audio_encode(self, abin):
1.126 + aqueue = gst.element_factory_make("queue", "aqueue")
1.127 + aconvert = gst.element_factory_make("audioconvert", "aconvert")
1.128 + arate = gst.element_factory_make("audioresample", "arate")
1.129 + aencode = gst.element_factory_make("queue", "aencode")
1.130 + aqueue_src = gst.element_factory_make("queue", "aqueue_src")
1.131 +
1.132 + if None in [abin, aqueue, arate, aencode, aqueue_src]:
1.133 + self.log.info("Fail to create video encode elements.")
1.134 + return False
1.135 +
1.136 + abin.add(aqueue, aconvert, arate, aencode, aqueue_src)
1.137 +
1.138 + if not gst.element_link_many(aqueue, aconvert, arate, aencode, aqueue_src):
1.139 + self.log.info("Fail to link video elements")
1.140 + return False
1.141 +
1.142 + abin.add_pad(gst.GhostPad("sink", aqueue.get_pad("sink")))
1.143 + abin.add_pad(gst.GhostPad("src", aqueue_src.get_pad("src")))
1.144 +
1.145 + return True
1.146 + # _setup_audio_encode()
1.147 +
1.148 +
1.149 + def setup(self, uri, mux, vcodec, vbitrate,
1.150 + fps, acodec, abitrate, width, height, options):
1.151 +
1.152 + ## Pipelines
1.153 + pipe = gst.Pipeline()
1.154 + src, decode, mux, sink = self._create_start_elements(uri)
1.155 +
1.156 + if None in [src, decode, mux, sink]:
1.157 + self.log.info("Problems with while starting basic elements");
1.158 + return False
1.159 +
1.160 + #video encode
1.161 + #queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! videorate !
1.162 + #ffenc_h263p bitrate=256000 me-method=2 ! rtph263ppay ! udpsink host=224.0.0.1
1.163 + #port=5000
1.164 +
1.165 + vbin = gst.Bin()
1.166 + if not self._setup_video_encode(vbin, width, height):
1.167 + return False
1.168 +
1.169 + #audio encode
1.170 + #audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !
1.171 + #udpsink name=upd_audio host=224.0.0.1 port=5002
1.172 +
1.173 + abin = gst.Bin()
1.174 + if not self._setup_audio_encode(abin):
1.175 + return False
1.176 +
1.177 + #Finish Pipeline
1.178 + pipe.add(src, decode, abin, vbin, mux, sink)
1.179 + gst.element_link_many(src, decode)
1.180 + gst.element_link_many(mux, sink)
1.181 +
1.182 + #Linking decode with mux
1.183 + mux_audio = mux.get_pad("audio_0")
1.184 + mux_video = mux.get_pad("video_0")
1.185 +
1.186 + audio_pad = abin.get_pad("src")
1.187 + video_pad = vbin.get_pad("src")
1.188 +
1.189 + if audio_pad.link(mux_audio) != gst.PAD_LINK_OK:
1.190 + self.log.info("Fail to link audio with mux")
1.191 + return False
1.192 +
1.193 + if video_pad.link(mux_video) != gst.PAD_LINK_OK:
1.194 + self.log.info("Fail to link audio with mux")
1.195 + return False
1.196 +
1.197 + self.stream_data = self.StreamData(self.log, pipe, abin, vbin, sink)
1.198 + bus = pipe.get_bus()
1.199 + bus.add_signal_watch()
1.200 + bus.connect("message", self.__on_bus_message, self.stream_data)
1.201 +
1.202 + decode.connect("new-decoded-pad", self.__on_decode_new_pad, self.stream_data)
1.203 + decode.connect("unknown-type", self.__on_decode_unknown_type, self.stream_data)
1.204 +
1.205 + self.log.info("Setting PIPELINE state to PAUSED")
1.206 + pipe.set_state(gst.STATE_PAUSED)
1.207 + self.log.info("Running Loop")
1.208 + self.stream_data.Loop.run()
1.209 + # setup()
1.210 +
1.211 + def __on_bus_message(self, bus, message, stream_data):
1.212 +
1.213 + t = message.type
1.214 +
1.215 + if t == gst.MESSAGE_STATE_CHANGED:
1.216 + oldstate = -1
1.217 + newstate = -1
1.218 + pending = -1
1.219 +
1.220 + oldstate, newstate, pending = message.parse_state_changed()
1.221 +
1.222 + if oldstate == gst.STATE_READY and \
1.223 + newstate == gst.STATE_PAUSED and \
1.224 + pending == gst.STATE_VOID_PENDING and \
1.225 + stream_data.Ready == False:
1.226 +
1.227 + state_changed_status, current_state, pending_state = stream_data.Pipe.get_state()
1.228 + if current_state == gst.STATE_PAUSED and \
1.229 + pending_state == gst.STATE_VOID_PENDING:
1.230 + self.log.info("Pipe paused")
1.231 + stream_data.Loop.quit()
1.232 + stream_data.Ready = True
1.233 +
1.234 + elif t == gst.MESSAGE_EOS:
1.235 + self.log.info("Pipe finished")
1.236 + stream_data.Loop.quit()
1.237 + self.quit = True
1.238 +
1.239 + elif t == gst.MESSAGE_ERROR:
1.240 + err, debug = message.parse_error()
1.241 + self.log.error("Error: %s %s" %(err, debug))
1.242 + stream_data.Loop.quit()
1.243 + stream_data.Ready = False
1.244 +
1.245 + return True
1.246 + # __on_bus_message()
1.247 +
1.248 + def __on_decode_unknown_type(self, decode, pad, caps, stream_data):
1.249 + self.log.info("Unknown Type")
1.250 + return None
1.251 + # __on_decode_unknown_type
1.252 +
1.253 + def __on_decode_new_pad(self, decode, pad, arg1, stream_data):
1.254 +
1.255 + caps = pad.get_caps().to_string()
1.256 + self.log.info("New pad " + caps)
1.257 + if caps.rfind("audio") != -1:
1.258 + apad = stream_data.Abin.get_pad("sink")
1.259 + if pad.link(apad) != gst.PAD_LINK_OK:
1.260 + self.log.info("Error on link audio pad")
1.261 + return None
1.262 + elif caps.rfind("video") != -1:
1.263 + vpad = stream_data.Vbin.get_pad("sink")
1.264 + if pad.link(vpad) != gst.PAD_LINK_OK:
1.265 + self.log.info("Error on link video pad")
1.266 + return None
1.267 + else:
1.268 + self.log.info("Invalid caps")
1.269 + self.log.info("Linked")
1.270 + # __on_decode_new_pad
1.271 +
1.272 +
1.273 + def start(self, outfd):
1.274 + params_first = self.params_first
1.275 +
1.276 + self.setup(params_first("uri", ""), params_first("mux", "avi"),
1.277 + params_first("vcodec", "ffenc_h263p"), params_first("vbitrate", 256000),
1.278 + params_first("fps", 25), params_first("acodec", "faac"),
1.279 + params_first("abitrate", 192000), params_first("width", 320),
1.280 + params_first("height", 240), params_first("options", ""))
1.281 +
1.282 + self.log.info("Play %s", outfd.fileno())
1.283 + self.stream_data.Sink.set_property("fd", outfd.fileno())
1.284 + self.log.info("Setting Pipeline state to PLAYING")
1.285 + self.stream_data.Pipe.set_state(gst.STATE_PLAYING)
1.286 +
1.287 + # keep playing until EOS
1.288 + self.log.info("QUIT: %s" % self.quit)
1.289 +
1.290 + i = 0
1.291 + loop = gobject.MainLoop()
1.292 + loop.run()
1.293 +
1.294 + self.log.info("quit loop")
1.295 +
1.296 + return True
1.297 + # start()
1.298 +
1.299 + def stop(self):
1.300 + self.log.info("Stop stream_data: %s" % self.stream_data)
1.301 +
1.302 + if self.stream_data:
1.303 + self.stream_data.Pipe.set_state(gst.STATE_NULL)
1.304 + self.quit = True
1.305 +
1.306 + del self.stream_data
1.307 + self.stream_data = None
1.308 + # stop