forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoChunkEncoder.swift
More file actions
1322 lines (1160 loc) · 50.8 KB
/
Copy pathVideoChunkEncoder.swift
File metadata and controls
1322 lines (1160 loc) · 50.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@preconcurrency import AVFoundation
import AppKit
import CoreGraphics
import Foundation
import Sentry
import os
/// Identifies one writer's ownership of a video chunk. The generation remains
/// necessary even though new paths include a unique suffix: older stored paths
/// and test seams can still use second-granular names.
struct RewindVideoChunkReservation: Equatable, Sendable {
let generation: UInt64
let relativePath: String
}
/// Signals that one exact writer generation was cancelled before it produced a
/// durable MP4 trailer. The encoder deliberately emits this ownership token
/// instead of reaching into persistence: the indexer/storage boundary owns the
/// corresponding database and filesystem recovery.
struct RewindAbandonedVideoChunkError: Error, Sendable {
let relativePath: String
}
/// A marker could not be persisted before a writer needed cancellation. The
/// storage owner must use its DB-first fallback before it force-cancels this
/// exact reservation.
struct RewindAbandonedVideoChunkMarkerWriteError: Error, Sendable {
let reservation: RewindVideoChunkReservation
}
enum RewindVideoChunkCancellationResult: Sendable {
case noActiveChunk
case markerRecorded(RewindVideoChunkReservation)
case markerWriteFailed(RewindVideoChunkReservation)
}
private enum RewindVideoChunkLifecyclePhase: Equatable, Sendable {
case writing
case finalizing
var diagnosticName: String {
switch self {
case .writing: return "writing"
case .finalizing: return "finalizing"
}
}
}
/// Actor-local ownership for the currently installed video writer.
///
/// A finalizer captures the reservation before awaiting AVFoundation. Its later
/// cleanup may only clear that exact reservation, never a replacement writer.
struct RewindVideoChunkLifecycle: Equatable, Sendable {
private(set) var activeReservation: RewindVideoChunkReservation?
private var phase: RewindVideoChunkLifecyclePhase?
mutating func install(_ reservation: RewindVideoChunkReservation) {
activeReservation = reservation
phase = .writing
}
/// Atomically transitions the current writer to finalization. A writer may
/// only be finalized once; concurrent callers must leave the owner alone.
@discardableResult
mutating func beginFinalization(of reservation: RewindVideoChunkReservation) -> Bool {
guard activeReservation == reservation, phase == .writing else {
return false
}
phase = .finalizing
return true
}
/// Clears the active reservation when it is still owned by `expected`.
/// `nil` is an unconditional lifecycle reset used by explicit cancellation.
@discardableResult
mutating func reset(onlyIfCurrent expected: RewindVideoChunkReservation? = nil) -> RewindVideoChunkReservation? {
if let expected, activeReservation != expected {
return nil
}
let releasedReservation = activeReservation
activeReservation = nil
phase = nil
return releasedReservation
}
func owns(_ reservation: RewindVideoChunkReservation) -> Bool {
activeReservation == reservation
}
func isWriting(_ reservation: RewindVideoChunkReservation) -> Bool {
activeReservation == reservation && phase == .writing
}
var diagnosticPhase: String {
phase?.diagnosticName ?? "idle"
}
}
/// Encodes screenshot frames into H.265 video chunks using VideoToolbox for efficient storage.
actor VideoChunkEncoder {
static let shared = VideoChunkEncoder()
// MARK: - Configuration
private let chunkDuration: TimeInterval = 60.0 // 60-second chunks
/// First chunk after launch finalizes faster so Rewind shows screenshots within
/// seconds of monitoring starting, instead of waiting up to 60s + 10s stale grace.
/// The Rewind UI filters out the active (unfinalized) chunk, so without this you'd
/// see nothing on the Rewind page for ~1-2 minutes after every launch.
private let firstChunkDuration: TimeInterval = 5.0
private var frameRate: Double {
let interval = RewindSettings.shared.effectiveCaptureInterval(isOnBattery: PowerMonitor.cachedBatteryState())
return 1.0 / interval // e.g. 0.5s interval = 2 FPS
}
private let maxResolution: CGFloat = 3000 // Maximum dimension
/// Threshold for aspect ratio change that triggers a new chunk (20% difference)
private let aspectRatioChangeThreshold: CGFloat = 0.2
/// Seconds the new aspect ratio must remain stable before switching chunks.
/// Prevents rapid app switching from spawning bursts of short-lived chunks
/// that churn the hardware encoder during finalization.
private let aspectRatioStabilityDelay: TimeInterval = 2.0
/// Maximum frames to buffer before forcing a flush (memory safety)
/// Calculated from chunk duration + frame rate + padding so the normal
/// duration-based finalization always fires before this safety limit.
private var maxBufferFrames: Int {
// Use the chunk's frozen rate while a chunk is active so a mid-chunk rate
// change can't shrink the cap and trip a spurious buffer-overflow reset.
Int(chunkDuration * (currentChunkFrameRate ?? frameRate)) + 20
}
/// Maximum consecutive encoder failures before emergency reset
private let maxConsecutiveFailures = 5
/// Consecutive not-ready states that force an encoder reset. This keeps
/// writer readiness loops bounded instead of emitting one Sentry event per frame.
private let maxConsecutiveNotReadyFailures = 3
// MARK: - State
/// Buffer stores only timestamps, not CGImages (memory optimization)
/// CGImages are written to the encoder immediately and not retained.
private var frameTimestamps: [Date] = []
/// Track consecutive encoder write failures for recovery.
private var consecutiveWriteFailures = 0
private var encoderRestartCount = 0
private var emergencyResetCount = 0
private var writerNotReadyCount = 0
/// Pending aspect ratio debounce state
private var pendingAspectRatioSize: CGSize?
private var pendingAspectRatioSince: Date?
private var currentChunkStartTime: Date?
/// Capture frame rate frozen at chunk start. Presentation timestamps and the
/// buffer cap must use one rate for a chunk's whole lifetime: `frameRate` is a
/// live property of the battery state, and a mid-chunk change (AC power plugged
/// in shrinks the capture interval 3x → higher rate) would make a later frame's
/// PTS (`frameOffset / rate`) fall below an already-appended sample's PTS,
/// which AVAssetWriter rejects as non-monotonic — dropping frames and, past the
/// failure threshold, discarding the entire in-progress chunk.
private var currentChunkFrameRate: Double?
private(set) var currentChunkPath: String?
private var chunkLifecycle = RewindVideoChunkLifecycle()
/// Completion waiters for the reservation currently writing its MP4 trailer.
/// A flush is a durability barrier: callers that arrive during finalization
/// must wait for that same writer rather than treating it as an empty buffer.
private var inFlightFinalization: InFlightFinalization?
private var frameOffsetInChunk: Int = 0
// Native HEVC writer state
private var assetWriter: AVAssetWriter?
private var writerInput: AVAssetWriterInput?
private var pixelBufferAdaptor: AVAssetWriterInputPixelBufferAdaptor?
private var currentOutputSize: CGSize?
private var currentChunkInputSize: CGSize? // Track input size for aspect ratio comparison
/// Timer to finalize stale chunks when frames stop arriving
private var stalenessCheckTask: Task<Void, Never>?
private var videosDirectory: URL?
private var isInitialized = false
/// True after the first chunk finalizes. Until then we use `firstChunkDuration`
/// instead of `chunkDuration` so Rewind UI starts showing frames within seconds
/// of launch.
private var hasFinalizedAnyChunk = false
// Test-only synchronization hooks. They are unset in production and let the
// behavioral regression test hold a real writer after the in-flight state is
// published, without relying on wall-clock scheduling.
private var beforeFinishWritingForTesting: (@Sendable () async -> Void)?
private var finalizationJoinedForTesting: (@Sendable () -> Void)?
/// When set, appends succeed this many times before every later append fails.
/// This intentionally models an AVFoundation append failure after rows may
/// already have been persisted for earlier frames in the same chunk.
private var appendFailureAfterSuccessfulFramesForTesting: Int?
/// Exercises the DB-first owner-transition fallback when a sidecar cannot
/// be created before cancellation.
private var abandonmentMarkerWriteFailuresForTesting = 0
/// Exercises finalization recovery at the indexer stop boundary without
/// relying on an AVFoundation codec failure.
private var finishWritingFailuresForTesting = 0
// MARK: - Types
struct EncodedFrame: Sendable {
let videoChunkPath: String // Relative path to .mp4
let frameOffset: Int // Frame index within chunk
let timestamp: Date
}
struct ChunkFlushResult: Sendable {
let videoChunkPath: String
let frames: [EncodedFrame]
}
private struct InFlightFinalization {
let reservation: RewindVideoChunkReservation
var waiters: [CheckedContinuation<Bool, Error>] = []
}
// MARK: - Initialization
private init() {}
/// Initialize the encoder with the videos directory
func initialize(videosDirectory: URL) async throws {
if isInitialized {
guard self.videosDirectory != videosDirectory else { return }
throw RewindError.storageError("Video encoder must reset before changing storage owner")
}
self.videosDirectory = videosDirectory
isInitialized = true
log("VideoChunkEncoder: Initialized at \(videosDirectory.path)")
}
func videosDirectoryForTesting() -> URL? {
videosDirectory
}
func setFinalizationHooksForTesting(
beforeFinishWriting: (@Sendable () async -> Void)?,
finalizationJoined: (@Sendable () -> Void)?
) {
beforeFinishWritingForTesting = beforeFinishWriting
finalizationJoinedForTesting = finalizationJoined
}
func setAppendFailureAfterSuccessfulFramesForTesting(_ successfulFrames: Int?) {
if let successfulFrames {
precondition(successfulFrames >= 0)
}
appendFailureAfterSuccessfulFramesForTesting = successfulFrames
}
func setAbandonmentMarkerWriteFailuresForTesting(_ failures: Int) {
precondition(failures >= 0)
abandonmentMarkerWriteFailuresForTesting = failures
}
func setFinishWritingFailuresForTesting(_ failures: Int) {
precondition(failures >= 0)
finishWritingFailuresForTesting = failures
}
func hasFinalizedChunkForDedupe() -> Bool {
hasFinalizedAnyChunk
}
// MARK: - Frame Processing
/// Add a frame to the buffer. Returns encoded info if this frame completed a chunk.
func addFrame(image: CGImage, timestamp: Date) async throws -> EncodedFrame? {
guard isInitialized, let videosDir = videosDirectory else {
throw RewindError.storageError("VideoChunkEncoder not initialized")
}
let newFrameSize = CGSize(width: image.width, height: image.height)
// SAFETY: Check if buffer exceeded max size (memory leak prevention)
if frameTimestamps.count >= maxBufferFrames {
log("VideoChunkEncoder: Buffer exceeded \(maxBufferFrames) frames, forcing flush to prevent memory leak")
logError("VideoChunkEncoder: Emergency buffer flush triggered - \(frameTimestamps.count) frames")
if let abandonment = try await emergencyReset(reason: "buffer_overflow") {
throw abandonment
}
}
// Check if aspect ratio changed significantly.
// Debounce: require the new ratio to be stable for aspectRatioStabilityDelay before
// switching chunks. Rapid app switching (e.g. Firefox ↔ terminal) previously caused
// bursts of 1-3 frame chunks whose hevc_videotoolbox encoder hung on finalization,
// blocking the actor for 10s per event and chaining into a cascade.
if let currentInputSize = currentChunkInputSize,
hasSignificantAspectRatioChange(from: currentInputSize, to: newFrameSize)
{
let now = Date()
let pendingMatches =
pendingAspectRatioSize.map {
!hasSignificantAspectRatioChange(from: $0, to: newFrameSize)
} ?? false
if pendingMatches,
let since = pendingAspectRatioSince,
now.timeIntervalSince(since) >= aspectRatioStabilityDelay
{
// New aspect ratio has been stable for the required delay — commit the switch
log(
"VideoChunkEncoder: Aspect ratio changed significantly (\(currentInputSize) -> \(newFrameSize)), starting new chunk"
)
pendingAspectRatioSize = nil
pendingAspectRatioSince = nil
// Finalization can suspend while AVFoundation finishes the old writer.
// A cancellation/restart that wins during that suspension owns the
// encoder now, so this stale frame must not continue into the new chunk.
guard try await finalizeCurrentChunk() else {
return nil
}
} else {
// Not yet stable — record/refresh the pending candidate and drop this frame
if !pendingMatches {
pendingAspectRatioSize = newFrameSize
pendingAspectRatioSince = now
}
return nil
}
} else {
// Aspect ratio matches current chunk — clear any pending transition
pendingAspectRatioSize = nil
pendingAspectRatioSince = nil
}
// Start new chunk if needed
if currentChunkStartTime == nil {
currentChunkStartTime = timestamp
currentChunkFrameRate = frameRate // freeze for this chunk's whole lifetime
let chunkPath = Self.generateChunkPath(for: timestamp)
currentChunkPath = chunkPath
frameOffsetInChunk = 0
currentChunkInputSize = newFrameSize
// Start native HEVC writer for this chunk
do {
chunkLifecycle.install(
try startVideoWriter(
for: chunkPath,
videosDir: videosDir,
imageSize: newFrameSize
))
consecutiveWriteFailures = 0 // Reset on successful start
writerNotReadyCount = 0
encoderRestartCount += 1
} catch {
consecutiveWriteFailures += 1
logError(
"VideoChunkEncoder: Failed to start video writer (\(consecutiveWriteFailures)/\(maxConsecutiveFailures))",
error: error,
context: StorageFailureDiagnostics.context(
pathClass: "video-chunk",
containingURL: videosDir,
databaseURL: nil,
error: error,
appIsTerminating: RewindDatabase.isTerminationInProgress))
// Reset the half-initialized chunk state so the NEXT frame cleanly retries
// starting the writer. Without this, currentChunkStartTime stays set while
// writer state is nil, so every subsequent frame skips the start path and
// fails inside writeFrame() with "Video writer not ready" — needlessly dropping
// ~5 frames (2.5s of Rewind footage) and emitting misleading write-failure
// errors until the emergency-reset threshold finally clears the state.
currentChunkStartTime = nil
currentChunkFrameRate = nil
currentChunkPath = nil
_ = chunkLifecycle.reset()
currentChunkInputSize = nil
currentOutputSize = nil
frameOffsetInChunk = 0
if consecutiveWriteFailures >= maxConsecutiveFailures {
logError("VideoChunkEncoder: Too many video writer failures, performing emergency reset")
if let abandonment = try await emergencyReset(reason: "writer_start_failure") {
throw abandonment
}
}
throw error
}
}
guard let reservation = chunkLifecycle.activeReservation else {
return nil
}
// Write frame to the encoder FIRST (CGImage not stored after this). Only a
// successful write may consume a frame index: if the write throws, the
// caller skips the DB insert for this frame, so advancing frameOffsetInChunk
// / appending a timestamp here would desync every later frame in the chunk
// by one (DB frameOffset N pointing at real sample N-1, and the last record
// pointing past the end of the video). Record the frame only after success.
do {
guard try await writeFrame(image: image, reservation: reservation) else {
return nil
}
guard chunkLifecycle.isWriting(reservation) else {
return nil
}
consecutiveWriteFailures = 0 // Reset on successful write
resetStalenessTimer()
} catch let abandonment as RewindAbandonedVideoChunkError {
throw abandonment
} catch {
// `waitForWriterInputReady` can suspend. If a cancel/restart replaced
// this writer while it was waiting, the old call must not increment or
// emergency-reset the replacement writer.
guard chunkLifecycle.isWriting(reservation) else {
return nil
}
consecutiveWriteFailures += 1
logError(
"VideoChunkEncoder: Failed to write frame (\(consecutiveWriteFailures)/\(maxConsecutiveFailures))",
error: error,
context: StorageFailureDiagnostics.context(
pathClass: "video-chunk",
containingURL: videosDir,
databaseURL: nil,
error: error,
appIsTerminating: RewindDatabase.isTerminationInProgress)
)
if consecutiveWriteFailures >= maxConsecutiveFailures {
logError("VideoChunkEncoder: Too many write failures, performing emergency reset")
if let abandonment = try await emergencyReset(reason: "write_failure") {
throw abandonment
}
}
throw error
}
// Write succeeded — now record the timestamp (CGImage is not retained) and
// claim this frame's offset, so offsets match real encoded sample indices.
frameTimestamps.append(timestamp)
let frameInfo = EncodedFrame(
videoChunkPath: currentChunkPath!,
frameOffset: frameOffsetInChunk,
timestamp: timestamp
)
frameOffsetInChunk += 1
// Check if chunk duration exceeded.
// First chunk uses the shorter `firstChunkDuration` so Rewind starts showing
// frames within ~5s of launch instead of waiting out the full 60s window.
let effectiveDuration = hasFinalizedAnyChunk ? chunkDuration : firstChunkDuration
if let startTime = currentChunkStartTime,
timestamp.timeIntervalSince(startTime) >= effectiveDuration
{
// Finalize current chunk
guard try await finalizeCurrentChunk() else {
return nil
}
hasFinalizedAnyChunk = true
return frameInfo
}
return frameInfo
}
/// Force flush current buffer (app termination, etc.)
func flushCurrentChunk() async throws -> ChunkFlushResult? {
guard let chunkPath = currentChunkPath,
!frameTimestamps.isEmpty,
let reservation = chunkLifecycle.activeReservation
else {
return nil
}
let frames = frameTimestamps.enumerated().map { index, timestamp in
EncodedFrame(
videoChunkPath: chunkPath,
frameOffset: index,
timestamp: timestamp
)
}
// A second flush can arrive while the owner is suspended in
// `finishWriting`. Joining that completion makes shutdown wait until the
// MP4 trailer is durable instead of incorrectly reporting an empty buffer.
if inFlightFinalization?.reservation == reservation {
guard try await joinInFlightFinalization(reservation) else {
throw RewindError.storageError("Video chunk finalization was cancelled before flush completed")
}
return ChunkFlushResult(videoChunkPath: chunkPath, frames: frames)
}
guard try await finalizeCurrentChunk() else {
throw RewindError.storageError("Video chunk finalization was cancelled before flush completed")
}
return ChunkFlushResult(videoChunkPath: chunkPath, frames: frames)
}
// MARK: - Video Writer Management
private func startVideoWriter(
for relativePath: String,
videosDir: URL,
imageSize: CGSize
) throws -> RewindVideoChunkReservation {
try RewindVideoDirectoryMutation.startActiveChunk(at: relativePath) {
try startVideoWriterLocked(for: relativePath, videosDir: videosDir, imageSize: imageSize)
}
}
/// Holds the shared directory mutation lock through day-directory creation and
/// `AVAssetWriter.startWriting()`. Retention cleanup can otherwise remove an
/// empty just-created day directory before the writer materializes its file.
private func startVideoWriterLocked(for relativePath: String, videosDir: URL, imageSize: CGSize) throws {
// Create day subdirectory if needed
let components = relativePath.components(separatedBy: "/")
if components.count > 1 {
let dayDir = videosDir.appendingPathComponent(components[0], isDirectory: true)
try FileManager.default.createDirectory(at: dayDir, withIntermediateDirectories: true)
}
let fullPath = videosDir.appendingPathComponent(relativePath)
// Calculate output size (maintain aspect ratio, max 3000)
let outputSize = calculateOutputSize(for: imageSize)
currentOutputSize = outputSize
if FileManager.default.fileExists(atPath: fullPath.path) {
try FileManager.default.removeItem(at: fullPath)
}
let width = Int(outputSize.width)
let height = Int(outputSize.height)
let bitrate = estimatedHEVCBitrate(width: width, height: height)
let writer = try AVAssetWriter(outputURL: fullPath, fileType: .mp4)
writer.shouldOptimizeForNetworkUse = true
let settings = Self.hevcVideoSettings(
width: width,
height: height,
bitrate: bitrate,
frameRate: frameRate
)
let input = AVAssetWriterInput(mediaType: .video, outputSettings: settings)
input.expectsMediaDataInRealTime = true
guard writer.canAdd(input) else {
throw RewindError.storageError("Cannot add HEVC writer input")
}
writer.add(input)
let adaptor = AVAssetWriterInputPixelBufferAdaptor(
assetWriterInput: input,
sourcePixelBufferAttributes: [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
kCVPixelBufferWidthKey as String: width,
kCVPixelBufferHeightKey as String: height,
kCVPixelBufferIOSurfacePropertiesKey as String: [:],
]
)
guard writer.startWriting() else {
if let writerError = writer.error {
throw RewindError.storageWriteFailed("Failed to start HEVC writer", underlying: writerError)
}
throw RewindError.storageError("Failed to start HEVC writer: unknown error")
}
writer.startSession(atSourceTime: .zero)
assetWriter = writer
writerInput = input
pixelBufferAdaptor = adaptor
log("VideoChunkEncoder: Started native HEVC writer for chunk at \(relativePath)")
// Log frame dimensions to Sentry for debugging user quality issues
let breadcrumb = Breadcrumb(level: .info, category: "video_encoder")
breadcrumb.message = "Started video chunk encoding"
breadcrumb.data = [
"chunk_path": relativePath,
"input_width": Int(imageSize.width),
"input_height": Int(imageSize.height),
"output_width": Int(outputSize.width),
"output_height": Int(outputSize.height),
"quality": 65,
"estimated_bitrate": bitrate,
"encoder": "AVAssetWriter.hevc",
"input_format": "cvpixelbuffer_bgra",
"max_resolution": Int(maxResolution),
]
SentrySDK.addBreadcrumb(breadcrumb)
}
/// HEVC's VideoToolbox encoder rejects
/// `AVVideoMaxKeyFrameIntervalDurationKey` on some Intel/macOS combinations
/// (including hvc1), terminating the app while constructing the writer input.
/// Keep the common HEVC settings in one testable factory and omit that
/// unsupported property; keyframe cadence is encoder-controlled instead.
nonisolated static func hevcVideoSettings(
width: Int,
height: Int,
bitrate: Int,
frameRate: Double
) -> [String: Any] {
[
AVVideoCodecKey: AVVideoCodecType.hevc,
AVVideoWidthKey: width,
AVVideoHeightKey: height,
AVVideoCompressionPropertiesKey: [
AVVideoAverageBitRateKey: bitrate,
AVVideoExpectedSourceFrameRateKey: max(1, Int(ceil(frameRate))),
AVVideoAllowFrameReorderingKey: false,
],
]
}
/// Presentation timestamp (in seconds) for the frame at `frameOffset` within a chunk.
/// Callers pass the offset of the frame being written now; the writer requires strictly
/// increasing timestamps, so this must be a strictly increasing function of frameOffset
/// for 0, 1, 2, … `nonisolated static` so it is synchronously unit-testable.
nonisolated static func framePresentationSeconds(frameOffset: Int, frameRate: Double) -> Double {
Double(max(0, frameOffset)) / frameRate
}
/// The frame rate a chunk's presentation timestamps are computed against: the
/// rate frozen when the chunk started, falling back to the live rate only
/// before the first frame. Using a single rate for the whole chunk keeps
/// `framePresentationSeconds(frameOffset:)` strictly increasing even when the
/// live capture rate changes mid-chunk (e.g. AC power connected). `nonisolated
/// static` so it is synchronously unit-testable.
nonisolated static func chunkPresentationFrameRate(frozen: Double?, live: Double) -> Double {
frozen ?? live
}
/// Appends a frame only while `reservation` still owns a writing-phase
/// chunk. `false` means a cancel, restart, or finalization won while this
/// method was suspended and the caller must not touch replacement state.
private func writeFrame(image: CGImage, reservation: RewindVideoChunkReservation) async throws -> Bool {
guard chunkLifecycle.isWriting(reservation) else {
return false
}
guard let input = writerInput,
let adaptor = pixelBufferAdaptor,
let outputSize = currentOutputSize
else {
writerNotReadyCount += 1
if writerNotReadyCount >= maxConsecutiveNotReadyFailures {
logError("VideoChunkEncoder: Video writer not ready \(writerNotReadyCount)x, resetting encoder state")
if let abandonment = try await emergencyReset(reason: "writer_not_ready_loop") {
throw abandonment
}
} else {
log("VideoChunkEncoder: Video writer not ready (\(writerNotReadyCount)/\(maxConsecutiveNotReadyFailures))")
}
throw RewindError.storageError("Video writer not ready")
}
guard try await waitForWriterInputReady(input, reservation: reservation) else {
return false
}
guard chunkLifecycle.isWriting(reservation) else {
return false
}
// Keep this actor-isolated: autoreleasepool's closure is treated as a sending
// boundary by Swift 6, which cannot safely retain the AVFoundation adaptor.
let pixelBuffer = try Self.createPixelBuffer(from: image, size: outputSize, adaptor: adaptor)
// frameOffsetInChunk is the index of the frame being written RIGHT NOW; it is
// incremented in addFrame only AFTER this write succeeds (so a failed write does
// not consume an offset). The presentation timestamp must therefore be derived
// from the current offset directly. A stale `- 1` here (left over from when the
// offset was pre-incremented before writeFrame) makes frame 0 and frame 1 both
// resolve to PTS 0, and AVAssetWriterInputPixelBufferAdaptor.append requires
// strictly increasing timestamps — so every chunk's second frame is rejected,
// failures cascade to an emergency reset, and no video chunk is ever persisted.
let presentationTime = CMTime(
seconds: Self.framePresentationSeconds(
frameOffset: frameOffsetInChunk,
frameRate: Self.chunkPresentationFrameRate(frozen: currentChunkFrameRate, live: frameRate)
),
preferredTimescale: 600
)
if let successfulFrames = appendFailureAfterSuccessfulFramesForTesting {
guard successfulFrames > 0 else {
throw RewindError.storageError("Injected append failure for abandoned-chunk recovery test")
}
appendFailureAfterSuccessfulFramesForTesting = successfulFrames - 1
}
guard adaptor.append(pixelBuffer, withPresentationTime: presentationTime) else {
if let writerError = assetWriter?.error {
throw RewindError.storageWriteFailed("Failed to append frame to HEVC writer", underlying: writerError)
}
throw RewindError.storageError("Failed to append frame to HEVC writer: unknown error")
}
writerNotReadyCount = 0
return true
}
/// Finalize the writer that owned the encoder at entry.
///
/// `finishWriting` suspends this actor. A cancellation can therefore install a
/// newer writer before this continuation resumes. Returning `false` tells the
/// caller that its captured writer lost ownership, so it must not mutate the
/// newer chunk after the await.
private func finalizeCurrentChunk() async throws -> Bool {
stalenessCheckTask?.cancel()
stalenessCheckTask = nil
guard let reservation = chunkLifecycle.activeReservation,
chunkLifecycle.beginFinalization(of: reservation)
else {
return false
}
guard let input = writerInput, let writer = assetWriter else {
return resetCurrentChunkState(onlyIfCurrent: reservation)
}
let frameCount = frameTimestamps.count
input.markAsFinished()
inFlightFinalization = InFlightFinalization(reservation: reservation)
if let beforeFinishWritingForTesting {
await beforeFinishWritingForTesting()
}
guard chunkLifecycle.owns(reservation) else {
resolveInFlightFinalization(reservation, with: .success(false))
return false
}
do {
try await finishWriting(writer)
} catch {
// A cancellation/restart may have replaced this writer while its finish
// callback was pending. That stale completion is an expected no-op for
// the caller rather than an error from the newer generation.
guard chunkLifecycle.owns(reservation) else {
resolveInFlightFinalization(reservation, with: .success(false))
return false
}
// A failed finish may leave an MP4 without its trailer. Record its exact
// path before cancelling the writer so recovery survives a crash between
// this failure and the indexer/storage cleanup.
switch cancelCurrentChunkAfterRecordingAbandonment() {
case .markerRecorded(let abandonedReservation):
let abandonment = RewindAbandonedVideoChunkError(relativePath: abandonedReservation.relativePath)
resolveInFlightFinalization(reservation, with: .failure(abandonment))
throw abandonment
case .markerWriteFailed(let failedReservation):
let markerFailure = RewindAbandonedVideoChunkMarkerWriteError(reservation: failedReservation)
resolveInFlightFinalization(reservation, with: .failure(markerFailure))
throw markerFailure
case .noActiveChunk:
resolveInFlightFinalization(reservation, with: .success(false))
return false
}
}
guard resetCurrentChunkState(onlyIfCurrent: reservation) else {
resolveInFlightFinalization(reservation, with: .success(false))
return false
}
resolveInFlightFinalization(reservation, with: .success(true))
log(
"VideoChunkEncoder: Finalized chunk with \(frameCount) frames (restartCount=\(encoderRestartCount), emergencyResets=\(emergencyResetCount))"
)
return true
}
/// Joins the active writer's trailer write without cancelling it. A flush is
/// a durability boundary; explicit `cancel()` remains the only path that can
/// abandon a finalization and resolve its waiters with `false`.
private func joinInFlightFinalization(_ reservation: RewindVideoChunkReservation) async throws -> Bool {
guard inFlightFinalization?.reservation == reservation else {
return false
}
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Bool, Error>) in
guard var finalization = inFlightFinalization, finalization.reservation == reservation else {
continuation.resume(returning: false)
return
}
finalization.waiters.append(continuation)
inFlightFinalization = finalization
finalizationJoinedForTesting?()
}
}
/// Resolves every flush that joined this exact writer. Clearing the stored
/// continuations before resuming them prevents a resumed caller from joining
/// the completed generation again.
private func resolveInFlightFinalization(
_ reservation: RewindVideoChunkReservation,
with result: Result<Bool, Error>
) {
guard let finalization = inFlightFinalization, finalization.reservation == reservation else {
return
}
inFlightFinalization = nil
finalization.waiters.forEach { $0.resume(with: result) }
}
/// Clears encoder state only if the expected reservation still owns it.
/// This guard must run before changing any field: a stale finalizer is not
/// allowed to touch a newer writer after an actor reentrancy point.
@discardableResult
private func resetCurrentChunkState(onlyIfCurrent expectedReservation: RewindVideoChunkReservation? = nil) -> Bool {
if let expectedReservation, !chunkLifecycle.owns(expectedReservation) {
return false
}
let activeChunkReservation = chunkLifecycle.reset()
// Reset state (timestamps only - no CGImages retained)
frameTimestamps.removeAll()
currentChunkStartTime = nil
currentChunkFrameRate = nil
currentChunkPath = nil
frameOffsetInChunk = 0
currentOutputSize = nil
currentChunkInputSize = nil
assetWriter = nil
writerInput = nil
pixelBufferAdaptor = nil
consecutiveWriteFailures = 0
pendingAspectRatioSize = nil
pendingAspectRatioSince = nil
RewindVideoDirectoryMutation.finishActiveChunk(activeChunkReservation)
return true
}
// MARK: - Staleness Detection
/// Reset the staleness timer after each successful frame write.
/// If no new frame arrives within chunkDuration + 10s, finalize the chunk
/// to release the H.265 hardware encoder context.
private func resetStalenessTimer() {
stalenessCheckTask?.cancel()
guard let reservation = chunkLifecycle.activeReservation,
chunkLifecycle.isWriting(reservation)
else {
stalenessCheckTask = nil
return
}
let timeout = chunkDuration + 10.0
stalenessCheckTask = Task { [weak self, reservation] in
try? await Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000))
guard !Task.isCancelled else { return }
await self?.finalizeStaleChunkIfNeeded(onlyIfCurrent: reservation)
}
}
/// Finalize a chunk that has gone stale (no new frames for longer than chunk duration).
private func finalizeStaleChunkIfNeeded(onlyIfCurrent reservation: RewindVideoChunkReservation) async {
guard chunkLifecycle.isWriting(reservation) else { return }
guard let startTime = currentChunkStartTime else { return }
let age = Date().timeIntervalSince(startTime)
guard age >= chunkDuration else { return }
log(
"VideoChunkEncoder: Stale chunk detected (age: \(String(format: "%.0f", age))s, no new frames) — finalizing to release encoder resources"
)
let breadcrumb = Breadcrumb(level: .warning, category: "video_encoder")
breadcrumb.message = "Stale chunk finalized"
breadcrumb.data = [
"age_seconds": Int(age),
"frame_count": frameTimestamps.count,
"chunk_path": currentChunkPath ?? "none",
]
SentrySDK.addBreadcrumb(breadcrumb)
do {
_ = try await finalizeCurrentChunk()
} catch {
do {
let recovered = try await RewindStorage.shared.recoverAbandonedVideoChunkIfNeeded(error)
if !recovered {
logError("VideoChunkEncoder: Failed to finalize stale video chunk", error: error)
}
} catch {
logError("VideoChunkEncoder: Failed to recover stale video chunk", error: error)
}
}
}
func finalizeStaleChunkForTesting() async {
guard let reservation = chunkLifecycle.activeReservation else { return }
await finalizeStaleChunkIfNeeded(onlyIfCurrent: reservation)
}
// MARK: - Helpers
/// Builds a collision-proof chunk path while keeping absolute capture time
/// in the filename for recovery. The UUID prevents a cancel/restart within
/// one millisecond from overwriting a prior durable chunk.
nonisolated static func generateChunkPath(for timestamp: Date, uniqueID: UUID = UUID()) -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = .current
dateFormatter.dateFormat = "yyyy-MM-dd"
let dayString = dateFormatter.string(from: timestamp)
let timeFormatter = DateFormatter()
timeFormatter.locale = Locale(identifier: "en_US_POSIX")
timeFormatter.timeZone = .current
timeFormatter.dateFormat = "HHmmss"
let timeString = timeFormatter.string(from: timestamp)
let epochMilliseconds = Int64((timestamp.timeIntervalSince1970 * 1_000).rounded(.down))
return "\(dayString)/chunk_\(timeString)_\(epochMilliseconds)_\(uniqueID.uuidString.lowercased()).mp4"
}
/// Check if aspect ratio changed significantly between two sizes
private func hasSignificantAspectRatioChange(from oldSize: CGSize, to newSize: CGSize) -> Bool {
guard oldSize.height > 0 && newSize.height > 0 else { return true }
let oldAspect = oldSize.width / oldSize.height
let newAspect = newSize.width / newSize.height
// Calculate the relative difference in aspect ratio
let aspectDiff = abs(oldAspect - newAspect) / max(oldAspect, newAspect)
return aspectDiff > aspectRatioChangeThreshold
}
private func calculateOutputSize(for size: CGSize) -> CGSize {
let maxDimension = max(size.width, size.height)
if maxDimension <= maxResolution {
// Round to even numbers (required by video codecs)
return CGSize(
width: CGFloat(Int(size.width) / 2 * 2),
height: CGFloat(Int(size.height) / 2 * 2)
)
}
let scale = maxResolution / maxDimension
let newWidth = Int(size.width * scale) / 2 * 2
let newHeight = Int(size.height * scale) / 2 * 2
return CGSize(width: CGFloat(newWidth), height: CGFloat(newHeight))
}
private func estimatedHEVCBitrate(width: Int, height: Int) -> Int {
let bitsPerPixelFrame = 0.35
let bitrate = Double(width * height) * max(frameRate, 1.0) * bitsPerPixelFrame
return max(350_000, min(8_000_000, Int(bitrate)))
}
private nonisolated static func createPixelBuffer(
from image: CGImage,
size: CGSize,
adaptor: AVAssetWriterInputPixelBufferAdaptor
) throws -> CVPixelBuffer {
let width = Int(size.width)
let height = Int(size.height)
guard width > 0, height > 0 else {
throw RewindError.storageError("Invalid pixel buffer size \(size)")
}
let pixelBuffer: CVPixelBuffer
if let pool = adaptor.pixelBufferPool {
var pooledBuffer: CVPixelBuffer?
let status = CVPixelBufferPoolCreatePixelBuffer(nil, pool, &pooledBuffer)
guard status == kCVReturnSuccess, let pooledBuffer else {
throw RewindError.storageError("Failed to create pooled pixel buffer: \(status)")
}
pixelBuffer = pooledBuffer
} else {
var createdBuffer: CVPixelBuffer?
let status = CVPixelBufferCreate(
nil,
width,
height,
kCVPixelFormatType_32BGRA,
[
kCVPixelBufferIOSurfacePropertiesKey as String: [:]
] as CFDictionary,
&createdBuffer
)
guard status == kCVReturnSuccess, let createdBuffer else {
throw RewindError.storageError("Failed to create pixel buffer: \(status)")