Gstreamer v4l2loopback hw mjpeg stream

I’m trying to convert my usb camera to an mjpeg stream with gstreamer v4l2sink.

My camera captures YUYV, NV12, and YU12

I can’t seem to get any pipeline to preroll for reason "streaming stopped, reason not-negotiated (-4)

avenc_mjpeg has no problem.
For example, gst-launch-1.0 videotestsrc ! ‘video/x-raw,width=1280,height=720,framerate=(fraction)30/1’ ! avenc_mjpeg ! v4l2sink device=/dev/video1
works fine.

Can I use nvjpegenc somehow?
something along the lines of:
gst-launch-1.0 videotestsrc ! ‘video/x-raw,width=1280,height=720,framerate=(fraction)30/1’ ! nvjpegenc ! ‘image/jpeg,width=1280,height=720,framerate=(fraction)30/1’ ! v4l2sink device=/dev/video1
is giving me errors.

I am using v4l2loopback from the repositories.
Any advice?

Hi,
There are related topics:
Jetpack 4.2.1 : building v4l2loopback fails - #5 by Honey_Patouceul
errror executing modules prepare - #2 by linuxdev
implementation of v4l2loopback for devkit onboard csi camera - #8 by _av

Please check and give it a try.

With fakesink it runs well:

$ gst-launch-1.0 videotestsrc ! ‘video/x-raw,width=1280,height=720,framerate=(fraction)30/1’ ! nvjpegenc ! ‘image/jpeg,width=1280,height=720,framerate=(fraction)30/1’ ! fakesink

So if v4l2loopback is installed properly, it should work fine.

Thank you for your response.

I have built v4l2loopback from source, and I’m still getting same error.

gst-launch-1.0 videotestsrc ! ‘video/x-raw,width=1280,height=720,framerate=(fraction)30/1’ ! nvjpegenc ! ‘image/jpeg,width=1280,height=720,framerate=(fraction)30/1’ ! v4l2sink device=/dev/video1
Setting pipeline to PAUSED …
Pipeline is PREROLLING …
ERROR: from element /GstPipeline:pipeline0/GstVideoTestSrc:videotestsrc0: Internal data stream error.
Additional debug info:
gstbasesrc.c(3055): gst_base_src_loop (): /GstPipeline:pipeline0/GstVideoTestSrc:videotestsrc0:
streaming stopped, reason not-negotiated (-4)
ERROR: pipeline doesn’t want to preroll.
Setting pipeline to NULL …
Freeing pipeline …

EDIT:
The build from source is giving me new problems. i.e. simple pipeline won’t work.

gst-launch-1.0 videotestsrc ! ‘video/x-raw, format=(string)I420, framerate=(fraction)30/1’ ! v4l2sink device=/dev/video1
Setting pipeline to PAUSED …
Pipeline is PREROLLING …
Pipeline is PREROLLED …
Setting pipeline to PLAYING …
New clock: GstSystemClock
ERROR: from element /GstPipeline:pipeline0/GstVideoTestSrc:videotestsrc0: Internal data stream error.
Additional debug info:
gstbasesrc.c(3055): gst_base_src_loop (): /GstPipeline:pipeline0/GstVideoTestSrc:videotestsrc0:
streaming stopped, reason error (-5)
Execution ended after 0:00:00.033488326
Setting pipeline to PAUSED …
Setting pipeline to READY …
Setting pipeline to NULL …
Freeing pipeline …

Maybe this could be of some use to somebody.
At this point, I’ve decided to go a different route.

v4l2loopback sink may only support a subset of jpeg encoding, especially colorspace and sof marker.

Beforehand, if running R32.6, the v4l2loopback-dkms packaged version 0.10.0 may not work properly for this case (seems 0.10.0 is working with R32.5.1, though).
You may have to build and install either 0.9.1 or 0.12.5 version.
Here is how to install 0.12.5 version in case you would not be familiar with this :

# log in as root
sudo su

# update apt sources list and install dkms package
apt update
apt install dkms

# get v4l2loopback sources
cd /usr/src
git clone https://github.com/umlaeute/v4l2loopback.git v4l2loopback

# Checkout tag 0.12.5
cd v4l2loopback
git checkout v0.12.5

# back to /usr/src, and create a symbolic as expected by dkms
cd ..
ln -s v4l2loopback v4l2loopback-0.12.5

# ready for dkms to build and install module
dkms build -m v4l2loopback/0.12.5
dkms install -m v4l2loopback/0.12.5
# now you should have freshly built module /lib/modules/4.9.253-tegra/updates/dkms/v4l2loopback.ko

# load v4l2loopback module
rmmod v4l2loopback
modprobe v4l2loopback && dmesg | tail -n 1

exit

Now you would use a pipeline similar to the following converting a ZED camera (video1) in 1344x376p30 into a JPEG v4l2loopback (video2), using nvjpegenc producing jpeg with sof-marker=4 and colorspace bt601 or bt709 and using multipart mux/demux to convert into colorspace 2:4:7:1 and sof-marker 0 as expected by v4l2sink:

gst-launch-1.0 v4l2src device=/dev/video1 ! video/x-raw,format=YUY2,width=1344,height=376,framerate=30/1 ! nvvidconv ! 'video/x-raw(memory:NVMM),format=I420' ! nvjpegenc ! multipartmux ! multipartdemux single-stream=1 ! 'image/jpeg, parsed=(boolean)true, width=(int)1344, height=(int)376, colorimetry=(string)2:4:7:1, framerate=(fraction)30/1,sof-marker=(int)0' ! v4l2sink device=/dev/video2 -v

You would adapt the resolution, framerate and input format for your usb camera.

Check result with :

gst-launch-1.0 -e v4l2src device=/dev/video2 ! jpegdec ! videoconvert ! xvimagesink

# or, using HW decoder
gst-launch-1.0 -e v4l2src device=/dev/video2 ! nvv4l2decoder mjpeg=1 ! nvvidconv ! xvimagesink
2 Likes

Thank you @Honey_Patouceul