Friday, June 29, 2018

Mac, Linux or BSD

The eternal question:
Which is better - EMACS or Vi?
OK, this post is actually about the other eternal question!
As I use Linux, Mac, Open and Free BSD (Yes, and that other ball of wax too...), I think I can answer objectively:
Both OpenBSD and FreeBSD are reasonably easy to download, install and run on pretty much anything. At least, I have not found a server/desktop/laptop computer that it would not run on.  I even ran OpenBSD on a Sparc Station - remember those?

OpenBSD

Theo De Raadt has a 'cut the nonsense' mentality so OpenBSD is simpler, with a smaller repository of programs, about 30,000 packages. However, with a little effort, you can install FreeBSD software on OpenBSD to get the rest. After a few days of use, you will know how.

The best OpenBSD book is Absolute OpenBSD: UNIX for the Practical Paranoid.

In general, OpenBSD feels a lot like Slackware Linux: Simple and very fast.

FreeBSD

FreeBSD can also with some effort, run Linux programs and you can use a virtualizer to run other systems, so you are never locked into one thing.

FreeBSD has a gigantic repository of about 50,000 programs and it has very good documentation in the online FreeBSD Handbook.

MacOS

Compared to OpenBSD, Dragonfly and Slackware, some distributions look fancy and are very slow - there are many reasons why - see below.  MacOS obviously falls into the fancy and slow category. So if you want a Mac replacement then you first need to decide whether you want a fancy or a fast system.
My preference is to install a reasonably fast system on the host, then use a virtualizer for experiments and work and I frequently run multiple systems at the same time.  All the BSDs are good for that, be it Open, Free or Mac.
My home use system is a Macbook Pro running the latest MacOS with the Macports and Homebrew software repositories.  I even have the XFCE desktop installed, so when I get annoyed with the overbearing Mac GUI, I run XFCE, to get a weirdly satisfying Linux-Mac hybrid.


Linux

Linux is the step child of UNIX, which took over the world.  Of the Top 500 Super Computers, all now run Linux.  My work engineering system is an ancient Dell T420 running the latest Fedora Linux on the host.  All my machines have Virtualbox and a zoo of virtual machines for the rest.

Note that the Mandatory Access Control security systems on Red Hat and Debian distributions slow them down a lot (in the order of 50%).  If you have to have a fast and responsive system and can afford to trade it for security, then turn SELinux or AppArmor off.

Latency

For the control and remote sensing systems of robots, aircraft and rockets, the worst case OS latency matters very much.  For low latency, nothing beats Linux, since the whole kernel and all spinlocks are pre-emptible.

On average, all OS's have the same interrupt service latency - a few tens of nanoseconds.  However, every once in a while, the latency will be much worse.  In the case of Linux, the worst case will be below 1 ms, but for Win10, it can can be 20 ms and for Win7, 800 ms.  The trouble in robotics and remote sensing, is that you need to design for the worst case.

I have observed a dotNet video player on Windows 7, after a couple days of uptime, stop dead for two seconds, every 8 seconds - obviously not good for a remote sensing application.  Windows 10 latency is much improved, though still a little worse than Mac OS, which has 2 orders of magnitude worse latency than Linux - see below for why this is.

See this very good real-time performance analysis: https://ennerf.github.io/2016/09/20/A-Practical-Look-at-Latency-in-Robotics-The-Importance-of-Metrics-and-Operating-Systems.html

(Intentional performance degradation was worst in Windows Vista and has been dialled back a bit since).

What is the Performance Problem with Windows and Mac OS?

It is not that MS and Apple don't know how to make a real-time OS.  They are not allowed to do it.

The reason why, is in the US export regulations.  Windows and Mac OS have a Mass Market Exemption (EAR 5D992.c).  This is necessary because MS, Apple and the US State Department simply cannot process billions of export licenses.

Some useful links:





The Mass Market exemption is described in the EAR (2-21.a., 1-5.A.4.a, 1-5.A.1.f, Cat 2, Cat 4, cat 6, cat 7) and MTCR (Group 6, 6-1.A., 6-19.A.1., 6-19.A.2.) regulations, and is defined in what a Mass Market OS is NOT allowed to do:
  • It is not allowed to do real-time processing of audio (Sonar).
  • It is not allowed to provide advanced networking and deep packet inspection (Network inspection).
  • It is not allowed to do precision tracking (of a missile or UAV).
  • It is not allowed to provide C4I video and meta data processing (Information from a missile, MALE or HALE UAV).
Items not specifically controlled, can also be controlled under section 744.3 of the EAR ("catch-all", or EPCI, controls). Items require an export license if they will be used in the design, development, production, or use of:
  • Rocket systems (including ballistic missile systems, space launch vehicles, and sounding rockets) or unmanned aerial vehicles (including cruise missile systems, target drones, and reconnaissance drones) capable of a range of at least 300 km for use in or by a country listed in Country Group D:4 (see  Supplement No. 1 to Part 738 of the EAR).
  • Any rocket system or unmanned aerial vehicles in a D:4 country where system characteristics or use are unknown.
  • Any rocket systems or unmanned aerial vehicles for the delivery of chemical, biological, or nuclear weapons to anywhere in the world, except by governmental programs for nuclear weapons delivery of the Nuclear Non-Proliferation Treaty Nuclear Weapons States that are also members of NATO.
So, in the extreme, if you use US chewing gum to glue together parts of a big missile, then the gum may need an export license.

Windows and Mac OS therefore contain special code to degrade the performance slightly when you try to process multiple video and audio streams - see the above paragraphs on latency.  With a single stream it works, so it is good for home use, or for a musical band, but it is not great for high performance systems.

If you would use Windows or Mac OS for one of the above disallowed functions, then MS and Apple are not allowed to support you and in the extreme, they may even be forced to completely boycott your country (North Korea, Iran...).

Free Open Source Software (BSD, Linux...) is covered under ITAR (120.10(b)) and EAR (734.3(b)(3)(iii)) and is allowed to do the above without requiring export licenses.  (The horse bolted, so there is no point in trying to close the gate now).


So, which OS is best? 
It depends on what exactly you need to do with your system...
:)
Herman

Saturday, June 23, 2018

Compile The Latest Gstreamer From GIT

Compile The Latest gstreamer 1.15 on Ubuntu Linux 18.04 LTS

While working on a way to embed Key Length Value (KLV) metadata in a MPEG-2 TS video stream, I found that ffmpeg can copy and extract KLV, but cannot insert it.  There were some indications that the latest gstreamer has something under development, so I had to figure out how to compile gstreamer from the GIT repository, to get the latest mpegtsmux features.

The cryptic official gstreamer compile guide is here:
https://gstreamer.freedesktop.org/documentation/frequently-asked-questions/git.html#

As usual, the best way to do development work is on a virtual machine, so that you don't mess up your host.  I use Oracle Virtualbox on a Macbook Pro.  I downloaded Ubuntu Linux 18.04 LTS Server, made a 64 bit Virtualbox machine and installed the XFCE desktop, to get a light weight system that runs smoothly in a virtual environment.

The problem with the cryptic official guide is that it probably works on the machine of a developer that has been doing this for a few years, but on a fresh virtual machine, a whole zoo of dependencies are missing and will be discovered the hard way.

Install The GCC Compiler

If you haven't done so already, install a minimal desktop and the development tools:
$ sudo apt update 
$ sudo apt install xfce4
$ sudo apt install build-essential

Then log out and in again, to get your beautifully simple XFCE desktop with a minimum of toppings.

Prepare a Work Directory

Make a directory to work in:
$ cd
$ mkdir gstreamer
$ cd gstreamer

Dependencies

Set up all the dependencies that the official guide doesn't tell you about.   Some of these may pull in additional dependencies and others may not be strictly necessary, but it got me going:
$ sudo apt install gtk-doc-tools liborc-0.4-0 liborc-0.4-dev libvorbis-dev libcdparanoia-dev libcdparanoia0 cdparanoia libvisual-0.4-0 libvisual-0.4-dev libvisual-0.4-plugins libvisual-projectm vorbis-tools vorbisgain libopus-dev libopus-doc libopus0 libopusfile-dev libopusfile0 libtheora-bin libtheora-dev libtheora-doc libvpx-dev libvpx-doc libvpx? libqt5gstreamer-1.0-0 libgstreamer*-dev  libflac++-dev libavc1394-dev libraw1394-dev libraw1394-tools libraw1394-doc libraw1394-tools libtag1-dev libtagc0-dev libwavpack-dev wavpack

$ sudo apt install libfontconfig1-dev libfreetype6-dev libx11-dev libxext-dev libxfixes-dev libxi-dev libxrender-dev libxcb1-dev libx11-xcb-dev libxcb-glx0-dev

$ sudo apt install libxcb-keysyms1-dev libxcb-image0-dev libxcb-shm0-dev libxcb-icccm4-dev libxcb-sync0-dev libxcb-xfixes0-dev libxcb-shape0-dev libxcb-randr0-dev libxcb-render-util0-dev

$ sudo apt install libfontconfig1-dev libdbus-1-dev libfreetype6-dev libudev-dev

$ sudo apt install libasound2-dev libavcodec-dev libavformat-dev libswscale-dev libgstreamer*dev gstreamer-tools gstreamer*good gstreamer*bad

$ sudo apt install libicu-dev libsqlite3-dev libxslt1-dev libssl-dev

$ sudo apt install flex bison nasm

As you can see, the official guide is just ever so slightly insufficient.

Check Out Source Code From GIT

Now, after all the above preparations, you can check out the whole gstreamer extended family as in the official guide:
$ for module in gstreamer gst-plugins-base gst-plugins-good gst-plugins-ugly gst-plugins-bad gst-ffmpeg; do git clone git://anongit.freedesktop.org/gstreamer/$module ; done
...long wait...

BTW, if you do a long running process on another machine (real or virtual) over ssh, use screen:
$ ssh -t herman@server screen -R

Then, when the process is running, you can detach with ^aD and later reconnect to the screen session with the same command above.

See what happened:
$ ls
gst-ffmpeg  gst-plugins-bad  gst-plugins-base  gst-plugins-good  gst-plugins-ugly  gstreamer


Run The autogen.sh Scripts

Go into each directory and run ./autogen.sh.  If you get errors looking like 'nasm/yasm not found or too old... config.status: error: Failed to configure embedded Libav tree... configure failed', then of course you need to hunt down the missing package and add it with for example 'sudo apt install nasm', then try autogen.sh again.

Build and install the gstreamer and gst-plugins-base directories first, otherwise you will get a complaint about 'configure: Requested 'gstreamer-1.0 >= 1.15.0.1' but version of GStreamer is 1.14.0'.

You will get bazillions of compiler warnings, but should not get any errors.  All errors need to be fixed somehow and patches submitted upstream, otherwise you won't get a useful resulting program, but the warnings you can leave to the project developers - let them eat their own dog food.  To me, warnings is a sign of sloppy code and I don't want to fix the slop of young programmers who haven't learned better yet:

$ cd gstreamer; ./autogen.sh 
$ make
$ sudo make install
$ cd ..

$ cd gst-plugins-base; ./autogen.sh
$ make
$ sudo make install
$ cd ..

Gstreamer has plugins that are in various stages of development/neglect, called The Good, The Bad and The Ugly.  Sometimes there is even a Very Ugly version.  These two linked movies are rather more entertaining than compiling gstreamer, so that will give you something to do on your other screen.

$ cd gst-plugins-good; ./autogen.sh
$ make
$ sudo make install
$ cd ..

$ cd gst-plugins-bad; ./autogen.sh 
$ make
$ sudo make install
$ cd ..

$ cd gst-plugins-ugly; ./autogen.sh
$ make
$ sudo make install
$ cd ..
 
$ cd gst-ffmpeg; ./autogen.sh
$ make
$ sudo make install
$ cd ..

The Proof Of The Pudding

The mpegtsmux multiplexer can be used to insert KLV metadata into a video stream:
$ gst-inspect-1.0 mpegtsmux|grep klv
      meta/x-klv


I eventually figured out the syntax and there is now a complete example of how to take Humpty Dumpty apart and put him back together again, in here:
https://www.aeronetworks.ca/2018/05/mpeg-2-transport-streams.html

The above link explains this example pipeline below:

$ gst-launch-1.0 -e mpegtsmux name=mux ! filesink location=dayflightnew.ts \
filesrc location=dayflight.klv ! meta/x-klv ! mux. \
filesrc location=dayflight.ts ! 'video/x-h264, stream-format=byte-stream, alignment=au' ! mux.


Some more research is required to write a little application to prepare the meta data and I suggest that you string things together through sockets or FIFOs.


La Voila!

Herman