Get a window handle in PyGI

In my program, I use PyGObject / PyGI and GStreamer to display videos in my GUI. The video is shown in Gtk.DrawingArea, and so I need to get its window handle in the realize-signal handler. On Linux, I get this handle using:

drawing_area.get_property('window').get_xid()

But how do I get a handle on Windows?

I searched on the Internet but found only examples for PyGtk using window.handlethat which does not work using PyGI.

The GStreamer documentation provides an example that uses a macro GDK_WINDOW_HWNDto retrieve a handle. This macro uses AFAIK gdk_win32_drawable_get_handle. But how to do it in Python using PyGI?

Update 15-07-28: Added (simplified) code.
I still do not get video playback for working in Windows.
Problem 1: I cannot get the window handle in _on_video_realize ().
Problem 2: The _on_player_sync_message () method is never called.

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)
+1
source share
2 answers

I finally understood. To refer to the description of "window handle", I use the Marwin Schmitt workaround / hack method ( see here ):

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" . , , . . , 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

:

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    
0

All Articles