vladimirvivien / go4vl

A Go library for working with the Video for Linux API (V4L2).
MIT License
236 stars 42 forks source link

Simplecam example not running on Rasberry pi 4 with camera module 3 #46

Open florianbgt opened 1 year ago

florianbgt commented 1 year ago

Hi there,

Trying to run the simplecam example on a raspberry pi 4 with camera module 3 and I get the following error:

camera start: device: start stream loop: device: stream on: stream on: bad argument error

I did follow the step mentioned here and I am building the binaries from the PI itself https://medium.com/go4vl/build-a-wifi-camera-using-the-raspberry-pi-zero-w-a-camera-module-and-go-1d5fadfa7d76

Seem like a bug but feel free to close if it is not supported on this hardware.

Using go 1.20 and Raspberry os

Thanks in advance for any help!

florianbgt commented 1 year ago

Also tried with 1.19.3 (the go version mentioned in the link I posted above)

vladimirvivien commented 1 year ago

@florianbgt Thanks for pointing this out. I personally was never able to test on RPi 4 due to shortage of the device. But I will keep this open and hope to replicate your issue soon on an RPi 4 with the module 3.

What I would suggest is to ensure you have lates updates on the device itself.

florianbgt commented 1 year ago

I just double checked and everything is up to date on the device itself. I tried with a USB camera and it works just fine. Maybe something with the pi camera module 3?

vincent-vinf commented 1 year ago

Try to use the v4l2-ctl command to get the image. If the same error occurs, it may be a driver problem. Enable legacy camera stack via sudo raspi-config command.

vladimirvivien commented 1 year ago

Hello all @florianbgt and @vincent-vinf The software has not been tested with camera module 3 or 4. It does work with module 2 and high quality module. I will mark it as a bug so I can revisit and test with module 3.

garytong commented 1 year ago

Same issue with Pi Zero W, with camera module 3 aka v1.3.

It seems to not like the set/get FPS parameters. After removing that, it seems to die at C.VIDIOC_STREAMON over at https://github.com/vladimirvivien/go4vl/blob/main/v4l2/streaming.go#L153

Would love to help out but not sure where or how to start... any pointers?

vladimirvivien commented 1 year ago

Thanks for reporting @garytong . Jumping on the net to order new cam modules this weekend.

vladimirvivien commented 1 year ago

Would love to help out but not sure where or how to start... any pointers?

@garytong Until my module 3 arrives from adafruit, I won't be able to debug this. Judging by the location of the error, C.VIDIOC_STREAMON, most likely a driver configuration issue. It's possible that go4vl is sending ioctl commands that the driver does not support.

Below is a snippet of notes I keep/use when debugging ioctl v4l2 issues (which will be my first go-to once i get the module 3). I use strace on the device to see what calls are succeeding. If you are adventurous, you can start there. As a warning, if you have never looked at ioctl system calls, it can be intimidating.

Testing and debugging v4l2 calls

On device debugging can be useful to track down issues with ioctl calls using the strace command:

strace <options> <cli command to trace>

Example: strace v4l2-ctl

Using the v4l2-ctl command can be usedful to see the expected ioctl requests/reponses expected for a given functionality:

strace -e trace=ioctl v4l2-ctl --set-fmt-video pixelformat=1

Example: strace Go program

The strace command can be used with Go program to output traces of system calls made by a compiled Go program. This is useful to debug programs with ioctl calls:

go build -o webcam .
strace -o trace.log -e trace=ioctl  ./webcam
vladimirvivien commented 1 year ago

I found an Arducam (8 MP v2) camera module and testing it with the code base. It is displaying similar behavior where it is failing at VIDIOC_STREAMON ioctl message.

The following is the output of command strace -o trace.log -e trace=ioctl ./simplecam on a raspberry pi zero w

...
ioctl(3, VIDIOC_QUERYCAP, {driver="bm2835 mmal", card="mmal service 16.1", bus_info="platform:bcm2835-v4l2-0", version=KERNEL_VERSION(5, 15, 65), capabilities=V4L2_CAP_VIDEO_CAPTURE|V4L2_CAP_VIDEO_OVERLAY|V4L2_CAP_EXT_PIX_FORMAT|V4L2_CAP_READWRITE|V4L2_CAP_STREAMING|V4L2_CAP_DEVICE_CAPS, device_caps=V4L2_CAP_VIDEO_CAPTURE|V4L2_CAP_VIDEO_OVERLAY|V4L2_CAP_EXT_PIX_FORMAT|V4L2_CAP_READWRITE|V4L2_CAP_STREAMING}) = 0
ioctl(3, VIDIOC_CROPCAP, {type=V4L2_BUF_TYPE_VIDEO_CAPTURE}) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(3, VIDIOC_S_FMT, {type=V4L2_BUF_TYPE_VIDEO_CAPTURE, fmt.pix={width=640, height=480, pixelformat=v4l2_fourcc('M', 'J', 'P', 'G') /* V4L2_PIX_FMT_MJPEG */, field=V4L2_FIELD_ANY, bytesperline=0, sizeimage=0, colorspace=V4L2_COLORSPACE_DEFAULT}} => {fmt.pix={width=640, height=480, pixelformat=v4l2_fourcc('M', 'J', 'P', 'G') /* V4L2_PIX_FMT_MJPEG */, field=V4L2_FIELD_NONE, bytesperline=0, sizeimage=307200, colorspace=V4L2_COLORSPACE_SMPTE170M}}) = 0
ioctl(3, VIDIOC_G_PARM, {type=V4L2_BUF_TYPE_VIDEO_CAPTURE, parm.capture={capability=V4L2_CAP_TIMEPERFRAME, capturemode=0, timeperframe=1/30, extendedmode=0, readbuffers=1}}) = 0
ioctl(3, VIDIOC_REQBUFS, {type=V4L2_BUF_TYPE_VIDEO_CAPTURE, memory=V4L2_MEMORY_MMAP, count=2 => 2}) = 0
ioctl(3, VIDIOC_QUERYBUF_TIME32, 0x1095cbc) = 0
ioctl(3, VIDIOC_QUERYBUF_TIME32, 0x1095cbc) = 0
ioctl(3, VIDIOC_QBUF_TIME32, 0x1095d6c) = 0
ioctl(3, VIDIOC_QBUF_TIME32, 0x1095d6c) = 0
ioctl(3, VIDIOC_STREAMON, [V4L2_BUF_TYPE_VIDEO_CAPTURE]) = -1 EINVAL (Invalid argument)
...

There are two errors in this output. The first one is when the code attempts to set crop capability for the driver:

ioctl(3, VIDIOC_CROPCAP, {type=V4L2_BUF_TYPE_VIDEO_CAPTURE}) = -1 ENOTTY (Inappropriate ioctl for device)

This is ignored (i think).

The second error, is when the code attempt to start the video stream:

ioctl(3, VIDIOC_STREAMON, [V4L2_BUF_TYPE_VIDEO_CAPTURE]) = -1 EINVAL (Invalid argument)

This could be caused by a number of reasons, I will need to spend time with the camera to figure out.

Thanks for bringing this to my attention.

garytong commented 1 year ago

Thank you! Thank you for the pointers and dedication to this project. I’ll give it a shot later this week.