"One server to satisfy them all" could have been the name of this article by David Lewin, the author of BeagleBone Media Center. 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 article 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.
(For more resources related to this topic, see here.)
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:
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.
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
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
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:
Most recent webcams are compatible with the Linux USB Video Class (UVC), you can check yours at http://www.ideasonboard.org/uvc/#devices.
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.
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
There are four distinctive sections that let you know how your webcam will be used according to the current settings:
--set-crop-output=top=<x>,left=<y>,width=<w>,height=<h>
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
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.
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.
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.
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:
debian@arm:~$ cd mjpg-streamer-code/mjpg-streamer
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.
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?
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"
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:
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"
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"
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/.
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!:
Your first contact with the MJPG-Server
You might wonder how you would get informed about which port to use among those already assigned.
Now that the webcam is available across the network, you have several options to handle this:
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.
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 it start_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.
For those about to explore, we salute you!
Remember that at the beginning of this article, 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"
The second plugin is about the web server settings:
debian@arm:~$ ./mjpg_streamer --output "output_http.so --help"
Apart from input_uvc and output_http, you have other available plugins to play with. Let's take a look at the plugins directory.
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?
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.
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:
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.
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.
We already have Apache, MySQL, and PHP installed, and we need a few other prerequisites to run Leed:
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
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:
debian@arm:~$ git clone https://github.com/ldleman/Leed.git debian@arm:~$ ls mediadrop mjpg-streamer Leed music
debian@arm:~$ sudo mv Leed /var/www/
debian@arm:~$ chmod 777 /var/www/Leed/ -R
When you go to the server address (http//192.168.0.15/leed/install.php), you'll get the following installation screen:
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:
After the configuration, the installation is complete:
Leed is now ready for you.
If you want automatic updates for your feeds, you'll need to define a synchronization task with cron:
debian@arm:~$ sudo crontab –e
0 * * * * wget -q -O /var/www/leed/logsCron "http://192.168.0.15/Leed/action.php?action=synchronize
debian@arm:~$ rm /var/www/Leed/install.php
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:
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.
You will be asked to choose between two synchronisation modes: Complete and Graduated.
You also have the possibility of allowing anonymous people to read your feeds. Setting Allow anonymous readers to Yeswill let your guests access your feeds but not add any.
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:
debian@arm:~/Leed$ sudo cp –r Leed-market-master/adblock /var/www/Leed/plugins
In this article, we covered:
In this article, 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 article 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.