Kodi and Advanced Mame on ODROID-XU4 - Part 1

This is a guide to setup Kodi with Mame, on the ODROID-XU4 SBC, making it a nice media and game center. It lists all the steps to install the needed software on Ubuntu Linux. Installing Mame is optional as well as configuring the USB drive, the joystick, etc. You can stop at the Kodi level or go further and have a fully functional video, music and game machine. Not all the steps are straightforward. Some steps may require consulting information online. I have done all the research for you and have provided resource links, so you have the information to understand necessary concepts. The first part on building Kodi is self-contained. The part on Mame will require external additional resources. The rest is self-contained again.

Install Linux and Kodi

To install all of this, follow these steps:

1. The first step is ideally done on another Linux box. If you do not have one yet, it is good to migrate to Linux as soon as you can. Download an image from: https://wiki.odroid.com/ODROID-XU4/os_images/linux/ubuntu_4.14/20181203

2. Ensure the image is valid with md5sum and compare the result with the corresponding md5sum file at https://odroid.in/ubuntu_18.04lts/XU3_XU4_MC1_HC1_HC2/.

$ md5sum ubuntu-18.04.1-4.14-mate-ODROID-XU4-20181203.img.xz
3. Assuming you are doing all of this on a Linux box, expand the file you downloaded:
$ xz -d ubuntu-18.04.1-4.14-mate-ODROID-XU4-20181203.img.xz
4. Plugin your SD card on an USB port of your Linux box.

5. Search for the device with lsblk.

$ lsblk
Note: Let us assume your SD card is on `/dev/sdX`. Be very careful to choose the correct device or you will risk erasing the content of another disk.

6. Write the image to the SD card.

$ sudo dd if=ubuntu-18.04.1-4.14-mate-ODROID-XU4-20181203.img.xz of=/dev/sdX bs=1M conv=fsync
$ sudo sync
7. Unmount your SD card either from your desktop environment or on the command line:
$ sudo umount /dev/sdX
8. Insert your SD card in your ODROID-XU4 and boot it up. Then login on the desktop with the credentials as given at: https://bit.ly/30syIGW

At this stage, you have a fully functional ODROID-XU4 desktop. The next step will be to install Kodi and remove this direct desktop access so that your box will directly boot on the Kodi interface more like a regular TV set.

9. Open a terminal (search in the menu on the top-left of the screen).

10. Update the packages:

$ sudo apt update
$ sudo apt upgrade
$ sudo apt dist-upgrade
During the update, the first time you boot your machine, you may get an error regarding a lock file with dpkg (`/var/lib/dpkg/lock`). It means that one of the automated update procedures is still running, which is very likely relevant to the new install. Just let it run until the end and try the commands again.  If you get a message about boot.ini being replaced, just select 'OK'. If you get a question about `/etc/apt-fast.conf`, answer Y (for Yes).

11. Install Kodi:

$ sudo apt install kodi kodi-bin kodi-data libcec4 python-libcec openbox
12. The Mali graphical driver is installed by default and works well. In a terminal run `glmark2-es2` and you should see a demo (an image of a horse). The FPS should be around 300 frames/second. Run `glmark2` and you should see the same demo. The FPS should be around 30 frames/second--terrible. The reason is because the Mali driver only supports OpenGL ES (Embedded System) and not plain OpenGL. The solution is to use a library called `gl4es` (https://github.com/ptitSeb/gl4es), which you can find on Github.

13. Install `gl4es`

In a terminal, install the following development tools:

$ sudo apt install git cmake xorg-dev gcc g++ build-essential
Get the source code of gl4es. So, gl4es is an implementation of OpenGL using OpenGL ES (instead of implementing the library directly). Therefore, for systems like ODROID-XU4 with a driver supporting only OpenGL ES, it is possible to use software based on OpenGL too with hardware acceleration:
$ git clone https://github.com/ptitSeb/gl4es.git
$ cd gl4es
Compile it:
$ mkdir build
$ cd build
$ cmake .. -DODROID=1
$ make
It will compile and display its progress with a lot of messages. Towards the end, if the compilation is successful, you should see the following:
...
[ 96%] Building C object src/CMakeFiles/GL.dir/glx/utils.c.o
[ 98%] Linking C shared library ../lib/libGL.so.1
[100%] Built target
...
Save the Mesa version of OpenGL (the one installed by default):
$ sudo mv /usr/lib/arm-linux-gnueabihf/libGL.so.1 /usr/lib/arm-linux-gnueabihf/save.libGL.so.1
$ sudo mv /usr/lib/arm-linux-gnueabihf/libGL.so.1.0.0 /usr/lib/arm-linux-gnueabihf/save.libGL.so.1.0.0
and copy your new compiled version:
$ sudo cp lib/libGL.so.1 /usr/lib/arm-linux-gnueabihf/
$ sudo ldconfig
Now check that everything is fine by running:
$ glxinfo | head -6
If you see the following, then you are good to go (of course, the date on the second line will be different for you):
LIBGL: Initialising gl4es
LIBGL: v1.1.1 built on Mar 28 2019 06:22:26
LIBGL: Using GLES 2.0 backend
LIBGL: loaded: libGLESv2.so
LIBGL: loaded: libEGL.so
LIBGL: Using GLES 2.0 backend
Now you can run glmark2 again:
$ glmark2
This time you should see FPS around 300 to 600. At this stage, your ODROID-XU4 also has full OpenGL support. You can even use software like Blender (https://www.blender.org/).

14. For security reasons, Kodi and its associated programs will be run by a user with limited privileges, with no password and automatic login. We will call this user, `kodi`:

$ sudo adduser --disabled-password --gecos "" kodi
The output will be:
Adding user `kodi' ...
Adding new group `kodi' (1000) ...
Adding new user `kodi' (1000) with group `kodi' ...
Creating home directory `/home/kodi' ...
Copying files from `/etc/skel' ...
15. Assign some privileges to this user:
$ sudo usermod -a -G cdrom,video,plugdev,users,dialout,dip,input,netdev,audio,pulse,pulse-access,games kodi
16. Add options to allow Kodi to start its own X server:
$ sudo sed -ie 's/allowed_users=console/allowed_users=anybody/g' /etc/X11/Xwrapper.config
$ sudo sed -ie "\$aneeds_root_rights = yes" /etc/X11/Xwrapper.config
17. Add the Kodi service to `systemd` (better to copy and paste the following rather than typing it):
$ cat << EOF | sudo tee /etc/systemd/system/kodi.service
 [Unit]
 Description = Kodi Media Center
 After = systemd-user-sessions.service network.target sound.target mysql.service dbus.service polkit.service upower.service
 Wants = mysql.service
 [Service]
 User = kodi
 Group = kodi
 Type = simple
 #PAMName = login # you might want to try this one, did not work on all systems
 ExecStart = /usr/bin/xinit /usr/bin/dbus-launch --exit-with-session openbox --startup /usr/bin/kodi-standalone -- :0 -nolisten tcp vt7
 Restart = on-abort
 RestartSec = 5

 [Install]
 WantedBy = multi-user.target
 EOF
A quick note on the first line:  the `sudo` command works on `tee` to write with super-user rights to the file, because if I would do it on `cat` and follow the same pattern as the other commands in this document, the redirection to the file would not work. Indeed, the redirection is done by the shell (belonging to the user `odroid`) and not by the command. So, `sudo cat` would not work.

18. Enable Kodi at boot time:

$ sudo systemctl enable kodi
$ sudo systemctl set-default multi-user.target
19. On the Hardkernel Wiki (https://bit.ly/2Qjvcdf), it is said that a recent Canonical EGL package blocked access to the Mali GPU. It is quite true. The fix is:
$ sudo apt install mali-x11 --reinstall
At the time of this publication, it fixes the loss of Mali GPU access bug. So it is highly recommended to apply this fix. In fact, if you update the system as I said before, you will have the bug and you will fix it with the reinstall `mali-x11` as above.

20. Now you have a fully functional Kodi system. If you want to reboot, your ODROID-XU4 will directly start into Kodi. If you want to add more features, like MAME (https://www.mamedev.org, https://en.wikipedia.org/wiki/MAME), an external USB drive, a joystick, a firewall, you can follow the rest of this article.

If you want to stop and enjoy your ODROID-XU4 now, you can reboot it:

$ sudo reboot
Everything in the following sections can be done either by connecting to your ODROID-XU4 with `ssh` or on screen, directly (you will need a keyboard). If you want to connect on the screen directly, after rebooting your ODROID-XU4 on Kodi, you can press `Ctrl+Alt+F1` to switch to a terminal (text screen). At any time, you can go back to Kodi by pressing `Alt+F7`.

Install an USB external drive

1. Switch to a terminal or login with `ssh`.

2. Install `usbmount` to automount USB drives:

$ sudo apt install usbmount
There is a bug in the version 0.0.22 of `usbmount` as provided by the stock Ubuntu Linux when connecting 2 USB drives at the same time. The bug has been fixed with `usbmount 0.0.24` which is not yet on the Ubuntu repository. You can upgrade it manually, if you like.

If your second drive (or even the first) does not mount properly when you plug it in, you can try to upgrade `usbmount` as follows:

$ git clone https://github.com/rbrito/usbmount.git
$ cd usbmount
$ sudo apt-get update && sudo apt-get install -y debhelper build-essential fakeroot
$ dpkg-buildpackage -us -uc -b
$ cd ..
$ sudo dpkg -i usbmount_0.0.24_all.deb
3. By default, usbmount will mount the external USB drive with the `sync` option. `sync` means that all write access to the disk will be immediately flushed to the disk. It can dramatically slow down all the disk operations.
$ sudo sed -ie 's/^MOUNTOPTIONS=.*/MOUNTOPTIONS="noexec,nodev,noatime,nodiratime"/' /etc/usbmount/usbmount.conf
I assume it is safe to use the `async` option here because you're not supposed to unplug this disk at any time. When I did that, the write speed of my external hard drive has been multiplied by 10.

Install MAME to play arcade games from Kodi

This section is long and will require external resources if you want to have all the bells and whistles of a full-featured MAME installation. From time to time, we will refer to external guides too.

1. Install MAME

$ sudo apt install mame mame-data mame-doc mame-extra mame-tools libsdl2-gfx-1.0-0 libsdl2-image-2.0-0 libsdl2-mixer-2.0-0 libsdl2-net-2.0-0 libsdl2-net-2.0-0 python-pil
Create a new config file with the correct paths, using OpenGL ES 2, the ALSA audio driver, tuning the speed and using all the cores:
$ cat << EOF | sudo tee /etc/mame/mame.ini
 inipath $HOME/.mame;/etc/mame
 rompath /home/kodi/AML-ROMs/
 samplepath /home/kodi/AML-assets/samples/
 ctrlrpath /home/kodi/AML-assets/ctrlr
 hashpath /usr/share/games/mame/hash
 cfg_directory $HOME/.mame/cfg
 nvram_directory $HOME/.mame/nvram
 memcard_directory $HOME/.mame/memcard
 input_directory $HOME/.mame/inp
 state_directory $HOME/.mame/sta
 snapshot_directory $HOME/.mame/snap
 diff_directory $HOME/.mame/diff
 comment_directory $HOME/.mame/comments
 video auto
 render opengles2
 audiodriver alsa
 samplerate 32000
 numprocessors 8
 mouse 1
 uimodekey INSERT
 autoframeskip 1      
EOF
$ sudo chmod ugo+r /etc/mame/mame.ini
Then we need to remove the `gl4es` startup message to make MAME happy. Long story short: a Kodi plugin will extract the games' database `MAME.xml` to the standard output by running `mame`. When `mame` starts, `gl4es` is initialized and displays a nice message, like the one above. However, the Kodi plugin captures the standard output which is supposed to be an XML file, except that we have this welcome message on top of it from `gl4es`. So, when Kodi tries to read the XML file (in fact a Python library tries, too), it fails:
$ cat << EOF | sudo tee /usr/local/bin/mame
 #!/bin/sh
 export LIBGL_SILENTSTUB=1
 export LIBGL_NOBANNER=1
 /usr/games/mame "$@"
 EOF
$ sudo chmod ugo+x /usr/local/bin/mame
When configuring the AML add-on in Kodi, we will use this new mame command we have just created

2. Install and configure Advanced Mame Launcher plugin from Kodi

We are going to follow the official guide of a Kodi plugin called, Advanced Mame Launcher (AML) (https://forum.kodi.tv/showthread.php?tid=304186) and adapt a few steps to follow our setup. Here, I assume that we have an external USB drive connected to the ODROID-XU4. It will be helpful to store data for MAME.

Assuming you are on a text terminal (see above on how to switch to a text terminal from Kodi), the first step is to create directories to store MAME data. The external USB drive is mounted to `/media/usb0` and by default to `/media/usb,` too. We will assume this configuration is the case from now on.

$ cd /media/usb
$ sudo mkdir mame
$ sudo chown kodi.kodi mame
$ sudo su - kodi
$ cd /media/usb/mame
$ mkdir -p AML-ROMs AML-CHDs AML-SL-ROMs AML-SL-CHDs AML-assets/samples/
$ cd AML-assets
$ mkdir artpreviews artwork cabinets clearlogos covers_SL cpanels fanarts fanarts_SL flyers manuals manuals_SL marquees PCBs snaps snaps_SL titles titles_SL videosnaps videosnaps_SL
$ cd
$ ln -s /media/usb/mame/* .
$ exit
Then we need to fill in the directories with the latest data for Mame as described in https://forum.kodi.tv/showthread.php?tid=304186. We use the following script to download and install everything automatically:
$ sudo su - kodi
$ cd /media/usb/mame/AML-assets
# catlist.ini catver.ini genre.ini genre_OWS.ini mature.ini not_mature.ini
$ wget http://www.progettosnaps.net/catver/ -q -O - | grep 'download?tipo=catver' | sed "s#.*href=\"\(.*\.zip\)\".*#wget -q 'http://www.progettosnaps.net\1' -O file.zip#"|sh
$ unzip -jq file.zip '*.ini'
$ rm file.zip
# nplayers.ini
$ wget http://nplayers.arcadebelgium.be/ -q -O - | grep -E 'nplayers[[ :digit: ]]{4}\.zip' | sed "s#.*href=\"\(http://nplayers.*zip\)\">.*#wget -q '\1' -O file.zip#" | sh
$ unzip -jq file.zip nplayers.ini
$ rm file.zip

# bestgames.ini
$ wget http://www.progettosnaps.net/bestgames/ -q -O - | grep 'download?tipo=bestgames' | sed "s#.*href=\"\(.*\.zip\)\".*#wget -q 'http://www.progettosnaps.net\1' -O file.zip#"|sh
$ unzip -jq file.zip '*.ini'
$ rm file.zip

# series.ini
$ wget http://www.progettosnaps.net/series/ -q -O - | grep 'download?tipo=series' | sed "s#.*href=\"\(.*\.zip\)\".*#wget -q 'http://www.progettosnaps.net\1' -O file.zip#"|sh
$ unzip -jq file.zip '*.ini'
$ rm file.zip

# history.dat
$ wget https://www.arcade-history.com/?page=download -q -O - | grep -o 'href="[^"]*\.zip"' | sed 's#href=\"\.\.\(.*zip\)\"#wget https://www.arcade-history.com\1 -q -O file.zip#'|sh
$ unzip -jq file.zip history.dat

# mameinfo.dat
$ wget 'http://mameinfo.mameworld.info' --header="User-Agent: Firefox/70.0" -q -O - |grep -o 'href=\"[^"]*Mameinfo.*\.zip"'|sort|tail -1| sed 's#href=\"\(.*zip\)\"#wget --header=\"User-Agent: Firefox/70.0\" \1 -q -O file.zip#'|sh
$ unzip -qjp file.zip *.7z > mameinfo.7z
$ 7z e '-i!mameinfo.dat' mameinfo.7z > /dev/null
$ rm file.zip mameinfo.7z

# gameinit.dat
$ wget http://www.progettosnaps.net/gameinit/ -q -O - | grep 'download?tipo=gameinit' | sed "s#.*href=\"\(.*\.zip\)\".*#wget -q 'http://www.progettosnaps.net\1' -O file.zip#"|sh
$ unzip -jq file.zip english/gameinit.dat
$ rm file.zip

# command.dat
$ wget http://www.progettosnaps.net/command/ -q -O - | grep 'download?tipo=command' | sed "s#.*href=\"\(.*\.zip\)\".*#wget -q 'http://www.progettosnaps.net\1' -O file.zip#"|sh
$ unzip -jq file.zip Longhand/command.dat
$ rm file.zip
$ exit
3. Modify `mame.ini` to reflect the new directory structure:
$ sudo sed -i 's#^rompath \+.*$#rompath /home/kodi/AML-ROMs/#' /etc/mame/mame.ini
$ sudo sed -i 's#^samplepath \+.*$#samplepath /home/kodi/AML-assets/samples/#' /etc/mame/mame.ini
4. Add assets.

Go back to Kodi: type 'Ctrl+D' on your terminal, then 'Atl+F7' to go back to Kodi.

In https://forum.kodi.tv/showthread.php?tid=304186, you can follow the paragraph called “Setting up MAME assets and Software List assets” to add more resources and assets. This is the way to get extra pictures, logos, etc. You can read the page at http://forum.pleasuredome.org.uk/index.php?showtopic=30715 about the MAME Extra packages to understand all the types of resources you can find on the Net for Mame.

On the above mentioned page, you can find links to Mame resources on the Internet. They come in huge packages you simply have to move to `/media/usb/mame,` that we created before. Let me give you some examples about those packages, which can be useful in the process.

If you find a package called `MAME 0.xxx EXTRAs` where `xxx` is a Mame version number, go into the directory you have downloaded. In this directory, you will find more directories. Move their content to the corresponding directory in `/media/usb/mame/AML-assets/`

Another similar package might be called something like `MAME 0.xxx Multimedia`. You should process it the same way

However, you will find `.zip` files too. You can simply unzip them and move them to the final directory as above. Some directories do not have the same name, so keep the names we created above. In this case, transfer the content of the directory instead of the directory itself.

If you find packages with `bios-devices` in their name you do not need them if you already downloaded the very big packages with `ROMs` in their name. They are just a subset for a different situation. You can read more about all those packages at: http://forum.pleasuredome.org.uk/index.php?showtopic=34705. These packages are made when you want a minimal version of the ROMs; for example, if you are running short on disk space.

There are packages called Rollbacks Roms too (https://bit.ly/2HL6Bdt). You only need them if you have a ROM manager for Mame which can deal with multiple versions. You will not need them in this tutorial.

There are the Software List ROMs and CHDs. Obviously, their content will go into the `/media/usb/mame/AML-SL-ROMS` and `/media/usb/mame/AML-SL-CHDs` directories we have created before. `SL` stands for `Software List`.

Finally, you might have messed up a bit with users' permission when downloading and moving files. So you want to make things well by assigning the `kodi` user to the `mame` directory:

$ sudo chown -R kodi.kodi /media/usb/mame
5. Install and configure the AML plugin. You will find it in Program Adds-on. It is called _Advanced Mame Launcher_.

In Kodi, go to **Settings**, **Addon settings**, **Install from repository**. In **Program add-ons**, look for **Advanced Mame Launcher** and install it. Follow this picture:

Installating the Advanced Mame Launcher

After it is installed, right-click on the add-ons logo and go to _Settings_:

Advanced Mame Launcher settings selection

Change the paths to the executable file and the data directories as shown on this picture:

Updating the paths in the Advanced Mame Launcher settings

Go back to Kodi's initial screen and look for the AML plugin, in general **Add-ons**, **Program Add-ons**, **Advanced Mame Launcher**.  Select any row, open the context menu with a right-click and select **Setup plugin**.  Run a full setup and configuration of the plugin by choosing the _All in one step_ options in the context menu of the plugin.

Choosing the All In One Step option in the Advanced Mame Launcher plugin

This step can take several minutes to an hour. You will see a lots of progress bars. If you did everything well before, it should work without error messages. The plugin is now configured and ready, and Mame is now ready to be used.

In the next installment of this article, we will look at how to install a joystick. For comments, questions, and suggestions, please visit the rogiainl article at https://forum.odroid.com/viewtopic.php?f=52&t=34760.

Be the first to comment

Leave a Reply