Search code examples
iosstreamreal-timecore-audiosynchronization

IOS synced music streamer


I am trying to stream music from one device to another over a GKSession such that the music is perfectly in sync. So far I know how to grab the audio using this, but I am unsure about a few things. I am pretty sure that I have to use AudioFileReadPackets to transfer the data.

  1. How should I package and send the packet data from the memory buffer as a stream?
  2. On the other end how do I convert the received packets back into an audio stream?
  3. What is the best way to ensure the packets are played in sync?

Solution

  • Important update: I just created a repo on github that pretty much does what you're asking for. Check it out!

    below is my older answer:

    1. a. first you want to get audio buffer data from your music, similar to how I did in the github project.

      b. you want to get this data and chunk it up into packets, with each packet containing header information that you will later on use to manage their delivery and esnure that they are synced. So for example the time stamp of each packet, the order of it, the music file hash etc.. see this answer to get an idea, and then take a look at this excellent tutorial that shows you in detail how to package a packet for any purpose.

    2. on the other side of the network, you unpack the packet in a way that's very similar to packaging it. again refer to the above tutorial that discusses at length how to do it.

    3. this is the hardest part. fortunately, you are only dealing with two devices.. so you can do it with GKSession.. otherwise you will have to drop a level lower (ie using bonjour.. wich GKSession is a wrapper of.. and which adds a lot of overhead that you can't afford for a real time app.. but don't panic.. this book has a nice and easy example for using bonjour.. )

    the idea is that you want to gather enough packets on the other end to fill an audio queue buffer, then you want to send an unreliable packet (read: udp protocol) to jump start the playback of the music. with a udp send, it's near instant delivery (i'd argue the human ear wouldn't distinguish the delay between just two devices).. once in a while an unreliable packet will be lost though.. so you will have to either live with it or figure a way to handle that.

    and last but not least you definitely want to grab a copy of learning core audio.. anyone doing core audio in iOS needs this. Also take a look at the discussion following this answer.. it will bring your attention to a lot of things to deal with when it comes to core audio /real time iOS apps.

    good luck!