Skip to content

Commit

Permalink
added a away feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wkaisertexas committed Mar 13, 2024
1 parent 7dd400a commit 30327f8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
3 changes: 2 additions & 1 deletion TimeLapze/Camera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Camera: NSObject, Recordable {
var offset: CMTime = CMTime(seconds: 0.0, preferredTimescale: 60)
var timeMultiple: Double = 1 // offset set based on settings
var frameCount: Int = 0
var frameChanged = true

var lastAppenedFrame: CMTime = .zero
var tmpFrameBuffer: CMSampleBuffer?
Expand Down Expand Up @@ -177,7 +178,7 @@ class Camera: NSObject, Recordable {
return
}

(tmpFrameBuffer, lastAppenedFrame) = appendBuffer(buffer: buffer)
(tmpFrameBuffer, lastAppenedFrame, frameChanged) = appendBuffer(buffer: buffer, source: .camera)

// log frame count
frameCount += 1
Expand Down
47 changes: 38 additions & 9 deletions TimeLapze/Recordable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protocol Recordable : CustomStringConvertible {

var lastAppenedFrame: CMTime {get set}
var tmpFrameBuffer: CMSampleBuffer? {get set}
var frameChanged: Bool {get set}
var frameRate: CMTimeScale {get}

// MARK: -Intents
Expand All @@ -29,7 +30,7 @@ protocol Recordable : CustomStringConvertible {
func getFilename() -> String
}

extension Recordable{
extension Recordable{
var frameRate: CMTimeScale {
guard let writer = writer else {return .zero}
return CMTimeScale(30.0)
Expand Down Expand Up @@ -114,33 +115,54 @@ extension Recordable{
/// Appends a buffer depending on a couple of factors
/// The `tmpFrameBuffer` is used to keep track of deletable buffers
/// Saves **30%** of space at only **2x** speed. Austensibly much higher for higher time multiples
func appendBuffer(buffer: CMSampleBuffer) -> (CMSampleBuffer, CMTime){
guard let input = input else { return (buffer, lastAppenedFrame) }
func appendBuffer(buffer: CMSampleBuffer, source: InputTypes) -> (CMSampleBuffer, CMTime, Bool){
guard let input = input else { return (buffer, lastAppenedFrame, true)}

// Determines if we should append
let currentPTS = buffer.presentationTimeStamp

let differenceTime = CMTimeMultiplyByFloat64(CMTime(seconds: 1.0 / 30, preferredTimescale: 30), multiplier: timeMultiple)

guard currentPTS > lastAppenedFrame + differenceTime else {
var changed = frameChanged
switch source {
case .camera:
changed = true // a camera is always changed
case .screen:
// needs to get the attachements array
if !changed, let attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(buffer,
createIfNecessary: false) as? [[SCStreamFrameInfo: Any]],
let attachments = attachmentsArray.first {
// okay, so we have attachments
if let rects = attachments[.dirtyRects] as? NSArray, rects.count > 0 {
changed = true // if we have dirty rects, something changed
}
} else {
// if we can not extract, then changed MUST be true
changed = true
}
default:
logger.warning("Unrecognized input device")
}

guard currentPTS > lastAppenedFrame + differenceTime || (source == .screen && !frameChanged) else {
// okay to replace the tmp buffer
return (buffer, lastAppenedFrame)
return (buffer, lastAppenedFrame, changed)
}

guard let newBuffer = try? tmpFrameBuffer?.offsettingTiming(by: offset, multiplier: 1.0 / timeMultiple) else {
return (buffer, lastAppenedFrame)
return (buffer, lastAppenedFrame, true)
}

guard input.append(newBuffer) else {
logger.error("failed to append data")
return (buffer, lastAppenedFrame)
return (buffer, lastAppenedFrame, true)
}

if let tmpFrameBuffer = tmpFrameBuffer{
return (buffer, tmpFrameBuffer.presentationTimeStamp)
return (buffer, tmpFrameBuffer.presentationTimeStamp, source != .screen) // we have not changed originally
} else {
// Initial condition
return (buffer, buffer.presentationTimeStamp)
return (buffer, buffer.presentationTimeStamp, source != .screen)
}

}
Expand Down Expand Up @@ -203,3 +225,10 @@ extension URL {
return self.absoluteString.starts(with: URL.temporaryDirectory.absoluteString)
}
}

/// Two-type input types
/// Used to not record non-changing frames
enum InputTypes {
case camera
case screen
}
3 changes: 2 additions & 1 deletion TimeLapze/Screen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Screen: NSObject, SCStreamOutput, Recordable {
var offset: CMTime = CMTime(seconds: 0.0, preferredTimescale: 60)
var timeMultiple: Double = 1 // offset set based on settings
var frameCount: Int = 0
var frameChanged = true

var lastAppenedFrame: CMTime = .zero
var tmpFrameBuffer: CMSampleBuffer?
Expand Down Expand Up @@ -266,7 +267,7 @@ class Screen: NSObject, SCStreamOutput, Recordable {
return
}

(tmpFrameBuffer, lastAppenedFrame) = appendBuffer(buffer: buffer)
(tmpFrameBuffer, lastAppenedFrame, frameChanged) = appendBuffer(buffer: buffer, source: .screen)

// Logs the frames
frameCount += 1
Expand Down

0 comments on commit 30327f8

Please sign in to comment.