Tech used

Installing Twonky Media Server in a Raspberry Pi

Updated 2022-09-14
Photo by Jonathan Velasquez on Unsplash

I generally don't like when a website gives you a whole personal story about whatever you are looking for. Chances are, whoever is doing web searches trying to figure out... whatever, they have been to several other sites trying to get a sense of what's going on. This is not what I do here.

You're here and there's a high chance you know what you are doing. To that end, feel free to skip the background section and dive right in.


Twonky Background

Twonky Media Server is a fine DLNA UPnP server that supports lots of media files, a rare example of the right combination of functionality and price. I can hear you asking: "But, Twonky is not free. Why should I bother?." The answer is simple: if you want a media server that doesn't take over your RPi that supports every file type imaginable, this is the way to go.

The big caveat here, apart from the cost, is that I'm writing this for the audiophiles amongst us. Twonky is one of the few DLNA servers for Linux that supports the streaming of DSD (DSF and DFF) files. For most people, this is irrelevant, as the playback of DSD files requires expensive equipment, not to mention the fair sourcing of said files in the first place. Most people will be happy streaming from their paid services and not bother with this.

If you are interested in DSD as a format, Wikipedia has a good write up on it.


Table of Contents

  1. Tools Needed
  2. Getting started with Raspberry Pi (Abridged)
  3. Downloading and installing Twonky
  4. Setting up a service for Twonky
  5. Dealing with the firewall
  6. Musings

Tools Needed

Well, this part is easy. There's only a few things needed to complete this project:
  • Raspberry Pi with Raspbian installed (I personally use RPi 4 Model B, 2GB variant)
  • Twonky Install Files
  • Time and patience to go through the process

If you're running a headless RPi, you'll also need a second computer to access the RPi.


Getting started with Raspberry Pi (Abridged)

I'm going to skip the basic setup of a brand new Raspberry Pi here. Should you need some assistance getting started from zero, I'd recommend Rasbperry Pi's own tutorial.

However, there's a few things I should note about the state in which the RPi need to be before we can really get going:

  • I strongly recommend you don't run your RPi with the default pi user. If you haven't had the chance to get around to it, do it before continuing. Follow this handy guide to get it done.
  • You will need sudo access to complete this tutorial.
  • This tutorial is targeting the command line, rather than the GUI way.
  • Rasbpian (or Ubuntu) must be fully updated. To do this, launch Terminal (or SSH into the RPi) simply run sudo apt update && sudo apt upgrade -y to get everything updated
  • If you haven't done so, enable UFW in your RPi to follow best security practices:
    • Run sudo ufw allow ssh to allow SSH access to your RPi from remote machines
    • Then sudo ufw enable to turn everything on. Please note that this will break any other access that's not allowlisted.

Downloading and installing Twonky

Twonky has had a rocky existence. While someone is still supporting the app, the information to get things done is not easily reachable. Tutorials on the web are pretty few and far between, with some including pictures of screen content (not actual screen shots, mind you). This tutorial should fill that gap.

The official website for Twonky contains their most recent version for the major platforms (Windows, MacOS, and Linux AMD64). However, they fail to mention that there's a trove of other files in their old forum, which include the files compiled for ARM, amongst many, many other platforms. Let's get started with the commands:

  • Run wget http://download.twonky.com/8.5.1/twonky-armel-glibc-2.13-hf-8.5.1.zip to fetch Twonky from their servers.
  • Run sudo mkdir /usr/local/twonky to create the final destination of Twonky's files.
  • Run unzip twonky-armel-glibc-2.13-hf-8.5.1.zip -d /usr/local/twonky to unzip the contents to the newly created folder.
  • Run cd /usr/local/twonky to move to said folder
  • Run sudo chmod 700 twonky.sh twonkys* cgi-bin/* plugins/* to ensure the necessary files are executable.
  • Run sudo chown user:user /usr/local/twonky/* to give ownership of the files to your user. Replace user:user with your own user on both sides.
  • Run /usr/local/twonky/twonky.sh start to test things out. You should see some output that states many things, including something about missing a TLS certificate. Do not worry about said error.
  • Run /usr/local/twonky/twonky.sh stop to stop the server we just stood up.

There is a possibility that after running the last command, the one that manually launches the application, an error along these lines appears:

Starting /usr/local/twonky/twonkystarter 
/usr/local/twonky/twonky.sh: 188: /usr/local/twonky/twonkystarter: not found

If this is the case, run the following command to install some missing dependencies:
sudo apt install libc6:armhf libc6-dbg:armhf libc6-dev:armhf

At this point, you have a working copy of Twonky installed. However, having to run the start command each time the RPi reboots is less than ideal.

Setting up a service

Next, we'll setup Twonky as a proper service. This way the OS makes sure the media server is brought up on boot every time; no user intervention required.

  • Run sudo nano /etc/systemd/system/twonky.service to create the service file. In it, we'll paste the following:
    • [Unit]
      Description=Twonky Media Server service
      After=syslog.target network.target
      
      [Service]
      Type=simple
      ExecStart=/usr/local/twonky/twonkystarter
      ExecStop=/usr/local/twonky/twonky.sh stop
      PIDFile=/run/mediaserver.pid
      Restart=always
      
      [Install]
      WantedBy=multi-user.target
      						
  • While in nano, hit ^s and ^x to save and exit respectively.
  • Now, run sudo service twonky status to make sure that the OS recognizes the newly created. If everything went well, the output should be something like this:
    • ● twonky.service - Twonky Media Server service
         Loaded: loaded (/etc/systemd/system/twonky.service; disabled; vendor preset: enabled)
         Active: inactive (dead)
      						
  • Run sudo systemctl enable twonky to enable the service to run on boot. This command will provide with some feedback about how symlinks where created that looks something like this:
    • Created symlink /etc/systemd/system/multi-user.target.wants/twonky.service → /etc/systemd/system/twonky.service.
      						
  • Lastly, run sudo service twonky start to start the newly created service. If everything went well, the output should look like this:
    •  twonky.service - Twonky Media Server service
         Loaded: loaded (/etc/systemd/system/twonky.service; enabled; vendor preset: enable
         Active: active (running) since Mon 2020-04-20 21:59:25 BST; 1h 42min ago
       Main PID: 30188 (twonkystarter)
          Tasks: 18 (limit: 4035)
         Memory: 9.4M
         CGroup: /system.slice/twonky.service
                 ├─30188 /usr/local/twonky/twonkystarter
                 └─30189 /usr/local/twonky/twonkyserver
      						
  • The server comes with a serviceable web UI that can be accessed in port 9000 of the RPi's IP address. But first, let's deal with the firewall.

Dealing with the firewall

If you followed my advise about turning on ufw a few steps ago, you won't be able to reach the configuration website of the media server just yet. This is because the firewall simply doesn't know let some of the traffic in the specific port go through. Thankfully, ufw is easy to deal with when it comes to this.

  • Run sudo ufw allow 9000/tcp to open the port to access the configuration website of the media server. The output should look something like this:
    • Rule added
      Rule added (v6)
      						
  • Run ip address to fetch the address of RPi. The output should look something like this (the value we're looking for is highlighted in green):
    • 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
          link/ether ab:cd:ef:fe:dc:ba brd ff:ff:ff:ff:ff:ff
          inet 192.168.1.2/24 brd 192.168.1.255 scope global noprefixroute eth0
             valid_lft forever preferred_lft forever
          inet6 fe80::95a9:f608:be77:2bbe/64 scope link 
             valid_lft forever preferred_lft forever
      
      If you're using Wi-Fi, you may be looking for the wlan0 entry in the ip address output.
      						
  • Lastly, for real this time, navigate to the configuration website from a browser by visiting http://localhost:9000/webconfig or, in the case of our previous query, http://192.168.1.2:9000/webconfig from any other computer in the same network. You should be greeted by something along these lines:
    • Twonky Server Status screenhot. Everything in the image shows that the server is up and running.

Musings

I hope you've found this useful. Here are a few take-aways from this:

  • I tried setting up the service to use its own user. This a good idea for security reasons. However, I failed to make it work. If you have a suggestion on how to achieve this, drop me a line and I'll make sure to include your input on this tutorial.
  • I think Twonky has transcoding-on-the-fly capabilities. However, these are untested for this specific setup. I'm not sure if the little RPi would be up to the task of transcoding on the fly files to make them compatible with your end device. After all, the RPi is equipped with a powerful-yet-modest Quad core Cortex-A72 (ARM v8) 64-bit SoC @ 1.5GHz.
  • After a few days of usage, I've noticed that when the server is, er, serving, the CPU gets a little warmer: about 10 Celsius above baseline. Doesn't worry me too much at this time.
  • In the twonky.sh file, there's a path for the PID file that the media server would create at runtime. I'm not sure if changing this to the new path is needed or not. I erred on the side of caution and did so anyways. The new path would read the same as what we added in the twonky.service unit file:
    • WORKDIR1="/usr/local/twonky"
      WORKDIR2="`dirname $0`"
      PIDFILE=/run/mediaserver.pid
      						
  • I had a weird experience with having a Wireguard VPN setup on the same RPi hosting Twonky. In Raspberry OS 32-bit, would be rebooting at least once per day (sometimes more). In Ubuntu 20.10 64-bit, Twonky would not crash but it would become unresponsive after a while. The logs were not very useful since they contains lots of extraneous information that was difficult to read through. All the more, since I didn't know how long the server would stand for before becoming unresponsive, the logs would be massive by the time it happened.
    I accidentally stumbled upon a simple solution for this issue: limit the network interfaces to which Twonky binds to. To do this, simply go to /var/twonky/twonkyserver/twonkyserver.ini and update the following line(s) and then bounce the service:
    • ...
      # Comma separated list of IP addresses to bind to (optionally can include a netmask e.g. 192.168.3.0|255.255.255.0 will match address range 192.168.3.x). Default: blank = all networks - only works with ipv4 addresses
      ip=0.0.0.0 # Optional. Replace this with your IP, if the RPi has a static IP.
      ...
      # Specifies a comma separated list of interface names to ignore
      ignoreiface=wg0 # The Wireguard interface name could be different for you. Adjust accordingly.
      ...
      						

    This stabilized the server and there have been no rogue reboots since the INIs were updated.
  • As of 2022-09-14, and probably before that, some clean installations of certain OS' stopped including the dependencies necessary for Twonky to run correctly. Based on the research done by @corneliu, the issue lays in a difference of architecture. Newer OS' are arm64 while Twinky is armhf. The dependencies that are not shipped by default anymore are the ones for libc6. Thank you @corneliu for your assistance!