Skip to content

Instantly share code, notes, and snippets.

@alvaro-jmp
Last active June 28, 2023 21:54
Show Gist options
  • Select an option

  • Save alvaro-jmp/cb2dae8490b34da2791d771180e26122 to your computer and use it in GitHub Desktop.

Select an option

Save alvaro-jmp/cb2dae8490b34da2791d771180e26122 to your computer and use it in GitHub Desktop.
Solution if the android sdk emulator version 32.1.13 can't access the /dev/video2 file created with the v4l2loopback module to use it as a virtual web cam in case you are capturing the screen for x utility.

FIX: Can't access the /dev/video2 in android sdk emulator version 32.1.13

Solution if the android sdk emulator can't access the /dev/video2 file created with the v4l2loopback module to use it as a virtual web cam in case you are capturing the screen for x utility.

If for example your pc represents the video capture devices with /dev/video0 and /dev/video1, you need to give the user read and write permissions to those files because the emulator iterates through each file /dev/video* and when it finds that it doesn't have the necessary permissions it stops iterating and fails to get to /dev/video2.

So you have to give read and write permissions to all files referred to /dev/video* if you are using them.

In case you are not using it you can do the following:

1.- Rename the files /dev/video0 and /dev/video1.

sudo mv /dev/video0 /dev/~video0
sudo mv /dev/video1 /dev/~video1

2.- Remove v4l2loopback kernel module if one exists. Create a file with the v4l2loopback module to use as a virtual web cam. Even if you use the option video_nr=0, it still creates it as /dev/video2.

sudo modprobe --remove v4l2loopback
sudo modprobe v4l2loopback video_nr=0 exclusive_caps=1

3.- Give the the user read and write permissions to /dev/video2 and create a symbolic link for to get access as /dev/video0.

sudo setfacl -m u:"$USER":rw /dev/video2
sudo ln -s /dev/video2 /dev/video0

4.- Screen recorder to be used in the virtual web cam.

You can run this part in a new terminal, if you end the execution and run it again it will throw an error. The way I saw to fix it is to repeat the steps from step 2.

ffmpeg -f x11grab -r $FPS -s 640x480 -i $DISPLAY+0,0 -vcodec rawvideo -pix_fmt yuv420p -f v4l2 /dev/video0

5.- To check if the emulator detects the virtual web cam. The command related to ffmpeg -f x11grab -r $FPS -s 640x480 ... needs to be running.

$ANDROID_SDK/emulator/emulator -webcam-list

In the example in this script there is only one capture device represented as /dev/video0, because the other files were renamed to /dev/~video0 and /dev/~video1

6.- You need to modify the virtual machine configuration file which is located in the following path ~/.android/avd/$NAME_OF_YOUR_ANDROID_VM/.config

hw.camera.back=webcam0

That's all !!!!

You want to return everything to its previous state.

You must abort the process the command related to ffmpeg -f x11grab -r $FPS -s 640x480 ...

1.- Remove v4l2loopback kernel module.

sudo modprobe --remove v4l2loopback

2.- Remove the symbolic link

sudo unlink /dev/video0

3.- Rename the files /dev/~video0 and /dev/~video1

sudo mv /dev/~video0 /dev/video0
sudo mv /dev/~video1 /dev/video1

Sources

https://stackoverflow.com/questions/27875415/android-emulator-unable-to-start-webcam-to-capture-picture-in-emulator/65268063#65268063

https://superuser.com/questions/1330959/what-is-the-right-ffmpeg-output-format-for-a-v4l2-loopback-device

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