Video playback with renderer accelleration fails (works fine with SW)

Hello everyone,

First of all apologies if this has been asked before, but I honestly couldn’t find it.
So here’s my problem, I’m trying to create a simple video/stream player using SDL2.
I’m using gstreamer to get the stream from an RTSP/H264 source.

I managed to get it working when I create the renderer with SDL_RENDERER_SOFTWARE flag on, but if I leave it empty (or set SDL_RENDERER_ACCELERATED) to get HW accelleration, nothing is displayed.
I’m sure I’m missing something very fundamental, but being a newbie in SDL2 world I can’t put my finger on it.

Here’s the source code:
http://tpcg.io/_3MENV0
(not sure what the common practice here is for sharing code)

The non-working output of the above code is:

** (process:35303): DEBUG: 18:09:08.819: Detected display 0: 1920 x 1080
** (process:35303): DEBUG: 18:09:08.844: Driver 0 : opengl
** (process:35303): DEBUG: 18:09:08.844: Driver 1 : opengles2
** (process:35303): DEBUG: 18:09:08.844: Driver 2 : software
** (process:35303): DEBUG: 18:09:08.844: Current video driver: x11
** (process:35303): DEBUG: 18:09:08.844: Rendering driver: opengl
** (process:35303): DEBUG: 18:09:08.846: Starting pipeline
** (process:35303): DEBUG: 18:09:08.846: GST init
** (rtspplayer:35303): DEBUG: 18:09:08.857: GST Parse launch
** (rtspplayer:35303): DEBUG: 18:09:08.894: GST Setting frame callback (0x648abef3cc10)
** (rtspplayer:35303): DEBUG: 18:09:08.894: GST Set to ready
** (rtspplayer:35303): DEBUG: 18:09:08.894: GST Set to playing
** (rtspplayer:35303): DEBUG: 18:09:08.894: Pipeline playing
** (rtspplayer:35303): DEBUG: 18:09:18.202: Rendered!
** (rtspplayer:35303): DEBUG: 18:09:18.242: Rendered!
** (rtspplayer:35303): DEBUG: 18:09:18.283: Rendered!
** (rtspplayer:35303): DEBUG: 18:09:18.325: Rendered!
** (rtspplayer:35303): DEBUG: 18:09:18.365: Rendered!
...
...

Some additional info:
glxinfo shows “direct rendering: Yes”
Running on Ubuntu 23.04 and Intel CPU and integrated GPU with, supposedly, all proper GPU drivers installed.

The same way, when trying to use the wayland driver I get the same behavior (nothing plays)

Thank you very much!

You call SDL_UpdateTexture() which is documented as “a fairly slow function, intended for use with static textures that do not change often” which doesn’t seem to fit a video playback application at all!

I would suggest you create your videoTexture as SDL_TEXTUREACCESS_STREAMING then update it using SDL_LockTexture() and SDL_UnlockTexture().

2 Likes

You can look at testffmpeg.c to see how to do hardware accelerated video playback on SDL3.

2 Likes

Thank you both for your tips.
I tried using SDL_LockTexture/SDL_UnlockTexture but I’m getting a greenish picture, most likely because of the pixel format used (YUV).
Actually, if I look at the example suggested by slouken, the YUV texture is using SDL_UpdateYUVTexture not lock/unlock.

In any case, the inital issue I was having (can’t get it to work with HW accelleration enabled) seems to persist.

It works OK for me. Are you definitely creating your texture with the correct pixel format? This is what I use:

  tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_UYVY, SDL_TEXTUREACCESS_STREAMING, XLEN, YLEN);

Then I render the frames like this (in my case reading directly from a video file to the texture to avoid any wasteful copy operations):

  SDL_LockTexture(tex, NULL, &pixels, &pitch);
  SDL_RWread(file, pixels, size, 1);
  SDL_UnlockTexture(tex);
  SDL_RenderCopy(renderer, tex, NULL, &dst);

This works fine with an accelerated renderer here, but I should emphasise that I’m using OpenGL (even in Windows), not Direct3D.

You can run my code here to prove it really works!

1 Like

Once again, thank you for the pointers.
I managed to get all 3 methods working fine (SDL_UpdateTexture, SDL_UpdateYUVTexture and SDL_LockTexture/SDL_UnlockTexture). Video plays nicely.

What I still couldn’t get working was the HW accelleration.
Whenever I set the renderer flag to SDL_RENDERER_ACCELERATED I get nothing on the screen (black), although the code processing each frame seems to be working fine.
Any ideas here are much appreciated.