BenjaminEHowe
Posts: 8
Joined: Sun Apr 03, 2016 6:18 pm
Contact: Website

Pi Zero + Camera Board = USB webcam gadget?

Mon May 16, 2016 10:18 am

Given the Raspberry Pi zero now has connectivity to the camera board, is it possible to set the zero up as a USB webcam gadget?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8503
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Pi Zero + Camera Board = USB webcam gadget?

Mon May 16, 2016 10:22 am

Almost certainly not, unless someone has written a V4L2 to UVC (USB Video Class) driver.
(and don't get confused if you google - V4L2 already supports connecting to UVC clients, but you're after the other way around where the Zero IS the UVC client).

Looks like the work has been done for gadget mode - see below.
Last edited by 6by9 on Mon May 16, 2016 1:02 pm, edited 1 time in total.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

djazz
Posts: 25
Joined: Fri Feb 03, 2012 3:26 pm
Location: Sweden
Contact: Website

Re: Pi Zero + Camera Board = USB webcam gadget?

Mon May 16, 2016 12:45 pm

If you can make the camera appear as a video device on the pi (like an usb camera) you could try set up the usb gadget mode with UVC.

Have a look at the documentation for UVC gadget:
https://github.com/torvalds/linux/blob/ ... g.txt#L656

Here is a guide on how to set up the Pi Zero as a usb gadget:
http://isticktoit.net/?p=1383
I think you can skip the rpi-update part, as kernel 4.4 is now default in the latest Raspbian.
Also, when the Pi Zero is in gadget mode, it won't work as a host, meaning you can't have a keyboard connected, or usb hub. Use a serial cable connection to debug the pi.

You can make the zero into multiple devices at once, for example mine is ethernet and audio device (for internet and sound input/output to a PC). Adding UVC to the list shouldn't be too hard, but making the video stream actually work, not sure. I think the UVC gadget creates a video output device of some sort.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8503
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Pi Zero + Camera Board = USB webcam gadget?

Mon May 16, 2016 1:02 pm

Wow, someone really did do a good job with USB gadget support! I'm happy to be corrected on that one.

Sounds like it's worth a whirl with "sudo modprobe bcm2835-v4l2" first, and then work out the magic runes to enable webcam mode of the USB gadget driver, and it looks like it has a fighting chance of working as either a YUYV or MJPEG webcam.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

crener
Posts: 3
Joined: Wed Jun 22, 2016 10:02 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Fri Jun 24, 2016 12:41 pm

have you guys managed to make any progress?

ATM i'm stuck on even getting my PC to recognise the Pi as webcam. Found the documentation for the UVC usb setup here if it helps anyone progress.

joe60
Posts: 4
Joined: Sat Jun 11, 2016 11:09 am
Location: Somewhere in Northern Finland

Re: Pi Zero + Camera Board = USB webcam gadget?

Tue Jul 26, 2016 10:57 am

Two points of this. There are two ways to handle webcam gadget: old "legacy" way g_webcam and "new"way using configfs. Both of them are supported in the current Linux version.
Other point: webcam gadget needs (userspace) application to send a request to host side (PC).

One nasty way to test webcam gadget is used less linux command for example (there is an error message, but you can ignore it):

Code: Select all

less -f /dev/video0
Of course dwc2 and g_webcam modules should be installed...Connect USB cable between Zero and PC. In windows 7 PC you should see UVC Cameara by using command View devices and printers. In Ubuntu PC lsusb shows this:

Code: Select all

Bus 001 Device 006: ID 1d6b:0102 Linux Foundation EEM Gadget
Example how to use configfs system find here https://wiki.tizen.org/wiki/USB/Linux_U ... _webcam.ko.

There is also real (test) application for this http://git.ideasonboard.org/uvc-gadget.git. It's pretty old, but there are two patchsets in here http://www.spinics.net/lists/linux-usb/msg84376.html and here http://www.spinics.net/lists/linux-usb/msg99220.html. I have used it for testing (as well as g_webcam that configfs based webcam), but streaming/capture is yet only a dream. In PC side (ubuntu desktop) I have used this application from here http://git.ideasonboard.org/yavta.git.

jywarren
Posts: 1
Joined: Tue Apr 25, 2017 2:57 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Tue Apr 25, 2017 3:00 pm

Wow, this is such a great idea. Has anyone figured this out yet? Thanks for sharing!

raat1979
Posts: 2
Joined: Tue Nov 05, 2013 7:18 am

Re: Pi Zero + Camera Board = USB webcam gadget?

Sat Feb 09, 2019 7:36 am

After searching a lot I finally have this working now

to get it working I used the following script for the configfs setup
https://gist.github.com/kbingham/c39c4c ... f5206e2f9f

and this version of the uvc-gadget tool
https://github.com/wlhe/uvc-gadget

kbingham
Posts: 6
Joined: Wed Mar 06, 2019 11:20 am

Re: Pi Zero + Camera Board = USB webcam gadget?

Wed Mar 06, 2019 12:05 pm

raat1979 wrote:
Sat Feb 09, 2019 7:36 am
After searching a lot I finally have this working now

to get it working I used the following script for the configfs setup
https://gist.github.com/kbingham/c39c4c ... f5206e2f9f

and this version of the uvc-gadget tool
https://github.com/wlhe/uvc-gadget
The uvc-gadget.sh configfs script I created was designed to work with the upstream UVC-Gadget application at
http://git.ideasonboard.org/uvc-gadget.git

There are considerable changes in the upstream version in regards to configuring things with ConfigFS - and so it might be worth checking against that too.

Some day I hope to rewrite that uvc-gadget.sh helper script to make it more friendly and usable.

jumpy88
Posts: 3
Joined: Sun Mar 31, 2019 5:44 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Sun Mar 31, 2019 6:17 pm

Hello, I'm trying to make this work on my raspberry pi zero (no wifi and I'm working headless) at boot, but I can't figure out how to use uvc-gadget.sh script. I've checked for UVC driver being installed with

Code: Select all

modinfo uvcvideo
and this is the output https://pastebin.com/zruKbxD3
I don't understand if UVC-Gadget application is something else I've to install separately. Anyway I tried adding a crontab executing

Code: Select all

@reboot sudo /home/pi/bin/uvc-gadget.sh start >>/home/pi/bin/uvc-gadget.log
and the output is

Code: Select all

Detecting platform:
  board : Raspberry Pi Zero Rev 1.3
  udc   : 20980000.usb
Creating the USB gadget
Creating gadget directory g1
OK
Setting Vendor and Product ID's
OK
Setting English strings
OK
Creating Config
Creating functions...
	Creating UVC gadget functionality : uvc.0
OK
Binding USB Device Controller
but when I connect it to my ubuntu computer lsusb doesn't show anything useful and in dmesg output I find this error:

Code: Select all

usb 2-1.3.3: new high-speed USB device number 33 using ehci-pci
usb 2-1.3.3: device descriptor read/64, error -71
Any idea what could I be missing? How should my cmdline.txt and config.txt look like?

Thank you in advance

ali1234
Posts: 21
Joined: Tue Jan 05, 2016 1:31 am

Re: Pi Zero + Camera Board = USB webcam gadget?

Mon Jul 01, 2019 11:21 am

There are currently two things preventing this from fully working:

1. Raspbian kernel doesn't have CONFIG_USB_CONFIGFS which means the uvc-gadget.sh cannot work. You must use the legacy g_webcam instead, or build your own kernel.

2. Both the uvc-gadget C programs require that the source v4l2 device has the V4L2_CAP_VIDEO_CAPTURE (=0x00000001) capability but the v4l2 driver for RPi Camera (bcm2835-v4l2) does not have this capability:

Code: Select all

Driver Info:
	Driver name      : bcm2835-codec
	Card type        : bcm2835-codec
	Bus info         : platform:bcm2835-codec
	Driver version   : 4.19.50
	Capabilities     : 0x84208000
		Video Memory-to-Memory
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps      : 0x04208000
		Video Memory-to-Memory
		Streaming
		Extended Pix Format
The only other way to attach a camera to a Pi Zero is with the USB port, which precludes it from also acting as a webcam.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8503
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Pi Zero + Camera Board = USB webcam gadget?

Mon Jul 01, 2019 11:54 am

ali1234 wrote:
Mon Jul 01, 2019 11:21 am
There are currently two things preventing this from fully working:

1. Raspbian kernel doesn't have CONFIG_USB_CONFIGFS which means the uvc-gadget.sh cannot work. You must use the legacy g_webcam instead, or build your own kernel.
What buld are you using? On a Pi4.

Code: Select all

[email protected]:~ $ zcat /proc/config.gz | grep USB_CONFIGFS
CONFIG_USB_CONFIGFS=m
CONFIG_USB_CONFIGFS_SERIAL=y
CONFIG_USB_CONFIGFS_ACM=y
CONFIG_USB_CONFIGFS_OBEX=y
CONFIG_USB_CONFIGFS_NCM=y
CONFIG_USB_CONFIGFS_ECM=y
CONFIG_USB_CONFIGFS_ECM_SUBSET=y
CONFIG_USB_CONFIGFS_RNDIS=y
CONFIG_USB_CONFIGFS_EEM=y
CONFIG_USB_CONFIGFS_MASS_STORAGE=y
CONFIG_USB_CONFIGFS_F_LB_SS=y
CONFIG_USB_CONFIGFS_F_FS=y
CONFIG_USB_CONFIGFS_F_UAC1=y
# CONFIG_USB_CONFIGFS_F_UAC1_LEGACY is not set
CONFIG_USB_CONFIGFS_F_UAC2=y
CONFIG_USB_CONFIGFS_F_MIDI=y
CONFIG_USB_CONFIGFS_F_HID=y
CONFIG_USB_CONFIGFS_F_UVC=y
CONFIG_USB_CONFIGFS_F_PRINTER=y
Ah, unless this is a difference between bcm2711_defconfig and bcmrpi_defconfig. I'm fairly certain that it had been enabled, so it's probably been accidentally dropped in one of the kernel bumps.
ali1234 wrote:2. Both the uvc-gadget C programs require that the source v4l2 device has the V4L2_CAP_VIDEO_CAPTURE (=0x00000001) capability but the v4l2 driver for RPi Camera (bcm2835-v4l2) does not have this capability:

Code: Select all

Driver Info:
	Driver name      : bcm2835-codec
	Card type        : bcm2835-codec
	Bus info         : platform:bcm2835-codec
	Driver version   : 4.19.50
	Capabilities     : 0x84208000
		Video Memory-to-Memory
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps      : 0x04208000
		Video Memory-to-Memory
		Streaming
		Extended Pix Format
Do you want to reread that output?
"bcm2835-codec". That would be the codec M2M devices which register by default as /dev/video10-12, not the camera node which registers by default as /dev/video0. The camera device will only register if a camera is physically connected.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

ali1234
Posts: 21
Joined: Tue Jan 05, 2016 1:31 am

Re: Pi Zero + Camera Board = USB webcam gadget?

Tue Jul 02, 2019 7:25 pm

Yeah it is missing from bcmrpi_defconfig. Reported: https://github.com/raspberrypi/linux/issues/3042

I just tried this all on a Pi 4 with a USB webcam and it all seemed to work, but I soon as I tried to open the emulated UVC device the whole Pi froze and obviously I got no video from it.

ali1234
Posts: 21
Joined: Tue Jan 05, 2016 1:31 am

Re: Pi Zero + Camera Board = USB webcam gadget?

Wed Jul 03, 2019 3:30 am

I finally have this transmitting video. Here are the detailed steps to make it work:

0. Have Raspberry Pi running Buster, connected to a PC on USB cable on a data-enabled port.

1. Set "dtoverlay=dwc2" in /boot/config.txt

2. Connect rpi camera and enable it with raspi-config and then reboot. After reboot the bcm2835-v4l2 module should be loaded automatically and the device node (probably /dev/video0) has these properties:

Code: Select all

Driver Info:
	Driver name      : bm2835 mmal
	Card type        : mmal service 16.1
	Bus info         : platform:bcm2835-v4l2
	Driver version   : 4.19.50
	Capabilities     : 0x85200005
		Video Capture
		Video Overlay
		Read/Write
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps      : 0x05200005
		Video Capture
		Video Overlay
		Read/Write
		Streaming
		Extended Pix Format
3. "sudo modprobe g_webcam". This will create a new device node (probably /dev/video1) with these properties:

Code: Select all

Driver Info:
	Driver name      : g_uvc
	Card type        : fe980000.usb
	Bus info         : gadget
	Driver version   : 4.19.50
	Capabilities     : 0x84200002
		Video Output
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps      : 0x04200002
		Video Output
		Streaming
		Extended Pix Format
4. Clone https://github.com/wlhe/uvc-gadget and build it. Then if rpi cam is /dev/video0 and g_webcam is /dev/video1, run it with "sudo ./uvc-gadget -u /dev/video1 -v /dev/video0"

5. Open the newly created video device on PC and you should see the rpicam video.

Notes:

The upstream uvc-gadget from http://git.ideasonboard.org/uvc-gadget.git still fails with the following error:

Code: Select all

Error: driver returned invalid frame ival type 2
Error opening device /dev/video0: unable to enumerate formats.
Only the wlhe version currently works.

This is still using the legacy g_webcam driver and the uvc-gadget.sh script isn't needed.

The whole set up is extremely fragile: once you start the uvc-gadget code you can't stop it with ctrl-c. Killing it will crash the whole OS. Touching the device nodes while it is running will crash the whole OS.

However, it does work. :)

jumpy88
Posts: 3
Joined: Sun Mar 31, 2019 5:44 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Sat Nov 02, 2019 10:58 pm

Hello Ali, thank you for your post and sorry for my late reply. I've been away from this forum for a while. I tried to follow your instructions and everything seems to be fine, but I can't check because my PI Zero isn't wifi and I need USB port to type commands with keyboard or via ssh. Now I'm trying to make it plug and play. I've added:
  • modules-load=dwc2,g_webcam at the end of /boot/cmdline.txt
  • /home/pi/uvc-gadget/uvc-gadget -u /dev/video0 -v /dev/video1 >>/home/pi/webcam.log 2>&1 in /etc/rc.local
When I plug it into my computer it creates a /dev/video0 but vlc doesn't manage to load the stream. The log file webcam.log is empty. If I swap video0 and video1 in uvc-gadget command I find "V4L2: /dev/video0 is no video capture device" in webcam.log. Any idea what could be wrong?

jumpy88
Posts: 3
Joined: Sun Mar 31, 2019 5:44 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Sun Nov 03, 2019 1:46 am

Update: with cheese it works fine, but I can't find configuration to make it work in vlc.

edit: ok, I managed to make it work with vlc: I've to manually specify width and height. Now it remains only to make it work connected to a windows host.

longinus
Posts: 3
Joined: Sun Mar 22, 2020 9:49 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Sun Mar 22, 2020 10:04 pm

Hey folks,
Following some of the last posts in this thread, I aaalmost got it working... but all I see is a black image!

I've tried 3 different camera modules (that all work fine with raspi camera tools), and both a Pi Zero and Pi 4.
I see the UVC Camera device in Windows, and can open it with different software, BUT it's always a black image... The log on uvc-gadget seems to make sense (it starts/stops the stream when it should, has the correct format).
I'm using the g_webcam driver and the uvc-gadget from wlhe github.

But again, all I get is a black screen with the right proportions.

Did anybody run into this problem? What am I missing?
Thanks!

eToiPi
Posts: 2
Joined: Tue Mar 31, 2020 1:50 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Tue Mar 31, 2020 2:11 pm

Hi longinus, did you solve the black-screen problem? I see the same, using Pi Zero W running Buster with V2.1 raspi-cam. Camera works OK, tested with

Code: Select all

$ raspistill -o image1.jpg
I'll keep you posted of new developments. Pointers anyone?

eToiPi
Posts: 2
Joined: Tue Mar 31, 2020 1:50 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Tue Mar 31, 2020 3:32 pm

Hi folks,
I'm running into the same problems as described by Longinus. I'm trying to get a Pi Zero W running Buster and connected to a raspi-cam V2.1 (which works, as tested by $ raspistill -o image1.jpg) to act as a USB camera for a MacBook Air 2019 with faulty camera. I first followed https://gist.github.com/gbaman/50b6cca61dd1c3f88f41, and https://learn.adafruit.com/turning-your ... usb-gadget, but without any luck on the g_webcam side (although I didn't try g_serial or g_ether either; I wanted to turn my Pi into a USB webcam, not a serial or ethernet gadget). Of note: most sites out there that discuss using the Pi Zero as a Gadget stop after these two examples and claim something similar to this Adafruit quote:
Serial and Ethernet are the easiest to get going but they are far from the only gadgets the Linux kernel supports...
One desperate last search brought me to this thread, but now I'm stuck at exactly the same point as Longinus. Can anyone point us in the right direction?
Cheers!

longinus
Posts: 3
Joined: Sun Mar 22, 2020 9:49 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Wed Apr 01, 2020 6:04 pm

eToiPi wrote:
Tue Mar 31, 2020 2:11 pm
Hi longinus, did you solve the black-screen problem?

Unfortunately I never got it working as a USB webcam...

So what I did was to create a streaming MJPEG camera, using this software -- https://github.com/jacksonliam/mjpg-streamer
From this streaming camera I can create virtual cameras in Windows, and use it with video conferencing apps (works well with Zoom).

On a Raspberry Pi 3 I get to 1280x720 - 15fps, without saturating its network bandwidth. I haven't tried it on the Pi 4 yet, but probably should be able to go to 30fps.

I'm using a Windows driver called IP Cam Adapter, but I've also made it work with OBS Studio. And there is also one called Webcamoid that is cross-platform and seems to work.

geekinchief
Posts: 4
Joined: Thu Sep 06, 2018 3:01 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Wed Apr 08, 2020 2:20 pm

Thanks for this advice, longinus. I tried what you said and it works for me on a Pi 3, but on a Pi Zero W, mjpg_stream fails, no matter how low I drop the settings. Either it gives an error or the feed just shows black. Can you share what settings you used for the Pi Zero that worked for you?

longinus
Posts: 3
Joined: Sun Mar 22, 2020 9:49 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Fri Apr 17, 2020 2:29 pm

geekinchief wrote:
Wed Apr 08, 2020 2:20 pm
Can you share what settings you used for the Pi Zero that worked for you?
Oh sorry, I actually didn't try this streaming on the Pi Zero... Mostly because I wanted to use LAN network instead of wifi (to hopefully reduce the lag on the stream), so I've never tried this setup on the Zero.

steeley
Posts: 14
Joined: Fri Jun 22, 2012 3:00 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Sat Apr 25, 2020 7:25 pm

How to get the RPi camera working? I had no luck.

I have the same issue, shows up as camera source in Quicktime capture but shows a black screen. vlc shows a device too but won't open it.
uvc-gadget shows a connection being opened and closed. Spent hours messing with this on an RPI4....

However, when I disabled the RPI onboard camera and plugged in an old Logitech c270 It worked like this:

Code: Select all

sudo ./uvc-gadget -u /dev/video2 -v /dev/video0
on my MacBook:

Code: Select all

ffplay -f avfoundation -video_size 640x360 -framerate 10   -i "0" 
does show the cam video, but note the parameters sent from uvc-gadget are rather odd video settings:

Code: Select all

[avfoundation @ 0x7fa82f838200] Selected framerate (25.000000) is not supported by the device.
[avfoundation @ 0x7fa82f838200] Supported modes:
[avfoundation @ 0x7fa82f838200]   [email protected][15.000015 15.000015]fps
[avfoundation @ 0x7fa82f838200]   [email protected][10.000000 10.000000]fps
[avfoundation @ 0x7fa82f838200]   [email protected][2.000000 2.000000]fps
[avfoundation @ 0x7fa82f838200]   [email protected][2.000000 2.000000]fps
[avfoundation @ 0x7fa82f838200]   [email protected][15.000015 15.000015]fps
[avfoundation @ 0x7fa82f838200]   [email protected][10.000000 10.000000]fps
[avfoundation @ 0x7fa82f838200]   [email protected][2.000000 2.000000]fps
[avfoundation @ 0x7fa82f838200]   [email protected][2.000000 2.000000]fps

g.letourneur
Posts: 1
Joined: Mon Apr 27, 2020 5:11 pm

Re: Pi Zero + Camera Board = USB webcam gadget?

Mon Apr 27, 2020 5:23 pm

Just some advice about the g_webcam driver in linux on pi4 it seems to be bugged.... Try to use uvc with configFS it works perfectly with windows, mac and linux.

uvc.sh example:

#!/bin/bash
mkdir /sys/kernel/config/usb_gadget/pi4

echo 0x1d6b > /sys/kernel/config/usb_gadget/pi4/idVendor
echo 0x0104 > /sys/kernel/config/usb_gadget/pi4/idProduct
echo 0x0100 > /sys/kernel/config/usb_gadget/pi4/bcdDevice
echo 0x0200 > /sys/kernel/config/usb_gadget/pi4/bcdUSB

echo 0xEF > /sys/kernel/config/usb_gadget/pi4/bDeviceClass
echo 0x02 > /sys/kernel/config/usb_gadget/pi4/bDeviceSubClass
echo 0x01 > /sys/kernel/config/usb_gadget/pi4/bDeviceProtocol

mkdir /sys/kernel/config/usb_gadget/pi4/strings/0x409
echo 100000000d2386db > /sys/kernel/config/usb_gadget/pi4/strings/0x409/serialnumber
echo "Samsung" > /sys/kernel/config/usb_gadget/pi4/strings/0x409/manufacturer
echo "PI4 USB Device" > /sys/kernel/config/usb_gadget/pi4/strings/0x409/product
mkdir /sys/kernel/config/usb_gadget/pi4/configs/c.1
mkdir /sys/kernel/config/usb_gadget/pi4/configs/c.1/strings/0x409
echo 500 > /sys/kernel/config/usb_gadget/pi4/configs/c.1/MaxPower
echo "UVC" > /sys/kernel/config/usb_gadget/pi4/configs/c.1/strings/0x409/configuration

mkdir /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0
mkdir -p /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/control/header/h
ln -s /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/control/header/h /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/control/class/fs
mkdir -p /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/streaming/mjpeg/m/720p
cat <<EOF > /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/streaming/mjpeg/m/720p/dwFrameInterval
5000000
EOF
cat <<EOF > /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/streaming/mjpeg/m/720p/wWidth
1280
EOF
cat <<EOF > /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/streaming/mjpeg/m/720p/wHeight
720
EOF
cat <<EOF > /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/streaming/mjpeg/m/720p/dwMinBitRate
29491200
EOF
cat <<EOF > /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/streaming/mjpeg/m/720p/dwMaxBitRate
29491200
EOF
cat <<EOF > /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/streaming/mjpeg/m/720p/dwMaxVideoFrameBufferSize
1843200
EOF
mkdir /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/streaming/header/h
cd /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0/streaming/header/h
ln -s ../../mjpeg/m
cd ../../class/fs
ln -s ../../header/h
cd ../../class/hs
ln -s ../../header/h
cd ../../../../..

ln -s /sys/kernel/config/usb_gadget/pi4/functions/uvc.usb0 /sys/kernel/config/usb_gadget/pi4/configs/c.1/uvc.usb0
udevadm settle -t 5 || :
ls /sys/class/udc > /sys/kernel/config/usb_gadget/pi4/UDC


git clone https://github.com/wlhe/uvc-gadget

./uvc-gadget -u /dev/videoXX -v /dev/videoYY -r 1 -f 1

I hope it could help....

ikester
Posts: 4
Joined: Sun Apr 26, 2020 3:08 am

Re: Pi Zero + Camera Board = USB webcam gadget?

Mon Apr 27, 2020 7:57 pm

This has been a very helpful thread. But I'm getting stuck in some of the same places that other people have. I have a couple of questions.

I've been trying to create a video/webcam loopback device with a RaspberryPi Zero W. With the recent clampdown that Zoom and other video conferencing apps have done, people can no longer use virtual cameras as sources for those apps (only signed drivers are allowed).

So my idea is to use the PiZ as a loopback device, that receives video from the host and streams it back in as a UVC webcam. Since USB webcams are allowed by default, this should allow me to use OBS, SnapCam or any other software to create custom video feeds that come back in as a general webcam video. The host (and all video apps) shouldn't be able to tell the difference from a regular USB webcam.

So here are my issues:

First, I haven't been able to stream video from the Pi to the host (a Mac in my case). I have dwc2 loaded properly at boot. If I use the older g_webcam driver, I can run uvc-gadget -d (from a fork like the one from jdonald above) and start a streaming session that kind of works. The "webcam" shows on the Mac and I can selected as a video capture device. When I connect to it, I get a screen with 3 horizontal green-to-pink gradient bars that scroll down continuously. Since I'm not sure what the -d option generates, I don't know if that the actual video or some bad interpretation of it.
Screen Shot 2020-04-27 at 2.55.40 PM.png
Screen Shot 2020-04-27 at 2.55.40 PM.png (125.6 KiB) Viewed 5208 times


Now, if I use the configfs method instead to create the device, I can still run uvc-gadget -d and see the gadget show up on the Mac, but I never get any video to come up (not even the pink/green bars). I tried passing many combination of arguments to the uvc-gadget application, but I have not been able to get any video to show on the Mac.

In both cases, this is the output of the command:

Code: Select all

uvc device is 20980000.usb on bus gadget
uvc open succeeded, file descriptor = 3
control request (req 86 cs 02)
control request (req 82 cs 02)
control request (req 83 cs 02)
control request (req 84 cs 02)
control request (req 87 cs 02)
control request (req 81 cs 02)
control request (req 01 cs 02)
setting unknown control, length = 2
Control Request data phase (cs 02 entity 02)
control request (req 86 cs 02)
control request (req 82 cs 02)
control request (req 81 cs 02)
control request (req 83 cs 02)
control request (req 81 cs 02)
control request (req 84 cs 02)
control request (req 87 cs 02)
control request (req 81 cs 02)
control request (req 81 cs 02)
control request (req 86 cs 02)
control request (req 81 cs 02)
control request (req 86 cs 02)
control request (req 81 cs 02)
control request (req 86 cs 02)
control request (req 81 cs 02)
control request (req 86 cs 02)
control request (req 81 cs 02)
control request (req 86 cs 02)
control request (req 81 cs 02)
control request (req 86 cs 02)
streaming request (req 01 cs 01)
setting probe control, length = 26
streaming request (req 81 cs 01)
streaming request (req 01 cs 02)
setting commit control, length = 26
UVC: Setting format to: YUYV 1280x720
UVC: 2 buffers allocated.
UVC: Starting video stream.
UVC: Stopping video stream.
So my first question to you all is: has someone successfully streamed synthetic (not from a Pi cam) video from the RPi to the host using the USB gadget? If so, can someone recommend a configuration and command line that I can use to send either a static image or an mjpeg sequence to the Mac? I've tried uvc-gadget -i sample.jpg to no avail.

Second question, why would the g_webcam driver work that differently, compared to the configfs one? When using the configfs approach I get stuck like steeley and longinus, and I don't even get the gradient video pattern. Knowing that g_webcam has been deprecated in favor of configfs, I would like to use the newer method if I can make it work.

If I can make this work, I think I can figure out how to get video from the host to the Pi over the network (WiFi) and feed that in to the usb-gadget so be streamed back in.

Now to the next phase, instead of having to configure and use WiFi for connecting to the Pi, I would prefer to configure it to also work as a USB Ethernet adapter. That way, I don't even have to deal with the WiFi config and just have the whole thing work through a single USB port. I could then power the Pi, use the Ethernet adapter interface to stream the video out to the Pi and the webcam device to stream it back in. Basically, a plug & play video loopback device.

But here's the next problem, whenever I configure more than one gadget function, the uvc-gadget app stops working. It doesn't matter if I do it with the old g_webcam and g_ether using g_multi, or if I use the configfs approach to create multiple gadget functions (using libcomposite). As soon as something else gets bound, the webcam device doesn't work anymore.

For example, after configuring two gadget functions, running uvc-gadget -d gives this output instead:

Code: Select all

uvc device is 20980000.usb on bus gadget
uvc open succeeded, file descriptor = 3
Just the first two lines. The control request lines never follow and the Mac can no longer detect the webcam as a video input device.

So the third question is, has someone been able to configure a Pi to work as both, an USB Ethernet device AND a UVC webcam device? Is there an inherent kernel/driver/USB spec limitation that would prevent a composite device like that from existing?

Any suggestions or ideas welcome. If I can at least get the video working, the Ethernet part is a "nice to have", as I can fall back on the WiFi for sending the video out.

Return to “Graphics, sound and multimedia”