We are seeing quite a few crashes on iOS in Freque...
# sdk-native
b
We are seeing quite a few crashes on iOS in FrequencyTracker, is this a known WebRTC issue on iOS? (We are using Expo)
com.apple.coreaudio.avfaudio: required condition is false: IsFormatSampleRateAndChannelCountValid(format)
Does this happen when mic can't be activated for some reason? Seeing a similar issue in the Swift SDK: https://github.com/livekit/client-sdk-swift/issues/577
šŸ‘€ 1
r
@able-flag-12349 Any ideas on this?
b
more context, let me know what you need to help troubleshoot https://giant-labs-inc.sentry.io/share/issue/b8a1b23414ec43af8776866fd467b98a/
b
Hi, which SDK ver is this ?
b
Copy code
"livekit-client": "2.15.4",
"@livekit/react-native": "^.9.1",
"@livekit/react-native-expo-plugin": "1.0.1",
"@livekit/react-native-krisp-noise-filter": "0.0.2",
"@livekit/react-native-webrtc": "137.0.1"
s
Hi @better-house-11852 et. al, we are also running into this. Any help would be much appreciated!
Copy code
"livekit-client": "^2.15.2",
 "@livekit/react-native": "^2.9.1",
  "@livekit/react-native-expo-plugin": "^1.0.1",
  "@livekit/react-native-krisp-noise-filter": "^0.0.2",
  "@livekit/react-native-webrtc": "^137.0.0",
this thread seems relevant too. are the PR fixes to that thread picked up in our react native versions? https://livekit-users.slack.com/archives/C07FVFGAUKX/p1753439608194909
b
Feels like it's happening when mic isn't available, eg other app taking over audio session. https://stackoverflow.com/questions/60984704/required-condition-is-false-isformatsamplerateandchannelcountvalidformat But this should be handled more gracefully and not crashing the app. We are always setting playAndRecord and activating before starting the livekit session, so this must happen mid call. It is intermittent, so very hard to reproduce. Any help here would be appreciated @better-house-11852 Anywhere we can patch to add logging or catching of this? Is there any sdk call to confirm we have access to the mic etc?
b
I think I know what's happening, we patched this in Swift SDK, but I'll investigate the React-Native SDK. The quick work-around is to ensure AVAudioSession.category is
.playAndRecord
when publishing mic: https://github.com/livekit/client-sdk-swift/blob/main/Sources/LiveKit/Audio/AudioDeviceModuleDelegateAdapter.swift#L43-L71
b
Yes this sounds very likely, can you point me in the direction where we are publishing the mic in RN? Is it in the WebRTCModule?
Here we are making sure category is correct and session is active before we set connect to true. But I guess it's not enough...
b
I'm not fully familiar with the react-native SDK but please give me a few hours to investigate.
šŸ™Œ 2
b
We used Vapi (DailyCo) before, and didn't have same issues with audio session/mic. Might be worth looking at what they do, looks like they are adding more control to devices/session on iOS. https://github.com/daily-co/react-native-webrtc/blob/d35f606d9fc873b5a00465f499ac36ca98aed7a1/ios/RCTWebRTC/WebRTCModule%2BDaily.m Happy to help test and troubleshoot this. I have patched both expo-audio and expo-video to not mess with the AudioSession FYI
s
I’m on my phone but echoing the above that we do something similar to make sure we set the audio mode and category to playAndRecord beforehand. We tried that with both expo-audio setAudioModeAsync and the useIOSAudioManagement hook provided by LiveKit I think the useIOSAudioManagement hook in particular can lead to a race condition or something becuase, while using that hook, I logged the mode and category here https://github.com/livekit/client-sdk-react-native/blob/main/ios/LiveKitReactNativeModule.swift#L163C1-L163C12 in XCode and was seeing the category set to Playback instead of playAndRecord But crash also still happens when simply setting audio mode and category with Expo AudioModule
b
yea we also avoid useIOSAudioManagement as it's setting category to playback, we never want this during a call.
s
cc author of useIOSAudioManagement @eager-raincoat-52616 ^
also it tends to work on the first call but not on subsequent calls similar to what was reported here https://livekit-users.slack.com/archives/C07FVFGAUKX/p1753439608194909
b
I think I will: 1. Start by moving the checks implemented in Swift to WebRTC level to ensure crashes are avoided. 2. Implement at React SDK level to throw/fail when mic publish can't start.
šŸ™ 2
b
Hi @better-house-11852, any updates on the iOS AudioSession issues? Let me know how I can help test or troubleshoot. We're seeing it quite frequently. Thanks!
b
Hello, Step 1 has been completed and I'm making a PR for React now šŸ™
šŸ™Œ 2
b
Hi @better-house-11852 just me checking in again lol, is there any pushed code I could test/patch? We've pushed an update to our RN code, I realized
setAppleAudioConfiguration()
wasn't throwing exceptions, so I rewrote our code to first use expo-audio to try and set playAndRecord, and abort call if it fails: It seems to work, ie the app doesn't crash if we can't activate audio. It fails with OSStatus error 561017449 (Insufficient priority?). So now to the root cause, how do we make sure our app gets higher priority. I guess this error should only happen if a real phone call is in progress. Eg Spotify should be paused IMO. Any insight here would be appreciated! Thanks for all the great work.
šŸ™ 1
b
Hi @big-author-39928 I'm awaiting the patch to WebRTC to merge and will ship this lib. This will ensure it won't crash but will fail gracefully when audio session can't be activated. cc @plain-wall-1315 can you take a look at the PR ?
ā¤ļø 1
I have another PR so
setAppleAudioConfiguration
will throw when it can't activate. I think some cases we simply can't activate due to priority (Apple apps such as Phone, Siri? running). I think the react SDK was not calling
setActive
also, which could trigger other app to pause (Eg Spotify). attempt to get priority/activate.
b
Sounds great @better-house-11852 as you see above I use a combination of expo-audio and livekit to set it up. startAudioSession() does nothing on iOS, but I kept it for now. But I bet most LiveKit users don't use this combo
b
I think these fixes should improve the experience, the other concern is that what defaults we should use
.mixWithOthers
/
.duckOthers
etc.
I don't think you should use another lib to configure AVAudioSession, are you using
expo-audio
only to configure ?
b
We use "doNotMix" which isn't a category, but the lack of category I guess(?). https://docs.expo.dev/versions/latest/sdk/audio/#interruptionmode
I use expo-audio for audio playback and for some audio session management. But with these changes I don't need expo-audio for session management
I'm happy to help write some notes for Expo users, are most will be using expo-audio and expo-video probably
expo-audio added this flag recently that you'll need to not have it kill the session
keepAudioSessionActive
and I had to patch expo-video to be hands off audio session lol
b
I think the important fixes for the SDK were: 1. WebRTC should never crash even configuration isn't correct. 2. setMicrophoneEnabled / getUserMedia(audio) should throw if fail to activate session. 3. setAppleAudioConfiguration should call setActive. The other options are app-specific and I'm not sure something like doNotMix should be the default, for Swift SDK we use
.mixWithOthers
as default. It is impossible to cover all user's app specific use cases so, for advanced users, I think we should add the ability to Not configure the AVAudioSession automatically. And you could use something like
expo-audio
for advance configuration. Currently it appears that always
iosCategoryEnforce()
is being invoked to hook
getUserMedia()
to always configure audio session, this could interfere with
expo-audio
. I think we should add an API/option to disable automatic audio session configuration, but you will need to ensure category is
.playAndRecord
when enabling mic.
b
1. 100% 2. Yes 3. Well you already have startAudioSession() for this, its just a noop right now
s
Thank you @better-house-11852 ! Is there a released package we can test with?
b
I will work with the team to get a release out soon. šŸ™ (I don't usually work on the React SDK, so I'm not aware of the release process)
ā¤ļø 2
s
Tyvm @better-house-11852 !
b
Update: We will probably get a patch release out tomorrow for the React SDK. With this change, in iOS this will throw if can't activate session (to avoid crash in the WebRTC):
Copy code
await AudioSession. setAppleAudioConfiguration({
  audioCategory: 'playAndRecord',
  audioMode: 'videoChat',
  audioCategoryOptions: ['allowAirPlay', "allowBluetooth', "defaultToSpeaker"],
});
await AudioSession.startAudioSession(); // ← This will throw if session can't activate (already in a call etc.)
await room.connect(...)
I had some corrections for my previous comments ā˜ļø • setMicrophoneEnabled / getUserMedia(audio) should throw if fail to activate session → For this update, this will not happen yet. • setAppleAudioConfiguration should call setActive → We'll keep this only for config (Will not call
setActive
for iOS)
ā¤ļø 1
b
Hi @better-house-11852 thanks for pushing this through! I'm testing it now, and the first time I call this code I get this error:
Error setting category: The operation couldn't be completed. (OSStatus error 1836282486.)
Copy code
await AudioSession.setAppleAudioConfiguration({
          audioCategory: 'playAndRecord',
          audioMode: 'voiceChat',
          audioCategoryOptions: ['allowAirPlay', 'defaultToSpeaker'],
        });
I am setting "playback" before, not sure how that relates. It works the 2nd time I call setAppleAudioConfiguration, which feels weird. Any intel on error:
1836282486
?
p
b
yes, but that error messages still doesn't tell me anthing,,,
s
Hi thanks for the continued help with this @better-house-11852 et. al. I upgraded to @swift-school-78240/react-native 2.9.2 @swift-school-78240/react-native-webrtc ^137.0.2 but am unfortunately still getting the exact same intermittent crash 😢 are there any other diagnoses here?
b
@some-pilot-52613 Can I see how you activate the audio session ? It should look something like this:
Copy code
// ↓ Attempt to configure, this could throw if something goes wrong
await AudioSession. setAppleAudioConfiguration({
  audioCategory: 'playAndRecord',
  audioMode: 'videoChat',
  audioCategoryOptions: ['allowAirPlay', "allowBluetooth', "defaultToSpeaker"],
});
await AudioSession.startAudioSession(); // ← This will throw if session can't activate (already in a call etc.)
await room.connect(...) // ← Connect after successful configuration
s
@better-house-11852 thanks I was doing this via
expo-audio
(which I think under the hood does the same?)
Copy code
await AudioModule.setAudioModeAsync({
        playsInSilentMode: true,
        shouldPlayInBackground: false,
        allowsRecording: true,
        shouldRouteThroughEarpiece: false,
        interruptionMode:
          Platform.OS !== "android" ? "mixWithOthers" : undefined,
      });
but I'll try with AudioSession instead
b
you might need to also call
await setIsAudioActiveAsync(true);
if using
expo-audio
. šŸ‘€ https://github.com/expo/expo/blob/9211c56ee86cbb52a6596c57e758acd599362779/packages/expo-audio/ios/AudioModule.swift#L475-L486
ā— 1
b
@some-pilot-52613 are you using expo-video by any chance? (I had to patch it as it is overriding the session category). And if you use expo-audio player, you need to set keepAudioSessionActive https://github.com/expo/expo/pull/39250
ā¤ļø 1