VoIP项目总结

两年前做过VoIP的项目,当然那时候iOS8还没出现,自从iOS8出现后,VoIP的通知的方式好像改成了推送通知了,之后的实现方式我也没怎么研究了,所以这篇文章是只针对基于iOS8之前的SDK开发的项目的。

VoIP后台模式对应用有什么影响

一般来说应用进入后台后,由background到suspended,程序代码是不能够执行的,所以如果服务器给客户端发送的消息客户端也是无法处理的,并且如果屏幕锁屏,网络连接会断掉(我自己测试是这样的)。那么增加VoIP后台模式的作用有两个:1、我们可以配置一个连接,程序进入后台后,可以让系统接管这个连接,当服务器发送消息给客户端时,系统会将我们的程序唤醒,给我们一段时间运行我们的代码,一般这个通知都是通知客户端有网络电话呼入,所以我们标准的作法是发送一个本地通知,提醒用户接听这个呼入电话。2、VoIP后台模式可以保证应用在系统重启的时候自动启动,我们启动之后应该立刻连接服务器,配置连接,使其可以被系统接管。另外,如果我们的程序被杀死的时候的退出返回值(也就是exit的返回值)是一个非零值(非零值一般来说是由于内存的压力导致的系统把我们的应用kill了),那么我们的应用也会被自动重启。所以可以认为只要不是用户手动把我们的应用杀掉,那么我们VoIP类型的应用就可以重启,随时监听着别人的来电。

苹果文档中相关的说明:Because VoIP apps need to stay running in order to receive incoming calls, the system automatically relaunches the app if it exits with a nonzero exit code. (This type of exit could happen when there is memory pressure and your app is terminated as a result.) However, terminating the app also releases all of its sockets, including the one used to maintain the VoIP service connection. Therefore, when the app is launched, it always needs to create its sockets from scratch.

为什么一般VoIP后台模式都要伴随着audio后台模式

audio后台模式只是应用在后台播放和录制音乐,也就是无论你是网络边下载边播放或者只是播放本地的音乐,只要你在播放或者录音,那么应用在后台就不会suspended,我们VoIP的应用为了模仿真实的打电话或者说为了方便用户边聊天边浏览其他的东西,那么我们也要支持在后台播放声音和录制声音。不止这些,因为我们VoIP应用在通话状态下是要一直有网络数据流动的,如果程序不在前台,被suspended的话,那么网络数据就无法传输,也就无法使用。有人会问之前不是说可以让系统接管这个连接么?这个连接并非那个连接,那个被接管的连接是用来通知有网络电话呼入,一般是连接的逻辑服务器,而这个传输语音的服务器一般来说是语音的服务器,一般来说都是分开的。而且VoIP只能让系统接管一个连接,不可以接管多个。

苹果文档中有相关的说明:When the UIBackgroundModes key contains theaudio value, the system’s media frameworks automatically prevent the corresponding app from being suspended when it moves to the background. As long as it is playing audio or video content or recording audio content, the app continues to run in the background. However, if recording or playback stops, the system suspends the app.

苹果的文档

苹果对于VoIP的介绍的很详细,我上面写的东西也是基于对于文档的理解和自己的测试。

Tips for developing a VoIP App

A Voice over Internet Protocol (VoIP) app allows the user to make phone calls using an Internet connection instead of the device’s cellular service. Such an app needs to maintain a persistent network connection to its associated service so that it can receive incoming calls and other relevant data. Rather than keep VoIP apps awake all the time, the system allows them to be suspended and provides facilities for monitoring their sockets for them. When incoming traffic is detected, the system wakes up the VoIP app and returns control of its sockets to it.

There are several requirements for implementing a VoIP app:

  1. Enable the Voice over IP background mode for your app. (Because VoIP apps involve audio content, it is recommended that you also enable the Audio and AirPlay background mode.) You enable background modes in the Capabilities tab of your Xcode project.
  2. Configure one of the app’s sockets for VoIP usage.
  3. Before moving to the background, call the setKeepAliveTimeout:handler: method to install a handler to be executed periodically. Your app can use this handler to maintain its service connection.
  4. Configure your audio session to handle transitions to and from active use.
  5. To ensure a better user experience on iPhone, use the Core Telephony framework to adjust your behavior in relation to cell-based phone calls; see Core Telephony Framework Reference.
  6. To ensure good performance for your VoIP app, use the System Configuration framework to detect network changes and allow your app to sleep as much as possible.

Enabling the VoIP background mode lets the system know that it should allow the app to run in the background as needed to manage its network sockets. This key also permits your app to play background audio (although enabling the Audio and AirPlay mode is still encouraged). An app that supports this mode is also relaunched in the background immediately after system boot to ensure that the VoIP services are always available.

In order for your app to maintain a persistent connection while it is in the background, you must tag your app’s main communication socket specifically for VoIP usage. Tagging this socket tells the system that it should take over management of the socket when your app is suspended. The handoff itself is totally transparent to your app. And when new data arrives on the socket, the system wakes up the app and returns control of the socket so that the app can process the incoming data.

You need to tag only the socket you use for communicating with your VoIP service. This is the socket you use to receive incoming calls or other data relevant to maintaining your VoIP service connection. Upon receipt of incoming data, the handler for this socket needs to decide what to do. For an incoming call, you likely want to post a local notification to alert the user to the call. For other noncritical data, though, you might just process the data quietly and allow the system to put your app back into the suspended state.

VoIP还有几个内容

1、要在应用进入后台时设置setKeepAliveTimeout:handler:,这个可以保证在一定时间内(我们一般设置600s,苹果要求不能低于这个值)这个handler中的代码被执行1次,每次被执行时都有10s钟的时间做事情,在这里我们一般都是检查连接是不是断开了或者发送心跳包之类的事情。

下面两个内容我体会不是很深刻,就直接贴原文了:

Configuring Your App’s Audio Session

As with any background audio app, the audio session for a VoIP app must be configured properly to ensure the app works smoothly with other audio-based apps. Because audio playback and recording for a VoIP app are not used all the time, it is especially important that you create and configure your app’s audio session object only when it is needed. For example, you would create the audio session to notify the user of an incoming call or while the user was actually on a call. As soon as the call ends, you would then remove strong references to the audio session and give other audio apps the opportunity to play their audio.

For information about how to configure and manage an audio session for a VoIP app, see Audio Session Programming Guide.

Using the Reachability Interfaces to Improve the User Experience

Because VoIP apps rely heavily on the network, they should use the reachability interfaces of the System Configuration framework to track network availability and adjust their behavior accordingly. The reachability interfaces allow an app to be notified whenever network conditions change. For example, a VoIP app could close its network connections when the network becomes unavailable and recreate them when it becomes available again. The app could also use those kinds of changes to keep the user apprised about the state of the VoIP connection.

To use the reachability interfaces, you must register a callback function with the framework and use it to track changes. To register a callback function:

  1. Create a SCNetworkReachabilityRef structure for your target remote host.

  2. Assign a callback function to your structure (using theSCNetworkReachabilitySetCallback function) that processes changes in your target’s reachability status.

  3. Add that target to an active run loop of your app (such as the main run loop) using the SCNetworkReachabilityScheduleWithRunLoop function.

Adjusting your app’s behavior based on the availability of the network can also help improve the battery life of the underlying device. Letting the system track the network changes means that your app can let itself go to sleep more often.

For more information about the reachability interfaces, see System Configuration Framework Reference.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值