Saturday, November 19, 2011

Ubuntu as a wireless speaker of iPhone using Airport (ShAirPort)

What I wanted to do at first was to use my PC speaker as a wireless speaker to play music from iPhone directly without any wiring.

My first idea was to use bluetooth.
I had a hard time to set this up and finally succeeded to make an audio sink from iPhone using bluez(http://www.bluez.org/).
I could listen music with desktop speaker via bluetooth, but it was not so easy to make it running every time I want.

So I bought a bluetooth receiver.
(http://www.belkin.com/IWCatProductPage.process?Product_Id=508754)

This was very compatible and easy to use.
It automatically turned on only when it pairs with my iphone.
But I have not been using this because I was disappointed its sound quality.


Finally, I could do this with Apple's Airplay (http://www.apple.com/itunes/airplay/).
ShAirPort (https://github.com/albertz/shairport) can make Ubuntu to emulate airplay speaker.
It seems to me that ShAirPort was implemented to use Apple's private key(http://en.wikipedia.org/wiki/AirPlay#Reverse_engineering_AirTunes_and_AirPlay), so please use this at your own risk.
Also note that this instruction is from INSTALL.md in ShAirPort source.

1. download source of ShAirPort

https://github.com/albertz/shairport
Just click 'ZIP' icon, then you can download all source files in one zip archive.

*UPDATE*
I confirmed new version of shairport does not work with my fresh ubuntu 12.04.
I am not sure what has been changed, but old version of shairport still works on my fresh ubuntu 12.04.
If you don't mind, you may download old version of shairport below.
<http://www.mediafire.com/file/9pvazneb15hgxbm/albertz-shairport-83c1b71.zip>
You may compile and run it as described below.


2. unzip

Open terminal, go to the directory where you stored the source file and unzip.

$ cd Downloads
$ unzip albertz-shairport-83c1b71.zip
$ cd albertz-shairport-83c1b71

3. Install followings

$ sudo apt-get install build-essential libssl-dev libcrypt-openssl-rsa-perl libao-dev libio-socket-inet6-perl libwww-perl avahi-utils pkg-config

4. Compile

$ make

Make sure that you have no error.

5. Run

$ ./shairport.pl


That's it.
Now see your iPhone and make sure that you have ios version 5 or later.
You may play it with iPad, iPod and iTunes also.

You should see a new icon when you play a music,
just select new speaker in that menu.


Pros :
- All you need is just local network between iPhone and Ubuntu
- No quality loss
- Easy to configure
- Only few resource consumption

Cons :
- Consumes wireless bandwidth (802.11n is recommended)

27 comments:

  1. I was using Totem with a plugin, but it worked only for video. This solution work perfectly for me and I can use my Ubuntu based media center as virtual "airport speaker" for my iPhone. Thanks for this tutorial !

    ReplyDelete
    Replies
    1. you are welcome...glad to hear this works for you...!

      Delete
  2. Great post! Worked for me :)

    ReplyDelete
  3. it works allright, but the sound is playing too fast =/ been googling like crazy after a solution.. anyone else had this issue?

    missing frame is also coming up alot, but the only notable issue is the "pitched" sound..

    ReplyDelete
    Replies
    1. Maybe your problem is related with the missing frame.

      You can check the network connectivity with
      $ ping ip-address-of-your-iphone
      from your desktop.

      (You may see the IP address of your iphone in settings -> wifi -> name-of-ssid).

      It depends on your wireless condition, you should have less than 5% of lost-rate (approximately).

      If you could verify some packet losses,
      - try to change your wireless channel to different channel in your wifi router
      - turn off channel-bonding if you use 802.11n

      Hope this helps...

      Delete
  4. indeed, very easy to setup and run.

    Thx very much !

    ReplyDelete
  5. Hey so for some reason it didnt work on my Ubuntu 12.04. This is the error it gave when i tried to run my shairport.pl script (and yes the make file did compile)

    Can't locate Net/SDP.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at ./shairport.pl line 48.
    BEGIN failed--compilation aborted at ./shairport.pl line 48.

    ReplyDelete
    Replies
    1. In a Terminal :
      cpan Net::SDP

      Regards,

      Delete
  6. it's the same for me... on Ubuntu 12.04.1 it doesn't seem to work, solution? (very good article, nice work!)

    ReplyDelete
    Replies
    1. http://trouch.com/2012/08/03/airpi-airplay-audio-with-raspberry/#comment-354

      Delete
  7. Thanks for information.
    I got same error on my fresh ubuntu 12.04, I do not know the reason.
    I also confirmed that the old version stored in my backup works correctly on ubuntu 12.04.
    So if you don't mind, use old one instead of new one in github.
    and let me know if you know the recent working version of shairport that works on ubuntu 12.04.

    ReplyDelete
  8. The last version doesnt' work for me, but the old ones work perfectly!
    I'm using Ubuntu 12.04. :)
    Thanks !

    ReplyDelete
  9. Just do an: sudo cpan install Net::SDP in the console to met the new dependencies requirements for Shairport.

    ReplyDelete
  10. I can confirm that viniciusferrao is right. Seems so obvious now. I am on Ubuntu 12.10.

    I just did:


    apt-get install build-essential libssl-dev libcrypt-openssl-rsa-perl libao-dev libio-socket-inet6-perl libwww-perl avahi

    cpan install HTTP::Request HTTP::Message Crypt::OpenSSL::RSA IO::Socket::INET6 Net::SDP

    And the latest version of ShairPort builds and executes fine.

    ReplyDelete
  11. It worked fine for me with a MacBook Pro client and Mountain Lion. However it failed with an iPhone 4S when starting to play.

    The following patch solved the problem:

    636c636,640
    < my %sdp = map { ($1, $2) if /^a=([^:]+):(.+)/ } @sdplines;
    ---
    > my %sdp = ();
    > my $sdplin = '';
    > foreach $sdplin(@sdplines) {
    > $sdp{$1} = $2 if $sdplin =~ /^a=([^:]+):(.+)/;
    > }

    Thank you for your blog which was very helpful to me.

    ReplyDelete
  12. THis happens as soon as I try to connect any device to the AirPlay.


    /albertz-shairport-83c1b71$ ./shairport.pl
    Etablerad under namnet "19218FE929A4@ShairPort 7758 on MEDIA"
    Odd number of elements in hash assignment at ./shairport.pl line 631.
    Use of uninitialized value in subroutine entry at ./shairport.pl line 632.
    avahi daemon terminated or 'avahi-publish-service' binary not found at ./shairport.pl line 236.

    ReplyDelete
  13. Can anyone please help me out with this error:

    /albertz-shairport-83c1b71$ ./shairport.pl
    Established under name '68615EC5F130@ShairPort 13675 on UbuntuLaptop'
    Odd number of elements in hash assignment at ./shairport.pl line 631.
    Use of uninitialized value in subroutine entry at ./shairport.pl line 632.
    no AESIV at ./shairport.pl line 632.

    Thanks

    ReplyDelete
  14. I get "Audio StreamWJQsH9Gp+CoiWav5ghqhQgFATAL: Could not open ao device" when I try to start playing audio from iTunes. I can connect fine, iTunes shows that I'm connected and shairport gives no errors when I connect, but it spits out this error when I try to start playing music, and no audio comes out of the speakers. Is there another program I need to install, or do I need a new sound card or something?

    ReplyDelete
    Replies
    1. Well, I managed to get rid of that error. I installed alsa-base and it turns out all of my outputs were muted. Now I can use aplay to play wav sound files with no problem, and shairport no longer gives me that error. However, there is still no sound coming out when I attempt to play music through iTunes. I know it's not a problem with my sound card or device settings, as I can play sounds with the aplay command.

      Delete
    2. It finally works! Turns out my firewall was in the way of it. I had already opened 5002/tcp, but netstat -lnp | grep hairtunes revealed that shairport uses both 6000/udp and 6001/udp as well.

      Delete
  15. this is awesome - but how do i make the sound output to my HDMI Audio Device instead of the default system speakers?

    ReplyDelete
  16. Wireless Headset this is amazing headset made the plantronics, simple and best performance in a very short budget, excellent voice, no distortion, no noise.

    ReplyDelete
  17. Buy & sell new and used iPhones.
    Wide selection of new and used iPhones for sale.
    More at this link

    ReplyDelete