Skip to content

Conversation

@hiroshihorie
Copy link
Member

@hiroshihorie hiroshihorie commented Dec 23, 2025

Calling setActive(false) could disrupt other audio in the app. I believe it's relatively harmless to not call it. But in this PR we keep the default to automatic deactivation and add doc about it.

Summary by CodeRabbit

  • New Features

    • Added an option to disable automatic audio session deactivation so the audio session can remain active after calls end.
  • Documentation

    • Added a new section explaining the automatic audio session deactivation behavior and how to disable it, with usage guidance and notes about post-call behavior.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 19, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Adds a public flag isAutomaticDeactivationEnabled to control whether the AVAudioSession is deactivated when both playout and recording become disabled; implementation switches to AVAudioSession APIs and docs explain toggling the new behavior.

Changes

Cohort / File(s) Summary
Changelog
\.changes/audio-session-deactivation-option
Changelog entry documenting the new isAutomaticDeactivationEnabled option and audio session deactivation behavior.
Documentation
Docs/audio.md
Added "Disabling automatic AVAudioSession deactivation" subsection describing default behavior and usage (AudioManager.shared.audioSession.isAutomaticDeactivationEnabled = false).
Audio Session Implementation
Sources/LiveKit/Audio/AudioSessionEngineObserver.swift
Added public isAutomaticDeactivationEnabled: Bool (default true) and internal state; switched to AVAudioSession APIs (setCategory(..., mode:, options:), setActive(..., options: .notifyOthersOnDeactivation)); deactivation now occurs only when the flag is true (otherwise logged and skipped).

Sequence Diagram(s)

mermaid
sequenceDiagram
participant App as Client App
participant AM as AudioManager / AudioSession API
participant Obs as AudioSessionEngineObserver
participant AV as AVAudioSession

App->>AM: set audioSession.isAutomaticDeactivationEnabled = false
App->>Obs: trigger playout/recording state change (disabled)
Obs->>Obs: evaluate oldState -> newState
alt both playout & recording disabled
    Obs->>Obs: check isAutomaticDeactivationEnabled
    alt enabled == true
        Obs->>AV: setActive(false, options: .notifyOthersOnDeactivation)
        AV-->>Obs: deactivated
    else enabled == false
        Obs-->>App: log "skipping AVAudioSession deactivation"
    end
else playout or recording enabled
    Obs->>AV: setCategory(..., mode:, options:)
    Obs->>AV: setActive(true)
    AV-->>Obs: activated
end

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • pblazej

Poem

🐰 I hop through code and tread the floor,
I guard the session, keep it near or store,
A tiny switch to hold the sound,
Or let it sleep when none is found,
Soft paws, gentle toggle—audio more.

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add option to disable automatic audio session deactivation' directly and clearly matches the main change: introducing a new isAutomaticDeactivationEnabled option to control audio session deactivation behavior.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4799425 and fd184ea.

📒 Files selected for processing (1)
  • .changes/audio-session-deactivation-option

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@pblazej
Copy link
Contributor

pblazej commented Jan 19, 2026

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jan 19, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@Docs/audio.md`:
- Around line 11-23: The phrase "By default, the SDK deactivates the
`AVAudioSession`…" should be changed to reference the deactivation flag instead
of asserting a default; update the text to state that when
`AudioManager.shared.audioSession.isAutomaticDeactivationEnabled` is true (and
`isAutomaticConfigurationEnabled` is not false), the SDK will deactivate the
`AVAudioSession` when playout and recording are disabled, and conversely explain
that setting `isAutomaticDeactivationEnabled = false` keeps the session active
after the call ends; use the symbols `AVAudioSession`,
`isAutomaticConfigurationEnabled`, and
`AudioManager.shared.audioSession.isAutomaticDeactivationEnabled` to locate and
update the wording.

In `@Sources/LiveKit/Audio/AudioSessionEngineObserver.swift`:
- Around line 36-50: The property isAutomaticDeactivationEnabled currently
defaults to true but the PR intends the default to be disabled; change the
initial state value for isAutomaticDeactivationEnabled in the State/initializer
(where _state.isAutomaticDeactivationEnabled is set) to false and update the
property documentation text and the “Default value:” comment to reflect false;
also make the same change for the other occurrence referenced (the duplicate at
lines 68-70) so both the runtime default and docs consistently indicate the
default is false.
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4d6f3aa and dfc9380.

📒 Files selected for processing (3)
  • .changes/audio-session-deactivation-option
  • Docs/audio.md
  • Sources/LiveKit/Audio/AudioSessionEngineObserver.swift
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (21)
  • GitHub Check: Build & Test (macos-26, 26.1, iOS Simulator,name=iPhone 17 Pro,OS=26.1, true)
  • GitHub Check: lint (macos-26, 26.1)
  • GitHub Check: lint (macos-14, 15.4)
  • GitHub Check: lint (macos-15, 16.4)
  • GitHub Check: Build & Test (macos-15, 16.4, macOS,variant=Mac Catalyst)
  • GitHub Check: Build & Test (macos-15, 16.4, iOS Simulator,name=iPhone 16 Pro,OS=18.5)
  • GitHub Check: Build & Test (macos-26, 26.1, tvOS Simulator,name=Apple TV,OS=26.1)
  • GitHub Check: Build & Test (macos-26, 26.1, iOS Simulator,name=iPhone 17 Pro,OS=26.1, true)
  • GitHub Check: Build & Test (macos-15, 16.4, visionOS Simulator,name=Apple Vision Pro,OS=2.5)
  • GitHub Check: Build & Test (macos-14, 15.4, macOS)
  • GitHub Check: Build & Test (macos-26, 26.1, macOS, true)
  • GitHub Check: Build & Test (macos-14, 15.4, tvOS Simulator,name=Apple TV,OS=17.5)
  • GitHub Check: Build & Test (macos-15, 16.4, tvOS Simulator,name=Apple TV,OS=18.5)
  • GitHub Check: Build & Test (macos-26, 26.1, macOS, true)
  • GitHub Check: Build & Test (macos-15, 16.4, macOS)
  • GitHub Check: Build & Test (macos-26, 26.1, macOS, true)
  • GitHub Check: Build & Test (macos-26, 26.1, visionOS Simulator,name=Apple Vision Pro,OS=26.1)
  • GitHub Check: Build & Test (macos-26, 26.1, macOS,variant=Mac Catalyst)
  • GitHub Check: Build & Test (macos-14, 15.4, iOS Simulator,name=iPhone 15 Pro,OS=17.5)
  • GitHub Check: Build & Test (macos-14, 15.4, macOS,variant=Mac Catalyst)
  • GitHub Check: Analyze (swift)
🔇 Additional comments (2)
Sources/LiveKit/Audio/AudioSessionEngineObserver.swift (1)

101-135: LGTM for deactivation gating.

The deactivation behavior is now properly guarded by the new flag and the log path makes the decision visible.

.changes/audio-session-deactivation-option (1)

1-1: Changelog entry looks good.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Comment on lines +11 to +23
## Disabling automatic `AVAudioSession` deactivation

> **Note**: If you have already set `isAutomaticConfigurationEnabled = false`, you don't need to worry about this setting since the SDK won't touch the audio session at all.

By default, the SDK deactivates the `AVAudioSession` when both playout and recording are disabled (e.g., after disconnecting from a room). This allows other apps' audio (like Music) to resume.

However, if your app has its own audio features that could be disrupted by deactivating the audio session, you can disable automatic deactivation:

```swift
AudioManager.shared.audioSession.isAutomaticDeactivationEnabled = false
```

When set to `false`, the audio session remains active after the LiveKit call ends, preserving your app's audio state.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Align “By default” wording with final default.

If the intended default is disabled, the sentence “By default, the SDK deactivates…” becomes incorrect. Consider wording it in terms of the flag instead of a default statement.

📝 Suggested wording
-By default, the SDK deactivates the `AVAudioSession` when both playout and recording are disabled (e.g., after disconnecting from a room). This allows other apps' audio (like Music) to resume.
+When `isAutomaticDeactivationEnabled` is `true`, the SDK deactivates the `AVAudioSession` when both playout and recording are disabled (e.g., after disconnecting from a room). This allows other apps' audio (like Music) to resume.
🤖 Prompt for AI Agents
In `@Docs/audio.md` around lines 11 - 23, The phrase "By default, the SDK
deactivates the `AVAudioSession`…" should be changed to reference the
deactivation flag instead of asserting a default; update the text to state that
when `AudioManager.shared.audioSession.isAutomaticDeactivationEnabled` is true
(and `isAutomaticConfigurationEnabled` is not false), the SDK will deactivate
the `AVAudioSession` when playout and recording are disabled, and conversely
explain that setting `isAutomaticDeactivationEnabled = false` keeps the session
active after the call ends; use the symbols `AVAudioSession`,
`isAutomaticConfigurationEnabled`, and
`AudioManager.shared.audioSession.isAutomaticDeactivationEnabled` to locate and
update the wording.

Comment on lines +36 to +50
/// Controls whether the audio session is deactivated when the audio engine stops.
///
/// - When `true`: The `AVAudioSession` is deactivated when both playout and recording are disabled
/// - When `false`: The `AVAudioSession` remains active when the audio engine stops
///
/// > Note: This value is only used when `isAutomaticConfigurationEnabled` is `true`.
///
/// > Tip: Set to `false` if your app has other audio features that could be disrupted
/// > by deactivating the audio session.
///
/// Default value: `true`
public var isAutomaticDeactivationEnabled: Bool {
get { _state.isAutomaticDeactivationEnabled }
set { _state.mutate { $0.isAutomaticDeactivationEnabled = newValue } }
}
Copy link

@coderabbitai coderabbitai bot Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Default still enables deactivation—verify intent vs PR objective.

PR objective says deactivation should be disabled by default, but the property docs and state default are still true. If the intended default is disabled, flip the default to false and update the inline doc.

✅ Proposed fix
-    /// Default value: `true`
+    /// Default value: `false`
     public var isAutomaticDeactivationEnabled: Bool {

-        var isAutomaticDeactivationEnabled: Bool = true
+        var isAutomaticDeactivationEnabled: Bool = false

Also applies to: 68-70

🤖 Prompt for AI Agents
In `@Sources/LiveKit/Audio/AudioSessionEngineObserver.swift` around lines 36 - 50,
The property isAutomaticDeactivationEnabled currently defaults to true but the
PR intends the default to be disabled; change the initial state value for
isAutomaticDeactivationEnabled in the State/initializer (where
_state.isAutomaticDeactivationEnabled is set) to false and update the property
documentation text and the “Default value:” comment to reflect false; also make
the same change for the other occurrence referenced (the duplicate at lines
68-70) so both the runtime default and docs consistently indicate the default is
false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai why do you think the PR description states the deactivation should be disabled by default? It's only about adding certain possibility/config options.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Fetch the actual PR description from GitHub
gh pr view 881 --repo livekit/client-sdk-swift --json title,body

Length of output: 876


@pblazej You're right, and I apologize for the confusion. I was interpreting the PR title "Disable automatic audio session deactivation by default" literally—the phrase "by default" suggested to me that the default value should be changed to false.

However, if the intent is simply to add the configuration option while keeping the existing behavior (deactivation enabled by default), then the current implementation with isAutomaticDeactivationEnabled = true is correct. The PR title is a bit ambiguous in that case, but I understand now that you're adding the capability to disable automatic deactivation, not changing the default behavior itself.

Thank you for the clarification!

Copy link
Contributor

@pblazej pblazej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put it into the list of possible Audio API improvements

@hiroshihorie hiroshihorie changed the title Disable automatic audio session deactivation by default Add option to disable automatic audio session deactivation Jan 27, 2026
@hiroshihorie hiroshihorie merged commit c71fd6f into main Jan 27, 2026
5 of 7 checks passed
@hiroshihorie hiroshihorie deleted the hiroshi/dont-deactivate-audio-session branch January 27, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants