Raspberry Pi running Active Directory, using Samba on FreeBSD.

Kodi on a Raspberry Pi

Building a Media Player:
Kodi, Linux, and the Raspberry Pi

Kodi is free, open-source media player software. It was developed by the XBMC Foundation, as the software was originally named Xbox Media Center or XBMC. Now the software's name has been changed to Kodi.

OSMC or Open Source Media Center is the combination of Kodi running on Debian Linux.

Raspberry Pi is a physically small, low-cost, low-power computer. The single-board computer itself retails for US$ 30-35, you also need a microSD memory card, a smart phone charger, and an HDMI cable. The total cost is about like one month's basic cable bill.

These are my notes from just starting to experiment with Kodi / OSMC / Linux / Raspberry Pi.

Raspberry Pi hardware

Let's start at the bottom, with the hardware, and work our way up. This is a Raspberry Pi 3 that I used for this project, as opposed to the model 1 shown in the banner at the top of the page.

Raspberry Pi 3: power, HDMI, audio/video connectors.
Raspberry Pi 3: Ethernet, USB, power, HDMI, audio/video connectors.

The Raspberry Pi 3 is based on a Broadcom BCM2387 SoC or System-on-Chip that includes the CPU, RAM, and GPU. The CPU is a 1.2 GHz 64-bit quad-core ARM Cortex-A53. There is 1 GB of RAM. The RAM is shared with the GPU, a Broadcom VideoCore IV.

Peripherals include a 10/100 Mbps Ethernet port, four USB 2.0 ports, HDMI, a composite audio/video output, and 17 general-purpose input/output lines.

The SoC and the chip with the USB and Ethernet controllers can run pretty hot, so CanaKit bundles a Raspberry Pi 3 with two small heatsinks, a 2500 mA power supply, and a clear case.

You will also need a microSD memory card, a USB keyboard, and an HDMI cable. I found a 25-foot Amazon house-brand HDMI cable that was much less per foot than an HDMI cable from Walmart.

Raspberry Pi 3, top view of main board.
Raspberry Pi 3, top view of main board with heatsinks installed.
Raspberry Pi 3 enclosed in acrylic case.
Raspberry Pi 3 enclosed in acrylic case.

Debian Linux and OSMC

Debian derivatives of Linux like Raspbian are the most common operating system on Raspberry Pi platforms. Others exist, see my project to run Active Directory with Samba on the FreeBSD operating system.

OSMC has been optimized to boot very quickly. Its graphical interface is up and running within a few seconds.

Download
OSMC

I downloaded OSMC from the main site. It comes down as a gzip-compressed file which expands to 256 MB. List your storage devices /dev/sd* and then plug in a card reader with your microSD card. Then list the devices /dev/sd* again and see which disk is new. I did this on a system with a lot of storage, disks /dev/sda through sdf, so the new disk was sdg with its partition sdg1.

Be very careful to determine which disk device is the newly added microSD one, as an error here can wipe out all of your data. Change the image file name and the device sdX in the following as needed.

# dd if=OSMC_TGT_rbp2_YYYYMMDD.img of=/dev/sdX bs=10M 

Plug in an Ethernet cable and its power supply, and let it boot up. Now you're ready to explore Kodi!

Kodi

The interface is pretty intuitive. A mouse isn't needed, one can't even be used. Navigation is with the arrow keys on the keyboard, <Enter> selects the highlighted item, <Escape> backs up one level or returns to the top. Remember that .. takes you up one level, the same as in most any computer operating system.

It walks you through some basic setup the first time. You can go back through the Settings menu to change things.

Keyboard
controls

You interact with Kodi through the keyboard, there are many keyboard commands. Immediately useful ones include:
Up/Down, PageUp/PageDown, and Home/End to navigate menus;
<Enter> to select the highlighted item;
<Escape> to return to the previous menu or to go to the main menu;
<Space> to pause and unpause;
x to stop what's playing;
and to seek backward and forward 10 seconds (and two together for 30 seconds, three for a minute, four for 3 minutes);
and + to decrease and increase the volume.

Add-ons

I have just started to look through some of the many add-ons that come with Kodi. Some of this is really arcane, like VimCasts, "Screencasts about Vim, the text editor".

Cinepub.ro has some Romanian films. Or at least it's a Romanian site with some movies.

Comet TV Live works, seems to be what's carried as a secondary channel on some terrestrial broadcast and picked up on cable.

MeTV works, it's an archive of old TV shows.

MyTV.bg (Bulgarian) needs an account.

NASA TV works. It has NASA live programming, and a live feed looking down from the International Space Station.

NHK Newsline works, it has English-language 30-minute news programs about Japan.

PBS Think TV lists a lot of shows, but all that I tried took me to choices of 0 Episodes, 0 Clips, and 0 Previews.

RTK has live streams of TV from Kosovo. RTK 1 is Balkan sitcoms, news, and other programming. RTK 2 is music videos at times, documentaries at other times. RTK 3 has been down most of the times I have tried. RTK 4 is music videos. Wikipedia's explanation of Radio Television of Kosovo says that RTK 2 focuses on minorities, RTK 3 is a news channel, and RTK 4 is arts and documentaries. Some of the music is like Eurovision, some is a guy in a fez playing an oud. You see, the fez was outlawed in the Republic of Turkey, but it lives on in parts of the former Ottoman Empire.

RTS - Emissions TV (French) works.

Smithsonian Channel works, it has an archive of 80 or so documentaries.

SyFy has episodes of programs created for that network.

TV Viet Nam has some Vietnam Television channels — VTV1, VTV2, VTV3, VTV4, VTV6, and VTV9 plus several other Vietnamese channels, 21 in total.

USTVNow is aimed at U.S. military overseas. For that, it would be nice. You register an e-mail address, and the first time you try to access it via Kodi you need to enter the address and password. It's 7 broadcast stations from central Pennsylvania, streamed live:

ABC WHTM Harrisburg PA (not working when I tested)
CBS WHP Harrisburg PA
CW WHP-DT3 Harrisburg PA
FOX WPMT York PA
MyTV WHP 21.2 Harrisburg PA (not working when I tested)
NBC WGAL Lancaster PA
PBS WPSU State College PA

The PBS station is noticeably fuzzy, with very limited frames per second at times. At times it's like it's live Desert Storm coverage over satellite phones again. But... it only needs about 0.6 to 1.2 Mbps network bandwidth. (compare that to other measurements below)

The FOX station is a little fuzzy with some artifacts, but it's not bad at all.

The others are quite good.

WABC, WCBS, and WNBC all work, they are the New York broadcast stations of the three major networks. All have about 75-100 shows. Some are current shows with their most recent 5 episodes, some are older shows going back at least to the mid 1980s, to the 1960s in a few cases. Most of the older ones are something like the first 5 to 10 episodes of the first season.

An exception is WNBC and Miami Vice, which has 111 episodes from Season 1 / Episode 1 (09/16/1984) through Season 5 / Episode 22 (01/25/1990), all but one episode according to Wikipedia.

Several Kodi add-ons are archived at comparitech.com.

This page says that you can put one of those zip files in ~osmc and then install it by going through the menus: System → Settings → Add-ons and then selecting Install from zip file.

Going deeper: Raspberry Pi 3 CPU

The CPU is detected as an ARMv7 because the provided kernel only supports 32-bit mode. With a 64-bit kernel, it would be detected as an ARMv8. I think this is because of the entries shown below from /boot/config-4.4.27-7-osmc.

$ lscpu
Architecture:          armv7l
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    4
Socket(s):             1
Model name:            ARMv7 Processor rev 4 (v7l)
CPU max MHz:           1200.0000
CPU min MHz:           600.0000
$ egrep 'CONFIG_CPU' config-4.4.27-7-osmc | less
CONFIG_CPUSETS=y
CONFIG_CPU_V7=y
CONFIG_CPU_32v6K=y
CONFIG_CPU_32v7=y
CONFIG_CPU_ABRT_EV7=y
CONFIG_CPU_PABRT_V7=y
CONFIG_CPU_CACHE_V7=y
CONFIG_CPU_CACHE_VIPT=y
CONFIG_CPU_COPY_V6=y
CONFIG_CPU_TLB_V7=y
CONFIG_CPU_HAS_ASID=y
CONFIG_CPU_CP15=y
CONFIG_CPU_CP15_MMU=y
^C
$ cat /proc/cpuinfo
processor       : 0
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt 
vfpd32 lpae evtstrm crc32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

[... identical processors 1-3 deleted ...]

Hardware        : BCM2709
Revision        : a22082
Serial          : 00000000f0036b37

Going deeper: Debian Linux and OSMC user environment

Run ip neigh on the DHCP server to find its assigned IP address and MAC address. I modified /etc/dhcpd.conf on the server to assign it a specific IP address, modified the DNS server to associate a hostname with that IP address, and reloaded the DHCP and DNS servers. Then I restarted the Raspberry Pi, and it came up with a known IP address I could refer to by name.

Connect in over SSH. The user name is osmc with a password of osmc.

The user osmc can use sudo bash to get an interactive session as root.

I used a 32 GB microSD card, which was way more than enough:

$ df -hT
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/mmcblk0p2 ext4   29G  859M   27G   4% /
$ uname -a
Linux kodi 4.4.27-7-osmc #1 SMP PREEMPT Tue Jan 24 03:38:31 UTC 2017 armv7l GNU/Linux

OSMC's design is focused on graphical use. It lacks basic command-line tools like file, tree, bc, and vim (although of course vi is there). But you can easily add any Debian packages with apt-get.

Going deeper: Kodi configuration and add-ons

The system settings are saved under ~osmc/.kodi/ with subdirectories addons, media, system, temp, and userdata.

The Add-ons specify streams from the Internet. Each add-on creates a hierarchy under ~osmc/.kodi/addons/, like this one for the Smithsonian Channel. An XML file defines the stream itself.

$ cd ~osmc
$ tree -F .kodi/addons/plugin.video.smithsonian/
/home/osmc/.kodi/addons/plugin.video.smithsonian/
|-- LICENSE.txt
|-- README.txt
|-- addon.xml
|-- changelog.txt
|-- default.py
|-- fanart.jpg
|-- icon.png
|-- lib/
|   |-- __init__.py
|   |-- __init__.pyo
|   |-- scraper.py
|   `-- scraper.pyo
`-- resources/
    |-- language/
    |   `-- English/
    |       `-- strings.xml
    `-- settings.xml

4 directories, 13 file

$ cat .kodi/addons/plugin.video.smithsonian/addon.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="plugin.video.smithsonian"
       name="Smithsonian Channel"
       version="3.0.1"
       provider-name="t1m">
  <requires>
    <import addon="xbmc.python" version="2.20.0"/>
    <import addon="script.module.t1mlib" version="1.0.14"/>
  </requires>
  <extension point="xbmc.python.pluginsource"
            library="default.py">
        <provides>video</provides>
  </extension>
  <extension point="xbmc.addon.metadata">
    <summary lang="en">Smithsonian Channel Video Addon</summary>
    <description lang="en">Play 1080 HD videos from the Smithsonian Channel.</description>
    <disclaimer lang="en">Feel free to use this script. For information visit the wiki.</disclaimer>
    <platform>all</platform>
    <language>en</language>
    <license>GNU GENERAL PUBLIC LICENSE. Version 2, June 1991</license>
    <forum>http://forum.kodi.tv/showthread.php?tid=214784</forum>
    <website>www.smithsonianchannel.com</website>
    <email></email>
    <source>https://github.com/learningit/plugin.video.smithsonian/</source>
  </extension>
</addon> 

Going deeper: CPU and network load

My Comcast connection provides about 22 Mbps down and 5.7 Mbps up. HD streams like Agents of Shield from WABC take about 3.0-4.5 Mbps, about 14-20% of my download bandwidth. Something like a mid-1980s Miami Vice episode streamed from WNBC takes about half of that. Lo-fi RTK video, even less.

Linux tool iptraf-ng showing network traffic rate.

iptraf-ng showing traffic rate on exterior interface of network gateway while streaming a current HD program from WABC.

The kodi.bin process listens on TCP port 8080 with a web-based interface like what you have on your HDMI-connected TV. Point your browser to http://kodi:8080/, or its IP address if you don't have DNS set up.

# lsof -i
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
[... several lines deleted ...]
kodi.bin  333     osmc   23u  IPv4   8788      0t0  TCP *:1824 (LISTEN)
kodi.bin  333     osmc   24u  IPv6   8776      0t0  TCP *:http-alt (LISTEN)
kodi.bin  333     osmc   31u  IPv4   8796      0t0  TCP *:36666 (LISTEN)
kodi.bin  333     osmc   32u  IPv4   8780      0t0  TCP *:http-alt (LISTEN)
kodi.bin  333     osmc   33u  IPv4   8785      0t0  UDP *:1900 
kodi.bin  333     osmc   39u  IPv4   8792      0t0  UDP *:4428 
kodi.bin  333     osmc   42u  IPv6   8795      0t0  TCP osmc:9090 (LISTEN)
kodi.bin  333     osmc   43u  IPv4   6128      0t0  UDP osmc:9777 
kodi.bin  333     osmc   44u  IPv6   8797      0t0  TCP *:36666 (LISTEN)
kodi.bin  333     osmc   46u  IPv6   8798      0t0  TCP *:36667 (LISTEN)
[... several lines deleted ...]

The CPU load isn't bad at all. Here's a snapshot taken while playing an HD stream. The Kodi program is using about 23% of one CPU core. For a mid-1980s TV episode the CPU use drops to about 15.3%. Remember that this is a four-core CPU, so this is only about 23% of one of the four cores, under 6% of total CPU resources. The other three CPU cores are almost completely idle.

$ top -d 10
top - 20:09:42 up  5:00,  2 users,  load average: 0.40, 0.40, 0.31
Tasks: 132 total,   1 running, 131 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.5 us,  1.1 sy,  0.2 ni, 93.1 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
KiB Mem:    751024 total,   466592 used,   284432 free,    37592 buffers
KiB Swap:        0 total,        0 used,        0 free.   248484 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  312 cromwell  20   0  611712 152744  48480 S  22.9 20.3  57:28.87 kodi.bin
 1150 root      20   0    3068   1656   1292 S   0.8  0.2   0:28.35 top
   76 root       1 -19       0      0      0 S   0.7  0.0   1:46.59 VCHIQ-0
   87 root      10 -10       0      0      0 S   0.3  0.0   0:38.37 SMIO
 3135 root      20   0    3072   1700   1388 R   0.3  0.2   0:00.07 top
    3 root      20   0       0      0      0 S   0.1  0.0   0:10.01 ksoftirqd/0
  236 avahi     20   0    3372   2016   1780 S   0.1  0.3   0:05.73 avahi-daem+
  240 root      20   0    3300   1404   1260 S   0.1  0.2   0:03.97 irqbalance
  798 cromwell  20   0   10772   2828   2140 S   0.1  0.4   0:01.17 sshd
 1170 root      20   0       0      0      0 S   0.1  0.0   0:01.42 kworker/0:1
 2123 root      20   0       0      0      0 S   0.1  0.0   0:00.12 kworker/3:1
 3111 root      20   0       0      0      0 S   0.1  0.0   0:00.02 kworker/u8+
    1 root      20   0   23048   3224   2136 S   0.0  0.4   0:03.51 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:+

Going deeper: NFS auto-mounting

I added a line, highlighted in yellow below, to automount an NFS share with video files. This does not automatically mount the file system at boot time, as OSMC will come up much faster than the server. It automatically mounts the file system when you try to access it.

# cat /etc/fstab 
/dev/mmcblk0p1  /boot    vfat     defaults,noatimenoauto,x-systemd.automount    0   0
# rootfs is not mounted in fstab as we do it via initramfs. Uncomment for remount (slower boot)
#/dev/mmcblk0p2  /    ext4      defaults,noatime    0   0
myserver:/home/video  /home/osmc/video  nfs  ro,noauto,x-systemd.automount  0 0
Parameter Meaning
myserver:/home/video The NFS server is myserver, it is exporting or sharing its /home/video area of its file system.
/home/osmc/video That shared file system is connected as /home/osmc/video. Create an empty directory by that name.
ro Mount the shared file read-only.
noatime Don't update access times in the file system. This improves performance, as has already been done for the root file system. However, notice that we did not specify this for the NFS mount, as the Linux NFS client by default does not update file and directory access times. We could include noatime,nodiratime but that would have no effect.
See man nfs for details.
noauto Don't automatically mount this at boot time.
x-systemd.automount Get systemd to automatically mount this when needed. It will probably take a reboot for systemd to pick this up.
0 0 Don't make backups, and don't run fsck.

Now we can test the mounting.

$ ls /home/osmc/video
[... lots of output ...]
$ df -hT
Filesystem            Type      Size  Used Avail Use% Mounted on
devtmpfs              devtmpfs  362M     0  362M   0% /dev
tmpfs                 tmpfs     367M  5.0M  362M   2% /run
/dev/mmcblk0p2        ext4       29G  917M   27G   4% /
tmpfs                 tmpfs     367M     0  367M   0% /dev/shm
tmpfs                 tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs                 tmpfs     367M     0  367M   0% /sys/fs/cgroup
myserver:/home/video  nfs4      3.7T  2.0T  1.7T  55% /home/osmc/video
/dev/mmcblk0p1        vfat      240M   21M  219M   9% /boot
tmpfs                 tmpfs      74M     0   74M   0% /run/user/1000
$ mount | grep nfs
myserver:/home/video on /home/osmc/video type nfs4 (ro,noatime,vers=4.0,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.1.1.236,local_lock=none,addr=10.1.1.100) 
Ethernet, TCP, and NFS Performance Tuning

Notice that by default it uses NFSv4 over TCP with 1 MB read and write requests, the maximum size. So, it's already tuned for performance at the NFS layer.

Going deeper: Logging

Logs are kept in ~osmv/.kodi/temp/kodi.log. Here is Kodi failing to play an episode of Twin Peaks from WCBS:

15:05:06.002 T:1958847408  NOTICE: VideoPlayer: Opening: rtmp://cp48590.edgefcs.net/ondemand/?auth=daEcJatcwaDchb8buchdEcJd2cOazcfcPdD-bySEnX-h0-J9FOeUsf&aifp=v001&slist=video/temp_hd_gallery_video/CBS_Production_Outlet_VMS/video_robot/CBS_Production_Entertainment/2012/09/13/41708222/ playpath=mp4:video/temp_hd_gallery_video/CBS_Production_Outlet_VMS/video_robot/CBS_Production_Entertainment/2012/09/13/41708222/CBS_TWINPEAKS_001_CONTENT_CIAN_385707_1928.mp4 swfurl=http://canstatic.cbs.com/chrome/canplayer.swf swfvfy=true timeout=90
15:05:06.002 T:1958847408 WARNING: CDVDMessageQueue(player)::Put MSGQ_NOT_INITIALIZED
15:05:06.003 T:1365242864  NOTICE: Creating InputStream
15:05:06.006 T:1365242864  NOTICE: Creating Demuxer
15:05:06.112 T:1365242864   ERROR: OpenDemuxStream - Error creating demuxer
15:05:06.112 T:1365242864  NOTICE: CVideoPlayer::OnExit()
15:05:06.112 T:1958847408   ERROR: Playlist Player: skipping unplayable item: 0, path [plugin://plugin.video.wral/?mode=GV&url=%2Fshows%2Ftwin_peaks%2Fvideo%2F723246501%2Ftwin-peaks-traces-to-nowhere%2F]
15:05:06.120 T:1958847408  NOTICE: CVideoPlayer::CloseFile()
15:05:06.120 T:1958847408  NOTICE: VideoPlayer: waiting for threads to exit
15:05:06.120 T:1958847408  NOTICE: VideoPlayer: finished waiting
15:05:06.120 T:1958847408  NOTICE: CVideoPlayer::CloseFile()
15:05:06.120 T:1958847408  NOTICE: VideoPlayer: waiting for threads to exit
15:05:06.120 T:1958847408  NOTICE: VideoPlayer: finished waiting

Going deeper: Playing ISO images despite Cinavia and AACS

If you make an image of a DVD with a tool like K3b or dvdbackup, you get a UDF file system within a file. So, not really an ISO image, but a UDF image. We have gotten into the habit of saying "ISO image" for a bit-for-bit copy of media, in spite of the fact that a DVD and its image contain a UDF file system instead of an ISO-9660 file system. But anyway...

Let's say that you make a copy of a DVD that you own, so you can take the copy on a trip without worrying about losing or damaging the original. You cannot always play the resulting DVD media in a player. For some titles, after 20 minutes the audio mutes and an on-screen message appears.

This is because of the Cinavia steganography and analog watermarking used on some DVDs. Firmware support for Cinavia became mandatory for Blu-ray players in 2012. The copied disk will lack the AACS or Advanced Access Content System key stored within the Volume ID and not copied when duplicating the disk content. While the Cinavia/AACS combination was done to prevent Blu-ray copying, the technology is used in some DVDs.

Kodi can play an ISO image of a DVD, but it will not detect the Cinavia steganographic watermarking. So, it plays images you can't burn onto optical media and play.