gmyth-stream/server/0.2/plugins/transcoders/gstreamer.py
author rosfran
Wed Apr 18 23:13:26 2007 +0100 (2007-04-18)
branchtrunk
changeset 568 f5ef83bbe8b5
child 570 00ecee7b9625
permissions -rw-r--r--
[svn r573] New function to get the information about the program info path directiory.
morphbr@565
     1
#vim:ts=4:sw=4:et
morphbr@565
     2
import pygst
morphbr@565
     3
pygst.require("0.10")
morphbr@565
     4
import gst
morphbr@565
     5
import gobject
morphbr@565
     6
import time
morphbr@565
     7
import lib.utils as utils
morphbr@565
     8
import lib.server as server
morphbr@565
     9
morphbr@565
    10
from threading import Thread
morphbr@565
    11
morphbr@565
    12
__all__ = ("TranscoderGstreamer",)
morphbr@565
    13
morphbr@565
    14
class TranscoderGstreamer(server.Transcoder):
morphbr@565
    15
    gstreamer_path = utils.which("gst-launch-0.10")
morphbr@565
    16
    name = "gstreamer"
morphbr@565
    17
    priority = -2
morphbr@565
    18
morphbr@565
    19
    # StreamListener()
morphbr@565
    20
morphbr@565
    21
    class StreamData:
morphbr@565
    22
        stream_count = 0
morphbr@565
    23
morphbr@565
    24
        def __init__(self, log, pipe, abin, vbin, sink):
morphbr@565
    25
            self.log = log
morphbr@565
    26
            self.stream_count += 1
morphbr@565
    27
            self.Id = self.stream_count
morphbr@565
    28
            self.Pipe = pipe
morphbr@565
    29
            self.Abin = abin
morphbr@565
    30
            self.Vbin = vbin
morphbr@565
    31
            self.Sink = sink
morphbr@565
    32
            self.Loop = gobject.MainLoop()
morphbr@565
    33
            self.ACaps = ""
morphbr@565
    34
            self.VCaps = ""
morphbr@565
    35
            self.Ready = False
morphbr@565
    36
            self.Connection = None
morphbr@565
    37
            self.Addr = None
morphbr@565
    38
        # __init__()
morphbr@565
    39
morphbr@565
    40
    # StreamData()
morphbr@565
    41
morphbr@565
    42
morphbr@565
    43
    def __init__(self, params):
morphbr@565
    44
        server.Transcoder.__init__(self, params)
morphbr@565
    45
        # set gstreamer basic options
morphbr@565
    46
        self.connection = None
morphbr@565
    47
        self.addr = None
morphbr@565
    48
        self.ready = False
morphbr@565
    49
        self.quit = False
morphbr@565
    50
morphbr@565
    51
        self.log.info("Params for Gstreamer: %s" % self.params)
morphbr@565
    52
    # __init__()
morphbr@565
    53
morphbr@565
    54
morphbr@565
    55
    def _create_start_elements(self, uri):
morphbr@565
    56
        self.log.info("Opening Uri:" + uri)
morphbr@565
    57
        src = gst.element_make_from_uri(gst.URI_SRC, uri, "src")
morphbr@565
    58
        decode = gst.element_factory_make("decodebin", "decode")
morphbr@565
    59
        mux = gst.element_factory_make("avimux", "mux")
morphbr@565
    60
        sink = gst.element_factory_make("fdsink", "sink")
morphbr@565
    61
morphbr@565
    62
        return [src, decode, mux, sink]
morphbr@565
    63
    # _create_start_elements()
morphbr@565
    64
morphbr@565
    65
morphbr@565
    66
    def _setup_video_encode(self, vbin, width, height):
morphbr@565
    67
        vqueue = gst.element_factory_make("queue", "vqueue")
morphbr@565
    68
        colorspace = gst.element_factory_make("ffmpegcolorspace", "")
morphbr@565
    69
        vrate = gst.element_factory_make("videorate", "vrate")
morphbr@565
    70
        vencode = gst.element_factory_make("ffenc_mpeg4", "vencode")
morphbr@565
    71
        vqueue_src = gst.element_factory_make("queue", "vqueue_src")
morphbr@565
    72
morphbr@565
    73
        vencode.set_property("bitrate", 200)
morphbr@565
    74
morphbr@565
    75
        if None in [vbin, vqueue, vrate, vencode, vqueue_src]:
morphbr@565
    76
            self.log.info("Fail to create video encode elements.")
morphbr@565
    77
            return False
morphbr@565
    78
morphbr@565
    79
        vbin.add(vqueue)
morphbr@565
    80
        if int(width) > 0 and int(height) > 0:
morphbr@565
    81
            self.log.info(("Formating output to %d / %d" %(int(width), int(height))))
morphbr@565
    82
morphbr@565
    83
            vscale = gst.element_factory_make("ffvideoscale", "vscale")
morphbr@565
    84
morphbr@565
    85
            vbin.add(vscale);
morphbr@565
    86
            if not vqueue.link(vscale):
morphbr@565
    87
                self.log.info("Fail to link video elements")
morphbr@565
    88
                return False
morphbr@565
    89
morphbr@565
    90
            vbin.add(colorspace)
morphbr@565
    91
morphbr@565
    92
            if not vscale.link(colorspace, \
morphbr@565
    93
                gst.caps_from_string("video/x-raw-yuv,width=(int)%d,height=(int)%d" %(\
morphbr@565
    94
                int(width), int(height)))):
morphbr@565
    95
                self.log.info("Fail to link video elements")
morphbr@565
    96
                return False
morphbr@565
    97
        else:
morphbr@565
    98
            vbin.add(colorspace)
morphbr@565
    99
            vqueue.link(colorspace)
morphbr@565
   100
morphbr@565
   101
        vbin.add(vrate, vencode, vqueue_src)
morphbr@565
   102
        if not colorspace.link(vrate):
morphbr@565
   103
            self.log.info("Fail to colorspace with vrate")
morphbr@565
   104
            return False
morphbr@565
   105
morphbr@565
   106
        if not vrate.link(vencode, \
morphbr@565
   107
            gst.caps_from_string("video/x-raw-yuv,framerate=(fraction)10/1")):
morphbr@565
   108
            self.log.info("Fail to link vrate element")
morphbr@565
   109
            return False
morphbr@565
   110
morphbr@565
   111
        if not vencode.link(vqueue_src):
morphbr@565
   112
            self.log.info("Fail to link video encode with queue")
morphbr@565
   113
            return False
morphbr@565
   114
morphbr@565
   115
        vbin.add_pad(gst.GhostPad("sink", vqueue.get_pad("sink")))
morphbr@565
   116
        vbin.add_pad(gst.GhostPad("src", vqueue_src.get_pad("src")))
morphbr@565
   117
morphbr@565
   118
        return True
morphbr@565
   119
    # _setup_video_encode()
morphbr@565
   120
morphbr@565
   121
morphbr@565
   122
    def _setup_audio_encode(self, abin):
morphbr@565
   123
        aqueue = gst.element_factory_make("queue", "aqueue")
morphbr@565
   124
        aconvert = gst.element_factory_make("audioconvert", "aconvert")
morphbr@565
   125
        arate = gst.element_factory_make("audioresample", "arate")
morphbr@565
   126
        aencode = gst.element_factory_make("queue", "aencode")
morphbr@565
   127
        aqueue_src = gst.element_factory_make("queue", "aqueue_src")
morphbr@565
   128
morphbr@565
   129
        if None in [abin, aqueue, arate, aencode, aqueue_src]:
morphbr@565
   130
            self.log.info("Fail to create video encode elements.")
morphbr@565
   131
            return False
morphbr@565
   132
morphbr@565
   133
        abin.add(aqueue, aconvert, arate, aencode, aqueue_src)
morphbr@565
   134
morphbr@565
   135
        if not gst.element_link_many(aqueue,  aconvert, arate, aencode, aqueue_src):
morphbr@565
   136
            self.log.info("Fail to link video elements")
morphbr@565
   137
            return False
morphbr@565
   138
morphbr@565
   139
        abin.add_pad(gst.GhostPad("sink", aqueue.get_pad("sink")))
morphbr@565
   140
        abin.add_pad(gst.GhostPad("src", aqueue_src.get_pad("src")))
morphbr@565
   141
morphbr@565
   142
        return True
morphbr@565
   143
    # _setup_audio_encode()
morphbr@565
   144
morphbr@565
   145
morphbr@565
   146
    def setup(self, uri, mux, vcodec, vbitrate,
morphbr@565
   147
              fps, acodec, abitrate, width, height, options):
morphbr@565
   148
morphbr@565
   149
        ## Pipelines
morphbr@565
   150
        pipe = gst.Pipeline()
morphbr@565
   151
        src, decode, mux, sink = self._create_start_elements(uri)
morphbr@565
   152
morphbr@565
   153
        if None in [src, decode, mux, sink]:
morphbr@565
   154
            self.log.info("Problems with while starting basic elements");
morphbr@565
   155
            return False
morphbr@565
   156
morphbr@565
   157
        #video encode
morphbr@565
   158
        #queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! videorate !
morphbr@565
   159
        #ffenc_h263p bitrate=256000 me-method=2 ! rtph263ppay ! udpsink  host=224.0.0.1
morphbr@565
   160
        #port=5000
morphbr@565
   161
morphbr@565
   162
        vbin = gst.Bin()
morphbr@565
   163
        if not self._setup_video_encode(vbin, width, height):
morphbr@565
   164
            return False
morphbr@565
   165
morphbr@565
   166
        #audio encode
morphbr@565
   167
        #audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !
morphbr@565
   168
        #udpsink name=upd_audio host=224.0.0.1 port=5002
morphbr@565
   169
morphbr@565
   170
        abin = gst.Bin()
morphbr@565
   171
        if not self._setup_audio_encode(abin):
morphbr@565
   172
            return False
morphbr@565
   173
morphbr@565
   174
        #Finish Pipeline
morphbr@565
   175
        pipe.add(src, decode, abin, vbin, mux, sink)
morphbr@565
   176
        gst.element_link_many(src, decode)
morphbr@565
   177
        gst.element_link_many(mux, sink)
morphbr@565
   178
morphbr@565
   179
        #Linking decode with mux
morphbr@565
   180
        mux_audio = mux.get_pad("audio_0")
morphbr@565
   181
        mux_video = mux.get_pad("video_0")
morphbr@565
   182
morphbr@565
   183
        audio_pad = abin.get_pad("src")
morphbr@565
   184
        video_pad = vbin.get_pad("src")
morphbr@565
   185
morphbr@565
   186
        if audio_pad.link(mux_audio) != gst.PAD_LINK_OK:
morphbr@565
   187
            self.log.info("Fail to link audio with mux")
morphbr@565
   188
            return False
morphbr@565
   189
morphbr@565
   190
        if video_pad.link(mux_video) != gst.PAD_LINK_OK:
morphbr@565
   191
            self.log.info("Fail to link audio with mux")
morphbr@565
   192
            return False
morphbr@565
   193
morphbr@565
   194
        self.stream_data = self.StreamData(self.log, pipe, abin, vbin, sink)
morphbr@565
   195
        bus = pipe.get_bus()
morphbr@565
   196
        bus.add_signal_watch()
morphbr@565
   197
        bus.connect("message", self.__on_bus_message, self.stream_data)
morphbr@565
   198
morphbr@565
   199
        decode.connect("new-decoded-pad", self.__on_decode_new_pad, self.stream_data)
morphbr@565
   200
        decode.connect("unknown-type", self.__on_decode_unknown_type, self.stream_data)
morphbr@565
   201
morphbr@565
   202
        self.log.info("Setting PIPELINE state to PAUSED")
morphbr@565
   203
        pipe.set_state(gst.STATE_PAUSED)
morphbr@565
   204
        self.log.info("Running Loop")
morphbr@565
   205
        self.stream_data.Loop.run()
morphbr@565
   206
    # setup()
morphbr@565
   207
morphbr@565
   208
    def __on_bus_message(self, bus, message, stream_data):
morphbr@565
   209
morphbr@565
   210
        t = message.type
morphbr@565
   211
morphbr@565
   212
        if t == gst.MESSAGE_STATE_CHANGED:
morphbr@565
   213
            oldstate = -1
morphbr@565
   214
            newstate = -1
morphbr@565
   215
            pending = -1
morphbr@565
   216
morphbr@565
   217
            oldstate, newstate, pending = message.parse_state_changed()
morphbr@565
   218
morphbr@565
   219
            if oldstate == gst.STATE_READY and \
morphbr@565
   220
               newstate == gst.STATE_PAUSED and \
morphbr@565
   221
               pending == gst.STATE_VOID_PENDING and \
morphbr@565
   222
               stream_data.Ready == False:
morphbr@565
   223
morphbr@565
   224
                state_changed_status, current_state, pending_state = stream_data.Pipe.get_state()
morphbr@565
   225
                if current_state == gst.STATE_PAUSED and \
morphbr@565
   226
                    pending_state == gst.STATE_VOID_PENDING:
morphbr@565
   227
                    self.log.info("Pipe paused")
morphbr@565
   228
                    stream_data.Loop.quit()
morphbr@565
   229
                    stream_data.Ready = True
morphbr@565
   230
morphbr@565
   231
        elif t == gst.MESSAGE_EOS:
morphbr@565
   232
            self.log.info("Pipe finished")
morphbr@565
   233
            stream_data.Loop.quit()
morphbr@565
   234
            self.quit = True
morphbr@565
   235
morphbr@565
   236
        elif t == gst.MESSAGE_ERROR:
morphbr@565
   237
            err, debug = message.parse_error()
morphbr@565
   238
            self.log.error("Error: %s %s" %(err, debug))
morphbr@565
   239
            stream_data.Loop.quit()
morphbr@565
   240
            stream_data.Ready = False
morphbr@565
   241
morphbr@565
   242
        return True
morphbr@565
   243
    # __on_bus_message()
morphbr@565
   244
morphbr@565
   245
    def __on_decode_unknown_type(self, decode, pad, caps, stream_data):
morphbr@565
   246
        self.log.info("Unknown Type")
morphbr@565
   247
        return None
morphbr@565
   248
    # __on_decode_unknown_type
morphbr@565
   249
morphbr@565
   250
    def __on_decode_new_pad(self, decode, pad, arg1, stream_data):
morphbr@565
   251
morphbr@565
   252
        caps = pad.get_caps().to_string()
morphbr@565
   253
        self.log.info("New pad " + caps)
morphbr@565
   254
        if caps.rfind("audio") != -1:
morphbr@565
   255
            apad = stream_data.Abin.get_pad("sink")
morphbr@565
   256
            if pad.link(apad) != gst.PAD_LINK_OK:
morphbr@565
   257
                self.log.info("Error on link audio pad")
morphbr@565
   258
                return None
morphbr@565
   259
        elif caps.rfind("video") != -1:
morphbr@565
   260
            vpad = stream_data.Vbin.get_pad("sink")
morphbr@565
   261
            if pad.link(vpad) != gst.PAD_LINK_OK:
morphbr@565
   262
                self.log.info("Error on link video pad")
morphbr@565
   263
                return None
morphbr@565
   264
        else:
morphbr@565
   265
            self.log.info("Invalid caps")
morphbr@565
   266
        self.log.info("Linked")
morphbr@565
   267
    # __on_decode_new_pad
morphbr@565
   268
morphbr@565
   269
morphbr@565
   270
    def start(self, outfd):
morphbr@565
   271
        params_first = self.params_first
morphbr@565
   272
morphbr@565
   273
        self.setup(params_first("uri", ""), params_first("mux", "avi"),
morphbr@565
   274
                   params_first("vcodec", "ffenc_h263p"), params_first("vbitrate", 256000),
morphbr@565
   275
                   params_first("fps", 25),  params_first("acodec", "faac"),
morphbr@565
   276
                   params_first("abitrate", 192000),  params_first("width", 320),
morphbr@565
   277
                   params_first("height", 240), params_first("options", ""))
morphbr@565
   278
morphbr@565
   279
        self.log.info("Play %s", outfd.fileno())
morphbr@565
   280
        self.stream_data.Sink.set_property("fd", outfd.fileno())
morphbr@565
   281
        self.log.info("Setting Pipeline state to PLAYING")
morphbr@565
   282
        self.stream_data.Pipe.set_state(gst.STATE_PLAYING)
morphbr@565
   283
morphbr@565
   284
        # keep playing until EOS
morphbr@565
   285
        self.log.info("QUIT: %s" % self.quit)
morphbr@565
   286
morphbr@565
   287
        i = 0
morphbr@565
   288
        loop = gobject.MainLoop()
morphbr@565
   289
        loop.run()
morphbr@565
   290
morphbr@565
   291
        self.log.info("quit loop")
morphbr@565
   292
morphbr@565
   293
        return True
morphbr@565
   294
    # start()
morphbr@565
   295
morphbr@565
   296
    def stop(self):
morphbr@565
   297
        self.log.info("Stop stream_data: %s" % self.stream_data)
morphbr@565
   298
morphbr@565
   299
        if self.stream_data:
morphbr@565
   300
            self.stream_data.Pipe.set_state(gst.STATE_NULL)
morphbr@565
   301
            self.quit = True
morphbr@565
   302
morphbr@565
   303
        del self.stream_data
morphbr@565
   304
        self.stream_data = None
morphbr@565
   305
    # stop