Showing posts with label Raspberry Pi. Show all posts
Showing posts with label Raspberry Pi. Show all posts

Tuesday, September 1, 2020

Running jackd command line works, but not via systemd because [Cannot allocate memory]

I am working on a personal project with my husband (also a software dev), where he got the video saving piece working with a raspberry pi, python, and picamera.  Audio wasn't working yet, so I decided to take that piece on.

Environment:
Raspberry Pi 2 Model B
Raspberry Pi Camera Module V2
Raspberry Pi USB WiFi Adapter
Raspberry Pi OS 10
USB Microphone
Python 3.7.3
systemctl --version => systemd 241 (241)

I'm currently working with JACK audio server and jack_capture to capture audio files.   The jackd2 package installation included the /etc/security/limits.d/audio.conf.disabled file, which when activated, enables realtime permissions and memlock permissions to the audio group.  You can activate it via the following command or rename the file without the .disabled extension at the end.  Make sure to logout and login again for the audio server to have the configuration recognized - no need to reboot/restart.

dpkg-reconfigure -p high jackd
I installed jackd2 via the following commands:

# audio server
sudo apt -y install jackd2
# modify /etc/security/limits.d/audio.conf to bring realtime priorities to the audio group (which is usually fine for a single user desktop usage)
sudo mv /etc/security/limits.d/audio.conf.disabled /etc/security/limits.d/audio.conf

The audio.conf file configures the following; I verified the default pi user is in the audio group.

@audio   -  rtprio     95
@audio   -  memlock    unlimited

I installed jack_capture via the following commands.  The liblo-tools were necessary for me to remote control jack_capture via UDP port with OSC (Open Sound Control) messages.

# audio capture
sudo apt -y install liblo-tools
sudo apt -y install jack-capture

Starting jackd with python via command line worked great!  When I started the python script as a service with systemd this error kept occurring:

Cannot lock down 13589280 byte memory area (Cannot allocate memory)

Why?!  I read through numerous jackd configuration articles.  Running command line was fine.  Checking "ulimit -l" was unlimited (as the audio.conf file told the machine to do).

I stumbled upon this and this article (looked very old but it still applied for my problem!) that pointed me in the right direction that "When using the RPM or Debian packages on systems that use systemd, system limits must be specified via systemd."  Essentially running jackd with systemd ignores the audio.conf file, so you must set it manually with systemd.

# The four settings under Service is for jackd support
# LimitRTPRIO and LimitMEMLOCK must be set in systemd service file, because when run as a service
# etc security limits.d audio.conf is not honored.  MEMLOCK unlimited is infinity in systemd
[Service]
LimitRTPRIO=95
LimitMEMLOCK=infinity
Environment="DISPLAY=:0"
Environment="JACK_NO_AUDIO_RESERVATION=1"

Note:

  • The LimitMEMLOCK is set to "infinity" for the systemd service, but in the audio.conf was set to "unlimited". 
  • The two environment variables DISPLAY and JACK_NO_AUDIO_RESERVATION were set to get rid of these two errors I had previous to the "Cannot allocate memory error".
To bypass device reservation via session bus, set JACK_NO_AUDIO_RESERVATION=1 
Failed to connect to session bus for device reservation Unable to autolaunch a dbus-daemon without a $DISPLAY for X11

These additional articles were helpful:

Tuesday, July 28, 2020

Raspberry Pi set up for development

I had a really old Raspberry Pi sitting around that I set up for development yesterday. The Pi 2 doesn't come with WiFi by default, so I set up a WiFi Adapter so it would be easier to develop on my localhost and ssh as necessary.

Environment:
Host: macOS Catalina
Raspberry Pi 2 Model B
Raspberry Pi USB WiFi Adapter (I have this one)
Raspberry Pi Camera Module V2
  1. Install Raspberry Pi OS image on an SD card: I used Raspberry Pi Imager rbi-imager for macOS, chose Raspberry Pi OS (32-bit) -  https://www.raspberrypi.org/documentation/installation/installing-images/
  2. If you plan to use the WiFi Adapter and Camera, connect those.
  3. Boot into the Raspberry Pi and login
  4. Connect any peripherals (monitor, keyboard, mouse)
  5. Open terminal
  6. Confirm the OS version. Mine is "Raspbian GNU/Linux 10 (buster)"
  7. cat /etc/os-release
    
  8. Update settings for US.  
    • On the Welcome to Raspberry Pi screen
      • Click Next
        • Country: Choose United States
        • Language: American English
        • Time Zone: New York
          • Enable Use English language and Use US keyboard
      • Click Next
        • Enter password
      • I skipped the WiFi configuration screen since my SSID is not broadcasting so need to configure it manually
    • This is an alternative way to do it via the command line.  Key thing is I needed for my keyboard to recognize the double-quote key
    • sudo raspi-config 
      Choose Localisation Options 
      Choose Change Locale => en_US.UTF-8 UTF-8 
      Choose Change Keyboard Layout => Generic 101-key PC => Other => English (US) (Country of Origin) => English (US) (Keyboard layout) => The default for the keyboard layout => No compose key => 
      Choose Change WLAN Country => US United States 
      sudo reboot
  9. Configure Raspberry Pi to connect to WiFi

    Backup file and copy to your home folder
    sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.orig
    cp /etc/wpa_supplicant/wpa_supplicant.conf ~
    

    Add below to the end of the file. I had to add scan_ssid=1 because my SSID is not broadcasting.  NOTE:  If you have special characters in your password, e.g. double quote I didn't have to escape it but I had to add in key_mgmt=WPA-PSK config attribute (with no quotes).
    vi ~/wpa_supplicant.conf 
    
    network={ 
      ssid="<ssid>"
      psk="<secret password>"
      scan_ssid=1 
    } 
    

    Copy updated file back
    sudo cp ~/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf 
    

    Verify if the “inet addr” is available on wlan0
    ifconfig wlan0 
    

    If not, reboot to connect
    sudo reboot 
    

    On reboot, I can see it the IP address is XXX.XXX.XX.XXX”

    To verify internet connectivity perform a wget
    wget www.google.com
    
  10. Enable ssh on Raspberry Pi
    sudo raspi-config 
    Choose Interface Options 
    Choose SSH => Yes 
    [Didn’t need to reboot]
    
  11. Enable camera on Raspberry Pi
    sudo raspi-config 
    Choose Interface Options 
    Choose Camera => Yes 
    [Prompted to reboot] => Yes
    
  12. Connect to Raspberry Pi via ssh from localhost, update macOS hosts file with Raspberry Pi IP
    sudo vi /private/etc/hosts 
    

    Added this to bottom of file
    # raspberry pi 
    XXX.XXX.XX.XXXX csrp
    

    ssh to the Raspberry Pi
    ssh pi@csrp
    
  13. Verify camera works
    • The Raspberry Pi captures video as raw H264 video stream
      • raspistill -o ~/image-small.jpg -w 640 -h 480
        raspivid -o ~/video.h264 
        
    • secure copy to and open it in macOS
      • scp pi@csrp:/home/pi/image-small.jpg ~/Downloads
        scp pi@csrp:/home/pi/video.h264 ~/Downloads
        
  14. Shutdown the Raspberry Pi
    sudo halt