Skip to content

Commit

Permalink
Add movieFragmentInterval value.
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Sep 8, 2024
1 parent e83c6dc commit 884dadc
Showing 1 changed file with 11 additions and 0 deletions.
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, 0 < movieFragmentInterval {
self.movieFragmentInterval = min(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

0 comments on commit 884dadc

Please sign in to comment.