Camlink
Camlink 4k with 2600k/780ti.
Did you ever want to stream qw, but all streaming software bugged you, you get input lag and weird lag, bad frametimes etc, software that announce good things but in reality is very very bad.
This guide will tell you all the secrets about the Camlink 4k and a rasperry pi. The Camlink 4k is a capture card, meaning it needs to sit in a usb port to operate. It's important to understand that placing the card in the same machine as playing qw from won't work any good, because we use opensource drivers and we can't encode locally. And however that it works, we get lag.
Scenarios.
First i tried this with nvidia drivers
[maincomputer/gpu-screen-recoder] -> [rasperrypi] -> [back to maincomputer using nvenc].
However, this won't work, because for some reason, when gpu-screen-recorder records it limits the fps to 30 fps althought it's recording with 60 fps. That is not desired and completly unaccepatble. We get high fps. The nvenc encoder gets annoyed because it's encode it's own output and the machine is crashing for unknown reasons. However skill in qw with the nvidia driver is so bad it's not worth it.
[maincomputer/gpu-screen-recorder] -> twitch. Works like shit, the fps is struggling and it's not worth it and we all hate the nvidia driver, we want pure nouveau (opensource drivers) and no bullshit.
Then i tried this scenarion.
[maincomputer] [dual outputting to dispayplport/hdmi with multiple soundcards in pulseaudio] -> twitch
This situation is somehow good, however, it won't meet our requirements, in the end the sound is to delayed and we all hate when Audio and Video is not in sync, it's none-fixable and not a great hack. It's really no point doing this since something is wrong with the pulseaudio core.
[maincomputer] [dual outputting to hdmi/dp with loopback and wave]
This a long chapter, because at this point audio/video sync is out of sync but the capture is doing fine. the ffmpeg at the rpi 4 is doing fine accepting 1280x720 stream (from hdmi output on the 2600k to the hdmi-input on the camlink 4k). the ffmpeg on the rpi 4 is accepting wave and delay is somehow ok. We want raw data from the soundcard without being encoding it, because the 2600k can hold 1.1-1.0ms in frametime when doing so.
What needs to be done on the RPI4?
First of all, the rpi 4 is a beatiful piece of crap computer that cost very little, it's able to encoding incoming uncomprressed data (via the hdmi-input) and re-encode it to 1280x720 x264.
In this example the rasperry pi is using raspian On the the rpi 4 you need to use this script.
#!/usr/bin/env bash # You need to run # sudo apt install v4l2loopback-utils v4l2loopback-dkms # once and restart your computer after that so that it reloads the kernel (just in case) # Creates a dummy device sudo modprobe v4l2loopback devices=1 exclusive_caps=1 # Find the dummy device file path V4LOOP=$(v4l2-ctl --list-devices | grep Dummy -A 1 | tail -n 1 | tr -d '\t') -z "$V4LOOP" && echo "No v4l2 loop device found" && exit 1 echo "Video4Linux loop device is $V4LOOP" # Find the cam link device file path ELGATO=$(v4l2-ctl --list-devices | grep 'Cam Link' -A 1 | tail -n 1 | tr -d '\t') -z "$ELGATO" && echo "No Elgato Camlink device found" && exit 1 echo "Elgato Cam Link loop device is $ELGATO" # Streams the elgato to the dummy, mapping the correct pixel format # Don't change the resolution/framerate, the driver will change it back anyway ffmpeg -f v4l2 -input_format yuyv422 -framerate 60 -video_size 1280x720 -i $ELGATO -pix_fmt yuyv422 -codec copy -f v4l2 $V4LOOP
This beatuiful script makes a loopback, and will record 24/7 from the hdmi
input source, which is nice, capturing directly from the /dev/video0 device
(Camlink 4k) won't work because when chaning resolution and restarting the
stream it will fail, the loopback solution is superb, now we get a /dev/video2
interface (in this example, you might get something else)
so /dev/video0 -> /dev/video2 is copying all the time in 1280x720@60fps in raw format. This is amazing. We don't want audio, we just want the video from
maincomputer -> hdmi/dp (same output) -> capture card -> video loop
Now to the magic on the rpi, make sure to change resolution to 1280x720 on the main computer, use scaling if neccesary. My output is 1920x1080 scaled to 1280x720 and outputting the 1280x720 to the hdmi port at the same time. A line could look like something like this, this is for the novueau driver
xrandr --output DVI-I-1 --off --output DVI-D-1 --off --output HDMI-1 --mode 1280x720 --pos 0x0 --rotate normal --output DP-1 --mode 1920x1080 --rate 200 --scale 0.6666x0.6666 --pos 0x0 --rotate normal
The 780ti can't do more than 200hz in 1920x1080 on the PG258Q but the monitor
actual supports 240hz, however, we're fine with the nouveau driver as there is
no buffering at all and qw skill increases. I won't go in to details, but my
setup is 2600k/780ti (1 hdmi 1 dp, 2x dvi ports) and with this setup
frametimes stays stable at 1.1-1.0ms
Now to the next thing, the magic line for recording on the rasperry pi with the camlink in 1280x720@60fps.
sudo ffmpeg -i tcp://192.168.255.1:5900?listen -f v4l2 -itsoffset 1.1 -i /dev/video2 -r 60 -c:v h264_v4l2m2m -c:a aac -b:v 6000k -f flv example.flv -y
It's fine to to this test, it's possible to stream directly to twitch/youtube with this, this is ez.
On the maincomputer use this nice line.
ffmpeg -f pulse -ar 44100 -i alsa_output.pci-0000_00_1b.0.analog-stereo.monitor -f wav tcp://192.168.255.1:5900/dummy.wav
We wan't to just capture the monitor of the pulse audio device. We want to use pulseaudio, because i couldn't figure out how pipewire works and it always bugged out for me with discord for some strange reason.
In this example we use pactl
raket@skal:~$ pactl list sources |grep monitor
Name: alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
device.class = "monitor"
Have fun streaming! :-)