Commit b957aa1e by Daniel Dahan

Capture is born. Working CaptureSession and CapturePreviewView

parent 47e5a079
...@@ -21,11 +21,9 @@ import AVFoundation ...@@ -21,11 +21,9 @@ import AVFoundation
public class CapturePreviewView : MaterialView { public class CapturePreviewView : MaterialView {
/** /**
:name: layerClass :name: previewLayer
*/ */
public override class func layerClass() -> AnyClass { public private(set) lazy var previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer()
return AVCaptureVideoPreviewLayer.self
}
/** /**
:name: capture :name: capture
...@@ -33,31 +31,51 @@ public class CapturePreviewView : MaterialView { ...@@ -33,31 +31,51 @@ public class CapturePreviewView : MaterialView {
public private(set) lazy var captureSession: CaptureSession = CaptureSession() public private(set) lazy var captureSession: CaptureSession = CaptureSession()
/** /**
:name: layoutSublayersOfLayer
*/
public override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
if self.layer == layer {
layoutPreviewLayer()
}
}
/**
:name: prepareView :name: prepareView
*/ */
public override func prepareView() { public override func prepareView() {
super.prepareView() super.prepareView()
prepareSession() preparePreviewLayer()
} }
/** /**
:name: captureDevicePointOfInterestForPoint :name: captureDevicePointOfInterestForPoint
*/ */
public func captureDevicePointOfInterestForPoint(point: CGPoint) -> CGPoint { public func captureDevicePointOfInterestForPoint(point: CGPoint) -> CGPoint {
return (layer as! AVCaptureVideoPreviewLayer).captureDevicePointOfInterestForPoint(point) return previewLayer.captureDevicePointOfInterestForPoint(point)
} }
/** /**
:name: pointForCaptureDevicePointOfInterest :name: pointForCaptureDevicePointOfInterest
*/ */
public func pointForCaptureDevicePointOfInterest(point: CGPoint) -> CGPoint { public func pointForCaptureDevicePointOfInterest(point: CGPoint) -> CGPoint {
return (layer as! AVCaptureVideoPreviewLayer).pointForCaptureDevicePointOfInterest(point) return previewLayer.pointForCaptureDevicePointOfInterest(point)
}
//
// :name: preparePreviewLayer
//
private func preparePreviewLayer() {
previewLayer.session = captureSession.session
visualLayer.addSublayer(previewLayer)
} }
// //
// :name: prepareSession // :name: layoutPreviewLayer
// //
private func prepareSession() { internal func layoutPreviewLayer() {
(layer as! AVCaptureVideoPreviewLayer).session = captureSession.session previewLayer.frame = visualLayer.bounds
previewLayer.position = CGPointMake(width / 2, height / 2)
previewLayer.cornerRadius = visualLayer.cornerRadius
} }
} }
\ No newline at end of file
...@@ -19,6 +19,20 @@ ...@@ -19,6 +19,20 @@
import UIKit import UIKit
import AVFoundation import AVFoundation
public enum CaptureSessionPreset {
case High
}
/**
:name: CaptureSessionPresetToString
*/
public func CaptureSessionPresetToString(preset: CaptureSessionPreset) -> String {
switch preset {
case .High:
return AVCaptureSessionPresetHigh
}
}
@objc(CaptureSessionDelegate) @objc(CaptureSessionDelegate)
public protocol CaptureSessionDelegate { public protocol CaptureSessionDelegate {
/** /**
...@@ -64,10 +78,10 @@ public class CaptureSession : NSObject { ...@@ -64,10 +78,10 @@ public class CaptureSession : NSObject {
// //
private lazy var movieOutput: AVCaptureMovieFileOutput = AVCaptureMovieFileOutput() private lazy var movieOutput: AVCaptureMovieFileOutput = AVCaptureMovieFileOutput()
/** //
:name: session // :name: session
*/ //
public private(set) lazy var session: AVCaptureSession = AVCaptureSession() internal lazy var session: AVCaptureSession = AVCaptureSession()
/** /**
:name: isRunning :name: isRunning
...@@ -75,6 +89,50 @@ public class CaptureSession : NSObject { ...@@ -75,6 +89,50 @@ public class CaptureSession : NSObject {
public private(set) lazy var isRunning: Bool = false public private(set) lazy var isRunning: Bool = false
/** /**
:name: activeCamera
*/
public var activeCamera: AVCaptureDevice? {
return videoInput?.device
}
/**
:name: init
*/
public override init() {
super.init()
prepareSession()
}
/**
:name: inactiveCamera
*/
public var inactiveCamera: AVCaptureDevice? {
var device: AVCaptureDevice?
if 1 < cameraCount {
if activeCamera?.position == .Back {
device = cameraWithPosition(.Front)
} else if activeCamera?.position == .Front {
device = cameraWithPosition(.Back)
}
}
return device
}
/**
:name: cameraCount
*/
public var cameraCount: Int {
return AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo).count
}
/**
:name: canSwitchCameras
*/
public var canSwitchCameras: Bool {
return 1 < cameraCount
}
/**
:name: delegate :name: delegate
*/ */
public weak var delegate: CaptureSessionDelegate? public weak var delegate: CaptureSessionDelegate?
...@@ -82,7 +140,7 @@ public class CaptureSession : NSObject { ...@@ -82,7 +140,7 @@ public class CaptureSession : NSObject {
/** /**
:name: startSession :name: startSession
*/ */
public func startSesstion() { public func startSession() {
if !isRunning { if !isRunning {
dispatch_async(videoQueue) { dispatch_async(videoQueue) {
self.session.startRunning() self.session.startRunning()
...@@ -93,7 +151,7 @@ public class CaptureSession : NSObject { ...@@ -93,7 +151,7 @@ public class CaptureSession : NSObject {
/** /**
:name: startSession :name: startSession
*/ */
public func stopSesstion() { public func stopSession() {
if isRunning { if isRunning {
dispatch_async(videoQueue) { dispatch_async(videoQueue) {
self.session.stopRunning() self.session.stopRunning()
...@@ -101,12 +159,40 @@ public class CaptureSession : NSObject { ...@@ -101,12 +159,40 @@ public class CaptureSession : NSObject {
} }
} }
/**
:name: sessionPreset
*/
public func sessionPreset(preset: CaptureSessionPreset) {
session.sessionPreset = CaptureSessionPresetToString(preset)
}
/**
:name: switchCameras
*/
public func switchCameras() -> Bool {
if canSwitchCameras {
do {
let vi: AVCaptureDeviceInput? = try AVCaptureDeviceInput(device: inactiveCamera!)
if session.canAddInput(vi) {
session.beginConfiguration()
session.removeInput(videoInput)
session.addInput(vi)
videoInput = vi
session.commitConfiguration()
return true
}
} catch let e as NSError {
delegate?.captureSessionFailedWithError?(self, error: e)
}
}
return false
}
// //
// :name: prepareSession // :name: prepareSession
// //
private func prepareSession() { private func prepareSession() {
session.sessionPreset = AVCaptureSessionPresetHigh sessionPreset(.High)
prepareVideoInput() prepareVideoInput()
prepareAudioInput() prepareAudioInput()
prepareImageOutput() prepareImageOutput()
...@@ -159,4 +245,17 @@ public class CaptureSession : NSObject { ...@@ -159,4 +245,17 @@ public class CaptureSession : NSObject {
session.addOutput(movieOutput) session.addOutput(movieOutput)
} }
} }
//
// :name: cameraWithPosition
//
private func cameraWithPosition(position: AVCaptureDevicePosition) -> AVCaptureDevice? {
let devices: Array<AVCaptureDevice> = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! Array<AVCaptureDevice>
for device in devices {
if device.position == position {
return device
}
}
return nil
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment