hex/ios/HEXShared/AudioSessionControlling.swift
Ryan Vogel d23acdf8ca
iOS: dictation picks the car's mic in CarPlay, and a warm session never holds call audio (#2179)
Settings › Dictation › Microphone (Automatic, iPhone, Bluetooth) replaces the
Always use iPhone microphone switch. In a car the old default recorded from
the iPhone, often in a cupholder hearing the speakers and the road, while the
car's microphone sat unused; Hex's own recording mixes, so the car went on
playing over it. Automatic (the default) now records from a wired or USB
headset's microphone, from the car's in CarPlay (the driver's microphone Siri
uses), else from the iPhone's, so AirPods and other Bluetooth headphones keep
playing at full quality and nothing waits for call audio. iPhone is the old
default; Bluetooth also takes Bluetooth hands-free microphones (AirPods,
Bluetooth car kits, which iOS can't tell from headphones). The stored switch
migrates: left on reads as Automatic, turned off as Bluetooth.

Changed behaviour:
- The input is always set explicitly (MicrophoneRoute). Before, Bluetooth
  mode set no preferred input, so an iPhone preference set earlier in the same
  session stuck, and Automatic-style CarPlay/wired choices didn't exist.
- The warm keepalive stays on the iPhone's microphone without HFP whatever the
  choice. Before, with the switch off, it took the headset's or car's
  hands-free microphone for the whole warm session (up to an hour of call
  audio). A dictation moves to its input as it starts; a new route device
  re-pins the keepalive in every mode, not only with Bluetooth allowed.
- In-keyboard dictation asks WebKit for the same input by its label
  (getUserMedia deviceId), read from the keyboard's own session. Before, with
  the switch off it asked for nothing, and WebKit takes the input of the route
  already playing, the iPhone's while a car or headphones play over A2DP, so
  the switch had no effect there. The probe log now lists the inputs offered
  and the one chosen.

Kept: mixWithOthers, no preferred sample rate, RetiredRecorders, keepalive
backoff, and the keyboard's resume request after WebKit's capture.

Verified: xcodebuild test on an iPhone 18 Pro simulator (iOS 27.2) on the Mac
Studio, 519 tests, 0 failures (1 opt-in skip), including new cases for each
choice on each route (CarPlay, AirPods, Bluetooth car kit, wired headset,
nothing), the keepalive's input, HFP only for Bluetooth, the migration and the
controller passing its choice to the capture. Settings row rendered in English
and Simplified Chinese. The simulator has no Bluetooth or CarPlay: the route
behaviour itself is unverified on an iPhone.
2026-11-21 07:21:46 -05:00

51 lines
2.3 KiB
Swift

import AVFAudio
/// The `AVAudioSession` calls Hex makes, so tests can check how it records and lets go.
protocol AudioSessionControlling: AnyObject {
var isOtherAudioPlaying: Bool { get }
/// The inputs the current category offers.
var microphones: [Microphone] { get }
func setCategory(
_ category: AVAudioSession.Category,
mode: AVAudioSession.Mode,
options: AVAudioSession.CategoryOptions
) throws
/// Records from `microphone`, or from iOS's choice for nil.
func prefer(_ microphone: Microphone?) throws
func setActive(_ active: Bool, options: AVAudioSession.SetActiveOptions) throws
}
extension AVAudioSession: AudioSessionControlling {
var microphones: [Microphone] {
(availableInputs ?? []).map(Microphone.init)
}
func prefer(_ microphone: Microphone?) throws {
try setPreferredInput(microphone.flatMap { wanted in
availableInputs?.first { $0.uid == wanted.uid }
})
}
}
/// Lets other apps' audio that in-keyboard dictation interrupted play again.
///
/// The keyboard records through WebKit (`getUserMedia`), whose audio session lives in
/// WebKit's GPU process: WebKit sets `.playAndRecord` in `.videoChat` mode without
/// `.mixWithOthers`, and no page or app setting changes that. Going active, it interrupts
/// another app's video or music; ending, it deactivates without `.notifyOthersOnDeactivation`,
/// so the player is never told it may resume. When other audio played as the capture began
/// and nothing plays once it has ended, the keyboard activates its own session (nothing else
/// plays, so it interrupts nothing) and deactivates it with `.notifyOthersOnDeactivation`,
/// for players that honour `.shouldResume`. Whether iOS passes that on to a player WebKit
/// interrupted can only be seen on an iPhone: the simulator has no interruptions.
nonisolated enum InterruptedAudio {
static func shouldAskToResume(playingAtStart: Bool, playingNow: Bool) -> Bool {
playingAtStart && !playingNow
}
static func askToResume(_ session: AudioSessionControlling) throws {
try session.setCategory(.playback, mode: .default, options: [])
try session.setActive(true, options: [])
try session.setActive(false, options: .notifyOthersOnDeactivation)
}
}