SDL icon indicating copy to clipboard operation
SDL copied to clipboard

SDL3: Linux V4L2 cameras never acquire a frame

Open BlockBuilder57 opened this issue 1 year ago • 15 comments

Hello, I've just seen that SDL3 seems to be (experimentally) ready for use, so I've been trying to experiment with the new stuff. However, when I try to acquire a frame on a camera, I am only ever seeing null pointers returned, even when running the example file. A tight loop to see if it just takes some time also doesn't seem to help.

This seems to be irrespective of format as well, which seems to be against what was seen in #9263. I've tried a capture card and Droidcam's loopback camera, and neither seem to work. SDL definitely sees the cameras - they're listed by SDL_GetCameras and SDL_GetCameraSupportedFormats lists each one of their formats - but acquiring a frame seems to be out of reach.

I'm running Arch, and I'm on the latest sdl3 package on the AUR. At the moment, that's SDL3-preview-3.1.6 @ fe3566c. Definitely includes #10881, so I believe this to be a separate issue. If needed for reference, here's a dump of the only SDL_CameraSpec that Droidcam returns. (The capture card returns around 120 of them!)

desiredSpec = {SDL_CameraSpec} 
 format = {SDL_PixelFormat} SDL_PIXELFORMAT_IYUV
 colorspace = {SDL_Colorspace} SDL_COLORSPACE_BT709_LIMITED
 width = {int} 640
 height = {int} 480
 framerate_numerator = {int} 30
 framerate_denominator = {int} 1

If there's any additional debugging information that's needed, please let me know.

BlockBuilder57 avatar Nov 15 '24 19:11 BlockBuilder57

Does the SDL testcamera app work for you?

slouken avatar Nov 15 '24 19:11 slouken

No, I just built it and it does not work on either camera.

BlockBuilder57 avatar Nov 15 '24 20:11 BlockBuilder57

Is there any chance we can log in and debug? We don't have this environment and hardware for testing.

slouken avatar Nov 15 '24 20:11 slouken

I'm a bit hesitant, but I'm happy to help. What do you need me to do, then?

BlockBuilder57 avatar Nov 15 '24 20:11 BlockBuilder57

If you reach out to me via e-mail at [email protected], we can schedule a Zoom live debugging session.

slouken avatar Nov 15 '24 22:11 slouken

An update, I just tried the same code with the same SDL version with my laptop's integrated camera, and it works! It's internally connected over USB, which theoretically should be the same behavior as the capture card - but of course, in this case it functions. It's possible that this problem may be more related to v4l2loopback devices? (Of which Droidcam, and as I've since tested the OBS Virtual Camera are.)

Oh, and I've sent you an email.

BlockBuilder57 avatar Nov 15 '24 23:11 BlockBuilder57

Ah, I don't know anything about v4l2loopback devices. Maybe @icculus knows more, or we can contact one of the authors for some help here?

slouken avatar Nov 16 '24 00:11 slouken

I don't know more at the moment, but we can probably add some logging to see why it opens the camera but never gets any frames.

icculus avatar Nov 16 '24 04:11 icculus

@BlockBuilder57 The simplest way to get extra logging is to set https://github.com/libsdl-org/SDL/blob/572cc7af634ba713b5523ff25cca71307f7b6c65/src/camera/SDL_syscamera.h#L28 to 1.

(assuming you guys don't already sat together)

Green-Sky avatar Nov 16 '24 09:11 Green-Sky

@BlockBuilder57, can you set that and see what output you get, both in the successful case and the unsuccessful one?

slouken avatar Nov 16 '24 15:11 slouken

Compiled from latest on both. desktop.log laptop.log

Seems like the desktop is never getting its camera permission approved? I'm using Plasma 6.2.3 as my DE, if that's relevant.

BlockBuilder57 avatar Nov 16 '24 21:11 BlockBuilder57

What happens if you change the camera code to get frames without waiting for approval?

slouken avatar Nov 16 '24 21:11 slouken

Commenting out these lines, you mean?

https://github.com/libsdl-org/SDL/blob/0f9e551d7127e9d244b25bf8ceef63422f77cfc2/src/camera/SDL_camera.c#L1223-L1227

It did not seem to help. As an experiment I also checked if the SurfaceList was getting populated, and it's not.

BlockBuilder57 avatar Nov 16 '24 21:11 BlockBuilder57

Seems like the desktop is never getting its camera permission approved

The v4l2 code does not have a concept of permission, so it approves everything immediately.

...is it possible the desktop is using the PipeWire backend and not v4l2?

icculus avatar Nov 17 '24 04:11 icculus

I use PipeWire on both the desktop and laptop, so as far as I know that shouldn't be the problem. I also just tried pw-v4l2 to see if it would start working, but to no avail. There could be a difference in the config, though?

BlockBuilder57 avatar Nov 17 '24 20:11 BlockBuilder57

(I'm asking this on all the open bugs about this.)

If you're still having this problem, try forcing the v4l2 backend:

SDL_CAMERA_DRIVER=v4l2 ./MyCameraProgram

It defaults to PipeWire (and, on my Ubuntu 24.04 system, this is what it chooses by default). I'm wondering if the actual V4L2 backend works better than the PipeWire one.

icculus avatar Jan 04 '25 08:01 icculus

It seems to work just fine! It likely does, and I would not be surprised if it was just a PipeWire issue. I'll rename this issue to better clarify.

BlockBuilder57 avatar Jan 04 '25 19:01 BlockBuilder57

I think (for the time being) we're just going to favor v4l2 over pipewire. My guess is this is causing all the camera problems.

icculus avatar Jan 04 '25 20:01 icculus

I think (for the time being) we're just going to favor v4l2 over pipewire. My guess is this is causing all the camera problems.

That sounds like a good idea.

slouken avatar Jan 04 '25 21:01 slouken

Bumping this from the milestone, since we're favoring v4l2 now.

icculus avatar Jan 04 '25 21:01 icculus