-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Just one capture session per input component. - Avoids starting/stoping capture during collection reloads, which can make the live camera take longer to capture or even hang. - Moves creation/destruction of capture session to the background: when opening/closing the input component many times it was resulting in capture start / stop blocking the main thread for some seconds on iOS 8 - Queues updates in the collection view: doing a reloadData right after reloadItemsAtIndexPaths (update video) results in the visible cells not being refreshed Todo: tests
- Loading branch information
1 parent
25b32bb
commit 4e2dfd2
Showing
10 changed files
with
640 additions
and
454 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
ChattoAdditions/Source/Input/Photos/LiveCameraCaptureSession.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015-present Badoo Trading Limited. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
import Foundation | ||
import Photos | ||
|
||
class LiveCameraCaptureSession: LiveCameraCaptureSessionProtocol { | ||
|
||
private enum OperationType: String { | ||
case start | ||
case stop | ||
} | ||
|
||
var isInitialized: Bool = false | ||
|
||
var isCapturing: Bool { | ||
return self.isInitialized && self.captureSession.running | ||
} | ||
|
||
deinit { | ||
var layer = self.captureLayer | ||
var session: AVCaptureSession? = self.captureSession | ||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { | ||
// Analogously to AVCaptureSession creation, dealloc can take very long, so let's do it out of the main thread | ||
if layer != nil { layer = nil } | ||
if session != nil { session = nil } | ||
} | ||
} | ||
|
||
func startCapturing(completion: () -> Void) { | ||
let operation = NSBlockOperation() | ||
operation.addExecutionBlock { [weak operation, weak self] in | ||
guard let strongSelf = self, strongOperation = operation else { return } | ||
if !strongOperation.cancelled && !strongSelf.captureSession.running { | ||
strongSelf.captureSession.startRunning() | ||
NSOperationQueue.mainQueue().addOperationWithBlock(completion) | ||
} | ||
} | ||
self.queue.cancelOperation(forKey: "stopCapturingOperation") | ||
self.queue.addOperation(operation, forKey: "startCapturingOperation") | ||
} | ||
|
||
func stopCapturing(completion: () -> Void) { | ||
let operation = NSBlockOperation() | ||
operation.addExecutionBlock { [weak operation, weak self] in | ||
guard let strongSelf = self, strongOperation = operation else { return } | ||
if !strongOperation.cancelled && strongSelf.captureSession.running { | ||
strongSelf.captureSession.stopRunning() | ||
NSOperationQueue.mainQueue().addOperationWithBlock(completion) | ||
} | ||
} | ||
self.queue.cancelOperation(forKey: "startCapturingOperation") | ||
self.queue.addOperation(operation, forKey: "stopCapturingOperation") | ||
} | ||
|
||
private (set) var captureLayer: AVCaptureVideoPreviewLayer? | ||
|
||
private lazy var queue: KeyedOperationQueue = { | ||
let queue = KeyedOperationQueue() | ||
queue.qualityOfService = .UserInitiated | ||
queue.maxConcurrentOperationCount = 1 | ||
return queue | ||
}() | ||
|
||
private lazy var captureSession: AVCaptureSession = { | ||
assert(!NSThread.isMainThread(), "This can be very slow, make sure it happens in a background thread") | ||
let session = AVCaptureSession() | ||
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) | ||
do { | ||
let input = try AVCaptureDeviceInput(device: device) | ||
session.addInput(input) | ||
} catch { | ||
|
||
} | ||
self.captureLayer = AVCaptureVideoPreviewLayer(session: session) | ||
self.captureLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill | ||
self.isInitialized = true | ||
return session | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.