Improved the Capturing protocol in the Recording package target to capture interruptions and pause any active capture process.
This commit is contained in:
@@ -6,6 +6,13 @@ import Foundation
|
|||||||
/// state machine — for example, with a real microphone backend in the app, or with a mock in unit tests.
|
/// state machine — for example, with a real microphone backend in the app, or with a mock in unit tests.
|
||||||
public protocol Capturing: Sendable {
|
public protocol Capturing: Sendable {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
/// The stream of events the service emits outside its method calls.
|
||||||
|
///
|
||||||
|
/// Defaults to an empty stream that finishes immediately, for services that emit no events.
|
||||||
|
var events: AsyncStream<CapturingEvent> { get }
|
||||||
|
|
||||||
// MARK: Methods
|
// MARK: Methods
|
||||||
|
|
||||||
/// Starts a new audio recording from the microphone.
|
/// Starts a new audio recording from the microphone.
|
||||||
@@ -23,3 +30,24 @@ public protocol Capturing: Sendable {
|
|||||||
func stop() async throws -> URL
|
func stop() async throws -> URL
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Implementations
|
||||||
|
|
||||||
|
public extension Capturing {
|
||||||
|
|
||||||
|
/// The empty stream of events, which finishes immediately, for services that emit no events.
|
||||||
|
var events: AsyncStream<CapturingEvent> {
|
||||||
|
AsyncStream { continuation in
|
||||||
|
continuation.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Events
|
||||||
|
|
||||||
|
/// An event emitted by a ``Capturing`` service outside its method calls.
|
||||||
|
public enum CapturingEvent: Sendable {
|
||||||
|
/// The system interrupted the ongoing capture, pausing it.
|
||||||
|
case interrupted
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,24 +4,66 @@ import AVFoundation
|
|||||||
///
|
///
|
||||||
/// The service records into a temporary `.m4a` file through an `AVAudioRecorder`, and returns the file's location when the recording stops.
|
/// The service records into a temporary `.m4a` file through an `AVAudioRecorder`, and returns the file's location when the recording stops.
|
||||||
/// The user's permission to record is requested before a recording starts and, on the platforms that require it, the shared audio session is
|
/// The user's permission to record is requested before a recording starts and, on the platforms that require it, the shared audio session is
|
||||||
/// configured for recording while the capture is in progress.
|
/// configured for recording while the capture is in progress. When the system interrupts the session while a recording is in progress —
|
||||||
|
/// because of a phone call or Siri, for example — the service emits ``CapturingEvent/interrupted`` through ``events``.
|
||||||
@MainActor
|
@MainActor
|
||||||
public final class AudioCapturing: Capturing {
|
public final class AudioCapturing: Capturing {
|
||||||
|
|
||||||
// MARK: Properties
|
// MARK: Properties
|
||||||
|
|
||||||
|
/// The stream of events the service emits outside its method calls.
|
||||||
|
public let events: AsyncStream<CapturingEvent>
|
||||||
|
|
||||||
|
/// The continuation that feeds ``events``.
|
||||||
|
private let continuation: AsyncStream<CapturingEvent>.Continuation
|
||||||
|
|
||||||
/// The recorder that captures the audio from the microphone into a temporary file.
|
/// The recorder that captures the audio from the microphone into a temporary file.
|
||||||
private var recorder: AVAudioRecorder?
|
private var recorder: AVAudioRecorder?
|
||||||
|
|
||||||
|
#if os(iOS) || os(visionOS)
|
||||||
|
/// The task that observes the interruptions of the audio session while the service lives.
|
||||||
|
private var taskInterruptions: Task<Void, Never>?
|
||||||
|
#endif
|
||||||
|
|
||||||
// MARK: Initializers
|
// MARK: Initializers
|
||||||
|
|
||||||
/// Creates an audio recording service.
|
/// Creates an audio recording service.
|
||||||
public init() {}
|
public init() {
|
||||||
|
(events, continuation) = AsyncStream.makeStream(of: CapturingEvent.self)
|
||||||
|
|
||||||
|
#if os(iOS) || os(visionOS)
|
||||||
|
taskInterruptions = Task { [weak self] in
|
||||||
|
let notifications = NotificationCenter.default.notifications(
|
||||||
|
named: AVAudioSession.interruptionNotification,
|
||||||
|
object: AVAudioSession.sharedInstance()
|
||||||
|
)
|
||||||
|
|
||||||
|
for await notification in notifications {
|
||||||
|
guard let self else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
self.handleInterruption(notification)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
#if os(iOS) || os(visionOS)
|
||||||
|
taskInterruptions?.cancel()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
continuation.finish()
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: Methods
|
// MARK: Methods
|
||||||
|
|
||||||
/// Starts a new audio recording from the microphone.
|
/// Starts a new audio recording from the microphone.
|
||||||
///
|
///
|
||||||
|
/// On the platforms that require an audio session, the session is deactivated again when the recorder fails to start after
|
||||||
|
/// the session was activated.
|
||||||
|
///
|
||||||
/// - Throws: ``AudioCapturingError/permissionNotGranted`` when the user denies the app access to the microphone,
|
/// - Throws: ``AudioCapturingError/permissionNotGranted`` when the user denies the app access to the microphone,
|
||||||
/// ``AudioCapturingError/captureNotStarted`` when the recorder fails to start, or any error thrown while configuring
|
/// ``AudioCapturingError/captureNotStarted`` when the recorder fails to start, or any error thrown while configuring
|
||||||
/// the audio session or creating the recorder.
|
/// the audio session or creating the recorder.
|
||||||
@@ -37,16 +79,24 @@ public final class AudioCapturing: Capturing {
|
|||||||
try session.setActive(true)
|
try session.setActive(true)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
let recorder = try AVAudioRecorder(
|
do {
|
||||||
url: Constant.File.url,
|
let recorder = try AVAudioRecorder(
|
||||||
settings: Constant.Audio.settings
|
url: Constant.File.url,
|
||||||
)
|
settings: Constant.Audio.settings
|
||||||
|
)
|
||||||
|
|
||||||
guard recorder.record() else {
|
guard recorder.record() else {
|
||||||
throw AudioCapturingError.captureNotStarted
|
throw AudioCapturingError.captureNotStarted
|
||||||
|
}
|
||||||
|
|
||||||
|
self.recorder = recorder
|
||||||
|
} catch {
|
||||||
|
#if os(iOS) || os(visionOS)
|
||||||
|
try? AVAudioSession.sharedInstance().setActive(false)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
self.recorder = recorder
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pauses the ongoing recording.
|
/// Pauses the ongoing recording.
|
||||||
@@ -95,6 +145,33 @@ public final class AudioCapturing: Capturing {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if os(iOS) || os(visionOS)
|
||||||
|
// MARK: - Helpers
|
||||||
|
|
||||||
|
private extension AudioCapturing {
|
||||||
|
|
||||||
|
/// Handles an interruption notification of the audio session, emitting ``CapturingEvent/interrupted`` through ``events``
|
||||||
|
/// when the system began interrupting an ongoing recording. The end of an interruption is deliberately ignored: the capture is
|
||||||
|
/// never resumed without the user asking for it.
|
||||||
|
///
|
||||||
|
/// - Parameter notification: The interruption notification posted by the audio session.
|
||||||
|
func handleInterruption(
|
||||||
|
_ notification: Notification
|
||||||
|
) {
|
||||||
|
guard
|
||||||
|
recorder != nil,
|
||||||
|
let typeValue = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt,
|
||||||
|
AVAudioSession.InterruptionType(rawValue: typeValue) == .began
|
||||||
|
else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
continuation.yield(.interrupted)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// MARK: - Errors
|
// MARK: - Errors
|
||||||
|
|
||||||
/// The errors thrown by ``AudioCapturing``.
|
/// The errors thrown by ``AudioCapturing``.
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ extension RecordingView {
|
|||||||
@ObservationIgnored
|
@ObservationIgnored
|
||||||
private var taskCapturer: Task<Void, Never>?
|
private var taskCapturer: Task<Void, Never>?
|
||||||
|
|
||||||
|
/// The task that listens to the events emitted by the capturing service, or `nil` until the first recording starts.
|
||||||
|
@ObservationIgnored
|
||||||
|
private var taskEvents: Task<Void, Never>?
|
||||||
|
|
||||||
/// The task that updates ``elapsedSeconds`` from the measured recording time once per second while recording.
|
/// The task that updates ``elapsedSeconds`` from the measured recording time once per second while recording.
|
||||||
@ObservationIgnored
|
@ObservationIgnored
|
||||||
private var taskTimer: Task<Void, Never>?
|
private var taskTimer: Task<Void, Never>?
|
||||||
@@ -130,16 +134,11 @@ extension RecordingView {
|
|||||||
case .notRecording:
|
case .notRecording:
|
||||||
state = .recording
|
state = .recording
|
||||||
|
|
||||||
|
listenToEvents()
|
||||||
startTimer(true)
|
startTimer(true)
|
||||||
startCapturer(true)
|
startCapturer(true)
|
||||||
case .recording:
|
case .recording:
|
||||||
state = .paused
|
pauseRecording()
|
||||||
|
|
||||||
stopTimer()
|
|
||||||
|
|
||||||
enqueueCapturer {
|
|
||||||
try? await self.capturer.pause()
|
|
||||||
}
|
|
||||||
case .paused:
|
case .paused:
|
||||||
state = .recording
|
state = .recording
|
||||||
|
|
||||||
@@ -224,6 +223,40 @@ private extension RecordingView.Model {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Starts listening to the events emitted by the capturing service, unless already listening: an interruption of the capture pauses
|
||||||
|
/// an ongoing recording, exactly like a press of the main button would.
|
||||||
|
func listenToEvents() {
|
||||||
|
guard taskEvents == nil else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
taskEvents = Task { [weak self, events = capturer.events] in
|
||||||
|
for await event in events {
|
||||||
|
guard let self else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch event {
|
||||||
|
case .interrupted:
|
||||||
|
if state == .recording {
|
||||||
|
pauseRecording()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pauses the ongoing recording: it stops the timer and pauses the audio capture.
|
||||||
|
func pauseRecording() {
|
||||||
|
state = .paused
|
||||||
|
|
||||||
|
stopTimer()
|
||||||
|
|
||||||
|
enqueueCapturer {
|
||||||
|
try? await self.capturer.pause()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Starts the audio capture through the attached ``Capturing``, falling back to the not-recording state when the service fails.
|
/// Starts the audio capture through the attached ``Capturing``, falling back to the not-recording state when the service fails.
|
||||||
///
|
///
|
||||||
/// - Parameter isNewRecording: Whether a new recording should be started, as opposed to a paused one being resumed.
|
/// - Parameter isNewRecording: Whether a new recording should be started, as opposed to a paused one being resumed.
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import Recording
|
|||||||
@MainActor
|
@MainActor
|
||||||
final class CapturingMock: Capturing {
|
final class CapturingMock: Capturing {
|
||||||
|
|
||||||
|
/// The stream of events the service emits outside its method calls.
|
||||||
|
let events: AsyncStream<CapturingEvent>
|
||||||
|
|
||||||
/// The duration every method takes before returning, simulating slow capture work.
|
/// The duration every method takes before returning, simulating slow capture work.
|
||||||
var delay: Duration = .zero
|
var delay: Duration = .zero
|
||||||
|
|
||||||
@@ -15,6 +18,18 @@ final class CapturingMock: Capturing {
|
|||||||
/// The names of the methods called on the service, in call order.
|
/// The names of the methods called on the service, in call order.
|
||||||
private(set) var calls: [String] = []
|
private(set) var calls: [String] = []
|
||||||
|
|
||||||
|
/// The continuation that feeds ``events``.
|
||||||
|
private let continuation: AsyncStream<CapturingEvent>.Continuation
|
||||||
|
|
||||||
|
init() {
|
||||||
|
(events, continuation) = AsyncStream.makeStream(of: CapturingEvent.self)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Emits an interruption of the ongoing capture through ``events``.
|
||||||
|
func interrupt() {
|
||||||
|
continuation.yield(.interrupted)
|
||||||
|
}
|
||||||
|
|
||||||
func start() async throws {
|
func start() async throws {
|
||||||
try await called("start")
|
try await called("start")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -342,6 +342,47 @@ struct RecordingViewModelTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: Interruptions
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
@Suite("Interruptions")
|
||||||
|
struct Interruptions {
|
||||||
|
|
||||||
|
@Test func `pauses an ongoing recording when the capture is interrupted`() async throws {
|
||||||
|
let capturer = CapturingMock()
|
||||||
|
let model = Model(capturer: capturer)
|
||||||
|
|
||||||
|
model.pressedMain()
|
||||||
|
|
||||||
|
try await Task.sleep(for: .seconds(0.1))
|
||||||
|
|
||||||
|
capturer.interrupt()
|
||||||
|
|
||||||
|
try await Task.sleep(for: .seconds(0.1))
|
||||||
|
|
||||||
|
#expect(model.state == .paused)
|
||||||
|
#expect(capturer.calls == ["start", "pause"])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `changes nothing when the capture is interrupted while paused`() async throws {
|
||||||
|
let capturer = CapturingMock()
|
||||||
|
let model = Model(capturer: capturer)
|
||||||
|
|
||||||
|
model.pressedMain()
|
||||||
|
model.pressedMain()
|
||||||
|
|
||||||
|
try await Task.sleep(for: .seconds(0.1))
|
||||||
|
|
||||||
|
capturer.interrupt()
|
||||||
|
|
||||||
|
try await Task.sleep(for: .seconds(0.1))
|
||||||
|
|
||||||
|
#expect(model.state == .paused)
|
||||||
|
#expect(capturer.calls == ["start", "pause"])
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: Processing
|
// MARK: Processing
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
|
|||||||
Reference in New Issue
Block a user