开发者

Streaming audio from a microphone on a Mac to an iPhone

开发者 https://www.devze.com 2023-04-11 06:05 出处:网络
I\'m working on a personal project where the iPhone connects to a server-type application running on a Mac.The iPhone send and receives textual/ASCII data via standard sockets.I now need to stream the

I'm working on a personal project where the iPhone connects to a server-type application running on a Mac. The iPhone send and receives textual/ASCII data via standard sockets. I now need to stream the microphone from the Mac to the iPhone. I've done some work with AudioServices before but wanted to check my thoughts here before getting too deep.

I'm thinking I can:

1. Create an Audio Queue in the standard Cocoa application on the Mac.

2. In my Audio Queue Callback function, rather than writing it to a file, write it to another sock开发者_开发问答et I open for audio streaming.

3. On the iPhone, receive the raw sampled/encoded audio data from the TCP stream and dump it into an Audio Queue Player which outputs to headphone/speaker.

I know this is no small task and I've greatly simplified what I need to do but could it be as easy as that?

Thanks for any help you can provide,

Stateful


This looks broadly sensible, but you'll almost certainly need to do a few more things:

  • Buffering. On the "recording" end, you probably don't want to block the audio queue if the buffer is full. On the "playback" end, I don't think you can just pass buffers into the queue (IIRC you'll need to buffer it until you get a callback).
  • Concurrency. I'm pretty sure AQ callbacks happen on their own thread, so you'll need some sort of locking/barriers around your buffer accesses.
  • Buffer pools, if memory allocation ends up being a big overhead.
  • Compression. AQ might be able to give you "IMA4" frames (IMA ADPCM 4:1, or so); I'm not sure if it does hardware MP3 decompression on the iPhone.
  • Packetization, if e.g. you need to interleave voice chat with text chat.
  • EDIT: Playback sync (or whatever you're supposed to call it). You need to be able to handle different effective audio clock rates, whether it's due to a change in latency or something else. Skype does it by changing playback speed (with pitch-correction).
  • EDIT: Packet loss. You might be able to get away with using TCP over a short link, but that depends a lot on the quality of your wireless network. UDP is a minor pain to get right (especially if you have to detect an MTU hole).

Depending on your data rates, it might be worthwhile going for the lower-level (BSD) socket API and potentially even using readv()/writev().

If all you want is an "online radio" service and you don't care about the protocol used, it might be easier to use AVPlayer/MPMoviePlayer to play audio from a URL instead. This involves implementing a server which speaks Apple's HTTP streaming protocol; I believe Apple has some sample code that does this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号