How I made a video broadcast of a bird feeder

One evening I suddenly realized that I hadn't done any pet projects for a long time. And here comes another season of feeding birds at the dacha. It's very interesting to watch the birds through the window, but why am I the only one enjoying this spectacle? I need to make others happy too!

For the most impatient, the result can be viewed here, and for those interested in the backstory and the many failures encountered, welcome to the thrilling saga titled "A penny saved is a penny earned" 😜

A little lyrical prehistory:

For a couple of years now, I have been feeding birds at the dacha. I work from home, and my desk is right in front of the window, outside of which hangs a feeder. Every morning I pour a new portion of seeds, and then I watch different birds flocking to the feast.

It turns out that the feathered world of central Russia is quite rich. Very often I had to use Google to determine what kind of bird came to lunch.

Most often, of course, ordinary tits fly in. This bird is very cautious. First, it sits on a branch of an apple tree growing nearby and looks around. Then it quickly flies up, grabs a seed, and back to the branch, where it already peels it.

Other frequent guests are sparrows. These are much bolder. They always fly in a flock, eat while sitting right in the feeder. Those who are stronger and bolder also drive away their comrades. It's funny to watch their confrontation.

In winter, bullfinches often visit the feeder. It turns out that the female bullfinch's belly is not red at all, but just gray. At first, I could not understand for a long time what kind of strange bird it was, it seemed to look like a bullfinch, but not red!

Rarer guests that I have seen are robins, forest canaries, thrushes, nightingales. I saw a hawfinch a couple of times. Last winter, two partridges often flew in, they did not sit on the feeder, they "grazed" in the snow under it.

The situation is made especially piquant by my house cat. She really loves to hunt all sorts of living creatures. When she is at home, she sits next to the window and eagerly watches the birds. She even tries to "chirp". Then she asks to go outside, and then the birds have to be doubly careful. Despite her completely unmasking color and bell, the cat sometimes manages to catch some careless bird.

In general, an interesting and sometimes mesmerizing movie unfolds under my window, with natural actors. So I thought, why am I the only one watching all this, I need to share this wonderful spectacle with the world. The age of high technology is in the yard after all. I decided to put up a camera and organize this broadcast.

My experience with video streaming was limited to broadcasting via OBS on YouTube about ten years ago. So I started studying the topic. I set myself the task of organizing a broadcast from a camera outside the window to the website through NAT. At the same time, my internet connection is not very fast, and besides streaming, I also need to do regular internet activities.

The first thing I came across was the combination of gstreamer + mediamtx. We set up mediamtx on a VPS, configure minimal security (so that publishing is only possible with a username and password), and through gstreamer, we send the stream via RTSP from any device, simultaneously setting transformations and codecs. I tried it on my built-in laptop webcam - I was delighted, everything worked as it should!

So, I can order a camera, and while it arrives, I will write the software. I didn't bother much with the choice of camera. Since this is, after all, a pet project, the budget is minimal. The only requirement for the camera was the ability to work over the network. I ordered this one on the market for just one and a half thousand rubles:

Hidden text

When it arrived, my disappointment knew no bounds. The camera could only work with an ad-saturated Chinese app and nothing else. No web interface, no support for popular protocols... On 4pda, I found a topic dedicated to it - it used to be possible to awaken the necessary functionality in it through special files on a flash drive, but in later versions, this was fixed. And I got just such a version. The purchase turned out to be useless, except that I used the power supply from it with the next cameras.

The second purchase was more deliberate. I set the filters to mandatory RTSP support. For two thousand rubles and some change, I chose this one:

Hidden text

Of course, this is not an outdoor camera at all, but let me remind you about the budget, I wanted to save money, and for now, be content with this one, protecting it from snow and rain with a dome made from a plastic canister. (spoiler: saving money didn't work out again).

This model, as it seemed to me, had only one drawback - the web interface worked through ActiveX and, accordingly, only in Windows. But it didn't seem like a big problem - set it up once, and then take the stream via rtsp on any device.

While this camera saga was going on, I was writing code. As I already said, the broadcast itself will be carried out by mediamtx, and for the rest, I wrote a small websocket server on NodeJS. Comments were needed on the site next to the video, like on YouTube. The web application itself was made on react with playing the hls stream that mediamtx gives.

At first glance, everything worked, but problems began. The camera had too wide a viewing angle (usually this is a feature, but in my case - a drawback), and a lot of unnecessary things got into the frame. The image quality was frankly bad. I tried to solve this problem using gstreamer parameters.

First of all, I tried to send only part of the frame to the server. With the videobox plugin, you can set offsets from all sides for the frame. But naturally, with this approach, I lost quality - if you cut 640x360 from 1080p, you get 360p. I increased the stream to 4k in the camera settings and started cutting out 1080p. Thus, the situation with the viewing angle seemed to be resolved. However, I had to overlay the time on the cut-out frame with the clockoverlay plugin, because the time that the camera overlaid remained in the cut-out piece.

The quality problem remained. The idea came, since I use gstreamer, it doesn't matter what I use as a source. Why not try a regular USB webcam? Said and done. A webcam with the smallest viewing angle and an active USB extension cable were ordered. The total cost is about 2 thousand rubles.

Hidden text

It turned out to be a quiet horror. The camera had just terrible light sensitivity. Even indoors with artificial lighting, the graininess in the image was just off the charts. It turned out that the extension cord only supports power, but not data - the camera simply refused to work through it. In general, money thrown to the wind.

All this time I was broadcasting through the "old" IP camera, and noticed many more problems. The gstreamer command looked something like this:

gst-launch-1.0 rtspsrc location="rtsp://192.168.0.10:554/user=admin&password=&channel=1&stream=0?.sdp" protocols=tcp latency=0 ! decodebin ! queue ! videobox autocrop=false left=1000 right=700 top=200 bottom=800 ! clockoverlay font-desc="Sans 14" valignment=bottom time-format="%d.%m.%Y %H:%M:%S +3GMT" shaded-background=true  ! queue ! x264enc speed-preset=veryfast tune=zerolatency ! rtspclientsink location=rtsp://birds.unger1984.pro:8554/mystream user-id=* user-pw=* protocols=tcp latency=0

It is worth mentioning the protocols=tcp option separately - if it is not specified, frame loss begins. With UDP, gstreamer works completely incorrectly. At best, we get a gray screen. But with this option, gstreamer itself began to fall off. As soon as it starts catching a large frame loss, it just crashes! For this reason, the broadcast was constantly interrupted.

The solution was to replace gstreamer with ffmpeg - it also knows how to retransmit the stream, and works much more stably. Frame drops happen, sometimes artifacts appear on the image, but they disappear quite quickly and this happens rarely.

While I was dealing with this, a new camera arrived, almost for 4k rubles:

Hidden text

View of the bird feeder with the camera ready for video broadcasting.

https://market.yandex.ru/product--kamera-videonabliudeniia-ulichnaia-ip-3s-ipc-t200cs-4mp/87743667

The camera is from the same company, also works through ActiveX, but it is already outdoor and the image quality is an order of magnitude higher (the characteristics are almost the same, but this time they are real and not from the ceiling). This time the quality completely satisfied me, the FullHD stream was just great, but other problems appeared.

Let me remind you, everything is happening at the dacha, where traditionally there are problems with the internet. Although a couple of years ago they pulled optics to us, the channel remained very narrow. Often it was not even enough for work calls! If you also run a constant stream in 1080p through this, you understand what will happen. Therefore, an MTS SIM card from Ecomobile with full unlimited for 700 rubles/month was purchased. A separate router was installed with it and the broadcast was organized through it.

One fine morning the broadcast stopped working. It was found out by trial and error that the internet on the SIM card was not working. The signal quality is excellent, the personal account says that the remaining packages are Unlimited, but there is no internet. A conversation with technical support revealed the following - the limit on the card is 150GB, after which the speed is limited to 64kbps! A quick search on the internet showed that there are no really unlimited tariffs for anyone. The maximum is 1TB and then - hello speed limit!

Hoping for a miracle, I called my "wired" provider - have there been any new, faster tariffs? And then it turned out that I have an old tariff and for the same money 100MB has long been available! For some reason, there is no such information in the personal account or on the website. If I hadn't called, I would have been sitting with a flawed speed, but here they just switched it by phone and that's it.

Well, there were very few things left - to teach mediamtx to split the incoming stream by resolution into several low-quality ones, to make a resolution switcher on the site (unfortunately, the main feature of HLS with automatic quality selection does not work yet) and to write a mobile application.

I wrote the application on Flutter and published it only in RuStore. I can't publish anything in Google Play anymore because I can't update the payment profile data, for known reasons.

This is the story that turned out. I wanted to save money on a pet project - in the end I spent much more than I could. Of course, it's my own fault - I didn't fully understand the topic, but as they say, "experience is the son of difficult mistakes." But now you can enjoy the sight of birds with me https://birds.unger1984.pro/

Comments