获取PyGI中的窗口句柄

时间:2014-09-13 13:07:56

标签: gstreamer gtk3 pygobject python-gstreamer

在我的程序中,我使用PyGObject / PyGI和GStreamer在我的GUI中显示视频。视频显示在Gtk.DrawingArea中,因此我需要在realize - 信号处理程序中获取它的窗口句柄。在Linux上,我使用:

获得该句柄
drawing_area.get_property('window').get_xid()

但是如何在Windows上获得处理?

我在互联网上搜索但是只找到使用window.handle的PyGtk的例子,这些例子使用PyGI无效。

GStreamer文档提供example,它使用GDK_WINDOW_HWND宏来获取句柄。此宏使用AFAIK gdk_win32_drawable_get_handle。但是如何使用PyGI在Python中完成它?

更新15-07-28:添加(简化)代码
我还没有让视频播放在Windows上运行 问题1:我无法在_on_video_realize()中获取窗口句柄。
问题2:永远不会调用方法_on_player_sync_message()。

class MultimediaPlayer:
    def __init__(self):
        # ... some init stuff ...

        self._drawing_area.connect('realize', self._on_video_realize)
        self._drawing_area.connect('unrealize', self._on_video_unrealize)

        # GStreamer setup
        # ---------------
        self._player = Gst.ElementFactory.make('playbin', 'MultimediaPlayer')
        bus = self._player.get_bus()
        bus.add_signal_watch()
        bus.connect('message', self._on_player_message)
        bus.enable_sync_message_emission()
        bus.connect('sync-message::element', self._on_player_sync_message)

    def _on_video_realize(self, widget):
        print('----------> _on_video_realize')
        # The xid must be retrieved first in GUI-thread and before
        # playing pipeline.
        if sys.platform == "win32":
            self._drawing_area.get_property('window').ensure_native()
            # -------------------------------------------------------------
            # TODO [PROBLEM 1] How to get handle here?
            #                  self._drawing_area.GetHandle() does not exist!
            # -------------------------------------------------------------
        else:
            self._wnd_hnd = (self._drawing_area.get_property('window')
                                                                    .get_xid())

    def _on_video_unrealize(self, widget):
        self._player.set_state(Gst.State.NULL)

    def _on_player_message(self, bus, message):
        # ... handle some messages here ...

    def _on_player_sync_message(self, bus, message):
        # ---------------------------------------------------------------------
        # TODO [PROBLEM 2] This method is never called on Windows after opening
        #                  a video_file! But on Linux it is!
        # ---------------------------------------------------------------------

        print('----------> _on_player_sync_message')
        if message.get_structure() is None:
            return True
        if message.get_structure().get_name() == "prepare-window-handle":
            imagesink = message.src
            imagesink.set_property("force-aspect-ratio", True)
            imagesink.set_window_handle(self._wnd_hnd)

    def play(self):
        self._player.set_state(Gst.State.PLAYING)

    def stop(self):
        self._player.set_state(Gst.State.NULL)

    def set_file(self, file):
        # ... 
        self._player.set_property('uri', "file:///" + file)

2 个答案:

答案 0 :(得分:1)

我终于明白了。为了解决“窗口句柄”问题,我使用了Marwin Schmitt(see here)的解决方法/ hack:

def _on_video_realize(self, widget):
    # The window handle must be retrieved first in GUI-thread and before
    # playing pipeline.
    video_window = self._drawing_area.get_property('window')
    if sys.platform == "win32":
        if not video_window.ensure_native():
            print("Error - video playback requires a native window")
        ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
        ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object]
        drawingarea_gpointer = ctypes.pythonapi.PyCapsule_GetPointer(video_window.__gpointer__, None)
        gdkdll = ctypes.CDLL ("libgdk-3-0.dll")
        self._video_window_handle = gdkdll.gdk_win32_window_get_handle(drawingarea_gpointer)
    else:
        self._video_window_handle = video_window.get_xid()

但也有问题,“sync-message” - 处理程序从未被调用过。我发现并非所有视频接收器都支持嵌入式视频see here。例如,d3dvideosink确实支持嵌入式视频,但我在虚拟机中运行Windows,即使激活了3D硬件加速,它也可能不起作用。在非虚拟化的Windows上运行相同的代码会导致回调“sync-message”-handler,其中可以设置先前获取的窗口句柄:

def _on_player_sync_message(self, bus, message):
    if message.get_structure() is None:
        return
    if not GstVideo.is_video_overlay_prepare_window_handle_message(message):
        return
    imagesink = message.src
    imagesink.set_property("force-aspect-ratio", True)
    imagesink.set_window_handle(self._video_window_handle)

在Windows上播放现在运行正常。

答案 1 :(得分:0)

你试过了吗?

def OnSyncMessage(self, bus, msg):
    if msg.get_structure() is None:
        return True
    message_name = msg.get_structure().get_name()
    if message_name == 'prepare-window-handle':
        imagesink = msg.src
        imagesink.set_property('force-aspect-ratio', True)
        imagesink.set_window_handle(self.DrawingArea.GetHandle())
    return True