28

I want to use the webcam on my laptop as video device on my pc to use it for video conferencing etc.

So I not just want to stream the video. I actually want to "stream" the entire device over network.

So, two computers, one streams its webcam, the other picks it up as video device.

Both computes run normal Ubuntu 18.04 however I will soon upgrade to 20.04

3
  • So, two computers, one streams its webcam, the other picks it up as video device, is that it? Apr 7, 2020 at 18:14
  • 1
    To start with, which version of Linux have you installed (Ubuntu server, Ubuntu desktop, Kubuntu, Lubuntu, Xubuntu, Ubuntu MATE, et al.) on the laptop and the desktop, and which release number for each? Different releases have different tools for us to recommend. Please click edit and add that vital information to your question so all the facts we need are in the question. Please don't use Add Comment, since that's our channel to you. All facts about your system should go in the Question with edit
    – K7AAY
    Apr 7, 2020 at 18:31
  • If you specify your needs with a little more detail, you would receive more focused help. In particular, if having the "remote" webcam exactly as if it were a local device is not possible. Apr 13, 2020 at 11:20

6 Answers 6

21
+500

Asuming both machines are connected in a network and the PC can reach the laptop's IP. Please follow these steps:

On the laptop (with the webcam)

Install v4l-utils and ffmpeg by running:

sudo apt install v4l-utils ffmpeg

Install matroska media container like so:

sudo apt install libmatroska6v5

Run the following command to open a netcat listener for the camera stream:

ffmpeg -i /dev/video0 -codec copy -f matroska - | nc -l 9999

On the PC

Install v4l-utils and ffmpeg by running:

sudo apt install v4l-utils ffmpeg

Install v4l2loopback and load the virtual camera:

sudo apt install v4l2loopback-dkms v4l2loopback-utils

sudo modprobe -r v4l2loopback

sudo depmod -a

sudo modprobe v4l2loopback exclusive_caps=1 card_label="MyLaptopCam:MyLaptopCam"

Test it:

  1. Stream the real camera on the laptop to the virtual camera on the PC ( change Laptop_IP to the IP of the laptop ):

    nc Laptop_IP 9999 | ffmpeg -i /dev/stdin -codec copy -f v4l2 /dev/video0
    
  2. Launch and play the virtual camera:

    ffplay /dev/video0
    
  3. Say cheese to your laptop's camera.

If you can see your face, then it's working.

Microphone?

To send over the microphone output as well from the laptop to the PC, please follow these steps:

On the laptop:

arecord -f cd -c 1 | nc -l 7777

On the pc:

sudo modprobe snd-aloop

arecord -l | grep -i loopback

The output will be something like this:

card 1: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
card 1: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]

we will use device 1 ( this is usually the virtual microphone ) on card 1 like this hw:1,1

  1. Then run this ( change Laptop_IP to the IP of the laptop ):

    nc Laptop_IP 7777 | aplay -f cd -D hw:1,1 
    
  2. Select the new microphone device in System Setting -> Sound -> Input.

Test the microphone and be advised there will be sound latency because no compression is used.

Notice

  • This has been tested and working flawlessly on two machines running up to date equal versions of Ubuntu 19.10.

If the test fails, then it is probably because you have other camera devices virtual or otherwise present in your PC's /dev directory.

To fix this, run ls /dev/video* and note how many are there. Then use the one with the greater number after it in the two commands in the test. This is done like so:

  1. Run sudo modprobe -r v4l2loopback

  2. Then run ls /dev/video* and note the existing cameras like /dev/video0 /dev/video1 /dev/video2 ... etc

  3. Then run:

    sudo modprobe v4l2loopback exclusive_caps=1 card_label="MyLaptopCam:MyLaptopCam"
    
  4. Then run again ls /dev/video* a new camera /dev/video3 will be added like so /dev/video0 /dev/video1 /dev/video2 /dev/video3

  5. Use the new camera in the last two commands instead of /dev/video0 like so:

    nc Laptop_IP 9999 | ffmpeg -i /dev/stdin -codec copy -f v4l2 /dev/video3
    
    ffplay /dev/video3
    

Troubleshooting

To test if the stream is going through if you are having issues with v4l2loopback, you can run on the PC:

nc Laptop_IP 9999 | mplayer -

or

nc Laptop_IP 9999 | vlc -

or

nc Laptop_IP 9999 | ffplay -

To use it with Google Hangouts:

Thanks to @janjaromirhorak as indicated in this comment quoted below.

There might be more ways to do this, but this was my method in Mozilla Firefox: Open Google Hangouts, start a videocall and allow both requests for microphone and internal webcam. Then click the cogwhell icon to open a settings dialog that allows you to choose which webcam you want to use. Select your virtual webcam, accept the permission request and click "done". Also this setting seems to persist to future videocalls - next time I found my virtual webcam already selected and working. :)

25
  • The streaming on the laptop seem to work. however i got "/dev/stdin: Invalid data found when processing input" error on the pc. so i tried to run it locally and it gave me this error: Stream mapping: Stream #0:0 -> #0:0 (h264 (native) -> rawvideo (native)) [v4l2 @ 0x561b1778b3c0] ioctl(VIDIOC_G_FMT): Invalid argument Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument Error initializing output stream 0:0 -- Conversion failed!
    – nichtNox
    Apr 15, 2020 at 9:33
  • @JohnDoe This means missing v4l2 format. Install it by running sudo apt install v4l-utils ffmpeg on both
    – Raffa
    Apr 15, 2020 at 10:08
  • no it is installed. has to be something else
    – nichtNox
    Apr 15, 2020 at 10:25
  • 1
    i have 0.10.0-1. and mplayer works. i am on ubuntu 18.04. So it should work on 20.04
    – nichtNox
    Apr 15, 2020 at 11:48
  • 1
    I know I didnt ask for it but could you do a bonus for the microphone if you know how to do that?
    – nichtNox
    Apr 16, 2020 at 12:28
4

So you want to borrow the camera for another machine. May be late but this question seems to have solution.

The usual issue I see, most conference/chat apps does only support local camera devices (no network or local file streaming).

Your are missing only one piece:

v4l2loopback

  1. Create a V4L2 loopback device in Desktop(PC2)
  2. In Laptop(PC1), use GStreamer(gst-launch-1.0), ffmpeg, vlc, ... to stream video from cam device to network
  3. In Desktop(PC2), use GStreamer(gst-launch-1.0), ffmpeg, vlc, ... to receive and pipe video to theloopback device

Then set conference/chat apps in Desktop(PC2) use the V4L2 loopback device created in step (1) as camera.

References:

5
  • 1
    I like your OS agnostic approach with gst-launch-1.0: I cannot get the network-streaming commands running... do you know of a gstreamer device I can use on a windows webcam, in your example PC1, to stream instead of device v4l2src? Thanks @user.dz
    – Leder
    Apr 16, 2020 at 18:40
  • 1
    @Leder ksvideosrc and you can also probe all available sources using gst-device-monitor-1.0 , it gives verbose info including cmd parameters to use with gst-launch-1.0 Reference: gstreamer.freedesktop.org/documentation/winks/…
    – user.dz
    Apr 16, 2020 at 19:40
  • 1
    I do not get it: I think I open up a new question, this is my gstreamer error: ERROR: from element /GstPipeline:pipeline0/GstKsVideoSrc:ksvideosrc0: Internal data stream error.
    – Leder
    Apr 17, 2020 at 3:49
  • 1
    @Leder I will check back your post askubuntu.com/q/1227835/26246 when I give this setup an experiment shot
    – user.dz
    Apr 17, 2020 at 8:48
  • I'll be back!!!
    – Leder
    Apr 17, 2020 at 20:48
3

If I understand correctly, you mean to have a video device on one computer getting the input from a physical device on another. Even if I do not have here the resources to test it, I guess it is possible. I remember some time ago doing something similar but not with video. According to this (also this; both old posts), video can also be done.

In summary

Solutions and comments in those posts use dd, ffmpeg or netcat (nc) to dump/stream video, and mplayer or vlc to see the tunneled video, and even piping with tee.

If you first try any of the solutions available in posts above, and report feedback, it would be a good starting point for moving ahead.

Creating a local device, to access it exactly as if the webcam were connected locally, was not tested. This (old post) suggests that using SSHFS might do the trick. YMMV, depending on what use you intend for the tunneled video.

References

  1. Piping video device over SSH or tcptunnel?

  2. Can I pipe /dev/video over ssh

  3. Watching remote webcam over piping and SSH?

  4. SSHFS

2

vlc

Using vlc is one way to achieve this goal.

Step 1 - setup

To install VLC on Debian, Ubuntu or Linux Mint:

$ sudo apt-get install vlc

To install VLC on Fedora, first enable RPM Fusion's free repository, then run:

$ sudo yum install vlc

To install VLC on CentOS or RHEL 6, first set up EPEL repository, and then use the following commands:

$ cd /etc/yum.repos.d/
$ sudo wget http://pkgrepo.linuxtech.net/el6/release/linuxtech.repo
$ sudo yum install vlc

Step 2 - Verify Webcam in VLC

Make sure your webcam is detected by your Linux system and VLC.

You’ll need to know the webcam’s name for that. In the example below, the webcam is named /dev/video0.

$ ls /dev/video*
/dev/video0

Then you have to test video from your webcam. Here is the command you need to use, do not forget to replace "video0" with the name of your device.

$ vlc v4l2:///dev/video0

If your webcam is successfully detected by VLC, you should be able to see your video stream.

Step 3 - Configure Webcam Streaming on VLC

You have successfully detected your webcam in VLC, next is configuring webcam streaming.

In this example webcam is streamed over HTTP in WMV format. To configure VLC for webcam streaming, first launch VLC.

$ vlc

In VLC menu choose "Streaming".

On the screen select your webcam’s or audio device’s name, e.g., /dev/video0 for webcam, and hw:0,0 for audio. Tick "Show more options" checkbox and make a note of value strings in "MRL" and "Edit Options" fields. These strings will be used later in the tutorial. Click "Stream" button.

Verify the video source, e.g., v4l2:///dev/video, and click "Next" to continue.

Choose the destination, i.e., streaming method/target, of webcam streaming. In our example we choose HTTP from the drop down list, and click "Add".

Next, specify port number and path of a streaming service. For port number, type 8080; we assume the port number is not occupied, for path - "/stream.wmv". For transcoding choose "Video - WMV + WMA (ASF)" profile from the drop down list. Click "Next".

The next screen displays automatically generated stream output string. Make a note of it and click "Stream" button.

At this point, VLC should start streaming video from your webcam over HTTP. Streaming traffic is sent directly to localhost at TCP port number 8080, so you won’t be able to see anything in the VLC window.

To verify that VLC is running correctly at TCP port 8080, run the following command, and look for VLC.

$ sudo netstat -nap | grep 8080

Step 4 - Watch Streaming Video from Webcam

Once a streaming server starts running, the webcam live feed is available at http://:8080/stream.wmv

You can use VLC player or MPlayer to access the webcam feed as follows.

$ vlc http://:8080/stream.wmv
$ mplayer http://:8080/stream.wmv

If you are testing the feed from the same host, use loopback address 127.0.0.1 instead.

2
  • thanks for your response. however this is not what i wanted. I want to register this stream as video device so I can use it in firefox for video conferencing
    – nichtNox
    Apr 11, 2020 at 19:03
  • @JohnDoe - Would you mind giving some more detail on how you mean to use this? A particular conferencing site? Please edit the OP, do not answer to comments. Apr 14, 2020 at 13:21
1

You could try to use DroidCam. I do not have PC with laptop in the one place, so I've just tested it on Android phone and Ubuntu 19.10. There's Wifi Server option present you could try in your case.

Installation for Ubuntu:

sudo apt install linux-headers-`uname -r`
sudo apt install v4l2loopback-dkms v4l2loopback-utils
sudo modprobe v4l2loopback
wget https://www.dev47apps.com/files/600/droidcam-64bit.tar.bz2 -O droidcam.tar.bz2
tar xjvf droidcam.tar.bz2
cd droidcam-64bit/
sudo ./install
echo -e '[Desktop Entry]\n Version=1.0\n Name=DroidCam\n Exec=droidcam\n Icon=droidcam\n Type=Application\n Categories=Application' | sudo tee /usr/share/applications/droidcam.desktop

You could run it from terminal: droidcam &

enter image description here

Allowing it in firewall:

sudo ufw allow 4747

enter image description here

For 20.04 this droidcam installation guide could not work. But I've found a different way described here.

0

My suggestion would be a little different, since all options are already given.

  1. Install TeamViewer and use it to connect to the laptop, the problem with this idea is that the laptop camera has to be pointed at you, even when you use the Desktop PC. This will work even if both computers are not on the same network. TeamViewer has a free(non-commercial) use and paid(commercial) use.

  2. Use the remote connection You can use the remote connection on your Dekstop computer with Ubuntu.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .