Skip to content

Commit

Permalink
compile for Swift 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
msach22 committed Feb 12, 2019
1 parent c95e110 commit fad6908
Show file tree
Hide file tree
Showing 22 changed files with 103 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -23,4 +23,4 @@ DerivedData
#CocoaPods
Pods
*.xcworkspacedata
*.xcodeworkspace
*.xcworkspace
4 changes: 2 additions & 2 deletions Basic-Video-Chat/Basic-Video-Chat.xcodeproj/project.pbxproj
Expand Up @@ -278,7 +278,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -291,7 +291,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tokbox.Hello-World";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand Down
2 changes: 1 addition & 1 deletion Basic-Video-Chat/Basic-Video-Chat/AppDelegate.swift
Expand Up @@ -13,7 +13,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
}
Expand Down
Expand Up @@ -288,7 +288,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tokbox.--Custom-Audio-Driver";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -301,7 +301,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tokbox.--Custom-Audio-Driver";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand Down
2 changes: 1 addition & 1 deletion Custom-Audio-Driver/Custom-Audio-Driver/AppDelegate.swift
Expand Up @@ -13,7 +13,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}

Expand Down
48 changes: 24 additions & 24 deletions Custom-Audio-Driver/Custom-Audio-Driver/DefaultAudioDevice.swift
Expand Up @@ -56,8 +56,8 @@ class DefaultAudioDevice: NSObject {
fileprivate var recordingVoiceUnit: AudioUnit?
fileprivate var playoutVoiceUnit: AudioUnit?

fileprivate var previousAVAudioSessionCategory = ""
fileprivate var avAudioSessionMode = ""
fileprivate var previousAVAudioSessionCategory: AVAudioSession.Category?
fileprivate var avAudioSessionMode: AVAudioSession.Mode?
fileprivate var avAudioSessionPreffSampleRate = Double(0)
fileprivate var avAudioSessionChannels = 0
fileprivate var isAudioSessionSetup = false
Expand Down Expand Up @@ -244,7 +244,9 @@ class DefaultAudioDevice: NSObject {

let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(previousAVAudioSessionCategory)
guard let previousAVAudioSessionCategory = previousAVAudioSessionCategory else { return }
try session.setCategory(previousAVAudioSessionCategory, mode: .default)
guard let avAudioSessionMode = avAudioSessionMode else { return }
try session.setMode(avAudioSessionMode)
try session.setPreferredSampleRate(avAudioSessionPreffSampleRate)
try session.setPreferredInputNumberOfChannels(avAudioSessionChannels)
Expand Down Expand Up @@ -406,7 +408,7 @@ extension DefaultAudioDevice: OTAudioDevice {

// MARK: - AVAudioSession
extension DefaultAudioDevice {
func onInterruptionEvent(notification: Notification) {
@objc func onInterruptionEvent(notification: Notification) {
let type = notification.userInfo?[AVAudioSessionInterruptionTypeKey]
safetyQueue.async {
self.handleInterruptionEvent(type: type as? Int)
Expand All @@ -419,7 +421,7 @@ extension DefaultAudioDevice {
}

switch UInt(interruptionType) {
case AVAudioSessionInterruptionType.began.rawValue:
case AVAudioSession.InterruptionType.began.rawValue:
if recording {
isRecorderInterrupted = true
let _ = stopCapture()
Expand All @@ -428,23 +430,23 @@ extension DefaultAudioDevice {
isPlayerInterrupted = true
let _ = stopRendering()
}
case AVAudioSessionInterruptionType.ended.rawValue:
case AVAudioSession.InterruptionType.ended.rawValue:
configureAudioSessionWithDesiredAudioRoute(desiredAudioRoute: DefaultAudioDevice.kAudioDeviceBluetooth)
restartAudioAfterInterruption()
default:
break
}
}

func onRouteChangeEvent(notification: Notification) {
@objc func onRouteChangeEvent(notification: Notification) {
safetyQueue.async {
self.handleRouteChangeEvent(notification: notification)
}
}

func appDidBecomeActive(notification: Notification) {
@objc func appDidBecomeActive(notification: Notification) {
safetyQueue.async {
self.handleInterruptionEvent(type: Int(AVAudioSessionInterruptionType.ended.rawValue))
self.handleInterruptionEvent(type: Int(AVAudioSession.InterruptionType.ended.rawValue))
}
}

Expand All @@ -453,12 +455,12 @@ extension DefaultAudioDevice {
return
}

if reason == AVAudioSessionRouteChangeReason.routeConfigurationChange.rawValue {
if reason == AVAudioSession.RouteChangeReason.routeConfigurationChange.rawValue {
return
}

if reason == AVAudioSessionRouteChangeReason.override.rawValue ||
reason == AVAudioSessionRouteChangeReason.categoryChange.rawValue {
if reason == AVAudioSession.RouteChangeReason.override.rawValue ||
reason == AVAudioSession.RouteChangeReason.categoryChange.rawValue {

let oldRouteDesc = notification.userInfo?[AVAudioSessionRouteChangePreviousRouteKey] as! AVAudioSessionRouteDescription
let outputs = oldRouteDesc.outputs
Expand Down Expand Up @@ -490,13 +492,11 @@ extension DefaultAudioDevice {
let notificationCenter = NotificationCenter.default

notificationCenter.addObserver(self, selector: #selector(DefaultAudioDevice.onInterruptionEvent),
name: Notification.Name.AVAudioSessionInterruption, object: nil)

name: AVAudioSession.interruptionNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(DefaultAudioDevice.onRouteChangeEvent(notification:)),
name: Notification.Name.AVAudioSessionRouteChange, object: nil)

name: AVAudioSession.routeChangeNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(DefaultAudioDevice.appDidBecomeActive(notification:)),
name: Notification.Name.UIApplicationDidBecomeActive, object: nil)
name: UIApplication.didBecomeActiveNotification, object: nil)

areListenerBlocksSetup = true
}
Expand All @@ -516,10 +516,10 @@ extension DefaultAudioDevice {
do {
try session.setPreferredSampleRate(Double(DefaultAudioDevice.kSampleRate))
try session.setPreferredIOBufferDuration(0.01)
let audioOptions = AVAudioSessionCategoryOptions.mixWithOthers.rawValue |
AVAudioSessionCategoryOptions.allowBluetooth.rawValue |
AVAudioSessionCategoryOptions.defaultToSpeaker.rawValue
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVideoChat, options: AVAudioSessionCategoryOptions(rawValue: audioOptions))
let audioOptions = AVAudioSession.CategoryOptions.mixWithOthers.rawValue |
AVAudioSession.CategoryOptions.allowBluetooth.rawValue |
AVAudioSession.CategoryOptions.defaultToSpeaker.rawValue
try session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.videoChat, options: AVAudioSession.CategoryOptions(rawValue: audioOptions))
setupListenerBlocks()

try session.setActive(true)
Expand All @@ -534,7 +534,7 @@ extension DefaultAudioDevice {
// MARK: - Audio Route functions
extension DefaultAudioDevice {
fileprivate func setBluetoothAsPreferredInputDevice() {
let btRoutes = [AVAudioSessionPortBluetoothA2DP, AVAudioSessionPortBluetoothLE, AVAudioSessionPortBluetoothHFP]
let btRoutes = [AVAudioSession.Port.bluetoothA2DP, AVAudioSession.Port.bluetoothLE, AVAudioSession.Port.bluetoothHFP]
AVAudioSession.sharedInstance().availableInputs?.forEach({ el in
if btRoutes.contains(el.portType) {
do {
Expand All @@ -554,9 +554,9 @@ extension DefaultAudioDevice {
}
do {
if desiredAudioRoute == DefaultAudioDevice.kAudioDeviceSpeaker {
try session.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
try session.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
} else {
try session.overrideOutputAudioPort(AVAudioSessionPortOverride.none)
try session.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
}
} catch let err as NSError {
print("Error setting audio route: \(err)")
Expand Down
Expand Up @@ -293,7 +293,7 @@
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -308,7 +308,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.tokbox.Lets-Build-OTPublisher";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand Down
Expand Up @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
Expand Down
Expand Up @@ -10,7 +10,7 @@ import OpenTok
import AVFoundation

extension UIApplication {
func currentDeviceOrientation(cameraPosition pos: AVCaptureDevicePosition) -> OTVideoOrientation {
func currentDeviceOrientation(cameraPosition pos: AVCaptureDevice.Position) -> OTVideoOrientation {
let orientation = statusBarOrientation
if pos == .front {
switch orientation {
Expand All @@ -32,14 +32,14 @@ extension UIApplication {
}
}

extension String {
extension AVCaptureSession.Preset {
func dimensionForCapturePreset() -> (width: UInt32, height: UInt32) {
switch self {
case AVCaptureSessionPreset352x288: return (352, 288)
case AVCaptureSessionPreset640x480, AVCaptureSessionPresetHigh: return (640, 480)
case AVCaptureSessionPresetLow: return (192, 144)
case AVCaptureSessionPresetMedium: return (480, 360)
case AVCaptureSessionPreset1280x720: return (1280, 720)
case AVCaptureSession.Preset.cif352x288: return (352, 288)
case AVCaptureSession.Preset.vga640x480, AVCaptureSession.Preset.high: return (640, 480)
case AVCaptureSession.Preset.low: return (192, 144)
case AVCaptureSession.Preset.medium: return (480, 360)
case AVCaptureSession.Preset.hd1280x720: return (1280, 720)
default: return (352, 288)
}
}
Expand All @@ -58,7 +58,7 @@ class ExampleVideoCapture: NSObject, OTVideoCapture {

var delegate: FrameCapturerMetadataDelegate?

fileprivate var capturePreset: String {
fileprivate var capturePreset: AVCaptureSession.Preset {
didSet {
(captureWidth, captureHeight) = capturePreset.dimensionForCapturePreset()
}
Expand All @@ -72,7 +72,7 @@ class ExampleVideoCapture: NSObject, OTVideoCapture {
let captureQueue: DispatchQueue

override init() {
capturePreset = AVCaptureSessionPreset640x480
capturePreset = AVCaptureSession.Preset.vga640x480
captureQueue = DispatchQueue(label: "com.tokbox.VideoCapture", attributes: [])
(captureWidth, captureHeight) = capturePreset.dimensionForCapturePreset()
videoFrame = OTVideoFrame(format: OTVideoFormat(nv12WithWidth: captureWidth, height: captureHeight))
Expand All @@ -94,16 +94,24 @@ class ExampleVideoCapture: NSObject, OTVideoCapture {
}

videoInput = try AVCaptureDeviceInput(device: device)
guard let videoInput = self.videoInput else {
print("There was an error creating videoInput")
return
}
captureSession?.addInput(videoInput)

// Configure Ouput
videoOutput = AVCaptureVideoDataOutput()
videoOutput?.alwaysDiscardsLateVideoFrames = true
videoOutput?.videoSettings = [
kCVPixelBufferPixelFormatTypeKey as AnyHashable: Int(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)
kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)
]
videoOutput?.setSampleBufferDelegate(self, queue: captureQueue)

guard let videoOutput = self.videoOutput else {
print("There was an error creating videoOutput")
return
}
captureSession?.addOutput(videoOutput)
setFrameRate()
captureSession?.commitConfiguration()
Expand Down Expand Up @@ -141,8 +149,8 @@ class ExampleVideoCapture: NSObject, OTVideoCapture {

}

fileprivate func camera(withPosition pos: AVCaptureDevicePosition) -> AVCaptureDevice? {
return AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).filter({ ($0 as! AVCaptureDevice).position == pos }).first as? AVCaptureDevice
fileprivate func camera(withPosition pos: AVCaptureDevice.Position) -> AVCaptureDevice? {
return AVCaptureDevice.devices(for: AVMediaType.video).filter({ ($0 as! AVCaptureDevice).position == pos }).first as? AVCaptureDevice
}

fileprivate func updateCaptureFormat(width w: UInt32, height h: UInt32) {
Expand Down Expand Up @@ -205,20 +213,22 @@ class ExampleVideoCapture: NSObject, OTVideoCapture {
}

fileprivate var hasMultipleCameras : Bool {
return AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 1
return AVCaptureDevice.devices(for: AVMediaType.video).count > 1
}

func setCameraPosition(_ position: AVCaptureDevicePosition) -> Bool {
func setCameraPosition(_ position: AVCaptureDevice.Position) -> Bool {
guard let preset = captureSession?.sessionPreset else {
return false
}

let newVideoInput: AVCaptureDeviceInput? = {
do {
if position == AVCaptureDevicePosition.back {
return try AVCaptureDeviceInput.init(device: backFacingCamera())
} else if position == AVCaptureDevicePosition.front {
return try AVCaptureDeviceInput.init(device: frontFacingCamera())
if position == AVCaptureDevice.Position.back {
guard let backFacingCamera = backFacingCamera() else { return nil }
return try AVCaptureDeviceInput.init(device: backFacingCamera)
} else if position == AVCaptureDevice.Position.front {
guard let frontFacingCamera = backFacingCamera() else { return nil }
return try AVCaptureDeviceInput.init(device: frontFacingCamera)
} else {
return nil
}
Expand All @@ -235,6 +245,7 @@ class ExampleVideoCapture: NSObject, OTVideoCapture {

captureQueue.sync {
captureSession?.beginConfiguration()
guard var videoInput = videoInput else { return }
captureSession?.removeInput(videoInput)

if captureSession?.canAddInput(newInput) ?? false {
Expand Down
Expand Up @@ -50,29 +50,29 @@ class ExampleVideoRender: UIView {

NotificationCenter.default
.addObserver(self, selector: #selector(ExampleVideoRender.willResignActive),
name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default
.addObserver(self, selector: #selector(ExampleVideoRender.didBecomeActive),
name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
name: UIApplication.didBecomeActiveNotification, object: nil)


displayLinkProxy = DisplayLinkProxy(glkView: glkView!, videoRender: self)
displayLink = CADisplayLink(target: displayLinkProxy!, selector:#selector(DisplayLinkProxy.displayLinkDidFire(_:)))
displayLink!.frameInterval = 2
displayLink!.add(to: RunLoop.main, forMode: RunLoopMode.commonModes)
displayLink!.add(to: RunLoop.main, forMode: RunLoop.Mode.common)

renderer!.setupGL()

displayLink!.isPaused = false
}

func willResignActive() {
@objc func willResignActive() {
displayLink!.isPaused = true
glkView?.deleteDrawable()
renderer!.teardownGL()
}

func didBecomeActive() {
@objc func didBecomeActive() {
renderer!.setupGL()
displayLink!.isPaused = false
}
Expand Down
4 changes: 2 additions & 2 deletions FrameMetadata/FrameMetadata.xcodeproj/project.pbxproj
Expand Up @@ -295,7 +295,7 @@
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -313,7 +313,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand Down

0 comments on commit fad6908

Please sign in to comment.