Proxying camera streams with PHP to keep an eye on my home

What’s the point of a smarthome system that can remotely control everything if you can’t check in on it?

Recently I was in need of watching my oven while testing a new recipe. Being me, I didn’t want to sit in front of it for half an hour, so I decided to put my smartphone to good use and streamed the whole thing. To stream I used DroidCam, which worked out great. But a few things were bothering me. First, it was using the default interface of the app, and that wasn’t quite pleasant to look at. Second, I can’t watch the stream from outside of my network if I ever wanted to.

The solution is a proxy. The “hard” way would be to set up something with Apache’s config, but I wasn’t having any of that. As it turns out, I just need two lines of PHP in order to proxy via my smarthome framework, which in turn also includes user based restrictions and comfortable changes to settings:

header("Cache-Control: max-age=0"); //Optional, only used if you want to be able to reload the stream while staying on the same page
header("Content-Type: multipart/x-mixed-replace;boundary=--dcmjpeg");
readfile($url);

That’s it.

The stream coming from the DroidCam app is just a stream of images coming in being replaced whenever a new one shows up. In case of DroidCame, the pictures are seperated by “–dcmjpeg” (you can check that one by just reading the raw output from the stream).
“readfile” reads everything coming from the url and passes it right through.

After that it’s just a matter of providing easy ways to change some settings related to the camera and adding some checks to allow access to the cameras only to authorized users. The usual stuff.

This is pretty cool, because now I can check if remotely controlled devices are actually doing what they’re supposed to do.