简体   繁体   English

实时音频对话iOS

[英]Real time audio conversation iOS

I am designing an iOS app for a customer who wants to allow real-time (with minimum lag, max 50ms) conversations between users (a sort of Teamspeak). 我正在为希望允许用户之间实时(最小延迟,最长50毫秒)对话的客户设计一个iOS应用程序(一种Teamspeak)。 The lag must be low because the audio can also be live music, played with instruments, so all the users need to synchronize. 滞后必须很低,因为音频也可以是现场音乐,用乐器演奏,所以所有用户都需要同步。 I need a server, which will request audio recordings to every client and send to others (and make them hear the same sound at the same time). 我需要一台服务器,它会向每个客户端请求录音并发送给其他人(并让他们同时听到相同的声音)。 HTTP is easy to manage/implement and easy to scale, but very low-performing because an average HTTP request takes > 50ms... (with a mid-level hardware), so I was thinking of TCP/UDP connections kept open between clients and server. HTTP易于管理/实现且易于扩展,但性能非常低,因为平均HTTP请求需要> 50ms ...(使用中级硬件),所以我在考虑在客户端之间保持打开的TCP / UDP连接和服务器。 But I have some questions: 但我有一些问题:

  • If I develop the server in Python (using TwistedMatrix, for example), how are its performance ? 如果我用Python开发服务器(例如使用TwistedMatrix),它的性能如何?
  • I can't develop the server in C++ because it is hard to manage (scalable) and to develop. 我不能用C ++开发服务器,因为它很难管理(可扩展)和开发。
  • Anyone used Nodejs (which is easy to scale) to manage TCP/UDP connections? 任何人都使用Nodejs(易于扩展)来管理TCP / UDP连接?
  • If I use HTTP, will it be fast enough with Keep-Alive? 如果我使用HTTP,它是否足够快与Keep-Alive? Becuase usually the time required for an HTTP Request to be performed is > 50ms (because opening-closing connection is hard), and I want the total procedure to be less than that time. 因为通常执行HTTP请求所需的时间大于50毫秒(因为打开 - 关闭连接很难),并且我希望总程序小于该时间。
  • The server will be running on a Linux machine. 服务器将在Linux机器上运行。

And finally: which type of compression can you suggest me? 最后:你能建议我使用哪种压缩方式? I thought Ogg Vorbis would be nice, but if there's anything better (and can be used in iOS), I am open to changes. 我认为Ogg Vorbis会很好,但如果有更好的东西(并且可以在iOS中使用),我愿意接受改变。

Thank you, Umar. 谢谢你,奥马尔。

What concerns the server, the request itself is not a bottleneck. 与服务器有什么关系,请求本身不是瓶颈。 I guess you have sufficient time to set up the connection, as it happens only in the beginning of the session. 我猜你有足够的时间来建立连接,因为它只在会话开始时发生。 Therefore the protocol is not of much relevance. 因此,该协议没有多大意义。

But consider that HTTP is a stateless protocol and not suitable for audio streaming. 但请考虑HTTP是无状态协议,不适合音频流。 There are a couple of real time streaming protocols you can choose from. 您可以选择几种实时流媒体协议。 All of them will work over TCP or UDP (eg use raw sockets), and there are plenty of implementations. 所有这些都可以通过TCP或UDP工作(例如使用原始套接字),并且有很多实现。

In your case, the bottleneck with latency is not the server but the network itself. 在您的情况下,延迟的瓶颈不是服务器而是网络本身。 The connection between an iOS device and a wireless access point (AP) eats up about 40ms if the AP is not misconfigured and connection is good. 如果AP配置错误且连接良好,则iOS设备和无线接入点(AP)之间的连接会耗尽大约40毫秒。 (ping your iPhone.) In total, you'd have a minimum of 80ms for the path iOS -> AP -> Server -> AP -> iOS. (ping你的iPhone。)总的来说,路径iOS - > AP - > Server - > AP - > iOS总共至少有80ms。 But it is difficult to keep that latency stable. 但是很难保持这种延迟稳定。 (Typical latency of AirPlay on my local network is about 300ms.) (我本地网络上AirPlay的典型延迟时间约为300毫秒。)

I think live music over iOS devices is not practicable today. 我认为iOS设备上的现场音乐今天不可行。 Try skype between two iOS devices and look how close you can get to 50ms. 尝试两个iOS设备之间的Skype,看看你有多接近50毫秒。 I'd bet no one can do it significantly better, what concerns latency. 我敢打赌,没有人可以做得更好,有什么关系延迟。

Update: New research result! 更新:新的研究成果!

I have to revise my claims regarding the latency of wifi connections of the iDevice. 我必须修改关于iDevice的wifi连接延迟的声明。 Apparently when you first ping your device, latency will be bad. 显然,当您第一次ping您的设备时,延迟会很糟糕。 But if I ping again no later than 200ms after that, I see an average latency 2ms-3ms between AP and iDevice. 但是如果我在不迟于200ms之后再次ping通,我会看到AP和iDevice之间的平均延迟为2ms-3ms。

My interpretation is that if there is no communication between AP and iDevice for more than 200ms, the network adapter of the iDevice will go to a less responsive sleep mode, probably to save battery power. 我的解释是,如果AP和iDevice之间没有通信超过200ms,iDevice的网络适配器将进入反应较慢的睡眠模式,可能是为了节省电池电量。

So it seems, live music is within reach again... :-) 所以看来,现场音乐再次触手可及...... :-)

Update 2 更新2

The ping-interval required for keep alive of low latency apparently differs from device to device. 保持低延迟所需的ping间隔显然因设备而异。 The reported 200ms is for an 3rd gen. 报道的200毫秒是第三代。 iPad. iPad兼容。 For my iPhone 4 it's more like 50ms. 对于我的iPhone 4,它更像是50ms。

While streaming audio you probably don't need to bother with this, as data is exchanged on a more frequent basis. 虽然流媒体音频你可能不需要为此烦恼,因为数据的交换频率更高。 In my own context, I have sparse communication between an iDevice and a server, but low latency is crucial. 在我自己的上下文中,我在iDevice和服务器之间进行稀疏通信,但低延迟是至关重要的。 A keep alive therefore is the way to go. 因此,保持活力是可行的方法。

Best, Peter 最好,彼得

First off, you are not going to get sub 50 ms latency. 首先,您不会获得低于50毫秒的延迟。 Others have tried this. 其他人试过这个。 See for example http://ejamming.com/ a service that attempts to do what you are doing, but has a musically noticeable delay over the line and is therefore, in the ears of many, completely unusable. 例如,参见http://ejamming.com/这是一项试图做你正在做的事情的服务,但是在线路上有一个音乐明显的延迟,因此,在许多人看来,它完全无法使用。 They use special routing techniques to get the latency as low as possible and last I heard their service doesn't work with some router configurations. 他们使用特殊的路由技术来尽可能降低延迟,最后我听说他们的服务不适用于某些路由器配置。

Secondly, what language you use on server probably doesn't make much difference, as the delay from client to server will be worse than any delay caused by your service, but if I understand your service correctly, you are going to need a lot of servers (or server threads) just relaying audio data between clients or doing some sort of minimal mixing. 其次,你在服务器上使用的语言可能没什么区别,因为从客户端到服务器的延迟将比你的服务造成的任何延迟更糟,但如果我理解你的服务正确,你将需要大量的服务器(或服务器线程)只是在客户端之间中继音频数据或进行某种最小程度的混合。 This is a small amount of work per connection, but a lot of connections, so you need something that can handle that. 这是每个连接的少量工作,但是很多连接,所以你需要能够处理它的东西。 I would lean towards something like Java, Scala, or maybe Go. 我倾向于像Java,Scala或者Go这样的东西。 I could be wrong, but I don't think this is a good use-case for node, which, as I understand it, does not do multithreading well at this time. 我可能是错的,但我认为这不是一个很好的节点用例,据我所知,它目前还不能很好地进行多线程处理。 Also, don't poo-poo C++, scalable services have been built C++. 另外,不要poo-poo C ++,可扩展服务已经构建了C ++。 You could also build the relay part of the service in C++ and the rest in whatever. 您还可以使用C ++构建服务的中继部分,其余部分也可以构建。

Third, when choosing a compression format, you'll have to choose one that can survive packet loss if you plan to use UDP, and I think UDP is the only way to go for this. 第三,在选择压缩格式时,如果计划使用UDP,则必须选择能够在丢包中幸存的格式,我认为UDP是唯一可行的方法。 I don't think vorbis is up to this task, but I could be wrong. 我不认为vorbis能胜任这项任务,但我可能错了。 Off the top of my head, I'm not sure of anything that works on the iPhone and is UDP friendly, but I'm sure there are lots of things. 在我的头脑中,我不确定在iPhone上有什么作用并且是UDP友好的,但我确信有很多东西。 Speex is an example and is open-source. Speex就是一个例子,是开源的。 Not sure if the latency and quality meet your needs. 不确定延迟和质量是否满足您的需求。

Finally, to be blunt, I think there are som other things you should research a bit more. 最后,说实话,我认为还有其他事情你应该再研究一下。 eg. 例如。 DNS is usually cached locally and not checked every http call (though it may depend on the system/library. At least most systems cache dns locally). DNS通常在本地缓存,而不是每次http调用都检查(尽管它可能取决于系统/库。至少大多数系统在本地缓存dns)。 Also, there is no such protocol as TCP/UDP. 此外,没有TCP / UDP这样的协议。 There is TCP/IP (sometimes just called TCP) and UDP/IP (sometimes just called UDP). 有TCP / IP(有时称为TCP)和UDP / IP(有时称为UDP)。 You seem to refer to the two as if they are one. 你似乎把它们称为两者。 The difference is very important for what you are doing. 差异对于你正在做的事情非常重要。 For example, HTTP runs on top of TCP, not UDP, and UDP is considered "unreliable", but has less overhead, so it's good for streaming. 例如,HTTP运行在TCP之上,而不是UDP,UDP被认为是“不可靠的”,但开销较少,因此它对流式传输很有用。

Edit: speex 编辑:speex

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

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