Platform docs
Auth, users, webhooks & more

AudioSession Policies

The StreamVideo SDK provides a flexible mechanism for configuring audio sessions through its AudioSessionPolicy protocol. These policies govern how the app configures the audio session category, mode, and other-audio ducking, taking into account user capabilities or app-specific requirements.

By attaching an AudioSessionPolicy to your call, you can standardize how audio input (microphone) and output (speaker/headphones) are managed in different scenarios.

Built-In Policies

DefaultAudioSessionPolicy

Use this for straightforward call scenarios where the user can send and receive audio without complex restrictions. Typically:

  • Category: .playAndRecord.
  • Mode: .voiceChat.
  • Options: .allowBluetoothHFP and .allowBluetoothA2DP.
  • Output: routed to the speaker when CallSettings.speakerOn is true.

This lets participants communicate freely, using the microphone and speaker/headphones.

Example usage

let call = streamVideo.call(callType: "default", callId: "chat-123")
let policy = DefaultAudioSessionPolicy()

Task {
    await call.updateAudioSessionPolicy(policy)
}

OwnCapabilitiesAudioSessionPolicy

Targets situations where different participants have different permissions. For instance, if a user cannot send audio (no .sendAudio capability), the policy will set .playback category (receiving audio only). Otherwise, it transitions between .playback and .playAndRecord depending on user’s state (e.g., whether they’ve toggled microphone or speaker on).

Example usage

let call = streamVideo.call(callType: "default", callId: "team-meet")
let policy = OwnCapabilitiesAudioSessionPolicy()

Task {
    await call.updateAudioSessionPolicy(policy)
}

This is useful if you need dynamic restrictions based on your business logic (e.g., Livestream viewers, paying customers can speak, free-tier can only listen).

LivestreamAudioSessionPolicy

This policy is intended to be used for livestreams where audio quality matters the most. It supports stereo playout across various audio source outputs including device's speaker and supported external devices (e.g. bluetooth or wired headsets).

The policy uses .playback category (or .playAndRecord when the user has the sendAudio capability), .default mode and .allowBluetoothA2DP category options.

Example usage

let call = streamVideo.call(callType: "default", callId: "team-meet")
let policy = LivestreamAudioSessionPolicy()

Task {
    await call.updateAudioSessionPolicy(policy)
}
Warning:

This policy only supports participants that don't have microphone permissions. That's due to a WebRTC limitation and the underline AVAudioUnit implementation/usage. When the microphone activates, AVAudioUnit gets configured for mono ouput in order to support microphone input. Muting the microphone again won't restore stereo playout. The only way to restore stereo playout is to leave and join the call again.

Other-audio ducking

Voice processing ducks other audio from your app while a call captures the microphone. Short in-app sounds, such as join, leave, and mute effects, can sound too quiet. Set otherAudioDucking on the AudioSessionConfiguration returned by your policy.

The default is OtherAudioDuckingConfiguration.platformDefault: advanced ducking on, level .default. That matches the audio engine before a policy changes it. The built-in policies keep this default.

OtherAudioDuckingConfiguration.Level sets how far voice processing attenuates other audio:

  • .default
  • .min, which leaves other audio loudest
  • .mid
  • .max, which attenuates other audio most

isAdvancedDuckingEnabled ducks other audio only while local or remote voice is active. Set it to false to keep ducking on for the whole capture session.

The engine applies the value while voice processing and capture are on. That requires iOS 17 or macOS 14. Older systems, and tvOS, keep the engine default. Music mode and stereo playout turn voice processing off, so this setting has no effect in those modes.

Example usage

struct InCallSoundPolicy: AudioSessionPolicy {
    func configuration(
        for callSettings: CallSettings,
        ownCapabilities: Set<OwnCapability>
    ) -> AudioSessionConfiguration {
        AudioSessionConfiguration(
            isActive: callSettings.audioOutputOn,
            category: .playAndRecord,
            mode: .voiceChat,
            options: [.allowBluetoothHFP, .allowBluetoothA2DP],
            overrideOutputAudioPort: callSettings.speakerOn
                ? .speaker
                : AVAudioSession.PortOverride.none,
            otherAudioDucking: OtherAudioDuckingConfiguration(
                level: .min,
                isAdvancedDuckingEnabled: true
            )
        )
    }
}

let call = streamVideo.call(callType: "default", callId: "chat-123")

Task {
    await call.updateAudioSessionPolicy(InCallSoundPolicy())
}

When to Use Each Policy

  • DefaultAudioSessionPolicy: Ideal for general-purpose calls where everyone is allowed to speak.
  • OwnCapabilitiesAudioSessionPolicy: Useful for dynamically restricting or enabling send-audio privileges based on user roles or subscription tiers.
  • LivestreamAudioSessionPolicy: Ideal for Livestreams where the participant doesn't have (and won't be granted) the sendAudio capability.

Audio session ownership

During a call, the StreamVideo SDK owns the AVAudioSession. Your app and other SDKs must not call AVAudioSession.setActive(false) while a call is active.

When the session is deactivated during a call, iOS stops the audio engine and the microphone stops sending audio:

  • If the SDK activated the session, the SDK tries to restart the microphone. This recovery is not guaranteed.
  • If CallKit activated the session, the deactivation fails, but iOS still stops the audio engine. The SDK cannot detect this, and the microphone stays silent for the rest of the call. The component that called setActive(false) gets the error Session deactivation failed (NSOSStatusErrorDomain code -12988), and the system log shows iounit stopped unexpectedly > pausing the engine.

Make sure that every component in your app leaves the audio session unchanged during a call. This includes audio players, voice recorders, and media SDKs.

Stream Chat

The audio player of the StreamChat SDK activates and deactivates the audio session when it plays or stops a voice message. When you show chat during a call, give the chat SDK an AudioSessionConfiguring implementation that does not change the audio session:

import StreamChat
import StreamChatSwiftUI

final class InCallAudioSessionConfigurator: AudioSessionConfiguring, Sendable {
    private let permissionProvider = StreamAudioSessionConfigurator()

    func activateRecordingSession() throws {}
    func deactivateRecordingSession() throws {}
    func activatePlaybackSession() throws {}
    func deactivatePlaybackSession() throws {}

    func requestRecordPermission(
        _ completionHandler: @escaping @Sendable (Bool) -> Void
    ) {
        permissionProvider.requestRecordPermission(completionHandler)
    }
}

let utils = Utils()
let audioSessionConfigurator = InCallAudioSessionConfigurator()
utils.audioPlayerBuilder = {
    StreamAudioPlayer(
        assetPropertyLoader: StreamAssetPropertyLoader(),
        audioSessionConfigurator: audioSessionConfigurator
    )
}
utils.audioRecorderBuilder = {
    StreamAudioRecorder(
        configuration: .default,
        audioSessionConfigurator: audioSessionConfigurator
    )
}
Warning:

Conform to AudioSessionConfiguring instead of subclassing StreamAudioSessionConfigurator. The audio player calls the variants with a completion handler. A subclass that overrides only the methods without a completion handler still changes the audio session.

For more information about chat in a call, see Chat Integration.

Conclusion

By selecting an appropriate AudioSessionPolicy, you easily manage how your app handles input, output, and other-audio ducking. This integration reduces boilerplate and keeps audio logic centralized, ensuring consistent experiences across various call types and user roles.