fix warnings
This commit is contained in:
1 parent
e14dc25891
commit
dee1eab897
4 files changed
+288
-210
No files matched your search
@@ -3,7 +3,7 @@ import Combine
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
import UIKit
|
import UIKit
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
@@ -92,14 +92,14 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
try AVAudioSession.sharedInstance().setCategory(
|
try AVAudioSession.sharedInstance().setCategory(
|
||||||
.playAndRecord,
|
.playAndRecord,
|
||||||
mode: .videoRecording,
|
mode: .videoRecording,
|
||||||
options: [.defaultToSpeaker, .allowBluetoothHFP]
|
options: [.defaultToSpeaker, .allowBluetoothHFP]
|
||||||
)
|
)
|
||||||
try AVAudioSession.sharedInstance().setActive(true)
|
try AVAudioSession.sharedInstance().setActive(true)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
let graph = try await configureCaptureGraph()
|
let graph = try await configureCaptureGraph()
|
||||||
guard !Task.isCancelled, sessionGeneration == generation else {
|
guard !Task.isCancelled, sessionGeneration == generation else {
|
||||||
@@ -118,10 +118,12 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
|
|
||||||
func start() throws {
|
func start() throws {
|
||||||
guard lifecycle == .prepared,
|
guard lifecycle == .prepared,
|
||||||
isPrepared,
|
isPrepared,
|
||||||
session.isRunning else { throw VideoNoteRecorderError.notPrepared }
|
session.isRunning
|
||||||
|
else { throw VideoNoteRecorderError.notPrepared }
|
||||||
guard !movieOutput.isRecording,
|
guard !movieOutput.isRecording,
|
||||||
activeRecordingURL == nil else {
|
activeRecordingURL == nil
|
||||||
|
else {
|
||||||
throw VideoNoteRecorderError.recordingBusy
|
throw VideoNoteRecorderError.recordingBusy
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,8 +170,9 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
guard let self else { return }
|
guard let self else { return }
|
||||||
await self.stopMovieOutput()
|
await self.stopMovieOutput()
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
self.lifecycle == .stopping,
|
self.lifecycle == .stopping,
|
||||||
let url = self.activeRecordingURL else { return }
|
let url = self.activeRecordingURL
|
||||||
|
else { return }
|
||||||
self.scheduleFinalizationTimeout(for: url)
|
self.scheduleFinalizationTimeout(for: url)
|
||||||
}
|
}
|
||||||
case .stopping:
|
case .stopping:
|
||||||
@@ -180,48 +183,51 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
func switchCamera() async throws {
|
func switchCamera() async throws {
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
guard isPrepared,
|
guard isPrepared,
|
||||||
lifecycle == .prepared,
|
lifecycle == .prepared,
|
||||||
let currentInput = cameraInput else {
|
let currentInput = cameraInput
|
||||||
throw VideoNoteRecorderError.notPrepared
|
else {
|
||||||
}
|
throw VideoNoteRecorderError.notPrepared
|
||||||
let nextPosition: AVCaptureDevice.Position = isUsingFrontCamera ? .back : .front
|
}
|
||||||
guard let camera = AVCaptureDevice.default(
|
let nextPosition: AVCaptureDevice.Position = isUsingFrontCamera ? .back : .front
|
||||||
.builtInWideAngleCamera,
|
guard
|
||||||
for: .video,
|
let camera = AVCaptureDevice.default(
|
||||||
position: nextPosition
|
.builtInWideAngleCamera,
|
||||||
) else {
|
for: .video,
|
||||||
throw VideoNoteRecorderError.alternateCameraUnavailable
|
position: nextPosition
|
||||||
}
|
)
|
||||||
|
else {
|
||||||
let nextInput = try AVCaptureDeviceInput(device: camera)
|
throw VideoNoteRecorderError.alternateCameraUnavailable
|
||||||
let session = session
|
|
||||||
try await withCheckedThrowingContinuation {
|
|
||||||
(continuation: CheckedContinuation<Void, Error>) in
|
|
||||||
sessionQueue.async {
|
|
||||||
session.beginConfiguration()
|
|
||||||
session.removeInput(currentInput)
|
|
||||||
guard session.canAddInput(nextInput) else {
|
|
||||||
if session.canAddInput(currentInput) {
|
|
||||||
session.addInput(currentInput)
|
|
||||||
}
|
|
||||||
session.commitConfiguration()
|
|
||||||
continuation.resume(throwing: VideoNoteRecorderError.configurationFailed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
session.addInput(nextInput)
|
|
||||||
session.commitConfiguration()
|
|
||||||
continuation.resume()
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
cameraInput = nextInput
|
let nextInput = try AVCaptureDeviceInput(device: camera)
|
||||||
isUsingFrontCamera = nextPosition == .front
|
let session = session
|
||||||
configureVideoConnection()
|
try await withCheckedThrowingContinuation {
|
||||||
#else
|
(continuation: CheckedContinuation<Void, Error>) in
|
||||||
throw VideoNoteRecorderError.alternateCameraUnavailable
|
sessionQueue.async {
|
||||||
#endif
|
session.beginConfiguration()
|
||||||
|
session.removeInput(currentInput)
|
||||||
|
guard session.canAddInput(nextInput) else {
|
||||||
|
if session.canAddInput(currentInput) {
|
||||||
|
session.addInput(currentInput)
|
||||||
|
}
|
||||||
|
session.commitConfiguration()
|
||||||
|
continuation.resume(throwing: VideoNoteRecorderError.configurationFailed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
session.addInput(nextInput)
|
||||||
|
session.commitConfiguration()
|
||||||
|
continuation.resume()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cameraInput = nextInput
|
||||||
|
isUsingFrontCamera = nextPosition == .front
|
||||||
|
configureVideoConnection()
|
||||||
|
#else
|
||||||
|
throw VideoNoteRecorderError.alternateCameraUnavailable
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func toggleMicrophoneMuted() {
|
func toggleMicrophoneMuted() {
|
||||||
@@ -261,7 +267,8 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
|
|
||||||
private func requestStop(afterMinimumDuration minimumDuration: TimeInterval) {
|
private func requestStop(afterMinimumDuration minimumDuration: TimeInterval) {
|
||||||
guard isRecording,
|
guard isRecording,
|
||||||
lifecycle == .starting || lifecycle == .recording else { return }
|
lifecycle == .starting || lifecycle == .recording
|
||||||
|
else { return }
|
||||||
stopRequested = true
|
stopRequested = true
|
||||||
requestedMinimumDuration = max(
|
requestedMinimumDuration = max(
|
||||||
requestedMinimumDuration,
|
requestedMinimumDuration,
|
||||||
@@ -271,11 +278,13 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func recordingDidStart(url: URL) {
|
private func recordingDidStart(url: URL) {
|
||||||
guard VideoNoteRecordingLifecycle.acceptsCompletion(
|
guard
|
||||||
activeURL: activeRecordingURL,
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
outputURL: url
|
activeURL: activeRecordingURL,
|
||||||
), isRecording,
|
outputURL: url
|
||||||
lifecycle == .starting || lifecycle == .stopping else { return }
|
), isRecording,
|
||||||
|
lifecycle == .starting || lifecycle == .stopping
|
||||||
|
else { return }
|
||||||
|
|
||||||
startupTimeoutTask?.cancel()
|
startupTimeoutTask?.cancel()
|
||||||
startupTimeoutTask = nil
|
startupTimeoutTask = nil
|
||||||
@@ -293,11 +302,12 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
guard let self else { return }
|
guard let self else { return }
|
||||||
await self.stopMovieOutput()
|
await self.stopMovieOutput()
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
self.lifecycle == .stopping,
|
self.lifecycle == .stopping,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
self.scheduleFinalizationTimeout(for: url)
|
self.scheduleFinalizationTimeout(for: url)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -319,9 +329,10 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
|
|
||||||
private func scheduleStopAfterActualStartIfNeeded() {
|
private func scheduleStopAfterActualStartIfNeeded() {
|
||||||
guard stopRequested,
|
guard stopRequested,
|
||||||
lifecycle == .recording,
|
lifecycle == .recording,
|
||||||
startedAt != nil,
|
startedAt != nil,
|
||||||
let url = activeRecordingURL else { return }
|
let url = activeRecordingURL
|
||||||
|
else { return }
|
||||||
|
|
||||||
stopRequestTask?.cancel()
|
stopRequestTask?.cancel()
|
||||||
let minimumDuration = requestedMinimumDuration
|
let minimumDuration = requestedMinimumDuration
|
||||||
@@ -331,20 +342,22 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
stopRequestTask = Task { @MainActor [weak self] in
|
stopRequestTask = Task { @MainActor [weak self] in
|
||||||
while true {
|
while true {
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
let self,
|
let self,
|
||||||
self.lifecycle == .recording,
|
self.lifecycle == .recording,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
|
|
||||||
let snapshot = await self.movieOutputSnapshot()
|
let snapshot = await self.movieOutputSnapshot()
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
self.lifecycle == .recording,
|
self.lifecycle == .recording,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
|
|
||||||
if !snapshot.isRecording {
|
if !snapshot.isRecording {
|
||||||
// AVFoundation stopped without the user's stop reaching
|
// AVFoundation stopped without the user's stop reaching
|
||||||
@@ -374,30 +387,34 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
let self,
|
let self,
|
||||||
self.lifecycle == .recording,
|
self.lifecycle == .recording,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
|
|
||||||
self.lifecycle = .stopping
|
self.lifecycle = .stopping
|
||||||
await self.stopMovieOutput()
|
await self.stopMovieOutput()
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
self.lifecycle == .stopping,
|
self.lifecycle == .stopping,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
self.scheduleFinalizationTimeout(for: url)
|
self.scheduleFinalizationTimeout(for: url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func recordingDidFinish(url: URL, error: Error?) {
|
private func recordingDidFinish(url: URL, error: Error?) {
|
||||||
guard VideoNoteRecordingLifecycle.acceptsCompletion(
|
guard
|
||||||
activeURL: activeRecordingURL,
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
outputURL: url
|
activeURL: activeRecordingURL,
|
||||||
) else {
|
outputURL: url
|
||||||
|
)
|
||||||
|
else {
|
||||||
// A delayed callback from an earlier attempt must not tear down a
|
// A delayed callback from an earlier attempt must not tear down a
|
||||||
// newer capture graph. Do not delete it here: macOS can spell the
|
// newer capture graph. Do not delete it here: macOS can spell the
|
||||||
// same temporary URL through a filesystem alias, and preserving a
|
// same temporary URL through a filesystem alias, and preserving a
|
||||||
@@ -433,10 +450,11 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
fallbackDuration: fallbackDuration
|
fallbackDuration: fallbackDuration
|
||||||
)
|
)
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
self.fileValidationTask = nil
|
self.fileValidationTask = nil
|
||||||
|
|
||||||
if self.discardCurrentRecording {
|
if self.discardCurrentRecording {
|
||||||
@@ -446,29 +464,33 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let inspection,
|
if let inspection,
|
||||||
VideoNoteRecordingCompletionPolicy.wasRequestedOrReachedLimit(
|
VideoNoteRecordingCompletionPolicy.wasRequestedOrReachedLimit(
|
||||||
stopRequested: wasStopRequested,
|
stopRequested: wasStopRequested,
|
||||||
error: error,
|
error: error,
|
||||||
recordedDuration: inspection.duration,
|
recordedDuration: inspection.duration,
|
||||||
maximumDuration: VideoNoteStopPolicy.maximumCaptureDuration
|
maximumDuration: VideoNoteStopPolicy.maximumCaptureDuration
|
||||||
),
|
),
|
||||||
VideoNoteStopPolicy.isValidFinalDuration(inspection.duration) {
|
VideoNoteStopPolicy.isValidFinalDuration(inspection.duration)
|
||||||
self.finishAndReset(.success(Recording(
|
{
|
||||||
url: url,
|
self.finishAndReset(
|
||||||
duration: inspection.duration
|
.success(
|
||||||
)))
|
Recording(
|
||||||
|
url: url,
|
||||||
|
duration: inspection.duration
|
||||||
|
)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try? FileManager.default.removeItem(at: url)
|
try? FileManager.default.removeItem(at: url)
|
||||||
let failure: Error
|
let failure: Error
|
||||||
if !wasStopRequested,
|
if !wasStopRequested,
|
||||||
!VideoNoteRecordingCompletionPolicy.wasRequestedOrReachedLimit(
|
!VideoNoteRecordingCompletionPolicy.wasRequestedOrReachedLimit(
|
||||||
stopRequested: false,
|
stopRequested: false,
|
||||||
error: error,
|
error: error,
|
||||||
recordedDuration: inspection?.duration ?? fallbackDuration,
|
recordedDuration: inspection?.duration ?? fallbackDuration,
|
||||||
maximumDuration: VideoNoteStopPolicy.maximumCaptureDuration
|
maximumDuration: VideoNoteStopPolicy.maximumCaptureDuration
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
failure = VideoNoteRecorderError.recordingInterrupted
|
failure = VideoNoteRecorderError.recordingInterrupted
|
||||||
} else if !VideoNoteRecordingCompletionPolicy.shouldKeepOutput(for: error), let error {
|
} else if !VideoNoteRecordingCompletionPolicy.shouldKeepOutput(for: error), let error {
|
||||||
failure = error
|
failure = error
|
||||||
@@ -501,10 +523,11 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
return await withCheckedContinuation { continuation in
|
return await withCheckedContinuation { continuation in
|
||||||
sessionQueue.async {
|
sessionQueue.async {
|
||||||
let seconds = CMTimeGetSeconds(output.recordedDuration)
|
let seconds = CMTimeGetSeconds(output.recordedDuration)
|
||||||
continuation.resume(returning: MovieOutputSnapshot(
|
continuation.resume(
|
||||||
isRecording: output.isRecording,
|
returning: MovieOutputSnapshot(
|
||||||
recordedDuration: seconds.isFinite && seconds > 0 ? seconds : 0
|
isRecording: output.isRecording,
|
||||||
))
|
recordedDuration: seconds.isFinite && seconds > 0 ? seconds : 0
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -532,21 +555,23 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
nanoseconds: VideoNoteStopPolicy.startupTimeoutNanoseconds
|
nanoseconds: VideoNoteStopPolicy.startupTimeoutNanoseconds
|
||||||
)
|
)
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
let self,
|
let self,
|
||||||
self.lifecycle == .starting,
|
self.lifecycle == .starting,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
|
|
||||||
self.lifecycle = .stopping
|
self.lifecycle = .stopping
|
||||||
await self.stopMovieOutput()
|
await self.stopMovieOutput()
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
self.lifecycle == .stopping,
|
self.lifecycle == .stopping,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
self.scheduleFinalizationTimeout(for: url)
|
self.scheduleFinalizationTimeout(for: url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -558,12 +583,13 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
nanoseconds: VideoNoteStopPolicy.finalizationTimeoutNanoseconds
|
nanoseconds: VideoNoteStopPolicy.finalizationTimeoutNanoseconds
|
||||||
)
|
)
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
let self,
|
let self,
|
||||||
self.lifecycle == .stopping,
|
self.lifecycle == .stopping,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
|
|
||||||
// A few macOS camera drivers stop the file output but delay its
|
// A few macOS camera drivers stop the file output but delay its
|
||||||
// delegate callback until the capture session itself is flushed.
|
// delegate callback until the capture session itself is flushed.
|
||||||
@@ -574,42 +600,47 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
nanoseconds: VideoNoteStopPolicy.finalizationRecoveryGraceNanoseconds
|
nanoseconds: VideoNoteStopPolicy.finalizationRecoveryGraceNanoseconds
|
||||||
)
|
)
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
self.lifecycle == .stopping,
|
self.lifecycle == .stopping,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
let fallbackDuration = self.measuredWallClockDuration
|
let fallbackDuration = self.measuredWallClockDuration
|
||||||
let inspection = await self.fileInspector.inspect(
|
let inspection = await self.fileInspector.inspect(
|
||||||
url: url,
|
url: url,
|
||||||
fallbackDuration: fallbackDuration
|
fallbackDuration: fallbackDuration
|
||||||
)
|
)
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
self.lifecycle == .stopping,
|
self.lifecycle == .stopping,
|
||||||
VideoNoteRecordingLifecycle.acceptsCompletion(
|
VideoNoteRecordingLifecycle.acceptsCompletion(
|
||||||
activeURL: self.activeRecordingURL,
|
activeURL: self.activeRecordingURL,
|
||||||
outputURL: url
|
outputURL: url
|
||||||
) else { return }
|
)
|
||||||
|
else { return }
|
||||||
|
|
||||||
self.finalizationTimeoutTask = nil
|
self.finalizationTimeoutTask = nil
|
||||||
if self.discardCurrentRecording {
|
if self.discardCurrentRecording {
|
||||||
try? FileManager.default.removeItem(at: url)
|
try? FileManager.default.removeItem(at: url)
|
||||||
self.finishAndReset(nil)
|
self.finishAndReset(nil)
|
||||||
} else if let inspection,
|
} else if let inspection,
|
||||||
VideoNoteRecordingCompletionPolicy.wasRequestedOrReachedLimit(
|
VideoNoteRecordingCompletionPolicy.wasRequestedOrReachedLimit(
|
||||||
stopRequested: self.stopRequested,
|
stopRequested: self.stopRequested,
|
||||||
error: nil,
|
error: nil,
|
||||||
recordedDuration: inspection.duration,
|
recordedDuration: inspection.duration,
|
||||||
maximumDuration: VideoNoteStopPolicy.maximumCaptureDuration
|
maximumDuration: VideoNoteStopPolicy.maximumCaptureDuration
|
||||||
),
|
),
|
||||||
VideoNoteStopPolicy.isValidFinalDuration(inspection.duration) {
|
VideoNoteStopPolicy.isValidFinalDuration(inspection.duration)
|
||||||
|
{
|
||||||
// Some macOS camera drivers finalize the .mov but omit the
|
// Some macOS camera drivers finalize the .mov but omit the
|
||||||
// delegate callback. A playable file is more authoritative
|
// delegate callback. A playable file is more authoritative
|
||||||
// than the missing callback and remains safe to send.
|
// than the missing callback and remains safe to send.
|
||||||
self.finishAndReset(.success(Recording(
|
self.finishAndReset(
|
||||||
url: url,
|
.success(
|
||||||
duration: inspection.duration
|
Recording(
|
||||||
)))
|
url: url,
|
||||||
|
duration: inspection.duration
|
||||||
|
)))
|
||||||
} else if !self.stopRequested {
|
} else if !self.stopRequested {
|
||||||
try? FileManager.default.removeItem(at: url)
|
try? FileManager.default.removeItem(at: url)
|
||||||
self.finishAndReset(.failure(VideoNoteRecorderError.recordingInterrupted))
|
self.finishAndReset(.failure(VideoNoteRecorderError.recordingInterrupted))
|
||||||
@@ -653,9 +684,10 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
isRecording = false
|
isRecording = false
|
||||||
isUsingFrontCamera = true
|
isUsingFrontCamera = true
|
||||||
isMicrophoneMuted = false
|
isMicrophoneMuted = false
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
|
try? AVAudioSession.sharedInstance().setActive(
|
||||||
#endif
|
false, options: .notifyOthersOnDeactivation)
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private func waitForPreviousRecordingToFinish() async throws {
|
private func waitForPreviousRecordingToFinish() async throws {
|
||||||
@@ -673,29 +705,61 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
|
|
||||||
private func configureVideoConnection() {
|
private func configureVideoConnection() {
|
||||||
guard let connection = movieOutput.connection(with: .video) else { return }
|
guard let connection = movieOutput.connection(with: .video) else { return }
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
configureInteroperableVideoCodec(for: connection)
|
configureInteroperableVideoCodec(for: connection)
|
||||||
#endif
|
#endif
|
||||||
if connection.isVideoMirroringSupported {
|
if connection.isVideoMirroringSupported {
|
||||||
connection.automaticallyAdjustsVideoMirroring = false
|
connection.automaticallyAdjustsVideoMirroring = false
|
||||||
connection.isVideoMirrored = isUsingFrontCamera
|
connection.isVideoMirrored = isUsingFrontCamera
|
||||||
}
|
}
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
if connection.isVideoOrientationSupported {
|
if #available(iOS 17.0, *) {
|
||||||
connection.videoOrientation = Self.currentVideoOrientation
|
let angle = videoRotationAngle
|
||||||
}
|
if connection.isVideoRotationAngleSupported(angle) {
|
||||||
#endif
|
connection.videoRotationAngle = angle
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if connection.isVideoOrientationSupported {
|
||||||
|
connection.videoOrientation = Self.currentVideoOrientation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
private func configureInteroperableVideoCodec(for connection: AVCaptureConnection) {
|
private var videoRotationAngle: CGFloat {
|
||||||
guard movieOutput.availableVideoCodecTypes.contains(.h264) else { return }
|
switch UIDevice.current.orientation {
|
||||||
movieOutput.setOutputSettings(
|
case .portraitUpsideDown: return 180
|
||||||
[AVVideoCodecKey: AVVideoCodecType.h264],
|
case .landscapeLeft: return 90
|
||||||
for: connection
|
case .landscapeRight: return 270
|
||||||
)
|
default: return 0
|
||||||
}
|
}
|
||||||
#endif
|
}
|
||||||
|
|
||||||
|
@available(iOS, deprecated: 17.0)
|
||||||
|
private static var currentVideoOrientation: AVCaptureVideoOrientation {
|
||||||
|
switch UIDevice.current.orientation {
|
||||||
|
case .portraitUpsideDown:
|
||||||
|
return .portraitUpsideDown
|
||||||
|
case .landscapeLeft:
|
||||||
|
return .landscapeRight
|
||||||
|
case .landscapeRight:
|
||||||
|
return .landscapeLeft
|
||||||
|
default:
|
||||||
|
return .portrait
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if os(iOS)
|
||||||
|
private func configureInteroperableVideoCodec(for connection: AVCaptureConnection) {
|
||||||
|
guard movieOutput.availableVideoCodecTypes.contains(.h264) else { return }
|
||||||
|
movieOutput.setOutputSettings(
|
||||||
|
[AVVideoCodecKey: AVVideoCodecType.h264],
|
||||||
|
for: connection
|
||||||
|
)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
private func resetCaptureGraph() {
|
private func resetCaptureGraph() {
|
||||||
let session = session
|
let session = session
|
||||||
@@ -728,19 +792,21 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
sessionQueue.async {
|
sessionQueue.async {
|
||||||
session.beginConfiguration()
|
session.beginConfiguration()
|
||||||
do {
|
do {
|
||||||
session.sessionPreset = session.canSetSessionPreset(.vga640x480)
|
session.sessionPreset =
|
||||||
|
session.canSetSessionPreset(.vga640x480)
|
||||||
? .vga640x480
|
? .vga640x480
|
||||||
: .medium
|
: .medium
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
let camera = AVCaptureDevice.default(
|
let camera =
|
||||||
.builtInWideAngleCamera,
|
AVCaptureDevice.default(
|
||||||
for: .video,
|
.builtInWideAngleCamera,
|
||||||
position: .front
|
for: .video,
|
||||||
) ?? AVCaptureDevice.default(for: .video)
|
position: .front
|
||||||
#else
|
) ?? AVCaptureDevice.default(for: .video)
|
||||||
let camera = AVCaptureDevice.default(for: .video)
|
#else
|
||||||
#endif
|
let camera = AVCaptureDevice.default(for: .video)
|
||||||
|
#endif
|
||||||
guard let camera else {
|
guard let camera else {
|
||||||
throw VideoNoteRecorderError.cameraUnavailable
|
throw VideoNoteRecorderError.cameraUnavailable
|
||||||
}
|
}
|
||||||
@@ -768,11 +834,12 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
preferredTimescale: 600
|
preferredTimescale: 600
|
||||||
)
|
)
|
||||||
session.commitConfiguration()
|
session.commitConfiguration()
|
||||||
continuation.resume(returning: CaptureGraph(
|
continuation.resume(
|
||||||
cameraInput: cameraInput,
|
returning: CaptureGraph(
|
||||||
microphoneInput: microphoneInput,
|
cameraInput: cameraInput,
|
||||||
usesFrontCamera: camera.position == .front
|
microphoneInput: microphoneInput,
|
||||||
))
|
usesFrontCamera: camera.position == .front
|
||||||
|
))
|
||||||
} catch {
|
} catch {
|
||||||
session.commitConfiguration()
|
session.commitConfiguration()
|
||||||
continuation.resume(throwing: error)
|
continuation.resume(throwing: error)
|
||||||
@@ -792,8 +859,9 @@ final class VideoNoteRecorder: NSObject, ObservableObject, AVCaptureFileOutputRe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
guard !Task.isCancelled,
|
guard !Task.isCancelled,
|
||||||
sessionGeneration == generation,
|
sessionGeneration == generation,
|
||||||
session.isRunning else {
|
session.isRunning
|
||||||
|
else {
|
||||||
throw CancellationError()
|
throw CancellationError()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -824,16 +892,18 @@ private actor VideoNoteFileInspector {
|
|||||||
try? await Task.sleep(nanoseconds: delay)
|
try? await Task.sleep(nanoseconds: delay)
|
||||||
}
|
}
|
||||||
guard let values = try? url.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey]),
|
guard let values = try? url.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey]),
|
||||||
values.isRegularFile == true,
|
values.isRegularFile == true,
|
||||||
let size = values.fileSize,
|
let size = values.fileSize,
|
||||||
size > 1_024 else { continue }
|
size > 1_024
|
||||||
|
else { continue }
|
||||||
|
|
||||||
let asset = AVURLAsset(url: url)
|
let asset = AVURLAsset(url: url)
|
||||||
guard let duration = try? await asset.load(.duration),
|
guard let duration = try? await asset.load(.duration),
|
||||||
duration.seconds.isFinite,
|
duration.seconds.isFinite,
|
||||||
duration.seconds > 0.04,
|
duration.seconds > 0.04,
|
||||||
let tracks = try? await asset.loadTracks(withMediaType: .video),
|
let tracks = try? await asset.loadTracks(withMediaType: .video),
|
||||||
!tracks.isEmpty else { continue }
|
!tracks.isEmpty
|
||||||
|
else { continue }
|
||||||
return Inspection(duration: duration.seconds)
|
return Inspection(duration: duration.seconds)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -842,9 +912,10 @@ private actor VideoNoteFileInspector {
|
|||||||
// of macOS camera drivers that delay movie metadata longer than the
|
// of macOS camera drivers that delay movie metadata longer than the
|
||||||
// delegate callback itself.
|
// delegate callback itself.
|
||||||
guard fallbackDuration > 0.25,
|
guard fallbackDuration > 0.25,
|
||||||
let values = try? url.resourceValues(forKeys: [.fileSizeKey]),
|
let values = try? url.resourceValues(forKeys: [.fileSizeKey]),
|
||||||
let size = values.fileSize,
|
let size = values.fileSize,
|
||||||
size > 16_384 else { return nil }
|
size > 16_384
|
||||||
|
else { return nil }
|
||||||
return Inspection(duration: fallbackDuration)
|
return Inspection(duration: fallbackDuration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -872,20 +943,20 @@ enum VideoNoteStopPolicy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
private extension VideoNoteRecorder {
|
extension VideoNoteRecorder {
|
||||||
static var currentVideoOrientation: AVCaptureVideoOrientation {
|
fileprivate static var currentVideoOrientation: AVCaptureVideoOrientation {
|
||||||
switch UIDevice.current.orientation {
|
switch UIDevice.current.orientation {
|
||||||
case .portraitUpsideDown:
|
case .portraitUpsideDown:
|
||||||
return .portraitUpsideDown
|
return .portraitUpsideDown
|
||||||
case .landscapeLeft:
|
case .landscapeLeft:
|
||||||
return .landscapeRight
|
return .landscapeRight
|
||||||
case .landscapeRight:
|
case .landscapeRight:
|
||||||
return .landscapeLeft
|
return .landscapeLeft
|
||||||
default:
|
default:
|
||||||
return .portrait
|
return .portrait
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private enum VideoNoteRecorderError: LocalizedError {
|
private enum VideoNoteRecorderError: LocalizedError {
|
||||||
@@ -920,7 +991,8 @@ private enum VideoNoteRecorderError: LocalizedError {
|
|||||||
case .recordingBusy:
|
case .recordingBusy:
|
||||||
return "Предыдущая запись ещё завершается. Подождите секунду и повторите."
|
return "Предыдущая запись ещё завершается. Подождите секунду и повторите."
|
||||||
case .recordingInterrupted:
|
case .recordingInterrupted:
|
||||||
return "Камера прервала запись раньше времени. Видеосообщение не отправлено. Повторите запись."
|
return
|
||||||
|
"Камера прервала запись раньше времени. Видеосообщение не отправлено. Повторите запись."
|
||||||
case .finalizationTimedOut:
|
case .finalizationTimedOut:
|
||||||
return "Камера не смогла завершить видеофайл. Повторите запись."
|
return "Камера не смогла завершить видеофайл. Повторите запись."
|
||||||
case .emptyRecording:
|
case .emptyRecording:
|
||||||
|
|||||||
@@ -889,7 +889,7 @@ private final class LumaJingleSession: JingleSession {
|
|||||||
|
|
||||||
// MARK: - Live WebRTC call
|
// MARK: - Live WebRTC call
|
||||||
|
|
||||||
private final class LumaWebRTCCall: NSObject, @unchecked Sendable, RTCPeerConnectionDelegate {
|
private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate, @unchecked Sendable {
|
||||||
let id = UUID()
|
let id = UUID()
|
||||||
let startedAt = Date()
|
let startedAt = Date()
|
||||||
unowned let engine: LumaCallEngine
|
unowned let engine: LumaCallEngine
|
||||||
|
|||||||
@@ -6,17 +6,17 @@ import Martin
|
|||||||
final class LumaRoomStore: RoomStore {
|
final class LumaRoomStore: RoomStore {
|
||||||
typealias Room = RoomBase
|
typealias Room = RoomBase
|
||||||
|
|
||||||
private let dispatcher = QueueDispatcher(label: "LumaRoomStore")
|
private let queue = DispatchQueue(label: "LumaRoomStore")
|
||||||
private var roomsByJID: [BareJID: RoomBase] = [:]
|
private var roomsByJID: [BareJID: RoomBase] = [:]
|
||||||
|
|
||||||
func rooms(for context: Context) -> [RoomBase] {
|
func rooms(for context: Context) -> [RoomBase] {
|
||||||
dispatcher.sync {
|
queue.sync {
|
||||||
Array(roomsByJID.values)
|
Array(roomsByJID.values)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func room(for context: Context, with jid: BareJID) -> RoomBase? {
|
func room(for context: Context, with jid: BareJID) -> RoomBase? {
|
||||||
dispatcher.sync {
|
queue.sync {
|
||||||
roomsByJID[jid]
|
roomsByJID[jid]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,8 +26,8 @@ final class LumaRoomStore: RoomStore {
|
|||||||
with jid: BareJID,
|
with jid: BareJID,
|
||||||
nickname: String,
|
nickname: String,
|
||||||
password: String?
|
password: String?
|
||||||
) -> ConversationCreateResult<RoomBase> {
|
) -> ConversationCreateResult {
|
||||||
dispatcher.sync {
|
queue.sync {
|
||||||
if let room = roomsByJID[jid] {
|
if let room = roomsByJID[jid] {
|
||||||
return .found(room)
|
return .found(room)
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,7 @@ final class LumaRoomStore: RoomStore {
|
|||||||
jid: jid,
|
jid: jid,
|
||||||
nickname: nickname,
|
nickname: nickname,
|
||||||
password: password,
|
password: password,
|
||||||
dispatcher: dispatcher
|
dispatcher: queue
|
||||||
)
|
)
|
||||||
roomsByJID[jid] = room
|
roomsByJID[jid] = room
|
||||||
return .created(room)
|
return .created(room)
|
||||||
@@ -44,7 +44,7 @@ final class LumaRoomStore: RoomStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func close(room: RoomBase) -> Bool {
|
func close(room: RoomBase) -> Bool {
|
||||||
dispatcher.sync {
|
queue.sync {
|
||||||
guard roomsByJID[room.jid] === room else { return false }
|
guard roomsByJID[room.jid] === room else { return false }
|
||||||
roomsByJID.removeValue(forKey: room.jid)
|
roomsByJID.removeValue(forKey: room.jid)
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -1838,7 +1838,7 @@ final class XMPPService {
|
|||||||
makeMembersOnly: Bool
|
makeMembersOnly: Bool
|
||||||
) async throws -> Bool {
|
) async throws -> Bool {
|
||||||
let roomJID = JID(room.jid)
|
let roomJID = JID(room.jid)
|
||||||
let configuration: JabberDataElement = try await withCheckedThrowingContinuation {
|
let configuration: DataForm = try await withCheckedThrowingContinuation {
|
||||||
continuation in
|
continuation in
|
||||||
muc.getRoomConfiguration(roomJid: roomJID) { result in
|
muc.getRoomConfiguration(roomJid: roomJID) { result in
|
||||||
switch result {
|
switch result {
|
||||||
@@ -1849,25 +1849,31 @@ final class XMPPService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var supportsNonAnonymousRooms = false
|
var supportsNonAnonymousRooms = false
|
||||||
if let field: ListSingleField = configuration.getField(named: "muc#roomconfig_whois") {
|
if let field: DataForm.ListSingleField = configuration.getField(
|
||||||
|
named: "muc#roomconfig_whois")
|
||||||
|
{
|
||||||
field.value = "anyone"
|
field.value = "anyone"
|
||||||
supportsNonAnonymousRooms = true
|
supportsNonAnonymousRooms = true
|
||||||
} else if let field: TextSingleField = configuration.getField(named: "muc#roomconfig_whois")
|
} else if let field: DataForm.TextSingleField = configuration.getField(
|
||||||
|
named: "muc#roomconfig_whois")
|
||||||
{
|
{
|
||||||
field.value = "anyone"
|
field.value = "anyone"
|
||||||
supportsNonAnonymousRooms = true
|
supportsNonAnonymousRooms = true
|
||||||
}
|
}
|
||||||
if let field: ListMultiField = configuration.getField(named: "muc#roomconfig_getmemberlist")
|
if let field: DataForm.ListMultiField = configuration.getField(
|
||||||
|
named: "muc#roomconfig_getmemberlist")
|
||||||
{
|
{
|
||||||
field.value = Array(Set(field.value + ["moderator", "participant"])).sorted()
|
field.value = Array(Set(field.value + ["moderator", "participant"])).sorted()
|
||||||
}
|
}
|
||||||
if makeMembersOnly,
|
if makeMembersOnly,
|
||||||
let field: BooleanField = configuration.getField(named: "muc#roomconfig_membersonly")
|
let field: DataForm.BooleanField = configuration.getField(
|
||||||
|
named: "muc#roomconfig_membersonly")
|
||||||
{
|
{
|
||||||
field.value = true
|
field.value = true
|
||||||
}
|
}
|
||||||
if makeMembersOnly,
|
if makeMembersOnly,
|
||||||
let field: BooleanField = configuration.getField(named: "muc#roomconfig_persistentroom")
|
let field: DataForm.BooleanField = configuration.getField(
|
||||||
|
named: "muc#roomconfig_persistentroom")
|
||||||
{
|
{
|
||||||
field.value = true
|
field.value = true
|
||||||
}
|
}
|
||||||
@@ -1876,7 +1882,7 @@ final class XMPPService {
|
|||||||
(continuation: CheckedContinuation<Void, Error>) in
|
(continuation: CheckedContinuation<Void, Error>) in
|
||||||
muc.setRoomConfiguration(roomJid: roomJID, configuration: configuration) { result in
|
muc.setRoomConfiguration(roomJid: roomJID, configuration: configuration) { result in
|
||||||
switch result {
|
switch result {
|
||||||
case .success(let value): continuation.resume(returning: value)
|
case .success: continuation.resume()
|
||||||
case .failure(let error): continuation.resume(throwing: error)
|
case .failure(let error): continuation.resume(throwing: error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user