Search code examples
python-3.xsdl-2libvlc

Python error "expected LP_SDL_Window" when trying "SDL_GetWindowID"


I'm trying to get the Window ID of the SDL window, to give to VLC so it can play the video in the window.

Being new to Python, I'm vaguely aware this has to do with variable type conversions to play nice with SDL, and using the correct python binding...

The line with the error is "win_id = SDL_GetWindowID(window)"

Here is my code;

import sys
import sdl2.ext
import vlc

import ctypes
from sdl2 import *

RESOURCES = sdl2.ext.Resources(__file__, "resources")
sdl2.ext.init()

window = sdl2.ext.Window("Hello World!", size=(640, 480))
window.show()

factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)
sprite = factory.from_image(RESOURCES.get_path("hello.bmp"))

spriterenderer = factory.create_sprite_render_system(window)
spriterenderer.render(sprite)


vlcInstance = vlc.Instance("--no-xlib")
player = vlcInstance.media_player_new()
win_id = SDL_GetWindowID(window)
player.set_xwindow(win_id)
player.set_mrl("agro.mp4")
player.play()

processor = sdl2.ext.TestEventProcessor()
processor.run(window)
sdl2.ext.quit()

Solution

  • What you get with SDL_GetWindowID is SDL's internal window ID that it itself refers to in e.g. events. What you need is X11 window ID, which you can get through SDL_GetWindowWMInfo. That however requires some trickery with SDL versioning, e.g. (I'm not sure it is safe to call that in python if SDL version changes but pysdl2 is not updated):

    wminfo = SDL_SysWMinfo();
    SDL_GetVersion(wminfo.version);
    if(SDL_GetWindowWMInfo(window.window, wminfo) == 0):
        print("can't get SDL WM info");
        sys.exit(1);
    
    win_id = wminfo.info.x11.window;
    

    Then use that win_id to feed to vlc.