AVAudioEngine crash - required condition is false: hwFormat

Hi,


We recently started using AVAudioEngine in our app, and are receiving reports of crashes in the wild, specifically:

com.apple.coreaudio.avfaudio

required condition is false: hwFormat

These crashes are occurring on all iOS versions, including the latest (10.0.2 14A456).

The crashes are always on a background thread. Here is one example stack trace:

(CoreFoundation + 0x0012f1c0 )  __exceptionPreprocess
(libobjc.A.dylib + 0x00008558 )  objc_exception_throw
(CoreFoundation + 0x0012f090 )  +[NSException raise:format:arguments:]
(AVFAudio + 0x00016788 )  AVAE_RaiseException(NSString*, ...)
(AVFAudio + 0x0008f168 )  AVAudioIOUnit::_GetHWFormat(unsigned int, unsigned int*)
(AVFAudio + 0x0008ee64 )  ___ZN13AVAudioIOUnit22IOUnitPropertyListenerEPvP28OpaqueAudioComponentInstancejjj_block_invoke_2
(libdispatch.dylib + 0x000011fc )  _dispatch_call_block_and_release
(libdispatch.dylib + 0x000011bc )  _dispatch_client_callout
(libdispatch.dylib + 0x0000f440 )  _dispatch_queue_serial_drain
(libdispatch.dylib + 0x000049a4 )  _dispatch_queue_invoke
(libdispatch.dylib + 0x00011388 )  _dispatch_root_queue_drain
(libdispatch.dylib + 0x000110e8 )  _dispatch_worker_thread3
(libsystem_pthread.dylib + 0x000012c4 )  _pthread_wqthread
(libsystem_pthread.dylib + 0x00000db0 )  start_wqthread

When this crash occurs, the main thread is generally responding to an audio route change. Here is one example stack trace:

(libsystem_kernel.dylib + 0x0000116c )  mach_msg_trap
(libsystem_kernel.dylib + 0x00000fd8 )  mach_msg
(libdispatch.dylib + 0x0001cac0 )  _dispatch_mach_msg_send
(libdispatch.dylib + 0x0001c214 )  _dispatch_mach_send_drain
(libdispatch.dylib + 0x0001d414 )  _dispatch_mach_send_push_and_trydrain
(libdispatch.dylib + 0x000174a8 )  _dispatch_mach_send_msg
(libdispatch.dylib + 0x000175cc )  dispatch_mach_send_with_result
(libxpc.dylib + 0x00002c80 )  _xpc_connection_enqueue
(libxpc.dylib + 0x00003cf0 )  xpc_connection_send_message_with_reply
(MediaRemote + 0x00011edc )  MRMediaRemoteServiceGetPickedRouteHasVolumeControl
(MediaPlayer + 0x0009d57c )  -[MPAVRoutingController _pickableRoutesDidChangeNotification:]
(CoreFoundation + 0x000c9228 )  __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
(CoreFoundation + 0x000c892c )  _CFXRegistrationPost
(CoreFoundation + 0x000c86a8 )  ___CFXNotificationPost_block_invoke
(CoreFoundation + 0x00137b98 )  -[_CFXNotificationRegistrar find:object:observer:enumerator:]
(CoreFoundation + 0x0000abf0 )  _CFXNotificationPost
(Foundation + 0x000066b8 )  -[NSNotificationCenter postNotificationName:object:userInfo:]
(MediaServices + 0x00003100 )  -[MSVDistributedNotificationObserver _handleDistributedNotificationWithNotifyToken:]
(MediaServices + 0x00002f10 )  __78-[MSVDistributedNotificationObserver initWithDistributedName:localName:queue:]_block_invoke
(libsystem_notify.dylib + 0x00009ea4 )  ___notify_dispatch_local_notification_block_invoke
(libdispatch.dylib + 0x000011fc )  _dispatch_call_block_and_release
(libdispatch.dylib + 0x000011bc )  _dispatch_client_callout
(libdispatch.dylib + 0x00005d68 )  _dispatch_main_queue_callback_4CF
(CoreFoundation + 0x000dcf28 )  __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
(CoreFoundation + 0x000dab14 )  __CFRunLoopRun
(CoreFoundation + 0x00009044 )  CFRunLoopRunSpecific
(GraphicsServices + 0x0000c194 )  GSEventRunModal
(UIKit + 0x0007b624 )  -[UIApplication _run]
(UIKit + 0x0007635c )  UIApplicationMain


This looks like the same issue as https://forums.developer.apple.com/message/36184#36184


One potential complication is that we are calling AVAudioEngine methods off of the main thread. My belief is that this is safe - but I can't find any official reference to confirm that it is. We find that AVAudioEngine method calls can block, which is why we moved the work off the main thread.


We are listening for audio engine configuration change notifications and handling them similarly to the AVAudioEngine sample code.


I have attempted to reproduce this issue locally by performing various actions in combination (receiving phone calls, suspending the app, plugging in or unplugging headphones, etc) with no luck.


Any thoughts on what conditions might be triggering this exception? Hopefully, I can at least narrow down a set of conditions to allow me to reproduce the crash in a controlled environment.


Thanks,

Rob

Have you found the issue for this crash? And solved it? Experience the same crash... :/

It ended up being a weird interaction with another part of our app which was doing audio recording for speech recognition. The other component initialized an audio unit and didn't tear it down properly, and this caused a crash when we happened to be using AVAudioEngine to play something at the same time.


So, this advice is pretty vague, but try checking if there are parts of your app which are using audio units, and make sure they're managing the lifecycle properly.

Hi:

I have the same problem 😟. The app crashes when use Speech Recognition through SiriKit after using Audiokit to audio frequency recognition and vice versa. This error happens when running the app on the iphone and not in the xcode simulator.

I hope you can help me


Thanks.


https://github.com/borrecan/sirikit-audiokit.git

I created a minimal example which reproduces the crash: https://github.com/robgaunt/avaudioengine-audiounit-crash/blob/master/AVAudioEngine_AudioUnit_Crash/ViewController.m


The sample application creates an AVAudioEngine and attempts to initialize multiple input audio units. I filed a bug report with Apple about this crash. I recommend you file one too, with your sample application.


As for how to solve the crash... sorry, I don't have any good workarounds. I only skimmed your example app (not familiar with AudioKit) but it sounds like your problem may be similar, depending on what AudioKit is doing under the hood.

Hi Robgaunt,


do you have any tip on how to solve this problem?


I have a similar one:

https://forums.developer.apple.com/thread/73166


What do you mean with audio unit and didn't tear it down properly ? Can you privide some code on how to tear it down correctly?


thank you

Hi guys,


I faced the same exception only in the release build of my app and specific to iPhone7.

The exception seems to occur at a changing point of audio session category.

In my case, changing from

AVAudioSessionCategorySoloAmbient

to

AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker


I read your previous replies, and thanks to you guys, I found a workaround which works at least just for me.

I added the following codes before the change of audio session category

AudioOutputUnitStop((engine.inputNode?.audioUnit)!)

AudioUnitUninitialize((engine.inputNode?.audioUnit)!)


engine is the instance of AVAudioEngine.


It might prevent initialization of multiple input audio unit.


I hope it will help you guys!


Takatomo

Make sure that you have a microphone, I have this issue because i dont have a built in mic.

@HongKongPhooey It is correct. Thank you.

AVAudioEngine crash - required condition is false: hwFormat
 
 
Q