diff --git a/Sources/verbemo/Meetings/AutoRecordController.swift b/Sources/verbemo/Meetings/AutoRecordController.swift index 178e1b9..fa85bf0 100644 --- a/Sources/verbemo/Meetings/AutoRecordController.swift +++ b/Sources/verbemo/Meetings/AutoRecordController.swift @@ -719,6 +719,7 @@ final class AutoRecordController { /// touched the microphone for `stopDelay`, `silence` means neither track /// made a sound for `silenceStop`, and `calendar-event-ended` means the /// far end was quiet for `calendarEndQuiet` after the event was over. + /// A confirmed call exit stops without a silence timer. /// /// `nil` for every other reason, because the others say nothing about /// whether a meeting happened: "app-quit" and "max-duration" mean we @@ -730,6 +731,7 @@ final class AutoRecordController { ) -> TimeInterval? { switch reason { case "call-ended": return settings.stopDelay + case "call-ended-confirmed": return 0 case "silence": return settings.silenceStop case "calendar-event-ended": return calendarEndQuiet default: return nil diff --git a/Sources/verbemo/RecordingSession.swift b/Sources/verbemo/RecordingSession.swift index b2c565a..993c5d1 100644 --- a/Sources/verbemo/RecordingSession.swift +++ b/Sources/verbemo/RecordingSession.swift @@ -104,7 +104,13 @@ final class RecordingSession { } } guard let original = integrationCall else { return } - guard let current = observations.first(where: { $0.integrationID == original.integrationID }), + let current = observations.first { $0.integrationID == original.integrationID } + if let current, current.participation == .idle, (0...5).contains(now.timeIntervalSince(current.observedAt)), + trigger != .manual, participantRecorder != nil { + endDirectCall() + return + } + guard let current, current.participation == .inCall, original.call == nil || original.call?.id == current.call?.id || (original.source != current.source && original.call?.url != nil && original.call?.url == current.call?.url) else { @@ -375,18 +381,29 @@ final class RecordingSession { guard !recordingStopped else { return } closeDirectPeriod() await audioCapture?.stop() - guard !recordingStopped else { return } - captureSource = .ended + endDirectCall() + } + + private func endDirectCall() { + guard !recordingStopped, captureSource != .ended else { return } + closeDirectPeriod() + sourceError = nil writeManifest() + captureSource = .ended } private func startFallback(after error: any Error, scope: String, permission: @MainActor @Sendable () -> Bool) async { - guard !recordingStopped, captureSource != .fallback, captureSource != .failed else { return } + guard !recordingStopped, captureSource != .ended, captureSource != .fallback, captureSource != .failed else { return } closeDirectPeriod() sourceError = String(describing: error) captureSource = .preparing await audioCapture?.stop() - guard !recordingStopped, !Task.isCancelled else { return } + if !recordingStopped, captureSource != .ended, trigger != .manual, participantRecorder != nil, integrationCall != nil { + // The room can close the audio socket before the independent call + // monitor sees the exit. Give its next polls time to confirm it. + try? await Task.sleep(for: .seconds(2)) + } + guard !recordingStopped, captureSource != .ended, !Task.isCancelled else { return } do { guard permission() else { throw StartFailure.microphoneAccess diff --git a/Sources/verbemo/Verbemo.swift b/Sources/verbemo/Verbemo.swift index fd55911..98a6805 100644 --- a/Sources/verbemo/Verbemo.swift +++ b/Sources/verbemo/Verbemo.swift @@ -1121,6 +1121,8 @@ final class AppController { } else if source == .direct, session.trigger != .manual { notifyUser(title: localised("verbemo — recording started", "verbemo — запись началась"), body: session.title ?? session.dir.lastPathComponent, opening: session.dir) + } else if source == .ended, session.trigger != .manual { + stopSession(reason: "call-ended-confirmed") } else if source == .failed { notifyUser(title: localised("Recording stopped: audio unavailable", "Запись остановлена: звук недоступен"), body: session.sourceError ?? localised("Check audio access in Settings.", "Проверьте доступ к звуку в настройках."), diff --git a/Tests/verbemoTests/AutoRecordTests.swift b/Tests/verbemoTests/AutoRecordTests.swift index b172913..69fa3c1 100644 --- a/Tests/verbemoTests/AutoRecordTests.swift +++ b/Tests/verbemoTests/AutoRecordTests.swift @@ -38,6 +38,14 @@ struct AutoRecordTests { trigger: .micActivity, reason: "call-ended", duration: 45 * 60, settings: settings)) } + @Test func confirmedCallExitUsesActualDuration() { + let minimum = settings.minDuration + #expect(!AutoRecordController.shouldDiscard( + trigger: .micActivity, reason: "call-ended-confirmed", duration: minimum, settings: settings)) + #expect(AutoRecordController.shouldDiscard( + trigger: .micActivity, reason: "call-ended-confirmed", duration: minimum - 1, settings: settings)) + } + /// If you pressed the button, nine seconds of it were nine seconds you /// meant. The guard is on the trigger, not on the reason, so it holds even /// when a manual recording happens to be stopped by one of the auto rules. diff --git a/Tests/verbemoTests/ParticipantCaptureLifecycleTests.swift b/Tests/verbemoTests/ParticipantCaptureLifecycleTests.swift index e68edc7..4f78e7a 100644 --- a/Tests/verbemoTests/ParticipantCaptureLifecycleTests.swift +++ b/Tests/verbemoTests/ParticipantCaptureLifecycleTests.swift @@ -108,8 +108,8 @@ struct ParticipantCaptureLifecycleTests { #expect(participants) } - private func wait(_ condition: () -> Bool) async throws { - for _ in 0..<200 { + private func wait(attempts: Int = 200, _ condition: () -> Bool) async throws { + for _ in 0..