简体   繁体   English

将音频从Mac上的麦克风流式传输到iPhone

[英]Streaming audio from a microphone on a Mac to an iPhone

I'm working on a personal project where the iPhone connects to a server-type application running on a Mac. 我正在开发一个个人项目,iPhone连接到运行在Mac上的服务器类型的应用程序。 The iPhone send and receives textual/ASCII data via standard sockets. iPhone通过标准套接字发送和接收文本/ ASCII数据。 I now need to stream the microphone from the Mac to the iPhone. 我现在需要将麦克风从Mac流式传输到iPhone。 I've done some work with AudioServices before but wanted to check my thoughts here before getting too deep. 我之前已经完成了一些与AudioServices的合作,但是想要在太深入之前检查我的想法。

I'm thinking I can: 我想我可以:
1. Create an Audio Queue in the standard Cocoa application on the Mac. 1.在Mac上的标准Cocoa应用程序中创建音频队列。
2. In my Audio Queue Callback function, rather than writing it to a file, write it to another socket I open for audio streaming. 2.在我的音频队列回调函数中,不是将其写入文件,而是将其写入我为音频流打开的另一个套接字。
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. 3.在iPhone上,从TCP流接收原始采样/编码音频数据并将其转储到音频队列播放器中,该播放器输出到耳机/扬声器。

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). 在“回放”端,我认为你不能只将缓冲区传递到队列中(IIRC你需要缓冲它直到你得到一个回调)。
  • 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. 我很确定AQ回调会在他们自己的线程上发生,因此你需要在缓冲区访问周围使用某种锁定/障碍。
  • 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); AQ可能会给你“IMA4”帧(IMA ADPCM 4:1,左右); I'm not sure if it does hardware MP3 decompression on the iPhone. 我不确定它是否在iPhone上进行硬件MP3解压缩。
  • Packetization, if eg 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). Skype通过改变播放速度(使用音调校正)来实现。
  • 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. 您可以通过短链接使用TCP,但这在很大程度上取决于无线网络的质量。 UDP is a minor pain to get right (especially if you have to detect an MTU hole). UDP是一个很小的痛苦(特别是如果你必须检测一个MTU漏洞)。

Depending on your data rates, it might be worthwhile going for the lower-level (BSD) socket API and potentially even using readv()/writev(). 根据您的数据速率,可能值得使用较低级别(BSD)套接字API,甚至可能使用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. 如果你想要的只是一个“在线广播”服务而你并不关心所使用的协议,那么使用AVPlayer / MPMoviePlayer来播放来自URL的音频可能会更容易。 This involves implementing a server which speaks Apple's HTTP streaming protocol; 这涉及实现一个讲Apple的HTTP流协议的服务器; I believe Apple has some sample code that does this. 我相信Apple有一些示例代码可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM