CatxFish / obs-virtual-cam

obs-studio plugin to simulate a directshow webcam
https://obsproject.com/forum/resources/obs-virtualcam.539/
GNU General Public License v2.0
1.75k stars 341 forks source link

Hello CatxFish... Any way to install OBS-virtual-cam on ubuntu? #17

Open pixel-one opened 6 years ago

pixel-one commented 6 years ago

Hello. Hope youre doing fine. I was reading your other post that you said obs works with genymotion. then i realized i can't even stream. I mean it was complicated. Then i found out that youre talking about obs-virtualcam but i have obs studio. are these 2 different softwares? Sorry im alittle noobish.
If not so can i install obs-virtual cam on linux? Or can i do teh same with on obs studio? I have obs studio but i dunno how to stream internal. i want to use it as genymotion camera . but obs studio wants to conenct to a server and wants server keys and such. so i cant start streaming even to test to see if genymotion detects teh cam or not. Could you please help me? Thanks alot

CatxFish commented 6 years ago

Hi , OBS-Virtualcam is a plugin of OBS-Studio on Windows, which means you have to install OBS-Studio first ,then install this project to get addition features , and it's only on windows. You can check in obs-studio plugins.

And since it is a lack of cross-platform solutions to simulate a device , the project dose not support linux or mac version now. But some simulators on Windows would transform the windows camera device to an inner camera ,like Genymotion.

I am sorry I can't provide a workable solution for you , but I do get an information about someone use v4l2loopback to do a virtual device trick on linux. I don't know how exactly this works, but maybe you can search in this way.

paulerickson commented 6 years ago

@pixel-one Here's a proof of concept using v4l2loopback on Ubuntu to get output from OBS into a virtual device. The performance is terrible, because of the fifo, and I was only able to detect the device in Firefox, not Chrome, due to some issue with "capabilities" metadata. For better performance, you might try setting up a local RTMP proxy server with nginx; that's the best I could come up with, unless it's possible to write directly to some virtual file created by v4l.

Cheers!

# Install packages
sudo apt install v4l-utils v4l2loopback v4l2loopback-utils ffmpeg obs-studio

# Create loopback device
sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1
# Note: Chrome doesn't detect this device; exclusive_caps is supposed to fix, but doesn't

# Create fifo to channel obs output to ffmpeg
mkfifo /tmp/pipe

# Open OBS, go to Settings->Output->Recording, set Type to "Custom Output (FFmpeg)", Container Format to "flv"; Apply and Start Recording
obs

# Send obs output to the virtual device
ffmpeg -re -f live_flv -i "/tmp/pipe" -f v4l2 /dev/video1
pixel-one commented 6 years ago

Thank you paulerickson . could you test it on Genymotion?
i get this error upon installing... Reading package lists... Done Building dependency tree
Reading state information... Done E: Unable to locate package v4l2loopback

paulerickson commented 6 years ago

Sorry, I don't have that.

I originally did this on an older Ubuntu, so I think you can use v4l2loopback-dkms now. Also, I realized you can write to UDP, so

sudo apt install v4l-utils v4l2loopback-utils obs-studio v4l2loopback-dkms
sudo rmmod v4l2loopback #if you need to reload it for some reason
sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1
ffmpeg -re -f live_flv -i udp://localhost:12345 -f v4l2 /dev/video1

And then in OBS "record to file" udp://localhost:12345 with flv. Other containers work too, some with worse latency, but I have had no luck with rawvideo.

pixel-one commented 6 years ago

Hello again, Thanks for teh help. I entered the commands and it stopped at third... Please have a look... Thanks again for helping, I appreciate this really none@none-Latitude:~$ sudo apt install v4l-utils v4l2loopback-utils obs-studio v4l2loopback-dkms [sudo] password for none: Reading package lists... Done Building dependency tree Reading state information... Done v4l2loopback-dkms is already the newest version. obs-studio is already the newest version. The following extra packages will be installed: libv4l2rds0 The following NEW packages will be installed: libv4l2rds0 v4l-utils v4l2loopback-utils 0 upgraded, 3 newly installed, 0 to remove and 17 not upgraded. Need to get 150 kB of archives. After this operation, 609 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libv4l2rds0 amd64 1.0.1-1 [15.9 kB] Get:2 http://archive.ubuntu.com/ubuntu/ trusty/universe v4l-utils amd64 1.0.1-1 [123 kB] Get:3 http://archive.ubuntu.com/ubuntu/ trusty/universe v4l2loopback-utils all 0.8.0-1 [11.0 kB] Fetched 150 kB in 5s (29.7 kB/s) Selecting previously unselected package libv4l2rds0:amd64. (Reading database ... 1514548 files and directories currently installed.) Preparing to unpack .../libv4l2rds0_1.0.1-1_amd64.deb ... Unpacking libv4l2rds0:amd64 (1.0.1-1) ... Selecting previously unselected package v4l-utils. Preparing to unpack .../v4l-utils_1.0.1-1_amd64.deb ... Unpacking v4l-utils (1.0.1-1) ... Selecting previously unselected package v4l2loopback-utils. Preparing to unpack .../v4l2loopback-utils_0.8.0-1_all.deb ... Unpacking v4l2loopback-utils (0.8.0-1) ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... Setting up libv4l2rds0:amd64 (1.0.1-1) ... Setting up v4l-utils (1.0.1-1) ... Setting up v4l2loopback-utils (0.8.0-1) ... Processing triggers for libc-bin (2.19-0ubuntu6.13) ... none@none-Latitude:~$ sudo rmmod v4l2loopback rmmod: ERROR: Module v4l2loopback is not currently loaded none@none-Latitude:~$ sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1 modprobe: FATAL: Module v4l2loopback not found. none@none-Latitude:~$ sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1 modprobe: FATAL: Module v4l2loopback not found. none@none-Latitude:~$ sudo rmmod v4l2loopback rmmod: ERROR: Module v4l2loopback is not currently loaded none@none-Latitude:~$

paulerickson commented 6 years ago

k, I didn't mean to confuse things — just skip the rmmod command, but that is how you would unload the module if you needed to recreate the device. If you can get this device to show up in Firefox (for instance, test at https://appr.tc/) but not in Genymotion, then I bet you'll need to figure out how those capability flags work (I have no clue).

pixel-one commented 6 years ago

Thanks again Paul. I loaded up OBS after running this command.. ffmpeg -re -f live_flv -i udp://localhost:12345 -f v4l2 /dev/video1 Then tried to follow your post. and i dont understand this part .... >>>>

And then in OBS "record to file" udp://localhost:12345 with flv. Other containers work too, some with worse latency, but I have had no luck with rawvideo.

<<<< Could you please explain again what do i do after i open OBS. i dont see record to file" in obs recording settong. .
Also i just entered your secong set of commands. I didnt do this one ..

mkfifo /tmp/pipe

. Could you please once more type all the steps in one post? I got confused which post to follow. Thanks much

paulerickson commented 6 years ago

Forget about the fifo. You already did

sudo apt install v4l-utils v4l2loopback-utils obs-studio v4l2loopback-dkms
sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1
ffmpeg -re -f live_flv -i udp://localhost:12345 -f v4l2 /dev/video1

So now go into OBS and click Settings. Click on the Output section, and then choose Advanced from the dropdown. Go to the Recording tab, change the Type to "Custom Output (ffmpeg)", and the FFmpeg Output Type to "Save to URL". In the "File path or URL" input, type "udp://localhost:12345" and choose "flv" from the Container format dropdown. Hit OK and then from the main screen try "Start Recording" (not "Start Streaming").

pixel-one commented 6 years ago

Thank you again for your time. Here is 2 problem i get... First when i enter the command""" sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1 """" I get modprobe: FATAL: Module v4l2loopback not found. And i ignore it so i contonue doing the next command and then open the OBS and i do the changes in setting too then when i click on start recording OBS hangs and closes fast... Here is teh logs in the terminal... ( Because i should open obs with this command " LIBGL_ALWAYS_SOFTWARE=1 obs"" otherwise it wont open. so anyway...
I get this error when ops crashes..
none@none-Latitude:~$ LIBGL_ALWAYS_SOFTWARE=1 obs Attempted path: share/obs/obs-studio/locale/en-US.ini Attempted path: /usr/share/obs/obs-studio/locale/en-US.ini Attempted path: share/obs/obs-studio/locale.ini Attempted path: /usr/share/obs/obs-studio/locale.ini Attempted path: share/obs/obs-studio/themes/Default.qss Attempted path: /usr/share/obs/obs-studio/themes/Default.qss Attempted path: share/obs/obs-studio/license/gplv2.txt Attempted path: /usr/share/obs/obs-studio/license/gplv2.txt info: CPU Name: Intel(R) Core(TM) i5 CPU M 560 @ 2.67GHz info: CPU Speed: 1599.000MHz info: Physical Cores: 2, Logical Cores: 4 info: Physical Memory: 7781MB Total, 357MB Free info: Kernel Version: Linux 4.4.0-98-generic info: Distribution: "Ubuntu" "14.04" info: Portable mode: false QMetaObject::connectSlotsByName: No matching signal for on_advAudioProps_clicked() QMetaObject::connectSlotsByName: No matching signal for on_advAudioProps_destroyed() info: OBS 20.1.0 (linux) info: --------------------------------- info: --------------------------------- info: audio settings reset: samples per sec: 44100 speakers: 2 info: --------------------------------- info: Initializing OpenGL... info: OpenGL version: 3.3 (Core Profile) Mesa 11.2.0 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 info: --------------------------------- info: video settings reset: base resolution: 680x420 output resolution: 680x420 downscale filter: Bicubic fps: 30/1 format: NV12 info: Audio monitoring device: name: Default id: default info: --------------------------------- libDeckLinkAPI.so: cannot open shared object file: No such file or directory info: No blackmagic support error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed info: VLC found, VLC video source enabled info: --------------------------------- info: Loaded Modules: info: vlc-video.so info: text-freetype2.so info: rtmp-services.so info: obs-x264.so info: obs-transitions.so info: obs-outputs.so info: obs-libfdk.so info: obs-filters.so info: obs-ffmpeg.so info: linux-v4l2.so info: linux-pulseaudio.so info: linux-jack.so info: linux-decklink.so info: linux-capture.so info: linux-alsa.so info: image-source.so info: frontend-tools.so info: --------------------------------- info: ==== Startup complete =============================================== info: All scene data cleared info: ------------------------------------------------ error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed info: pulse-input: Server name: 'pulseaudio 4.0' error: pulse-input: An error occurred while getting the source info! info: pulse-input: Server name: 'pulseaudio 4.0' error: pulse-input: An error occurred while getting the source info! info: [Media Source 'Media Source']: settings: input: /home/none/Videos/WPS Pixie Dust Attack Manual RalinkBroadcom (OLDER).mp4 input_format: (null) is_looping: no is_hw_decoding: yes is_clear_on_media_end: yes restart_on_activate: yes close_when_inactive: no info: Switched to scene 'Scene' info: ------------------------------------------------ info: Loaded scenes: info: - scene 'Scene': info: - source: 'Media Source' (ffmpeg_source) info: - scene 'Scene 2': info: ------------------------------------------------ info: adding 92 milliseconds of audio buffering, total audio buffering is now 92 milliseconds error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed info: ==== Recording Start =============================================== Segmentation fault (core dumped) none@none-Latitude:~$

MikkoMMM commented 6 years ago

Since I couldn't get it to work with the webcam, I also tried it without setting up my webcam to make things simpler.

I have File path or URL set to "udp://localhost:12345" and Container Format as flv. I do these steps:

[mikko@localhost lvdata]$ sudo rmmod v4l2loopback
[mikko@localhost lvdata]$ sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1
[mikko@localhost lvdata]$ ffmpeg -re -f live_flv -i udp://localhost:12345 -f v4l2 /dev/video0
ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.3.0 (GCC)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-avisynth --enable-avresample --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-shared --enable-version3 --enable-omx
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100

Then I start recording in OBS.

However, none of ffplay, Cheese or Chromium seem to recognize the stream.

[mikko@localhost lvdata]$ ffplay /dev/video0
ffplay version 3.4.2 Copyright (c) 2003-2018 the FFmpeg developers
  built with gcc 7.3.0 (GCC)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-avisynth --enable-avresample --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-shared --enable-version3 --enable-omx
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
[video4linux2,v4l2 @ 0x7f0144000b80] Not a video capture device.
/dev/video0: No such device
    nan    :  0.000 fd=   0 aq=    0KB vq=    0KB sq=    0B f=0/0

Have you got any ideas on this?

notfood commented 6 years ago

This solution used to work but v4l2 updated and now I'm getting this error


[v4l2 @ 0x55d4d1400fc0] 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!```

Any ideas?
notfood commented 6 years ago

It was due to this issue. Ignore me.

https://github.com/umlaeute/v4l2loopback/issues/172

CatxFish commented 5 years ago

I make a experimental repository for this feature.

grigio commented 5 years ago

I get this crash

Stream mapping:
  Stream #0:1 -> #0:0 (flv1 (flv) -> rawvideo (native))
Press [q] to stop, [?] for help
[v4l2 @ 0x563fbb7b3cc0] 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 --
tsankuanglee commented 5 years ago

@CatxFish Is it possible to make use of https://github.com/webcamoid/akvcam , which is used as webcamoid's virtual webcam?

CatxFish commented 5 years ago

@tsankuanglee Maybe it will work if it implement all the v4l2 standard the output plugin need. I really don't know, why wouldn't you try yourself and share your result?

tsankuanglee commented 5 years ago

Reporting back about akvcam I mentioned above:

However, I can't get the latency below 5 seconds in either method.

@CatxFish, the reason I mentioned akvcam is that webcamoid has very low latency, so I wonder whether there's a way to do the same in your obs-v4l2sink can. I wish I knew enough about this to contribute.

tsankuanglee commented 5 years ago

OK, I made some progress and got the latency down to less than 0.5 sec. Quite practical for real-time streaming. Steps:

  1. Create a V4L2 virtual cam with akvcam. Let's say it's /dev/video2 (for capturing). (I used udev rule to make it /dev/video0 so it's the first choice for software that only supports the first device, but we don't need to do this here.)

  2. In OBS Settings -> output -> Recording, set type to Custom Output (FFmpeg), Output to URL, and use file:///dev/video2 as the URL. Container set to rawvideo (Video), and the codec also set to rawvideo.

Since akvcam works fine with this line, ffmpeg -i video.webm -s 640x480 -r 30 -f v4l2 -vcodec rawvideo -pix_fmt rgb24 /dev/video2

I added video_size=640x480 framerate=30 pixel_format=rgb24 as the Video Encoder Settings. (according to ffmpeg's format doc).

Audio should automatically be disabled.

(Side note: Some times rawvideo (Video) doesn't show up in Container Format drop down. I have no idea why. My trial and error workaround: choose Null as the container, and rawvideo as the encoder. Apply. Quit OBS. Re-open OBS. Now it's in the menu. A bug?)

Now the latency is low, but the resolution is wrong. There are 3x2 of my output in the receiving end. Like this: 3x2 output

I feel we are closer. Hopefully this is the right direction, and somebody can figure this out.

josephsamela commented 5 years ago

I don't like changing the recording settings because then I can't stream while recording to disk simultaneously. Here's a solution that allows both.

First install the software.

sudo apt install v4l-utils v4l2loopback-utils obs-studio v4l2loopback-dkms

Create a loopback video device - for example loopback 1.

sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1

Start ffmpeg as an rtmp server passing video to the loopback 1 video device you just created.

ffmpeg -f flv -listen 1 -i rtmp://localhost:1935/live/test -f v4l2 -vcodec rawvideo /dev/video1

Finally, configure obs to stream to the rtmp server you just created.

image

Now loopback 1 shows up as a video device. Here's it working in discord - there's ~2sec latency.

screen

tsankuanglee commented 5 years ago

@JosephSamela Good job!

I pursued the akvcam route because that gave me a <0.5s latency, which doesn't create too big a problem for audio-video syncing, which is essential for video conferencing. However, I got stuck at that the 6 screen problem mentioned above.

Did you figure out a way to deal with the audio sync problem with this method?

I do have a simple workaround under Linux that works for me in video conferencing, although it's resource heavy. http://blog.tklee.org/2019/04/streaming-custom-videos-in-google.html

xxRockOnxx commented 5 years ago

Successfully managed to use OBS stream as a webcam source on Linux (Arch to be specific).

First, add another device using modprobe:

sudo modprobe v4l2loopback devices=1 video_nr=1 card_label="RTMP server" exclusive_caps=1

To understand what the parameters are, just see the original documentation.

I put video_nr=1 because /dev/video0 is already used by Droidcam which allows an android device to be used as a webcam. exclusive_caps "seems" to be required also to be detected by browsers

Second, start an RTMP server using ffmpeg forwarding the output to /dev/video1

ffmpeg -listen 1 -i rtmp://127.0.0.1:1935/live -f v4l2 -vcodec rawvideo /dev/video1

Note: I tried @JosephSamela's answer first and was confused why it was not working. For some reason OBS can't find localhost but can access 127.0.0.1

Third, use the created RTMP server (rtmp://127.0.0.1:1935/live) and start streaming on your OBS..

If your application does not find the new virtual device, try restarting it.

shalkam commented 5 years ago

@JosephSamela Your solution is working perfectly, but the image is flipped horizontally

shalkam commented 5 years ago

fixed by adding -vf hflip

ffmpeg -f flv -listen 1 -i rtmp://localhost:1935/live/test -vf hflip -f v4l2 -vcodec rawvideo /dev/video0
KishCom commented 4 years ago

Thanks to the excellent work of @JosephSamela I was able to replicate his setup and get latency down to around 1 second.

ffmpeg -an -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://localhost:1935/live/test -f v4l2 -vcodec rawvideo /dev/video2

I believe the issue is that FFMPEG does a bunch of stuff after the stream starts but before it starts to push out to /dev/videoX -- part of which is it analyzes the stream to figure out how it's encoded. I decreased the amount of time it tries to do that (-probesize 32 -analyzeduration 0), but ideally: how can this probe be disabled? We know the incoming stream. Secondly, disabling audio altogether (-an). For our purposes we only care about video.

nweldev commented 4 years ago

After having tried a lot of different approaches, I never managed to get latency under ~1s... And I think it's impossible, due to some limitations with ffmpeg.

But, latency around 1s isn't really a big issue by itself for video conferencing (for me), as we often see people with bigger network latency. It's problematic only because it makes the sound out of sync with the video. So, why not also use the sound from OBS Studio!

Here is the alias I use for that:

alias obscam="sudo modprobe v4l2loopback devices=1 video_nr=10 card_label=\"OBS Cam\" exclusive_caps=1 \
    && sudo modprobe snd-aloop index=10 id=\"OBS Mic\" \
    && pacmd 'update-source-proplist alsa_input.platform-snd_aloop.0.analog-stereo device.description=\"OBS Mic\" ' \
    && ffmpeg -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://127.0.0.1:1935/live/test -map 0:1 -f v4l2 -vcodec rawvideo /dev/video10 -map 0:0 -f alsa hw:10,1"

And here is the full step-by-step (on Arch Linux):

0. install required packages

sudo pacman -S obs-studio v4l-utils
git clone https://aur.archlinux.org/v4l2loopback-dkms.git
cd v4l2loopback-dkms
makepkg -scCi

1. add a virtual webcam

sudo modprobe v4l2loopback devices=1 video_nr=10 card_label="OBS Cam" exclusive_caps=1

:point_right: https://github.com/CatxFish/obs-virtual-cam/issues/17#issuecomment-490866572 for parameters details - I use video_nr=10 in order to always avoid conflict

2. add a virtual audio card

sudo modprobe snd-aloop index=10 id="OBS Mic"

Here, again, I use index=10 in order to avoid any conflict with existing sound cards, and id="OBS Mic" in order to identify it more easily in VC softs & pavucontrol.

:point_right: ArchLinux doc :point_right: Module-aloop doc

3. rename your new mic

By default, the new sound card is named "Built-in Audio Analog Stereo" is pulse audio, which makes it complicated to differentiate it from your real build-in sound card in pavucontrol, chrome, or any VC software.

pacmd 'update-source-proplist alsa_input.platform-snd_aloop.0.analog-stereo device.description=\"OBS Mic\" '

:point_right: https://unix.stackexchange.com/questions/316067/change-what-pulseaudio-calls-a-device

4. Configure OBS

:question: I'm new to OBS, so if any of you think some other settings would permit to reduce latency...

  1. set streaming server

:warning: same thing than https://github.com/CatxFish/obs-virtual-cam/issues/17#issuecomment-490866572: use 127.0.0.1, not localhost

image

  1. streaming output settings

image

  1. enable your mic

image

5. test streaming

:question: any better solution for that? I don't know how to use ffprobe for this kind of use case.

  1. run a "test" ffmpeg server
ffmpeg -an -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://127.0.0.1:1935/live/test -f v4l2 -vcodec rawvideo /dev/video10
  1. start streaming in OBS Studio

image

  1. look at ffmpeg output and identify which streams are used by audio & video
Stream #0:0: Audio: aac, 44100 Hz, stereo, 163 kb/s
Stream #0:1: Video: h264 (Constrained Baseline), yuv420p, 1280x720, 2560 kb/s, 30 fps, 30 tbr, 1k tbn, 60 tbc
  1. stop the ffmpeg server & stop streaming in OBS Studio

6. run the (real) ffmpeg server

ffmpeg -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://127.0.0.1:1935/live/test -map 0:1 -f v4l2 -vcodec rawvideo /dev/video10 -map 0:0 -f alsa hw:10,1

-map permits to differenciate audio and video streams.

:point_right: ffmpeg advanced options :point_right: ffmpeg wiki Map page

7. start streaming in OBS

8. sync audio & video

:information_source: FYI, I'm also using IP Webcam with my Galaxy S8 here, which add ~400ms delay. So, the real delay to consider here should be the one between your mic and your webcam. For me, it's around ~200ms but you can assume it's near 0 for you. There is a good chance you don't need this step if you only need to use OBS as a webcam + mic.

Do some tests (for example using https://webcamera.io and clapping).

A. sound ahead of video

Change your mic sync offset accordingly: image

Advanced Audio Properties

image

B. video ahead of sound

  1. right-click on the video source > filters
  2. click on + > select Video Delay (async)
  3. set your delay value

image

MikkoMMM commented 4 years ago

@noelmace The video stream works fine, but I'm having trouble getting desktop audio to play in my stream in addition to microphone audio after following your steps and making a script for ones that need to be redone:

#!/bin/bash -u

sudo modprobe snd-aloop index=9 id="OBS Mic"
pacmd update-source-proplist alsa_input.platform-snd_aloop.0.analog-stereo device.description=\"OBS Mic\"
ffmpeg -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://127.0.0.1:1935/live -map 0:1 -f v4l2 -vcodec rawvideo /dev/video9 -map 0:0 -f alsa hw:9,1

Under Advanced Audio Properties I've put both sources to output only to track one. I've also tried having the tracks with Monitor and Output. The desktop audio should be unmuted as well, and I do see the bars in Audio Mixer fluctuating when there's desktop audio.

Do you have any ideas? Also, my webcam image comes from a DSLR's live view with another script, but I'm doubting it has anything to do with this.

MikkoMMM commented 4 years ago

Hmm, wait. The desktop audio does seem to work now in the stream. Weird, this thing. Well, if the issue returns maybe I'll get better understanding of what is causing it.

hjacobs commented 4 years ago

After trying suggestions in this thread (ffmpeg etc), I found that the "v4l2-sink" plugin works flawlessly (~0 latency) on Ubuntu 19.10 for my use case (Google Meet). Here the blog post about my setup: https://srcco.de/posts/using-obs-studio-with-v4l2-for-google-hangouts-meet.html

MikkoMMM commented 4 years ago

@hjacobs Seems to work on Arch Linux as well, though it doesn't magically resolve the latency of (I believe) getting the image from my DSLR's live view to the computer, so it remained at around 300 milliseconds which it was also with the ffmpeg method. And I still needed to use ffmpeg for rerouting the audio output from OBS to a new virtual device.

metareven commented 4 years ago

Tried to do exactly as @noelmace suggested but I just get this error message: [v4l2 @ 0x55b6349529c0] 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 --

Does anyone have any suggestions as to what might be wrong?

I also tried @hjacobs suggestion, but it fails with the error message "format not support" when I try to set up V4l2sinkProperties

Cere-dev commented 4 years ago

After trying suggestions in this thread (ffmpeg etc), I found that the "v4l2-sink" plugin works flawlessly (~0 latency) on Ubuntu 19.10 for my use case (Google Meet). Here the blog post about my setup: https://srcco.de/posts/using-obs-studio-with-v4l2-for-google-hangouts-meet.html

Hi. I have issues with this. when I try to select a path it says device open failed.

Edit: now it works, but which URL should we use???

Edit2: nvm. everything works fine! Thank you!

drmichaeljgruber commented 4 years ago

After trying suggestions in this thread (ffmpeg etc), I found that the "v4l2-sink" plugin works flawlessly (~0 latency) on Ubuntu 19.10 for my use case (Google Meet). Here the blog post about my setup: https://srcco.de/posts/using-obs-studio-with-v4l2-for-google-hangouts-meet.html

That made me try the same solution, and it works great so far, thanks! The fact that video conferencing software (Hangouts for you, Pexip/DFNconf for me) show the stream flipped in the monitoring window and OK for the receiver, is on purpose, I guess: You see yourself in a "mirror" (anything else would feel strange), others see you in a "window" (anything else would look strange).

raphaelh commented 4 years ago

After trying suggestions in this thread (ffmpeg etc), I found that the "v4l2-sink" plugin works flawlessly (~0 latency) on Ubuntu 19.10 for my use case (Google Meet). Here the blog post about my setup: https://srcco.de/posts/using-obs-studio-with-v4l2-for-google-hangouts-meet.html

Thanks for your blog post.

After the command: cmake -DLIBOBS_INCLUDE_DIR="../../obs-studio/libobs" -DCMAKE_INSTALL_PREFIX=/usr ..

I've got the following error:

...
-- Could NOT find Libobs (missing: LIBOBS_LIB) 
CMake Error at external/FindLibObs.cmake:106 (message):
  Could not find the libobs library
Call Stack (most recent call first):
  CMakeLists.txt:5 (include)

I'm on Ubuntu 19.10 also...

dz0ny commented 4 years ago

After trying suggestions in this thread (ffmpeg etc), I found that the "v4l2-sink" plugin works flawlessly (~0 latency) on Ubuntu 19.10 for my use case (Google Meet). Here the blog post about my setup: https://srcco.de/posts/using-obs-studio-with-v4l2-for-google-hangouts-meet.html

Thanks for your blog post.

After the command: cmake -DLIBOBS_INCLUDE_DIR="../../obs-studio/libobs" -DCMAKE_INSTALL_PREFIX=/usr ..

I've got the following error:

...
-- Could NOT find Libobs (missing: LIBOBS_LIB) 
CMake Error at external/FindLibObs.cmake:106 (message):
  Could not find the libobs library
Call Stack (most recent call first):
  CMakeLists.txt:5 (include)

I'm on Ubuntu 19.10 also...

Do:

  sudo apt install obs-studio
  sudo apt build-dep obs-studio
  sudo apt install libobs-dev
  git clone https://github.com/CatxFish/obs-v4l2sink.git
  cd obs-v4l2sink
  mkdir build && cd build
  cmake -DCMAKE_INSTALL_PREFIX=/usr ..
  make -j4
  sudo make install
  sudo cp /usr/lib/obs-plugins/v4l2sink.so /usr/lib/x86_64-linux-gnu/obs-plugins/
raphaelh commented 4 years ago

Do:

  sudo apt install obs-studio
  sudo apt build-dep obs-studio
  sudo apt install libobs-dev
  git clone https://github.com/CatxFish/obs-v4l2sink.git
  cd obs-v4l2sink
  mkdir build && cd build
  cmake -DCMAKE_INSTALL_PREFIX=/usr ..
  make -j4
  sudo make install
  sudo cp /usr/lib/obs-plugins/v4l2sink.so /usr/lib/x86_64-linux-gnu/obs-plugins/

Thanks for your help.

I get the following error now:

CMake Error at external/FindLibObs.cmake:106 (message):
  Could not find the libobs library
Call Stack (most recent call first):
  CMakeLists.txt:5 (include)

If I do

sudo dpkg -L libobs-dev | grep obs.h$
/usr/include/obs/obs.h

and

sudo dpkg -L libobs-dev | grep libobs.so$
/usr/lib/x86_64-linux-gnu/libobs.so
raphaelh commented 4 years ago

And if I try cmake -DCMAKE_INSTALL_PREFIX=/usr -DLIBOBS_INCLUDE_DIR="/usr/include/obs/" -DLIBOBS_LIB="/usr/lib/x86_64-linux-gnu/" ..

-- Found Libobs: /usr/lib/x86_64-linux-gnu  
CMake Error at external/FindLibObs.cmake:98 (include):
  include could not find load file:

    /usr/include/obs/../cmake/external/ObsPluginHelpers.cmake
Call Stack (most recent call first):
  CMakeLists.txt:5 (include)
drmichaeljgruber commented 4 years ago

It seems the build system does not quite adjust to the different paths on different distributions (and may different obs-devel versions). On Fedora 31 I had to do the following, which might help some folks: change external/FindLibObs.cmake as follows (look at the changed include):

diff --git i/external/FindLibObs.cmake w/external/FindLibObs.cmake
index ab0a3de..4fac4aa 100644
--- i/external/FindLibObs.cmake
+++ w/external/FindLibObs.cmake
@@ -95,7 +95,7 @@ if(LIBOBS_FOUND)

        set(LIBOBS_INCLUDE_DIRS ${LIBOBS_INCLUDE_DIR} ${W32_PTHREADS_INCLUDE_DIR})
        set(LIBOBS_LIBRARIES ${LIBOBS_LIB} ${W32_PTHREADS_LIB})
-       include(${LIBOBS_INCLUDE_DIR}/../cmake/external/ObsPluginHelpers.cmake)
+       include(${LIBOBS_INCLUDE_DIR}/ObsPluginHelpers.cmake)

        # allows external plugins to easily use/share common dependencies that are often included with libobs (such as FFmpeg)
        if(NOT DEFINED INCLUDED_LIBOBS_CMAKE_MODULES)

Run cmake from the build directory like this:

cmake -DLIBOBS_INCLUDE_DIR=/usr/lib64/cmake/LibObs -DCMAKE_INSTALL_PREFIX=/usr ..
Xipooo commented 4 years ago

@hjacobs > After trying suggestions in this thread (ffmpeg etc), I found that the "v4l2-sink" plugin works flawlessly (~0 latency) on Ubuntu 19.10 for my use case (Google Meet). Here the blog post about my setup: https://srcco.de/posts/using-obs-studio-with-v4l2-for-google-hangouts-meet.html

I followed this walkthrough along with some solutions found here and elsewhere to finally get everything compiled. However I'm stuck with a persistent device failure and even crashing OBS itself. This may be related to my use of Mint instead of Ubuntu but seems rather insignificant a difference to cause device issues.

MarioMey commented 4 years ago

After trying suggestions in this thread (ffmpeg etc), I found that the "v4l2-sink" plugin works flawlessly (~0 latency) on Ubuntu 19.10 for my use case (Google Meet). Here the blog post about my setup: https://srcco.de/posts/using-obs-studio-with-v4l2-for-google-hangouts-meet.html

Hey, yesterday I compile OBS. But, for compiling v4l2sink, I had to install libobs-dev, as you say in your post. Buuut... today, I had to compile OBS again and I had problems when installing deb package. I had to remove libobs-dev before doing it. Maybe you can add this information to your post.

Thanks for your tutorial.

wildgruber commented 4 years ago

With regard to compilation errors of the "v4l2-sink" plugin due to incoherent path mechanisms, check out this https://github.com/CatxFish/obs-v4l2sink/issues/15#issuecomment-610547455! This worked perfectly.

KovalevArtem commented 4 years ago

https://github.com/obsproject/rfcs/pull/15

dszryan commented 4 years ago

Setup Description

  1. running arch linux host
  2. running windows 10 guest
  3. to the output/live steam you want to add
    • the windows 10 guest's speaker and video output (eg: your want to stream the video+audio for your xbox based jackbox games)
    • the linux hosts mic and camera output (so that people can see you and hear you in the stream as well)

Solution Description

  1. setup an audio loopback device
    • configure the loopback speaker as 'OBS Speaker', this will capture the output of the windows guest
    • configure the mic as 'OBS Mic'; this will be the live stream device
  2. have a headset connected to the linux host
    • this will function as the monitor device for the steam feed
    • you will be able to hear the windows 10 guest and your mic output on this device
    • do NOT monitor on a speaker, HAS TO BE A HEADSET (to avoid a positive feedback loop)

Setup Steps:

NOTE: the order of the steps is CRITICAL.

0. Install the following packages

1. Connect physical hardware

2. Setup virtual loop back device

3. Configure the linux host

4. Start the windows 10 guest

5. OBS Settings [the important part]

6. Audio Mixer Setting

7. Video Sources

8. Start steaming

9. Check output levels on the monitor device/headset

10. Go Live

11. ???

12. Profit

pinheirof commented 4 years ago

Hi, I am trying run OBS with virtual cam on Ubuntu 20.04. Followed this https://srcco.de/posts/using-obs-studio-with-v4l2-for-google-hangouts-meet.html.

However, need to run sudo modprobe v4l2loopback devices=1 video_nr=10 card_label="OBS Cam" exclusive_caps=1 every time I need to use OBS with virtual cam. Otherwise, I see a message "device open failed". Besides, it's not 100% certain that running modprobe OBS will work properly.

Any idea what I'm doing wrong? Btw, newbie here.

tsankuanglee commented 4 years ago

@pinheirof the command you run creates a virtual cam via a kernel module. You should only need it once per boot (unless you remove that virtual cam via sudo rmmod v42loopback). video_nr=10 is the cam number, so if you run the same command again, it may cause an error because of the number conflict.

pinheirof commented 4 years ago

@pinheirof the command you run creates a virtual cam via a kernel module. You should only need it once per boot (unless you remove that virtual cam via sudo rmmod v42loopback). video_nr=10 is the cam number, so if you run the same command again, it may cause an error because of the number conflict.

But wasn't /dev/video10 supposed to be there every time a turn on my pc?

For example, when I reboot the system and run v4l2-ctl --list-devices, v4l2loopback isn't there.

pinheirof commented 4 years ago

@tsankuanglee , my point is: why isn't the /dev/video10, created by v4l2loopback, a permanent device in my machine?

tsankuanglee commented 4 years ago

modprobe is a dynamic command. To make it permanent, you can either run that command in your boot sequence, or add it to modprobe.d (see https://wiki.archlinux.org/index.php/Kernel_module#Using_files_in_/etc/modprobe.d/ )

leoherzog commented 3 years ago

@pinheirof @tsankuanglee

I've tried adding

options v4l2loopback devices=1 video_nr=10 card_label="OBS Virtualcam" exclusive_caps=1

to /etc/modprobe.d/virtualcam.conf, then running sudo update-initramfs -c -k $(uname -r). When I reboot, I still get a Device open failed error in OBS and have to run the modprobe command by manually. Can anyone advise, what is the format of the modprobe.d conf file?

dszryan commented 3 years ago

@pinheirof @tsankuanglee

I've tried adding

options v4l2loopback devices=1 video_nr=10 card_label="OBS Virtualcam" exclusive_caps=1

to /etc/modprobe.d/virtualcam.conf, then running sudo update-initramfs -c -k $(uname -r). When I reboot, I still get a Device open failed error in OBS and have to run the modprobe command by manually. Can anyone advise, what is the format of the modprobe.d conf file?

i have the same issue, saw the message and did some more reading. realised the following.

i am using module as a dkms. so may be the fix would be to install the device via a kernel parameter https://wiki.archlinux.org/index.php/Kernel_module#Using_kernel_command_line_2

and the use to udev to initialize the other required options

will try and test it on the weekend, when i have time for a lot of reboots.

dszryan commented 3 years ago

@pinheirof @tsankuanglee

I've tried adding

options v4l2loopback devices=1 video_nr=10 card_label="OBS Virtualcam" exclusive_caps=1

to /etc/modprobe.d/virtualcam.conf, then running sudo update-initramfs -c -k $(uname -r). When I reboot, I still get a Device open failed error in OBS and have to run the modprobe command by manually. Can anyone advise, what is the format of the modprobe.d conf file?

the working solution was a LOT simpler. decided to bake the module into the initial ramdisk with the following config. excerpt from /etc/mkinitcpio.conf below.

MODULES=(v4l2loopback)
FILES=(/etc/modprobe.d/v4l2loopback.conf)

NB: need to bake in the config as well. hence the inclusion in the files. also, this has worked on Arch Linux, not sure how or if it will work on ubuntu.