Skip to content

Commit

Permalink
Upgrade to Swift 4
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcampoli committed Nov 15, 2017
1 parent 2683a19 commit ad1a932
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 76 deletions.
46 changes: 32 additions & 14 deletions framework/GPUImage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0820;
LastUpgradeCheck = 0800;
LastUpgradeCheck = 0910;
ORGANIZATIONNAME = "Sunset Lake Software LLC";
TargetAttributes = {
BC6E7CAA1C39A9D8006DF678 = {
Expand Down Expand Up @@ -1753,16 +1753,24 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "";
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = dwarf;
Expand All @@ -1789,7 +1797,7 @@
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -1803,16 +1811,24 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "";
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
Expand All @@ -1831,7 +1847,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.9;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -1855,7 +1871,7 @@
PRODUCT_NAME = GPUImage;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -1876,8 +1892,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.sunsetlakesoftware.GPUImage;
PRODUCT_NAME = GPUImage;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 3.0;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -1890,6 +1906,7 @@
OTHER_SWIFT_FLAGS = "-DGL";
PRODUCT_BUNDLE_IDENTIFIER = "com.sunsetlakesoftware.GPUImage-MacTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -1903,6 +1920,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.sunsetlakesoftware.GPUImage-MacTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -1914,7 +1932,7 @@
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
Expand All @@ -1930,7 +1948,7 @@
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -1943,7 +1961,7 @@
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
Expand All @@ -1959,7 +1977,7 @@
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand All @@ -1983,7 +2001,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -2005,7 +2023,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand Down
44 changes: 27 additions & 17 deletions framework/Source/Mac/Camera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ public class Camera: NSObject, ImageSource, AVCaptureVideoDataOutputSampleBuffer
let captureAsYUV:Bool
let yuvConversionShader:ShaderProgram?
let frameRenderingSemaphore = DispatchSemaphore(value:1)
let cameraProcessingQueue = DispatchQueue.global(priority:standardProcessingQueuePriority)
let audioProcessingQueue = DispatchQueue.global(priority:lowProcessingQueuePriority)
let cameraProcessingQueue = standardProcessingQueue
let audioProcessingQueue = lowProcessingQueue

var numberOfFramesCaptured = 0
var totalFrameTimeDuringCapture:Double = 0.0
var framesSinceLastCheck = 0
var lastCheckTime = CFAbsoluteTimeGetCurrent()

public init(sessionPreset:String, cameraDevice:AVCaptureDevice? = nil, orientation:ImageOrientation = .portrait, captureAsYUV:Bool = true) throws {
self.inputCamera = cameraDevice ?? AVCaptureDevice.defaultDevice(withMediaType:AVMediaTypeVideo)
public init(sessionPreset:AVCaptureSession.Preset, cameraDevice:AVCaptureDevice? = nil, orientation:ImageOrientation = .portrait, captureAsYUV:Bool = true) throws {

guard let inputCamera = cameraDevice ?? AVCaptureDevice.default(for: AVMediaType.video) else {
fatalError("Failed to get input camera.")
}
self.inputCamera = inputCamera

self.orientation = orientation
self.captureAsYUV = captureAsYUV

Expand All @@ -70,23 +75,23 @@ public class Camera: NSObject, ImageSource, AVCaptureVideoDataOutputSampleBuffer

if captureAsYUV {
supportsFullYUVRange = false
let supportedPixelFormats = videoOutput.availableVideoCVPixelFormatTypes
for currentPixelFormat in supportedPixelFormats! {
if ((currentPixelFormat as! NSNumber).int32Value == Int32(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)) {
let supportedPixelFormats = videoOutput.availableVideoPixelFormatTypes
for currentPixelFormat in supportedPixelFormats {
if ((currentPixelFormat as NSNumber).int32Value == Int32(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)) {
supportsFullYUVRange = true
}
}

if (supportsFullYUVRange) {
yuvConversionShader = crashOnShaderCompileFailure("Camera"){try sharedImageProcessingContext.programForVertexShader(defaultVertexShaderForInputs(2), fragmentShader:YUVConversionFullRangeFragmentShader)}
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable:NSNumber(value:Int32(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange))]
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String:NSNumber(value:Int32(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange))]
} else {
yuvConversionShader = crashOnShaderCompileFailure("Camera"){try sharedImageProcessingContext.programForVertexShader(defaultVertexShaderForInputs(2), fragmentShader:YUVConversionVideoRangeFragmentShader)}
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable:NSNumber(value:Int32(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange))]
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String:NSNumber(value:Int32(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange))]
}
} else {
yuvConversionShader = nil
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable:NSNumber(value:Int32(kCVPixelFormatType_32BGRA))]
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String:NSNumber(value:Int32(kCVPixelFormatType_32BGRA))]
}

if (captureSession.canAddOutput(videoOutput)) {
Expand All @@ -108,8 +113,8 @@ public class Camera: NSObject, ImageSource, AVCaptureVideoDataOutputSampleBuffer
}
}

public func captureOutput(_ captureOutput:AVCaptureOutput!, didOutputSampleBuffer sampleBuffer:CMSampleBuffer!, from connection:AVCaptureConnection!) {
guard (captureOutput != audioOutput) else {
public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard (output != audioOutput) else {
self.processAudioSampleBuffer(sampleBuffer)
return
}
Expand Down Expand Up @@ -208,22 +213,27 @@ public class Camera: NSObject, ImageSource, AVCaptureVideoDataOutputSampleBuffer
// MARK: Audio processing

func addAudioInputsAndOutputs() throws {
guard (audioOutput == nil) else { return }
guard (self.audioOutput == nil) else { return }

captureSession.beginConfiguration()
defer {
captureSession.commitConfiguration()
}
microphone = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
audioInput = try AVCaptureDeviceInput(device:microphone)
guard let microphone = AVCaptureDevice.default(for: .audio) else {
return
}
let audioInput = try AVCaptureDeviceInput(device:microphone)
if captureSession.canAddInput(audioInput) {
captureSession.addInput(audioInput)
}
audioOutput = AVCaptureAudioDataOutput()
let audioOutput = AVCaptureAudioDataOutput()
if captureSession.canAddOutput(audioOutput) {
captureSession.addOutput(audioOutput)
}
audioOutput?.setSampleBufferDelegate(self, queue:audioProcessingQueue)
self.microphone = microphone
self.audioInput = audioInput
self.audioOutput = audioOutput
audioOutput.setSampleBufferDelegate(self, queue:audioProcessingQueue)
}

func removeAudioInputsAndOutputs() {
Expand Down
6 changes: 3 additions & 3 deletions framework/Source/Mac/MovieInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MovieInput: ImageSource {
assetReader = try AVAssetReader(asset:self.asset)

let outputSettings:[String:AnyObject] = [(kCVPixelBufferPixelFormatTypeKey as String):NSNumber(value:Int32(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange))]
let readerVideoTrackOutput = AVAssetReaderTrackOutput(track:self.asset.tracks(withMediaType: AVMediaTypeVideo)[0], outputSettings:outputSettings)
let readerVideoTrackOutput = AVAssetReaderTrackOutput(track:self.asset.tracks(withMediaType: AVMediaType.video)[0], outputSettings:outputSettings)
readerVideoTrackOutput.alwaysCopiesSampleData = false
assetReader.add(readerVideoTrackOutput)
// TODO: Audio here
Expand All @@ -44,7 +44,7 @@ public class MovieInput: ImageSource {

public func start() {
asset.loadValuesAsynchronously(forKeys: ["tracks"], completionHandler: {
DispatchQueue.global(priority:standardProcessingQueuePriority).async {
standardProcessingQueue.async {
guard (self.asset.statusOfValue(forKey:"tracks", error:nil) == .loaded) else { return }

guard self.assetReader.startReading() else {
Expand All @@ -55,7 +55,7 @@ public class MovieInput: ImageSource {
var readerVideoTrackOutput:AVAssetReaderOutput? = nil;

for output in self.assetReader.outputs {
if(output.mediaType == AVMediaTypeVideo) {
if(output.mediaType == AVMediaType.video.rawValue) {
readerVideoTrackOutput = output;
}
}
Expand Down
6 changes: 3 additions & 3 deletions framework/Source/Mac/MovieOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MovieOutput: ImageConsumer, AudioEncodingTarget {
private var previousAudioTime = kCMTimeNegativeInfinity
private var encodingLiveVideo:Bool

public init(URL:Foundation.URL, size:Size, fileType:String = AVFileTypeQuickTimeMovie, liveVideo:Bool = false, settings:[String:AnyObject]? = nil) throws {
public init(URL:Foundation.URL, size:Size, fileType:AVFileType = .mov, liveVideo:Bool = false, settings:[String:AnyObject]? = nil) throws {
self.size = size
assetWriter = try AVAssetWriter(url:URL, fileType:fileType)
// Set this to make sure that a functional movie is produced, even if the recording is cut off mid-stream. Only the last second should be lost in that case.
Expand All @@ -39,7 +39,7 @@ public class MovieOutput: ImageConsumer, AudioEncodingTarget {
localSettings[AVVideoHeightKey] = localSettings[AVVideoHeightKey] ?? NSNumber(value:size.height)
localSettings[AVVideoCodecKey] = localSettings[AVVideoCodecKey] ?? AVVideoCodecH264 as NSString

assetWriterVideoInput = AVAssetWriterInput(mediaType:AVMediaTypeVideo, outputSettings:localSettings)
assetWriterVideoInput = AVAssetWriterInput(mediaType:AVMediaType.video, outputSettings:localSettings)
assetWriterVideoInput.expectsMediaDataInRealTime = liveVideo
encodingLiveVideo = liveVideo

Expand Down Expand Up @@ -149,7 +149,7 @@ public class MovieOutput: ImageConsumer, AudioEncodingTarget {

public func activateAudioTrack() {
// TODO: Add ability to set custom output settings
assetWriterAudioInput = AVAssetWriterInput(mediaType:AVMediaTypeAudio, outputSettings:nil)
assetWriterAudioInput = AVAssetWriterInput(mediaType:AVMediaType.audio, outputSettings:nil)
assetWriter.add(assetWriterAudioInput!)
assetWriterAudioInput?.expectsMediaDataInRealTime = encodingLiveVideo
}
Expand Down
1 change: 1 addition & 0 deletions framework/Source/Mac/PictureInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public class PictureInput: ImageSource {
}

public convenience init(imageName:String, smoothlyScaleOutput:Bool = false, orientation:ImageOrientation = .portrait) {
let imageName = NSImage.Name(rawValue: imageName)
guard let image = NSImage(named:imageName) else { fatalError("No such image named: \(imageName) in your application bundle") }
self.init(image:image.cgImage(forProposedRect:nil, context:nil, hints:nil)!, smoothlyScaleOutput:smoothlyScaleOutput, orientation:orientation)
}
Expand Down
4 changes: 2 additions & 2 deletions framework/Source/Mac/PictureOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public class PictureOutput: ImageConsumer {
let bitmapRepresentation = NSBitmapImageRep(cgImage:cgImageFromBytes)
let imageData:Data
switch encodedImageFormat {
case .png: imageData = bitmapRepresentation.representation(using: .PNG, properties: ["":""])!
case .jpeg: imageData = bitmapRepresentation.representation(using: .JPEG, properties: ["":""])!
case .png: imageData = bitmapRepresentation.representation(using: .png, properties: [NSBitmapImageRep.PropertyKey(rawValue: ""):""])!
case .jpeg: imageData = bitmapRepresentation.representation(using: .jpeg, properties: [NSBitmapImageRep.PropertyKey(rawValue: ""):""])!
}

imageCallback(imageData)
Expand Down
17 changes: 8 additions & 9 deletions framework/Source/SerialDispatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@ extension SerialDispatch {

#else

public let standardProcessingQueuePriority:DispatchQueue.GlobalQueuePriority = {
// DispatchQueue.QoSClass.default
public var standardProcessingQueue:DispatchQueue {
if #available(iOS 10, OSX 10.10, *) {
return DispatchQueue.GlobalQueuePriority.default
return DispatchQueue.global(qos: .default)
} else {
return DispatchQueue.GlobalQueuePriority.default
return DispatchQueue.global(priority: .default)
}
}
}()

public let lowProcessingQueuePriority:DispatchQueue.GlobalQueuePriority = {
public var lowProcessingQueue:DispatchQueue {
if #available(iOS 10, OSX 10.10, *) {
return DispatchQueue.GlobalQueuePriority.low
return DispatchQueue.global(qos: .background)
} else {
return DispatchQueue.GlobalQueuePriority.low
return DispatchQueue.global(priority: .low)
}
}
}()

func runAsynchronouslyOnMainQueue(_ mainThreadOperation:@escaping () -> ()) {
if (Thread.isMainThread) {
Expand Down
Loading

0 comments on commit ad1a932

Please sign in to comment.