Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

feat(meeting): bidirectional route-following microphone for online calls

Shreeraman A Kde57a198c6d9
diff --git a/Fluid.xcodeproj/project.pbxproj b/Fluid.xcodeproj/project.pbxproj
index f16be580..93357428 100644
--- a/Fluid.xcodeproj/project.pbxproj
+++ b/Fluid.xcodeproj/project.pbxproj
@@ -41,6 +41,7 @@
E5EC00000000000000000082 /* MeetingMicrophoneCaptureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5EC00000000000000000081 /* MeetingMicrophoneCaptureTests.swift */; };
5A3B00000000000000000002 /* MeetingVoiceProcessingCaptureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A3B00000000000000000001 /* MeetingVoiceProcessingCaptureTests.swift */; };
5A4B00000000000000000002 /* MeetingPhase4Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A4B00000000000000000001 /* MeetingPhase4Tests.swift */; };
+ 5C67BA461CBF1149E733A4E9 /* MeetingEraBatchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A50F33BD2347EECE0B3853C /* MeetingEraBatchTests.swift */; };
E5EC000000000000000000A2 /* MeetingTimelineDriftProbeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5EC000000000000000000A1 /* MeetingTimelineDriftProbeTests.swift */; };
E5EC00000000000000000092 /* ClockRegressionHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5EC00000000000000000091 /* ClockRegressionHelper.swift */; };
A6E2246FA3744C7B3E2DB91C /* MeetingLiveTranscriptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 355C1248D5386D1F815EB7D7 /* MeetingLiveTranscriptionTests.swift */; };
@@ -110,6 +111,7 @@
E5EC00000000000000000081 /* MeetingMicrophoneCaptureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingMicrophoneCaptureTests.swift; sourceTree = "<group>"; };
5A3B00000000000000000001 /* MeetingVoiceProcessingCaptureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingVoiceProcessingCaptureTests.swift; sourceTree = "<group>"; };
5A4B00000000000000000001 /* MeetingPhase4Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingPhase4Tests.swift; sourceTree = "<group>"; };
+ 2A50F33BD2347EECE0B3853C /* MeetingEraBatchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingEraBatchTests.swift; sourceTree = "<group>"; };
E5EC000000000000000000A1 /* MeetingTimelineDriftProbeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingTimelineDriftProbeTests.swift; sourceTree = "<group>"; };
E5EC00000000000000000091 /* ClockRegressionHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClockRegressionHelper.swift; sourceTree = "<group>"; };
355C1248D5386D1F815EB7D7 /* MeetingLiveTranscriptionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingLiveTranscriptionTests.swift; sourceTree = "<group>"; };
@@ -227,6 +229,7 @@
E5EC00000000000000000081 /* MeetingMicrophoneCaptureTests.swift */,
5A3B00000000000000000001 /* MeetingVoiceProcessingCaptureTests.swift */,
5A4B00000000000000000001 /* MeetingPhase4Tests.swift */,
+ 2A50F33BD2347EECE0B3853C /* MeetingEraBatchTests.swift */,
E5EC000000000000000000A1 /* MeetingTimelineDriftProbeTests.swift */,
E5EC00000000000000000091 /* ClockRegressionHelper.swift */,
355C1248D5386D1F815EB7D7 /* MeetingLiveTranscriptionTests.swift */,
@@ -429,6 +432,7 @@
E5EC00000000000000000082 /* MeetingMicrophoneCaptureTests.swift in Sources */,
5A3B00000000000000000002 /* MeetingVoiceProcessingCaptureTests.swift in Sources */,
5A4B00000000000000000002 /* MeetingPhase4Tests.swift in Sources */,
+ 5C67BA461CBF1149E733A4E9 /* MeetingEraBatchTests.swift in Sources */,
E5EC000000000000000000A2 /* MeetingTimelineDriftProbeTests.swift in Sources */,
E5EC00000000000000000092 /* ClockRegressionHelper.swift in Sources */,
A6E2246FA3744C7B3E2DB91C /* MeetingLiveTranscriptionTests.swift in Sources */,
diff --git a/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingLiveTranscriptionCoordinator.swift b/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingLiveTranscriptionCoordinator.swift
index 4ceacfe9..c38847d1 100644
--- a/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingLiveTranscriptionCoordinator.swift
+++ b/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingLiveTranscriptionCoordinator.swift
@@ -36,6 +36,11 @@ final nonisolated class MeetingLiveTranscriptionCoordinator: @unchecked Sendable
self.onUpdate = onUpdate
}
+ /// Clears any in-progress "You" partial at a capture-side mic splice, so it can't straddle eras.
+ func resetMicrophoneUtterance() {
+ self.publish { $0.settingPartial(nil, for: .you) }
+ }
+
func setMicrophoneCaptureMethod(_ method: MeetingAudioTrackCaptureMethod?) {
self.stateLock.withLock { self.microphoneCaptureMethod = method }
DebugLogger.shared.info(
diff --git a/Sources/Fluid/Services/Meeting/MeetingAudioChunkWriter.swift b/Sources/Fluid/Services/Meeting/MeetingAudioChunkWriter.swift
index 9af85246..39a71fb7 100644
--- a/Sources/Fluid/Services/Meeting/MeetingAudioChunkWriter.swift
+++ b/Sources/Fluid/Services/Meeting/MeetingAudioChunkWriter.swift
@@ -31,10 +31,13 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
var start: CMTime
var end: CMTime
var discontinuities: [MeetingAudioDiscontinuity]
+ /// Captured at `beginChunk` — `self.track.format` may already be the NEXT chunk's by finalize.
+ var format: MeetingAudioFormat
+ var sourceFormatDescription: CMFormatDescription
}
private enum FinalizationResult: @unchecked Sendable {
- case finalized(MeetingAudioChunk)
+ case finalized(MeetingAudioChunk, MeetingAudioFormat)
case failed(MeetingAudioChunk, String)
}
@@ -51,8 +54,12 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
private let eventHandler: EventHandler
private let encoder: JSONEncoder
+ let trackID: MeetingAudioTrackID
+
private var track: MeetingAudioTrack
private var activeChunk: ActiveChunk?
+ /// Set synchronously when a chunk closes, since `track.chunks.last` can be stale (finalization is async).
+ private var lastFinalizedEnd: CMTime?
private var lifecycle: Lifecycle = .accepting
private var stopWaiters: [CheckedContinuation<MeetingAudioTrack, Never>] = []
private var stoppedTrack: MeetingAudioTrack?
@@ -72,6 +79,7 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
chunkDuration: TimeInterval,
eventHandler: @escaping EventHandler
) throws {
+ self.trackID = track.id
self.track = track
self.sessionDirectory = sessionDirectory
self.trackDirectory = sessionDirectory
@@ -156,12 +164,15 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
}
/// Unlike the `try?` writes elsewhere here, a persist failure propagates to the caller.
+ /// Transactional: mutates a copy, persists it, publishes to `self.track` only on success.
func updateTrackMetadata(_ mutate: @escaping (inout MeetingAudioTrack) -> Void) async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
self.queue.async { [self] in
- mutate(&self.track)
+ var candidate = self.track
+ mutate(&candidate)
do {
- try self.persistTrackManifest()
+ try self.persistTrackManifest(candidate)
+ self.track = candidate
continuation.resume()
} catch {
continuation.resume(throwing: error)
@@ -170,6 +181,30 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
}
}
+ /// Splice point for a capture-side swap: finalizes the active chunk, returns its `presentationEnd`. No-op unless `.accepting`.
+ func beginSplice() async -> MeetingMediaTime? {
+ await withCheckedContinuation { (continuation: CheckedContinuation<MeetingMediaTime?, Never>) in
+ self.queue.async { [self] in
+ self.stateLock.lock()
+ let accepting = self.lifecycle == .accepting
+ self.stateLock.unlock()
+ guard accepting else {
+ continuation.resume(returning: self.track.chunks.last?.presentationEnd)
+ return
+ }
+ guard let activeChunk = self.activeChunk else {
+ continuation.resume(returning: self.track.chunks.last?.presentationEnd)
+ return
+ }
+ let boundary = Self.mediaTime(activeChunk.end)
+ self.scheduleActiveChunkFinalization()
+ self.finalizationGroup.notify(queue: self.queue) {
+ continuation.resume(returning: boundary)
+ }
+ }
+ }
+ }
+
func snapshot() async -> MeetingAudioTrack {
await withCheckedContinuation { continuation in
self.queue.async { [self] in
@@ -211,6 +246,11 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
do {
if self.activeChunk == nil {
try self.beginChunk(with: sampleBuffer, at: presentationTime)
+ } else if let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer),
+ !CMFormatDescriptionEqual(formatDescription, otherFormatDescription: self.activeChunk?.sourceFormatDescription)
+ {
+ self.scheduleActiveChunkFinalization()
+ try self.beginChunk(with: sampleBuffer, at: presentationTime)
}
guard var activeChunk = self.activeChunk else { return }
guard activeChunk.input.isReadyForMoreMediaData else {
@@ -294,14 +334,26 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
writer.error?.localizedDescription ?? "The audio writer could not start."
)
}
- writer.startSession(atSourceTime: start)
+ // A cross-clock-domain producer swap must not overlap the chunk just finalized.
+ let clampedStart: CMTime
+ if let lastFinalizedEnd = self.lastFinalizedEnd, start < lastFinalizedEnd {
+ clampedStart = lastFinalizedEnd
+ DebugLogger.shared.warning(
+ "Meeting audio writer clamped a chunk start that preceded the prior chunk's end.",
+ source: "MeetingAudioChunkWriter"
+ )
+ } else {
+ clampedStart = start
+ }
+ writer.startSession(atSourceTime: clampedStart)
- self.track.format = MeetingAudioFormat(
+ let format = MeetingAudioFormat(
codec: "aac-lc",
sampleRate: asbd.mSampleRate,
channelCount: channelCount,
bitRate: bitRate
)
+ self.track.format = format
let relativePath = finalURL.path.replacingOccurrences(
of: self.sessionDirectory.path + "/",
with: ""
@@ -313,15 +365,18 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
relativeFinalPath: relativePath,
writer: writer,
input: input,
- start: start,
- end: start,
- discontinuities: []
+ start: clampedStart,
+ end: clampedStart,
+ discontinuities: [],
+ format: format,
+ sourceFormatDescription: formatDescription
)
}
private func scheduleActiveChunkFinalization() {
guard let activeChunk = self.activeChunk else { return }
self.activeChunk = nil
+ self.lastFinalizedEnd = activeChunk.end
activeChunk.input.markAsFinished()
guard self.finalizationSlots.wait(timeout: .now()) == .success else {
@@ -346,12 +401,10 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
private func applyFinalization(_ result: FinalizationResult) {
switch result {
- case let .finalized(chunk):
+ case let .finalized(chunk, format):
self.track.chunks.append(chunk)
try? self.persistTrackManifest()
- if let format = self.track.format {
- self.eventHandler(.chunkFinalized(trackID: self.track.id, chunk: chunk, format: format))
- }
+ self.eventHandler(.chunkFinalized(trackID: self.track.id, chunk: chunk, format: format))
case let .failed(chunk, detail):
self.track.chunks.append(chunk)
self.track.health.status = .degraded
@@ -405,17 +458,20 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
ofItemAtPath: activeChunk.finalURL.path
)
let values = try activeChunk.finalURL.resourceValues(forKeys: [.fileSizeKey])
- return try .finalized(MeetingAudioChunk(
- id: UUID(),
- sequence: activeChunk.sequence,
- relativeFilePath: activeChunk.relativeFinalPath,
- presentationStart: self.mediaTime(activeChunk.start),
- presentationEnd: self.mediaTime(activeChunk.end),
- discontinuities: activeChunk.discontinuities,
- sha256: self.sha256(of: activeChunk.finalURL),
- byteCount: Int64(values.fileSize ?? 0),
- finalizationState: .finalized
- ))
+ return try .finalized(
+ MeetingAudioChunk(
+ id: UUID(),
+ sequence: activeChunk.sequence,
+ relativeFilePath: activeChunk.relativeFinalPath,
+ presentationStart: self.mediaTime(activeChunk.start),
+ presentationEnd: self.mediaTime(activeChunk.end),
+ discontinuities: activeChunk.discontinuities,
+ sha256: self.sha256(of: activeChunk.finalURL),
+ byteCount: Int64(values.fileSize ?? 0),
+ finalizationState: .finalized
+ ),
+ activeChunk.format
+ )
} catch {
return .failed(self.failedChunk(from: activeChunk), error.localizedDescription)
}
@@ -465,8 +521,8 @@ final nonisolated class MeetingAudioChunkWriter: @unchecked Sendable {
self.eventHandler(.trackHealth(trackID: self.track.id, health: self.track.health))
}
- private func persistTrackManifest() throws {
- let data = try self.encoder.encode(self.track)
+ private func persistTrackManifest(_ track: MeetingAudioTrack? = nil) throws {
+ let data = try self.encoder.encode(track ?? self.track)
try data.write(to: self.manifestURL, options: .atomic)
try FileManager.default.setAttributes(
[.posixPermissions: NSNumber(value: Int16(0o600))],
diff --git a/Sources/Fluid/Services/Meeting/MeetingCaptureEngine.swift b/Sources/Fluid/Services/Meeting/MeetingCaptureEngine.swift
index 822458d8..af6faa7c 100644
--- a/Sources/Fluid/Services/Meeting/MeetingCaptureEngine.swift
+++ b/Sources/Fluid/Services/Meeting/MeetingCaptureEngine.swift
@@ -795,6 +795,317 @@ private final nonisolated class InRoomMicrophoneCaptureRuntime: NSObject, Meetin
}
}
+/// Single-resume continuation box; resuming twice is a safe no-op.
+private final nonisolated class MeetingOneShotValue<T: Sendable>: @unchecked Sendable {
+ private let lock = NSLock()
+ private var continuation: CheckedContinuation<T, Never>?
+
+ init(_ continuation: CheckedContinuation<T, Never>) {
+ self.continuation = continuation
+ }
+
+ func resume(_ value: T) {
+ let continuation = self.lock.withLock { () -> CheckedContinuation<T, Never>? in
+ defer { self.continuation = nil }
+ return self.continuation
+ }
+ continuation?.resume(returning: value)
+ }
+}
+
+/// Reusable AVCaptureSession mic component: `wasInterrupted` restarts only if still not running; runtime errors get ONE bounded restart.
+final nonisolated class MeetingAVCaptureMicrophoneComponent: NSObject,
+ AVCaptureAudioDataOutputSampleBufferDelegate, @unchecked Sendable
+{
+ private static let interruptionEndedWaitSeconds: Double = 3
+ private static let restartSupervisionSeconds: Double = 3
+ private static let stopTimeoutSeconds: Double = 3
+
+ let deviceUID: String?
+ let deviceName: String
+
+ private let session = AVCaptureSession()
+ private let output = AVCaptureAudioDataOutput()
+ private let controlQueue = DispatchQueue(label: "com.fluidvoice.meeting.avcapture.control")
+ private let stateLock = NSLock()
+ private let onSample: @Sendable (CMSampleBuffer) -> Void
+ private let onTerminalStop: @Sendable (String) -> Void
+ private var stopping = false
+ private var emittedTerminalStop = false
+ private var runtimeErrorRestarted = false
+ private var didStopRunningRestarted = false
+ private var interruptionEndWaiters: [MeetingOneShotValue<Void>] = []
+
+ init(
+ microphone: MeetingMicrophoneIdentity,
+ onSample: @escaping @Sendable (CMSampleBuffer) -> Void,
+ onTerminalStop: @escaping @Sendable (String) -> Void
+ ) throws {
+ self.deviceUID = microphone.coreAudioUID
+ self.deviceName = microphone.displayName
+ self.onSample = onSample
+ self.onTerminalStop = onTerminalStop
+ super.init()
+ guard let device = AVCaptureDevice(uniqueID: microphone.captureDeviceID) else {
+ throw MeetingCaptureError.microphoneUnavailable
+ }
+ let input = try AVCaptureDeviceInput(device: device)
+ self.session.beginConfiguration()
+ defer { self.session.commitConfiguration() }
+ guard self.session.canAddInput(input), self.session.canAddOutput(self.output) else {
+ throw MeetingCaptureError.microphoneUnavailable
+ }
+ self.session.addInput(input)
+ self.session.addOutput(self.output)
+ self.output.setSampleBufferDelegate(
+ self,
+ queue: DispatchQueue(label: "com.fluidvoice.meeting.avcapture.samples", qos: .userInteractive)
+ )
+ self.observeSessionLifecycle()
+ }
+
+ deinit {
+ for observer in self.observers { NotificationCenter.default.removeObserver(observer) }
+ }
+
+ private var observers: [NSObjectProtocol] = []
+
+ /// Bounded retry at 0.5/1/2s on `startRunning` failure plus a supervised overall timeout.
+ func start() async throws {
+ for (attempt, delay) in ([0] + Self.startRetryDelays).enumerated() {
+ if attempt > 0 { try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000)) }
+ if self.stateLock.withLock({ self.stopping }) { throw CancellationError() }
+ let started = await MeetingSupervisedTimeout.run(seconds: Self.startSupervisionSeconds) { [session] in
+ await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
+ let box = MeetingOneShotValue<Bool>(continuation)
+ self.controlQueue.async {
+ session.startRunning()
+ box.resume(session.isRunning)
+ }
+ }
+ }
+ if started == true { return }
+ }
+ throw MeetingCaptureError.captureStartFailed("The microphone did not start.")
+ }
+
+ private static let startRetryDelays: [Double] = [0.5, 1.0, 2.0]
+ private static let startSupervisionSeconds: Double = 4
+
+ func stop() async throws {
+ self.stateLock.lock()
+ self.stopping = true
+ let waiters = self.interruptionEndWaiters
+ self.interruptionEndWaiters = []
+ self.stateLock.unlock()
+ waiters.forEach { $0.resume(()) }
+
+ let result = await withCheckedContinuation { continuation in
+ let completion = MeetingOneShotCompletion(continuation)
+ self.controlQueue.async { [session] in
+ if session.isRunning { session.stopRunning() }
+ completion.resume(.success)
+ }
+ DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + Self.stopTimeoutSeconds) {
+ completion.resume(.failure("Timed out while stopping microphone capture."))
+ }
+ }
+ if case let .failure(detail) = result {
+ throw MeetingCaptureRuntimeError.stopFailed(detail)
+ }
+ }
+
+ /// `onComplete(false)` means the supervision window elapsed before `stop()` finished.
+ func stopDetached(supervisedSeconds: Double, onComplete: @escaping @Sendable (Bool) -> Void) {
+ Task { [weak self] in
+ guard let self else { onComplete(false); return }
+ let completed = await MeetingSupervisedTimeout.run(seconds: supervisedSeconds) {
+ try? await self.stop()
+ return true
+ }
+ onComplete(completed == true)
+ }
+ }
+
+ func captureOutput(
+ _ output: AVCaptureOutput,
+ didOutput sampleBuffer: CMSampleBuffer,
+ from connection: AVCaptureConnection
+ ) {
+ self.stateLock.lock()
+ let shouldAccept = !self.stopping
+ self.stateLock.unlock()
+ guard shouldAccept else { return }
+ self.onSample(sampleBuffer)
+ }
+
+ private func observeSessionLifecycle() {
+ let center = NotificationCenter.default
+ self.observers = [
+ center.addObserver(
+ forName: AVCaptureSession.wasInterruptedNotification, object: self.session, queue: nil
+ ) { [weak self] _ in self?.handleInterruptionBegan() },
+ center.addObserver(
+ forName: AVCaptureSession.interruptionEndedNotification, object: self.session, queue: nil
+ ) { [weak self] _ in self?.handleInterruptionEnded() },
+ center.addObserver(
+ forName: AVCaptureSession.runtimeErrorNotification, object: self.session, queue: nil
+ ) { [weak self] note in self?.handleRuntimeError(note) },
+ center.addObserver(
+ forName: AVCaptureSession.didStopRunningNotification, object: self.session, queue: nil
+ ) { [weak self] _ in self?.handleDidStopRunning() },
+ ]
+ }
+
+ private func handleInterruptionBegan() {
+ Task { [weak self] in
+ guard let self else { return }
+ await self.waitForInterruptionEnd(timeoutSeconds: Self.interruptionEndedWaitSeconds)
+ guard !(self.stateLock.withLock({ self.stopping })) else { return }
+ if !self.session.isRunning {
+ await self.attemptSupervisedRestart(
+ reason: "Route settled after an interruption but the microphone did not resume."
+ )
+ }
+ }
+ }
+
+ private func waitForInterruptionEnd(timeoutSeconds: Double) async {
+ await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
+ let box = MeetingOneShotValue<Void>(continuation)
+ self.stateLock.lock()
+ self.interruptionEndWaiters.append(box)
+ self.stateLock.unlock()
+ DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + timeoutSeconds) {
+ box.resume(())
+ }
+ }
+ }
+
+ private func handleInterruptionEnded() {
+ self.stateLock.lock()
+ let waiters = self.interruptionEndWaiters
+ self.interruptionEndWaiters = []
+ self.stateLock.unlock()
+ waiters.forEach { $0.resume(()) }
+ }
+
+ private func handleRuntimeError(_ notification: Notification) {
+ let shouldRestart = self.stateLock.withLock { () -> Bool in
+ guard !self.stopping, !self.runtimeErrorRestarted else { return false }
+ self.runtimeErrorRestarted = true
+ return true
+ }
+ let detail = (notification.userInfo?[AVCaptureSessionErrorKey] as? Error)?.localizedDescription
+ ?? "Microphone capture reported a runtime error."
+ guard shouldRestart else {
+ self.emitTerminalStopIfNeeded(detail: detail)
+ return
+ }
+ Task { [weak self] in await self?.attemptSupervisedRestart(reason: detail) }
+ }
+
+ private func handleDidStopRunning() {
+ let shouldRestart = self.stateLock.withLock { () -> Bool in
+ guard !self.stopping, !self.didStopRunningRestarted else { return false }
+ self.didStopRunningRestarted = true
+ return true
+ }
+ guard shouldRestart else {
+ self.emitTerminalStopIfNeeded(detail: "Microphone capture stopped unexpectedly.")
+ return
+ }
+ Task { [weak self] in
+ await self?.attemptSupervisedRestart(reason: "Microphone capture stopped unexpectedly.")
+ }
+ }
+
+ private func attemptSupervisedRestart(reason: String) async {
+ guard !(self.stateLock.withLock({ self.stopping })) else { return }
+ let restarted = await MeetingSupervisedTimeout.run(seconds: Self.restartSupervisionSeconds) { [session] in
+ await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
+ let box = MeetingOneShotValue<Bool>(continuation)
+ self.controlQueue.async {
+ guard !session.isRunning else { box.resume(true); return }
+ session.startRunning()
+ box.resume(session.isRunning)
+ }
+ }
+ }
+ guard restarted != true, !(self.stateLock.withLock({ self.stopping })) else { return }
+ self.emitTerminalStopIfNeeded(detail: reason)
+ }
+
+ private func emitTerminalStopIfNeeded(detail: String) {
+ let shouldEmit = self.stateLock.withLock { () -> Bool in
+ guard !self.stopping, !self.emittedTerminalStop else { return false }
+ self.emittedTerminalStop = true
+ return true
+ }
+ guard shouldEmit else { return }
+ self.onTerminalStop(detail)
+ }
+}
+
+/// Bounds wall-clock time even when `operation` ignores cancellation; a late operation finishes detached, result discarded.
+nonisolated enum MeetingSupervisedTimeout {
+ static func run<T: Sendable>(seconds: Double, operation: @escaping @Sendable () async -> T) async -> T? {
+ await withCheckedContinuation { (continuation: CheckedContinuation<T?, Never>) in
+ let completion = MeetingOneShotValue<T?>(continuation)
+ Task {
+ let result = await operation()
+ completion.resume(result)
+ }
+ DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + max(0, seconds)) {
+ completion.resume(nil)
+ }
+ }
+ }
+}
+
+/// Per-era acceptance fence, wrapping each sample callback in `beginSample()`/`defer { endSample() }`.
+final nonisolated class MeetingEraAcceptanceFence: @unchecked Sendable {
+ private let lock = NSLock()
+ private var accepting = true
+ private var inFlight = 0
+ private var drainWaiters: [MeetingOneShotValue<Void>] = []
+
+ func beginSample() -> Bool {
+ self.lock.lock()
+ defer { self.lock.unlock() }
+ guard self.accepting else { return false }
+ self.inFlight += 1
+ return true
+ }
+
+ func endSample() {
+ self.lock.lock()
+ self.inFlight -= 1
+ let shouldNotify = !self.accepting && self.inFlight == 0
+ let waiters = shouldNotify ? self.drainWaiters : []
+ if shouldNotify { self.drainWaiters = [] }
+ self.lock.unlock()
+ waiters.forEach { $0.resume(()) }
+ }
+
+ func quiesce() async {
+ let alreadyDrained: Bool = self.lock.withLock {
+ self.accepting = false
+ return self.inFlight == 0
+ }
+ guard !alreadyDrained else { return }
+ await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
+ let box = MeetingOneShotValue<Void>(continuation)
+ let pending: Bool = self.lock.withLock {
+ guard self.inFlight > 0 else { return false }
+ self.drainWaiters.append(box)
+ return true
+ }
+ if !pending { box.resume(()) }
+ }
+ }
+}
+
private nonisolated enum MeetingOneShotResult: Sendable {
case success
case failure(String)
@@ -1113,15 +1424,15 @@ nonisolated enum MeetingVoiceProcessingWatchdog {
/// Registration failure at init means VPIO is not viable — fail closed. Re-registers everything on
/// `kAudioHardwarePropertyServiceRestarted`, since CoreAudio discards all prior listeners on reset.
private final nonisolated class MeetingOutputRouteListener: @unchecked Sendable {
- private let onViolation: @Sendable () -> Void
+ private let onChange: @Sendable () -> Void
private let lock = NSLock()
private var defaultOutputToken: AudioObjectPropertyListenerBlock?
private var serviceRestartedToken: AudioObjectPropertyListenerBlock?
private var dataSourceToken: AudioObjectPropertyListenerBlock?
private var dataSourceDeviceID: AudioObjectID?
- init?(onViolation: @escaping @Sendable () -> Void) {
- self.onViolation = onViolation
+ init?(onChange: @escaping @Sendable () -> Void) {
+ self.onChange = onChange
guard self.register() else { return nil }
}
@@ -1129,12 +1440,8 @@ private final nonisolated class MeetingOutputRouteListener: @unchecked Sendable
self.unregister()
}
- private func predicateFails() -> Bool {
- MeetingCapturePathDecider.outputRouteDeclineReason(MeetingCaptureEngine.currentOutputRouteSnapshot()) != nil
- }
-
private func reevaluate() {
- if self.predicateFails() { self.onViolation() }
+ self.onChange()
}
@discardableResult
@@ -1207,7 +1514,7 @@ private final nonisolated class MeetingOutputRouteListener: @unchecked Sendable
private func reregisterAfterServiceRestart() {
self.unregister()
guard self.register() else {
- self.onViolation()
+ self.onChange()
return
}
self.reevaluate()
@@ -1253,30 +1560,102 @@ nonisolated enum MeetingVoiceProcessingDriftSnapshot {
}
}
-/// Owns a VPIO `MeetingMicrophoneCapture` plus an app-audio-only `ScreenCaptureMeetingRuntime`.
-/// On any failure before commit, aborts cleanly and falls back to today's app+mic SCStream path
-/// using the SAME writers — the fallback never sees a byte or event from the aborted attempt.
+
+/// Device election for a capture-side swap: prefers the system default input; role transfers only when it equals the saved/original device.
+nonisolated struct MeetingCaptureDeviceElection: Sendable, Equatable {
+ var identity: MeetingMicrophoneIdentity
+ var role: MeetingMicrophoneRole
+
+ static func elect(original: MeetingMicrophoneIdentity) -> MeetingCaptureDeviceElection {
+ Self.decide(
+ original: original,
+ catalog: MeetingCaptureSourceCatalog.availableMicrophones(),
+ defaultInputUID: AudioDevice.getDefaultInputDevice()?.uid
+ )
+ }
+
+ /// Pure decision extracted from `elect(original:)` for hardware-free testing.
+ static func decide(
+ original: MeetingMicrophoneIdentity,
+ catalog: [MeetingMicrophoneIdentity],
+ defaultInputUID: String?
+ ) -> MeetingCaptureDeviceElection {
+ guard let defaultInputUID else {
+ return MeetingCaptureDeviceElection(identity: original, role: original.role)
+ }
+ guard let matched = catalog.first(where: { $0.coreAudioUID == defaultInputUID }) else {
+ return MeetingCaptureDeviceElection(identity: original, role: original.role)
+ }
+ if matched.captureDeviceID == original.captureDeviceID {
+ return MeetingCaptureDeviceElection(identity: matched, role: original.role)
+ }
+ // A built-in mic is inherently the owner's; .unknown would block VPIO on upgrades.
+ let role: MeetingMicrophoneRole = matched.coreAudioUID == "BuiltInMicrophoneDevice" ? .personal : .unknown
+ return MeetingCaptureDeviceElection(
+ identity: MeetingMicrophoneIdentity(
+ captureDeviceID: matched.captureDeviceID,
+ coreAudioUID: matched.coreAudioUID,
+ displayName: matched.displayName,
+ role: role
+ ),
+ role: role
+ )
+ }
+}
+
+/// Pure discard rule extracted from the upgrade's ring-flush for hardware-free testing.
+nonisolated enum MeetingUpgradeRingDiscard {
+ static func discardingSamples(atOrBehind boundary: CMTime?, from samples: [CMSampleBuffer]) -> [CMSampleBuffer] {
+ guard let boundary else { return samples }
+ return samples.filter { CMSampleBufferGetPresentationTimeStamp($0) > boundary }
+ }
+}
+
+/// Owns a swappable mic component (VPIO or AVCaptureSession) plus an app-audio-only SCStream that never stops across a swap.
private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRuntime, @unchecked Sendable {
private static let totalStartDeadlineSeconds: Double = 10
private static let firstBufferDeadlineSeconds: Double = 2
+ private static let downgradeSettleSeconds: Double = 0.75
+ private static let upgradeSettleSeconds: Double = 0.75
+ private static let upgradeDwellSeconds: Double = 10
+ private static let maxUpgradeAttemptsPerSession = 3
+ private static let upgradeBackoffBaseSeconds: Double = 5
+ private static let oldProducerStopSupervisionSeconds: Double = 0.3
+
+ private enum Phase: Equatable {
+ case starting
+ case vpio
+ case transitioning
+ case avCapture
+ case failed
+ }
private let application: MeetingApplicationIdentity
- private let microphone: MeetingMicrophoneIdentity
+ private let originalMicrophone: MeetingMicrophoneIdentity
private let applicationWriter: MeetingAudioChunkWriter
private let microphoneWriter: MeetingAudioChunkWriter
private let eventHandler: @Sendable (MeetingCaptureEvent) -> Void
private let liveAudioHandler: (@Sendable (MeetingAudioTrackKind, CMSampleBuffer) -> Void)?
- private let micCapture = MeetingMicrophoneCapture()
- private let gate = MeetingVoiceProcessingCommitGate()
private let stateLock = NSLock()
+ private var micCapture = MeetingMicrophoneCapture()
+ private var gate = MeetingVoiceProcessingCommitGate()
+ private var avCaptureComponent: MeetingAVCaptureMicrophoneComponent?
+ private var currentFence = MeetingEraAcceptanceFence()
+ private var interruptedThisEra = false
+
private var appOnlyRuntime: ScreenCaptureMeetingRuntime?
private var fallbackRuntime: ScreenCaptureMeetingRuntime?
- private var promoted = false
- private var committed = false
- private var interrupted = false
+ private var phase: Phase = .starting
private var watchdogTask: Task<Void, Never>?
private var outputRouteListener: MeetingOutputRouteListener?
+ private var transitionTask: Task<Void, Never>?
+ private var dwellTask: Task<Void, Never>?
+ private var dwellArming = false
+ private var stopRequested = false
+ private var quarantined = false
+ private var upgradeAttempts = 0
+ private let abortedCaptureDrain = DispatchGroup()
var captureScope: MeetingCaptureScope? {
if let fallback = self.stateLock.withLock({ self.fallbackRuntime }) { return fallback.captureScope }
@@ -1292,7 +1671,7 @@ private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRun
liveAudioHandler: (@Sendable (MeetingAudioTrackKind, CMSampleBuffer) -> Void)?
) {
self.application = application
- self.microphone = microphone
+ self.originalMicrophone = microphone
self.applicationWriter = applicationWriter
self.microphoneWriter = microphoneWriter
self.eventHandler = eventHandler
@@ -1334,14 +1713,19 @@ private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRun
try await self.attemptVoiceProcessingStart()
} catch {
if error is CancellationError { throw error }
- if self.stateLock.withLock({ self.promoted }) { throw error }
- self.gate.abort()
- self.outputRouteListener = nil
- await self.micCapture.stop()
+ if self.stateLock.withLock({ self.phase != .starting }) { throw error }
+ let (gateToAbort, captureToStop, staleListener) = self.stateLock.withLock {
+ () -> (MeetingVoiceProcessingCommitGate, MeetingMicrophoneCapture, MeetingOutputRouteListener?) in
+ defer { self.outputRouteListener = nil }
+ return (self.gate, self.micCapture, self.outputRouteListener)
+ }
+ _ = staleListener // released here, outside the lock
+ gateToAbort.abort()
+ await captureToStop.stop()
await self.stopAppOnlyIfStarted()
let fallback = try await ScreenCaptureMeetingRuntime.make(
application: self.application,
- microphone: self.microphone,
+ microphone: self.originalMicrophone,
includeMicrophone: true,
applicationWriter: self.applicationWriter,
microphoneWriter: self.microphoneWriter,
@@ -1358,9 +1742,13 @@ private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRun
let microphoneWriter = self.microphoneWriter
let liveAudioHandler = self.liveAudioHandler
let gate = self.gate
- let outcome = try await self.micCapture.start(
- microphone: self.microphone,
+ let fence = self.currentFence
+ let capture = self.stateLock.withLock { self.micCapture }
+ let outcome = try await capture.start(
+ microphone: self.originalMicrophone,
onSample: { sample in
+ guard fence.beginSample() else { return }
+ defer { fence.endSample() }
switch gate.offer(sample) {
case .passthrough:
microphoneWriter.enqueue(sample)
@@ -1370,7 +1758,7 @@ private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRun
}
},
onEvent: { [weak self] event in
- self?.handleMicEvent(event)
+ self?.handleMicEvent(event, owner: capture)
}
)
if case .unavailable = outcome {
@@ -1383,7 +1771,7 @@ private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRun
let appOnly = try await ScreenCaptureMeetingRuntime.make(
application: self.application,
- microphone: self.microphone,
+ microphone: self.originalMicrophone,
includeMicrophone: false,
applicationWriter: self.applicationWriter,
microphoneWriter: self.microphoneWriter,
@@ -1393,30 +1781,31 @@ private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRun
try await appOnly.start()
self.stateLock.withLock { self.appOnlyRuntime = appOnly }
- self.outputRouteListener = MeetingOutputRouteListener { [weak self] in
- self?.interrupt(detail: "Output switched to Bluetooth/headphones — voice-processing capture cannot continue.")
- }
- guard self.outputRouteListener != nil else {
- throw MeetingCaptureError.captureStartFailed("Could not register the output-route listener.")
- }
-
guard var batch = self.gate.beginCommitFlush() else {
throw MeetingCaptureError.captureStartFailed("Voice-processing commit gate aborted before commit.")
}
// Promote provenance BEFORE the gate flushes a single VPIO byte; after this point a failure
// must fail the start outright — falling back would mix provenance on promoted writers.
- self.stateLock.withLock { self.promoted = true }
+ self.stateLock.withLock { self.phase = .vpio }
let settled = await self.micCapture.settledConfiguration()
try await self.microphoneWriter.updateTrackMetadata { track in
track.captureMethod = .voiceProcessing
track.voiceProcessingConfig = settled
+ track.captureEras = [MeetingCaptureEra(
+ method: .voiceProcessing,
+ deviceUID: self.originalMicrophone.coreAudioUID,
+ deviceName: self.originalMicrophone.displayName,
+ roleAtElection: self.originalMicrophone.role,
+ startSeconds: 0,
+ settledConfig: settled
+ )]
}
while true {
await self.flushBatch(batch)
guard let next = self.gate.continueCommitFlush() else { break }
batch = next
}
- self.stateLock.withLock { self.committed = true }
+ self.startOutputRouteWatch()
self.startWatchdog()
}
@@ -1446,46 +1835,74 @@ private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRun
}
}
- private func waitForBufferProgress(timeoutSeconds: Double) async -> Bool {
- let baseline = await self.micCapture.statistics().buffersEmitted
+ private func waitForBufferProgress(timeoutSeconds: Double, using capture: MeetingMicrophoneCapture? = nil) async -> Bool {
+ let capture = capture ?? self.stateLock.withLock { self.micCapture }
+ let baseline = await capture.statistics().buffersEmitted
let deadline = Date().addingTimeInterval(timeoutSeconds)
while Date() < deadline {
- if await self.micCapture.statistics().buffersEmitted > baseline { return true }
+ if await capture.statistics().buffersEmitted > baseline { return true }
try? await Task.sleep(nanoseconds: 50_000_000)
}
return false
}
private func startWatchdog() {
- self.watchdogTask = Task { [weak self] in
+ let task = Task { [weak self] in
guard let self else { return }
- var lastCount = await self.micCapture.statistics().buffersEmitted
+ let capture = self.stateLock.withLock { self.micCapture }
+ var lastCount = await capture.statistics().buffersEmitted
var lastProgress = Date()
while !Task.isCancelled {
try? await Task.sleep(nanoseconds: 1_000_000_000)
if Task.isCancelled { return }
- let current = await self.micCapture.statistics()
+ guard self.stateLock.withLock({ self.phase == .vpio && self.micCapture === capture }) else { return }
+ let current = await capture.statistics()
if current.buffersEmitted != lastCount {
lastCount = current.buffersEmitted
lastProgress = Date()
continue
}
if MeetingVoiceProcessingWatchdog.isStalled(secondsSinceLastEmittedBuffer: Date().timeIntervalSince(lastProgress)) {
- self.interrupt(detail: "No microphone audio emitted for 10s (drops=\(current.droppedPostCapCount), conversionFailures=\(current.conversionFailures)).")
+ self.beginDowngradeTransition(reason: "No microphone audio emitted for 10s (drops=\(current.droppedPostCapCount), conversionFailures=\(current.conversionFailures)).")
return
}
}
}
+ self.stateLock.withLock { self.watchdogTask = task }
+ }
+
+ /// Registered once: `handleRouteChange()` re-reads `phase` on every fire, so it never needs re-arming.
+ private func startOutputRouteWatch() {
+ let listener = MeetingOutputRouteListener { [weak self] in
+ self?.handleRouteChange()
+ }
+ self.stateLock.withLock { self.outputRouteListener = listener }
}
- /// Terminal pre-commit events abort the attempt; non-terminal ones are swallowed by the gate.
- private func handleMicEvent(_ event: MeetingMicrophoneCaptureEvent) {
- guard self.gate.isCommitted else {
+ private func handleRouteChange() {
+ let currentPhase = self.stateLock.withLock { self.phase }
+ switch currentPhase {
+ case .vpio:
+ let reason = MeetingCapturePathDecider.outputRouteDeclineReason(MeetingCaptureEngine.currentOutputRouteSnapshot())
+ if let reason {
+ self.beginDowngradeTransition(reason: "Output route changed: \(reason)")
+ }
+ case .avCapture:
+ self.armUpgradeDwellIfViable()
+ default:
+ break
+ }
+ }
+
+ private func handleMicEvent(_ event: MeetingMicrophoneCaptureEvent, owner: MeetingMicrophoneCapture) {
+ let (phase, isOwner) = self.stateLock.withLock { (self.phase, self.micCapture === owner) }
+ guard isOwner else { return }
+ guard phase != .starting else {
switch event {
case .engineStopped:
self.gate.offerTerminalEventPreCommit()
case .defaultInputChanged:
- if let requestedUID = self.microphone.coreAudioUID,
+ if let requestedUID = self.originalMicrophone.coreAudioUID,
AudioDevice.getDefaultInputDevice()?.uid != requestedUID
{
self.gate.offerTerminalEventPreCommit()
@@ -1495,6 +1912,7 @@ private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRun
}
return
}
+ guard phase == .vpio else { return }
switch event {
case .configurationChanged:
self.eventHandler(.interrupted(
@@ -1504,57 +1922,462 @@ private final nonisolated class VoiceProcessingMeetingRuntime: MeetingCaptureRun
Task { [weak self] in
guard let self else { return }
if await !self.waitForBufferProgress(timeoutSeconds: Self.firstBufferDeadlineSeconds) {
- self.interrupt(detail: "No microphone audio resumed after a configuration change.")
+ self.beginDowngradeTransition(detail: "No microphone audio resumed after a configuration change.")
}
}
case .defaultInputChanged:
- guard let requestedUID = self.microphone.coreAudioUID else { return }
+ guard let requestedUID = self.originalMicrophone.coreAudioUID else { return }
if AudioDevice.getDefaultInputDevice()?.uid != requestedUID {
- self.interrupt(detail: "The default input device changed away from the requested microphone.")
+ self.beginDowngradeTransition(reason: "The default input device changed away from the requested microphone.")
}
case .overload:
self.eventHandler(.interrupted(kind: .voiceProcessingOverload, trackID: nil, detail: "Voice-processing microphone reported an overload."))
case .engineStopped:
- self.interrupt(detail: "The voice-processing microphone engine stopped unexpectedly.")
+ self.beginDowngradeTransition(reason: "The voice-processing microphone engine stopped unexpectedly.")
}
}
- private func interrupt(detail: String) {
+ private func handleAVCaptureFailureSignal(detail: String) {
+ guard self.stateLock.withLock({ self.phase == .avCapture }) else { return }
let shouldEmit = self.stateLock.withLock { () -> Bool in
- guard self.committed, !self.interrupted else { return false }
- self.interrupted = true
+ guard !self.interruptedThisEra else { return false }
+ self.interruptedThisEra = true
+ self.phase = .failed
return true
}
guard shouldEmit else { return }
self.eventHandler(.interrupted(kind: .captureStoppedUnexpectedly, trackID: nil, detail: detail))
}
- func stop() async throws {
- self.watchdogTask?.cancel()
- self.watchdogTask = nil
- self.outputRouteListener = nil
+ private func beginDowngradeTransition(reason: String) {
+ self.beginDowngradeTransition(detail: reason)
+ }
- if let fallback = self.stateLock.withLock({ self.fallbackRuntime }) {
- try await fallback.stop()
- return
+ private func beginDowngradeTransition(detail: String) {
+ let shouldStart = self.stateLock.withLock { () -> Bool in
+ guard self.phase == .vpio, !self.stopRequested else { return false }
+ self.phase = .transitioning
+ return true
}
- guard self.stateLock.withLock({ self.committed }) else {
- self.gate.abort()
- await self.micCapture.stop()
- await self.stopAppOnlyIfStarted()
+ guard shouldStart else { return }
+ let cancellables = self.stateLock.withLock { () -> [Task<Void, Never>?] in
+ defer { self.dwellTask = nil; self.watchdogTask = nil }
+ return [self.dwellTask, self.watchdogTask]
+ }
+ cancellables.forEach { $0?.cancel() }
+ let task = Task { [weak self] in
+ guard let self else { return }
+ await self.runDowngrade(detail: detail)
+ }
+ self.stateLock.withLock { self.transitionTask = task }
+ }
+
+ private func runDowngrade(detail: String) async {
+ try? await Task.sleep(nanoseconds: UInt64(Self.downgradeSettleSeconds * 1_000_000_000))
+ guard !(self.stateLock.withLock({ self.stopRequested })) else {
+ self.finishTransition()
return
}
- // Quiesce first: stats must not be snapshotted while the tap can still emit, and stop-time
- // churn must not poison the drift record (capture.stop tears down observers before the tap).
- await self.micCapture.stop()
- let statsSnapshot = await self.micCapture.statistics()
+ let oldFence = self.stateLock.withLock { self.currentFence }
+ let oldMicCapture = self.stateLock.withLock { self.micCapture }
+ await oldFence.quiesce()
+ let statsSnapshot = await oldMicCapture.statistics()
let drift = MeetingClockDriftRecord(
cumulativeAbsorbedSeconds: statsSnapshot.cumulativeAbsorbedCorrectionSeconds,
elapsedValidHostSeconds: MeetingVoiceProcessingDriftSnapshot.elapsedValidHostSeconds(statsSnapshot),
eligible: MeetingVoiceProcessingDriftSnapshot.isEligible(statsSnapshot)
)
- try await self.microphoneWriter.updateTrackMetadata { track in track.clockDrift = drift }
+ // Detached, supervised: releases the HAL device before the new client opens it, without blocking the swap.
+ oldMicCapture.stopDetachedSupervised(seconds: Self.oldProducerStopSupervisionSeconds) { [weak self] completedInTime in
+ guard let self, !completedInTime else { return }
+ self.stateLock.withLock { self.quarantined = true }
+ }
+
+ guard !(self.stateLock.withLock({ self.stopRequested })) else {
+ self.finishTransition()
+ return
+ }
+
+ let elected = MeetingCaptureDeviceElection.elect(original: self.originalMicrophone)
+ let newFence = MeetingEraAcceptanceFence()
+ let newGate = MeetingVoiceProcessingCommitGate()
+ let microphoneWriter = self.microphoneWriter
+ let liveAudioHandler = self.liveAudioHandler
+ let component: MeetingAVCaptureMicrophoneComponent
+ do {
+ component = try MeetingAVCaptureMicrophoneComponent(
+ microphone: elected.identity,
+ onSample: { sample in
+ guard newFence.beginSample() else { return }
+ defer { newFence.endSample() }
+ switch newGate.offer(sample) {
+ case .passthrough:
+ microphoneWriter.enqueue(sample)
+ liveAudioHandler?(.microphone, sample)
+ case .buffered, .aborted:
+ break
+ }
+ },
+ onTerminalStop: { [weak self] detail in
+ self?.handleAVCaptureFailureSignal(detail: detail)
+ }
+ )
+ try await component.start()
+ } catch {
+ self.stateLock.withLock { self.phase = .failed }
+ self.eventHandler(.interrupted(
+ kind: .captureStoppedUnexpectedly, trackID: nil,
+ detail: "Microphone unavailable after a route change: \(error.localizedDescription)"
+ ))
+ self.finishTransition()
+ return
+ }
+
+ if self.stateLock.withLock({ self.stopRequested }) {
+ try? await component.stop()
+ self.finishTransition()
+ return
+ }
+
+ let boundary = await self.microphoneWriter.beginSplice()
+
+ // A nil or non-monotonic boundary would misattribute audio; skip the era record instead.
+ var recordedNewEra = false
+ try? await self.microphoneWriter.updateTrackMetadata { [weak self] track in
+ guard let self else { return }
+ var eras = track.captureEras ?? [self.legacyEra(from: track)]
+ if var last = eras.popLast() {
+ last.clockDrift = drift
+ eras.append(last)
+ }
+ if let boundary, boundary.seconds > (eras.last?.startSeconds ?? -.infinity) {
+ eras.append(MeetingCaptureEra(
+ method: .avCaptureSession,
+ deviceUID: elected.identity.coreAudioUID,
+ deviceName: elected.identity.displayName,
+ roleAtElection: elected.role,
+ startSeconds: boundary.seconds
+ ))
+ recordedNewEra = true
+ }
+ track.captureEras = eras
+ // Multi-era: a track-level-only reader sees the conservative (AVCapture) value.
+ track.captureMethod = .avCaptureSession
+ track.clockDrift = drift
+ }
+ if !recordedNewEra {
+ DebugLogger.shared.warning(
+ "Meeting downgrade: no valid splice boundary (nil or non-monotonic) — the AVCapture era was not recorded; turns after this point stay attributed to the prior era.",
+ source: "MeetingCaptureEngine"
+ )
+ }
+
+ self.stateLock.withLock {
+ self.currentFence = newFence
+ self.avCaptureComponent = component
+ self.phase = .avCapture
+ self.interruptedThisEra = false
+ }
+
+ // Ack before flushing: readers must re-arm before any post-boundary sample reaches them.
+ let ack = MeetingCaptureMethodChangeAck()
+ self.eventHandler(.captureMethodChanged(trackID: self.microphoneWriter.trackID, method: .avCaptureSession, ack: ack))
+ await ack.wait()
+
+ if var batch = newGate.beginCommitFlush() {
+ while true {
+ await self.flushBatch(batch)
+ guard let next = newGate.continueCommitFlush() else { break }
+ batch = next
+ }
+ }
+
+ self.finishTransition()
+ self.armUpgradeDwellIfViable()
+ }
+
+ private func legacyEra(from track: MeetingAudioTrack) -> MeetingCaptureEra {
+ MeetingCaptureEra(
+ method: track.captureMethod ?? .voiceProcessing,
+ deviceUID: self.originalMicrophone.coreAudioUID,
+ deviceName: self.originalMicrophone.displayName,
+ roleAtElection: self.originalMicrophone.role,
+ startSeconds: 0,
+ settledConfig: track.voiceProcessingConfig,
+ clockDrift: track.clockDrift
+ )
+ }
+
+ /// `dwellArming` reserves the slot in the SAME critical section as the `dwellTask == nil` check.
+ private func armUpgradeDwellIfViable() {
+ guard self.stateLock.withLock({
+ guard self.phase == .avCapture, self.dwellTask == nil, !self.dwellArming, !self.quarantined,
+ !self.stopRequested, self.upgradeAttempts < Self.maxUpgradeAttemptsPerSession
+ else { return false }
+ self.dwellArming = true
+ return true
+ }) else { return }
+ let decision = MeetingCapturePathDecider.decide(
+ mode: .onlineCall,
+ microphone: self.originalMicrophone,
+ outputRoute: MeetingCaptureEngine.currentOutputRouteSnapshot()
+ )
+ guard case .voiceProcessing = decision else {
+ self.stateLock.withLock { self.dwellArming = false }
+ return
+ }
+
+ let attemptIndex = self.stateLock.withLock { self.upgradeAttempts }
+ let backoffSeconds = attemptIndex == 0 ? 0 : Self.upgradeBackoffBaseSeconds * pow(2, Double(attemptIndex - 1))
+ let task = Task { [weak self] in
+ try? await Task.sleep(nanoseconds: UInt64(backoffSeconds * 1_000_000_000))
+ try? await Task.sleep(nanoseconds: UInt64(Self.upgradeDwellSeconds * 1_000_000_000))
+ guard let self, !Task.isCancelled else { return }
+ self.stateLock.withLock { self.dwellTask = nil }
+ guard !(self.stateLock.withLock({ self.stopRequested })), self.stateLock.withLock({ self.phase == .avCapture }) else { return }
+ let stillViable = MeetingCapturePathDecider.decide(
+ mode: .onlineCall,
+ microphone: self.originalMicrophone,
+ outputRoute: MeetingCaptureEngine.currentOutputRouteSnapshot()
+ )
+ guard case .voiceProcessing = stillViable else { return }
+ await self.attemptUpgrade()
+ }
+ self.stateLock.withLock { self.dwellTask = task; self.dwellArming = false }
+ }
+
+ private func attemptUpgrade() async {
+ let shouldStart = self.stateLock.withLock { () -> Bool in
+ guard self.phase == .avCapture, !self.stopRequested else { return false }
+ self.phase = .transitioning
+ self.upgradeAttempts += 1
+ return true
+ }
+ guard shouldStart else { return }
+ let task = Task { [weak self] in
+ guard let self else { return }
+ await self.runUpgrade()
+ }
+ self.stateLock.withLock { self.transitionTask = task }
+ await task.value
+ }
+
+ private func runUpgrade() async {
+ try? await Task.sleep(nanoseconds: UInt64(Self.upgradeSettleSeconds * 1_000_000_000))
+ guard !(self.stateLock.withLock({ self.stopRequested })) else {
+ self.finishTransition()
+ return
+ }
+ guard case .voiceProcessing = MeetingCapturePathDecider.decide(
+ mode: .onlineCall, microphone: self.originalMicrophone,
+ outputRoute: MeetingCaptureEngine.currentOutputRouteSnapshot()
+ ) else {
+ self.stateLock.withLock { self.phase = .avCapture }
+ self.finishTransition()
+ return
+ }
+
+ let elected = MeetingCaptureDeviceElection.elect(original: self.originalMicrophone)
+ let candidateMicCapture = MeetingMicrophoneCapture()
+ let candidateGate = MeetingVoiceProcessingCommitGate()
+ let candidateFence = MeetingEraAcceptanceFence()
+
+ let outcome: MeetingMicrophoneBindingOutcome
+ do {
+ outcome = try await candidateMicCapture.start(
+ microphone: elected.identity,
+ onSample: { sample in
+ guard candidateFence.beginSample() else { return }
+ defer { candidateFence.endSample() }
+ _ = candidateGate.offer(sample) // ring-gated: AVCapture stays authoritative
+ },
+ onEvent: { [weak self] event in
+ self?.handleMicEvent(event, owner: candidateMicCapture)
+ }
+ )
+ } catch {
+ self.abortUpgrade(candidateMicCapture: candidateMicCapture, reason: error.localizedDescription)
+ return
+ }
+ if case .unavailable = outcome {
+ self.abortUpgrade(candidateMicCapture: candidateMicCapture, reason: "Voice-processing microphone capture was unavailable.")
+ return
+ }
+ guard await self.waitForBufferProgress(timeoutSeconds: Self.firstBufferDeadlineSeconds, using: candidateMicCapture) else {
+ self.abortUpgrade(candidateMicCapture: candidateMicCapture, reason: "No microphone audio was captured within 2s.")
+ return
+ }
+ guard !(self.stateLock.withLock({ self.stopRequested })) else {
+ self.abortUpgrade(candidateMicCapture: candidateMicCapture, reason: nil)
+ return
+ }
+ guard case .voiceProcessing = MeetingCapturePathDecider.decide(
+ mode: .onlineCall, microphone: self.originalMicrophone,
+ outputRoute: MeetingCaptureEngine.currentOutputRouteSnapshot()
+ ) else {
+ self.abortUpgrade(candidateMicCapture: candidateMicCapture, reason: "Route changed back before the upgrade committed.")
+ return
+ }
+
+ // Fence AVCapture's own acceptance so exactly one producer feeds the post-boundary chunk.
+ let oldFence = self.stateLock.withLock { self.currentFence }
+ let oldComponent = self.stateLock.withLock { self.avCaptureComponent }
+ await oldFence.quiesce()
+
+ let boundary = await self.microphoneWriter.beginSplice()
+ let boundaryTime = boundary.map { CMTime(value: $0.value, timescale: $0.timescale) }
+
+ guard let ring = candidateGate.beginCommitFlush() else {
+ self.abortUpgrade(candidateMicCapture: candidateMicCapture, reason: "Voice-processing commit gate aborted before commit.")
+ return
+ }
+ // Discard ring samples at/behind the boundary — else every upgrade duplicates ~2s.
+ var pendingRing = MeetingUpgradeRingDiscard.discardingSamples(atOrBehind: boundaryTime, from: ring)
+
+ let settled = await candidateMicCapture.settledConfiguration()
+ // A nil or non-monotonic boundary would misattribute audio; skip the era record instead.
+ var recordedNewEra = false
+ try? await self.microphoneWriter.updateTrackMetadata { track in
+ var eras = track.captureEras ?? []
+ if let boundary, boundary.seconds > (eras.last?.startSeconds ?? -.infinity) {
+ eras.append(MeetingCaptureEra(
+ method: .voiceProcessing,
+ deviceUID: elected.identity.coreAudioUID,
+ deviceName: elected.identity.displayName,
+ roleAtElection: elected.role,
+ startSeconds: boundary.seconds,
+ settledConfig: settled
+ ))
+ recordedNewEra = true
+ }
+ track.captureEras = eras
+ track.captureMethod = .avCaptureSession // still multi-era: conservative legacy value
+ track.voiceProcessingConfig = settled
+ }
+ if !recordedNewEra {
+ DebugLogger.shared.warning(
+ "Meeting upgrade: no valid splice boundary (nil or non-monotonic) — the voice-processing era was not recorded; turns after this point stay attributed to the prior era.",
+ source: "MeetingCaptureEngine"
+ )
+ }
+
+ // Awaited: the MainActor hop (filter re-arm, history entry) must land before the gate opens.
+ let ack = MeetingCaptureMethodChangeAck()
+ self.eventHandler(.captureMethodChanged(trackID: self.microphoneWriter.trackID, method: .voiceProcessing, ack: ack))
+ await ack.wait()
+
+ self.stateLock.withLock {
+ self.currentFence = candidateFence
+ self.micCapture = candidateMicCapture
+ self.gate = candidateGate
+ self.phase = .vpio
+ self.interruptedThisEra = false
+ self.upgradeAttempts = 0
+ self.avCaptureComponent = nil
+ }
+
+ while true {
+ await self.flushBatch(pendingRing)
+ guard let next = candidateGate.continueCommitFlush() else { break }
+ pendingRing = next
+ }
+
+ self.startWatchdog()
+
+ if let oldComponent {
+ oldComponent.stopDetached(supervisedSeconds: Self.oldProducerStopSupervisionSeconds) { [weak self] completedInTime in
+ guard let self, !completedInTime else { return }
+ self.stateLock.withLock { self.quarantined = true }
+ }
+ }
+
+ self.finishTransition()
+ }
+
+ private func abortUpgrade(candidateMicCapture: MeetingMicrophoneCapture, reason: String?) {
+ if let reason {
+ DebugLogger.shared.log("Meeting VPIO upgrade aborted: \(reason)", source: "MeetingCaptureEngine")
+ }
+ // Tracked in `abortedCaptureDrain` so stop() can wait it out instead of leaking it.
+ self.abortedCaptureDrain.enter()
+ candidateMicCapture.stopDetachedSupervised(seconds: Self.oldProducerStopSupervisionSeconds) { [weak self] _ in
+ self?.abortedCaptureDrain.leave()
+ }
+ self.stateLock.withLock { self.phase = .avCapture }
+ self.finishTransition()
+ self.armUpgradeDwellIfViable()
+ }
+
+ private func drainAbortedCaptures() async {
+ await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
+ self.abortedCaptureDrain.notify(queue: .global(qos: .userInitiated)) {
+ continuation.resume()
+ }
+ }
+ }
+
+ private func finishTransition() {
+ self.stateLock.withLock { self.transitionTask = nil }
+ }
+
+ func stop() async throws {
+ let dwell = self.stateLock.withLock { () -> Task<Void, Never>? in
+ self.stopRequested = true
+ defer { self.dwellTask = nil }
+ return self.dwellTask
+ }
+ dwell?.cancel()
+
+ if let transitionTask = self.stateLock.withLock({ self.transitionTask }) {
+ await transitionTask.value
+ }
+ await self.drainAbortedCaptures()
+ let (watchdog, staleListener) = self.stateLock.withLock { () -> (Task<Void, Never>?, MeetingOutputRouteListener?) in
+ defer { self.watchdogTask = nil; self.outputRouteListener = nil }
+ return (self.watchdogTask, self.outputRouteListener)
+ }
+ watchdog?.cancel()
+ _ = staleListener // released here, outside the lock
+
+ if let fallback = self.stateLock.withLock({ self.fallbackRuntime }) {
+ try await fallback.stop()
+ return
+ }
+
+ let phase = self.stateLock.withLock { self.phase }
+ guard phase == .vpio || phase == .avCapture else {
+ let (gateToAbort, captureToStop) = self.stateLock.withLock { (self.gate, self.micCapture) }
+ gateToAbort.abort()
+ await captureToStop.stop()
+ if let component = self.stateLock.withLock({ self.avCaptureComponent }) {
+ try? await component.stop()
+ }
+ await self.stopAppOnlyIfStarted()
+ return
+ }
+
+ if phase == .vpio {
+ // Quiesce first — stats must not be snapshotted while the tap can still emit.
+ let micCapture = self.stateLock.withLock { self.micCapture }
+ await micCapture.stop()
+ let statsSnapshot = await micCapture.statistics()
+ let drift = MeetingClockDriftRecord(
+ cumulativeAbsorbedSeconds: statsSnapshot.cumulativeAbsorbedCorrectionSeconds,
+ elapsedValidHostSeconds: MeetingVoiceProcessingDriftSnapshot.elapsedValidHostSeconds(statsSnapshot),
+ eligible: MeetingVoiceProcessingDriftSnapshot.isEligible(statsSnapshot)
+ )
+ try? await self.microphoneWriter.updateTrackMetadata { track in
+ track.clockDrift = drift
+ if var eras = track.captureEras, var last = eras.popLast() {
+ last.clockDrift = drift
+ eras.append(last)
+ track.captureEras = eras
+ }
+ }
+ } else if let component = self.stateLock.withLock({ self.avCaptureComponent }) {
+ try? await component.stop()
+ }
if let appOnly = self.stateLock.withLock({ self.appOnlyRuntime }) {
try await appOnly.stop()
diff --git a/Sources/Fluid/Services/Meeting/MeetingMicrophoneCapture.swift b/Sources/Fluid/Services/Meeting/MeetingMicrophoneCapture.swift
index a5568e0e..5a3c77a9 100644
--- a/Sources/Fluid/Services/Meeting/MeetingMicrophoneCapture.swift
+++ b/Sources/Fluid/Services/Meeting/MeetingMicrophoneCapture.swift
@@ -749,6 +749,17 @@ actor MeetingMicrophoneCapture {
self.tapState = nil
}
+ /// Fire-and-forget, bounded teardown. `onComplete(false)` means the supervision window elapsed first.
+ nonisolated func stopDetachedSupervised(seconds: Double, onComplete: @escaping @Sendable (Bool) -> Void) {
+ Task {
+ let completed = await MeetingSupervisedTimeout.run(seconds: seconds) {
+ await self.stop()
+ return true
+ }
+ onComplete(completed == true)
+ }
+ }
+
func statistics() -> Stats {
self.statsBox.snapshot()
}
diff --git a/Sources/Fluid/Services/Meeting/MeetingModels.swift b/Sources/Fluid/Services/Meeting/MeetingModels.swift
index 8078a6bc..da3aabf9 100644
--- a/Sources/Fluid/Services/Meeting/MeetingModels.swift
+++ b/Sources/Fluid/Services/Meeting/MeetingModels.swift
@@ -168,6 +168,32 @@ nonisolated struct MeetingMicrophoneIdentity: Codable, Equatable, Sendable {
}
}
+/// Pure decision for the new-meeting sheet's default microphone: the system default input wins; a remembered role only carries over when that default IS the saved device.
+nonisolated enum MeetingMicrophonePreselection {
+ static func select(
+ identities: [MeetingMicrophoneIdentity],
+ savedDeviceID: String?,
+ savedRole: MeetingMicrophoneRole,
+ systemDefaultUID: String?,
+ preferredInputUID: String?,
+ systemDefaultCaptureID: String?
+ ) -> (deviceID: String?, role: MeetingMicrophoneRole) {
+ let matched = systemDefaultUID.flatMap { uid in
+ identities.first(where: { $0.coreAudioUID == uid })
+ } ?? preferredInputUID.flatMap { uid in
+ identities.first(where: { $0.coreAudioUID == uid })
+ } ?? systemDefaultCaptureID.flatMap { captureID in
+ identities.first(where: { $0.captureDeviceID == captureID })
+ } ?? identities.first
+
+ guard let matched else { return (nil, .unknown) }
+ if matched.captureDeviceID == savedDeviceID { return (matched.captureDeviceID, savedRole) }
+ // A built-in mic is inherently the owner's; .unknown would silently disable voice processing.
+ let role: MeetingMicrophoneRole = matched.coreAudioUID == "BuiltInMicrophoneDevice" ? .personal : .unknown
+ return (matched.captureDeviceID, role)
+ }
+}
+
nonisolated struct MeetingPlatformProfile: Codable, Equatable, Sendable {
var identifier: String
var displayName: String
@@ -318,24 +344,39 @@ nonisolated struct MeetingClockDriftRecord: Codable, Equatable, Sendable {
var eligible: Bool
}
+/// Provenance for one contiguous span of a track's capture. Absence of `captureEras` means single-era legacy.
+nonisolated struct MeetingCaptureEra: Codable, Equatable, Sendable {
+ var method: MeetingAudioTrackCaptureMethod
+ var deviceUID: String?
+ var deviceName: String?
+ var roleAtElection: MeetingMicrophoneRole
+ var startSeconds: Double
+ var settledConfig: MeetingMicrophoneSettledConfig? = nil
+ var clockDrift: MeetingClockDriftRecord? = nil
+}
+
+/// Consumes and discards one malformed `captureEras` array element.
+private struct MeetingCaptureEraSkippedElement: Decodable {}
+
/// Applied ONLY to emitted turn boundaries, never to raw `chunkOffset`. `k = 1 + cumulative/elapsed`:
/// for a fast mic `cumulativeAbsorbedSeconds` is negative, so `k < 1` compresses the timeline.
nonisolated enum MeetingMicrophoneDeDrift {
static let materialityThresholdSeconds: Double = 0.005
static let maxAbsolutePPM: Double = 100
- static func correct(_ t: Double, track: MeetingAudioTrack, origin: Double) -> Double {
- guard track.captureMethod == .voiceProcessing,
- let drift = track.clockDrift, drift.eligible,
+ /// Era-scoped: applies the era's own drift record within its span; `era.startSeconds` must already be origin-relative.
+ static func correct(_ t: Double, era: MeetingCaptureEra, eraEndRelative: Double?) -> Double {
+ guard era.method == .voiceProcessing,
+ let drift = era.clockDrift, drift.eligible,
drift.elapsedValidHostSeconds > 0
else { return t }
+ let eraStartRelative = era.startSeconds
+ guard t >= eraStartRelative, eraEndRelative.map({ t < $0 }) ?? true else { return t }
let fraction = drift.cumulativeAbsorbedSeconds / drift.elapsedValidHostSeconds
guard abs(drift.cumulativeAbsorbedSeconds) > Self.materialityThresholdSeconds,
- abs(fraction) * 1_000_000 < Self.maxAbsolutePPM,
- let micFirstPTS = track.chunks.map(\.presentationStart.seconds).min()
+ abs(fraction) * 1_000_000 < Self.maxAbsolutePPM
else { return t }
- let micFirstRelative = micFirstPTS - origin
- return micFirstRelative + (t - micFirstRelative) * (1 + fraction)
+ return eraStartRelative + (t - eraStartRelative) * (1 + fraction)
}
}
@@ -351,6 +392,10 @@ nonisolated struct MeetingAudioTrack: Identifiable, Equatable, Sendable {
var captureMethod: MeetingAudioTrackCaptureMethod? = nil
var voiceProcessingConfig: MeetingMicrophoneSettledConfig? = nil
var clockDrift: MeetingClockDriftRecord? = nil
+ /// Ordered. PERSISTED startSeconds mixes domains: index 0 stores 0, eras 1+ store raw PTS
+ /// seconds — readers must go through the pipeline's microphoneEras(for:origin:), which
+ /// normalizes to origin-relative. Absence means single-era legacy.
+ var captureEras: [MeetingCaptureEra]? = nil
init(
id: MeetingAudioTrackID,
@@ -363,7 +408,8 @@ nonisolated struct MeetingAudioTrack: Identifiable, Equatable, Sendable {
chunks: [MeetingAudioChunk],
captureMethod: MeetingAudioTrackCaptureMethod? = nil,
voiceProcessingConfig: MeetingMicrophoneSettledConfig? = nil,
- clockDrift: MeetingClockDriftRecord? = nil
+ clockDrift: MeetingClockDriftRecord? = nil,
+ captureEras: [MeetingCaptureEra]? = nil
) {
self.id = id
self.kind = kind
@@ -376,13 +422,14 @@ nonisolated struct MeetingAudioTrack: Identifiable, Equatable, Sendable {
self.captureMethod = captureMethod
self.voiceProcessingConfig = voiceProcessingConfig
self.clockDrift = clockDrift
+ self.captureEras = captureEras
}
}
extension MeetingAudioTrack: Codable {
private enum CodingKeys: String, CodingKey {
case id, kind, sourceIdentifier, sourceDisplayName, format, timebase, health, chunks
- case captureMethod, voiceProcessingConfig, clockDrift
+ case captureMethod, voiceProcessingConfig, clockDrift, captureEras
}
init(from decoder: Decoder) throws {
@@ -400,6 +447,25 @@ extension MeetingAudioTrack: Codable {
self.captureMethod = captureMethodRaw.flatMap(MeetingAudioTrackCaptureMethod.init(rawValue:))
self.voiceProcessingConfig = try container.decodeIfPresent(MeetingMicrophoneSettledConfig.self, forKey: .voiceProcessingConfig)
self.clockDrift = try container.decodeIfPresent(MeetingClockDriftRecord.self, forKey: .clockDrift)
+ self.captureEras = try Self.decodeTolerantCaptureEras(container: container, key: .captureEras)
+ }
+
+ /// An unrecognized era `method` skips that era rather than failing the whole session decode.
+ private static func decodeTolerantCaptureEras(
+ container: KeyedDecodingContainer<CodingKeys>,
+ key: CodingKeys
+ ) throws -> [MeetingCaptureEra]? {
+ guard container.contains(key) else { return nil }
+ var unkeyed = try container.nestedUnkeyedContainer(forKey: key)
+ var eras: [MeetingCaptureEra] = []
+ while !unkeyed.isAtEnd {
+ if let era = try? unkeyed.decode(MeetingCaptureEra.self) {
+ eras.append(era)
+ } else {
+ _ = try? unkeyed.decode(MeetingCaptureEraSkippedElement.self)
+ }
+ }
+ return eras
}
func encode(to encoder: Encoder) throws {
@@ -415,6 +481,7 @@ extension MeetingAudioTrack: Codable {
try container.encodeIfPresent(self.captureMethod, forKey: .captureMethod)
try container.encodeIfPresent(self.voiceProcessingConfig, forKey: .voiceProcessingConfig)
try container.encodeIfPresent(self.clockDrift, forKey: .clockDrift)
+ try container.encodeIfPresent(self.captureEras, forKey: .captureEras)
}
}
@@ -867,10 +934,46 @@ nonisolated enum MeetingModelValidationError: LocalizedError, Equatable {
}
}
+/// Resumed by the coordinator once its MainActor work lands, before the upgrade opens the gate.
+final nonisolated class MeetingCaptureMethodChangeAck: @unchecked Sendable {
+ private let lock = NSLock()
+ private var continuation: CheckedContinuation<Void, Never>?
+ private var resumedEarly = false
+
+ /// Bounded: a dropped event (generation guard) never resumes, and an unbounded wait would deadlock the transition.
+ func wait(timeoutSeconds: Double = 2.0) async {
+ let timeoutTask = Task { [weak self] in
+ try? await Task.sleep(nanoseconds: UInt64(timeoutSeconds * 1_000_000_000))
+ self?.resume()
+ }
+ await withCheckedContinuation { continuation in
+ let shouldResumeNow = self.lock.withLock { () -> Bool in
+ if self.resumedEarly { return true }
+ self.continuation = continuation
+ return false
+ }
+ if shouldResumeNow { continuation.resume() }
+ }
+ timeoutTask.cancel()
+ }
+
+ func resume() {
+ let pending = self.lock.withLock { () -> CheckedContinuation<Void, Never>? in
+ defer { self.resumedEarly = true }
+ let continuation = self.continuation
+ self.continuation = nil
+ return continuation
+ }
+ pending?.resume()
+ }
+}
+
nonisolated enum MeetingCaptureEvent: Sendable {
case trackHealth(trackID: MeetingAudioTrackID, health: MeetingTrackHealth)
case chunkFinalized(trackID: MeetingAudioTrackID, chunk: MeetingAudioChunk, format: MeetingAudioFormat)
case interrupted(kind: MeetingInterruptionKind, trackID: MeetingAudioTrackID?, detail: String?)
+ /// `ack` is `nil` for downgrades — the mic is already dead, no race to gate on.
+ case captureMethodChanged(trackID: MeetingAudioTrackID, method: MeetingAudioTrackCaptureMethod, ack: MeetingCaptureMethodChangeAck?)
}
/// The scope ScreenCaptureKit was filtering to for this capture. `nil` when the runtime has no
diff --git a/Sources/Fluid/Services/Meeting/MeetingProcessingPipeline.swift b/Sources/Fluid/Services/Meeting/MeetingProcessingPipeline.swift
index 4915520b..e7f1522a 100644
--- a/Sources/Fluid/Services/Meeting/MeetingProcessingPipeline.swift
+++ b/Sources/Fluid/Services/Meeting/MeetingProcessingPipeline.swift
@@ -319,6 +319,7 @@ private final class MeetingProviderLanguagePin {
@MainActor
final class MeetingProcessingPipeline: MeetingProcessingControlling {
/// Bump whenever classification rules change so a resumed run can't mix rules mid-session.
+ /// Era-awareness did NOT bump this: the mic pass re-runs every time, and multi-era tracks cannot predate this build.
static let pipelineVersion = 7
/// Per-turn engines starve on short turns, and an all-empty chunk collapses to unlabeled.
static let perTurnTurnMergeGapSeconds = 5.0
@@ -390,7 +391,8 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
elected: SessionSpeakerID?,
captureMethod: MeetingAudioTrackCaptureMethod?,
// `unscoredOverlap` pins to 0.0s under VPIO; this carries the widening delta instead.
- widenedIndices: Set<Int> = []
+ widenedIndices: Set<Int> = [],
+ captureEras: [MeetingCaptureEra]? = nil
) {
let widenedSeconds = widenedIndices.reduce(0.0) { total, offset in
guard turns.indices.contains(offset) else { return total }
@@ -443,8 +445,11 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
}
}
let captureMethodDescription = captureMethod.map(String.init(describing:)) ?? "nil"
+ let eraDescription = captureEras.map { eras in
+ ", eras=[" + eras.map { String(format: "%@@%.1fs", String(describing: $0.method), $0.startSeconds) }.joined(separator: ", ") + "]"
+ } ?? ""
DebugLogger.shared.info(
- "Local speaker election (role=\(role), captureMethod=\(captureMethodDescription), "
+ "Local speaker election (role=\(role), captureMethod=\(captureMethodDescription)\(eraDescription), "
+ "elected=\(elected?.uuidString.prefix(8).description ?? "none"), "
+ "widened=\(widenedIndices.count)/\(String(format: "%.1f", widenedSeconds))s):\n "
+ rows.joined(separator: "\n ")
@@ -456,32 +461,42 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
/// Drops turns the microphone only heard at a distance. They are not echo — a TV or another
/// room never matches the far end's words — so nothing downstream can catch them, and left in
/// they form a rival cluster that outweighs the user and blocks the "You" election entirely.
- nonisolated static func rejectingFarField(_ turns: [StagedMicrophoneTurn]) -> [StagedMicrophoneTurn] {
- let measured = turns.filter { $0.rms > 0 }
- guard !measured.isEmpty else { return turns }
- let reference = MeetingNearFieldGate.referenceRMS(
- turns: measured.map { (rms: $0.rms, duration: max(0, $0.end - $0.start)) }
- )
- var kept: [StagedMicrophoneTurn] = []
- var rejected: [StagedMicrophoneTurn] = []
- for turn in turns {
- // An unmeasured turn is kept: absence of a measurement is not evidence of distance.
- if turn.rms <= 0 || MeetingNearFieldGate.isNearField(rms: turn.rms, reference: reference) {
- kept.append(turn)
- } else {
- rejected.append(turn)
+ /// Gates each turn against its own era's reference (VPIO runs hotter via AGC).
+ nonisolated static func rejectingFarField(
+ _ turns: [StagedMicrophoneTurn],
+ eras: [MeetingCaptureEra]
+ ) -> [StagedMicrophoneTurn] {
+ guard !turns.isEmpty else { return turns }
+ var indicesByEra: [Int: [Int]] = [:]
+ for (offset, turn) in turns.enumerated() {
+ indicesByEra[Self.eraIndex(containing: turn.start, eras: eras), default: []].append(offset)
+ }
+ var rejectedOffsets = Set<Int>()
+ for (eraIndex, offsets) in indicesByEra {
+ let eraTurns = offsets.map { turns[$0] }
+ let measured = eraTurns.filter { $0.rms > 0 }
+ guard !measured.isEmpty else { continue }
+ let reference = MeetingNearFieldGate.referenceRMS(
+ turns: measured.map { (rms: $0.rms, duration: max(0, $0.end - $0.start)) }
+ )
+ var eraRejectedSeconds: TimeInterval = 0
+ for offset in offsets {
+ let turn = turns[offset]
+ guard turn.rms > 0, !MeetingNearFieldGate.isNearField(rms: turn.rms, reference: reference) else { continue }
+ rejectedOffsets.insert(offset)
+ eraRejectedSeconds += max(0, turn.end - turn.start)
}
+ guard eraRejectedSeconds > 0 else { continue }
+ DebugLogger.shared.info(
+ String(
+ format: "Near-field gate: era %d dropped %.1fs of %d microphone turns below reference %.5f RMS − %.0f dB",
+ eraIndex, eraRejectedSeconds, eraTurns.count, reference ?? 0, MeetingNearFieldGate.rejectionDepthDB
+ ),
+ source: "MeetingProcessingPipeline"
+ )
}
- guard !rejected.isEmpty else { return turns }
- let rejectedSeconds = rejected.reduce(0.0) { $0 + max(0, $1.end - $1.start) }
- DebugLogger.shared.info(
- String(
- format: "Near-field gate: dropped %d of %d microphone turns (%.1fs) below reference %.5f RMS − %.0f dB",
- rejected.count, turns.count, rejectedSeconds, reference ?? 0, MeetingNearFieldGate.rejectionDepthDB
- ),
- source: "MeetingProcessingPipeline"
- )
- return kept
+ guard !rejectedOffsets.isEmpty else { return turns }
+ return turns.enumerated().filter { !rejectedOffsets.contains($0.offset) }.map(\.element)
}
/// Reads the chunk once and measures each turn's RMS. Failure degrades to zeros, which the gate
@@ -1354,6 +1369,9 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
)
var stagedTurns: [StagedMicrophoneTurn] = []
var echoMonitor = EchoMonitorAccumulator()
+ let eras = Self.microphoneEras(for: track, origin: context.origin)
+ // Era 0 has no writer-boundary anchor, so de-drift anchors it at the track's own first PTS.
+ let era0AnchorRelative = (track.chunks.map(\.presentationStart.seconds).min() ?? context.origin) - context.origin
for chunk in track.chunks where chunk.finalizationState == .finalized {
let accumulatorSnapshot = accumulator
let stagedTurnsSnapshot = stagedTurns
@@ -1361,6 +1379,8 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
try await self.processMicrophoneChunk(
chunk,
track: track,
+ eras: eras,
+ era0AnchorRelative: era0AnchorRelative,
diarizer: diarizer,
context: context,
remoteSpeech: remoteSpeech,
@@ -1382,6 +1402,7 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
self.finishMicrophoneTrack(
track,
session: session,
+ origin: context.origin,
stagedTurns: stagedTurns,
echoMonitor: echoMonitor,
applicationTrackHadSpeech: !remoteSpeech.isEmpty,
@@ -1392,6 +1413,8 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
private func processMicrophoneChunk(
_ chunk: MeetingAudioChunk,
track: MeetingAudioTrack,
+ eras: [MeetingCaptureEra],
+ era0AnchorRelative: TimeInterval,
diarizer: SpeakerDiarizationService,
context: ProcessingContext,
remoteSpeech: [TimelineInterval],
@@ -1485,8 +1508,18 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
for (offset, element) in transcribedTurns.enumerated() {
let (index, turn, text) = element
// De-drift applies ONLY here — echo verdicts below keep consuming raw chunkOffset.
- let start = MeetingMicrophoneDeDrift.correct(chunkOffset + turn.startSeconds, track: track, origin: context.origin)
- let end = MeetingMicrophoneDeDrift.correct(chunkOffset + turn.endSeconds, track: track, origin: context.origin)
+ let rawStart = chunkOffset + turn.startSeconds
+ let turnEraIndex = Self.eraIndex(containing: rawStart, eras: eras)
+ var turnEra = eras[turnEraIndex]
+ // Era 0's containment boundary is a sentinel, not a real anchor.
+ if turnEraIndex == 0 { turnEra.startSeconds = era0AnchorRelative }
+ let turnEraEnd = Self.eraEndRelative(afterIndex: turnEraIndex, eras: eras)
+ let start = MeetingMicrophoneDeDrift.correct(
+ rawStart, era: turnEra, eraEndRelative: turnEraEnd
+ )
+ let end = MeetingMicrophoneDeDrift.correct(
+ chunkOffset + turn.endSeconds, era: turnEra, eraEndRelative: turnEraEnd
+ )
let overlapsRemote = remoteSpeech.contains {
min(end, $0.end) - max(start, $0.start) > 0.15
}
@@ -1553,11 +1586,42 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
}
}
- /// Detector loop unchanged; `.voiceProcessing` then widens `echoScored` to every turn
- /// (fallback-gap/no-app-track evidence hole). `isLikelyEcho` keeps real detector results.
+ /// The only place in the mic batch path allowed to read `track.captureMethod` directly.
+ /// `captureEras[].startSeconds` is raw PTS, converted here to origin-relative; era 0 is pinned to `-infinity`.
+ nonisolated static func microphoneEras(for track: MeetingAudioTrack, origin: TimeInterval) -> [MeetingCaptureEra] {
+ guard let rawEras = track.captureEras, !rawEras.isEmpty else {
+ return [MeetingCaptureEra(
+ method: track.captureMethod ?? .avCaptureSession,
+ deviceUID: nil,
+ deviceName: nil,
+ roleAtElection: .unknown,
+ startSeconds: -.infinity,
+ settledConfig: track.voiceProcessingConfig,
+ clockDrift: track.clockDrift
+ )]
+ }
+ return rawEras.enumerated().map { index, era in
+ var converted = era
+ converted.startSeconds = index == 0 ? -.infinity : era.startSeconds - origin
+ return converted
+ }
+ }
+
+ nonisolated static func eraIndex(containing turnStart: TimeInterval, eras: [MeetingCaptureEra]) -> Int {
+ var index = 0
+ for (offset, era) in eras.enumerated() where era.startSeconds <= turnStart {
+ index = offset
+ }
+ return index
+ }
+
+ nonisolated static func eraEndRelative(afterIndex index: Int, eras: [MeetingCaptureEra]) -> TimeInterval? {
+ eras.indices.contains(index + 1) ? eras[index + 1].startSeconds : nil
+ }
+
nonisolated static func classifyMicrophoneTurns(
_ turns: [StagedMicrophoneTurn],
- captureMethod: MeetingAudioTrackCaptureMethod?,
+ eras: [MeetingCaptureEra],
applicationTrackID: MeetingAudioTrackID?,
segments: [MeetingTranscriptSegment],
fallbackSpeakerID: SessionSpeakerID?
@@ -1583,13 +1647,13 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
}
}
var widenedIndices = Set<Int>()
- if captureMethod == .voiceProcessing {
- for index in classified.indices {
- if !detectorExamined.contains(index) {
- widenedIndices.insert(index)
- }
- classified[index].echoScored = true
+ for index in classified.indices {
+ let era = eras[Self.eraIndex(containing: classified[index].start, eras: eras)]
+ guard era.method == .voiceProcessing else { continue }
+ if !detectorExamined.contains(index) {
+ widenedIndices.insert(index)
}
+ classified[index].echoScored = true
}
return (classified, widenedIndices)
}
@@ -1636,15 +1700,15 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
private func finishMicrophoneTrack(
_ track: MeetingAudioTrack,
session: MeetingSession,
+ origin: TimeInterval,
stagedTurns allStagedTurns: [StagedMicrophoneTurn],
echoMonitor: EchoMonitorAccumulator,
applicationTrackHadSpeech: Bool,
accumulator: inout Accumulator
) {
- // Session-wide, never per chunk: a chunk holding only distant audio would otherwise take
- // that audio as its own reference and gate nothing.
+ let eras = Self.microphoneEras(for: track, origin: origin)
let stagedTurns = session.selectedMicrophone.role == .personal
- ? Self.rejectingFarField(allStagedTurns)
+ ? Self.rejectingFarField(allStagedTurns, eras: eras)
: allStagedTurns
if session.mode == .inRoom {
@@ -1689,7 +1753,7 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
let fallbackSpeakerID = accumulator.speakerIDByKey["meeting-audio"]
let (classifiedTurns, widenedIndices) = Self.classifyMicrophoneTurns(
stagedTurns,
- captureMethod: track.captureMethod,
+ eras: eras,
applicationTrackID: applicationTrackID,
segments: accumulator.segments,
fallbackSpeakerID: fallbackSpeakerID
@@ -1705,8 +1769,9 @@ final class MeetingProcessingPipeline: MeetingProcessingControlling {
index: accumulator.embeddingIndexByTrack[.microphone],
role: session.selectedMicrophone.role,
elected: localClusterID,
- captureMethod: track.captureMethod,
- widenedIndices: widenedIndices
+ captureMethod: eras.last?.method,
+ widenedIndices: widenedIndices,
+ captureEras: track.captureEras
)
Self.logEchoMonitorSummary(
turns: classifiedTurns,
diff --git a/Sources/Fluid/Services/Meeting/MeetingSessionCoordinator.swift b/Sources/Fluid/Services/Meeting/MeetingSessionCoordinator.swift
index a64cae45..9352b86f 100644
--- a/Sources/Fluid/Services/Meeting/MeetingSessionCoordinator.swift
+++ b/Sources/Fluid/Services/Meeting/MeetingSessionCoordinator.swift
@@ -1326,6 +1326,31 @@ final class MeetingSessionCoordinator: ObservableObject {
} else {
session.timebase.firstPresentationTime = chunk.presentationStart
}
+ case let .captureMethodChanged(trackID, method, ack):
+ // Resumed after this lands, before the upgrade opens the gate. Downgrades pass `ack: nil`.
+ defer { ack?.resume() }
+ guard let index = session.audioTracks.firstIndex(where: { $0.id == trackID }) else { return }
+ session.audioTracks[index].captureMethod = method
+ if session.audioTracks[index].kind == .microphone {
+ self.liveTranscriptionCoordinator?.setMicrophoneCaptureMethod(method)
+ self.liveTranscriptionCoordinator?.resetMicrophoneUtterance()
+ }
+ session.events.append(MeetingSessionEvent(
+ id: UUID(),
+ occurredAt: Date(),
+ kind: .microphoneChanged,
+ trackID: trackID,
+ detail: "Microphone capture switched to \(method.rawValue)."
+ ))
+ // Only clear degradation this change plausibly caused — never a sticky one.
+ if session.state == .recordingDegraded,
+ self.degradeReason == nil || self.degradeReason == .sourceLoss || self.degradeReason == .silence,
+ !session.audioTracks.contains(where: { $0.health.status == .degraded })
+ {
+ session.state = .recording
+ self.state = .recording(session.id)
+ self.degradeReason = nil
+ }
case let .interrupted(kind, trackID, detail):
session.events.append(MeetingSessionEvent(
id: UUID(),
diff --git a/Sources/Fluid/UI/MeetingTranscriptionView.swift b/Sources/Fluid/UI/MeetingTranscriptionView.swift
index 05ff5e25..7826bf0a 100644
--- a/Sources/Fluid/UI/MeetingTranscriptionView.swift
+++ b/Sources/Fluid/UI/MeetingTranscriptionView.swift
@@ -37,8 +37,9 @@ struct MeetingTranscriptionSetupDraft: Equatable {
self.mode = defaults.mode
self.title = Self.defaultTitle(mode: defaults.mode, applicationDisplayName: nil)
self.selectedApplicationID = nil
- self.selectedMicrophoneID = defaults.microphoneCaptureDeviceID
- self.microphoneRole = defaults.microphoneRole
+ // Left unset here: selectPreferredMicrophone decides once identities load.
+ self.selectedMicrophoneID = nil
+ self.microphoneRole = .unknown
}
static func defaultTitle(mode: MeetingCaptureMode, applicationDisplayName: String?) -> String {
@@ -488,19 +489,19 @@ struct MeetingTranscriptionView: View {
return
}
- let settings = SettingsStore.shared
- let defaults = settings.meetingRecordingDefaults
- let preferredCoreAudioUID = defaults.microphoneCoreAudioUID ?? settings.preferredInputDeviceUID
+ let defaults = SettingsStore.shared.meetingRecordingDefaults
let savedMicrophone = defaults.savedMicrophone(in: identities)
- if defaults.isConfigured {
- self.setupDraft.selectedMicrophoneID = savedMicrophone?.captureDeviceID
- return
- }
-
- let preferred = savedMicrophone ?? (try? MeetingCaptureSourceCatalog.defaultMicrophone(
- preferredCoreAudioUID: preferredCoreAudioUID
- ))
- self.setupDraft.selectedMicrophoneID = preferred?.captureDeviceID ?? identities.first?.captureDeviceID
+ // Default preselection follows the system's current input, not the remembered device.
+ let selection = MeetingMicrophonePreselection.select(
+ identities: identities,
+ savedDeviceID: savedMicrophone?.captureDeviceID,
+ savedRole: defaults.microphoneRole,
+ systemDefaultUID: AudioDevice.getDefaultInputDevice()?.uid,
+ preferredInputUID: SettingsStore.shared.preferredInputDeviceUID,
+ systemDefaultCaptureID: AVCaptureDevice.default(for: .audio)?.uniqueID
+ )
+ self.setupDraft.selectedMicrophoneID = selection.deviceID
+ self.setupDraft.microphoneRole = selection.role
}
private func selectPreferredApplication(from identities: [MeetingApplicationIdentity]) {
diff --git a/Tests/FluidDictationIntegrationTests/MeetingEraBatchTests.swift b/Tests/FluidDictationIntegrationTests/MeetingEraBatchTests.swift
new file mode 100644
index 00000000..0f3d9b6a
--- /dev/null
+++ b/Tests/FluidDictationIntegrationTests/MeetingEraBatchTests.swift
@@ -0,0 +1,271 @@
+@testable import FluidVoice_Debug
+import Foundation
+import XCTest
+
+/// Batch era-awareness: `captureEras[].startSeconds` is raw PTS; `microphoneEras(for:origin:)`'s domain conversion to origin-relative seconds is pinned here.
+final class MeetingEraBatchTests: XCTestCase {
+ private func track(
+ captureMethod: MeetingAudioTrackCaptureMethod? = nil,
+ clockDrift: MeetingClockDriftRecord? = nil,
+ captureEras: [MeetingCaptureEra]? = nil
+ ) -> MeetingAudioTrack {
+ MeetingAudioTrack(
+ id: UUID(), kind: .microphone, sourceIdentifier: "mic", sourceDisplayName: "Mic",
+ format: nil,
+ timebase: MeetingTimebaseMetadata(startedHostTime: 0, machTimebaseNumerator: 1, machTimebaseDenominator: 1, firstPresentationTime: nil),
+ health: .waiting, chunks: [], captureMethod: captureMethod, clockDrift: clockDrift,
+ captureEras: captureEras
+ )
+ }
+
+ private func era(
+ _ method: MeetingAudioTrackCaptureMethod,
+ startSeconds: Double,
+ clockDrift: MeetingClockDriftRecord? = nil
+ ) -> MeetingCaptureEra {
+ MeetingCaptureEra(
+ method: method, deviceUID: nil, deviceName: nil, roleAtElection: .unknown,
+ startSeconds: startSeconds, clockDrift: clockDrift
+ )
+ }
+
+ private func turn(
+ clusterID: SessionSpeakerID = UUID(),
+ start: TimeInterval,
+ end: TimeInterval,
+ text: String = "hi",
+ overlapsRemote: Bool = false,
+ rms: Double = 0
+ ) -> MeetingProcessingPipeline.StagedMicrophoneTurn {
+ MeetingProcessingPipeline.StagedMicrophoneTurn(
+ chunkID: UUID(), index: 0, clusterID: clusterID, clusterLabel: "spk",
+ start: start, end: end, text: text, overlapsRemote: overlapsRemote, rms: rms
+ )
+ }
+
+ // MARK: - Era resolution
+
+ func testLegacyTrackYieldsSingleEraOfCaptureMethod() {
+ let track = self.track(captureMethod: .voiceProcessing)
+ let eras = MeetingProcessingPipeline.microphoneEras(for: track, origin: 0)
+ XCTAssertEqual(eras.count, 1)
+ XCTAssertEqual(eras[0].method, .voiceProcessing)
+ XCTAssertEqual(eras[0].startSeconds, -.infinity, "the single era must own every turn regardless of origin")
+ }
+
+ func testLegacyTrackWithNilCaptureMethodFallsBackSafely() {
+ let track = self.track(captureMethod: nil)
+ let eras = MeetingProcessingPipeline.microphoneEras(for: track, origin: 0)
+ XCTAssertEqual(eras.count, 1)
+ XCTAssertEqual(eras[0].method, .avCaptureSession, "never default to VPIO for an unknown legacy method")
+ }
+
+ /// Era 0 is pinned to `-infinity`; every later era is converted to origin-relative — NOT returned verbatim.
+ func testMultiEraTrackConvertsStoredRawPTSToOriginRelative() {
+ let origin: TimeInterval = 100_000
+ let rawEras = [self.era(.avCaptureSession, startSeconds: 0), self.era(.voiceProcessing, startSeconds: origin + 30)]
+ let track = self.track(captureMethod: .avCaptureSession, captureEras: rawEras)
+ let eras = MeetingProcessingPipeline.microphoneEras(for: track, origin: origin)
+ XCTAssertEqual(eras.count, 2)
+ XCTAssertEqual(eras[0].startSeconds, -.infinity)
+ XCTAssertEqual(eras[1].startSeconds, 30, accuracy: 1e-9)
+ }
+
+ /// Domain-pinning regression: comparing turn times directly against the RAW (unconverted)
+ /// boundary — the bug this guards against — would put both turns in era 0.
+ func testEraIndexResolvesCorrectlyAcrossANonZeroOrigin() {
+ let origin: TimeInterval = 100_000
+ let rawEras = [self.era(.voiceProcessing, startSeconds: 0), self.era(.avCaptureSession, startSeconds: origin + 30)]
+ let track = self.track(captureMethod: .avCaptureSession, captureEras: rawEras)
+ let eras = MeetingProcessingPipeline.microphoneEras(for: track, origin: origin)
+
+ XCTAssertEqual(MeetingProcessingPipeline.eraIndex(containing: 25, eras: eras), 0, "before the boundary")
+ XCTAssertEqual(MeetingProcessingPipeline.eraIndex(containing: 35, eras: eras), 1, "after the boundary")
+
+ XCTAssertEqual(MeetingProcessingPipeline.eraIndex(containing: 25, eras: rawEras), 0)
+ XCTAssertEqual(
+ MeetingProcessingPipeline.eraIndex(containing: 35, eras: rawEras), 0,
+ "raw (unconverted) eras misclassify the post-boundary turn as era 0 — this is why conversion is mandatory"
+ )
+ }
+
+ func testEraIndexMapsTurnToContainingEraIncludingExactBoundary() {
+ let eras = [self.era(.avCaptureSession, startSeconds: 0), self.era(.voiceProcessing, startSeconds: 30)]
+ XCTAssertEqual(MeetingProcessingPipeline.eraIndex(containing: 0, eras: eras), 0)
+ XCTAssertEqual(MeetingProcessingPipeline.eraIndex(containing: 15, eras: eras), 0)
+ XCTAssertEqual(MeetingProcessingPipeline.eraIndex(containing: 29.999, eras: eras), 0)
+ XCTAssertEqual(MeetingProcessingPipeline.eraIndex(containing: 30, eras: eras), 1, "exact boundary belongs to the era that starts there")
+ XCTAssertEqual(MeetingProcessingPipeline.eraIndex(containing: 1000, eras: eras), 1)
+ }
+
+ func testEraEndRelativeIsNextEraStartOrNilForTheLastEra() {
+ let eras = [self.era(.avCaptureSession, startSeconds: 0), self.era(.voiceProcessing, startSeconds: 30)]
+ XCTAssertEqual(MeetingProcessingPipeline.eraEndRelative(afterIndex: 0, eras: eras), 30)
+ XCTAssertNil(MeetingProcessingPipeline.eraEndRelative(afterIndex: 1, eras: eras))
+ }
+
+ // MARK: - Multi-era widening: this must FAIL if any consumer still reads track.captureMethod
+
+ func testWideningAppliesOnlyInsideTheVoiceProcessingEra() {
+ let eras = [self.era(.voiceProcessing, startSeconds: 0), self.era(.avCaptureSession, startSeconds: 30)]
+ let turns = [
+ self.turn(start: 5, end: 8, overlapsRemote: false), // inside VPIO era
+ self.turn(start: 40, end: 42, overlapsRemote: false), // inside AVCapture era
+ ]
+ let (classified, widened) = MeetingProcessingPipeline.classifyMicrophoneTurns(
+ turns, eras: eras, applicationTrackID: nil, segments: [], fallbackSpeakerID: nil
+ )
+ XCTAssertTrue(classified[0].echoScored, "VPIO-era turn must be widened")
+ XCTAssertEqual(widened, [0], "only the VPIO-era turn widens; the AVCapture-era turn is untouched")
+ XCTAssertFalse(classified[1].echoScored)
+ }
+
+ func testNonVoiceProcessingSingleEraNeverWidens() {
+ let eras = [self.era(.avCaptureSession, startSeconds: 0)]
+ let turns = [self.turn(start: 0, end: 3)]
+ let (classified, widened) = MeetingProcessingPipeline.classifyMicrophoneTurns(
+ turns, eras: eras, applicationTrackID: nil, segments: [], fallbackSpeakerID: nil
+ )
+ XCTAssertTrue(widened.isEmpty)
+ XCTAssertFalse(classified[0].echoScored)
+ }
+
+ // MARK: - Per-era de-drift
+
+ func testDeDriftCorrectsOnlyWithinItsOwnEraAndLeavesLaterErasRaw() {
+ let drift = MeetingClockDriftRecord(cumulativeAbsorbedSeconds: 0.02, elapsedValidHostSeconds: 300, eligible: true)
+ let vpioEra = self.era(.voiceProcessing, startSeconds: 0, clockDrift: drift)
+ let avEra = self.era(.avCaptureSession, startSeconds: 30)
+ let eras = [vpioEra, avEra]
+
+ let preBoundaryEnd = MeetingProcessingPipeline.eraEndRelative(afterIndex: 0, eras: eras)
+ let corrected = MeetingMicrophoneDeDrift.correct(20, era: vpioEra, eraEndRelative: preBoundaryEnd)
+ XCTAssertNotEqual(corrected, 20, "materially drifted VPIO era must correct")
+
+ let postEnd = MeetingProcessingPipeline.eraEndRelative(afterIndex: 1, eras: eras)
+ let uncorrected = MeetingMicrophoneDeDrift.correct(40, era: avEra, eraEndRelative: postEnd)
+ XCTAssertEqual(uncorrected, 40, "AVCapture era is never corrected")
+
+ let outOfSpan = MeetingMicrophoneDeDrift.correct(40, era: vpioEra, eraEndRelative: preBoundaryEnd)
+ XCTAssertEqual(outOfSpan, 40)
+ }
+
+ func testEraZeroSentinelSwapYieldsFiniteCorrection() {
+ let drift = MeetingClockDriftRecord(cumulativeAbsorbedSeconds: 0.02, elapsedValidHostSeconds: 300, eligible: true)
+ var track = self.track(captureMethod: .avCaptureSession)
+ track.captureEras = [
+ self.era(.voiceProcessing, startSeconds: 0, clockDrift: drift),
+ self.era(.avCaptureSession, startSeconds: 100_030),
+ ]
+ let eras = MeetingProcessingPipeline.microphoneEras(for: track, origin: 100_000)
+ var era0 = eras[0]
+ XCTAssertEqual(era0.startSeconds, -.infinity)
+ // The pipeline swaps the sentinel for the mic anchor before any arithmetic; prove finite.
+ era0.startSeconds = 5.0
+ let corrected = MeetingMicrophoneDeDrift.correct(
+ 20, era: era0, eraEndRelative: MeetingProcessingPipeline.eraEndRelative(afterIndex: 0, eras: eras)
+ )
+ XCTAssertTrue(corrected.isFinite)
+ XCTAssertNotEqual(corrected, 20)
+ }
+
+ // MARK: - Near-field per-era gate
+
+ func testTwelveDBEraStepProducesZeroCrossEraRejections() {
+ let eras = [self.era(.voiceProcessing, startSeconds: 0), self.era(.avCaptureSession, startSeconds: 100)]
+ let hotRMS = 0.04
+ let quietRMS = hotRMS * pow(10, -12.0 / 20) // 12dB quieter, still above the AVCapture floor
+ var turns: [MeetingProcessingPipeline.StagedMicrophoneTurn] = []
+ for offset in stride(from: 0.0, to: 20.0, by: 2.0) {
+ turns.append(self.turn(start: offset, end: offset + 1, rms: hotRMS))
+ }
+ for offset in stride(from: 100.0, to: 120.0, by: 2.0) {
+ turns.append(self.turn(start: offset, end: offset + 1, rms: quietRMS))
+ }
+ let kept = MeetingProcessingPipeline.rejectingFarField(turns, eras: eras)
+ XCTAssertEqual(kept.count, turns.count, "quiet-but-legit AVCapture-era turns must not be dropped against the hotter VPIO reference")
+ }
+
+ func testSubThreeSecondEraFailsOpen() {
+ let eras = [self.era(.voiceProcessing, startSeconds: 0), self.era(.avCaptureSession, startSeconds: 100)]
+ var turns = [self.turn(start: 0, end: 10, rms: 0.03)]
+ turns.append(self.turn(start: 100, end: 100.5, text: "quiet", rms: 0.01))
+ turns.append(self.turn(start: 101, end: 101.5, text: "quiet", rms: 0.01))
+ let kept = MeetingProcessingPipeline.rejectingFarField(turns, eras: eras)
+ XCTAssertEqual(kept.count, turns.count, "an era with <3s usable turns must fail open (no reference to gate against)")
+ }
+
+ func testSingleEraFixtureMatchesTodaysMath() {
+ let eras = [self.era(.voiceProcessing, startSeconds: 0)]
+ let userRMS = 0.0287
+ let roomRMS = 0.0023
+ var turns: [MeetingProcessingPipeline.StagedMicrophoneTurn] = []
+ for offset in stride(from: 0.0, to: 40.0, by: 2.0) {
+ turns.append(self.turn(start: offset, end: offset + 1.5, rms: userRMS))
+ }
+ for offset in stride(from: 100.0, to: 140.0, by: 2.0) {
+ turns.append(self.turn(start: offset, end: offset + 1.5, rms: roomRMS))
+ }
+ let kept = MeetingProcessingPipeline.rejectingFarField(turns, eras: eras)
+ XCTAssertTrue(kept.allSatisfy { $0.rms == userRMS }, "single-era gating still rejects the room-audio cluster wholesale")
+ XCTAssertEqual(kept.count, 20)
+ }
+
+ // MARK: - Tolerant Codable
+
+ func testMultiEraTrackRoundTrips() throws {
+ let eras = [
+ self.era(.voiceProcessing, startSeconds: 0, clockDrift: MeetingClockDriftRecord(cumulativeAbsorbedSeconds: 0.02, elapsedValidHostSeconds: 60, eligible: true)),
+ self.era(.avCaptureSession, startSeconds: 45),
+ ]
+ let track = self.track(captureMethod: .avCaptureSession, captureEras: eras)
+ let encoder = JSONEncoder()
+ encoder.dateEncodingStrategy = .iso8601
+ let decoder = JSONDecoder()
+ decoder.dateDecodingStrategy = .iso8601
+ let decoded = try decoder.decode(MeetingAudioTrack.self, from: try encoder.encode(track))
+ XCTAssertEqual(decoded.captureEras, eras)
+ }
+
+ func testLegacyJSONWithoutCaptureErasYieldsSingleEraViaHelper() throws {
+ let legacyJSON: [String: Any] = [
+ "id": UUID().uuidString,
+ "kind": "microphone",
+ "sourceIdentifier": "mic",
+ "sourceDisplayName": "Mic",
+ "timebase": ["startedHostTime": 0, "machTimebaseNumerator": 1, "machTimebaseDenominator": 1],
+ "health": ["status": "waiting", "level": 0, "droppedSampleCount": 0],
+ "chunks": [],
+ "captureMethod": "voiceProcessing",
+ ]
+ let data = try JSONSerialization.data(withJSONObject: legacyJSON)
+ let track = try JSONDecoder().decode(MeetingAudioTrack.self, from: data)
+ XCTAssertNil(track.captureEras)
+ let eras = MeetingProcessingPipeline.microphoneEras(for: track, origin: 0)
+ XCTAssertEqual(eras.count, 1)
+ XCTAssertEqual(eras[0].method, .voiceProcessing)
+ }
+
+ func testUnknownEraMethodIsToleratedBySkippingThatEra() throws {
+ let legacyJSON: [String: Any] = [
+ "id": UUID().uuidString,
+ "kind": "microphone",
+ "sourceIdentifier": "mic",
+ "sourceDisplayName": "Mic",
+ "timebase": ["startedHostTime": 0, "machTimebaseNumerator": 1, "machTimebaseDenominator": 1],
+ "health": ["status": "waiting", "level": 0, "droppedSampleCount": 0],
+ "chunks": [],
+ "captureMethod": "avCaptureSession",
+ "captureEras": [
+ ["method": "avCaptureSession", "roleAtElection": "unknown", "startSeconds": 0],
+ ["method": "someFutureMethodThisBuildDoesNotKnow", "roleAtElection": "unknown", "startSeconds": 30],
+ ["method": "voiceProcessing", "roleAtElection": "unknown", "startSeconds": 60],
+ ],
+ ]
+ let data = try JSONSerialization.data(withJSONObject: legacyJSON)
+ let track = try JSONDecoder().decode(MeetingAudioTrack.self, from: data)
+ let eras = try XCTUnwrap(track.captureEras)
+ XCTAssertEqual(eras.count, 2, "the unrecognized era is skipped, not defaulted, and the rest of the session still decodes")
+ XCTAssertEqual(eras.map(\.startSeconds), [0, 60])
+ }
+}
diff --git a/Tests/FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests.swift b/Tests/FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests.swift
index a8098b0e..83367048 100644
--- a/Tests/FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests.swift
+++ b/Tests/FluidDictationIntegrationTests/MeetingMicrophoneCaptureTests.swift
@@ -284,7 +284,7 @@ final class MeetingMicrophoneCaptureTests: XCTestCase {
XCTAssertNotEqual(mismatchReason, errorReason)
}
- // MARK: - Writer-coupled synthetic stamp-sequence test (plan rev #17)
+ // MARK: - Writer-coupled synthetic stamp-sequence test
private func makeWriter() throws -> (writer: MeetingAudioChunkWriter, sessionDirectory: URL) {
let sessionDirectory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
@@ -505,3 +505,442 @@ extension MeetingMicrophoneCaptureTests {
XCTAssertEqual(pts.seconds, 9.0, accuracy: 1.0 / 48_000)
}
}
+
+// MARK: - Bidirectional route-following mic: capture-side unit tests (no real engines/sessions)
+extension MeetingMicrophoneCaptureTests {
+ // MARK: MeetingEraAcceptanceFence
+
+ func testFenceBeginSampleAfterQuiesceReturnsFalse() async {
+ let fence = MeetingEraAcceptanceFence()
+ await fence.quiesce()
+ XCTAssertFalse(fence.beginSample())
+ }
+
+ func testFenceQuiesceAwaitsInFlightZero() async {
+ let fence = MeetingEraAcceptanceFence()
+ XCTAssertTrue(fence.beginSample())
+ XCTAssertTrue(fence.beginSample())
+
+ let quiesced = Task { await fence.quiesce() }
+ try? await Task.sleep(nanoseconds: 100_000_000)
+ XCTAssertFalse(quiesced.isCancelled, "quiesce must still be awaiting drain with 2 in-flight samples")
+
+ fence.endSample()
+ try? await Task.sleep(nanoseconds: 100_000_000)
+ // Still one in-flight: a concurrent beginSample must be rejected once quiesce() starts.
+ XCTAssertFalse(fence.beginSample())
+
+ fence.endSample()
+ await quiesced.value
+ }
+
+ func testFenceReuseAfterQuiesceStaysClosed() async {
+ let fence = MeetingEraAcceptanceFence()
+ await fence.quiesce()
+ XCTAssertFalse(fence.beginSample())
+ XCTAssertFalse(fence.beginSample())
+ }
+
+
+ func testAckResumeBeforeWaitReturnsImmediately() async {
+ let ack = MeetingCaptureMethodChangeAck()
+ ack.resume()
+ let start = Date()
+ await ack.wait(timeoutSeconds: 2.0)
+ XCTAssertLessThan(Date().timeIntervalSince(start), 0.5)
+ }
+
+ func testAckWaitThenResume() async {
+ let ack = MeetingCaptureMethodChangeAck()
+ let waiter = Task { await ack.wait(timeoutSeconds: 2.0) }
+ try? await Task.sleep(nanoseconds: 50_000_000)
+ ack.resume()
+ let start = Date()
+ await waiter.value
+ XCTAssertLessThan(Date().timeIntervalSince(start), 0.5)
+ }
+
+ func testAckBoundedTimeoutFiresWhenNeverResumed() async {
+ let ack = MeetingCaptureMethodChangeAck()
+ let start = Date()
+ await ack.wait(timeoutSeconds: 2.0)
+ XCTAssertLessThan(Date().timeIntervalSince(start), 2.5)
+ }
+
+ func testAckDoubleResumeHarmless() async {
+ let ack = MeetingCaptureMethodChangeAck()
+ ack.resume()
+ ack.resume()
+ let start = Date()
+ await ack.wait(timeoutSeconds: 2.0)
+ XCTAssertLessThan(Date().timeIntervalSince(start), 0.5)
+ }
+
+ // MARK: Writer beginSplice
+
+ func testBeginSpliceReturnsFinalizedChunkPresentationEnd() async throws {
+ let (writer, sessionDirectory) = try self.makeWriter()
+ defer { try? FileManager.default.removeItem(at: sessionDirectory) }
+
+ for tick in 0..<5 { self.pushBuffer(writer, ptsSeconds: Double(tick) * 0.01) }
+ try? await Task.sleep(nanoseconds: 50_000_000) // let enqueue's async consume land
+
+ let boundary = await writer.beginSplice()
+ let track = await writer.stop()
+
+ XCTAssertEqual(track.chunks.count, 1)
+ let finalized = try XCTUnwrap(track.chunks.first)
+ XCTAssertEqual(finalized.finalizationState, .finalized)
+ XCTAssertFalse(finalized.sha256.isEmpty)
+ XCTAssertEqual(try XCTUnwrap(boundary).seconds, finalized.presentationEnd.seconds, accuracy: 1e-9)
+ }
+
+ func testBeginSpliceThenEnqueueOpensNewChunk() async throws {
+ let (writer, sessionDirectory) = try self.makeWriter()
+ defer { try? FileManager.default.removeItem(at: sessionDirectory) }
+
+ self.pushBuffer(writer, ptsSeconds: 0)
+ try? await Task.sleep(nanoseconds: 50_000_000)
+ _ = await writer.beginSplice()
+
+ self.pushBuffer(writer, ptsSeconds: 5)
+ let track = await writer.stop()
+
+ XCTAssertEqual(track.chunks.count, 2)
+ XCTAssertTrue(track.chunks.allSatisfy { $0.finalizationState == .finalized })
+ }
+
+ /// Once `.accepting` is false, `beginSplice` no-ops and returns the last finalized boundary.
+ func testBeginSpliceOnStoppedWriterNoOps() async throws {
+ let (writer, sessionDirectory) = try self.makeWriter()
+ defer { try? FileManager.default.removeItem(at: sessionDirectory) }
+
+ _ = await writer.stop() // stopped with zero chunks: nothing was ever finalized
+ let boundary = await writer.beginSplice()
+ XCTAssertNil(boundary)
+ }
+
+ /// Same no-op, but with existing chunks: returns the last finalized boundary.
+ func testBeginSpliceOnStoppedWriterReturnsLastFinalizedBoundary() async throws {
+ let (writer, sessionDirectory) = try self.makeWriter()
+ defer { try? FileManager.default.removeItem(at: sessionDirectory) }
+
+ self.pushBuffer(writer, ptsSeconds: 0)
+ let stoppedTrack = await writer.stop()
+ let lastEnd = try XCTUnwrap(stoppedTrack.chunks.last?.presentationEnd)
+
+ let rawBoundary = await writer.beginSplice()
+ let boundary = try XCTUnwrap(rawBoundary)
+ XCTAssertEqual(boundary.seconds, lastEnd.seconds, accuracy: 1e-9)
+
+ let trackAfter = await writer.snapshot()
+ XCTAssertEqual(trackAfter.chunks.count, stoppedTrack.chunks.count)
+ }
+
+ func testPostSpliceSubHalfSecondGapDoesNotMergeIntoOldChunk() async throws {
+ let (writer, sessionDirectory) = try self.makeWriter()
+ defer { try? FileManager.default.removeItem(at: sessionDirectory) }
+
+ self.pushBuffer(writer, ptsSeconds: 0) // ends at 0.01s
+ try? await Task.sleep(nanoseconds: 50_000_000)
+ _ = await writer.beginSplice()
+
+ // Under the merge threshold, but the splice already forced rotation into a new chunk.
+ self.pushBuffer(writer, ptsSeconds: 0.21)
+ let track = await writer.stop()
+
+ XCTAssertEqual(track.chunks.count, 2)
+ XCTAssertEqual(track.chunks[1].discontinuities, [])
+ XCTAssertEqual(track.chunks[1].presentationStart.seconds, 0.21, accuracy: 1e-6)
+ }
+
+ // MARK: Writer format-mismatch guard
+
+ private func makeStereoBuffer(frameCount: Int, fill: (Int) -> Float) -> AVAudioPCMBuffer {
+ let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 48_000, channels: 2, interleaved: false)!
+ let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(frameCount))!
+ buffer.frameLength = AVAudioFrameCount(frameCount)
+ for channel in 0..<2 {
+ let data = buffer.floatChannelData![channel]
+ for index in 0..<frameCount { data[index] = fill(index) }
+ }
+ return buffer
+ }
+
+ private func pushStereoBuffer(_ writer: MeetingAudioChunkWriter, ptsSeconds: Double, frameCount: Int = 480) {
+ let buffer = self.makeStereoBuffer(frameCount: frameCount) { _ in 0.1 }
+ let pts = CMTime(seconds: ptsSeconds, preferredTimescale: 48_000)
+ guard let sampleBuffer = meetingMicrophoneSynthesizeSampleBuffer(from: buffer, presentationTime: pts) else {
+ return XCTFail("failed to synthesize a stereo sample buffer")
+ }
+ writer.enqueue(sampleBuffer)
+ }
+
+ func testFormatMismatchWithGapProducesTwoChunksZeroWriterFailures() async throws {
+ let sessionDirectory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
+ defer { try? FileManager.default.removeItem(at: sessionDirectory) }
+ let track = MeetingAudioTrack(
+ id: UUID(), kind: .microphone, sourceIdentifier: "test-mic", sourceDisplayName: "Test Microphone",
+ format: nil,
+ timebase: MeetingTimebaseMetadata(startedHostTime: 0, machTimebaseNumerator: 1, machTimebaseDenominator: 1, firstPresentationTime: nil),
+ health: .waiting, chunks: []
+ )
+ var events: [MeetingCaptureEvent] = []
+ let lock = NSLock()
+ let writer = try MeetingAudioChunkWriter(track: track, sessionDirectory: sessionDirectory, chunkDuration: 60) { event in
+ lock.withLock { events.append(event) }
+ }
+
+ self.pushBuffer(writer, ptsSeconds: 0) // mono, ends at 0.01s
+ try? await Task.sleep(nanoseconds: 50_000_000)
+ self.pushStereoBuffer(writer, ptsSeconds: 0.21) // stereo, 0.2s gap
+ let finalTrack = await writer.stop()
+
+ XCTAssertEqual(finalTrack.chunks.count, 2)
+ XCTAssertTrue(finalTrack.chunks.allSatisfy { $0.finalizationState == .finalized })
+
+ let writerFailures = lock.withLock { events }.compactMap { event -> MeetingInterruptionKind? in
+ guard case let .interrupted(kind, _, _) = event else { return nil }
+ return kind
+ }.filter { $0 == .writerFailure }
+ XCTAssertEqual(writerFailures.count, 0)
+
+ let finalizedFormats = lock.withLock { events }.compactMap { event -> MeetingAudioFormat? in
+ guard case let .chunkFinalized(_, _, format) = event else { return nil }
+ return format
+ }
+ XCTAssertEqual(finalizedFormats.count, 2)
+ XCTAssertEqual(finalizedFormats[0].channelCount, 1)
+ XCTAssertEqual(finalizedFormats[1].channelCount, 2)
+ }
+
+ // MARK: Transactional updateTrackMetadata
+
+ func testUpdateTrackMetadataPersistsSuccessfully() async throws {
+ let (writer, sessionDirectory) = try self.makeWriter()
+ defer { try? FileManager.default.removeItem(at: sessionDirectory) }
+
+ try await writer.updateTrackMetadata { track in
+ track.captureMethod = .avCaptureSession
+ }
+ let track = await writer.stop()
+ XCTAssertEqual(track.captureMethod, .avCaptureSession)
+ }
+
+ func testUpdateTrackMetadataThrowOnPersistFailureLeavesInMemoryUnchanged() async throws {
+ let (writer, sessionDirectory) = try self.makeWriter()
+ defer { try? FileManager.default.removeItem(at: sessionDirectory) }
+
+ // Remove the track directory the manifest is persisted under so the write fails.
+ let trackDirectory = sessionDirectory.appendingPathComponent("tracks", isDirectory: true)
+ .appendingPathComponent(MeetingAudioTrackKind.microphone.rawValue, isDirectory: true)
+ try FileManager.default.removeItem(at: trackDirectory)
+
+ do {
+ try await writer.updateTrackMetadata { track in
+ track.captureMethod = .avCaptureSession
+ }
+ XCTFail("expected the persist failure to propagate")
+ } catch {}
+
+ let track = await writer.snapshot()
+ XCTAssertNil(track.captureMethod, "a failed persist must leave the in-memory track untouched")
+ }
+
+ // MARK: MeetingCaptureDeviceElection
+
+ private func micIdentity(id: String, uid: String?, name: String, role: MeetingMicrophoneRole = .unknown) -> MeetingMicrophoneIdentity {
+ MeetingMicrophoneIdentity(captureDeviceID: id, coreAudioUID: uid, displayName: name, role: role)
+ }
+
+ func testElectionRoleTransfersOnlyWhenElectedDeviceEqualsOriginal() {
+ let original = self.micIdentity(id: "dev-1", uid: "uid-1", name: "MacBook Mic", role: .personal)
+ let catalog = [original]
+ let elected = MeetingCaptureDeviceElection.decide(original: original, catalog: catalog, defaultInputUID: "uid-1")
+ XCTAssertEqual(elected.identity.captureDeviceID, "dev-1")
+ XCTAssertEqual(elected.role, .personal)
+ }
+
+ func testElectionDifferentDeviceReturnsUnknown() {
+ let original = self.micIdentity(id: "dev-1", uid: "uid-1", name: "MacBook Mic", role: .personal)
+ let other = self.micIdentity(id: "dev-2", uid: "uid-2", name: "AirPods", role: .unknown)
+ let catalog = [original, other]
+ let elected = MeetingCaptureDeviceElection.decide(original: original, catalog: catalog, defaultInputUID: "uid-2")
+ XCTAssertEqual(elected.identity.captureDeviceID, "dev-2")
+ XCTAssertEqual(elected.role, .unknown)
+ }
+
+ func testElectionFallsBackToOriginalWhenNoSystemDefault() {
+ let original = self.micIdentity(id: "dev-1", uid: "uid-1", name: "MacBook Mic", role: .personal)
+ let elected = MeetingCaptureDeviceElection.decide(original: original, catalog: [original], defaultInputUID: nil)
+ XCTAssertEqual(elected.identity, original)
+ XCTAssertEqual(elected.role, .personal)
+ }
+
+ // MARK: Codable: captureEras
+
+ private func sampleEra(method: MeetingAudioTrackCaptureMethod, start: Double) -> MeetingCaptureEra {
+ MeetingCaptureEra(
+ method: method, deviceUID: "uid-1", deviceName: "MacBook Mic",
+ roleAtElection: .personal, startSeconds: start
+ )
+ }
+
+ func testMultiEraCaptureErasRoundTrip() throws {
+ var track = MeetingAudioTrack(
+ id: UUID(), kind: .microphone, sourceIdentifier: "mic", sourceDisplayName: "Mic",
+ format: nil,
+ timebase: MeetingTimebaseMetadata(startedHostTime: 0, machTimebaseNumerator: 1, machTimebaseDenominator: 1, firstPresentationTime: nil),
+ health: .waiting, chunks: []
+ )
+ track.captureMethod = .avCaptureSession
+ track.captureEras = [
+ self.sampleEra(method: .voiceProcessing, start: 0),
+ self.sampleEra(method: .avCaptureSession, start: 120),
+ ]
+ let encoder = JSONEncoder()
+ let data = try encoder.encode(track)
+ let decoded = try JSONDecoder().decode(MeetingAudioTrack.self, from: data)
+ XCTAssertEqual(decoded.captureEras?.count, 2)
+ XCTAssertEqual(decoded.captureEras?[0].method, .voiceProcessing)
+ XCTAssertEqual(decoded.captureEras?[1].method, .avCaptureSession)
+ XCTAssertEqual(decoded.captureEras?[1].startSeconds, 120)
+ }
+
+ func testLegacyJSONWithoutCaptureErasDecodesNil() throws {
+ let json = """
+ {
+ "id": "\(UUID().uuidString)",
+ "kind": "microphone",
+ "sourceIdentifier": "mic",
+ "sourceDisplayName": "Mic",
+ "timebase": {"startedHostTime": 0, "machTimebaseNumerator": 1, "machTimebaseDenominator": 1},
+ "health": {"status": "waiting", "level": 0, "droppedSampleCount": 0},
+ "chunks": []
+ }
+ """
+ let decoded = try JSONDecoder().decode(MeetingAudioTrack.self, from: Data(json.utf8))
+ XCTAssertNil(decoded.captureEras)
+ XCTAssertNil(decoded.captureMethod)
+ }
+
+ /// A single malformed era (unknown `method`) is skipped, not fatal to the whole track decode.
+ func testUnknownEraMethodRawValueIsSkippedNotFatal() throws {
+ let json = """
+ {
+ "id": "\(UUID().uuidString)",
+ "kind": "microphone",
+ "sourceIdentifier": "mic",
+ "sourceDisplayName": "Mic",
+ "timebase": {"startedHostTime": 0, "machTimebaseNumerator": 1, "machTimebaseDenominator": 1},
+ "health": {"status": "waiting", "level": 0, "droppedSampleCount": 0},
+ "chunks": [],
+ "captureEras": [
+ {"method": "bluetoothHFP", "roleAtElection": "unknown", "startSeconds": 0},
+ {"method": "avCaptureSession", "roleAtElection": "unknown", "startSeconds": 30}
+ ]
+ }
+ """
+ let decoded = try JSONDecoder().decode(MeetingAudioTrack.self, from: Data(json.utf8))
+ XCTAssertEqual(decoded.captureEras?.count, 1, "the malformed era is skipped, not fatal")
+ XCTAssertEqual(decoded.captureEras?.first?.method, .avCaptureSession)
+ }
+
+ func testMultiEraTrackCaptureMethodIsAvCaptureSession() {
+ // Multi-era tracks write the conservative track-level captureMethod for legacy readers.
+ var track = MeetingAudioTrack(
+ id: UUID(), kind: .microphone, sourceIdentifier: "mic", sourceDisplayName: "Mic",
+ format: nil,
+ timebase: MeetingTimebaseMetadata(startedHostTime: 0, machTimebaseNumerator: 1, machTimebaseDenominator: 1, firstPresentationTime: nil),
+ health: .waiting, chunks: []
+ )
+ track.captureEras = [
+ self.sampleEra(method: .voiceProcessing, start: 0),
+ self.sampleEra(method: .avCaptureSession, start: 90),
+ ]
+ track.captureMethod = .avCaptureSession
+ XCTAssertEqual(track.captureMethod, .avCaptureSession)
+ XCTAssertEqual(track.captureEras?.first?.method, .voiceProcessing)
+ }
+
+ // MARK: Gate re-use per era
+
+ func testFreshGatePerEraPassesBufferingFlushPassthroughWhilePreviousEraGateStaysTerminal() throws {
+ let firstEraGate = MeetingVoiceProcessingCommitGate()
+ let buffer = self.makeMonoBuffer(frameCount: 480) { _ in 0.1 }
+ let sample = try XCTUnwrap(meetingMicrophoneSynthesizeSampleBuffer(
+ from: buffer, presentationTime: CMTime(value: 0, timescale: 48_000)
+ ))
+
+ XCTAssertEqual(firstEraGate.offer(sample), .buffered)
+ let flushed = try XCTUnwrap(firstEraGate.beginCommitFlush())
+ XCTAssertEqual(flushed.count, 1)
+ XCTAssertNil(firstEraGate.continueCommitFlush())
+ XCTAssertTrue(firstEraGate.isCommitted)
+ XCTAssertEqual(firstEraGate.offer(sample), .passthrough)
+
+ let secondEraGate = MeetingVoiceProcessingCommitGate()
+ XCTAssertEqual(secondEraGate.offer(sample), .buffered)
+ XCTAssertFalse(secondEraGate.isCommitted)
+ _ = secondEraGate.beginCommitFlush()
+ XCTAssertNil(secondEraGate.continueCommitFlush())
+ XCTAssertTrue(secondEraGate.isCommitted)
+
+ XCTAssertTrue(firstEraGate.isCommitted, "unaffected by the second era's gate lifecycle")
+ }
+
+ // MARK: - MeetingSupervisedTimeout wall-clock bound
+
+ /// A non-cooperative 3s sleep must not block `run` past the supervision deadline.
+ func testSupervisedTimeoutReturnsNilByDeadlineEvenWhenTheOperationIsNonCooperative() async {
+ let start = Date()
+ let result = await MeetingSupervisedTimeout.run(seconds: 0.2) { () -> Bool in
+ try? await Task.sleep(nanoseconds: 3_000_000_000)
+ return true
+ }
+ XCTAssertNil(result)
+ XCTAssertLessThan(Date().timeIntervalSince(start), 0.5)
+ }
+
+ // MARK: - Upgrade ring-discard boundary
+
+ func testRingDiscardDropsSamplesAtOrBehindBoundaryAndKeepsSamplesAfterIt() throws {
+ let buffer = self.makeMonoBuffer(frameCount: 480) { _ in 0.1 }
+ let before = try XCTUnwrap(meetingMicrophoneSynthesizeSampleBuffer(
+ from: buffer, presentationTime: CMTime(value: 0, timescale: 48_000)
+ ))
+ let atBoundary = try XCTUnwrap(meetingMicrophoneSynthesizeSampleBuffer(
+ from: buffer, presentationTime: CMTime(value: 48_000, timescale: 48_000)
+ ))
+ let after = try XCTUnwrap(meetingMicrophoneSynthesizeSampleBuffer(
+ from: buffer, presentationTime: CMTime(value: 48_001, timescale: 48_000)
+ ))
+ let boundary = CMTime(value: 48_000, timescale: 48_000)
+
+ let kept = MeetingUpgradeRingDiscard.discardingSamples(atOrBehind: boundary, from: [before, atBoundary, after])
+ XCTAssertEqual(kept.count, 1, "only the strictly-after sample survives")
+
+ let unfiltered = MeetingUpgradeRingDiscard.discardingSamples(atOrBehind: nil, from: [before, atBoundary, after])
+ XCTAssertEqual(unfiltered.count, 3, "nil boundary means the writer produced no splice — keep everything")
+ }
+
+ // MARK: - Not covered here: phase-machine transitions (file-private, hardware-driven —
+ // needs the Phase1 probe) and writer finalizationSlots saturation (no test seam exposed).
+ func testPreselectionBuiltInMicDefaultsToPersonalRole() {
+ let builtIn = MeetingMicrophoneIdentity(captureDeviceID: "built-in", coreAudioUID: "BuiltInMicrophoneDevice", displayName: "MacBook Pro Microphone")
+ let selection = MeetingMicrophonePreselection.select(
+ identities: [builtIn], savedDeviceID: "other-device", savedRole: .personal,
+ systemDefaultUID: "BuiltInMicrophoneDevice", preferredInputUID: nil, systemDefaultCaptureID: nil
+ )
+ XCTAssertEqual(selection.deviceID, "built-in")
+ XCTAssertEqual(selection.role, .personal)
+ }
+
+ func testElectionBuiltInMicDefaultsToPersonalRole() {
+ let original = MeetingMicrophoneIdentity(captureDeviceID: "airpods", coreAudioUID: "EC:input", displayName: "AirPods", role: .personal)
+ let builtIn = MeetingMicrophoneIdentity(captureDeviceID: "built-in", coreAudioUID: "BuiltInMicrophoneDevice", displayName: "MacBook Pro Microphone")
+ let elected = MeetingCaptureDeviceElection.decide(original: original, catalog: [builtIn], defaultInputUID: "BuiltInMicrophoneDevice")
+ XCTAssertEqual(elected.role, .personal)
+ }
+
+}
diff --git a/Tests/FluidDictationIntegrationTests/MeetingPhase4Tests.swift b/Tests/FluidDictationIntegrationTests/MeetingPhase4Tests.swift
index 3b2dd2d4..4c06263e 100644
--- a/Tests/FluidDictationIntegrationTests/MeetingPhase4Tests.swift
+++ b/Tests/FluidDictationIntegrationTests/MeetingPhase4Tests.swift
@@ -23,6 +23,13 @@ final class MeetingPhase4ClassificationTests: XCTestCase {
)
}
+ private func singleEra(_ method: MeetingAudioTrackCaptureMethod?) -> [MeetingCaptureEra] {
+ [MeetingCaptureEra(
+ method: method ?? .avCaptureSession, deviceUID: nil, deviceName: nil,
+ roleAtElection: .unknown, startSeconds: 0
+ )]
+ }
+
private func turn(
clusterID: SessionSpeakerID = UUID(),
start: TimeInterval,
@@ -50,7 +57,7 @@ final class MeetingPhase4ClassificationTests: XCTestCase {
let segments = [self.segment(trackID: appTrackID, start: 0, end: 3, text: echoedText)]
let (classified, widened) = MeetingProcessingPipeline.classifyMicrophoneTurns(
- turns, captureMethod: .voiceProcessing, applicationTrackID: appTrackID,
+ turns, eras: self.singleEra(.voiceProcessing), applicationTrackID: appTrackID,
segments: segments, fallbackSpeakerID: nil
)
@@ -67,7 +74,7 @@ final class MeetingPhase4ClassificationTests: XCTestCase {
var vpioTurn = self.turn(start: 0, end: 3, text: "some text", overlapsRemote: false)
vpioTurn.signalVerdict = .echo
let (classified, _) = MeetingProcessingPipeline.classifyMicrophoneTurns(
- [vpioTurn], captureMethod: .voiceProcessing, applicationTrackID: nil, segments: [], fallbackSpeakerID: nil
+ [vpioTurn], eras: self.singleEra(.voiceProcessing), applicationTrackID: nil, segments: [], fallbackSpeakerID: nil
)
XCTAssertTrue(classified[0].echoScored)
XCTAssertTrue(classified[0].effectiveEcho, "signal veto is untouched by Phase 4 — echoScored never feeds effectiveEcho")
@@ -78,7 +85,7 @@ final class MeetingPhase4ClassificationTests: XCTestCase {
vpioTurn.signalVerdict = .containsLocalSpeech
vpioTurn.isLikelyEcho = true
let (classified, _) = MeetingProcessingPipeline.classifyMicrophoneTurns(
- [vpioTurn], captureMethod: .voiceProcessing, applicationTrackID: nil, segments: [], fallbackSpeakerID: nil
+ [vpioTurn], eras: self.singleEra(.voiceProcessing), applicationTrackID: nil, segments: [], fallbackSpeakerID: nil
)
XCTAssertFalse(classified[0].effectiveEcho, "the rescue rule is untouched: containsLocalSpeech always wins")
}
@@ -133,7 +140,7 @@ final class MeetingPhase4ClassificationTests: XCTestCase {
for captureMethod in [MeetingAudioTrackCaptureMethod?.none, .screenCaptureKit, .avCaptureSession] {
let (classified, widened) = MeetingProcessingPipeline.classifyMicrophoneTurns(
- turns, captureMethod: captureMethod, applicationTrackID: appTrackID,
+ turns, eras: self.singleEra(captureMethod), applicationTrackID: appTrackID,
segments: fallbackSegments, fallbackSpeakerID: fallbackSpeaker
)
XCTAssertTrue(widened.isEmpty, "non-VPIO never widens")
@@ -180,7 +187,7 @@ final class MeetingPhase4ClassificationTests: XCTestCase {
let turns = [self.turn(start: 0, end: 5, text: "local speech", overlapsRemote: false)]
let (classified, widened) = MeetingProcessingPipeline.classifyMicrophoneTurns(
- turns, captureMethod: track.captureMethod, applicationTrackID: nil, segments: [], fallbackSpeakerID: nil
+ turns, eras: self.singleEra(track.captureMethod), applicationTrackID: nil, segments: [], fallbackSpeakerID: nil
)
XCTAssertTrue(classified[0].echoScored, "the reconciled (decoded) track's captureMethod, not an in-memory one, drives widening")
XCTAssertEqual(widened, [0])
diff --git a/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingCaptureTests.swift b/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingCaptureTests.swift
index 5e2215a4..3851b5d5 100644
--- a/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingCaptureTests.swift
+++ b/Tests/FluidDictationIntegrationTests/MeetingVoiceProcessingCaptureTests.swift
@@ -10,7 +10,7 @@ import XCTest
/// screen-recording/microphone permission and are not exercised here. This covers the pure
/// decision, gate, drift-eligibility, de-drift transform and Codable pieces of Phase 3.
final class MeetingVoiceProcessingCaptureTests: XCTestCase {
- // MARK: - Decision table (plan §3)
+ // MARK: - Decision table (incl. role)
private func mic(uid: String? = "mic-uid", role: MeetingMicrophoneRole = .personal) -> MeetingMicrophoneIdentity {
MeetingMicrophoneIdentity(captureDeviceID: "dev-1", coreAudioUID: uid, displayName: "Mic", role: role)
@@ -64,8 +64,6 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
XCTAssertTrue(reason.contains("headphones"))
}
- // MARK: - Decision table: role (Phase 4 plan §1)
-
func testDecisionSucceedsForPersonalRole() {
let decision = MeetingCapturePathDecider.decide(
mode: .onlineCall, microphone: self.mic(role: .personal), outputRoute: self.viableRoute()
@@ -89,7 +87,7 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
XCTAssertTrue(reason.contains("personal"))
}
- // MARK: - Commit gate (plan §4, §7)
+ // MARK: - Commit gate
private func makeSampleBuffer(ptsSeconds: Double = 0) -> CMSampleBuffer {
let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 48_000, channels: 1, interleaved: false)!
@@ -167,7 +165,7 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
XCTAssertNil(gate.beginCommitFlush())
}
- // MARK: - Watchdog predicate (plan §5, §7)
+ // MARK: - Watchdog predicate
func testWatchdogDoesNotTripBeforeThreshold() {
XCTAssertFalse(MeetingVoiceProcessingWatchdog.isStalled(secondsSinceLastEmittedBuffer: 9.9))
@@ -178,7 +176,7 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
XCTAssertTrue(MeetingVoiceProcessingWatchdog.isStalled(secondsSinceLastEmittedBuffer: 30))
}
- // MARK: - ScreenCaptureKit audio-settings plumbing (rebuild regression, plan §7)
+ // MARK: - ScreenCaptureKit audio-settings plumbing (rebuild regression)
func testIncludeMicrophoneFalseAttachesNoMicrophoneDevice() {
let configuration = SCStreamConfiguration()
@@ -194,7 +192,7 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
XCTAssertEqual(configuration.microphoneCaptureDeviceID, "dev-1")
}
- // MARK: - Eligibility (plan §6, §7)
+ // MARK: - Eligibility
private func makeStats(
resyncCount: Int = 0,
@@ -235,7 +233,7 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
XCTAssertEqual(MeetingVoiceProcessingDriftSnapshot.elapsedValidHostSeconds(MeetingMicrophoneCaptureStats.Snapshot()), 0)
}
- // MARK: - End-to-end de-drift sign (plan §7, resolves sol#1)
+ // MARK: - End-to-end de-drift sign
private func makeTrack(chunkStart: Double, drift: MeetingClockDriftRecord?, captureMethod: MeetingAudioTrackCaptureMethod?) -> MeetingAudioTrack {
var track = MeetingAudioTrack(
@@ -258,13 +256,25 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
return track
}
+ /// Era anchored at origin-relative 0, mirroring a single-era track starting at the session origin.
+ private func makeEra(drift: MeetingClockDriftRecord?, captureMethod: MeetingAudioTrackCaptureMethod?) -> MeetingCaptureEra {
+ MeetingCaptureEra(
+ method: captureMethod ?? .avCaptureSession,
+ deviceUID: nil,
+ deviceName: nil,
+ roleAtElection: .unknown,
+ startSeconds: 0,
+ clockDrift: drift
+ )
+ }
+
/// Drives the REAL clock fast-mic (host elapses SLOWER than frame count — the mirror of
/// `testSlowDriftNeverFiresTheStepDetector`), then asserts the transform compresses the timeline.
func testDeDriftCorrectsAFastMicAndTheInverseFormulaFails() {
var clock = MeetingMicrophonePTSClock(timebase: mach_timebase_info_data_t(numer: 1, denom: 1))
let ppm = 6.7e-6
let windowSeconds = 0.1
- let windows = 9_500 // 950s: past the ~12.5min materiality crossover at 6.7ppm (plan note)
+ let windows = 9_500 // 950s: past the ~12.5min materiality crossover at 6.7ppm
for i in 0..<windows {
let hostSeconds = Double(i) * windowSeconds * (1 - ppm) // fast mic: less host time per window
_ = clock.stamp(hostTime: UInt64(hostSeconds * 1_000_000_000), frameCount: 4_800)
@@ -275,10 +285,10 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
XCTAssertGreaterThan(abs(cumulative), MeetingMicrophoneDeDrift.materialityThresholdSeconds)
let drift = MeetingClockDriftRecord(cumulativeAbsorbedSeconds: cumulative, elapsedValidHostSeconds: elapsed, eligible: true)
- let track = self.makeTrack(chunkStart: 0, drift: drift, captureMethod: .voiceProcessing)
+ let era = self.makeEra(drift: drift, captureMethod: .voiceProcessing)
let rawEnd = Double(windows) * windowSeconds
- let corrected = MeetingMicrophoneDeDrift.correct(rawEnd, track: track, origin: 0)
+ let corrected = MeetingMicrophoneDeDrift.correct(rawEnd, era: era, eraEndRelative: nil)
XCTAssertLessThan(corrected, rawEnd, "correction must compress the fast timeline toward the reference, not expand it")
XCTAssertEqual(corrected, rawEnd + cumulative, accuracy: 0.05, "k = 1 + cumulative/elapsed applied against the reference")
@@ -290,24 +300,24 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
func testDeDriftIsANoOpWhenIneligible() {
let drift = MeetingClockDriftRecord(cumulativeAbsorbedSeconds: -0.05, elapsedValidHostSeconds: 300, eligible: false)
- let track = self.makeTrack(chunkStart: 0, drift: drift, captureMethod: .voiceProcessing)
- XCTAssertEqual(MeetingMicrophoneDeDrift.correct(120, track: track, origin: 0), 120)
+ let era = self.makeEra(drift: drift, captureMethod: .voiceProcessing)
+ XCTAssertEqual(MeetingMicrophoneDeDrift.correct(120, era: era, eraEndRelative: nil), 120)
}
func testDeDriftIsANoOpForNonVoiceProcessingTracks() {
let drift = MeetingClockDriftRecord(cumulativeAbsorbedSeconds: -0.05, elapsedValidHostSeconds: 300, eligible: true)
- let track = self.makeTrack(chunkStart: 0, drift: drift, captureMethod: .screenCaptureKit)
- XCTAssertEqual(MeetingMicrophoneDeDrift.correct(120, track: track, origin: 0), 120)
+ let era = self.makeEra(drift: drift, captureMethod: .screenCaptureKit)
+ XCTAssertEqual(MeetingMicrophoneDeDrift.correct(120, era: era, eraEndRelative: nil), 120)
}
func testDeDriftIsANoOpBelowMaterialityThreshold() {
// 1ms cumulative over 300s is below the 5ms materiality gate.
let drift = MeetingClockDriftRecord(cumulativeAbsorbedSeconds: -0.001, elapsedValidHostSeconds: 300, eligible: true)
- let track = self.makeTrack(chunkStart: 0, drift: drift, captureMethod: .voiceProcessing)
- XCTAssertEqual(MeetingMicrophoneDeDrift.correct(120, track: track, origin: 0), 120)
+ let era = self.makeEra(drift: drift, captureMethod: .voiceProcessing)
+ XCTAssertEqual(MeetingMicrophoneDeDrift.correct(120, era: era, eraEndRelative: nil), 120)
}
- // MARK: - Ring flush pacing (plan §4/§7, opus N2)
+ // MARK: - Ring flush pacing
/// flushBatch's pacing (4 samples / 5ms) against a real writer: a full ~5s ring must not trip backpressure.
func testPacedRingFlushProducesNoWriterBackpressure() async throws {
@@ -337,7 +347,7 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
XCTAssertEqual(backpressureCount.withLock { $0 }, 0)
}
- // MARK: - Provenance promotion (plan §2, §7)
+ // MARK: - Provenance promotion
func testProvenanceIsPessimisticThenPromotedOnCommit() async throws {
let sessionDirectory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
@@ -361,7 +371,7 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
_ = await writer.stop()
}
- // MARK: - Codable tolerance (plan §7, kimi#7)
+ // MARK: - Codable tolerance
func testUnknownCaptureMethodRawValueDecodesToNil() throws {
var track = self.makeTrack(chunkStart: 0, drift: nil, captureMethod: .voiceProcessing)
@@ -409,4 +419,92 @@ final class MeetingVoiceProcessingCaptureTests: XCTestCase {
XCTAssertEqual(decoded.captureMethod, .voiceProcessing)
XCTAssertEqual(decoded.clockDrift, drift)
}
+
+ // MARK: - Sheet default mic preselection
+
+ private func identity(_ deviceID: String, uid: String?, role: MeetingMicrophoneRole = .unknown) -> MeetingMicrophoneIdentity {
+ MeetingMicrophoneIdentity(captureDeviceID: deviceID, coreAudioUID: uid, displayName: deviceID, role: role)
+ }
+
+ func testPreselectionPrefersSystemDefaultOverSavedDevice() {
+ let identities = [
+ self.identity("saved", uid: "saved-uid", role: .personal),
+ self.identity("default", uid: "default-uid"),
+ ]
+ let selection = MeetingMicrophonePreselection.select(
+ identities: identities,
+ savedDeviceID: "saved",
+ savedRole: .personal,
+ systemDefaultUID: "default-uid",
+ preferredInputUID: nil,
+ systemDefaultCaptureID: nil
+ )
+ XCTAssertEqual(selection.deviceID, "default")
+ XCTAssertEqual(selection.role, .unknown)
+ }
+
+ func testPreselectionReusesSavedRoleOnlyWhenDeviceMatchesSaved() {
+ let identities = [self.identity("saved", uid: "saved-uid", role: .personal)]
+ let selection = MeetingMicrophonePreselection.select(
+ identities: identities,
+ savedDeviceID: "saved",
+ savedRole: .personal,
+ systemDefaultUID: "saved-uid",
+ preferredInputUID: nil,
+ systemDefaultCaptureID: nil
+ )
+ XCTAssertEqual(selection.deviceID, "saved")
+ XCTAssertEqual(selection.role, .personal)
+ }
+
+ /// System default → preferred input UID → `AVCaptureDevice.default` unique ID → first identity.
+ func testPreselectionFallsBackThroughPreferredInputThenCaptureDeviceIDThenFirstIdentity() {
+ let identities = [
+ self.identity("a", uid: "a-uid"),
+ self.identity("b", uid: "b-uid"),
+ self.identity("c", uid: nil),
+ ]
+ let byPreferredInput = MeetingMicrophonePreselection.select(
+ identities: identities,
+ savedDeviceID: nil,
+ savedRole: .unknown,
+ systemDefaultUID: "no-match-uid",
+ preferredInputUID: "b-uid",
+ systemDefaultCaptureID: "a"
+ )
+ XCTAssertEqual(byPreferredInput.deviceID, "b", "preferred input UID must win over the AVCaptureDevice.default fallback")
+
+ let byCaptureID = MeetingMicrophonePreselection.select(
+ identities: identities,
+ savedDeviceID: nil,
+ savedRole: .unknown,
+ systemDefaultUID: "no-match-uid",
+ preferredInputUID: "no-match-uid",
+ systemDefaultCaptureID: "a"
+ )
+ XCTAssertEqual(byCaptureID.deviceID, "a")
+
+ let byFirst = MeetingMicrophonePreselection.select(
+ identities: identities,
+ savedDeviceID: nil,
+ savedRole: .unknown,
+ systemDefaultUID: "no-match-uid",
+ preferredInputUID: "no-match-uid",
+ systemDefaultCaptureID: "no-match-id"
+ )
+ XCTAssertEqual(byFirst.deviceID, "a")
+ }
+
+ func testPreselectionWithEmptyIdentitiesReturnsNil() {
+ let selection = MeetingMicrophonePreselection.select(
+ identities: [],
+ savedDeviceID: "saved",
+ savedRole: .personal,
+ systemDefaultUID: "default-uid",
+ preferredInputUID: "preferred-uid",
+ systemDefaultCaptureID: "default-id"
+ )
+ XCTAssertNil(selection.deviceID)
+ XCTAssertEqual(selection.role, .unknown)
+ }
}