在Python Gstreamer中连接文件

时间:2015-04-27 06:04:34

标签: python streaming gstreamer audio-streaming python-gstreamer

我想使用pygst将文件从一台计算机传输到另一台计算机。我写了以下代码:

import gobject
import pygst
pygst.require("0.10")
import gst
import sys

class CustomData:
    is_live = False
    pipeline = None
    loop = None
    src = None

def cb_message(bus, msg, data):
    def error():
        print >> sys.stderr, ("Error: {0}".format(err.message))
        data.pipeline.set_state(gst.STATE_READY)
        data.loop.quit()
    def eos():
        data.pipeline.set_state(gst.STATE_READY)
        data.src.set_property("location", "/home/user/con1.wav")
        data.pipeline.set_state(gst.STATE_PLAYING)
    def buffering():
        percent = 0

        if (data.is_live):
            return
        percent = msg.parse_buffering()
        sys.stdout.write("\rBuffering ({0}%)".format(percent))
        sys.stdout.flush()

        if (percent < 0):
            data.pipeline.set_state(gst.STATE_PAUSED)
        else:
            data.pipeline.set_state(gst.STATE_PLAYING)
    def clock_lost():
        #Get a new clock
        data.pipeline.set_state(gst.STATE_PAUSED)
        data.pipeline.set_state(gst.STATE_PLAYING)
    def default():
        pass

    handlers = dict({gst.MESSAGE_ERROR: error, 
                     gst.MESSAGE_EOS: eos,
                     gst.MESSAGE_BUFFERING: buffering,
                     gst.MESSAGE_CLOCK_LOST: clock_lost
                     })
    if (handlers.has_key(msg.type)):
        handlers[msg.type]()

def send_file(port):
    data = CustomData()
    pipeline = gst.Pipeline("client")
    bus = pipeline.get_bus()
    src = gst.element_factory_make("filesrc","curr_src")
    src.set_property("location", "/home/user/con2.wav")
    pipeline.add(src)

    client = gst.element_factory_make("tcpclientsink", "client")
    pipeline.add(client)
    client.set_property("host", "192.168.1.100")
    client.set_property("port", port)
    src.link(client)
    ret = pipeline.set_state(gst.STATE_PLAYING)
    if (ret == gst.STATE_CHANGE_FAILURE):
        print >> sys.stderr, ("Unable to set pipeline to the playing     state")
        exit(-1)
    # enter into a mainloo
    loop = gobject.MainLoop()
    data.loop = loop
    data.pipeline = pipeline
    data.src = src

    bus.enable_sync_message_emission()
    bus.add_signal_watch()
    bus.connect("message", cb_message, data)
    data.loop.run()
    #loop.run()

if __name__ == '__main__':
    send_file(int(sys.argv[1]))

但它只播放第一个文件然后停止。正如你所看到的,在EOS上我停止了管道并更改了src,但它没有给我任何东西。

如何将代码转换为&#34;连接&#34;第二个文件?

0 个答案:

没有答案