Roaming Media

After you start Icecast, the server configuration should be complete. At this point, start MPD as well. It should hook itself up to the Icecast port, and the logs should be free of errors. Next, an MPD client is needed in order to set up playlists on the server. I use MPDroid for this, an Android variant I use to control playlists from my phone, but there are clients for a wide variety of platforms available via your package manager or from http://mpd.wikia.com/wiki/Clients. Fire up a client, and add a few songs to a playlist for a test and tell it to play. The Icecast access log (not the MPD logs) should show that a SOURCE has connected, and it should show a 200 return. For example:


127.0.0.1 - - [20/Jul/2011:01:15:03 -0700] "SOURCE / 
 ↪HTTP/1.0" 200 19 "-" "MPD" 424

This shows the incoming connection from MPD on the local box, the current date and time and the fact that this is a SOURCE request as opposed to a player request. It shows the directory accessed "/", the protocol used and the return code "200". This is a great source of verification that the connection between MPD and Icecast is up and working properly, though it does not tell you that any data actually is being passed. For that, you'll need to test it end to end.

To test it, you need a music player that supports Ogg format (or MP3 if you've gone that route). I've chosen Rhythmbox for this example, because it supports Ogg, is popular across a wide range of distributions and has a command-line control system that you can use to start and stop the music as well as set the server URI. Install and launch Rhythmbox (or fire up your favorite player with these features) and set it to play from the Icecast URI. For example, mine is at http://templar:8000/. If you have changed the mount directive in the MPD configuration file to "/music.ogg", the URI would then be http://templar:8000/music.ogg. Either way, when the music player is pointed at this URI, the Icecast access.log file should show something like this:


mj-12 - - [01/Aug/2011:15:28:52 -0700] "GET / 
 ↪HTTP/1.1" 200 1194382 "-" "-" 70

The format for this line is very similar to the one above. The only real difference is that this is a GET request instead of a SOURCE request. Seeing this line in the access logs without a corresponding error in the error log shows you that the media player is requesting the music stream from the Icecast server properly.

If you don't hear music at this time, go back over the setup and check the log files. Check the volume level within MPD, and make sure that the MPD client says a song is playing. Many MPD clients also let you stream music directly, so you can verify that it's working as well.

The next step is to set up your Bluetooth token. In my case, this is my Android phone, but pretty much any Bluetooth device will work. Ranges vary, so placement of the Bluetooth receivers is important to avoid overlap or gaps in the area you're trying to cover.

The Linux package for Bluetooth support is called Bluez. It is widely available and comes as part of most distributions. Install this package if it is not installed on your system already. You don't need to make any configuration changes, because all you need to do is identify that the Bluetooth device is in range. You don't need to pair with it or transfer data between devices. After installing the Bluez package, start the software. Your logs should show that the software started correctly and that it identified your Bluetooth hardware properly.

In order to find the Bluetooth token, it needs to be put into discoverable mode temporarily. Turn that on, and run the following scan command from the command line:


mike@templar:~$ hcitool scan
Scanning ...
	D4:E9:C0:37:00:0D	eris

Make a note of the Bluetooth ID, and be sure that the name field is not blank. It can be anything, but it has to be something. After this step, you can turn off the discoverable mode on the Bluetooth device for increased security. You now have all the information that you need.

The following script checks whether the Bluetooth device is in range and stops or starts the music player based on the result. Replace the SERVER_URI variable with your MPD/Icecast server and the BTADDR variable with your device's Bluetooth ID (this ID comes from the hcitool scan command above):


#!/bin/bash
SERVER_URI="http://templar:8000/"
BTADDR="D4:E9:C0:37:00:0D"

DBUSADDR=`grep -z DBUS_SESSION_BUS_ADDRESS /proc/*/environ 2> 
 ↪/dev/null|
sed 's/DBUS/\nDBUS/g' | tail -n 1`

if [ "x$DBUSADDR" != "x" ]; then
  export $DBUSADDR
else
  echo "Cannot find DBUS Session for Rhythmbox. Please 
be sure the application is running"
  exit 1
fi

NAME=`hcitool name $BTADDR`

if [ -z "$NAME" ] ; then
    `rhythmbox-client --pause`
else
    `rhythmbox-client --play-uri=$SERVER_URI`
fi

Save this script as /usr/local/bin/musiccontrol.sh. Next, add the script as a crontab entry that runs every minute. This entry must be run as the same user as the user that owns the Rhythmbox process.

Edit the crontab from the correct user:


mike@templar:~$ crontab -e

Add the following line, and then save and exit:


* * * * * /usr/local/bin/musiccontrol.sh

Now, turn on the Bluetooth device (it does not need to be discoverable this time because you already have the address). At the turn of the next minute, the cron script will see the Bluetooth device and then tell Rhythmbox to start playing the music from the MPD/Icecast server. If you move the Bluetooth device out of range, the cron script will no longer see the Bluetooth device and will stop the music.

Rhythmbox, Bluetooth and this cron script must be set up on every machine that you intend to play music for you. If you do it on only one box, only that box will start and stop music as you enter or leave range. If you set up the system on multiple pieces of hardware, it will transition the music for you. When moving out of the range of one server and into the range of another, the music automatically will stop in the room you were in before and start in the room you are in now.

This is just a simple setup for moving media around automatically. Multiple Bluetooth devices could be set up for different members of the house, and a priority system could be put in place. Motion detection using the "motion" package could be set up to differentiate areas of your home further with overlapping Bluetooth. You even could use facial recognition with help from the OpenCV Project. There are many places you can go from here.

Headphones image via Shutterstock.com.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

roaming media

Roxely samus's picture

At heart, you think that music would not be restricted to headphones?
I and thousands of people hate Guando we conduct a public one and comes with a rude caxinha music without earphone oudido. Nobody is obliged to hear what he hears, then he would have to be educated to use their own handset oara substencia. But his lesson is well guarded and let's see if we can adopt it.
I thank the great audience that this site has and ask administrators the opportunity to leave my site where you realize the work of research and dissemination. My goal is not to create spam, but I publicize my work. Thank you cadeira universitaria

music play

dennisrert's picture

I agree with you grade and gender roxelly. Music in public place is to listen with headphones adestramento de cães

Roaming

Theras's picture

How does the off-the-shelf open-source programs? You disagree in part to talk about in-room-room. I really liked the article and I will look further. I leave my site Primeira Página do Google

Very interesting

marksen's picture

I will try to use MPD as for your directives.

Thanks for the little tutorial.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix