PJSUA and iOS background mode. App with VoIP capabilities dies after 180 second

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello guys,

anyone can help wit my iOS background problem of 180 seconds? 
Please, this thing is driving me crazy.

Thanks,
Filip

Sent from my iPhone

> On 05.06.2014, at 16:14, Filip Planinsky <filip at fonetize.com> wrote:
> 
> Hello to all PJSIP experts!
> 
> I am having some problem with PJSIP, iOS and Background mode.
> There is quite a lot of buzz around about such problems, but yet there seems to be no definitive solution.
> 
> When my App is put into background it lives approximately 180 seconds and then dies, regardless of the fact the the App is marked as VoIP and it works over TCP socket to connect to an Asterisk. The App is based on PJSUA.
> 
> I am fairly experienced with Asterisk and SIP (the protocol itself.)
> I also, have moderate experience with iOS Apps, but none up till now with PJSIP or background Apps.
> 
> Details:
> ------------
> 
> I am using the following software/hardware combination
> 
> 1. iPhone 4 with iOS 7.1.1 (11D201)
> 2. Mac Mini with Mac OS X 10.8.5
> 3. Asterisk 11.1.2, Copyright (C) 1999 - 2012 Digium, Inc.
> running on
> Linux 3.2.0-51-generic #77-Ubuntu SMP Wed Jul 24 20:18:19 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
> 4. XCode 5.1 (5B130a)
> 
> I am devloping an iOS VoIP App, which works fine, except when the App is in background.
> 
> The App has:
> 
> 1. Capabilities set: "VoIP" and "Audio and AirPlay" in Xcode project.
> 2. The App connects to the Asterisk over TCP socket on a non standart port, i.e. 7140, instead of 5060.
> 3. The Asterisk is configured to qualify the peer, which the App uses every 5 seconds. This means, that
> the Asterisk sends to the App an OPTIONS request every 5 seconds and the App responds with SIP/200 OK.
> (This is purely for Debuging reasons. I do understand it is an overkill in real situations.)
> 
> All is working OK - REGISTER, INVITE, OPTIONS, Audio stream in both directions.
> 
> When the App goes into background - it lives for approximately 180 seconds.
> This is the standard timeout given by Apple to any App going to background mode.
> The the TCP socket is closed and the App dies.
> So, this makes me think that the App never really enters VoIP background mode, because it dies as a regular App.
> 
> The App itself is very simple App, which uses PJSUA and just does:
> 1. pjsua_create()
> 2. configures the transport as follows:
>   status = pjsua_transport_create(PJSIP_TRANSPORT_TCP, &cfg, NULL);
> 3. adds an outbound proxy:
>   cfg.outbound_proxy[0] = pj_str("sip:XXX.XXX.XXX.XXX:7140;transport=tcp");
>   cfg.outbound_proxy_cnt = 1;
> 4. and starts the PJSUA with pjsua_start()
> 5. It also adds some handlers for incomming calls and other events.
> 6. It registers SIP peer with Asterisk by using the pjsua_acc_add() function.
> 
> As I said all is working fine until the App is put into background mode, when it dies after 180 seconds.
> 
> What I have done up till now to investigate:
> -----------------------------------------------------------
> 
> 1. I have added some detailed debug logs to the activesock.c: activesock_create_iphone_os_stream() function.
> These logs show me that CFReadStreamSetProperty(asock->readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP)
> is executed properly without errors.
> 
> And this should be enough for the App to stay alive in background, but obviously it isn't.
> 
> 2. I have created a very-very simple App, not related to PJSIP, which is marked as VoIP and uses a TCP socket to send packets to some server.
> No SIP or PJSIP involved here. Just a pure test with some plain TCP packets.
> This simple App is working perfectly OK in background, just by marking the input stream with NSStreamNetworkServiceTypeVoIP.
> I my simple App I am using "NSStreamNetworkServiceTypeVoIP", because I am bridging to NSInputStream, fi that should matter at all, where as I can see that PJSIP uses the CF classes.
> 
> 3. I have made a few tcpdumps on the Asterisk, while running my PJSIP App.
> In these dumps I can see how every 5 seconds the Asterisk sends OPTIONS and the App replies with SIP/200 OK.
> (Also the corresponding TCP ACKs are visible in the dump)
> After about 180 seconds, I can see that the Asterisk sends OPTIONS request to the App.
> Instead of answering with SIP/200 OK, I can see only a single TCP ACK coming from the App. This is pure TCP stack response, probably provided by iOS.
> 
> The next event is puzzling.
> The App side sends FIN/ACK - which means that the App now wants to close the TCP socket. So, obviously - there is something alive on the App side and it intentionally wants to close the TCP socket. My guess is that this is the iOS itself and not the PJSIP user agent.
> Of course, the Asterisk side responds with TCP FIN/ACK and the socket dies.
> 
> *** This makes me think, that after 180 seconds the socket is still Alive in the iOS TCP stack, however either the SIP worker thread in PJSIP is dead or not woken up by iOS in order to generate the SIP/200 OK response. Or it is just killed by iOS. ****
> 
> I also know that there is SIP worker thread in PJSIP, however I do not know how iOS handles such worker threads in background mode.
> Generally, the iOS need to wake up the App every time there is incoming traffic on the TCP socket.
> But what means wake up, which threads are woken is a mystery to me.
> Maybe, if PJSIP uses some worker threads to answer SIP requests, the iOS cannot wake them up?
> I don't know - this is just wild guessing? and probably wrong ? 
> 
> I found some similar case here:
> 
> http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2012-February/014122.html
> http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2012-February/014124.html
> http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2012-February/014127.html
> 
> but I do not see how keep alive on 10 minutes (re-REGISTER) would solve a problem, where the App is dead only after 3 minutes.
> 
> 
> If anyone has any idea, please help!
> Maybe, I am missing some configuration in PJSIP?
> For example, I configure TCP transport, but I do not explicitly forbid UDP - is such step possible or needed?
> 
> 
> I am really puzzled!
> 
> Thanks,
> Filip
> 
> 
> 



[Index of Archives]     [Asterisk Users]     [Asterisk App Development]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [Linux API]
  Powered by Linux