Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the movieFragmentInterval value to the recording feature. #1561

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/IO/IOStreamRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public final class IOStreamRecorder {
public var fileName: String?
/// The running indicies whether recording or not.
public private(set) var isRunning: Atomic<Bool> = .init(false)
/// Specifies the movie fragment interval in sec. This value allows the file to be written continuously, so the file will remain even if the app crashes or is forcefully terminated. A value of 10 seconds or more is recommended.
public var movieFragmentInterval: Double? {
didSet {
if let movieFragmentInterval {
self.movieFragmentInterval = max(10.0, movieFragmentInterval)
}
}
}
private let lockQueue = DispatchQueue(label: "com.haishinkit.HaishinKit.IOStreamRecorder.lock")
private var isReadyForStartWriting: Bool {
guard let writer = writer else {
Expand Down Expand Up @@ -214,6 +222,9 @@ extension IOStreamRecorder: Running {
let fileName = self.fileName ?? UUID().uuidString
let url = self.moviesDirectory.appendingPathComponent(fileName).appendingPathExtension("mp4")
self.writer = try AVAssetWriter(outputURL: url, fileType: .mp4)
if let movieFragmentInterval = self.movieFragmentInterval {
self.writer?.movieFragmentInterval = CMTime(seconds: movieFragmentInterval, preferredTimescale: 1)
}
self.isRunning.mutate { $0 = true }
} catch {
self.delegate?.recorder(self, errorOccured: .failedToCreateAssetWriter(error: error))
Expand Down
Loading