Random Thoughts and Musings of a
Mobile Application Developer
Today I will post all my findings on the iOS 4 Background Audio and VoIP stuff, so other do not have the same troubles I went through
This tutorial is for Applications using a C library to handle sockets and not the High-Level Abstractions from Apple. If you want to do this, you should read the Documentation over at Apple here
First you need a new Key in your Info.plist stating your Background modes called UIBackgroundModes with the following two values:
Then you need to make sure, that all your UDP sockets are reopend once you resume from Background, because they may be no longer valid and may receive SIGPIPE. There are more than one way to handle this. One would be to ignore SIGPIPE by setting the socket option:
This should be done right after the socket systemcall.
You also could use the very good exception handler from Matt Gallagher you find here and simply ignore the Signal in this case. Also StackOverflow is a good source for other ways to do this. You still have to handle outcome though. If you try reading from a broken socket you will get either EPIPE, ECONNRESET or ENOTCONN, which you have to handle appropriately.
So, and now for the Fun part. Right after the syscall to connect on the tcp socket we attach a Read Stream to it and try to set it to kCFStreamNetworkServiceTypeVoIP, so that iOS knows it has to listen on that socket. Now all traffic arriving at that socket is reported, even if the App is in the Background and the phone is in standby. An example implementation would look like this:
When resetting or closing the socket, the corresponding read stream should also be closed.
Yeah, it is as easy as that, but it took me ages to figure out how to do this all properly.
As always, you can use this code and modify it as you see fit, but if you find it useful or found something wrong or missing, a comment would be nice.
In my app sending email using SMTP Implementation.I am using above code for socket connection for SMTP Mail Application in background.
I got an error from this code. I need to add any frameworks .Do you have any sample code.
setsockopt(m_bsdSocket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(set));
what error did you get exactly? Here is a very good tutorial on whats needed for Low-Level Network functions like setsockopt to work correctly: OReilly
We need to send email using SMTP Implementation in background. What i to do send the email in background.
I am using the above code to my app nothing happens in background.In first Line, i got an error of m_bsdSocket is undeclared.
i dont know if apple allows using the Background mode for other than playing music, using VoIP and location updates. It does not cover sending and receiving Mail in the Background (At least not from a non-Apple App).
But if you want to try anyways you have to declare your App as voip in your dist.plist as stated in the post (If you want to upload your app to the AppStore it may be rejected if you state using this for any other purpose than for Voice over IP). If you still wanna try, you have to hold up a steady TCP connection as shown in the above code. The connection has to stay open (that means, it cannot be closed from the server or client side, otherwise your app will be suspended and no longer will be notified of traffic to said tcp socket)
m_bsdSocket in this case refers to any udp socket you are using in your Application (because once you go to the background these will likley report SIGPIPE and then crash your App). That means, you have either created it with the socket system call or you pulled it out of a CF..Stream. Right after you did either of these, you would set the socket option to *SO_NOSIGPIPE* so that you application does not crash in case of the application going to the background.
It's a great article for voip on iphone, but I have one question on your code. From this comment, "you have to hold up a steady TCP connection", i am wondering if it is possible for hold up udp socket to be notified when the incoming SIP message like INVITE was fired. Because my SIP proxy server only using UDP protocol as communications through UAs,
I am sorry to say that this is not possible. UDP is stateless, so it is not possible to hold a connection open. Also it is not supported by iOS. iOS only reacts on messages to flagged TCP sockets. So the only way to make use of the iOS backgrounding is to use TCP socket connections.
Thanks for your quick reply.
Can you post your code about how to create a TCP socket or wrapper a TCP BSD socket to map the streams like
CFStreamCreatePairWithSocket (kCFAllocatorDefault, bsdSocket, &hReadStream, NULL);
I would like to know how you created bsdSocket for this case, thanks for your work. Because I read a thread from appForum:
there some argument about UDP and TCP to handle background voip issue.
If you already have a working readStream i.a. because you are using Objective C to open the connection, you do not need this function at all. But if you want to create a bsd socket you could use http://www.cs.put.poznan.pl/csobaniec/examples/sockets/ and create it like it is specified in the Client example. For an overview about BSD sockets and how they work you best read http://en.wikipedia.org/wiki/Berkeley_sockets
In my case we have a class which creates a socket connection to one of our Servers and holds this connection open by using the above code. I am sorry I cannot show you excerpts from this code, because it is proprietary and belongs to the company I work for.
But basically we are doing just what is mentioned in the above TCP Client example to get a socket connection on which we then attach the read stream.
On the Post you mentioned from the Apple Developer Forum. Afaik it is only possible to use TCP to get backgrounding working (There are some workarounds like playing a silent stream over UDP or sending UDP bursts in short intervals) but as far as I heard they either are not reliable or drain your battery because they use the processor a lot even in background.
I already did the background inbound call working following your comment, thanks you agian. I did not process SIGPIPE issue, because when there is a audio conversation, I open a upd socket, once conversation done, then I close it. I think there will be no UDP SIGPIPE issue with this way.
Now I am working on Wifi or 3G detection, because TCP socket need to be reset when internet got lost, when iphone walks out the range of the Wifi area, 3G is gonna be set up automatically, TCP socket will reset as well.
i tried using your code style for my VoIP app which uses only UDP sockets.
i put all your code in applicationdidenterbackground callback. The problem is that the app cannot receive any incoming call, the UDP receiving socket (paired in a CFReadStreamRef) seems closed when in background. I don't know if a SIGPIPE was received...
What do you suggest me?
It is not possible to receive any Data on UDP Sockets in Background. This is forbidden and not supported by Apple.
Can you show me an official document from Apple where this constraint is specified, please ?? I've seen only a distinction between listening sockets and data sockets.
However, if you download Media5 from the appstore you'll see that it works perfectly in background using only UDP sockets.
Media5 uses a trick to get their VoIP app receive incoming calls via UDP, but I can not disclose how they do it. I think the official Documentation was rephrased, but I still think you are not able to uphold a UDP connection in the background, but ATM I have no documentation to prove it to you. But if you search the net, the common tenor is, that it is not possible.
so Media5 needs to mantain "active" the app while in background... I suspect this involves a higher battery drain. From my knowledge you can keep an active task in background for maximum 10 minutes.
So, il Apple forbid using UDP sockets while in background, the only alternative is to keep alive the app. But this involves higher battery consuming...
just a notification: if I create in applicationDidEnterBackground of my voip app two asynchronous UDP sockets (one sending to the loopback interface on a certain port and the other one receiving on the same port) in a dispatch_async block, then Media5 can NOT receive in background any more. But my app can (always with UDP)!!
Moreover, if I put in foreground Media5 it crashes in few seconds... do you know how iOS manages UDP sockets in this situation? Media5 and my app works on different UDP sockets and different ports.
I mean, I launch contemporary both apps (mine and Media5) but it seems that socket priority of Media5 is "lower" than mine...
I read your tutorial carefully but I'm not sure where is [socket systemcall], and also [syscall to connect on the tcp socket], I don't know where should I add those code. I tried some place but did not work.
I hope you help me to fix my problem.
Those syscalls are not there, because the focus of this tutorial is to add backgrounding to your already existing socket code.
But if you have written socket code already, than the code from the tutorial is placed right after the connect syscall.
I followed all above steps, but I have two problems. When run app and connect to my sip account
1)then switch to background, application did not receive call(I checked app logs and see the don't listen)
2)app in foreground receive call and accept it, when switch app to background, iphone status bar show my app name and be in red!
Do you know what happen in my app?
The red Bar is displayed whenever your Application records Audio and goes into the Background. Why it does not trigger I cannot tell you. Are you trying on an actual device?
I created a TCP socket and straight after the connect got a valid read stream back, but CFReadStreamSetProperty keeps returning FALSE. I haven't tried this on a actual device yet. But is this meant to work in the emulator?
Last I checked background Audio and TCP did not work in the Simulator
Hi am kind of new to this voip application development but was in iOS app dev for the past 2 years.
And now i have been tasked to work on creating a new voip api.Can some one of you help me out from where exactly i need to begin this.
Thanks in Advance .
RELATED VIDEO: Model Behati Prinsloo Talks About Her Last Wardrobe Malfunction: Thanks For Bringing That Up Again, Guys!Sex and the City era, is enjoying an updated return to the spotlight in a burst of colours and with decorations such as pretty bows and tougher-looking studs.For her reception, Ashlee opted for another custom Houghton look, but turned the tables with a funky (and fitting) crop top and trousers combo.nine times out of ten, my hand extends towards her things.The mass ceremony at a promenade in San Juan's colonial district took the same-sex couples through the traditional marriage vows and exchange of rings.You could just tell that she and Dave were so in love with each other.with plans to continue her studies in Paris the following year, she was one of the oldest entrants.Affordable Long & Short Evening Dresses Under $100 Evening Dresses Product Evening Dresses Price I ve done a lot of classical work, but to have an opportunity to play a role that s quite traditional on paper, and then lift it outside that box it s been a great challenge and a huge pleasure.
Affordable Long & Short Evening Dresses Under $100 Evening Dresses Product Evening Dresses Price The Giveaway is sponsored by IBT Media Inc.rootless loner from the wrong side of town.The wedding will be heavily Quebec focused.For the cake on the previous page, Fonseka interpreted the opening Valentino haute couture gown with some creative license, because she doesn’t like to make exact copies.During those 10 minutes, the young woman’s wheezing turned to gasping, the pauses between the tortured gasps lengthened, and the infrequent, irregular and ever-so-slight movements of her head stopped.Under the new rules, Baumgartner was facing a maximum sentence of life with no chance of parole for 75 years.Princess Elizabeth and Prince Philip first met at his cousin’s wedding in 1934.I think that’s because my stories are more character-driven than plot-driven.
In talking to locals, Ms.Ryan spat upon arrival.The results follow the King Lear pattern.He warned the jury not to be influenced by negative portrayals in the Austrian media, which have had a field day with the story of the Hispanic immigrant and her many lovers.The rest of us may cluck, but even we fearfully wait for a sanctioned holiday like Halloween, when such exuberant flights of fancy become downright mainstream, or for the permissible occasion of a thematic fancy-dress party.