与Python的GStreamer和PyQt的不很好地工作(Gstreamer with Pytho

2019-10-24 11:02发布

我有两个树莓派。 并希望从一个到另一个流视频。 为做到这一点我用第一树莓派下面的命令来播放流式视频:

raspivid -t 999999 -w 1080 -h 720 -fps 25 -hf -b 2000000 -o - | \gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 \! gdppay ! tcpserversink host=serverIp port=5000

在第二树莓裨我用下面的PyQt代码来捕捉视频流:

import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GstVideo
GObject.threads_init()
Gst.init(None)
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        # Create GStreamer pipeline
        self.Video_pipeline()

##############################################################################
#VIDEO Pipeline
#|
#|
#V
##############################################################################
    def Video_pipeline(self):
        self.pipeline = Gst.Pipeline()
        self.tcpsrc = Gst.ElementFactory.make('tcpclientsrc','tcpsrc')
        self.tcpsrc.set_property("host", '192.168.1.9')
        self.tcpsrc.set_property("port", 5000)
        self.gdepay = Gst.ElementFactory.make('gdpdepay', 'gdepay')
        self.rdepay = Gst.ElementFactory.make('rtph264depay', 'rdepay')
        self.avdec = Gst.ElementFactory.make('avdec_h264', 'avdec')
        self.vidconvert = Gst.ElementFactory.make('videoconvert', 'vidconvert')
        self.asink = Gst.ElementFactory.make('autovideosink', 'asink')
        self.asink.set_property('sync', False)

        self.pipeline.add(self.tcpsrc)
        self.pipeline.add(self.gdepay)
        self.pipeline.add(self.avdec)
        self.pipeline.add(self.rdepay)
        self.pipeline.add(self.vidconvert)
        self.pipeline.add(self.asink)

        self.tcpsrc.link(self.gdepay)
        self.gdepay.link(self.rdepay)
        self.rdepay.link(self.avdec)
        self.avdec.link(self.vidconvert)
        self.vidconvert.link(self.asink)
##############################################################################
#^
#|
#|
#VIDEO Pipeline
##############################################################################

    def start(self):
        self.pipeline.set_state(Gst.State.PLAYING)
        self.pipeline_A.set_state(Gst.State.PLAYING)
        #self.showMaximized()



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.start()
    sys.exit(app.exec_())

它工作得很好。 现在我需要设计一个GUI。 我写了这个代码:

import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GstVideo
GObject.threads_init()
Gst.init(None)
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)


        #creat Simple Window
        container = QtGui.QWidget(self)
        container.setWindowTitle('Test1')
        container.connect('destroy', self.quit)
        self.setCentralWidget(container)
        self.winId = container.winId()
        self.resize(480, 320)

        qbtn = QtGui.QPushButton('Quit', self)
        qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(50, 50)

                # Create GStreamer pipeline
        self.Video_pipeline()

        # Create bus to get events from GStreamer pipeline
        self.bus = self.pipeline.get_bus()
        self.bus.add_signal_watch()
        self.bus.enable_sync_message_emission()
        self.bus.connect('message::error', self.on_error)
        self.bus.connect('message::eos', self.on_eos)
        self.bus.connect('sync-message::element', self.on_sync_message)
##############################################################################
#VIDEO Pipeline
#|
#|
#V
##############################################################################
    def Video_pipeline(self):
        self.pipeline = Gst.Pipeline()
        self.tcpsrc = Gst.ElementFactory.make('tcpclientsrc','tcpsrc')
        self.tcpsrc.set_property("host", '192.168.1.9')
        self.tcpsrc.set_property("port", 5000)

        self.gdepay = Gst.ElementFactory.make('gdpdepay', 'gdepay')


        self.rdepay = Gst.ElementFactory.make('rtph264depay', 'rdepay')

        self.avdec = Gst.ElementFactory.make('avdec_h264', 'avdec')

        self.vidconvert = Gst.ElementFactory.make('videoconvert', 'vidconvert')

        self.asink = Gst.ElementFactory.make('autovideosink', 'asink')
        self.asink.set_property('sync', False)
        #self.asink.set_property('emit-signals', True)
        #self.set_property('drop', True)

        self.pipeline.add(self.tcpsrc)
        self.pipeline.add(self.gdepay)

        self.pipeline.add(self.avdec)

        self.pipeline.add(self.rdepay)

        self.pipeline.add(self.vidconvert)
        self.pipeline.add(self.asink)

        self.tcpsrc.link(self.gdepay)
        self.gdepay.link(self.rdepay)
        self.rdepay.link(self.avdec)
        self.avdec.link(self.vidconvert)
        self.vidconvert.link(self.asink)
##############################################################################
#^
#|
#|
#VIDEO Pipeline
##############################################################################

    def on_sync_message(self, bus, message):
        if message.get_structure().get_name() == 'prepare-window-handle':
            message.src.set_property('force-aspect-ratio', True)
            message.src.set_window_handle(self.winId)

    def quit(self, container):
        self.pipeline.set_state(Gst.State.NULL)
        self.pipeline_A.set_state(Gst.State.NULL)
        Gtk.main_quit()

    def on_eos(self, bus, msg):
        print('on_eos(): seeking to start of video')
        self.pipeline.seek_simple(
            Gst.Format.TIME,        
            Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT,
            0
        )

    def on_error(self, bus, msg):
        print('on_error():', msg.parse_error())

    def start(self):
        self.pipeline.set_state(Gst.State.PLAYING)
        self.pipeline_A.set_state(Gst.State.PLAYING)
        self.showMaximized()



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    window.start()
    sys.exit(app.exec_())

这个代码在我的Ubuntu系统工作得很好。 它显示了流式视频和一个按钮。 但是当我运行在树莓派这个代码,显示了一会儿一个窗口,那么它的消失了。

我得到Ubunu这些警告:

(DO2.py:7495):的GStreamer-WARNING **:gstpad.c:4506:store_sticky_event:粘事件乱序,得到了 '段' 前 '帽'

(DO2.py:7495):的GStreamer-WARNING **:gstpad.c:4506:store_sticky_event:粘事件乱序,得到了 '段' 前 '帽'

而这些对树莓:

qgtkstyle无法检测当前GTK(DO2.py:7495):

GStreamer的-WARNING **:gstpad.c:4506:store_sticky_event:粘事件乱序,得到了 '段' 前 '帽子'

(DO2.py:7495):的GStreamer-WARNING **:gstpad.c:4506:store_sticky_event:粘事件乱序,得到了 '段' 前 '帽' 分段故障

请帮我的问题或GUID我如何设计一个GUI在树莓工作。 非常感谢

文章来源: Gstreamer with Python and PyQt does not work very well