Getting Your Own Video and Feeds - BeagleBone Media Center (2015)

BeagleBone Media Center (2015)

Chapter 4. Getting Your Own Video and Feeds

"One server to satisfy them all" could have been the name of this chapter. We now have a great media server where we can share any media, but we would like to be more independent so that we can choose the functionalities the server can have. The goal of this chapter is to let you cross the bridge, where you are going to increase your knowledge by getting your hands dirty. After all, you want to build your own services, so why not create your own contents as well.

More specifically, here we will begin by building a webcam streaming service from scratch, and we will see how this can interact with what we have implemented previously in the server. We will also see how to set up a service to retrieve RSS feeds. We will discuss the services in the following sections:

· Installing and running MJPG-Streamer

· Detecting the hardware device and installing drivers and libraries for a webcam

· Configuring RSS feeds with Leed

Detecting the hardware device and installing drivers and libraries for a webcam

Even though today many webcams are provided with hardware encoding capabilities such as the Logitech HD Pro series, we will focus on those without this capability, as we want to have a low budget project. You will then learn how to reuse any webcam left somewhere in a box because it is not being used. At the end, you can then create a low cost video conference system as well.

How to know your webcam

As you plug in the webcam, the Linux kernel will detect it, so you can read every detail it's able to retrieve about the connected device.

We are going to see two ways to retrieve the webcam we have plugged in: the easy one that is not complete and the harder one that is complete.

"All magic comes with a price."

--Rumpelstiltskin, Once Upon a Time

Note

Often, at a certain point in your installation, you have to choose between the easy or the hard way. Most of the time, powerful Linux commands or tools are not thought to be easy at first but after some experiments you'll discover that they really can make your life better.

Let's start with the fast and easy way, which is lsusb :

debian@arm:~$ lsusb

Bus 001 Device 002: ID 046d:0802 Logitech, Inc. Webcam C200

Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

This just confirms that the webcam is running well and is seen correctly from the USB.

Most of the time we want more details, because a hardware installation is not exactly as described in books or documentations, so you might encounter slight differences. This is why the second solution comes in. Among some of the advantages, you are able to know each step that has taken place when the USB device was discovered by the board and Linux, such as in a hardware scenario:

debian@arm:~$ dmesg

How to know your webcam

A UVC device (here, a Logitech C200) has been used to obtain these messages

Most probably, you won't exactly have the same outputs, but they should be close enough so that you can interpret them easily when they are referred to:

· New USB device found: This is the main message. In case of any issue, we will check its presence elsewhere. This message indicates that this is a hardware error and not a software or configuration error that you need to investigate.

· idVendor and idProduct: This message indicates that the device has been detected. This information is interesting so you can check the constructor detail.

Note

Most recent webcams are compatible with the Linux USB Video Class (UVC), you can check yours at http://www.ideasonboard.org/uvc/#devices.

· Among all the messages, you should also look for the one that says Registered new interface driver interface because failing to find it can be a clue that Linux could detect the device but wasn't able to install it.

Note

The new device will be detected as /dev/video0. Nevertheless, at start, you can see your webcam as a different device name according to your BeagleBone configuration, for example, if a video capable cape is already plugged in.

Setting up your webcam

Now we know what is seen from the USB level. The next step is to use the crucial Video4Linux driver, which is like a Swiss army knife for anything related to video capture:

debian@arm:~$ Install v4l-utils

The primary use of this tool is to inquire about what the webcam can provide with some of its capabilities:

debian@arm:~$ v4l2-ctl -–all

Setting up your webcam

There are four distinctive sections that let you know how your webcam will be used according to the current settings:

· Driver info (1) : This contains the following information:

· Name, vendor, and product IDs that we find in the system message

· The driver info (the kernel's version)

· Capabilities: the device is able to provide video streaming

· Video capture supported format(s) (2): This contains the following information:

· What resolution(s) are to be used. As this example uses an old webcam, there is not much to choose from but you can easily have a lot of choices with devices nowadays.

· The pixel format is all about how the data is encoded but more details can be retrieved about format capabilities (see the next paragraph).

· The remaining stuff is relevant only if you want to know in precise detail.

· Crop capabilities (3): This contains your current settings. Indeed, you can define the video crop window that will be used. If needed, use the crop settings:

· --set-crop-output=top=<x>,left=<y>,width=<w>,height=<h>

· Video input (4): This contains the following information:

· The input number. Here we have used 0, which is the one that we found previously.

· Its current status.

· The famous frames per second, which gives you a local ratio. This is not what you will obtain when you'll be using a server, as network latencies will downgrade this ratio value.

You can grab capabilities for each parameter. For instance, if you want to see all the video formats the webcam can provide, type this command:

debian@arm:~$ v4l2-ctl --list-formats

Setting up your webcam

Here, we see that we can also use MJPEG format directly provided by the cam.

While this part is not mandatory, such a hardware tour is interesting because you know what you can do with your device. It is also a good habit to be able to retrieve diagnostics when the webcam shows some bad signs.

Tip

If you would like to get more in depth knowledge about your device, install the uvcdynctrl package, which lets you retrieve all the formats and frame rates supported.

Installing and running MJPG-Streamer

Now that we have checked the chain from the hardware level up to the driver, we can install the software that will make use of Video4Linux for video streaming. Here comes MJPG -Streamer.

This application aims to provide you with a JPEG stream on the network available for browsers and all video applications.

Besides this, we are also interested in this solution as it's made for systems with less advanced CPU, so we can start MJPG-Streamer as a service. With this streamer, you can also use the built-hardware compression and even control webcams such as pan, tilt, rotations, zoom capabilities, and so on.

Installing MJPG-Streamer

Before installing MJPG-Streamer, we will install all the necessary dependencies:

debian@arm:~$ install subversion libjpeg8-dev imagemagick

Next, we will retrieve the code from the project:

debian@arm:~$ svn checkout http://svn.code.sf.net/p/mjpg-streamer/code/ mjpg-streamer-code

You can now build the executable from the sources you just downloaded by performing the following steps:

1. Enter the following into the local directory you have downloaded:

2. debian@arm:~$ cd mjpg-streamer-code/mjpg-streamer

3. Then enter the following command:

4. debian@beaglebone:~/mjpg-streamer-code/mjpg-streamer$ make

When the compilation is complete, we end up with some new files. From this picture the new green files are produced from the compilation: there are the executables and some plugins as well.

Installing MJPG-Streamer

That's all that is needed, so the application is now considered ready. We can now try it out. Not so much to do after all, don't you think?

Starting the application

This section aims at getting you started quickly with MJPG-Streamer. At the end, we'll see how to start it as a service on boot.

Before getting started, the server requires some plugins to be copied into the dedicated lib directory for this purpose:

debian@beaglebone:~/mjpg-streamer-code/mjpg-streamer$ sudo cp input_uvc.so output_http.so /usr/lib

The MJPG-Streamer application has to know the path where these files can be found, so we define the following environment variable:

debian@beaglebone:~/mjpg-streamer-code/mjpg-streamer$ export LD_LIBRARY_PATH=/usr/lib;$LD_LIBRARY_PATH

Enough preparation! Time to start streaming:

debian@beaglebone:~/mjpg-streamer-code/mjpg-streamer$./mjpg_streamer -i "input_uvc.so" -o "output_http.so -w www"

Starting the application

As the script starts, the input parameters that will be taken into consideration are displayed. You can now identify this information, as they have been explained previously:

· The detected device from V4L2

· The resolution that will be displayed, according to your settings

· Which port will be opened

· Some controls that depend on your camera capabilities (tilt, pan, and so on)

Tip

If you need to change the port used by MJPG-Streamer, add -p xxxx at the end of the command, which is shown as follows:

debian@beaglebone:~/mjpg-streamer-code/mjpg-streamer$ ./mjpg_streamer -i "input_uvc.so" -o "output_http.so -w www –p 1234"

Let's add some security

If you want to add some security, then you should set the credentials:

debian@beaglebone:~/mjpg-streamer-code/mjpg-streamer$ ./mjpg-streamer -o "output_http.so -w ./www -c debian:temppwd"

Note

Credentials can always be stolen and used without your consent. The best way to ensure that your stream is confidential all along would be to encrypt it.

So if you intend to use strong encryption for secured applications, the crypto-cape is worth taking a look at

http://datko.net/2013/10/03/howto_crypto_beaglebone_black/.

"I'm famous" – your first stream

That's it. The webcam is made accessible to everyone across the network from BeagleBone; you can access the video from your browser and connect to http://192.168.0.15:8080/.

You will then see the default welcome screen, bravo!:

"I'm famous" – your first stream

Your first contact with the MJPG-Server

Note

You might wonder how you would get informed about which port to use among those already assigned. Then teleport yourself to Appendix A.

Using our stream across the network

Now that the webcam is available across the network, you have several options to handle this:

· You can use the direct flow available from the home page. On the left-hand side menu, just click on the stream tab.

· Using VLC, you can open the stream with the direct link available at http://192.168.0.15:8080/?action=stream.

The VideoLAN menu tab is a M3U-playlist link generator that you can click on. This will generate a playlist file you can open thereafter.

Tip

In this case, VLC is efficient, as you can transcode the webcam stream to any format you need. Although it's not mandatory, this solution is the most efficient, as it frees the BeagleBone's CPU so that your server can focus on providing services.

· Using MediaDrop, we can integrate this new stream in our shiny MediaDrop server, knowing that currently MediaDrop doesn't support direct local streams. You can create a new post with the related URL link in the message body, as shown in the following screenshot:

Using our stream across the network

Starting the streaming service automatically on boot

In the beginning, we saw that MJPG-Streamer needs only one command line to be started. We can put it in a bash script, but servicing on boot is far better. For this, use a console text editor – nano or vim – and create a file dedicated to this service. Let's call itstart_mjpgstreamer and add the following commands:

#! /bin/sh

# /etc/init.d/start_mjpgstreamer

export LD_LIBRARY_PATH="/home/debian/mjpg-streamer/mjpg-streamer-code/mjpg-streamer;$LD_LIBRARY_PATH"

EXEC_PATH="/home/debian/mjpg-streamer/mjpg-streamer-code/mjpg-streamer"

$EXEC_PATH/mjpg_streamer -i "input_uvc.so" -o "output_http.so -w EXEC_PATH /www"

You can then use administrator rights to add it to the services:

debian@arm:~$ sudo /etc/init.d/start_mjpgstreamer start

On the next reboot, MJPG-Streamer will be started automatically.

Exploring new capabilities to install

For those about to explore, we salute you!

As the book is not just about installation and configuration, I thought that it would be useful to play around with what we just installed in the previous chapters, such as getting into the plugins in more depth or, why not, a different service with the webcam.

Plugins

Remember that at the beginning of this chapter, we began the demonstration with two plugins:

debian@beaglebone:~/mjpg-streamer-code/mjpg-streamer$ ./mjpg_streamer -i "input_uvc.so" -o "output_http.so -w www"

If we take a moment to look at these plugins, we will understand that the first plugin is responsible for handling the webcam directly from the driver.

Simply ask for help and options as follows:

debian@beaglebone:~/mjpg-streamer-code/mjpg-streamer$ ./mjpg_streamer --input "input_uvc.so --help"

Plugins

The second plugin is about the web server settings:

· The path to the directory contains the final web server HTML pages. This implies that you can modify the existing pages with a little effort or create new ones based on those provided.

· Force a special port to be used. Like I said previously, port use is dedicated for a server. You define here which will be the one for this service.

· You can discover many others by asking:

· debian@arm:~$ ./mjpg_streamer --output "output_http.so --help"

Plugins

Tip

Apart from input_uvc and output_http, you have other available plugins to play with. Let's take a look at the plugins directory.

Another tool for the webcam

The Mjpg_streamer project is dedicated for streaming over network, but it is not the only one. For instance, do you have any specific needs such as monitoring your house/son/cat/Jon Snow figurine?

Tip

buuuuzzz: if you answered yes to the last one, you just defined yourself as a geek.

Well, in that case the Motion project is for you; just install the motion package and start it with the default motion.conf configuration. You will then record videos and pictures of any moving object/person that will be detected. As MJPG-Streamer motion aims to be a low CPU consumer, it works very well on BeagleBone Black.

Configuring RSS feeds with Leed

Our server can handle videos, pictures, and music from any source and it would be cool to have another tool to retrieve news from some RSS providers. This can be done with Leed, a RSS project organized for servers. You can have a final result, as shown in the following screenshot:

Configuring RSS feeds with Leed

This project has a "quick and easy" installation spirit, so you can give it a try without harness. Leed (for Light Feed) allows you to you access RSS feeds from any browser, so no RSS reader application is needed, and every user in your network can read them as well. You install it on the server and feeds are automatically updated.

Note

Well, the truth behind the scenes is that a cron task does this for you. You will be guided to set some synchronisation after the installation.

Creating the environment for Leed in three steps

We already have Apache, MySQL, and PHP installed, and we need a few other prerequisites to run Leed:

1. Create a database for Leed

2. Download the project code and set permissions

3. Install Leed itself

Creating a database for Leed

Remember the database that we created for MediaDrop in Chapter 1, Transforming Your BeagleBone Black into a Media Server?

You will be at ease here, as this is exactly the same approach because Leed requires its own database as well.

You will begin by opening a MySQL session:

debian@arm:~$ mysql –u root –p

What we need here is to have a dedicated Leed user with its database. This user will be connected using the following:

create user 'debian_leed'@'localhost' IDENTIFIED BY 'temppwd';

create database leed_db;

use leed_db;

grant create, insert, update, select, delete on leed_db.* to debian_leed@localhost;

exit

Creating a database for Leed

Downloading the project code and setting permissions

We prepared our server to have its environment ready for Leed, so after getting the latest version, we'll get it working with Apache by performing the following steps:

1. From your home, retrieve the latest project's code. It will also create a dedicated directory:

2. debian@arm:~$ git clone https://github.com/ldleman/Leed.git

3. debian@arm:~$ ls

4. mediadrop mjpg-streamer Leed music

5. Now, we need to put this new directory where the Apache server can find it:

6. debian@arm:~$ sudo mv Leed /var/www/

7. Change the permissions for the application:

8. debian@arm:~$ chmod 777 /var/www/Leed/ -R

Installing Leed

When you go to the server address (http//192.168.0.15/leed/install.php), you'll get the following installation screen:

Installing Leed

We now need to fill in the database details that we previously defined and add the Administrator credentials as well. Now save and quit. Don't worry about the explanations, we'll discuss these settings thereafter.

It's important that all items from the prerequisites list on the right are green.

Otherwise, a warning message will be displayed about the wrong permissions settings, as shown in the following screenshot:

Installing Leed

After the configuration, the installation is complete:

Installing Leed

Leed is now ready for you.

Setting up a cron job for feed updates

If you want automatic updates for your feeds, you'll need to define a synchronization task with cron:

1. Modify cron jobs:

2. debian@arm:~$ sudo crontab –e

3. Add the following line:

4. 0 * * * * wget -q -O /var/www/leed/logsCron "http://192.168.0.15/Leed/action.php?action=synchronize

5. Save it and your feeds will be refreshed every hour.

6. Finally, some little cleanup: remove install.php for security matters:

7. debian@arm:~$ rm /var/www/Leed/install.php

Using Leed to add your RSS feed

When you need to add some feeds from the Manage menu, in Feed Options (on the right- hand side) select Preferences and you just have to paste the RSS link and add it with the button:

Using Leed to add your RSS feed

You might find it useful to organize your feeds into groups, as we did for movies in MediaDrop. The Rename button will serve to achieve this goal. For example, here a TV Shows category has been created, so every feed related to this type will be organized on the main screen.

Some Leed preferences settings in a server environment

You will be asked to choose between two synchronisation modes: Complete and Graduated.

· Complete: This is to be used in a usual computer, as it will update all your feeds in a row, which is a CPU consuming task

· Graduated: Look for the oldest 10 feeds and update them if required

You also have the possibility of allowing anonymous people to read your feeds. If you remember, in Chapter 3, Examples of Real-world Situations, we allowed guests to access our content but not to publish any. This is the same case here. Setting Allow anonymous readers to Yes will let your guests access your feeds but not add any.

Extending Leed with plugins

If you want to extend Leed capabilities, you can use the Leed Market—as the author defined it—from Feed options in the Manage menu. There, you'll be directed to the Leed Market space. Installation is just a matter of downloading the ZIP file with all plugins:

debian@arm:~/Leed$ wget https://github.com/ldleman/Leed-market/archive/master.zip

debian@arm:~/Leed$ sudo unzip master.zip

Let's use the AdBlock plugin for this example:

1. Copy the content of the AdBlock plugin directory where Leed can see it:

2. debian@arm:~/Leed$ sudo cp –r Leed-market-master/adblock /var/www/Leed/plugins

3. Connect yourself and set the plugin by navigating to Manage | Available Plugins and then activate adblock with Enable, as follows:

Extending Leed with plugins

In this chapter, we covered:

· Some words about the hardware

· How to know your webcam

· Configuring RSS feeds with Leed

Summary

In this chapter, we had some good experiments with the hardware part of the server "from the ground," to finally end by successfully setting up the webcam service on boot. We discovered hardware detection, a way to "talk" with our local webcam and thus to be able to see what happens when we plug a device in the BeagleBone.

Through the topics, we also discovered video4linux to retrieve information about the device, and learned about configuring devices. Along the way, we encountered MJPG-Streamer. Finally, it's better to be on our own instead of being dependent on some GUI interfaces, where you always wonder where you need to click. Finally, our efforts have been rewarded, as we ended up with a web page we can use and modify according to our tastes.

RSS news can also be provided by our server so that you can manage all your feeds in one place, read them anywhere, and even organize dedicated groups.

Plenty of concepts have been seen for hardware and software. Then think of this chapter as a concrete example you can use and adapt to understand how Linux works.

I hope you enjoyed this freedom of choice, as you drag ideas and drop them in your BeagleBone as services. We entered in the DIY area, showing you ways to explore further. You can argue, saying that we can choose the software but still use off the shelf commercial devices.

Looking for more independence? Good! In the next chapter, we will see how to build a more personalized object, something that will resemble your choices: your own player device.