Usually, the quality of built in webcams on laptop computers is not very good. It can introduce a lot of noise in the picture, which is not always good for live streaming, video recording or even for video conferencing for that matter. External good quality webcams solve this problem; but they are expensive. If you have an android device with a good camera, you can use that as a webcam on your computer, which will give you a very good quality (high resolution) video feed. Did I mention that you can do this without using any proprietary software on any of the device? So let’s see how to do that.
For this you will need follwing tools.
In this process we are going to
- build
LiveVideoBroadcaster
andv4l2-loopback
- Install
LiveVideoBroadcaster
in the android device. - Create a virtual camera device in the linux machine using
v4l2-loopback
. - Build and use
gortmp
which we will be using as anRTMP
relay. - Use
ffmpeg
to read thertmp
stream coming from mobile and redirect it to the virtual camera.
So let’s get started.
You will also need following things setup on your system.
Note: Make sure you have GOROOT
and GOPATH
set properly we need that for building
gortmp
.
First off, let’s clone the tools.
LiveVideoBroadcaster
To clone it,
$ git clone https://github.com/ant-media/LiveVideoBroadcaster
Once that is done, open it in the android studio
let gradle
finish it’s
processing.
After that, change URL
for the RTMP
stream in
app/src/main/java/io/antmedia/android/MainActivity.java
at line number 16
to whatever your URL
is.
(according to this post it should be rtmp://<your deice ip>:1935/rtmp/
.)
Now build this project and install it in your device.
gortmp
The LiveVideoBroadcaster
is an application from antmedia,
They also provide an RTMP
server, (which I did not use because I’m lazy and
already had gortmp
on my system and that worked well ;) ).
So let’s clone and build gortmp
$ go get github.com/sevenzoe/gortmp
To build and use it
$ cd $GOPATH/src/github/sevenzoe/gortmp
$ go build
$ mkdir /tmp/rtmp
$ ./main.go
This will start the rtmp server on this machine.
Now we that have both our RTMP
server and android application
(LiveVideoBroadcaster) running, let’s test the setup once.
You get ffplay
tool with your ffmpeg installation, this can be used to play
this live stream from the mobile.
Start the mobile application, click on LiveVideoBroadcaster
button and enter
the stream name what ever you like. (for example stream1
).
on gortmp
console you will see logs for newly accepted connection.
Now to test it.
$ ffplay rtmp://localhost/rtmp/<your stream name>
# for example
$ ffplay rtmp://localhost/rtmp/stream1
This will render the live stream from your mobile on to your computer screen.
Once we have this thing setup we have to create a virtual camera on the computer and pass this incoming video feed from the mobile to it.
In order to do that we need to use v4l2-loopback
. Let’s clone and install that.
But before doing that, let’s first see how many cameras are available on the system.
$ ls /dev/ | grep "video"
here you will see the list of available cameras on the system.
V4l2-loopback
To clone it.
$ git clone https://github.com/umlaeute/v4l2loopback
To build it
$ cd v4l2loopback
$ make
$ sudo make install
$ sudo depmod -a
If you don’t see any warnings after sudo depmod -a
command, that means you
it setup properly.
Now let’s create the virtual camera.
$ sudo modprobe v4l2loopback
After inserting the v4l2loopback module, we should see one more camera on the system. let’s check it again.
$ ls /dev/ | grep "video"
Let’s try this new camera on the system. Let’s say the new virtual camera is
the device /dev/video3
on your system. Then the command will be
$ ffplay /dev/video3
You will probably see no output or a blank screen.
Remember we had to pass the data from the rtmp
stream to this camera? Let’s
do that.
For this purpose we will use ffmpeg
. The command will be
$ ffmpeg -re -i rtmp://localhost/live/rtmpv1 -an -f v4l2 -vcodec rawvideo -pix_fmt rgb24 /dev/video3
Let’s see what this command does.
-re
: Read input at native frame rate.-i
: specifies the input video source.-an
: Ignore the audio, we don’t want to pass the audio to this virtual cam.-f
: output format.-vcodec
: Video codec.-pix_fmt
: The output pixel format./dev/video3
: our virtual camera device.
If this works, let’s run ffplay to test the camera once again.
$ ffplay /dev/video3
At this point you should see the video feed from your mobile camera on the system’s virtual camera. You can use it for the video conferencing, Video recording and all sorts of things you use your web cam for.