USE SPARE ANDROID PHONE AS WEBCAM FOR YOUR PC.
Tags: android, linux, scrcpy, webcam
Published: 2023-05-15 22:14:16

Have you had that awkward moment when you had a meeting to be held on video with your PC for an interview and the camera was so bad you were left reeling?

Hold your breath yet, you can turn that spare android phone in the house into a webcam for your PC!
Chances are that the spare phone has a decent camera a tad better than your computer's inbuilt camera. Webcams are at a premium and if you don't have the money to afford one yet, you are at the right place.

In this tutorial, you only need to have:
a) Android Phone
b) USB cable
c) PC running Linux

This is a beginner level tutorial and if follow along, you will get it done. Now let us go!

**Table of Contents**

Prepare the Android Phone...
Install dependencies on the PC
Configure the phone with the PC
Launch the phone's camera as webcam for the PC.
Conclusion

**Prepare the Android Phone**

Android out of the box runs on the linux kernel and a few tweaks here and there gets it upto speed. Goto 
a) About Phone
b) Look out for build number and tap it 7 times!
c) Boom, you are now a developer.

Goto Developer Options, toggle 'USB Debugging' on and connect your phone via a USB cable to the PC.

**Installing Dependencies On the PC**

After connecting the android phone via USB to the PC, open a terminal and enter the command: ADB tool is used for port forewarding from android device to PC.

`sudo apt install android-tools-adb-y`

`adb devices`

A prompt will be displayed on your android phone, accept it and now your phone can communicate with your PC via adb.
ADB stands for Android Debug Bridge.
Install the latest version of the open source tool **SCRCPY**. Install [scrcpy](https://github.com/Genymobile/scrcpy).
Scrcpy which stands for 'Screen Copy' is an open source tool that allows us to mirror our android screen to your PC.
You may install with the following command,
 
`sudo apt install scrcpy` in the terminal.

Once it has been installed, launch the tool with the command `scrcpy` and it launches a screen mirroring your android phone.

This shows you have configured everything correctly.

**Launch The Phone's Camera as PC**
This chapter does over a bit over all the processes described earlier. Enter the following commands:

`sudo apt update -y`
`sudo apt install -y`

These commands will work on any debian OS. 
However, if the commands fail to work, you can build it from source. You may follow this [link](https://github.com/Genymobile/scrcpy)

The next step is to install 'ffmpeg', this will enable us to publish the received stream from the camera to a virtual camera on Linux System. This is the command:
`sudo apt install ffmpeg -y`

The next step is to install 'v4l2loopback' which reads as video for linux. This will be used to create the virtual camera.
This is the link to [v4l2loopback](https://github.com/umlaeute/v4l2loopback) on github.
These are the steps to install it;

   ```
   git clone https://github.com/umlaeute/v4l2loopback
   cd v4l2loopback
   make && sudo make install
   sudo depmod -a
   sudo modprobe v4l2loopback
   ```

**Setting Up Android Phone**

Install IPwebcam on your android device from [playstore](https://play.google.com/store/apps/details?id=com.pas.webcam&hl=en_IN&gl=US).
This application is bloated with google ads hence proceed to pay for the premium version.

**Steps To Use Camera Over USB**

1) Open the 'ipwebcam' application on you android phone, scroll to the bottom of the displayed page and click `start server`.

2)An IPv4 `IPv4` address will displayed at the bottom of the screen.

3)Initiate port forwarding from your phone to your PC via ADB to access the camera.
Open a terminal and scan for available devices with this command:
`adb devices -l`

Output:
```List of devices attached
affhh41a     device usb: product:phone brand device transport_id:2```

4)Export the device serial number in your terminal , once this step is done, you can connect multiple devices to the PC without any issue with ADB.

```export ANDROID_SERIAL=affhh41a```

Initiate port forwarding from the android device to your PC.

```adb forward tcp:8000 tcp:8000```
The first port indicates port on the host machine and the second port number indicates which port 'IPwebcam' is running on your android phone.

Visit http://localhost:8080/ on your PC you should see the ipwebcam interface. Similarly, if you visit http://localhost:8080/video you should see the ipwebcam's video feed in your browser.
This must be done in 'Mozilla Firefox Browser'.

![Ipwebcam Interface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9q04x8wywmk4syu91n7.png)

5) The video stream can now be redirected from a virtual camera device using v4l2loopback with this command:
Enter this command to create a dummy virtual camera;

`sudo modprobe v4l2loopback`


![Modprobe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcbuy8nawwq9zknjt5bg.png)

Enter this command to find your virtual camera:

`v4l2-ctl --list-devices`
That would be the last device on the list, dev/video2.


![Video Stream](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awdkycoudfp567i1egf2.png)


Redirect the stream from the browser to the virtual camera with the following commands;

```sudo ffmpeg -i http//localhost:8080/video -vf format=yuv420p -f v4l2 /dev/video2``` or 

```sudo ffmpeg -i /dev/video -vcodec rawvideo -pix_fmt yuv420p -thread 0 -f v4l2 /dev/video2```


![Virtual Camera in browser for google meet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffcow34qo18gqxankjsu.png)







Now the virtual camera is ready and can be used for any online meeting application /obs-studio / OpenCV applications.


![Obs studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cpc16rk8a3wawp3b05h.png)
Note: I reiterate please use Firefox browser, dummy camera will not be available in Google Chrome.

**Conclusion**
There are no Graphical User Interfaces for this, but then scrcpy does the job on the command line. Any comments?

They will be hugely appreciated.