gmyth-stream/server/0.1/plugins/media/gstreamer.py
author melunko
Wed May 16 16:11:06 2007 +0100 (2007-05-16)
branchtrunk
changeset 667 aa8c04b7c362
permissions -rw-r--r--
[svn r673] file transfer livetv read tries increaded to 20
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 socket
morphbr@565
     7
import time
morphbr@565
     8
from threading import Thread
morphbr@565
     9
morphbr@565
    10
class Media:
morphbr@565
    11
    class StreamListener(Thread):
morphbr@565
    12
        def __init__ (self, stream_data):
morphbr@565
    13
            Thread.__init__(self)
morphbr@565
    14
            self.stream = stream_data
morphbr@565
    15
            print "Thread Created"
morphbr@565
    16
morphbr@565
    17
        def run (self):
morphbr@565
    18
            #Create socket
morphbr@565
    19
            print "Waiting connection"
morphbr@565
    20
            self.stream.Socket.listen(1)
morphbr@565
    21
            self.stream.Connection, self.stream.Addr = self.stream.Socket.accept ()
morphbr@565
    22
            print "Connection requested"
morphbr@565
    23
            self.stream.Sink.set_property ("fd", self.stream.Connection.fileno())
morphbr@565
    24
            self.stream.Pipe.set_state(gst.STATE_PLAYING)
morphbr@565
    25
            print "PLAYING"
morphbr@565
    26
morphbr@565
    27
morphbr@565
    28
    class StreamData:
morphbr@565
    29
        stream_count = 0
morphbr@565
    30
morphbr@565
    31
        def __init__ (self, pipe, abin, vbin, sink):
morphbr@565
    32
            self.stream_count += 1
morphbr@565
    33
            self.Id = self.stream_count
morphbr@565
    34
            self.Pipe = pipe
morphbr@565
    35
            self.Abin = abin
morphbr@565
    36
            self.Vbin = vbin
morphbr@565
    37
            self.Sink = sink
morphbr@565
    38
            self.Loop = gobject.MainLoop()
morphbr@565
    39
            self.ACaps = ""
morphbr@565
    40
            self.VCaps = ""
morphbr@565
    41
            self.Ready = False
morphbr@565
    42
            self.Socket = None
morphbr@565
    43
            self.Connection = None
morphbr@565
    44
            self.Addr = None
morphbr@565
    45
morphbr@565
    46
    def __init__(self, config):
morphbr@565
    47
        # set gstreamer basic options
morphbr@565
    48
        self.config = config
morphbr@565
    49
        self.streams = []
morphbr@565
    50
        self.socket = None
morphbr@565
    51
        self.connection = None
morphbr@565
    52
        self.addr = None
morphbr@565
    53
        self.ready = False
morphbr@565
    54
		self.current = None
morphbr@565
    55
morphbr@565
    56
morphbr@565
    57
    def setup(self, uri, mux, vcodec, vbitrate,
morphbr@565
    58
              fps, acodec, abitrate, width, height, port, options):
morphbr@565
    59
morphbr@565
    60
        ## Pipelines
morphbr@565
    61
        pipe = gst.Pipeline ()
morphbr@565
    62
        print "Opening Uri:" + uri
morphbr@565
    63
        src = gst.element_make_from_uri (gst.URI_SRC, uri, "src")
morphbr@565
    64
        #src = gst.element_factory_make ("gnomevfssrc", "src")
morphbr@565
    65
        src.set_property ("location", uri)
morphbr@565
    66
        if (src is None):
morphbr@565
    67
            print "Fail to create src element"
morphbr@565
    68
            return None
morphbr@565
    69
morphbr@565
    70
        print ("Create source")
morphbr@565
    71
        decode = gst.element_factory_make ("decodebin", "decode")
morphbr@565
    72
        if (decode is None):
morphbr@565
    73
            print "Fail to create decodebin"
morphbr@565
    74
            return None
morphbr@565
    75
morphbr@565
    76
        print ("Create source")
morphbr@565
    77
        mux = gst.element_factory_make ("avimux", "mux")
morphbr@565
    78
        if (mux is None):
morphbr@565
    79
            print "Fail to create mux"
morphbr@565
    80
            return None
morphbr@565
    81
morphbr@565
    82
        sink = gst.element_factory_make ("fdsink", "sink")
morphbr@565
    83
        if (sink is None):
morphbr@565
    84
            print "Fail to create fdsink"
morphbr@565
    85
            return None
morphbr@565
    86
morphbr@565
    87
        print ("Create source")
morphbr@565
    88
morphbr@565
    89
        #video encode
morphbr@565
    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
morphbr@565
    91
        vbin = gst.Bin ()
morphbr@565
    92
        vqueue = gst.element_factory_make ("queue", "vqueue")
morphbr@565
    93
        colorspace = gst.element_factory_make ("ffmpegcolorspace", "")
morphbr@565
    94
        vrate = gst.element_factory_make ("videorate", "vrate")
morphbr@565
    95
        vencode = gst.element_factory_make ("ffenc_mpeg4", "vencode")
morphbr@565
    96
        #vencode = gst.element_factory_make ("ffenc_msmpeg4v1", "vencode")
morphbr@565
    97
        vqueue_src = gst.element_factory_make ("queue", "vqueue_src")
morphbr@565
    98
morphbr@565
    99
        #if (int(vbitrate) > 0):
morphbr@565
   100
        vencode.set_property ("bitrate", 200)
morphbr@565
   101
        #vencode.set_property ("quant-type", 1)
morphbr@565
   102
        vencode.set_property ("pass", 2)
morphbr@565
   103
        vencode.set_property ("quantizer", 5)
morphbr@565
   104
        #vencode.set_property ("me-method", 1)
morphbr@565
   105
morphbr@565
   106
morphbr@565
   107
        if (None in [vbin, vqueue, vrate, vencode, vqueue_src]):
morphbr@565
   108
            print "Fail to create video encode elements."
morphbr@565
   109
            return None
morphbr@565
   110
morphbr@565
   111
        vbin.add (vqueue)
morphbr@565
   112
        if ((int(width) > 0) and (int(height) > 0)):
morphbr@565
   113
            print ("formating output to %d / %d" % (int(width), int(height)))
morphbr@565
   114
  
morphbr@565
   115
            vscale = gst.element_factory_make ("ffvideoscale", "vscale")
morphbr@565
   116
morphbr@565
   117
            vbin.add (vscale);
morphbr@565
   118
            if (not vqueue.link (vscale)):
morphbr@565
   119
                print "Fail to link video elements"
morphbr@565
   120
                return None
morphbr@565
   121
morphbr@565
   122
            vbin.add (colorspace)
morphbr@565
   123
morphbr@565
   124
            if (not vscale.link (colorspace, \
morphbr@565
   125
                gst.caps_from_string ("video/x-raw-yuv,width=(int)%d,height=(int)%d" % (int(width), int(height))))):
morphbr@565
   126
                print "Fail to link video elements"
morphbr@565
   127
                return None
morphbr@565
   128
        else:                
morphbr@565
   129
            vbin.add (colorspace)
morphbr@565
   130
            vqueue.link (colorspace)
morphbr@565
   131
        
morphbr@565
   132
        vbin.add (vrate, vencode, vqueue_src)
morphbr@565
   133
        if (not colorspace.link (vrate)):
morphbr@565
   134
            print "Fail to colorspace with vrate"
morphbr@565
   135
            return None
morphbr@565
   136
morphbr@565
   137
morphbr@565
   138
        if (not vrate.link (vencode, \
morphbr@565
   139
            gst.caps_from_string ("video/x-raw-yuv,framerate=(fraction)10/1"))):
morphbr@565
   140
            print "Fail to link vrate element"
morphbr@565
   141
            return None
morphbr@565
   142
morphbr@565
   143
        if (not vencode.link (vqueue_src)):
morphbr@565
   144
            print "Fail to link video encode with queue"
morphbr@565
   145
            return None
morphbr@565
   146
morphbr@565
   147
        vbin.add_pad (gst.GhostPad ("sink", vqueue.get_pad ("sink")))
morphbr@565
   148
        vbin.add_pad (gst.GhostPad ("src", vqueue_src.get_pad ("src")))
morphbr@565
   149
morphbr@565
   150
        #audio encode
morphbr@565
   151
        #audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !  udpsink name=upd_audio host=224.0.0.1 port=5002
morphbr@565
   152
        abin = gst.Bin ()
morphbr@565
   153
        aqueue = gst.element_factory_make ("queue", "aqueue")
morphbr@565
   154
        aconvert = gst.element_factory_make ("audioconvert", "aconvert")
morphbr@565
   155
        arate = gst.element_factory_make ("audioresample", "arate")
morphbr@565
   156
        #aencode = gst.element_factory_make ("ffenc_ac3", "aencode")
morphbr@565
   157
        aencode = gst.element_factory_make ("queue", "aencode")
morphbr@565
   158
        #aencode = gst.element_factory_make ("lame", "aencode")
morphbr@565
   159
        #aencode = gst.element_factory_make ("ffenc_mp2", "aencode")
morphbr@565
   160
        aqueue_src = gst.element_factory_make ("queue", "aqueue_src")
morphbr@565
   161
morphbr@565
   162
        if (None in [abin, aqueue, arate, aencode, aqueue_src]):
morphbr@565
   163
            print "Fail to create video encode elements."
morphbr@565
   164
            return None
morphbr@565
   165
morphbr@565
   166
        abin.add (aqueue, aconvert, arate, aencode, aqueue_src)
morphbr@565
   167
        if (not gst.element_link_many (aqueue,  aconvert, arate, aencode, aqueue_src)):
morphbr@565
   168
            print "Fail to link video elements"
morphbr@565
   169
            return None
morphbr@565
   170
morphbr@565
   171
        abin.add_pad (gst.GhostPad ("sink", aqueue.get_pad ("sink")))
morphbr@565
   172
        abin.add_pad (gst.GhostPad ("src", aqueue_src.get_pad ("src")))
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
            print "Fail to link audio with mux"
morphbr@565
   188
            return None
morphbr@565
   189
morphbr@565
   190
        if (video_pad.link (mux_video) != gst.PAD_LINK_OK):
morphbr@565
   191
            print "Fail to link audio with mux"
morphbr@565
   192
            return None
morphbr@565
   193
morphbr@565
   194
        stream_data = self.StreamData (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, stream_data)
morphbr@565
   198
morphbr@565
   199
        decode.connect("new-decoded-pad", self.__on_decode_new_pad, stream_data)
morphbr@565
   200
        decode.connect("unknown-type", self.__on_decode_unknown_type, stream_data)
morphbr@565
   201
morphbr@565
   202
        print ("Create source")
morphbr@565
   203
        pipe.set_state (gst.STATE_PAUSED)
morphbr@565
   204
        print "Running Pipe"
morphbr@565
   205
        stream_data.Loop.run ()
morphbr@565
   206
        print "End run"
morphbr@565
   207
morphbr@565
   208
morphbr@565
   209
        #Create socket
morphbr@565
   210
        stream_data.Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
morphbr@565
   211
        print "Bind on port %d" % port
morphbr@565
   212
        stream_data.Socket.bind(('', int (port)))
morphbr@565
   213
        self.streams.append (stream_data)
morphbr@565
   214
        return (True, "")
morphbr@565
   215
morphbr@565
   216
    def play(self):
morphbr@565
   217
        print "Play"
morphbr@565
   218
        stream = self.streams[0]
morphbr@565
   219
        self.current = self.StreamListener(stream)
morphbr@565
   220
        self.current.start ()
morphbr@565
   221
        time.sleep (1)
morphbr@565
   222
        return (True, "")
morphbr@565
   223
morphbr@565
   224
    def stop(self):
morphbr@565
   225
		self.current.join ()
morphbr@565
   226
        self.current = None
morphbr@565
   227
        stream = self.streams[0]
morphbr@565
   228
        stream.Pipe.set_state(gst.STATE_NULL)
morphbr@565
   229
        del (stream.Pipe)
morphbr@565
   230
        stream.Pipe = None
morphbr@565
   231
        stream.Abin = None
morphbr@565
   232
        stream.Vbin = None
morphbr@565
   233
        stream.Sink = None
morphbr@565
   234
		if (stream.Connection != None):
morphbr@565
   235
	        stream.Connection.close ()
morphbr@565
   236
morphbr@565
   237
        self.streams = []
morphbr@565
   238
        time.sleep (5)             
morphbr@565
   239
        return (True, "")
morphbr@565
   240
morphbr@565
   241
morphbr@565
   242
    def __on_bus_message (self, bus, message, stream_data):
morphbr@565
   243
morphbr@565
   244
        t = message.type
morphbr@565
   245
        if (t == gst.MESSAGE_STATE_CHANGED):
morphbr@565
   246
            oldstate = -1
morphbr@565
   247
            newstate = -1
morphbr@565
   248
            pending = -1
morphbr@565
   249
            oldstate, newstate, pending = message.parse_state_changed ()
morphbr@565
   250
            if ((oldstate == gst.STATE_READY) and \
morphbr@565
   251
                (newstate == gst.STATE_PAUSED) and \
morphbr@565
   252
                (pending == gst.STATE_VOID_PENDING) and \
morphbr@565
   253
                (stream_data.Ready == False)):
morphbr@565
   254
                state_changed_status, current_state, pending_state = stream_data.Pipe.get_state ()
morphbr@565
   255
                if ((current_state == gst.STATE_PAUSED) and \
morphbr@565
   256
                    (pending_state == gst.STATE_VOID_PENDING)):
morphbr@565
   257
                    print "Pipe paused"
morphbr@565
   258
                    stream_data.Loop.quit ()
morphbr@565
   259
                    stream_data.Ready = True
morphbr@565
   260
        elif (t == gst.MESSAGE_ERROR):
morphbr@565
   261
            err, debug = message.parse_error()
morphbr@565
   262
            print "Error: %s" % err, debug
morphbr@565
   263
            stream_data.Loop.quit ()
morphbr@565
   264
            stream_data.Ready = False
morphbr@565
   265
morphbr@565
   266
        return True
morphbr@565
   267
morphbr@565
   268
    def __on_decode_unknown_type (self, decode, pad, caps, stream_data):
morphbr@565
   269
morphbr@565
   270
        print "Unknown Type"
morphbr@565
   271
        return None
morphbr@565
   272
morphbr@565
   273
    def __on_decode_new_pad (self, decode, pad, arg1, stream_data):
morphbr@565
   274
morphbr@565
   275
        caps = pad.get_caps().to_string()
morphbr@565
   276
        print "New pad " + caps
morphbr@565
   277
        if (caps.rfind ("audio") != -1):
morphbr@565
   278
            apad = stream_data.Abin.get_pad ("sink")
morphbr@565
   279
            if (pad.link (apad) != gst.PAD_LINK_OK):
morphbr@565
   280
                print "Error on link audio pad"
morphbr@565
   281
                return None
morphbr@565
   282
        elif (caps.rfind ("video") != -1):
morphbr@565
   283
            vpad = stream_data.Vbin.get_pad ("sink")
morphbr@565
   284
            if (pad.link (vpad) != gst.PAD_LINK_OK):
morphbr@565
   285
                print "Error on link video pad"
morphbr@565
   286
                return None
morphbr@565
   287
        else:
morphbr@565
   288
            print "Invalid caps"
morphbr@565
   289
        print "Linked"            
morphbr@565
   290