The interesting case of IP cam

Problem

I had a problem. I wanted to monitor my baby while I wasn't in the room; she was a newborn back then. In general, I work all over the house. If she cries, it's difficult to hear her in most parts of the house. So, either I stay in the room while she's sleeping, or I need to come up with an idea. I could have set up an IP camera, but I was too lazy to do so, and I also didn't want any IP camera in my house. On the other hand, even if I don't want any IP camera, I can build an alternative IP camera for the sake of my problem. So I came up with this idea.

Solution

I often use my laptop and mobile phone in my house, and both of them are connected to the same WiFi network. So, I can use one device as an IP camera and the other device as the client from anywhere in the house. The only thing I needed was a server for the IP camera. Even though I'm a back-end developer, I didn't have any experience creating such a server.

Since I'm using a Linux distribution as my operating system, I know that I can use the V4L2 subsystem from the Linux kernel to render images from my webcam. The only thing I needed was a programming language that has the ability to communicate with the webcam and work with the V4L2 subsystem. After some digging, I found this library for the Go language.

The next problem was how I should stream data for the client. Since this should be done in real-time, it was a pretty straightforward solution. I chose WebSocket. The Gorilla Web Toolkit has this great library to work with WebSocket.

Here's an overview of how this works:

I run the IP camera server on my laptop. It turns on my camera and gets images frame by frame. At the same time, there will be an HTTP server running with WebSocket. All frames will be passed to the endpoint of that HTTP server so the client can capture frames by connecting to the same WebSocket server from the other side. Vanilla JavaScript will do the rest to render the data as an image on the front-end side. Every time a frame passes to the front-end, the front-end will automatically update by rendering the new image instead of the current image. So, you will see it as a video. This is the best I can do to describe the process.

Source code

You can find the source code for this system at the following link.

Link: https://github.com/enindu/ipcam

Note that this system will only work with the V4L2 subsystem, so it will probably work with a Linux distribution only. Even if you're using a Linux distribution, this might not work for you. I didn't check this system with any device other than my own devices or operating systems.