Gstreamer总线日志信息在哪里?

问题描述 投票:1回答:1

我正试图流式传输一个 .mp4 到RTSP服务器,使用 Gstreamer 在python中

import sys
import gi

gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
gi.require_version('GstRtsp', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib, GstRtsp

loop = GLib.MainLoop()
Gst.init(None)
file_path = "test.mp4"
class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self):
        GstRtspServer.RTSPMediaFactory.__init__(self)

    def do_create_element(self, url):
        src_demux = f"filesrc location={file_path} ! qtdemux name=demux"
        h264_transcode = "demux.video_0"
        pipeline = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)
        print ("Element created: " + pipeline)

        self._pipeline = Gst.parse_launch(pipeline)
        def bus_handler(bus, message):
            print(message)
        self.bus = self._pipeline.get_bus()
        self.bus.connect('message', bus_handler)
        self.bus.add_signal_watch_full(1)
        return self._pipeline

class GstreamerRtspServer():
    def __init__(self):
        self.rtspServer = GstRtspServer.RTSPServer()
        factory = TestRtspMediaFactory()
        factory.set_shared(True)
        mountPoints = self.rtspServer.get_mount_points()
        self.address = '127.0.0.1' #my RPi's local IP
        self.port = '8553'
        self.rtspServer.set_address(self.address)
        self.rtspServer.set_service(self.port)
        urlstr = "/user=&password=.sdp"
        url = GstRtsp.RTSPUrl.parse(urlstr)
        mountPoints.add_factory(urlstr, factory)

        self.rtspServer.attach(None)

if __name__ == '__main__':
    s = GstreamerRtspServer()
    loop.run()

但我想了解如何使用 Gstreamer巴士 以记录以下信息 eos 或错误和警告,但我没有看到任何错误和警告,甚至当我发送eos事件和流媒体有效地停止了

s.rtspServer._pipeline._end_stream_event.set()
s.rtspServer._pipeline.send_event(Gst.Event.new_eos())

我是否正确使用?如果不正确,我怎样才能正确记录总线信息?

python gstreamer rtsp gstreamer-1.0 python-gstreamer
1个回答
0
投票

下面的解决方案是基于 这个 接受的,但不知为何不完整的答案。

我找到了不需要 "手动 "创建管道元素的方法,但它保持了(在这种情况下)便捷的 Gst.parse_launch(pipelineCmd) 方法,并扩展了Gst.Bin以实现消息调试。

这里是完整的示例源代码(检查注释行以获得一些解释)。

#!/usr/bin/env python

import sys
import gi

gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib

Gst.init(None)
loop = GLib.MainLoop()

# extended Gst.Bin that overrides do_handle_message and adds debugging
class ExtendedBin(Gst.Bin):
    def do_handle_message(self,message):
        if message.type == Gst.MessageType.ERROR:
            error, debug = message.parse_error()
            print("ERROR:", message.src.get_name(), ":", error.message)
            if debug:
                print ("Debug info: " + debug)
        elif message.type == Gst.MessageType.EOS:
            print ("End of stream")
        elif message.type == Gst.MessageType.STATE_CHANGED:
            oldState, newState, pendingState = message.parse_state_changed()
            print ("State changed -> old:{}, new:{}, pending:{}".format(oldState,newState,pendingState))
        else :
            print("Some other message type: " + str(message.type))

        #call base handler to enable message propagation
        Gst.Bin.do_handle_message(self,message)

class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):

    def __init__(self):
        GstRtspServer.RTSPMediaFactory.__init__(self)

    def do_create_element(self, url):
        #set mp4 file path to filesrc's location property
        src_demux = "filesrc location=/path/to/dir/test.mp4 ! qtdemux name=demux"
        h264_transcode = "demux.video_0"
        #uncomment following line if video transcoding is necessary
        #h264_transcode = "demux.video_0 ! decodebin ! queue ! x264enc"
        pipelineCmd = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)

        self.pipeline = Gst.parse_launch(pipelineCmd)
        print ("Pipeline created: " + pipelineCmd)

        # creates extended Gst.Bin with message debugging enabled
        extendedBin = ExtendedBin("extendedBin")

        # Gst.pipeline inherits Gst.Bin and Gst.Element so following is possible
        extendedBin.add(self.pipeline)

        # creates new Pipeline and adds extended Bin to it
        self.extendedPipeline = Gst.Pipeline.new("extendedPipeline")
        self.extendedPipeline.add(extendedBin)

        return self.extendedPipeline

class GstreamerRtspServer(GstRtspServer.RTSPServer):
    def __init__(self):
        self.rtspServer = GstRtspServer.RTSPServer()
        self.factory = TestRtspMediaFactory()
        self.factory.set_shared(True)
        mountPoints = self.rtspServer.get_mount_points()
        mountPoints.add_factory("/stream", self.factory)
        self.rtspServer.attach(None)
        print ("RTSP server is ready")

if __name__ == '__main__':
    s = GstreamerRtspServer()
    loop.run()

请注意 Gst.Pipeline 实际上是继承者的延伸 Gst.Bin (和Gst.Element),所以有可能(不管听起来多么奇怪) 将管道添加到bin中.

这个小 "技巧 "为我们这些 "懒惰 "的程序员节省了时间,使他们可以继续使用解析命令行语法来创建管道元素。

在一些比较复杂的情况下,命令行语法的解析是不适用的,解决办法如下。

  • 创建ExtendedBin,
  • "手动 "创建元素与 Gst.ElementFactory.make 方法(并设置必要的属性)
  • 添加创建的元素到 ExtendedBean
  • 链接元素
  • 创建新的管道,并添加bin到其中
  • 在需要的地方使用管道。
© www.soinside.com 2019 - 2024. All rights reserved.