代码之家  ›  专栏  ›  技术社区  ›  WisdomPill

Gstreamer python获取总线消息重新连接

  •  0
  • WisdomPill  · 技术社区  · 7 年前

    from tkinter.ttk import Widget
    
    import gi
    
    # Needed for set_window_handle():
    gi.require_version('GstVideo', '1.0')
    gi.require_version('Gst', '1.0')
    from gi.repository import Gst, GstVideo
    
    
    class GstWidget(Widget):
        def __init__(self, gst_launch_string, x, y, width, height, master=None, **kw):
            super(GstWidget, self).__init__(master, 'frame', **kw)
    
            self.place(x=x, y=y, width=width, height=height)
    
            self.frame_id = self.winfo_id()
    
            self.player = Gst.parse_launch(gst_launch_string)
            self.player.set_state(Gst.State.PLAYING)
    
            self.bus = self.player.get_bus()
            self.bus.add_signal_watch()
            self.bus.connect('message::error', self.on_error)
            self.bus.connect('message::state-changed', self.on_status_changed)
            self.bus.connect('message::eos', self.on_eos)
            self.bus.connect('message::info', self.on_info)
            self.bus.enable_sync_message_emission()
            self.bus.connect('sync-message::element', self.set_frame_handle)
    
        def on_status_changed(self, bus, message):
            print('status_changed message -> {}'.format(message))
    
        def on_eos(self, bus, message):
            print('eos message -> {}'.format(message))
    
        def on_info(self, bus, message):
            print('info message -> {}'.format(message))
    
        def on_error(self, bus, message):
            print('error message -> {}'.format(message.parse_error()))
    
        def play(self):
            print('Current state of my pipeline is {}'.format(self.player.current_state))
            print('setting pipeline state to playing')
            self.player.set_state(Gst.State.PLAYING)
    
        def close(self):
            self.player.set_state(Gst.State.NULL)
    
        def is_playing(self):
            print('\t\t{}'.format(self.player.current_state))
            return self.player.current_state is not Gst.State.PLAYING
    
        def set_frame_handle(self, bus, message):
            if message.get_structure().get_name() == 'prepare-window-handle':
                frame = message.src
                frame.set_property('force-aspect-ratio', True)
                frame.set_window_handle(self.frame_id)
    

    有人能指出我哪里错了吗? 我为所有事件设置了断点,而且它不会打印我想观看的事件中的任何内容。

    1 回复  |  直到 7 年前
        1
  •  0
  •   WisdomPill    7 年前

    我终于明白了。“问题”在于tkinter主回路。 由于GStreamer严重依赖Glib,而tkinter不使用Glib mainloop,因此我不得不创建一个类似于@jldupont at的建议 tkinter integration with glib mainloop