Music and resume troubles

Hi all,

I’d like some help to understand what is going on with my game.
I’m testing it on:

  • iPhone 4s with iOS 5.0
  • iPhone 3GS with iOS 4.3.5
    and using Corona build 2012.744

On the iPhone 4s I don’t have any issue, while on the 3GS I have several, listed in the following:

  1. stuttering background music
    It happens often in game start up and resume, but not only, and I’m using:
    gameMusic = audio.loadStream(“game_theme.mp3”, {bufferSize=8192, maxQueueBuffers=12, startupBuffers=4, buffersQueuedPerUpdate=2})

The mp3 is encoded at 96kbps.
I’m suspecting this depends in some way from ads (I’m using inneractive) because I see some improvements when I disable them.
Is there anything I can do/investigate to solve this issue?
2) the app often quits while resuming from background

Here is the typical crashlog:

Incident Identifier: 60A5D03E-8E3C-4D9B-8D1D-88215EDAAB4F  
CrashReporter Key: 8e084f52a2b8340b9008d671420ef85de31af4e8  
Hardware Model: iPhone2,1  
Process: match3 [1909]  
Path: /var/mobile/Applications/1471F573-29A7-43B3-911E-EF89D1C43B2C/match3.app/match3  
Identifier: match3  
Version: ??? (???)  
Code Type: ARM (Native)  
Parent Process: launchd [1]  
  
Date/Time: 2012-03-10 11:16:30.878 +0100  
OS Version: iPhone OS 4.3.5 (8L1)  
Report Version: 104  
  
Exception Type: 00000020  
Exception Codes: 0x8badf00d  
Highlighted Thread: 0  
  
Application Specific Information:  
match3 failed to resume in time  
  
Elapsed total CPU time (seconds): 9.560 (user 5.040, system 4.520), 94% CPU   
Elapsed application CPU time (seconds): 3.390, 33% CPU  
  
Thread 0 name: Dispatch queue: com.apple.main-thread  
Thread 0:  
0 libsystem\_kernel.dylib 0x32856c00 mach\_msg\_trap + 20  
1 libsystem\_kernel.dylib 0x32856758 mach\_msg + 44  
2 QuartzCore 0x333801fe CA::Render::wait\_fences(unsigned int, unsigned long) + 42  
3 QuartzCore 0x333572b8 CA::Context::commit\_transaction(CA::Transaction\*) + 456  
4 QuartzCore 0x33356fd0 CA::Transaction::commit() + 184  
5 QuartzCore 0x33362df8 CA::Transaction::flush() + 40  
6 QuartzCore 0x33362dc6 +[CATransaction flush] + 22  
7 UIKit 0x31bcdea0 PrepareAppAndDoSpringboardSnapshottingWithBlock + 544  
8 UIKit 0x31bcdc66 -[UIApplication \_saveSnapshotWithName:] + 42  
9 UIKit 0x31bccf10 -[UIApplication \_handleApplicationSuspend:eventInfo:] + 1028  
10 UIKit 0x31b66af2 -[UIApplication handleEvent:withNewEvent:] + 1910  
11 UIKit 0x31b6620e -[UIApplication sendEvent:] + 38  
12 UIKit 0x31b65c4c \_UIApplicationHandleEvent + 5084  
13 GraphicsServices 0x311a5e70 PurpleEventCallback + 660  
14 GraphicsServices 0x311a5efa PurpleEventSignalCallback + 10  
15 CoreFoundation 0x334e7a72 \_\_CFRUNLOOP\_IS\_CALLING\_OUT\_TO\_A\_SOURCE0\_PERFORM\_FUNCTION\_\_ + 6  
16 CoreFoundation 0x334e9758 \_\_CFRunLoopDoSources0 + 376  
17 CoreFoundation 0x334ea4e4 \_\_CFRunLoopRun + 224  
18 CoreFoundation 0x3347aebc CFRunLoopRunSpecific + 224  
19 CoreFoundation 0x3347adc4 CFRunLoopRunInMode + 52  
20 GraphicsServices 0x311a5418 GSEventRunModal + 108  
21 GraphicsServices 0x311a54c4 GSEventRun + 56  
22 UIKit 0x31b90d62 -[UIApplication \_run] + 398  
23 UIKit 0x31b8e800 UIApplicationMain + 664  
24 match3 0x000edb5c 0xeb000 + 11100  
25 match3 0x000edb14 0xeb000 + 11028  
  
Thread 1 name: Dispatch queue: com.apple.libdispatch-manager  
Thread 1:  
0 libsystem\_kernel.dylib 0x32859fbc kevent + 24  
1 libdispatch.dylib 0x311d2032 \_dispatch\_mgr\_invoke + 706  
2 libdispatch.dylib 0x311d303a \_dispatch\_queue\_invoke + 86  
3 libdispatch.dylib 0x311d25ea \_dispatch\_worker\_thread2 + 186  
4 libsystem\_c.dylib 0x3370258a \_pthread\_wqthread + 258  
5 libsystem\_c.dylib 0x33702bbc start\_wqthread + 0  
  
Thread 2 name: WebThread  
Thread 2:  
0 libsystem\_kernel.dylib 0x32856c00 mach\_msg\_trap + 20  
1 libsystem\_kernel.dylib 0x32856758 mach\_msg + 44  
2 CoreFoundation 0x334e82b8 \_\_CFRunLoopServiceMachPort + 88  
3 CoreFoundation 0x334ea562 \_\_CFRunLoopRun + 350  
4 CoreFoundation 0x3347aebc CFRunLoopRunSpecific + 224  
5 CoreFoundation 0x3347adc4 CFRunLoopRunInMode + 52  
6 WebCore 0x3555c27e RunWebThread(void\*) + 382  
7 libsystem\_c.dylib 0x3370130a \_pthread\_start + 242  
8 libsystem\_c.dylib 0x33702bb4 thread\_start + 0  
  
Thread 3:  
0 libsystem\_kernel.dylib 0x32856c00 mach\_msg\_trap + 20  
1 libsystem\_kernel.dylib 0x32856758 mach\_msg + 44  
2 CoreFoundation 0x334e82b8 \_\_CFRunLoopServiceMachPort + 88  
3 CoreFoundation 0x334ea562 \_\_CFRunLoopRun + 350  
4 CoreFoundation 0x3347aebc CFRunLoopRunSpecific + 224  
5 CoreFoundation 0x3347adc4 CFRunLoopRunInMode + 52  
6 Foundation 0x326be7f6 +[NSURLConnection(NSURLConnectionReallyInternal) \_resourceLoadLoop:] + 206  
7 Foundation 0x326b1382 -[NSThread main] + 38  
8 Foundation 0x327235c6 \_\_NSThread\_\_main\_\_ + 966  
9 libsystem\_c.dylib 0x3370130a \_pthread\_start + 242  
10 libsystem\_c.dylib 0x33702bb4 thread\_start + 0  
  
Thread 4 name: AURemoteIO::IOThread  
Thread 4:  
0 libsystem\_kernel.dylib 0x32856c00 mach\_msg\_trap + 20  
1 libsystem\_kernel.dylib 0x32856758 mach\_msg + 44  
2 AudioToolbox 0x3361e824 AURemoteIO::IOThread::Run() + 80  
3 AudioToolbox 0x33624342 AURemoteIO::IOThread::Entry(void\*) + 2  
4 AudioToolbox 0x3355948a CAPThread::Entry(CAPThread\*) + 138  
5 libsystem\_c.dylib 0x3370130a \_pthread\_start + 242  
6 libsystem\_c.dylib 0x33702bb4 thread\_start + 0  
  
Thread 5 name: com.apple.CFSocket.private  
Thread 5:  
0 libsystem\_kernel.dylib 0x32858c60 \_\_select + 20  
1 CoreFoundation 0x334ed8f2 \_\_CFSocketManager + 582  
2 libsystem\_c.dylib 0x3370130a \_pthread\_start + 242  
3 libsystem\_c.dylib 0x33702bb4 thread\_start + 0  
  
Thread 6:  
0 libsystem\_kernel.dylib 0x328593ec \_\_workq\_kernreturn + 8  
1 libsystem\_c.dylib 0x337026d8 \_pthread\_wqthread + 592  
2 libsystem\_c.dylib 0x33702bbc start\_wqthread + 0  
  
Unknown thread crashed with unknown flavor: 5, state\_count: 1  

I also have a question about memory.
The average memory level of the game (with collectgarbage(‘count’)) is about 900, the maximum level 1300.
Are they good? Is there a maximum we should keep in mind to avoid memory warnings?
I’m particularly interested because I frequently see memory warning on the iPhone 3GS.

Thanks in advance!
[import]uid: 47531 topic_id: 23098 reply_id: 323098[/import]

audio.loadStream - had similar problem with this function in older daily build, go for newer version of Corona. [import]uid: 12704 topic_id: 23098 reply_id: 92395[/import]

Sadly today, after posting the message, I’ve already tried the last build 765 but nothing has changed.

The problem is still affecting my game on iPhone 3GS :frowning: [import]uid: 47531 topic_id: 23098 reply_id: 92421[/import]

Please read the Audio Notes page for performance tuning. Try focusing on setting lower sampling rates like 22kHz.
http://developer.anscamobile.com/partner/audionotes

If that fails, you might try experimenting with changing the values of one or more of the parameters:
{bufferSize=8192, maxQueueBuffers=12, startupBuffers=4, buffersQueuedPerUpdate=2}
Again this is on the audio notes page.
As for the crashing, your log says:
“match3 failed to resume in time”
This means Apple killed your process because it took too long to resume. It’s hard to say what caused it. The main thread is stuck somewhere in graphics. Thread 5 seems to be waiting for network information. [import]uid: 7563 topic_id: 23098 reply_id: 92551[/import]

Thank you for your advices Ewing.
I spent today testing various audio settings.

I changed the size and number of start buffers with no result for the stuttering.
Finally I managed to solve this issue encoding the soundtrack music in mono, 46kbps.

As for the resume issue, apparently lowering the quality of the music has had also a positive impact on resume time, but I still have to do extensive test on this. In fact the thread 4 in the crashlog seems related to audio.
Do you think it is possible that the “match3 failed to resume in time” error depended solely on music buffering/decoding?
[import]uid: 47531 topic_id: 23098 reply_id: 92630[/import]

So I moved to build 765, lowered the quality of the background music and started again to test on both the iPhone devices (and the Corona simulator).

Currently I have no more stuttering for the audio, but I’m always experiencing crashes on iPhone 3GS (iOS 4.3.5). The crashes are now different from the “failed to resume in time” error. I don’t know if they are linked some way with suspend/resume activities, but they become more frequent around these events.
I’m really stuck with this because I cannot reproduce these crashes neither on the iPhone 4s nor on the simulator. So I don’t know what to address to correct the issue.

I’d appreciate ANY input on this.
The crashlog has changed and now I have 2 typical types:

Incident Identifier: 5DB848EE-B812-4F52-A21A-E2FFCA73F5C3  
CrashReporter Key: 8e084f52a2b8340b9008d671420ef85de31af4e8  
Hardware Model: iPhone2,1  
Process: match3 [1417]  
Path: /var/mobile/Applications/26892F3C-FE9B-48E9-A358-82D8DE97ACAD/match3.app/match3  
Identifier: match3  
Version: ??? (???)  
Code Type: ARM (Native)  
Parent Process: launchd [1]  
  
Date/Time: 2012-03-11 23:47:31.781 +0100  
OS Version: iPhone OS 4.3.5 (8L1)  
Report Version: 104  
  
Exception Type: EXC\_BAD\_ACCESS (SIGSEGV)  
Exception Codes: KERN\_INVALID\_ADDRESS at 0x1231fadc  
Crashed Thread: 0  
  
Thread 0 name: Dispatch queue: com.apple.main-thread  
Thread 0 Crashed:  
0 libobjc.A.dylib 0x309b6c9a objc\_msgSend + 18  
1 CoreFoundation 0x3380a208 \_\_CFXNotificationPost\_old + 396  
2 CoreFoundation 0x337a4ee4 \_CFXNotificationPostNotification + 112  
3 Foundation 0x329ca5cc -[NSNotificationCenter postNotificationName:object:userInfo:] + 64  
4 UIKit 0x31ea28aa -[UIDevice setOrientation:animated:] + 138  
5 UIKit 0x31e91cee -[UIApplication handleEvent:withNewEvent:] + 2418  
6 UIKit 0x31e9120e -[UIApplication sendEvent:] + 38  
7 UIKit 0x31e90c4c \_UIApplicationHandleEvent + 5084  
8 GraphicsServices 0x314d0e70 PurpleEventCallback + 660  
9 CoreFoundation 0x33812a90 \_\_CFRUNLOOP\_IS\_CALLING\_OUT\_TO\_A\_SOURCE1\_PERFORM\_FUNCTION\_\_ + 20  
10 CoreFoundation 0x33814838 \_\_CFRunLoopDoSource1 + 160  
11 CoreFoundation 0x33815606 \_\_CFRunLoopRun + 514  
12 CoreFoundation 0x337a5ebc CFRunLoopRunSpecific + 224  
13 CoreFoundation 0x337a5dc4 CFRunLoopRunInMode + 52  
14 GraphicsServices 0x314d0418 GSEventRunModal + 108  
15 GraphicsServices 0x314d04c4 GSEventRun + 56  
16 UIKit 0x31ebbd62 -[UIApplication \_run] + 398  
17 UIKit 0x31eb9800 UIApplicationMain + 664  
18 match3 0x0009b008 0x98000 + 12296  
19 match3 0x0009afc0 0x98000 + 12224  

[code]
Incident Identifier: 4438A831-1AF3-45C0-954B-5046E15DFD76
CrashReporter Key: 8e084f52a2b8340b9008d671420ef85de31af4e8
Hardware Model: iPhone2,1
Process: match3 [1394]
Path: /var/mobile/Applications/26892F3C-FE9B-48E9-A358-82D8DE97ACAD/match3.app/match3
Identifier: match3
Version: ??? (???)
Code Type: ARM (Native)
Parent Process: launchd [1]

Date/Time: 2012-03-11 23:39:31.977 +0100
OS Version: iPhone OS 4.3.5 (8L1)
Report Version: 104

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x1231fadc
Crashed Thread: 0

Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x309b6c9a objc_msgSend + 18
1 CoreFoundation 0x3380a208 __CFXNotificationPost_old + 396
2 CoreFoundation 0x337a4ee4 _CFXNotificationPostNotification + 112
3 Foundation 0x329ca5cc -[NSNotificationCenter postNotificationName:object:userInfo:] + 64
4 UIKit 0x31ed1178 -[UIApplication _setActivated:] + 196
5 UIKit 0x31ef7bf4 -[UIApplication _handleApplicationSuspend:eventInfo:] + 232
6 UIKit 0x31e91af2 -[UIApplication handleEvent:withNewEvent:] + 1910
7 UIKit 0x31e9120e -[UIApplication sendEvent:] + 38
8 UIKit 0x31e90c4c _UIApplicationHandleEvent + 5084
9 GraphicsServices 0x314d0e70 PurpleEventCallback + 660
10 CoreFoundation 0x33812a90 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 20
11 CoreFoundation 0x33814838 __CFRunLoopDoSource1 + 160
12 CoreFoundation 0x33815606 __CFRunLoopRun + 514
13 CoreFoundation 0x337a5ebc CFRunLoopRunSpecific + 224
14 CoreFoundation 0x337a5dc4 CFRunLoopRunInMode + 52
15 GraphicsServices 0x314d0418 GSEventRunModal + 108
16 GraphicsServices 0x314d04c4 GSEventRun + 56
17 UIKit 0x31ebbd62 -[UIApplication _run] + 398
18 UIKit 0x31eb9800 UIApplicationMain + 664
19 match3 0x000db008 0xd8000 + 12296
20 match3 0x000dafc0 0xd8000 + 12224
[/code] [import]uid: 47531 topic_id: 23098 reply_id: 92793[/import]

First, I want to make it clear that setting the MP3’s bitrate differently (96kbps) is not necessarily the same as changing the sampling rate/frequency (22kHz). If you are using 44kHz samples, you are still neglecting an easy optimization documented in Audio Notes.

Second, the audio thread is always running when you use audio. It’s hard to say if it’s blocked or not from your trace.

Third, I don’t know what you are doing in your code on resume. Typically, if you are playing just one stream, the overhead should really low because most of the stuff is still in memory when your app backgrounded. If you are calling loadSound or loadStream in your resume, then this might be where you are getting dinged. (Though loadStream shouldn’t be bad unless you have dozens of them.)

Another experiment you can try is to manually pause your audio going into a suspend, and then on resume, don’t resume immediately. Instead, use a timer to unpause the audio in say a second later just to see what happens. Since you said the ads are impacting performance, it could be that the system is just overwhelmed with too much to do at the same time.

Finally, if you are streaming multiple MP3s, then you may be using up the hardware decoder and falling back to software. This might be more painful on a 3GS. You might try encoding using IMA4 instead. (Again, on the Audio Notes page.)

[import]uid: 7563 topic_id: 23098 reply_id: 92831[/import]

Hi Ewing, thanks for your reply.

I’m going through all of your advice in the following, to explain what I’m doing.

First -> After your first post, the first thing I did was resampling my background music at 22kHz. This didn’t make any difference. So I tried to double the buffer size in the loadStream. This made a little difference, but not too much. After doing different other tests with the parameters, I went back to the defaults, and encoded the music resampled in 22kHz in mono 46kbps. This did the trick, i.e. I don’t have stuttering on iPhone 3GS.

Second -> Yes, I see.

Third -> In the game there is no handler registered on resume, and there is only a single background music. It is the same music going on from the start of the app, so you’re right and it should be still in memory.
The last two crashlogs you can read up above were obtained disabling ads.
So to summarize the current scenario, I think ads and music are not responsible of the situation but I’m still groping in the dark on how to solve the problem. [import]uid: 47531 topic_id: 23098 reply_id: 92850[/import]

Since watchdog is killing you, I would try to figure out approximately where you are getting killed. Some print statements or Instruments might help.
[import]uid: 7563 topic_id: 23098 reply_id: 92897[/import]