Showing posts with label pi. Show all posts
Showing posts with label pi. Show all posts

Monday, July 19, 2021

Airplay (Shairport-sync) on the Raspberry Pi and Disabling Wifi Power Save

As of this writing on Jul 19th 2021 I am having trouble with the packaged version of shairport-sync. This page outlines how to fairly simply compile it from a different repository and also to disable the problematic wifi power save mode which can cause skipping.

 

I am now running 4 different raspberry pi’s with the Shairport-sync software to receive airplay streams from our phones and computers and they work great. The ones connected via WiFi tend to have dropouts while the ones with physical connections seem to work perfectly all the time and almost never skip. This seems to be in spite of the power or memory of the machines in question which range from pi zero’s through Pi3 B+ and a couple of Pi4’s now.

I recently upgraded the one we use the most to a Pi4 trying to see if that would solve the problem, and I wanted it to run some other software at the same time and thought it would be worth doing a reload on that older Pi3. 

I initially tried the standard distribution of the shairport-sync installed via:

sudo apt install shairport-sync

That worked great except that I could not seem to make any changes to the configuration file. If you just want to use your hostname as the share name and do not need to change any other configuration options then this will work just fine and you don’t really need to do anything else other than enable it via the systemctl enable shairport-sync and then reboot. I didn’t want my hostname to be the share name and whenever I made any changes to the configuration file, which the standard package installs in /etc/shairport-sync.conf instead of the other standard location where it has been as long as I can remember in /usr/local/etc/shairport-sync.conf, it would just crash upon starting up.

I uninstalled it and decided to scrape around and do an actual build of the software from the same repository that I used originally for the others. There are several forks available the one I used successfully is this one by github user Mike Brady

First install some necessary packages to be able to build it, this should be done after you’ve updated the software on your pi via the normal methods.

sudo apt install autoconf libtool libdaemon-dev libasound2-dev libpopt-dev libconfig-dev
sudo apt install avahi-daemon libavahi-client-dev
sudo apt install libssl-dev
sudo apt install git

Now we need to clone the git repository and to the configure and make steps:


cd ~
git clone https://github.com/mikebrady/shairport-sync.git
cd shairport-sync
autoreconf -i -f
./configure --with-alsa --with-avahi --with-ssl=openssl --with-systemd --with-metadata

If there are other options you wish to compile in like the mqtt server or others that configure line is the place to add them.

Once those are done you can do the make and install:

make
sudo make install

And enable the app to run at startup like this:

sudo systemctl enable shairport-sync

If you wish to change the name of the share as it appears to iOS devices or increase the timeouts for resyncs which I also did for a little more leeway with a bad wifi connection you can edit the configuration file which should be placed in /usr/local/etc/shairport-sync.conf via the command:

sudo nano /usr/local/etc/shairport-sync.conf

You’ll find the name entry at the top of the first section, you can uncomment theirs and change it or just add a line like:

name="Living Room";

you can also scroll down a bit to find the drift and resync tolerances. If you really want to have several servers all receiving the stream at the same time and playing the same music in sync then you should not alter these, but if you are mostly using one at a time as I do for whatever room I’m in and not the entire system you can increase their default times by altering the default lines which look like:



// drift_tolerance_in_seconds = 0.002; // allow a timing error of this number of seconds of drift away from exact synchronis$
// resync_threshold_in_seconds = 0.050; // a synchronisation error greater than this number of seconds will cause resynchron$

uncomment those and experiment with larger values. For myself I increase the drift tolerance to 0.05 and the resync threshold to 0.1

There are SO many suggestions out there on how to force the wifi power save mode to stay off after a restart but only this one worked for me. You can check to see if the wifi power save is on by running the command:


iw dev wlan0 get power_save
and change it via the command:
sudo iw dev wlan0 set power_save off

It seems that despite all protestations by many people that this is now off by default on their pi’s it is most definitely on when mine reboot. We need to create a systemctl command to run at startup that will execute the command above. If you have more than one wifi interface or are using the descriptive names option in the newer Pi OS’s you may have to use a different name than wlan0 but that is the default if you haven’t changed anything as of this writing.

Create the file with the nano editor like this:


sudo nano /etc/systemd/system/wifi_power_save_off.service

Paste the following into the file and save with ctrl-o and then ctrl-x to exit the editor.

[Unit]
Description=Set WiFipower save off so no skipping of audio
After=sys-subsystem-net-devices-wlan0.device

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/iw dev wlan0 set power_save off

[Install]
WantedBy=sys-subsystem-net-devices-wlan0.device

In order for the systemctl system to see this additional file you have to run the reload daemon command and finally enable it with these 2 commands:

sudo systemctl daemon-reload
sudo systemctl enable wifi_power_save_off

Now reboot your pi and check to see that your shairport-sync share shows up in your iOS devices properly and then run the "iw dev wlan0 get power_save" command again to make sure that it has run properly.

Friday, January 3, 2020

Python Motion Detection on a Raspberry Pi Zero

I have been working non-stop the last few months to make my main project software compatible with Apple’s latest OS version. I finally realized how badly this was depressing me and how much I didn’t want to be doing this and how much it was making me hate Apple. I was spending literally man months of time to rebuild from scratch things that weren’t broken before Catalina and it was making me miserable. My rants about the absolute stupidity of everything that Apple chose to do to this alleged OS update will have to wait as I can’t afford the dental bills to fix the damage that gritting my teeth that hard will cause.

So in the interest of sanity and preserving the small quantity of it with which I have been granted  I took a short vacation from everything MacOS. I have been fighting with the horrible firmware of even the best available IP security cams for some time as I struggle to write software that can connect to them and insert their video streams into the web and other interfaces of XTension. I believed that there should be software for the Raspberry Pi that could leverage that camera for a much more reasonable price that would be terrific. Sadly even the best software out there for it just wasn’t doing the things that I wanted or at least with the quality of image and recording that I wanted.

The idea of controlling everything about a security camera is compelling! Not having to setup a firewall to keep it from contacting it’s own “cloud” and making sure it isn’t setting up a UPnP passthrough even when you told it not to have revealed to me several times that even commercial products don’t really do what you think they are. I wanted one based on software that I have written myself and so knew exactly what it’s doing.

It should also run well on a Pi Zero W so that you can make something small, though if it runs better on the latest multi-core Pi4’s thats OK too. Those little machines are worth every penny and very very nice!

The built in “motion” system is really interesting, but it’s remarkably difficult to get it to run at a high framerate and a larger frame size. Who wants a non-HD security cam now days anyway? The separate OS versions available may be spectacular but I want a software package for the stock Pi. After fighting with trying to get openCV to compile on a Pi Zero for the last week I wanted something that didn’t need that either. (Which I suspect is why they went with a totally separate image for their security cam OS as getting that stuff to install properly is almost hilarious)

The python camera support for the Pi is absolutely awesome. I can stream 1080p video and I can record it to disk and then post process it to standard MP4 files in almost real time. Those were not insignificant accomplishments ;) But the real sticking point was going to be the video motion detection. Using the pycamera code as a starting point and the hardware H264 encoders ability to output it’s changes from one frame to another makes it absolutely possible to do this at least as well as any commercial product. I’ll eventually open source this project but for the moment I’m working on it just myself. Here’s the 720p stream, part of the web interface and also a stream of just the motion sensing. This works as high as 1080p but at lower framerates though I’m sure I can optimize the calculations a bit more for that.


The image on the right is a live stream of the motion content and I’m just really enjoying playing with this at the moment.

I am currently building the web interface to the ability to mask out portions of the video for motion detection. The math for that is working great and adds very little if any overhead. Lots of work yet to do of course before something that is download/install ready but it will come before long. This is making me happy! Apple is making me mad.

Lest anyone think I’ll abandon developing software for Apple products over how much they drive me crazy sometimes really needs to spend some time using any of the other truly commercial desktop OS’s available out there. Don’t worry. XTension is undergoing a lot of changes because of this, but it’s not going away and unless I sell enough of it to hire a support team in another time zone I wont even consider doing a windows version. Sorry but if Apple frustrates me then Microsoft would kill me outright.

.code { background:#f5f8fa; background-repeat:no-repeat; border: solid #5C7B90; border-width: 1px 1px 1px 20px; color: #000000; font: 13px 'Courier New', Courier, monospace; line-height: 16px; margin: 10px 0 10px 10px; max-height: 200px; min-height: 16px; overflow: auto; padding: 28px 10px 10px; width: 90%; } .code:hover { background-repeat:no-repeat; }