Skip to content

Instantly share code, notes, and snippets.

@cadadr
Last active March 26, 2024 06:41
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cadadr/64df554935c6fd50aa45428b4e5e4a11 to your computer and use it in GitHub Desktop.
Save cadadr/64df554935c6fd50aa45428b4e5e4a11 to your computer and use it in GitHub Desktop.
Use android phone as webcam using scrcpy, OpenCamera, and v4l2-loopback

Download Android SDK and put it under a known location. Get the appropriate commandlinetools package from this page. Then, extract it under ~/Android/.

mkdir -p ~/Android/Sdk
cd ~/Android
cp ~/Downloads/commandlinetools-linux-6858069_latest.zip .
unzip commandlinetools-linux-6858069_latest.zip

After this, we have to set up the build environment, accept android licences, and build scrcpy from the dev branch.

export ANDROID_SDK_ROOT=$HOME/Android/Sdk
~/Android/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_SDK_ROOT --licenses
cd /tmp
git clone https://github.com/Genymobile/scrcpy
cd scrcpy
git co dev

First, install dependencies.

sudo apt-get install ffmpeg libsdl2-2.0-0 adb gcc git pkg-config meson ninja-build libavcodec-dev libavformat-dev libavutil-dev libsdl2-dev openjdk-11-jdk

These commands should suffice to build scrcpy, tho Android being Android, you never know.

meson x --buildtype release --strip -Db_lto=true
ninja -Cx

Create a v4l2-loopback device.

sudo modprobe v4l2-loopback

On the phone, enable USB debugging, and install OpenCamera on your Android device. Connect via USB, make sure to confirm USB debugging once Android asks, and launch OpenCamera. In it, go to Settings > On screen GUI > Immersive Mode and select Hide everything. Camera controls should disappear after a few moments of inaction when you go back to the camera.

Now we can launch scrcpy:

ADB=/home/g/Android/Sdk/platform-tools/adb ./run x --v4l2-sink=/dev/video0 -N -S --lock-video-orientation=0

Note: currently this seems to leak memory, see this issue.

This will expose /dev/video0 as a v4l2-loopback device.

The server will start listening. I have so far tested this with Zoom and Jitsi on Firefox, and also OBS from Debian stable repositories (22.x, AFAICT). Cheese doesn’t work.

@imneonizer
Copy link

I have automated the camera streaming, you can checkout the script here https://github.com/imneonizer/android-webcam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment